Beispiel #1
0
func getPath(master string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Name is empty", nil)
			return
		}

		log.Infof("Returning mount path to docker for volume: %q", vr.Name)

		pool, name, err := splitPath(vr.Name)
		if err != nil {
			httpError(w, "Configuring volume", err)
			return
		}

		// FIXME need to ensure that the mount exists before returning to docker
		driver := cephdriver.NewCephDriver()

		content, err := marshalResponse(VolumeResponse{Mountpoint: driver.MountPath(pool, name)})
		if err != nil {
			httpError(w, "Reply could not be marshalled", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #2
0
func getPath(master, tenantName string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Name is empty", nil)
			return
		}

		log.Infof("Returning mount path to docker for volume: %q", vr.Name)

		config, err := requestTenantConfig(master, tenantName, vr.Name)
		if err != nil {
			httpError(w, "Could not determine tenant configuration", err)
			return
		}

		driver := cephdriver.NewCephDriver(config.Pool)

		content, err := marshalResponse(VolumeResponse{Mountpoint: driver.MountPath(vr.Name)})
		if err != nil {
			httpError(w, "Reply could not be marshalled", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #3
0
func wrapSnapshotAction(config config, action func(config config, tenant string, volume *cephdriver.CephVolume)) {
	for tenant, value := range config {
		mutex.Lock()
		duration, err := time.ParseDuration(config[tenant].Snapshot.Frequency)
		if err != nil {
			panic(fmt.Sprintf("Runtime configuration incorrect; cannot use %q as a snapshot frequency", config[tenant].Snapshot.Frequency))
		}

		if value.UseSnapshots && time.Now().Unix()%int64(duration.Seconds()) == 0 {
			for _, volumes := range volumeMap {
				rbdConfig, err := librbd.ReadConfig("/etc/rbdconfig.json")
				if err != nil {
					log.Errorf("Cannot read RBD configuration: %v", err)
					break
				}
				driver, err := cephdriver.NewCephDriver(rbdConfig, config[tenant].Pool)
				if err != nil {
					log.Errorf("Cannot snap volumes for tenant %q: %v", tenant, err)
					break
				}
				for volName := range volumes {
					volume := driver.NewVolume(volName, config[tenant].Size)
					action(config, tenant, volume)
				}
			}
		}
		mutex.Unlock()
	}
}
Beispiel #4
0
func runSnapshot(config *config.TopLevelConfig, pool string, volume *config.VolumeConfig) {
	now := time.Now()
	cephVol := cephdriver.NewCephDriver().NewVolume(pool, strings.Join([]string{volume.TenantName, volume.VolumeName}, "."), volume.Options.Size)
	log.Infof("Snapping volume %q at %v", volume, now)
	if err := cephVol.CreateSnapshot(now.String()); err != nil {
		log.Errorf("Cannot snap volume: %q: %v", volume.VolumeName, err)
	}
}
Beispiel #5
0
func createImage(config configTenant, name string, size uint64) error {
	driver := cephdriver.NewCephDriver(config.Pool)

	if err := driver.NewVolume(name, config.Size).Create(); err != nil {
		return err
	}

	return nil
}
Beispiel #6
0
func mount(master, host string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Name is empty", nil)
			return
		}

		// FIXME check if we're holding the mount already
		log.Infof("Mounting volume %q", vr.Name)

		tenant, name, err := splitPath(vr.Name)
		if err != nil {
			httpError(w, "Configuring volume", err)
			return
		}

		volConfig, err := requestVolumeConfig(master, tenant, name)
		if err != nil {
			httpError(w, "Could not determine tenant configuration", err)
			return
		}

		driver := cephdriver.NewCephDriver()

		mt := &config.MountConfig{
			Volume:     volConfig.VolumeName,
			Pool:       volConfig.Options.Pool,
			MountPoint: driver.MountPath(volConfig.Options.Pool, name),
			Host:       host,
		}

		if err := reportMount(master, mt); err != nil {
			httpError(w, "Reporting mount to master", err)
			return
		}

		if err := driver.NewVolume(volConfig.Options.Pool, name, volConfig.Options.Size).Mount(volConfig.Options.FileSystem); err != nil {
			httpError(w, "Volume could not be mounted", err)
			return
		}

		content, err := marshalResponse(VolumeResponse{Mountpoint: driver.MountPath(volConfig.Options.Pool, name)})
		if err != nil {
			httpError(w, "Reply could not be marshalled", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #7
0
func createImage(tenant *config.TenantConfig, config *config.VolumeConfig) error {
	var (
		fscmd string
		ok    bool
	)

	if tenant.FileSystems == nil {
		fscmd = defaultFsCmd
	} else {
		fscmd, ok = tenant.FileSystems[config.Options.FileSystem]
		if !ok {
			return fmt.Errorf("Invalid filesystem %q", config.Options.FileSystem)
		}
	}

	return cephdriver.NewCephDriver().NewVolume(config.Options.Pool, joinVolumeName(config), config.Options.Size).Create(fscmd)
}
Beispiel #8
0
func create(tenantName string, rbdConfig librbd.RBDConfig) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Image name is empty", nil)
			return
		}

		config, err := requestTenantConfig(tenantName, vr.Name)
		if err != nil {
			httpError(w, "Could not determine tenant configuration", err)
			return
		}

		driver, err := cephdriver.NewCephDriver(rbdConfig, config.Pool)
		if err != nil {
			httpError(w, "Error creating ceph driver", err)
			return
		}

		vol := driver.NewVolume(vr.Name, config.Size)

		log.Infof("Creating volume with parameters: %v", vol)

		if err := vol.Create(); err != nil {
			httpError(w, "Could not make new image", err)
			return
		}

		content, err := marshalResponse(VolumeResponse{Mountpoint: vr.Name, Err: ""})
		if err != nil {
			httpError(w, "Could not marshal response", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #9
0
func unmount(tenantName string, rbdConfig librbd.RBDConfig) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Name is empty", nil)
			return
		}

		log.Infof("Unmounting volume %q", vr.Name)

		config, err := requestTenantConfig(tenantName, vr.Name)
		if err != nil {
			httpError(w, "Could not determine tenant configuration", err)
			return
		}

		driver, err := cephdriver.NewCephDriver(rbdConfig, config.Pool)
		if err != nil {
			httpError(w, "Error creating ceph driver", err)
			return
		}

		if err := driver.NewVolume(vr.Name, config.Size).Unmount(); err != nil {
			httpError(w, "Could not mount image", err)
			return
		}

		content, err := marshalResponse(VolumeResponse{Mountpoint: driver.MountPath(vr.Name)})
		if err != nil {
			httpError(w, "Reply could not be marshalled", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #10
0
func runSnapshotPrune(config *config.TopLevelConfig, pool string, volume *config.VolumeConfig) {
	cephVol := cephdriver.NewCephDriver().NewVolume(pool, strings.Join([]string{volume.TenantName, volume.VolumeName}, "."), volume.Options.Size)
	log.Debugf("starting snapshot prune for %q", volume.VolumeName)
	list, err := cephVol.ListSnapshots()
	if err != nil {
		log.Errorf("Could not list snapshots for volume %q", volume)
		return
	}

	toDeleteCount := len(list) - int(volume.Options.Snapshot.Keep)
	if toDeleteCount < 0 {
		return
	}

	for i := 0; i < toDeleteCount; i++ {
		log.Infof("Removing snapshot %q for  volume %q", list[i], volume)
		if err := cephVol.RemoveSnapshot(list[i]); err != nil {
			log.Errorf("Removing snapshot %q for volume %q failed: %v", list[i], volume, err)
		}
	}
}
Beispiel #11
0
func removeImage(config *config.VolumeConfig) error {
	return cephdriver.NewCephDriver().NewVolume(config.Options.Pool, joinVolumeName(config), 0).Remove()
}
Beispiel #12
0
func unmount(master string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vr, err := unmarshalRequest(r.Body)
		if err != nil {
			httpError(w, "Could not unmarshal request", err)
			return
		}

		if vr.Name == "" {
			httpError(w, "Name is empty", nil)
			return
		}

		log.Infof("Unmounting volume %q", vr.Name)

		tenant, name, err := splitPath(vr.Name)
		if err != nil {
			httpError(w, "Configuring volume", err)
			return
		}

		volConfig, err := requestVolumeConfig(master, tenant, name)
		if err != nil {
			httpError(w, "Could not determine tenant configuration", err)
			return
		}

		driver := cephdriver.NewCephDriver()

		if err := driver.NewVolume(volConfig.Options.Pool, joinPath(tenant, name), volConfig.Options.Size).Unmount(); err != nil {
			httpError(w, "Could not unmount image", err)
			return
		}

		hostname, err := os.Hostname()
		if err != nil {
			httpError(w, "Retrieving hostname", err)
			return
		}

		mt := &config.MountConfig{
			Volume:     name,
			MountPoint: driver.MountPath(volConfig.Options.Pool, joinPath(tenant, name)),
			Pool:       volConfig.Options.Pool,
			Host:       hostname,
		}

		if err := reportUnmount(master, mt); err != nil {
			httpError(w, "Reporting unmount to master", err)
			return
		}

		content, err := marshalResponse(VolumeResponse{Mountpoint: driver.MountPath(volConfig.Options.Pool, joinPath(tenant, name))})
		if err != nil {
			httpError(w, "Reply could not be marshalled", err)
			return
		}

		w.Write(content)
	}
}
Beispiel #13
0
func createImage(config *config.TenantConfig, pool, name string) error {
	return cephdriver.NewCephDriver().NewVolume(pool, name, config.Size).Create()
}
Beispiel #14
0
func removeImage(pool, name string) error {
	return cephdriver.NewCephDriver().NewVolume(pool, name, 0).Remove()
}