Esempio n. 1
0
func loadAsset(path, defaultValue string) string {

	devPath := fmt.Sprintf(
		"%s/src/github.com/emccode/rexray/daemon/module/admin/html/%s",
		os.Getenv("GOPATH"),
		path)

	if util.FileExists(devPath) {
		v, _ := ioutil.ReadFile(devPath)
		log.Printf("Loaded %s from %s\n", path, devPath)
		return string(v)
	}

	exeDir, _, _ := util.GetThisPathParts()

	relPath := fmt.Sprintf(
		"%s/html/%s",
		exeDir,
		path)

	if util.FileExists(relPath) {
		v, _ := ioutil.ReadFile(devPath)
		log.Printf("Loaded %s from %s\n", path, relPath)
		return string(v)
	}

	return defaultValue
}
Esempio n. 2
0
func New() *Config {
	log.Debug("initializing configuration")

	c := &Config{
		Viper: viper.New(),
	}

	cfgName := "config"
	cfgType := "yaml"
	etcRexRay := "/etc/rexray"
	usrRexRay := fmt.Sprintf("%s/.rexray", util.HomeDir())
	etcRexRayFile := fmt.Sprintf("%s/%s.%s", etcRexRay, cfgName, cfgType)
	usrRexRayFile := fmt.Sprintf("%s/%s.%s", usrRexRay, cfgName, cfgType)

	c.Viper.SetConfigName(cfgName)
	c.Viper.SetConfigType(cfgType)
	c.Viper.SetTypeByDefaultValue(true)

	log.WithFields(log.Fields{
		"name": cfgName,
		"type": cfgType}).Debug("set config name and type")

	c.Viper.AddConfigPath(etcRexRay)
	c.Viper.AddConfigPath(usrRexRay)

	log.WithFields(log.Fields{
		"global": etcRexRay,
		"user":   usrRexRay}).Debug("added config paths")

	if util.FileExists(etcRexRayFile) || util.FileExists(usrRexRayFile) {

		log.WithFields(log.Fields{
			"global": etcRexRayFile,
			"user":   usrRexRayFile}).Debug(
			"reading configuration file(s) from default path(s)")

		if readCfgErr := c.Viper.ReadInConfig(); readCfgErr != nil {
			log.WithFields(log.Fields{
				"global": etcRexRayFile,
				"user":   usrRexRayFile,
				"error":  readCfgErr}).Error(
				"error reading configuration file(s) from default path(s)")
		}
	}

	c.initConfigKeys()

	return c
}
Esempio n. 3
0
func NewConfig(
	loadGlobalConfig, loadUserConfig bool,
	configName, configType string) *Config {

	log.Debug("initializing configuration")

	c := &Config{
		plainTextConfig: plainTextConfig{
			secureConfig: secureConfig{},
		},
		Viper:               viper.New(),
		GlobalFlags:         &flag.FlagSet{},
		AdditionalFlags:     &flag.FlagSet{},
		jsonMarshalStrategy: JsonMarshalSecure,
	}
	c.Viper.SetTypeByDefaultValue(true)
	c.Viper.SetConfigName(configName)
	c.Viper.SetConfigType(configType)

	cfgFile := fmt.Sprintf("%s.%s", configName, configType)
	etcRexRayFile := fmt.Sprintf("%s/%s", util.EtcDirPath(), cfgFile)
	usrRexRayFile := fmt.Sprintf("%s/.rexray/%s", util.HomeDir(), cfgFile)

	if loadGlobalConfig && util.FileExists(etcRexRayFile) {
		log.WithField("path", etcRexRayFile).Debug("loading global config file")
		if err := c.ReadConfigFile(etcRexRayFile); err != nil {
			log.WithFields(log.Fields{
				"path":  etcRexRayFile,
				"error": err}).Error(
				"error reading global config file")
		}
	}

	if loadUserConfig && util.FileExists(usrRexRayFile) {
		log.WithField("path", usrRexRayFile).Debug("loading user config file")
		if err := c.ReadConfigFile(usrRexRayFile); err != nil {
			log.WithFields(log.Fields{
				"path":  usrRexRayFile,
				"error": err}).Error(
				"error reading user config file")
		}
	}

	c.initConfigKeys()

	return c

}
Esempio n. 4
0
func initUsageTemplates() {

	var ut string
	utPath := fmt.Sprintf("%s/.rexray/usage.template", util.HomeDir())
	log.WithField("path", utPath).Debug("usage template path")

	if util.FileExists(utPath) {
		dat, err := ioutil.ReadFile(utPath)
		if err != nil {
			panic(err)
		}
		log.WithField("source", utPath).Debug("loaded usage template")
		ut = string(dat)
	} else {
		log.WithField("source", "UsageTemplate").Debug("loaded usage template")
		ut = UsageTemplate
	}

	RexrayCmd.SetUsageTemplate(ut)
	RexrayCmd.SetHelpTemplate(ut)

	cobra.AddTemplateFuncs(template.FuncMap{
		"af":    additionalFlags,
		"hf":    hasFlags,
		"lf":    localFlags,
		"gf":    globalFlags,
		"ihf":   isHelpFlag,
		"ivf":   isVerboseFlag,
		"saf":   sansAdditionalFlags,
		"cmds":  commands,
		"rtrim": rtrim,
	})
}
Esempio n. 5
0
func status() {
	if !util.FileExists(util.PidFilePath()) {
		fmt.Println("REX-Ray is stopped")
		return
	}
	pid, _ := util.ReadPidFile()
	fmt.Printf("REX-Ray is running at pid %d\n", pid)
}
Esempio n. 6
0
func restart() {
	checkOpPerms("restarted")

	if util.FileExists(util.PidFilePath()) {
		stop()
	}

	start()
}
Esempio n. 7
0
func Restart() {
	checkOpPerms("restarted")

	if util.FileExists(util.PidFile()) {
		Stop()
	}

	Start()
}
Esempio n. 8
0
// NewConfig initialies a new instance of a Config object with the specified
// options.
func NewConfig(
	loadGlobalConfig, loadUserConfig bool,
	configName, configType string) *Config {

	log.Debug("initializing configuration")

	c := &Config{
		v:        viper.New(),
		FlagSets: map[string]*flag.FlagSet{},
	}
	c.v.SetTypeByDefaultValue(false)
	c.v.SetConfigName(configName)
	c.v.SetConfigType(configType)

	c.processRegistrations()

	cfgFile := fmt.Sprintf("%s.%s", configName, configType)
	etcRexRayFile := util.EtcFilePath(cfgFile)
	usrRexRayFile := fmt.Sprintf("%s/.rexray/%s", util.HomeDir(), cfgFile)

	if loadGlobalConfig && util.FileExists(etcRexRayFile) {
		log.WithField("path", etcRexRayFile).Debug("loading global config file")
		if err := c.ReadConfigFile(etcRexRayFile); err != nil {
			log.WithFields(log.Fields{
				"path":  etcRexRayFile,
				"error": err}).Error(
				"error reading global config file")
		}
	}

	if loadUserConfig && util.FileExists(usrRexRayFile) {
		log.WithField("path", usrRexRayFile).Debug("loading user config file")
		if err := c.ReadConfigFile(usrRexRayFile); err != nil {
			log.WithFields(log.Fields{
				"path":  usrRexRayFile,
				"error": err}).Error(
				"error reading user config file")
		}
	}

	return c
}
Esempio n. 9
0
func (c *CLI) preRun(cmd *cobra.Command, args []string) {

	if c.cfgFile != "" && util.FileExists(c.cfgFile) {
		if err := c.r.Config.ReadConfigFile(c.cfgFile); err != nil {
			panic(err)
		}
		cmd.Flags().Parse(os.Args[1:])
	}

	c.updateLogLevel()

	if isHelpFlag(cmd) {
		cmd.Help()
		panic(&helpFlagPanic{})
	}

	if permErr := c.checkCmdPermRequirements(cmd); permErr != nil {
		if term.IsTerminal() {
			printColorizedError(permErr)
		} else {
			printNonColorizedError(permErr)
		}

		fmt.Println()
		cmd.Help()
		panic(&printedErrorPanic{})
	}

	if c.isInitDriverManagersCmd(cmd) {
		if err := c.r.InitDrivers(); err != nil {

			if term.IsTerminal() {
				printColorizedError(err)
			} else {
				printNonColorizedError(err)
			}
			fmt.Println()

			helpCmd := cmd
			if cmd == c.volumeCmd {
				helpCmd = c.volumeGetCmd
			} else if cmd == c.snapshotCmd {
				helpCmd = c.snapshotGetCmd
			} else if cmd == c.deviceCmd {
				helpCmd = c.deviceGetCmd
			} else if cmd == c.adapterCmd {
				helpCmd = c.adapterGetTypesCmd
			}
			helpCmd.Help()

			panic(&printedErrorPanic{})
		}
	}
}
Esempio n. 10
0
func stop() {
	checkOpPerms("stopped")

	if !util.FileExists(util.PidFilePath()) {
		fmt.Println("REX-Ray is already stopped")
		panic(1)
	}

	fmt.Print("Shutting down REX-Ray...")

	pid, pidErr := util.ReadPidFile()
	failOnError(pidErr)

	proc, procErr := os.FindProcess(pid)
	failOnError(procErr)

	killErr := proc.Signal(syscall.SIGHUP)
	failOnError(killErr)

	fmt.Println("SUCCESS!")
}
Esempio n. 11
0
func start() {
	checkOpPerms("started")

	log.WithField("os.Args", os.Args).Debug("invoking service start")

	pidFile := util.PidFilePath()

	if util.FileExists(pidFile) {
		pid, pidErr := util.ReadPidFile()
		if pidErr != nil {
			fmt.Printf("Error reading REX-Ray PID file at %s\n", pidFile)
		} else {
			fmt.Printf("REX-Ray already running at PID %d\n", pid)
		}
		panic(1)
	}

	if fg || client != "" {
		startDaemon()
	} else {
		tryToStartDaemon()
	}
}