Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #127 from imikushin/attach
Browse files Browse the repository at this point in the history
Attach managed volumes to the host before starting container
  • Loading branch information
ibuildthecloud authored Jan 27, 2017
2 parents f8e2812 + 9204f7a commit e4a72fb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/compute/compute_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,19 @@ func setupVolumes(config *container.Config, instance model.Instance, hostConfig
if vMounts := instance.VolumesFromDataVolumeMounts; len(vMounts) > 0 {
for _, vMount := range vMounts {
storagePool := model.StoragePool{}
// volume active == exists, possibly not attached to this host
if ok, err := storage.IsVolumeActive(vMount, storagePool, client); !ok && err == nil {
if err := storage.DoVolumeActivate(vMount, storagePool, progress, client); err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to activate volume")
}
} else if err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to check whether volume is activated")
}
if storage.IsRancherVolume(vMount) {
if err := storage.RancherStorageVolumeAttach(vMount); err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to attach volume")
}
}
}
}
return nil
Expand Down
46 changes: 46 additions & 0 deletions core/storage/plugin_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// +build linux freebsd solaris openbsd darwin

package storage

import (
"bytes"
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/docker/go-connections/sockets"
"github.com/pkg/errors"
"github.com/rancher/agent/model"
"net/http"
)

func callRancherStorageVolumeAttach(volume model.Volume) error {
transport := new(http.Transport)
sockets.ConfigureTransport(transport, "unix", rancherStorageSockPath(volume))
client := &http.Client{
Transport: transport,
}
url := "http://volume-plugin/VolumeDriver.Attach"

bs, err := json.Marshal(struct{ Name string }{Name: volume.Name})
if err != nil {
return errors.Wrap(err, "Failed to marshal JSON")
}

driver := volume.Data.Fields.Driver
resp, err := client.Post(url, "application/json", bytes.NewReader(bs))
if err != nil {
logrus.Errorf("Failed to call /VolumeDriver.Attach '%s' (driver '%s'): %s", volume.Name, driver, err)
return err
}
defer resp.Body.Close()

switch {
case resp.StatusCode >= 200 && resp.StatusCode < 300:
logrus.Infof("Success: /VolumeDriver.Attach '%s' (driver '%s')", volume.Name, driver)
case resp.StatusCode >= 400 && resp.StatusCode < 500:
logrus.Infof("/VolumeDriver.Attach '%s' is not supported by driver '%s'", volume.Name, driver)
default:
return errors.Errorf("/VolumeDriver.Attach '%s' (driver '%s') returned status %v: %s", volume.Name, driver, resp.StatusCode, resp.Status)
}

return nil
}
6 changes: 6 additions & 0 deletions core/storage/plugin_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package storage

// callRancherStorageVolumeAttach is not supported on windows
func callRancherStorageVolumeAttach(volume model.Volume) error {
return nil
}
7 changes: 7 additions & 0 deletions core/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func DoVolumeActivate(volume model.Volume, storagePool model.StoragePool, progre
return nil
}

func RancherStorageVolumeAttach(volume model.Volume) error {
if err := callRancherStorageVolumeAttach(volume); err != nil {
return errors.Wrap(err, constants.RancherStorageVolumeAttachError+"failed to attach volume")
}
return nil
}

func PullImage(image model.Image, progress *progress.Progress, client *engineCli.Client, imageUUID string) error {
dockerCli := docker.GetClient(docker.DefaultVersion)
return DoImageActivate(image, model.StoragePool{}, progress, dockerCli, imageUUID)
Expand Down
20 changes: 20 additions & 0 deletions core/storage/storage_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"path/filepath"
)

const (
rancherSockDir = "/var/run/rancher/storage"
)

func IsVolumeActive(volume model.Volume, storagePool model.StoragePool, dockerClient *client.Client) (bool, error) {
Expand All @@ -27,6 +32,21 @@ func IsVolumeActive(volume model.Volume, storagePool model.StoragePool, dockerCl
return true, nil
}

func rancherStorageSockPath(volume model.Volume) string {
return filepath.Join(rancherSockDir, volume.Data.Fields.Driver+".sock")
}

func IsRancherVolume(volume model.Volume) bool {
if !isManagedVolume(volume) {
return false
}
sockFile := rancherStorageSockPath(volume)
if _, err := os.Stat(sockFile); err == nil {
return true
}
return false
}

func IsImageActive(image model.Image, storagePool model.StoragePool, dockerClient *client.Client) (bool, error) {
if utils.IsImageNoOp(image) {
return true, nil
Expand Down
1 change: 1 addition & 0 deletions utilities/constants/constants_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const DoVolumeRemoveError = "DoVolumeRemove: "
const IsVolumeRemovedError = "IsVolumeRemoved: "
const IsVolumeActiveError = "IsVolumeActive: "
const DoVolumeActivateError = "DoVolumeActivate: "
const RancherStorageVolumeAttachError = "RancherStorageVolumeAttach: "
const DoBuildError = "DoBuild: "
const IsImageActiveError = "IsImageActive: "
const GetResponseDataError = "GetResponseData: "
Expand Down

0 comments on commit e4a72fb

Please sign in to comment.