func getSize(c *cli.Context, err error) (int64, error) {
	size, err := getLowerCaseFlag(c, "size", false, err)
	if err != nil {
		return 0, err
	}
	if size == "" {
		return 0, nil
	}
	return util.ParseSize(size)
}
Example #2
0
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
}