Пример #1
0
Файл: env.go Проект: GoToolz/Env
func init() {

	// Init in memory env
	Env = make(map[string]string)

	// Use OS environement
	for _, e := range os.Environ() {
		pair := strings.Split(e, "=")
		Env[pair[0]] = pair[1]
	}

	envFile := ".env"
	if e := os.Getenv("GO_ENV_FILE"); e != "" {
		envFile = e
	}

	// Read .env file
	env, err := godotenv.Read(envFile)
	if err != nil {
		log.Println(err)
	}

	for k, v := range env {
		Env[k] = v
	}
}
Пример #2
0
// helper function to retrieve secret variables.
func getSecrets(c *cli.Context) []*model.Secret {

	var secrets []*model.Secret

	if c.String("secrets-file") != "" {
		envs, _ := godotenv.Read(c.String("secrets-file"))
		for k, v := range envs {
			secret := &model.Secret{
				Name:  k,
				Value: v,
				Events: []string{
					model.EventPull,
					model.EventPush,
					model.EventTag,
					model.EventDeploy,
				},
				Images: []string{"*"},
			}
			secrets = append(secrets, secret)
		}
	}

	for _, s := range c.StringSlice("secret") {
		parts := strings.SplitN(s, "=", 2)
		if len(parts) != 2 {
			continue
		}
		secret := &model.Secret{
			Name:  parts[0],
			Value: parts[1],
			Events: []string{
				model.EventPull,
				model.EventPush,
				model.EventTag,
				model.EventDeploy,
			},
			Images: []string{"*"},
		}
		secrets = append(secrets, secret)
	}
	return secrets
}
Пример #3
0
func runEnvLoad(cmd *Command, args []string) {
	appname := mustApp()
	message := getMessage()
	if len(args) != 1 {
		cmd.PrintUsage()
		os.Exit(2)
	}

	parsedVars, err := godotenv.Read(args[0])
	must(err)

	config := make(map[string]*string)
	for key, value := range parsedVars {
		config[key] = &value
	}

	_, err = client.ConfigVarUpdate(appname, config, message)
	must(err)
	log.Printf("Updated env vars from %s and restarted %s.", args[0], appname)
}
Пример #4
0
func bootCompleteRun(cmd *cobra.Command, args []string) {
	hostEnvironment, err := godotenv.Read(
		"/etc/os-release",
		"/etc/yochu-env",
		"/etc/mayu-env",
	)
	assert(err)

	serial, ok := hostEnvironment["SERIAL"]
	if !ok {
		fmt.Printf("Can't find serial in host environment (/etc/mayu-env)")
		os.Exit(1)
	}

	var host hostmgr.Host
	if bootCompleteFlags.UpdateVersions {
		for key, value := range hostEnvironment {
			switch key {
			case "VERSION":
				host.CoreOSVersion = value
			case "MAYU_VERSION":
				host.MayuVersion = value
			case "DOCKER_VERSION":
				host.DockerVersion = value
			case "ETCD_VERSION":
				host.EtcdVersion = value
			case "FLEET_VERSION":
				host.FleetVersion = value
			case "YOCHU_VERSION":
				host.YochuVersion = value
			case "RKT_VERSION":
				host.RktVersion = value
			case "K8S_VERSION":
				host.K8sVersion = value
			}
		}
	}

	err = mayu.BootComplete(serial, host)
	assert(err)
}
Пример #5
0
// LoadEnvironmentFile accepts filename of a file containing key=value pairs
// and puts these pairs into a map. If filename is "-" the file contents are
// read from the stdin argument, provided it is not nil.
func LoadEnvironmentFile(filename string, stdin io.Reader) (Environment, error) {
	errorFilename := filename

	if filename == "-" && stdin != nil {
		//once https://github.com/joho/godotenv/pull/20 is merged we can get rid of using tempfile
		temp, err := ioutil.TempFile("", "origin-env-stdin")
		if err != nil {
			return nil, fmt.Errorf("Cannot create temporary file: %s", err)
		}

		filename = temp.Name()
		errorFilename = "stdin"
		defer os.Remove(filename)

		if _, err = io.Copy(temp, stdin); err != nil {
			return nil, fmt.Errorf("Cannot write to temporary file %q: %s", filename, err)
		}
		temp.Close()
	}

	// godotenv successfuly returns empty map when given path to a directory,
	// remove this once https://github.com/joho/godotenv/pull/22 is merged
	if info, err := os.Stat(filename); err == nil && info.IsDir() {
		return nil, fmt.Errorf("Cannot read varaiables from %q: is a directory", filename)
	} else if err != nil {
		return nil, fmt.Errorf("Cannot stat %q: %s", filename, err)
	}

	env, err := godotenv.Read(filename)
	if err != nil {
		return nil, fmt.Errorf("Cannot read variables from file %q: %s", errorFilename, err)
	}
	for k, v := range env {
		if !cmdutil.IsValidEnvironmentArgument(fmt.Sprintf("%s=%s", k, v)) {
			return nil, fmt.Errorf("invalid parameter assignment in %s=%s", k, v)
		}
	}
	return env, nil
}
Пример #6
0
func SpawnBackendProcfile(pathToApp string) (*Backend, error) {
	port, err := getFreeTCPPort()
	if err != nil {
		return nil, err
	}
	log.Println("Spawning", pathToApp, "on port", port)

	env := os.Environ()

	pathbytes, err := ioutil.ReadFile(os.Getenv("HOME") + "/.pow/.path")
	path := os.Getenv("PATH")
	if err == nil {
		path = string(pathbytes)
	}
	// remove the old PATH
	for i, v := range env {
		if strings.Index(v, "PATH=") == 0 {
			env = append(env[:i], env[i+1:]...)
		}
	}
	env = append(env, "PATH="+path, "PORT="+strconv.Itoa(port))

	// add .env
	entries, err := godotenv.Read(pathToApp + "/.env")
	if err == nil {
		for k, v := range entries {
			env = append(env, k+"="+v)
		}
	} else {
		// allow absence of .env
		if !os.IsNotExist(err) {
			return nil, err
		}
	}

	procfile, err := ReadProcfile(pathToApp + "/Procfile")
	if err != nil {
		return nil, err
	}
	var CmdName string
	for _, v := range procfile.Entries {
		if v.Name == "web" {
			CmdName = v.Command
		}
	}

	if CmdName == "" {
		return nil, errors.New("No 'web' entry found in Procfile")
	}

	cmd := exec.Command("bash", "-c", "exec "+CmdName)

	var bootlog bytes.Buffer

	toStderrWithCapture := io.MultiWriter(os.Stderr, &bootlog)

	cmd.Stdout = toStderrWithCapture // never write to gowd's stdout
	cmd.Stderr = toStderrWithCapture
	cmd.Dir = pathToApp
	cmd.Env = env

	err = cmd.Start()
	if err != nil {
		return nil, err
	}

	exitChan := make(chan interface{}, 1)
	b := &Backend{appPath: pathToApp, host: "127.0.0.1", port: port, proxy: false, process: cmd.Process, startedAt: time.Now(), activityChan: make(chan interface{}), exitChan: exitChan}
	booting := true
	crashChan := make(chan error, 1)
	go func() {
		cmd.Wait()
		b.exited = true
		b.exitChan <- new(interface{})

		if booting {
			crashChan <- BootCrash{Log: bootlog, Env: env, Cmd: CmdName, Path: pathToApp}
		}
	}()

	log.Println("waiting for spawn result for", pathToApp)

	select {
	case <-awaitTCP(b.Address()):
		log.Println(pathToApp, "came up successfully")
		booting = false
		go b.watchForActivity()

		return b, nil
	case <-time.After(30 * time.Second):
		log.Println(pathToApp, "failed to bind")
		cmd.Process.Kill()
		return nil, errors.New("app failed to bind")
	case err := <-crashChan:
		log.Println(pathToApp, "crashed while starting")
		return nil, err
	}
}