Ejemplo 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 gotil.FileExists(devPath) {
		v, _ := ioutil.ReadFile(devPath)
		log.Printf("Loaded %s from %s\n", path, devPath)
		return string(v)
	}

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

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

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

	return defaultValue
}
Ejemplo n.º 2
0
func TestPrintVersion(t *testing.T) {
	version.Arch = "Linux-x86_64"
	version.Branch = "master"
	version.ShaLong = gotil.RandomString(32)
	version.Epoch = fmt.Sprintf("%d", time.Now().Unix())
	version.SemVer = "1.0.0"
	_, _, thisAbsPath := gotil.GetThisPathParts()
	epochStr := version.EpochToRfc1123()

	t.Logf("thisAbsPath=%s", thisAbsPath)
	t.Logf("epochStr=%s", epochStr)

	var buff []byte
	b := bytes.NewBuffer(buff)

	PrintVersion(b)

	vs := b.String()

	evs := `Binary: ` + thisAbsPath + `
SemVer: ` + version.SemVer + `
OsArch: ` + version.Arch + `
Branch: ` + version.Branch + `
Commit: ` + version.ShaLong + `
Formed: ` + epochStr + `
`

	if vs != evs {
		t.Fatalf("nexpectedVersionString=%s\n\nversionString=%s\n", evs, vs)
	}
}
Ejemplo n.º 3
0
func initPaths() {

	thisExeDir, thisExeName, thisExeAbsPath = gotil.GetThisPathParts()

	if libstorageHome == "" {
		libstorageHome = "/"
	}

	// if not root and home is /, change home to user's home dir
	if os.Geteuid() != 0 && libstorageHome == "/" {
		libstorageHome = path.Join(gotil.HomeDir(), ".libstorage")
	}

	for i := Home; i < maxFileKey; i++ {
		if i.isFileKeyMarker() {
			continue
		}

		// this initialized the value
		_ = i.String()

		if Debug {
			log.WithField(i.key(), i.String()).Info("libStorage path")
		}
	}
}
Ejemplo n.º 4
0
func (c *CLI) tryToStartDaemon() {
	_, _, thisAbsPath := gotil.GetThisPathParts()

	fmt.Print("Starting REX-Ray...")

	signal := make(chan byte)
	client := fmt.Sprintf("%s/%s.sock", os.TempDir(), gotil.RandomString(32))
	log.WithField("client", client).Debug("trying to start service")

	l, lErr := net.Listen("unix", client)
	failOnError(lErr)

	go func() {
		conn, acceptErr := l.Accept()
		if acceptErr != nil {
			fmt.Printf("FAILED!\n  %v\n", acceptErr)
			panic(acceptErr)
		}
		defer conn.Close()
		defer os.Remove(client)

		log.Debug("accepted connection")

		buff := make([]byte, 1)
		conn.Read(buff)

		log.Debug("received data")

		signal <- buff[0]
	}()

	cmdArgs := []string{
		"start",
		fmt.Sprintf("--client=%s", client),
		fmt.Sprintf("--logLevel=%v", c.logLevel())}

	if c.host() != "" {
		cmdArgs = append(cmdArgs, fmt.Sprintf("--host=%s", c.host()))
	}

	cmd := exec.Command(thisAbsPath, cmdArgs...)
	cmd.Stderr = os.Stderr

	cmdErr := cmd.Start()
	failOnError(cmdErr)

	sigVal := <-signal
	if sigVal != 0 {
		fmt.Println("FAILED!")
		panic(1)
	}

	pid, _ := util.ReadPidFile()
	fmt.Printf("SUCCESS!\n\n")
	fmt.Printf("  The REX-Ray daemon is now running at PID %d. To\n", pid)
	fmt.Printf("  shutdown the daemon execute the following command:\n\n")
	fmt.Printf("    sudo %s stop\n\n", thisAbsPath)
}
Ejemplo n.º 5
0
Archivo: util.go Proyecto: akutz/rexray
func init() {
	prefix = os.Getenv("REXRAY_HOME")
	BinFileDirPath, BinFileName, BinFilePath = gotil.GetThisPathParts()
	UnitFileName = fmt.Sprintf("%s.service", BinFileName)
	UnitFilePath = path.Join("/etc/systemd/system", UnitFileName)
	InitFileName = BinFileName
	InitFilePath = path.Join("/etc/init.d", InitFileName)
	PIDFileName = fmt.Sprintf("%s.pid", BinFileName)
	DotDirName = fmt.Sprintf(".rexray")
}
Ejemplo n.º 6
0
func install() {
	checkOpPerms("installed")

	_, _, exeFile := gotil.GetThisPathParts()

	if runtime.GOOS == "linux" {
		switch getInitSystemType() {
		case SystemD:
			installSystemD(exeFile)
		case UpdateRcD:
			installUpdateRcd(exeFile)
		case ChkConfig:
			installChkConfig(exeFile)
		}
	}
}
Ejemplo n.º 7
0
func uninstall(pkgManager bool) {
	checkOpPerms("uninstalled")

	_, _, binFile := gotil.GetThisPathParts()

	// if the uninstall command was executed manually we should check to see
	// if this file is owned by a package manager and remove it that way if so
	if !pkgManager {
		log.WithField("binFile", binFile).Debug("is this a managed file?")
		var pkgName string
		if isRpmInstall(binFile, &pkgName) {
			uninstallRpm(pkgName)
			return
		} else if isDebInstall(binFile, &pkgName) {
			uninstallDeb(pkgName)
			return
		}
	}

	func() {
		defer func() {
			recover()
		}()
		stop()
	}()

	switch getInitSystemType() {
	case SystemD:
		uninstallSystemD()
	case UpdateRcD:
		uninstallUpdateRcd()
	case ChkConfig:
		uninstallChkConfig()
	}

	if !pkgManager {
		os.Remove(binFile)
		if util.IsPrefixed() {
			os.RemoveAll(util.GetPrefix())
		}
	}
}
Ejemplo n.º 8
0
func TestPrintVersion(t *testing.T) {
	// this test works, and adding the libStorage info in just makes it more
	// trouble than it's worth right now to fix
	t.SkipNow()

	core.Version = &apitypes.VersionInfo{}
	core.Version.Arch = "Linux-x86_64"
	core.Version.Branch = "master"
	core.Version.ShaLong = gotil.RandomString(32)
	core.Version.BuildTimestamp = time.Now()
	core.Version.SemVer = "1.0.0"
	_, _, thisAbsPath := gotil.GetThisPathParts()
	epochStr := core.Version.BuildTimestamp.Format(time.RFC1123)

	t.Logf("thisAbsPath=%s", thisAbsPath)
	t.Logf("epochStr=%s", epochStr)

	var buff []byte
	b := bytes.NewBuffer(buff)

	PrintVersion(b)

	vs := b.String()

	evs := `Binary: ` + thisAbsPath + `
SemVer: ` + core.Version.SemVer + `
OsArch: ` + core.Version.Arch + `
Branch: ` + core.Version.Branch + `
Commit: ` + core.Version.ShaLong + `
Formed: ` + epochStr + `
`

	if vs != evs {
		t.Fatalf("nexpectedVersionString=%s\n\nversionString=%s\n", evs, vs)
	}
}
Ejemplo n.º 9
0
// Run the server.
func Run() {
	server.CloseOnAbort()

	flag.Usage = printUsage
	flag.Parse()

	if flagVersion != nil && *flagVersion {
		_, _, thisExeAbsPath := gotil.GetThisPathParts()
		fmt.Fprintf(os.Stdout, "Binary: %s\n", thisExeAbsPath)
		fmt.Fprint(os.Stdout, api.Version.String())
		os.Exit(0)
	}

	if flagEnv != nil && *flagEnv {
		for _, v := range os.Environ() {
			fmt.Fprintf(os.Stdout, "%s\n", v)
		}
		os.Exit(0)
	}

	// if a config is specified then do not care about any other options
	if flagConfig != nil && gotil.FileExists(*flagConfig) {

		config = gofigCore.New()

		if err := config.ReadConfigFile(*flagConfig); err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		if flagPrintConfig != nil && *flagPrintConfig {
			jstr, err := config.ToJSON()
			if err != nil {
				fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
				os.Exit(1)
			}
			fmt.Fprintln(os.Stdout, jstr)
			os.Exit(0)
		}

		s, errs, err := server.Serve(nil, config)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		err = <-errs
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}

		s.Close()
		os.Exit(0)
	}

	cfg, err := apiconfig.NewConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	config = cfg
	for _, fs := range config.FlagSets() {
		flag.CommandLine.AddFlagSet(fs)
	}

	if flagHelp != nil && *flagHelp {
		flag.Usage()
	}

	if len(flag.Args()) == 0 {
		flag.Usage()
	}

	if flagHost != nil {
		os.Setenv("LIBSTORAGE_HOST", *flagHost)
	}

	if flagLogLvl != nil {
		os.Setenv("LIBSTORAGE_LOGGING_LEVEL", *flagLogLvl)
	}

	if lvl, err := log.ParseLevel(
		config.GetString(apitypes.ConfigLogLevel)); err == nil {
		log.SetLevel(lvl)
	}

	if flagPrintConfig != nil && *flagPrintConfig {
		jstr, err := config.ToJSON()
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
			os.Exit(1)
		}
		fmt.Fprintln(os.Stdout, jstr)
		os.Exit(0)
	}

	buf := &bytes.Buffer{}
	fmt.Fprintf(buf, "libstorage:\n  server:\n    services:\n")
	for _, ds := range flag.Args() {
		dsp := strings.Split(ds, ":")
		dn := dsp[0]
		sn := dsp[0]
		if len(dsp) > 1 {
			sn = dsp[1]
		}
		fmt.Fprintf(buf, "      %s:\n        driver: %s\n", sn, dn)
	}
	if err := config.ReadConfig(buf); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	server.CloseOnAbort()

	_, errs, err := server.Serve(nil, config)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}

	<-errs
}
Ejemplo n.º 10
0
func init() {
	prefix = os.Getenv("REXRAY_HOME")

	thisExeDir, thisExeName, thisExeAbsPath = gotil.GetThisPathParts()
}