// Main is not redundant with main(), because it provides an entry point // for testing with arbitrary command line arguments. func Main(args []string) int { defer func() { if r := recover(); r != nil { buf := make([]byte, 4096) buf = buf[:runtime.Stack(buf, false)] logger.Criticalf("Unhandled panic: \n%v\n%s", r, buf) os.Exit(exit_panic) } }() var code int = 1 ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(exit_err) } commandName := filepath.Base(args[0]) if commandName == names.Jujud { code, err = jujuDMain(args, ctx) } else if commandName == names.Jujuc { fmt.Fprint(os.Stderr, jujudDoc) code = exit_err err = fmt.Errorf("jujuc should not be called directly") } else if commandName == names.JujuRun { code = cmd.Main(&RunCommand{}, ctx, args[1:]) } else { code, err = jujuCMain(commandName, ctx, args) } if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) } return code }
func nullContext() *cmd.Context { ctx, _ := cmd.DefaultContext() ctx.Stdin = io.LimitReader(nil, 0) ctx.Stdout = ioutil.Discard ctx.Stderr = ioutil.Discard return ctx }
// Run is the main entry point for the juju client. func (m main) Run(args []string) int { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) return 2 } // note that this has to come before we init the juju home directory, // since it relies on detecting the lack of said directory. newInstall := m.maybeWarnJuju1x() if err = juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) return 2 } if newInstall { fmt.Fprintf(ctx.Stderr, "Since Juju %v is being run for the first time, downloading latest cloud information.\n", jujuversion.Current.Major) updateCmd := cloud.NewUpdateCloudsCommand() if err := updateCmd.Run(ctx); err != nil { fmt.Fprintf(ctx.Stderr, "error: %v\n", err) } } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) return 0 } jcmd := NewJujuCommand(ctx) return cmd.Main(jcmd, ctx, args[1:]) }
// Main registers subcommands for the juju-metadata executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err := juju.InitJujuHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } metadatacmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "metadata", UsagePrefix: "juju", Doc: metadataDoc, Purpose: "tools for generating and validating image and tools metadata", Log: &cmd.Log{}}) metadatacmd.Register(envcmd.Wrap(&ValidateImageMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ImageMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ToolsMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ValidateToolsMetadataCommand{})) metadatacmd.Register(&SignMetadataCommand{}) metadatacmd.Register(envcmd.Wrap(&ListImagesCommand{})) metadatacmd.Register(envcmd.Wrap(&AddImageMetadataCommand{})) os.Exit(cmd.Main(metadatacmd, ctx, args[1:])) }
func nullContext() environs.BootstrapContext { ctx, _ := cmd.DefaultContext() ctx.Stdin = io.LimitReader(nil, 0) ctx.Stdout = ioutil.Discard ctx.Stderr = ioutil.Discard return envcmd.BootstrapContext(ctx) }
// Main registers subcommands for the juju executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. // This function returns the exit code, for main to pass to os.Exit. func Main(args []string) int { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) return 2 } if shouldWarnJuju1x() { warnJuju1x() } if err = juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) return 2 } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) return 0 } jcmd := NewJujuCommand(ctx) return cmd.Main(jcmd, ctx, args[1:]) }
func main() { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } os.Exit(cmd.Main(&MigrateCommand{}, ctx, os.Args[1:])) }
func nullContext(c *gc.C) *cmd.Context { ctx, err := cmd.DefaultContext() c.Assert(err, gc.IsNil) ctx.Stdin = io.LimitReader(nil, 0) ctx.Stdout = ioutil.Discard ctx.Stderr = ioutil.Discard return ctx }
// Main registers subcommands for the juju-local executable. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { logger.Debugf("error: %v\n", err) os.Exit(2) } plugin := jujuLocalPlugin() os.Exit(cmd.Main(plugin, ctx, args[1:])) }
func (s *destroyEnvSuite) TestDestroyEnvironmentCommandConfirmation(c *gc.C) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, gc.IsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin // Prepare the environment so we can destroy it. env, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) assertEnvironNotDestroyed(c, env, s.ConfigStore) // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString("n") opc, errc := runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-errc, gc.ErrorMatches, "environment destruction aborted") c.Check(<-opc, gc.IsNil) c.Check(stdout.String(), gc.Matches, "WARNING!.*dummyenv.*\\(type: dummy\\)(.|\n)*") assertEnvironNotDestroyed(c, env, s.ConfigStore) // EOF on stdin: equivalent to answering no. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-opc, gc.IsNil) c.Check(<-errc, gc.ErrorMatches, "environment destruction aborted") assertEnvironNotDestroyed(c, env, s.ConfigStore) // "--yes" passed: no confirmation request. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv", "--yes") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") c.Check(stdout.String(), gc.Equals, "") assertEnvironDestroyed(c, env, s.ConfigStore) // Any of casing of "y" and "yes" will confirm. for _, answer := range []string{"y", "Y", "yes", "YES"} { // Prepare the environment so we can destroy it. s.Reset(c) env, err := environs.PrepareFromName("dummyenv", nullContext(c), s.ConfigStore) c.Assert(err, gc.IsNil) stdin.Reset() stdout.Reset() stdin.WriteString(answer) opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "dummyenv") c.Check(<-errc, gc.IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, gc.Equals, "dummyenv") c.Check(stdout.String(), gc.Matches, "WARNING!.*dummyenv.*\\(type: dummy\\)(.|\n)*") assertEnvironDestroyed(c, env, s.ConfigStore) } }
func runJujuCommand(c *gc.C, args ...string) (*cmd.Context, error) { // NOTE (alesstimec): Writers need to be reset, because // they are set globally in the juju/cmd package and will // return an error if we attempt to run two commands in the // same test. loggo.RemoveWriter("warning") ctx, err := cmd.DefaultContext() c.Assert(err, jc.ErrorIsNil) command := jujucmd.NewJujuCommand(ctx) return testing.RunCommand(c, command, args...) }
func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err := juju.InitJujuHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } os.Exit(cmd.Main(envcmd.Wrap(&restoreCommand{}), ctx, args[1:])) }
// Main registers subcommands for the juju-metadata executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err := juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } os.Exit(cmd.Main(NewSuperCommand(), ctx, args[1:])) }
// Main is the entry point for this plugins. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "could not obtain context for command: %v\n", err) os.Exit(2) } if err := juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } os.Exit(cmd.Main(modelcmd.Wrap(&upgradeMongoCommand{}), ctx, args[1:])) }
func (s *DestroySuite) TestDestroyCommandConfirmation(c *gc.C) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, jc.ErrorIsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString("n") _, errc := cmdtesting.RunCommand(ctx, s.NewDestroyCommand(), "test2") select { case err := <-errc: c.Check(err, gc.ErrorMatches, "environment destruction: aborted") case <-time.After(testing.LongWait): c.Fatalf("command took too long") } c.Check(testing.Stdout(ctx), gc.Matches, "WARNING!.*test2(.|\n)*") checkEnvironmentExistsInStore(c, "test1", s.store) // EOF on stdin: equivalent to answering no. stdin.Reset() stdout.Reset() _, errc = cmdtesting.RunCommand(ctx, s.NewDestroyCommand(), "test2") select { case err := <-errc: c.Check(err, gc.ErrorMatches, "environment destruction: aborted") case <-time.After(testing.LongWait): c.Fatalf("command took too long") } c.Check(testing.Stdout(ctx), gc.Matches, "WARNING!.*test2(.|\n)*") checkEnvironmentExistsInStore(c, "test1", s.store) for _, answer := range []string{"y", "Y", "yes", "YES"} { stdin.Reset() stdout.Reset() stdin.WriteString(answer) _, errc = cmdtesting.RunCommand(ctx, s.NewDestroyCommand(), "test2") select { case err := <-errc: c.Check(err, jc.ErrorIsNil) case <-time.After(testing.LongWait): c.Fatalf("command took too long") } checkEnvironmentRemovedFromStore(c, "test2", s.store) // Add the test2 environment back into the store for the next test s.resetEnvironment(c) } }
func main() { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } admcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "charm-admin", }) admcmd.Register(&DeleteCharmCommand{}) os.Exit(cmd.Main(admcmd, ctx, os.Args[1:])) }
func (s *CmdSuite) TestDefaultContextReturnsErrorInDeletedDirectory(c *gc.C) { ctx := cmdtesting.Context(c) wd, err := os.Getwd() c.Assert(err, gc.IsNil) missing := ctx.Dir + "/missing" err = os.Mkdir(missing, 0700) c.Assert(err, gc.IsNil) err = os.Chdir(missing) c.Assert(err, gc.IsNil) defer os.Chdir(wd) err = os.Remove(missing) c.Assert(err, gc.IsNil) ctx, err = cmd.DefaultContext() c.Assert(err, gc.ErrorMatches, `getwd: no such file or directory`) c.Assert(ctx, gc.IsNil) }
func (s *KillSuite) TestKillCommandConfirmation(c *gc.C) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, jc.ErrorIsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString("n") _, errc := cmdtesting.RunCommand(ctx, s.newKillCommand(), "local.test1") select { case err := <-errc: c.Check(err, gc.ErrorMatches, "controller destruction aborted") case <-time.After(testing.LongWait): c.Fatalf("command took too long") } c.Check(testing.Stdout(ctx), gc.Matches, "WARNING!.*local.test1(.|\n)*") checkControllerExistsInStore(c, "local.test1", s.store) }
// Main registers subcommands for the juju executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err = juju.InitJujuHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) os.Exit(0) } jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, MissingCallback: RunPlugin, }) jcmd.AddHelpTopic("basics", "Basic commands", helpBasics) jcmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider", helpProviderStart+helpLocalProvider+helpProviderEnd) jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helpProviderStart+helpOpenstackProvider+helpProviderEnd, "openstack") jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helpProviderStart+helpEC2Provider+helpProviderEnd, "ec2", "aws", "amazon") jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helpProviderStart+helpHPCloud+helpProviderEnd, "hpcloud", "hp-cloud") jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helpProviderStart+helpAzureProvider+helpProviderEnd, "azure") jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helpConstraints) jcmd.AddHelpTopic("glossary", "Glossary of terms", helpGlossary) jcmd.AddHelpTopic("logging", "How Juju handles logging", helpLogging) jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jcmd, ctx) os.Exit(cmd.Main(jcmd, ctx, args[1:])) }
func (s *UnitSuite) TestDontUseLumberjack(c *gc.C) { ctx, err := cmd.DefaultContext() c.Assert(err, gc.IsNil) a := UnitAgent{ AgentConf: FakeAgentConfig{}, ctx: ctx, UnitName: "mysql/25", // this is what would get set by the CLI flags to tell us not to log to // the file. logToStdErr: true, } err = a.Init(nil) c.Assert(err, gc.IsNil) _, ok := ctx.Stderr.(*lumberjack.Logger) c.Assert(ok, jc.IsFalse) }
func (s *UnitSuite) TestUseLumberjack(c *gc.C) { ctx, err := cmd.DefaultContext() c.Assert(err, gc.IsNil) a := UnitAgent{ AgentConf: FakeAgentConfig{}, ctx: ctx, UnitName: "mysql/25", } err = a.Init(nil) c.Assert(err, gc.IsNil) l, ok := ctx.Stderr.(*lumberjack.Logger) c.Assert(ok, jc.IsTrue) c.Check(l.MaxAge, gc.Equals, 0) c.Check(l.MaxBackups, gc.Equals, 2) c.Check(l.Filename, gc.Equals, filepath.FromSlash("/var/log/juju/machine-42.log")) c.Check(l.MaxSize, gc.Equals, 300) }
// Main registers subcommands for the juju executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err = juju.InitJujuHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) os.Exit(0) } jcmd := NewJujuCommand(ctx) os.Exit(cmd.Main(jcmd, ctx, args[1:])) }
// Main is not redundant with main(), because it provides an entry point // for testing with arbitrary command line arguments. func Main(args []string) int { defer func() { if r := recover(); r != nil { buf := make([]byte, 4096) buf = buf[:runtime.Stack(buf, false)] logger.Criticalf("Unhandled panic: \n%v\n%s", r, buf) os.Exit(exit_panic) } }() ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(exit_err) } code := 1 commandName := filepath.Base(args[0]) switch commandName { case names.Jujud: // start pprof server and defer cleanup stop := pprof.Start() defer stop() code, err = jujuDMain(args, ctx) case names.Jujuc: fmt.Fprint(os.Stderr, jujudDoc) code = exit_err err = fmt.Errorf("jujuc should not be called directly") case names.JujuRun: code = cmd.Main(&RunCommand{}, ctx, args[1:]) case names.JujuDumpLogs: code = cmd.Main(dumplogs.NewCommand(), ctx, args[1:]) default: code, err = jujuCMain(commandName, ctx, args) } if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) } return code }
func (s *UnregisterSuite) unregisterCommandConfirms(c *gc.C, answer string) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, jc.ErrorIsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin stdin.Reset() stdout.Reset() stdin.WriteString(answer) _, errc := cmdtesting.RunCommand(ctx, controller.NewUnregisterCommand(s.store), "fake1") select { case err, ok := <-errc: c.Assert(ok, jc.IsTrue) c.Check(err, jc.ErrorIsNil) case <-time.After(testing.LongWait): c.Fatalf("command took too long") } c.Check(s.store.lookupName, gc.Equals, "fake1") c.Check(s.store.removedName, gc.Equals, "fake1") }
func (s *UnregisterSuite) unregisterCommandAborts(c *gc.C, answer string) { var stdin, stdout bytes.Buffer ctx, err := cmd.DefaultContext() c.Assert(err, jc.ErrorIsNil) ctx.Stdout = &stdout ctx.Stdin = &stdin // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString(answer) _, errc := cmdtesting.RunCommand(ctx, controller.NewUnregisterCommand(s.store), "fake1") select { case err, ok := <-errc: c.Assert(ok, jc.IsTrue) c.Check(err, gc.ErrorMatches, "unregistering controller: aborted") case <-time.After(testing.LongWait): c.Fatalf("command took too long") } c.Check(testing.Stdout(ctx), gc.Equals, unregisterMsg) c.Check(s.store.lookupName, gc.Equals, "fake1") c.Check(s.store.removedName, gc.Equals, "") }
// Main registers subcommands for the juju-local executable. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { logger.Debugf("error: %v\n", err) os.Exit(2) } if err := juju.InitJujuXDGDataHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } plugin := jujucmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "tools", UsagePrefix: "juju", Doc: doc, Purpose: "manage tools in the controller", Log: &cmd.Log{}, }) plugin.Register(modelcmd.Wrap(&buildToolsCommand{})) plugin.Register(modelcmd.Wrap(&uploadToolsCommand{})) plugin.Register(modelcmd.Wrap(&listToolsCommand{})) os.Exit(cmd.Main(plugin, ctx, args[1:])) }
// Main is not redundant with main(), because it provides an entry point // for testing with arbitrary command line arguments. func Main(args []string) { var code int = 1 ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } commandName := filepath.Base(args[0]) if commandName == "jujud" { code, err = jujuDMain(args, ctx) } else if commandName == "jujuc" { fmt.Fprint(os.Stderr, jujudDoc) code = 2 err = fmt.Errorf("jujuc should not be called directly") } else if commandName == "juju-run" { code = cmd.Main(&RunCommand{}, ctx, args[1:]) } else { code, err = jujuCMain(commandName, args) } if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) } os.Exit(code) }