func()

in ecs-init/volumes/ecs_volume_plugin.go [128:198]


func (a *AmazonECSVolumePlugin) Create(r *volume.CreateRequest) error {
	a.lock.Lock()
	defer a.lock.Unlock()

	seelog.Infof("Creating new volume %s", r.Name)
	_, ok := a.volumes[r.Name]
	if ok {
		return fmt.Errorf("volume %s already exists", r.Name)
	}

	// get driver type from options to get the corresponding volume driver
	var driverType, target string
	for k, v := range r.Options {
		switch k {
		case "type":
			driverType = v
		case "target":
			target = v
		}
	}
	volDriver, err := a.getVolumeDriver(driverType)
	if err != nil {
		seelog.Errorf("Volume %s's driver type %s not supported", r.Name, driverType)
		return err
	}
	if volDriver == nil {
		// this case should not happen normally
		return fmt.Errorf("no volume driver found for type %s", driverType)
	}

	if target == "" {
		seelog.Infof("Creating mount target for new volume %s", r.Name)
		// create the mount path on the host for the volume to be created
		target, err = a.GetMountPath(r.Name)
		if err != nil {
			seelog.Errorf("Volume %s creation failure: %v", r.Name, err)
			return err
		}
	}

	req := &CreateRequest{
		Name:    r.Name,
		Path:    target,
		Options: r.Options,
	}
	err = volDriver.Create(req)
	if err != nil {
		seelog.Errorf("Volume %s creation failure: %v", r.Name, err)
		cErr := a.CleanupMountPath(target)
		if cErr != nil {
			seelog.Warnf("Failed to cleanup mount path for volume %s: %v", r.Name, cErr)
		}
		return err
	}
	seelog.Infof("Volume %s created successfully", r.Name)
	vol := &Volume{
		Type:      driverType,
		Path:      target,
		Options:   r.Options,
		CreatedAt: time.Now().Format(time.RFC3339Nano),
	}
	// record the volume information
	a.volumes[r.Name] = vol
	seelog.Infof("Saving state of new volume %s", r.Name)
	// save the state of new volume
	err = a.state.recordVolume(r.Name, vol)
	if err != nil {
		seelog.Errorf("Error saving state of new volume %s: %v", r.Name, err)
	}
	return nil
}