示例#1
0
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
}
示例#2
0
func serverEnvironmentSetup(c *cli.Context) error {
	root := c.String("root")
	if root == "" {
		return fmt.Errorf("Have to specific root directory")
	}
	if err := util.MkdirIfNotExists(root); err != nil {
		return fmt.Errorf("Invalid root directory:", err)
	}

	lock = filepath.Join(root, LOCKFILE)
	if err := util.LockFile(lock); err != nil {
		return fmt.Errorf("Failed to lock the file", err.Error())
	}

	logName := c.String("log")
	if logName != "" {
		logFile, err := os.OpenFile(logName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			return err
		}
		logrus.SetFormatter(&logrus.JSONFormatter{})
		logrus.SetOutput(logFile)
	} else {
		logrus.SetOutput(os.Stderr)
	}

	return nil
}
func (s *Server) getVolumeMountPoint(volumeUUID, mountPoint string) (string, error) {
	if mountPoint != "" {
		return mountPoint, nil
	}
	dir := filepath.Join(s.MountsDir, volumeUUID)
	if err := util.MkdirIfNotExists(dir); err != nil {
		return "", err
	}
	return dir, nil
}
示例#4
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
}