// WriteEnvironments creates an environments file with envConfig and certs // from certNames. func WriteEnvironments(c *gc.C, envConfig string, certNames ...string) { envs := osenv.JujuXDGDataHomePath("environments.yaml") err := ioutil.WriteFile(envs, []byte(envConfig), 0644) c.Assert(err, jc.ErrorIsNil) for _, name := range certNames { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath(name+"-cert.pem"), []byte(CACert), 0600) c.Assert(err, jc.ErrorIsNil) err = ioutil.WriteFile(osenv.JujuXDGDataHomePath(name+"-private-key.pem"), []byte(CAKey), 0600) c.Assert(err, jc.ErrorIsNil) } }
// InitJujuXDGDataHome initializes the charm cache, environs/config and utils/ssh packages // to use default paths based on the $JUJU_DATA or $HOME environment variables. // This function should be called before running a Juju CLI command. func InitJujuXDGDataHome() error { jujuXDGDataHome := osenv.JujuXDGDataHomeDir() if jujuXDGDataHome == "" { return errors.New("cannot determine juju data home, required environment variables are not set") } charmrepo.CacheDir = osenv.JujuXDGDataHomePath("charmcache") if err := ssh.LoadClientKeys(osenv.JujuXDGDataHomePath("ssh")); err != nil { return errors.Annotate(err, "cannot load ssh client keys") } return nil }
func (s *JujuXDGDataHomeSuite) TestErrorHome(c *gc.C) { // Invalid juju home leads to panic when retrieving. f := func() { _ = osenv.JujuXDGDataHome() } c.Assert(f, gc.PanicMatches, "juju home hasn't been initialized") f = func() { _ = osenv.JujuXDGDataHomePath("current-environment") } c.Assert(f, gc.PanicMatches, "juju home hasn't been initialized") }
func (s *personalCloudSuite) TestReadUserSpecifiedClouds(c *gc.C) { file := osenv.JujuXDGDataHomePath("somemoreclouds.yaml") s.setupReadClouds(c, file) clouds, err := cloud.ParseCloudMetadataFile(file) c.Assert(err, jc.ErrorIsNil) s.assertPersonalClouds(c, clouds) }
func (s *showSuite) TestShowWithRegionConfig(c *gc.C) { data := ` clouds: homestack: type: openstack description: Openstack Cloud auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 region-config: london: bootstrap-timeout: 1800 `[1:] err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600) ctx, err := testing.RunCommand(c, cloud.NewShowCloudCommand(), "homestack") c.Assert(err, jc.ErrorIsNil) out := testing.Stdout(ctx) c.Assert(out, gc.Equals, ` defined: local type: openstack description: Openstack Cloud auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 region-config: london: bootstrap-timeout: 1800 `[1:]) }
func (s *AccountsFileSuite) TestReadEmptyFile(c *gc.C) { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("accounts.yaml"), []byte(""), 0600) c.Assert(err, jc.ErrorIsNil) accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath()) c.Assert(err, jc.ErrorIsNil) c.Assert(accounts, gc.HasLen, 0) }
func (s *ModelsFileSuite) TestReadEmptyFile(c *gc.C) { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("models.yaml"), []byte(""), 0600) c.Assert(err, jc.ErrorIsNil) models, err := jujuclient.ReadModelsFile(jujuclient.JujuModelsPath()) c.Assert(err, jc.ErrorIsNil) c.Assert(models, gc.HasLen, 0) }
func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, UserAliasesFilename: osenv.JujuXDGDataHomePath("aliases"), }) jcmd.AddHelpTopic("basics", "Basic commands", helptopics.Basics) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helptopics.OpenstackProvider, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helptopics.EC2Provider, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helptopics.HPCloud, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helptopics.AzureProvider, "azure") jcmd.AddHelpTopic("maas-provider", "How to configure a MAAS provider", helptopics.MAASProvider, "maas") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helptopics.Constraints) jcmd.AddHelpTopic("placement", "How to use placement directives", helptopics.Placement) jcmd.AddHelpTopic("spaces", "How to configure more complex networks using spaces", helptopics.Spaces, "networking") jcmd.AddHelpTopic("glossary", "Glossary of terms", helptopics.Glossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helptopics.Logging) jcmd.AddHelpTopic("juju", "What is Juju?", helptopics.Juju) jcmd.AddHelpTopic("controllers", "About Juju Controllers", helptopics.JujuControllers) jcmd.AddHelpTopic("users", "About users in Juju", helptopics.Users) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) return jcmd }
func (s *CredentialsFileSuite) TestReadEmptyFile(c *gc.C) { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("credentials.yaml"), []byte(""), 0600) c.Assert(err, jc.ErrorIsNil) credentialstore := jujuclient.NewFileCredentialStore() _, err = credentialstore.CredentialForCloud("foo") c.Assert(err, jc.Satisfies, errors.IsNotFound) }
func (s *ControllersFileSuite) TestReadEmptyFile(c *gc.C) { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("controllers.yaml"), []byte(""), 0600) c.Assert(err, jc.ErrorIsNil) controllerStore := jujuclient.NewFileClientStore() controllers, err := controllerStore.AllControllers() c.Assert(err, jc.ErrorIsNil) c.Assert(controllers, gc.IsNil) }
func (s *BootstrapConfigFileSuite) TestReadEmptyFile(c *gc.C) { path := osenv.JujuXDGDataHomePath("bootstrap-config.yaml") err := ioutil.WriteFile(path, []byte(""), 0600) c.Assert(err, jc.ErrorIsNil) configs, err := jujuclient.ReadBootstrapConfigFile(path) c.Assert(err, jc.ErrorIsNil) c.Assert(configs, gc.HasLen, 0) }
// WriteConfig writes a juju config file to the "home" directory. func (s *JujuConnSuite) WriteConfig(configData string) { if s.RootDir == "" { panic("SetUpTest has not been called; will not overwrite $JUJU_HOME/environments.yaml") } path := osenv.JujuXDGDataHomePath("environments.yaml") err := ioutil.WriteFile(path, []byte(configData), 0600) if err != nil { panic(err) } }
// NewJujuCommand ... func NewJujuCommand(ctx *cmd.Context) cmd.Command { jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, UserAliasesFilename: osenv.JujuXDGDataHomePath("aliases"), }) jcmd.AddHelpTopic("basics", "Basic Help Summary", usageHelp) registerCommands(jcmd, ctx) return jcmd }
func (s *personalCloudSuite) TestWritePersonalClouds(c *gc.C) { clouds := map[string]cloud.Cloud{ "homestack": cloud.Cloud{ Type: "openstack", AuthTypes: []cloud.AuthType{"userpass", "access-key"}, Endpoint: "http://homestack", Regions: []cloud.Region{ cloud.Region{Name: "london", Endpoint: "http://london/1.0"}, }, }, "azurestack": cloud.Cloud{ Type: "azure", AuthTypes: []cloud.AuthType{"userpass"}, Regions: []cloud.Region{{ Name: "prod", Endpoint: "http://prod.azurestack.local", }, { Name: "dev", Endpoint: "http://dev.azurestack.local", }, { Name: "test", Endpoint: "http://test.azurestack.local", }}, }, } err := cloud.WritePersonalCloudMetadata(clouds) c.Assert(err, jc.ErrorIsNil) data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) c.Assert(err, jc.ErrorIsNil) c.Assert(string(data), gc.Equals, ` clouds: azurestack: type: azure auth-types: [userpass] regions: prod: endpoint: http://prod.azurestack.local dev: endpoint: http://dev.azurestack.local test: endpoint: http://test.azurestack.local homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 `[1:]) }
func (s *removeSuite) createTestCloudData(c *gc.C) { err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("public-clouds.yaml"), []byte(` clouds: prodstack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack `[1:]), 0600) c.Assert(err, jc.ErrorIsNil) err = ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(` clouds: homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack homestack2: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack2 `[1:]), 0600) c.Assert(err, jc.ErrorIsNil) }
func (s *addSuite) TestAddExistingReplace(c *gc.C) { sourceFile := s.createTestCloudData(c) _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "homestack", sourceFile, "--replace") c.Assert(err, jc.ErrorIsNil) data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) c.Assert(string(data), gc.Equals, ` clouds: homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 new-york: endpoint: http://newyork/1.0 `[1:]) }
func (s *addSuite) createTestCloudData(c *gc.C) string { current := ` clouds: homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 `[1:] err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(current), 0600) c.Assert(err, jc.ErrorIsNil) sourceDir := c.MkDir() sourceFile := filepath.Join(sourceDir, "someclouds.yaml") source := ` clouds: aws: type: ec2 auth-types: [ access-key ] regions: us-east-1: endpoint: https://us-east-1.aws.amazon.com/v1.2/ localhost: type: lxd homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 new-york: endpoint: http://newyork/1.0 garage-maas: type: mass auth-types: [oauth] endpoint: http://garagemaas `[1:] err = ioutil.WriteFile(sourceFile, []byte(source), 0600) c.Assert(err, jc.ErrorIsNil) return sourceFile }
func (s *addSuite) TestAddNew(c *gc.C) { sourceFile := s.createTestCloudData(c) _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "garage-maas", sourceFile) c.Assert(err, jc.ErrorIsNil) data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) c.Assert(string(data), gc.Equals, ` clouds: garage-maas: type: mass auth-types: [oauth] endpoint: http://garagemaas homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 `[1:]) }
func (s *NewAPIClientSuite) TestWithBootstrapConfigAndNoEnvironmentsFile(c *gc.C) { s.PatchValue(&version.Current, coretesting.FakeVersionNumber) coretesting.MakeSampleJujuHome(c) store := configstore.NewMem() s.bootstrapEnv(c, coretesting.SampleModelName, store) info, err := store.ReadInfo(coretesting.SampleModelName) c.Assert(err, jc.ErrorIsNil) c.Assert(info.BootstrapConfig(), gc.NotNil) c.Assert(info.APIEndpoint().Addresses, gc.HasLen, 0) err = os.Remove(osenv.JujuXDGDataHomePath("environments.yaml")) c.Assert(err, jc.ErrorIsNil) apiOpen := func(*api.Info, api.DialOpts) (api.Connection, error) { return mockedAPIState(noFlags), nil } st, err := juju.NewAPIFromStore(coretesting.SampleModelName, store, apiOpen) c.Check(err, jc.ErrorIsNil) st.Close() }
func (s *listSuite) TestListPublicAndPersonalSameName(c *gc.C) { data := ` clouds: aws: type: ec2 auth-types: [access-key] endpoint: http://custom `[1:] err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600) c.Assert(err, jc.ErrorIsNil) ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand(), "--format", "yaml") c.Assert(err, jc.ErrorIsNil) out := testing.Stdout(ctx) out = strings.Replace(out, "\n", "", -1) // Just check a snippet of the output to make sure it looks ok. // local clouds are last. c.Assert(out, gc.Not(gc.Matches), `.*aws:[ ]*defined: public[ ]*type: ec2[ ]*auth-types: \[access-key\].*`) c.Assert(out, gc.Matches, `.*aws:[ ]*defined: local[ ]*type: ec2[ ]*auth-types: \[access-key\].*`) }
func (s *listSuite) TestListPublicAndPersonal(c *gc.C) { data := ` clouds: homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 `[1:] err := ioutil.WriteFile(osenv.JujuXDGDataHomePath("clouds.yaml"), []byte(data), 0600) c.Assert(err, jc.ErrorIsNil) ctx, err := testing.RunCommand(c, cloud.NewListCloudsCommand()) c.Assert(err, jc.ErrorIsNil) out := testing.Stdout(ctx) out = strings.Replace(out, "\n", "", -1) // Just check a snippet of the output to make sure it looks ok. // local: clouds are last. c.Assert(out, gc.Matches, `.*local\:homestack[ ]*openstack[ ]*london$`) }
func (s *addSuite) TestAddExistingPublicReplace(c *gc.C) { sourceFile := s.createTestCloudData(c) _, err := testing.RunCommand(c, cloud.NewAddCloudCommand(), "aws", sourceFile, "--replace") c.Assert(err, jc.ErrorIsNil) data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("clouds.yaml")) c.Assert(string(data), gc.Equals, ` clouds: aws: type: ec2 auth-types: [access-key] regions: us-east-1: endpoint: https://us-east-1.aws.amazon.com/v1.2/ homestack: type: openstack auth-types: [userpass, access-key] endpoint: http://homestack regions: london: endpoint: http://london/1.0 `[1:]) }
// readFileAttr reads the contents of an attribute from a file, if the // corresponding "-path" attribute is set, or otherwise from a default // path. func readFileAttr(attrs map[string]interface{}, key, defaultPath string) (content string, userSpecified bool, _ error) { path, ok := attrs[key+"-path"].(string) if ok { userSpecified = true } else { path = defaultPath } absPath, err := utils.NormalizePath(path) if err != nil { return "", userSpecified, errors.Trace(err) } if !filepath.IsAbs(absPath) { absPath = osenv.JujuXDGDataHomePath(absPath) } data, err := ioutil.ReadFile(absPath) if err != nil { return "", userSpecified, errors.Annotatef(err, "%q not set, and could not read from %q", key, path) } if len(data) == 0 { return "", userSpecified, errors.Errorf("file %q is empty", path) } return string(data), userSpecified, nil }
// JujuControllersPath is the location where controllers information is // expected to be found. func JujuControllersPath() string { return osenv.JujuXDGDataHomePath("controllers.yaml") }
// JujuPublicCloudsPath is the location where public cloud information is // expected to be found. Requires JUJU_HOME to be set. func JujuPublicCloudsPath() string { return osenv.JujuXDGDataHomePath("public-clouds.yaml") }
func (s *ModelsFileSuite) TestWriteFile(c *gc.C) { writeTestModelsFile(c) data, err := ioutil.ReadFile(osenv.JujuXDGDataHomePath("models.yaml")) c.Assert(err, jc.ErrorIsNil) c.Assert(string(data), gc.Equals, testModelsYAML[1:]) }
func environsPath(path string) string { if path == "" { path = osenv.JujuXDGDataHomePath("environments.yaml") } return path }
func (s *personalCloudSuite) TestReadPersonalClouds(c *gc.C) { s.setupReadClouds(c, osenv.JujuXDGDataHomePath("clouds.yaml")) clouds, err := cloud.PersonalCloudMetadata() c.Assert(err, jc.ErrorIsNil) s.assertPersonalClouds(c, clouds) }
func (s *JujuXDGDataHomeSuite) TestHomePath(c *gc.C) { testJujuHome := c.MkDir() osenv.SetJujuXDGDataHome(testJujuHome) envPath := osenv.JujuXDGDataHomePath("current-environment") c.Assert(envPath, gc.Equals, filepath.Join(testJujuHome, "current-environment")) }
// JujuBootstrapConfigPath is the location where bootstrap config is // expected to be found. func JujuBootstrapConfigPath() string { return osenv.JujuXDGDataHomePath("bootstrap-config.yaml") }