func main() { gearCmd := &cobra.Command{ Use: "gear", Short: "Gear(d) is a tool for installing Docker containers to systemd", Long: "A commandline client and server that installs Docker containers to systemd.\n\nComplete documentation is available at http://github.com/openshift/geard", Run: func(c *cobra.Command, args []string) { c.Help() }, } gearCmd.PersistentFlags().Var(&defaultTransport, "transport", "Specify an alternate mechanism to connect to the gear agent") gearCmd.PersistentFlags().BoolVar(&(config.SystemDockerFeatures.EnvironmentFile), "has-env-file", true, "Use --env-file with Docker, set false if older than 0.11") gearCmd.PersistentFlags().BoolVar(&(config.SystemDockerFeatures.ForegroundRun), "has-foreground", false, "(experimental) Use --foreground with Docker, requires alexlarsson/forking-run") gearCmd.PersistentFlags().BoolVarP(&insecure, "insecure", "k", false, "Do not verify CA certificate on SSL connections and transfers") // declare remote, then local commands cmd.ExtendCommands(gearCmd, false) cmd.ExtendCommands(gearCmd, true) // version information versionCmd := &cobra.Command{ Use: "version", Short: "Display version", //Long: "Display version", Run: func(c *cobra.Command, args []string) { fmt.Printf("gear %s\n", version) }, } gearCmd.AddCommand(versionCmd) // run if err := gearCmd.Execute(); err != nil { cmd.Fail(1, err.Error()) } }
// Parse the command line arguments and invoke one of the support subcommands. func Execute() { gearCmd := &cobra.Command{ Use: "gear", Short: "Gear(d) is a tool for installing Docker containers to systemd", Long: "A commandline client and server that allows Docker containers to be installed to systemd in an opinionated and distributed fashion.\n\nComplete documentation is available at http://github.com/openshift/geard", Run: gear, } gearCmd.PersistentFlags().StringVar(&(keyPath), "key-path", "", "Specify the directory containing the server private key and trusted client public keys") gearCmd.PersistentFlags().StringVarP(&(conf.Docker.Socket), "docker-socket", "S", "unix:///var/run/docker.sock", "Set the docker socket to use") gearCmd.PersistentFlags().BoolVar(&(config.SystemDockerFeatures.EnvironmentFile), "has-env-file", true, "Use --env-file with Docker, set false if older than 0.11") gearCmd.PersistentFlags().BoolVar(&(config.SystemDockerFeatures.ForegroundRun), "has-foreground", false, "(experimental) Use --foreground with Docker, requires alexlarsson/forking-run") gearCmd.PersistentFlags().StringVar(&deploymentPath, "with", "", "Provide a deployment descriptor to operate on") gearCmd.PersistentFlags().Var(&defaultTransport, "transport", "Specify an alternate mechanism to connect to the gear agent") gearCmd.PersistentFlags().BoolVarP(&insecure, "insecure", "k", false, "Do not verify CA certificate on SSL connections and transfers") deployCmd := &cobra.Command{ Use: "deploy <file|url> <host>...", Short: "Deploy a set of containers to the named hosts", Long: "Given a simple description of a group of containers, wire them together using the gear primitives.", Run: deployContainers, } deployCmd.Flags().BoolVar(&isolate, "isolate", false, "Use an isolated container running as a user") deployCmd.Flags().Int64VarP(&timeout, "timeout", "", 300, "Number of seconds to wait for HTTP/S server") gcmd.AddCommand(gearCmd, deployCmd, false) installImageCmd := &cobra.Command{ Use: "install <image> <name>... [<env>]", Short: "Install a docker image as a systemd service", Long: "Install a docker image as one or more systemd services on one or more servers.\n\nSpecify a location on a remote server with <host>[:<port>]/<name> instead of <name>. The default port is 2223.", Run: installImage, } installImageCmd.Flags().VarP(&portPairs, "ports", "p", "List of comma separated port pairs to bind '<internal>:<external>,...'. Use zero to request a port be assigned.") installImageCmd.Flags().VarP(&networkLinks, "net-links", "n", "List of comma separated port pairs to wire '<local_host>:<local_port>:<remote_host>:<remote_port>,...'. local_host may be empty. It defaults to 127.0.0.1.") installImageCmd.Flags().BoolVar(&start, "start", false, "Start the container immediately") installImageCmd.Flags().BoolVar(&isolate, "isolate", false, "Use an isolated container running as a user") installImageCmd.Flags().BoolVar(&sockAct, "socket-activated", false, "Use a socket-activated container (experimental, requires Docker branch)") installImageCmd.Flags().StringVar(&environment.Path, "env-file", "", "Path to an environment file to load") installImageCmd.Flags().StringVar(&environment.Description.Source, "env-url", "", "A url to download environment files from") installImageCmd.Flags().StringVar((*string)(&environment.Description.Id), "env-id", "", "An optional identifier for the environment being set") gcmd.AddCommand(gearCmd, installImageCmd, false) deleteCmd := &cobra.Command{ Use: "delete <name>...", Short: "Delete an installed container", Long: "Deletes one or more installed containers from the system. Will not clean up unused images.", Run: deleteContainer, } gcmd.AddCommand(gearCmd, deleteCmd, false) buildCmd := &cobra.Command{ Use: "build <source> <image> <tag> [<env>]", Short: "(Local) Build a new image on this host", Long: "Build a new Docker image named <tag> from a source repository and base image.", Run: buildImage, } buildCmd.Flags().BoolVar(&(buildReq.Clean), "clean", false, "Perform a clean build") buildCmd.Flags().StringVar(&(buildReq.WorkingDir), "dir", "tempdir", "Directory where generated Dockerfiles and other support scripts are created") buildCmd.Flags().StringVarP(&(buildReq.Ref), "ref", "r", "", "Specify a ref to check-out") buildCmd.Flags().BoolVar(&(buildReq.Verbose), "verbose", false, "Enable verbose output") buildCmd.Flags().StringVar(&(buildReq.CallbackUrl), "callbackUrl", "", "Specify a URL to invoke via HTTP POST upon build completion") buildCmd.Flags().StringVar(&environment.Path, "env-file", "", "Path to an environment file to load") buildCmd.Flags().StringVar(&environment.Description.Source, "env-url", "", "A url to download environment files from") buildCmd.Flags().StringVarP(&(buildReq.ScriptsUrl), "scripts", "s", "", "Specify a URL for the assemble and run scripts") gcmd.AddCommand(gearCmd, buildCmd, false) setEnvCmd := &cobra.Command{ Use: "set-env <name>... [<env>]", Short: "Set environment variable values on servers", Long: "Adds the listed environment values to the specified locations. The name is the environment id that multiple containers may reference. You can pass an environment file or key value pairs on the commandline.", Run: setEnvironment, } setEnvCmd.Flags().BoolVar(&resetEnv, "reset", false, "Remove any existing values") setEnvCmd.Flags().StringVar(&environment.Path, "env-file", "", "Path to an environment file to load") gcmd.AddCommand(gearCmd, setEnvCmd, false) envCmd := &cobra.Command{ Use: "env <name>...", Short: "Retrieve environment variable values by id", Long: "Return the environment variables matching the provided ids", Run: showEnvironment, } gcmd.AddCommand(gearCmd, envCmd, false) linkCmd := &cobra.Command{ Use: "link <name>...", Short: "Set network links for the named containers", Long: "Sets the network links for the named containers. A restart may be required to use the latest links.", Run: linkContainers, } linkCmd.Flags().VarP(&networkLinks, "net-links", "n", "List of comma separated port pairs to wire '<local_host>:local_port>:<host>:<remote_port>,...'. local_host may be empty. It defaults to 127.0.0.1") gearCmd.AddCommand(linkCmd) startCmd := &cobra.Command{ Use: "start <name>...", Short: "Invoke systemd to start a container", Long: "Queues the start and immediately returns.", // Use -f to attach to the logs.", Run: startContainer, } //startCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Attach to the logs after startup") gcmd.AddCommand(gearCmd, startCmd, false) stopCmd := &cobra.Command{ Use: "stop <name>...", Short: "Invoke systemd to stop a container", Long: ``, Run: stopContainer, } gcmd.AddCommand(gearCmd, stopCmd, false) restartCmd := &cobra.Command{ Use: "restart <name>...", Short: "Invoke systemd to restart a container", Long: "Queues the restart and immediately returns.", // Use -f to attach to the logs.", Run: restartContainer, } //startCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Attach to the logs after startup") gcmd.AddCommand(gearCmd, restartCmd, false) statusCmd := &cobra.Command{ Use: "status <name>...", Short: "Retrieve the systemd status of one or more containers", Long: "Shows the equivalent of 'systemctl status ctr-<name>' for each listed unit", Run: containerStatus, } gcmd.AddCommand(gearCmd, statusCmd, false) listUnitsCmd := &cobra.Command{ Use: "list-units <host>...", Short: "Retrieve the list of services across all hosts", Long: "Shows the equivalent of 'systemctl list-units ctr-<name>' for each installed container", Run: listUnits, } gcmd.AddCommand(gearCmd, listUnitsCmd, false) gcmd.ExtendCommands(gearCmd, false) daemonCmd := &cobra.Command{ Use: "daemon", Short: "(Local) Start the gear agent", Long: "Launch the gear agent. Will not send itself to the background.", Run: daemon, } daemonCmd.Flags().StringVarP(&listenAddr, "listen-address", "A", ":43273", "Set the address for the http endpoint to listen on") gcmd.AddCommand(gearCmd, daemonCmd, true) purgeCmd := &cobra.Command{ Use: "purge", Short: "Stop and disable all containers", Long: "Disable all registered resources from systemd to allow them to be removed from the system. Will reload the systemd daemon config.", Run: purge, } gcmd.AddCommand(gearCmd, purgeCmd, true) // createTokenCmd := &cobra.Command{ // Use: "create-token <type> <content_id>", // Short: "(Local) Generate a content request token", // Long: "Create a URL that will serve as a content request token using a server public key and client private key.", // Run: createToken, // } // createTokenCmd.Flags().Int64Var(&expiresAt, "expires-at", time.Now().Unix()+3600, "Specify the content request token expiration time in seconds after the Unix epoch") // gearCmd.gcmd.AddCommand(createTokenCmd) versionCmd := &cobra.Command{ Use: "version", Short: "Display version", Long: "Display version", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("gear %s\n", version) }, } gearCmd.AddCommand(versionCmd) gcmd.ExtendCommands(gearCmd, true) if err := gearCmd.Execute(); err != nil { gcmd.Fail(1, err.Error()) } }