func startServer(c *cli.Context) error {
	var err error
	if err = serverEnvironmentSetup(c); err != nil {
		return err
	}
	defer environmentCleanup()

	root := c.String("root")
	var server *Server
	if !util.ConfigExists(root, getCfgName()) {
		server, err = initServer(c)
		if err != nil {
			return err
		}
	} else {
		server, err = loadServerConfig(c)
		if err != nil {
			return err
		}
	}
	server.GlobalLock = &sync.RWMutex{}
	defer server.cleanup()

	if err := server.CheckEnvironment(); err != nil {
		return err
	}

	server.Router = createRouter(server)

	if err := util.MkdirIfNotExists(filepath.Dir(sockFile)); err != nil {
		return err
	}

	l, err := net.Listen("unix", sockFile)
	if err != nil {
		fmt.Println("listen err", err)
		return err
	}
	defer l.Close()

	sigs := make(chan os.Signal, 1)
	done := make(chan bool, 1)
	signal.Notify(sigs, os.Interrupt, os.Kill, syscall.SIGTERM)
	go func() {
		sig := <-sigs
		fmt.Printf("Caught signal %s: shutting down.\n", sig)
		done <- true
	}()

	go func() {
		err = http.Serve(l, server.Router)
		if err != nil {
			log.Error("http server error", err.Error())
		}
		done <- true
	}()

	<-done
	return nil
}
Exemple #2
0
func initFunc(root, cfgName string, config map[string]string) (objectstore.ObjectStoreDriver, error) {
	b := &S3ObjectStoreDriver{}
	if cfgName != "" {
		if util.ConfigExists(root, cfgName) {
			err := util.LoadConfig(root, cfgName, b)
			if err != nil {
				return nil, err
			}
			return b, nil
		} else {
			return nil, fmt.Errorf("Wrong configuration file for S3 objectstore driver")
		}
	}

	b.Service.Keys.AccessKey = config[S3_ACCESS_KEY]
	b.Service.Keys.SecretKey = config[S3_SECRET_KEY]
	b.Service.Region = config[S3_REGION]
	b.Service.Bucket = config[S3_BUCKET]
	b.Path = config[S3_PATH]
	if b.Service.Keys.AccessKey == "" || b.Service.Keys.SecretKey == "" ||
		b.Service.Region == "" || b.Service.Bucket == "" || b.Path == "" {
		return nil, fmt.Errorf("Cannot find all required fields: %v %v %v %v %v",
			S3_ACCESS_KEY, S3_SECRET_KEY, S3_REGION, S3_BUCKET, S3_PATH)
	}

	if strings.HasPrefix(b.Path, "/") {
		return nil, fmt.Errorf("Slash '/' is not allowed at beginning of path: %v", b.Path)
	}

	//Test connection
	if _, err := b.List(""); err != nil {
		return nil, err
	}
	return b, nil
}
func (config *Config) loadVolume(uuid string) *Volume {
	cfgName, err := getVolumeCfgName(uuid)
	if err != nil {
		return nil
	}
	if !util.ConfigExists(config.Root, cfgName) {
		return nil
	}
	volume := &Volume{}
	if err := util.LoadConfig(config.Root, cfgName, volume); err != nil {
		log.Error("Failed to load volume json ", cfgName)
		return nil
	}
	return volume
}
func (device *Device) loadImage(uuid string) *Image {
	if uuid == "" {
		return nil
	}
	cfgName, err := getImageCfgName(uuid)
	if err != nil {
		return nil
	}
	if !util.ConfigExists(device.Root, cfgName) {
		return nil
	}
	image := &Image{}
	if err := util.LoadConfig(device.Root, cfgName, image); err != nil {
		log.Error("Failed to load volume json ", cfgName)
		return nil
	}
	return image
}
Exemple #5
0
func initFunc(root, cfgName string, config map[string]string) (objectstore.ObjectStoreDriver, error) {
	b := &VfsObjectStoreDriver{}
	if cfgName != "" {
		if util.ConfigExists(root, cfgName) {
			err := util.LoadConfig(root, cfgName, b)
			if err != nil {
				return nil, err
			}
			return b, nil
		} else {
			return nil, fmt.Errorf("Wrong configuration file for VFS objectstore driver")
		}
	}

	//return temporily driver for loading objectstore config
	b.Path = config[VFS_PATH]
	if b.Path == "" {
		return nil, fmt.Errorf("Cannot find required field %v", VFS_PATH)
	}
	if _, err := b.List(""); err != nil {
		return nil, fmt.Errorf("VFS path %v doesn't exist or is not a directory", b.Path)
	}
	return b, nil
}
func Init(root, cfgName string, config map[string]string) (drivers.Driver, error) {
	devicemapper.LogInitVerbose(1)
	devicemapper.LogInit(&DMLogger{})

	if supported := devicemapper.UdevSetSyncSupport(true); !supported {
		return nil, fmt.Errorf("Udev sync is not supported. Cannot proceed.")
	}
	if util.ConfigExists(root, cfgName) {
		dev := Device{}
		err := util.LoadConfig(root, cfgName, &dev)
		d := &Driver{
			Mutex: &sync.Mutex{},
		}
		if err != nil {
			return d, err
		}
		d.Device = dev
		d.configName = cfgName
		d.root = root
		if err := d.activatePool(); err != nil {
			return d, err
		}
		return d, nil
	}

	dev, err := verifyConfig(config)
	if err != nil {
		return nil, err
	}

	dev.Root = root

	dataDev, err := os.Open(dev.DataDevice)
	if err != nil {
		return nil, err
	}
	defer dataDev.Close()

	metadataDev, err := os.Open(dev.MetadataDevice)
	if err != nil {
		return nil, err
	}
	defer metadataDev.Close()

	thinpSize, err := devicemapper.GetBlockDeviceSize(dataDev)
	if err != nil {
		return nil, err
	}
	dev.ThinpoolSize = int64(thinpSize)
	dev.LastDevID = 0

	err = createPool(filepath.Base(dev.ThinpoolDevice), dataDev, metadataDev, uint32(dev.ThinpoolBlockSize))
	if err != nil {
		return nil, err
	}

	err = util.SaveConfig(root, cfgName, &dev)
	if err != nil {
		return nil, err
	}
	d := &Driver{
		root:       root,
		configName: cfgName,
		Device:     *dev,
		Mutex:      &sync.Mutex{},
	}
	log.Debug("Init done")
	return d, nil
}
func initServer(c *cli.Context) (*Server, error) {
	root := c.String("root")
	driverName := c.String("driver")
	driverOpts := util.SliceToMap(c.StringSlice("driver-opts"))
	imagesDir := c.String("images-dir")
	mountsDir := c.String("mounts-dir")
	defaultSize := c.String("default-volume-size")
	if root == "" || driverName == "" || driverOpts == nil || imagesDir == "" || mountsDir == "" || defaultSize == "" {
		return nil, fmt.Errorf("Missing or invalid parameters")
	}

	size, err := util.ParseSize(defaultSize)
	if err != nil {
		return nil, err
	}

	log.Debug("Config root is ", root)

	if util.ConfigExists(root, getCfgName()) {
		return nil, fmt.Errorf("Configuration file already existed. Don't need to initialize.")
	}

	if err := util.MkdirIfNotExists(imagesDir); err != nil {
		return nil, err
	}
	log.Debug("Images would be stored at ", imagesDir)

	if err := util.MkdirIfNotExists(mountsDir); err != nil {
		return nil, err
	}
	log.Debug("Default mounting directory would be ", mountsDir)

	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON: LOG_REASON_PREPARE,
		LOG_FIELD_EVENT:  LOG_EVENT_INIT,
		LOG_FIELD_DRIVER: driverName,
		"root":           root,
		"driverOpts":     driverOpts,
	}).Debug()
	driver, err := drivers.GetDriver(driverName, root, driverOpts)
	if err != nil {
		return nil, err
	}

	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON: LOG_REASON_COMPLETE,
		LOG_FIELD_EVENT:  LOG_EVENT_INIT,
		LOG_FIELD_DRIVER: driverName,
	}).Debug()

	config := Config{
		Root:              root,
		Driver:            driverName,
		ImagesDir:         imagesDir,
		MountsDir:         mountsDir,
		DefaultVolumeSize: size,
	}
	server := &Server{
		Config:        config,
		StorageDriver: driver,
		NameVolumeMap: make(map[string]string),
	}
	err = util.SaveConfig(root, getCfgName(), &config)
	return server, err
}