Example #1
0
func (s *GetSuite) TestGetConfig(c *gc.C) {
	sch := s.AddTestingCharm(c, "dummy")
	svc := s.AddTestingService(c, "dummy-service", sch)
	err := svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"})
	c.Assert(err, jc.ErrorIsNil)
	for _, t := range getTests {
		ctx := coretesting.Context(c)
		code := cmd.Main(envcmd.Wrap(&GetCommand{}), ctx, []string{t.service})
		c.Check(code, gc.Equals, 0)
		c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
		// round trip via goyaml to avoid being sucked into a quagmire of
		// map[interface{}]interface{} vs map[string]interface{}. This is
		// also required if we add json support to this command.
		buf, err := goyaml.Marshal(t.expected)
		c.Assert(err, jc.ErrorIsNil)
		expected := make(map[string]interface{})
		err = goyaml.Unmarshal(buf, &expected)
		c.Assert(err, jc.ErrorIsNil)

		actual := make(map[string]interface{})
		err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual)
		c.Assert(err, jc.ErrorIsNil)
		c.Assert(actual, gc.DeepEquals, expected)
	}
}
Example #2
0
func main() {
	t := CircleCIConfig{}

	err := yaml.Unmarshal([]byte(data), &t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t:\n%v\n\n", t)

	d, err := yaml.Marshal(&t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t dump:\n%s\n\n", string(d))

	m := make(map[interface{}]interface{})

	err = yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m:\n%v\n\n", m)

	d, err = yaml.Marshal(&m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m dump:\n%s\n\n", string(d))

	for _, p := range t.Test.Pre {
		fmt.Println(p)
	}
}
Example #3
0
func main() {
	file1 := os.Args[1]
	file2 := os.Args[2]

	buf, err := ioutil.ReadFile(file1)
	if err != nil {
		panic(err)
	}
	m1 := make(map[interface{}]interface{})
	err = goyaml.Unmarshal([]byte(buf), &m1)
	if err != nil {
		panic(err)
	}

	buf2, err := ioutil.ReadFile(file2)
	if err != nil {
		panic(err)
	}
	m2 := make(map[interface{}]interface{})
	err = goyaml.Unmarshal([]byte(buf2), &m2)
	if err != nil {
		panic(err)
	}

	m3 := make(map[interface{}]interface{})
	addLeftParam(m3, m1)
	addRightParam(m3, m2)

	ommitSameParam(m3)
	//pretty.Printf("%# v\n\n", m3)

	printDiffYaml(m3, "")
}
Example #4
0
func ReadConfig(path string) (*Config, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	config := Config{}
	if err = yaml.Unmarshal(data, &config); err != nil {
		return nil, err
	}

	checks := struct {
		Checks    map[string][]map[string]interface{} `yaml:"checks"`
		Notifiers map[string]map[string]interface{}   `yaml:"notify"`
	}{}
	if err = yaml.Unmarshal(data, &checks); err != nil {
		return nil, err
	}

	for t, items := range checks.Checks {
		switch t {
		case "web":
			for _, item := range items {
				config.Checks = append(config.Checks, ParseWebCheck(item))
			}
		case "ping":
			for _, item := range items {
				config.Checks = append(config.Checks, ParsePingCheck(item))
			}
		case "dns":
			for _, item := range items {
				config.Checks = append(config.Checks, ParseDnsCheck(item))
			}
		case "port":
			for _, item := range items {
				config.Checks = append(config.Checks, ParsePortCheck(item))
			}
		default:
			return nil, fmt.Errorf("Invalid check type:", t)
		}
	}

	for t, items := range checks.Notifiers {
		switch t {
		case "slack":
			config.Notifiers = append(config.Notifiers, ParseSlackNotifier(items))
		default:
			return nil, fmt.Errorf("Invalid notifier type:", t)
		}
	}

	return &config, nil
}
Example #5
0
func TestTargetSetRecreatesTargetGroupsEveryRun(t *testing.T) {

	verifyPresence := func(tgroups map[string]*config.TargetGroup, name string, present bool) {
		if _, ok := tgroups[name]; ok != present {
			msg := ""
			if !present {
				msg = "not "
			}
			t.Fatalf("'%s' should %sbe present in TargetSet.tgroups: %s", name, msg, tgroups)
		}
	}

	cfg := &config.ServiceDiscoveryConfig{}

	sOne := `
static_configs:
- targets: ["foo:9090"]
- targets: ["bar:9090"]
`
	if err := yaml.Unmarshal([]byte(sOne), cfg); err != nil {
		t.Fatalf("Unable to load YAML config sOne: %s", err)
	}
	called := make(chan struct{})

	ts := NewTargetSet(&mockSyncer{
		sync: func([]*config.TargetGroup) { called <- struct{}{} },
	})
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go ts.Run(ctx)

	ts.UpdateProviders(ProvidersFromConfig(*cfg))
	<-called

	verifyPresence(ts.tgroups, "static/0/0", true)
	verifyPresence(ts.tgroups, "static/0/1", true)

	sTwo := `
static_configs:
- targets: ["foo:9090"]
`
	if err := yaml.Unmarshal([]byte(sTwo), cfg); err != nil {
		t.Fatalf("Unable to load YAML config sTwo: %s", err)
	}

	ts.UpdateProviders(ProvidersFromConfig(*cfg))
	<-called

	verifyPresence(ts.tgroups, "static/0/0", true)
	verifyPresence(ts.tgroups, "static/0/1", false)
}
Example #6
0
func mergeProject(p *Project, file string, bytes []byte) (map[string]*ServiceConfig, error) {
	configs := make(map[string]*ServiceConfig)

	var schema rawSchema
	if err := yaml.Unmarshal(bytes, &schema); err != nil {
		return nil, err
	}

	var datas = make(rawServiceMap)
	switch {
	case schema.Version == "2":
		datas = schema.Services
	case len(schema.Version) == 0:
		datas = make(rawServiceMap)
		if err := yaml.Unmarshal(bytes, &datas); err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("docker-compose file with schema version %q is not supported", schema.Version)
	}

	if err := interpolate(p.context.EnvironmentLookup, &datas); err != nil {
		return nil, err
	}

	for name, data := range datas {
		data, err := parse(p.context.ResourceLookup, p.context.EnvironmentLookup, file, data, datas)
		if err != nil {
			logrus.Errorf("Failed to parse service %s: %v", name, err)
			return nil, err
		}

		if _, ok := p.Configs[name]; ok {
			var rawExistingService rawService
			if err := Convert(p.Configs[name], &rawExistingService); err != nil {
				return nil, err
			}

			data = mergeConfig(rawExistingService, data)
		}

		datas[name] = data
	}

	if err := Convert(datas, &configs); err != nil {
		return nil, err
	}

	adjustValues(configs)

	return configs, nil
}
Example #7
0
func ReadConfig(conf interface{}, defaultYaml string) {
	var path string = cliArgs()
	err := yaml.Unmarshal([]byte(defaultYaml), conf)
	if err != nil {
		panic(err)
	}

	if path != "" {
		confData := readYaml(path)
		err := yaml.Unmarshal(confData, conf)
		if err != nil {
			panic(err)
		}
	}
}
Example #8
0
func (s *statusGetSuite) TestServiceStatus(c *gc.C) {
	expected := map[string]interface{}{
		"service-status": map[interface{}]interface{}{
			"status-data": map[interface{}]interface{}{},
			"units": map[interface{}]interface{}{
				"": map[interface{}]interface{}{
					"message":     "this is a unit status",
					"status":      "active",
					"status-data": map[interface{}]interface{}{},
				},
			},
			"message": "this is a service status",
			"status":  "active"},
	}
	hctx := s.GetStatusHookContext(c)
	setFakeServiceStatus(hctx)
	com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
	c.Assert(err, jc.ErrorIsNil)
	ctx := testing.Context(c)
	code := cmd.Main(com, ctx, []string{"--format", "json", "--include-data", "--service"})
	c.Assert(code, gc.Equals, 0)

	var out map[string]interface{}
	c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil)
	c.Assert(out, gc.DeepEquals, expected)

}
Example #9
0
// Set decodes the base64 value into yaml then expands that into a map.
func (v *yamlBase64Value) Set(value string) error {
	decoded, err := base64.StdEncoding.DecodeString(value)
	if err != nil {
		return err
	}
	return goyaml.Unmarshal(decoded, v)
}
Example #10
0
func setFromFileByPrefix(prefix, fn string, ret map[string]string) {
	p := path.Ext(fn)
	_, err := os.Stat(fn)
	if err != nil {
		return
	}

	file, err := os.Open(fn)
	if err != nil {
		log.Printf("Could not open flagconf file '%s': %s", fn, err)
		return
	}
	log.Printf("Loading config file: %s", fn)

	by, err := ioutil.ReadAll(file)
	if err != nil {
		goto out
	}

	switch p {
	case ".json":
		err = json.Unmarshal(by, &ret)
	default: // assume yaml
		err = yaml.Unmarshal(by, &ret)
	}

out:
	if err != nil {
		log.Printf("flagconf (prefix: %s, suffix: %s): %s", prefix, p, err)
	}
}
Example #11
0
// ClusterFromBytes Necessary for unit tests, which store configs as hardcoded strings
func ClusterFromBytes(data []byte) (*Cluster, error) {
	c := newDefaultCluster()
	if err := yaml.Unmarshal(data, c); err != nil {
		return nil, fmt.Errorf("failed to parse cluster: %v", err)
	}

	// HostedZone needs to end with a '.', amazon will not append it for you.
	// as it will with RecordSets
	c.HostedZone = WithTrailingDot(c.HostedZone)

	// If the user specified no subnets, we assume that a single AZ configuration with the default instanceCIDR is demanded
	if len(c.Subnets) == 0 && c.InstanceCIDR == "" {
		c.InstanceCIDR = "10.0.0.0/24"
	}

	c.HostedZoneID = withHostedZoneIDPrefix(c.HostedZoneID)

	if err := c.valid(); err != nil {
		return nil, fmt.Errorf("invalid cluster: %v", err)
	}

	// For backward-compatibility
	if len(c.Subnets) == 0 {
		c.Subnets = []Subnet{
			{
				AvailabilityZone: c.AvailabilityZone,
				InstanceCIDR:     c.InstanceCIDR,
			},
		}
	}
	return c, nil
}
Example #12
0
// LoadFromBytes loads a configuration from a bytes slice
func LoadFromBytes(data []byte) (*Config, error) {
	config := NewConfig()
	if err := yaml.Unmarshal(data, config); err != nil {
		return nil, err
	}
	return config, nil
}
Example #13
0
func (f *Framework) GetUnderlyingFederatedContexts() []E2EContext {
	kubeconfig := framework.KubeConfig{}
	configBytes, err := ioutil.ReadFile(framework.TestContext.KubeConfig)
	framework.ExpectNoError(err)
	err = yaml.Unmarshal(configBytes, &kubeconfig)
	framework.ExpectNoError(err)

	e2eContexts := []E2EContext{}
	for _, context := range kubeconfig.Contexts {
		if strings.HasPrefix(context.Name, "federation") && context.Name != framework.TestContext.FederatedKubeContext {
			user := kubeconfig.FindUser(context.Context.User)
			if user == nil {
				framework.Failf("Could not find user for context %+v", context)
			}

			cluster := kubeconfig.FindCluster(context.Context.Cluster)
			if cluster == nil {
				framework.Failf("Could not find cluster for context %+v", context)
			}

			dnsSubdomainName, err := GetValidDNSSubdomainName(context.Name)
			if err != nil {
				framework.Failf("Could not convert context name %s to a valid dns subdomain name, error: %s", context.Name, err)
			}
			e2eContexts = append(e2eContexts, E2EContext{
				RawName: context.Name,
				Name:    dnsSubdomainName,
				Cluster: cluster,
				User:    user,
			})
		}
	}

	return e2eContexts
}
Example #14
0
File: disk.go Project: exekias/juju
func (d *diskStore) readJENVFile(envName string) (*environInfo, error) {
	path := jenvFilename(d.dir, envName)
	data, err := ioutil.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, errors.NotFoundf("model %q", envName)
		}
		return nil, err
	}
	var info environInfo
	info.path = path
	if len(data) == 0 {
		return &info, nil
	}
	var values EnvironInfoData
	if err := goyaml.Unmarshal(data, &values); err != nil {
		return nil, errors.Annotatef(err, "error unmarshalling %q", path)
	}
	info.name = envName
	info.user = values.User
	info.credentials = values.Password
	info.environmentUUID = values.ModelUUID
	info.serverUUID = values.ServerUUID
	info.caCert = values.CACert
	info.apiEndpoints = values.Controllers
	info.apiHostnames = values.ServerHostnames
	info.bootstrapConfig = values.Config

	info.source = sourceJenv
	return &info, nil
}
Example #15
0
func loadConfig(agentConfig agent.Config) (*config, error) {
	config := &config{
		storageDir:  agentConfig.Value(StorageDir),
		storageAddr: agentConfig.Value(StorageAddr),
		authkey:     agentConfig.Value(StorageAuthKey),
	}

	caCertPEM := agentConfig.Value(StorageCACert)
	if len(caCertPEM) > 0 {
		config.caCertPEM = caCertPEM
	}

	caKeyPEM := agentConfig.Value(StorageCAKey)
	if len(caKeyPEM) > 0 {
		config.caKeyPEM = caKeyPEM
	}

	hostnames := agentConfig.Value(StorageHostnames)
	if len(hostnames) > 0 {
		err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames)
		if err != nil {
			return nil, err
		}
	}

	return config, nil
}
Example #16
0
File: main.go Project: jbuck/rack
func main() {
	flag.Parse()

	var manifest Manifest

	if stat, _ := os.Stdin.Stat(); stat.Mode()&os.ModeCharDevice == 0 {
		man, err := ioutil.ReadAll(os.Stdin)

		if err != nil {
			die(err)
		}

		err = yaml.Unmarshal(man, &manifest)

		if err != nil {
			die(err)
		}
	}

	data, err := buildTemplate(flagMode, "formation", randomPort, manifest)

	if err != nil {
		displaySyntaxError(data, err)
		die(err)
	}

	fmt.Println(data)
}
Example #17
0
// applicationSetSettingsYAML updates the settings for the given application,
// taking the configuration from a YAML string.
func applicationSetSettingsYAML(appName string, application Application, settings string) error {
	b := []byte(settings)
	var all map[string]interface{}
	if err := goyaml.Unmarshal(b, &all); err != nil {
		return errors.Annotate(err, "parsing settings data")
	}
	// The file is already in the right format.
	if _, ok := all[appName]; !ok {
		changes, err := settingsFromGetYaml(all)
		if err != nil {
			return errors.Annotate(err, "processing YAML generated by get")
		}
		return errors.Annotate(application.UpdateConfigSettings(changes), "updating settings with application YAML")
	}

	ch, _, err := application.Charm()
	if err != nil {
		return errors.Annotate(err, "obtaining charm for this application")
	}

	changes, err := ch.Config().ParseSettingsYAML(b, appName)
	if err != nil {
		return errors.Annotate(err, "creating config from YAML")
	}
	return errors.Annotate(application.UpdateConfigSettings(changes), "updating settings")
}
Example #18
0
func newConfigFromBytes(d []byte) (*Config, error) {
	out := NewDefaultConfig()
	if err := yaml.Unmarshal(d, out); err != nil {
		return nil, fmt.Errorf("failed decoding config file: %v", err)
	}

	if err := out.valid(); err != nil {
		return nil, fmt.Errorf("config file invalid: %v", err)
	}

	//TODO: this will look different once we support multiple controllers
	out.ETCDEndpoints = fmt.Sprintf("http://%s:2379", out.ControllerIP)
	out.APIServers = fmt.Sprintf("http://%s:8080", out.ControllerIP)
	out.SecureAPIServers = fmt.Sprintf("https://%s:443", out.ControllerIP)
	out.APIServerEndpoint = fmt.Sprintf("https://%s", out.ExternalDNSName)

	if out.WorkerSpotPrice == "" {
		out.MinWorkersASG = 0
	} else {
		out.MinWorkersASG = out.WorkerCount
	}

	out.MaxWorkersASG = out.WorkerCount + 1

	if out.AMI == "" {
		var err error
		if out.AMI, err = getAMI(out.Region, out.ReleaseChannel); err != nil {
			return nil, fmt.Errorf("Error getting region map: %v", err)
		}
	}

	return out, nil
}
Example #19
0
func TestConfig(t *testing.T) {
	filePath := "./config_test.yaml"
	bytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		t.Fatal("read file error: ", err)
	}

	config := new(Config)
	yaml.Unmarshal([]byte(bytes), config)

	if config.Server.Port != 8066 {
		t.Fatal("Read Config Server.Port Error: ", config.Server.Port)
	}
	if config.Server.Ip != "127.0.0.1" {
		t.Fatal("Read Config Server.Ip Error: ", config.Server.Ip)
	}
	if len(config.Kafka.Nodes) != 2 {
		t.Fatal("Read Config Kafka.Nodes Error: ", config.Kafka.Nodes)
	}
	if len(config.Redis.Read) != 1 {
		t.Fatal("Read Config Redis.Read Error: ", config.Redis.Read)
	}
	if len(config.Redis.Write) != 1 {
		t.Fatal("Read Config Redis.Write Error: ", config.Redis.Write)
	}
}
Example #20
0
func main() {
	name := os.Args[1]
	buf, err := ioutil.ReadFile(name)
	if err != nil {
		panic(err)
	}
	var schema Schema
	err = yaml.Unmarshal(buf, &schema)
	if err != nil {
		panic(err)
	}

	fmt.Printf("properties=%v\n", schema.Properties)

	fmt.Printf("\n\n/////\n")
	tag := strings.Split(schema.Tag, "/")
	tname := strings.Split(tag[len(tag)-1], "-")[0]
	fmt.Printf("type %s struct {\n", snaker.SnakeToCamel(tname))
	for k, prop := range schema.Properties {
		fmt.Printf("\t%s %v `yaml:\"%s\"` // %q\n",
			snaker.SnakeToCamel(k),
			prop.Type, k,
			strings.TrimSpace(prop.Description),
		)
		//fmt.Printf("// %#v\n", prop)
	}
	fmt.Printf("}\n")
}
/*
TODO(colhom): when we fully deprecate instanceCIDR/availabilityZone, this block of
logic will go away and be replaced by a single constant string
*/
func defaultConfigValues(t *testing.T, configYaml string) string {
	defaultYaml := `
externalDNSName: test.staging.core-os.net
keyName: test-key-name
region: us-west-1
clusterName: test-cluster-name
kmsKeyArn: "arn:aws:kms:us-west-1:xxxxxxxxx:key/xxxxxxxxxxxxxxxxxxx"
`
	yamlStr := defaultYaml + configYaml

	c := config.Cluster{}
	if err := yaml.Unmarshal([]byte(yamlStr), &c); err != nil {
		t.Errorf("failed umarshalling config yaml: %v :\n%s", err, yamlStr)
	}

	if len(c.Subnets) > 0 {
		for i := range c.Subnets {
			c.Subnets[i].AvailabilityZone = fmt.Sprintf("dummy-az-%d", i)
		}
	} else {
		//Legacy behavior
		c.AvailabilityZone = "dummy-az-0"
	}

	out, err := yaml.Marshal(&c)
	if err != nil {
		t.Errorf("error marshalling cluster: %v", err)
	}

	return string(out)
}
Example #22
0
func loadConf(path string) (cfg conf, err error) {
	var data []byte
	// read configuration from stdin if no config file was
	// passed in command line arguments
	if *config == "" {
		data, err = ioutil.ReadAll(os.Stdin)
	} else {
		data, err = ioutil.ReadFile(*config)
	}
	if err != nil {
		return
	}
	// try to decrypt the conf using sops or load it as plaintext
	// if it's not encrypted
	decryptedConf, err := decryptConf(data)
	if err != nil {
		// decryption would have failed if the file is not encrypted,
		// in which case simply continue loading as yaml. But if the
		// file is encrypted and decryption failed, exit here.
		if err != sops.MetadataNotFound {
			return
		}
	} else {
		data = decryptedConf
	}
	err = yaml.Unmarshal(data, &cfg)
	return
}
Example #23
0
func (f *Framework) GetUnderlyingFederatedContexts() []E2EContext {
	if !f.federated {
		Failf("geUnderlyingFederatedContexts called on non-federated framework")
	}

	kubeconfig := KubeConfig{}
	configBytes, err := ioutil.ReadFile(TestContext.KubeConfig)
	ExpectNoError(err)
	err = yaml.Unmarshal(configBytes, &kubeconfig)
	ExpectNoError(err)

	e2eContexts := []E2EContext{}
	for _, context := range kubeconfig.Contexts {
		if strings.HasPrefix(context.Name, "federation-e2e") {

			user := kubeconfig.findUser(context.Context.User)
			if user == nil {
				Failf("Could not find user for context %+v", context)
			}

			cluster := kubeconfig.findCluster(context.Context.Cluster)
			if cluster == nil {
				Failf("Could not find cluster for context %+v", context)
			}

			e2eContexts = append(e2eContexts, E2EContext{
				Name:    context.Name,
				Cluster: cluster,
				User:    user,
			})
		}
	}

	return e2eContexts
}
Example #24
0
func (*cloudinitSuite) TestCloudInitConfigureBootstrapLogging(c *gc.C) {
	loggo.GetLogger("").SetLogLevel(loggo.INFO)
	envConfig := minimalEnvironConfig(c)
	instConfig := makeBootstrapConfig("quantal").maybeSetEnvironConfig(envConfig)
	rendered := instConfig.render()
	cloudcfg, err := cloudinit.New(rendered.Series)
	c.Assert(err, jc.ErrorIsNil)
	udata, err := cloudconfig.NewUserdataConfig(&rendered, cloudcfg)

	c.Assert(err, jc.ErrorIsNil)
	err = udata.Configure()
	c.Assert(err, jc.ErrorIsNil)
	data, err := cloudcfg.RenderYAML()
	c.Assert(err, jc.ErrorIsNil)
	configKeyValues := make(map[interface{}]interface{})
	err = goyaml.Unmarshal(data, &configKeyValues)
	c.Assert(err, jc.ErrorIsNil)

	scripts := getScripts(configKeyValues)
	for i, script := range scripts {
		if strings.Contains(script, "bootstrap") {
			c.Logf("scripts[%d]: %q", i, script)
		}
	}
	expected := "jujud bootstrap-state --data-dir '.*' --env-config '.*'" +
		" --instance-id '.*' --bootstrap-constraints 'mem=4096M'" +
		" --environ-constraints 'mem=2048M' --show-log"
	assertScriptMatch(c, scripts, expected, false)
}
Example #25
0
func TestRunOrder(t *testing.T) {
	var m Manifest
	data := []byte(`web:
  links:
    - postgres
    - redis
worker_2:
  links:
    - postgres
    - redis
worker_1:
  links:
    - postgres
    - redis
redis:
  image: convox/redis
postgres:
  image: convox/postgres
`)

	_ = yaml.Unmarshal(data, &m)

	cases := Cases{
		{m.runOrder(), []string{"postgres", "redis", "web", "worker_1", "worker_2"}},
	}

	_assert(t, cases)
}
Example #26
0
// ReadEnvironsBytes parses the contents of an environments.yaml file
// and returns its representation. An environment with an unknown type
// will only generate an error when New is called for that environment.
// Attributes for environments with known types are checked.
func ReadEnvironsBytes(data []byte) (*Environs, error) {
	var raw struct {
		Default      string
		Environments map[string]map[string]interface{}
	}
	err := goyaml.Unmarshal(data, &raw)
	if err != nil {
		return nil, err
	}

	if raw.Default != "" && raw.Environments[raw.Default] == nil {
		return nil, errors.Errorf("default model %q does not exist", raw.Default)
	}
	if raw.Default == "" {
		// If there's a single environment, then we get the default
		// automatically.
		if len(raw.Environments) == 1 {
			for name := range raw.Environments {
				raw.Default = name
				break
			}
		}
	}
	for name, attrs := range raw.Environments {
		// store the name of the this environment in the config itself
		// so that providers can see it.
		attrs["name"] = name
	}
	return &Environs{raw.Default, raw.Environments}, nil
}
Example #27
0
// serviceSetSettingsYAML updates the settings for the given service,
// taking the configuration from a YAML string.
func serviceSetSettingsYAML(service *state.Service, settings string) error {
	b := []byte(settings)
	var all map[string]interface{}
	if err := goyaml.Unmarshal(b, &all); err != nil {
		return errors.Annotate(err, "parsing settings data")
	}
	// The file is already in the right format.
	if _, ok := all[service.Name()]; !ok {
		changes, err := settingsFromGetYaml(all)
		if err != nil {
			return errors.Annotate(err, "processing YAML generated by get")
		}
		return errors.Annotate(service.UpdateConfigSettings(changes), "updating settings with service YAML")
	}

	ch, _, err := service.Charm()
	if err != nil {
		return errors.Annotate(err, "obtaining charm for this service")
	}

	changes, err := ch.Config().ParseSettingsYAML(b, service.Name())
	if err != nil {
		return errors.Annotate(err, "creating config from YAML")
	}
	return errors.Annotate(service.UpdateConfigSettings(changes), "updating settings")
}
func FromYaml(YAML string) (dsc *ServiceDescriptor) {
	dsc = new(ServiceDescriptor)
	if err := yaml.Unmarshal([]byte(YAML), dsc); err != nil {
		panic(fmt.Sprint("Insane ServiceDescriptor", err))
	}
	return
}
Example #29
0
func (s *statusGetSuite) TestOutputFormatJustStatus(c *gc.C) {
	for i, t := range statusGetTests {
		c.Logf("test %d: %#v", i, t.args)
		hctx := s.GetStatusHookContext(c)
		setFakeStatus(hctx)
		com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
		c.Assert(err, jc.ErrorIsNil)
		ctx := testing.Context(c)
		code := cmd.Main(com, ctx, t.args)
		c.Assert(code, gc.Equals, 0)
		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")

		var out interface{}
		var outMap map[string]interface{}
		switch t.format {
		case formatYaml:
			c.Check(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
			out = outMap
		case formatJson:
			c.Check(json.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
			out = outMap
		default:
			out = string(bufferBytes(ctx.Stdout))
		}
		c.Check(out, gc.DeepEquals, t.out)
	}
}
Example #30
0
func SaveAuthToken(configPath, authtoken string) (err error) {
	// empty configuration by default for the case that we can't read it
	c := new(Configuration)

	// read the configuration
	oldConfigBytes, err := ioutil.ReadFile(configPath)
	if err == nil {
		// unmarshal if we successfully read the configuration file
		if err = goyaml.Unmarshal(oldConfigBytes, c); err != nil {
			return
		}
	}

	// no need to save, the authtoken is already the correct value
	if c.AuthToken == authtoken {
		return
	}

	// update auth token
	c.AuthToken = authtoken

	// rewrite configuration
	newConfigBytes, err := goyaml.Marshal(c)
	if err != nil {
		return
	}

	err = ioutil.WriteFile(configPath, newConfigBytes, 0600)
	return
}