本文整理汇总了Golang中launchpad/net/juju-core/juju.NewConnFromName函数的典型用法代码示例。如果您正苦于以下问题:Golang NewConnFromName函数的具体用法?Golang NewConnFromName怎么用?Golang NewConnFromName使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewConnFromName函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestNewConnFromName
func (*NewConnSuite) TestNewConnFromName(c *C) {
home := c.MkDir()
defer os.Setenv("HOME", os.Getenv("HOME"))
os.Setenv("HOME", home)
conn, err := juju.NewConnFromName("")
c.Assert(conn, IsNil)
c.Assert(err, ErrorMatches, ".*: no such file or directory")
if err := os.Mkdir(filepath.Join(home, ".juju"), 0755); err != nil {
c.Fatal("Could not create directory structure")
}
envs := filepath.Join(home, ".juju", "environments.yaml")
err = ioutil.WriteFile(envs, []byte(`
default:
erewhemos
environments:
erewhemos:
type: dummy
state-server: true
authorized-keys: i-am-a-key
admin-secret: conn-from-name-secret
`), 0644)
err = ioutil.WriteFile(filepath.Join(home, ".juju", "erewhemos-cert.pem"), []byte(coretesting.CACert), 0600)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(home, ".juju", "erewhemos-private-key.pem"), []byte(coretesting.CAKey), 0600)
c.Assert(err, IsNil)
// Just run through a few operations on the dummy provider and verify that
// they behave as expected.
conn, err = juju.NewConnFromName("")
c.Assert(err, ErrorMatches, "dummy environment not bootstrapped")
environ, err := environs.NewFromName("")
c.Assert(err, IsNil)
err = environs.Bootstrap(environ, false, panicWrite)
c.Assert(err, IsNil)
conn, err = juju.NewConnFromName("")
c.Assert(err, IsNil)
defer conn.Close()
c.Assert(conn.Environ, NotNil)
c.Assert(conn.Environ.Name(), Equals, "erewhemos")
c.Assert(conn.State, NotNil)
// Reset the admin password so the state db can be reused.
err = conn.State.SetAdminMongoPassword("")
c.Assert(err, IsNil)
// Close the conn (thereby closing its state) a couple of times to
// verify that multiple closes will not panic. We ignore the error,
// as the underlying State will return an error the second
// time.
conn.Close()
conn.Close()
}
示例2: Run
func (c *StatusCommand) Run(ctx *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
var context statusContext
if context.machines, err = fetchAllMachines(conn.State); err != nil {
return err
}
if context.services, context.units, err = fetchAllServicesAndUnits(conn.State); err != nil {
return err
}
context.instances, err = fetchAllInstances(conn.Environ)
if err != nil {
// We cannot see instances from the environment, but
// there's still lots of potentially useful info to print.
fmt.Fprintf(ctx.Stderr, "cannot retrieve instances from the environment: %v\n", err)
}
result := struct {
Machines map[string]machineStatus `json:"machines"`
Services map[string]serviceStatus `json:"services"`
}{
Machines: context.processMachines(),
Services: context.processServices(),
}
return c.out.Write(ctx, result)
}
示例3: Run
func (c *SetEnvironmentCommand) Run(ctx *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
// Here is the magic around setting the attributes:
// TODO(thumper): get this magic under test somewhere, and update other call-sites to use it.
// Get the existing environment config from the state.
oldConfig, err := conn.State.EnvironConfig()
if err != nil {
return err
}
// Apply the attributes specified for the command to the state config.
newConfig, err := oldConfig.Apply(c.values)
if err != nil {
return err
}
// Now validate this new config against the existing config via the provider.
provider := conn.Environ.Provider()
newProviderConfig, err := provider.Validate(newConfig, oldConfig)
if err != nil {
return err
}
// Now try to apply the new validated config.
return conn.State.SetEnvironConfig(newProviderConfig)
}
示例4: Run
// Run fetches the configuration of the service and formats
// the result as a YAML string.
func (c *GetCommand) Run(ctx *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
svc, err := conn.State.Service(c.ServiceName)
if err != nil {
return err
}
svcfg, err := svc.Config()
if err != nil {
return err
}
charm, _, err := svc.Charm()
if err != nil {
return err
}
chcfg := charm.Config().Options
config := merge(svcfg.Map(), chcfg)
result := map[string]interface{}{
"service": svc.Name(),
"charm": charm.Meta().Name,
"settings": config,
}
return c.out.Write(ctx, result)
}
示例5: Run
// Run updates the configuration of a service.
func (c *SetCommand) Run(ctx *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
service, err := conn.State.Service(c.ServiceName)
if err != nil {
return err
}
ch, _, err := service.Charm()
if err != nil {
return err
}
var settings charm.Settings
if c.SettingsYAML.Path != "" {
settingsYAML, err := c.SettingsYAML.Read(ctx)
if err != nil {
return err
}
settings, err = ch.Config().ParseSettingsYAML(settingsYAML, c.ServiceName)
if err != nil {
return err
}
} else if len(c.SettingsStrings) > 0 {
settings, err = ch.Config().ParseSettingsStrings(c.SettingsStrings)
if err != nil {
return err
}
} else {
return nil
}
return service.UpdateConfigSettings(settings)
}
示例6: Run
// Run resolves c.Target to a machine, or host of a unit and
// forks ssh with c.Args, if provided.
func (c *SCPCommand) Run(ctx *cmd.Context) error {
var err error
c.Conn, err = juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer c.Close()
// translate arguments in the form 0:/somepath or service/0:/somepath into
// [email protected]:/somepath so they can be presented to scp.
for i := range c.Args {
// BUG(dfc) This will not work for IPv6 addresses like 2001:db8::1:2:/somepath.
if v := strings.SplitN(c.Args[i], ":", 2); len(v) > 1 {
host, err := c.hostFromTarget(v[0])
if err != nil {
return err
}
c.Args[i] = "[email protected]" + host + ":" + v[1]
}
}
args := []string{"-o", "StrictHostKeyChecking no", "-o", "PasswordAuthentication no"}
args = append(args, c.Args...)
cmd := exec.Command("scp", args...)
cmd.Stdin = ctx.Stdin
cmd.Stdout = ctx.Stdout
cmd.Stderr = ctx.Stderr
c.Close()
return cmd.Run()
}
示例7: Run
func (c *AddMachineCommand) Run(_ *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
series := c.Series
if series == "" {
conf, err := conn.State.EnvironConfig()
if err != nil {
return err
}
series = conf.DefaultSeries()
}
params := state.AddMachineParams{
ParentId: c.MachineId,
ContainerType: c.ContainerType,
Series: series,
Constraints: c.Constraints,
Jobs: []state.MachineJob{state.JobHostUnits},
}
m, err := conn.State.AddMachineWithConstraints(¶ms)
if err == nil {
if c.ContainerType == "" {
log.Infof("created machine %v", m)
} else {
log.Infof("created %q container on machine %v", c.ContainerType, m)
}
}
return err
}
示例8: TestNewConnFromNameNotSetGetsDefault
func (*NewConnSuite) TestNewConnFromNameNotSetGetsDefault(c *C) {
defer coretesting.MakeSampleHome(c).Restore()
bootstrapEnv(c, "")
conn, err := juju.NewConnFromName("")
c.Assert(err, IsNil)
defer conn.Close()
c.Assert(conn.Environ.Name(), Equals, coretesting.SampleEnvName)
}
示例9: Run
func (c *DestroyMachineCommand) Run(_ *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
return conn.State.DestroyMachines(c.MachineIds...)
}
示例10: Run
// Run connects to the environment specified on the command line
// and calls conn.DestroyUnits.
func (c *DestroyUnitCommand) Run(_ *cmd.Context) (err error) {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
return conn.DestroyUnits(c.UnitNames...)
}
示例11: Run
// Run updates the configuration of a service
func (c *SetCommand) Run(ctx *cmd.Context) error {
var unvalidated = make(map[string]string)
var remove []string
contents, err := c.Config.Read(ctx)
if err != nil && err != cmd.ErrNoPath {
return err
}
if len(contents) > 0 {
if err := goyaml.Unmarshal(contents, &unvalidated); err != nil {
return err
}
} else {
unvalidated, remove, err = parse(c.Options)
if err != nil {
return err
}
}
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
srv, err := conn.State.Service(c.ServiceName)
if err != nil {
return err
}
charm, _, err := srv.Charm()
if err != nil {
return err
}
// 1. Validate will convert this partial configuration
// into a full configuration by inserting charm defaults
// for missing values.
validated, err := charm.Config().Validate(unvalidated)
if err != nil {
return err
}
// 2. strip out the additional default keys added in the previous step.
validated = strip(validated, unvalidated)
cfg, err := srv.Config()
if err != nil {
return err
}
// 3. Update any keys that remain after validation and filtering.
if len(validated) > 0 {
log.Debugf("cmd/juju: updating configuration items: %v", validated)
cfg.Update(validated)
}
// 4. Delete any removed keys.
if len(remove) > 0 {
log.Debugf("cmd/juju: removing configuration items: %v", remove)
for _, k := range remove {
cfg.Delete(k)
}
}
_, err = cfg.Write()
return err
}
示例12: Run
// Run changes the juju-managed firewall to hide any
// ports that were also explicitly marked by units as closed.
func (c *UnexposeCommand) Run(_ *cmd.Context) error {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
params := params.ServiceUnexpose{ServiceName: c.ServiceName}
return statecmd.ServiceUnexpose(conn.State, params)
}
示例13: TestConnMultipleCloseOk
func (*NewConnSuite) TestConnMultipleCloseOk(c *C) {
defer coretesting.MakeSampleHome(c).Restore()
bootstrapEnv(c, "")
// Error return from here is tested in TestNewConnFromNameNotSetGetsDefault.
conn, _ := juju.NewConnFromName("")
conn.Close()
conn.Close()
conn.Close()
}
示例14: TestNewConnFromNameNotDefault
func (*NewConnSuite) TestNewConnFromNameNotDefault(c *C) {
defer coretesting.MakeMultipleEnvHome(c).Restore()
// The default environment is "erewhemos", so make sure we get what we ask for.
const envName = "erewhemos-2"
bootstrapEnv(c, envName)
conn, err := juju.NewConnFromName(envName)
c.Assert(err, IsNil)
defer conn.Close()
c.Assert(conn.Environ.Name(), Equals, envName)
}
示例15: Run
// Run connects to the environment specified on the command line and destroys
// units therein.
func (c *DestroyUnitCommand) Run(_ *cmd.Context) (err error) {
conn, err := juju.NewConnFromName(c.EnvName)
if err != nil {
return err
}
defer conn.Close()
params := params.DestroyServiceUnits{
UnitNames: c.UnitNames,
}
return statecmd.DestroyServiceUnits(conn.State, params)
}