func switchHost(ctx *cobra.Command, args []string) { if len(args) < 1 { ErrorExit(ctx, "Needs an argument <NAME> to switch") } name := args[0] config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } host, err := config.GetHost(name) if err != nil { log.Fatal(err) } config.Default = host.Name if err := config.SaveConfig(configPath); err != nil { log.Fatal(err) } listHosts(ctx, args) }
func logoutRegistry(ctx *cobra.Command, args []string) { reg := client.INDEX_SERVER if len(args) > 0 { reg = args[0] } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } registry, notFound := config.GetRegistry(reg) if (notFound != nil) || (registry.Credentials == "") { log.Fatalf("Not logged in to a Docker registry at %s", reg) } config.LogoutRegistry(reg) if err := config.SaveConfig(configPath); err != nil { log.Fatal(err) } ctx.Printf("Removed login credentials for a Docker registry at %s\n\n", reg) listRegistries(ctx, args) }
func addHost(ctx *cobra.Command, args []string) { if len(args) < 2 { ErrorExit(ctx, "Needs two arguments <NAME> and <URL> at least") } var ( name = args[0] url = args[1] desc = "" ) if len(args) > 2 { desc = strings.Join(args[2:], " ") } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if _, err := config.GetHost(name); err == nil { log.Fatalf("\"%s\" already exists", name) } newHost := client.Host{ Name: name, URL: url, Description: desc, } if boolTLSVerify { boolTLS = true } if boolTLS { newHost.TLS = boolTLS if pathTLSCaCert != "" { newHost.TLSCaCert = pathTLSCaCert } if pathTLSCert != "" { newHost.TLSCert = pathTLSCert } if pathTLSKey != "" { newHost.TLSKey = pathTLSKey } newHost.TLSVerify = boolTLSVerify } config.Default = newHost.Name config.Hosts = append(config.Hosts, newHost) if err := config.SaveConfig(configPath); err != nil { log.Fatal(err) } listHosts(ctx, args) }
func catConfig(ctx *cobra.Command, args []string) { config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if err := FormatPrint(ctx.Out(), config); err != nil { log.Fatal(err) } }
func docker(ctx *cobra.Command, args []string) { if _, err := exec.LookPath("docker"); err != nil { log.Fatal(err) } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } host, err := config.GetHost(hostName) if err != nil { log.Fatal(err) } var env []string for _, value := range os.Environ() { switch { case strings.HasPrefix(value, "DOCKER_HOST="): case strings.HasPrefix(value, "DOCKER_CERT_PATH="): case strings.HasPrefix(value, "DOCKER_TLS_VERIFY="): default: env = append(env, value) } } args = append([]string{"--host", host.URL}, args...) if host.TLS { args = append([]string{"--tls"}, args...) args = append([]string{"--tlscacert", host.TLSCaCert}, args...) args = append([]string{"--tlscert", host.TLSCert}, args...) args = append([]string{"--tlskey", host.TLSKey}, args...) if host.TLSVerify { args = append([]string{"--tlsverify"}, args...) } } cmd := exec.Command("docker", args...) cmd.Env = env cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Fatal(err) } }
func listHosts(ctx *cobra.Command, args []string) { config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if boolQuiet { for _, host := range config.Hosts { ctx.Println(host.Name) } return } if boolYAML || boolJSON { if err := FormatPrint(ctx.Out(), config.Hosts); err != nil { log.Fatal(err) } return } var items [][]string for _, host := range config.Hosts { out := []string{ FormatBool(host.Name == config.Default, "*", ""), host.Name, host.URL, FormatNonBreakingString(host.Description), FormatBool(host.TLS, "YES", ""), } items = append(items, out) } header := []string{ "", "Name", "URL", "Description", "TLS", } PrintInTable(ctx.Out(), header, items, 0, tablewriter.ALIGN_DEFAULT) }
func pushImage(ctx *cobra.Command, args []string) { if len(args) < 1 { ErrorExit(ctx, "Needs an argument <NAME[:TAG]> to push") } reg, name, tag, err := client.ParseRepositoryName(args[0]) if err != nil { log.Fatal(err) } if len(strings.SplitN(name, "/", 2)) == 1 { log.Fatalf("You cannot push a \"root\" repository. Please rename your repository in <yourname>/%s", name) } if reg != "" { name = reg + "/" + name } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if reg == "" { reg = client.INDEX_SERVER } registry, err := config.GetRegistry(reg) if (err != nil) || (registry.Credentials == "") { log.Fatal("Please login prior to pushing an image.") } docker, err := client.NewDockerClient(configPath, hostName, ctx.Out()) if err != nil { log.Fatal(err) } if _, err := docker.PushImage(name, tag, registry.Credentials); err != nil { log.Fatal(err) } }
func listRegistries(ctx *cobra.Command, args []string) { config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if boolQuiet { for _, registry := range config.Registries { ctx.Println(registry.Registry) } return } if boolYAML || boolJSON { if err := FormatPrint(ctx.Out(), config.Registries); err != nil { log.Fatal(err) } return } var items [][]string for _, registry := range config.Registries { out := []string{ registry.Registry, registry.Username, registry.Email, FormatBool(registry.Credentials != "", "IN", ""), } items = append(items, out) } header := []string{ "Registry", "Username", "Email", "Logged", } PrintInTable(ctx.Out(), header, items, 0, tablewriter.ALIGN_DEFAULT) }
func exportHostEnv(ctx *cobra.Command, args []string) { if len(args) > 0 { hostName = args[0] } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } host, err := config.GetHost(hostName) if err != nil { log.Fatal(err) } ctx.Printf("export DOCKER_HOST=%s;\n", host.URL) if host.TLS { ctx.Printf("export DOCKER_CERT_PATH=%s;\n", filepath.Dir(host.TLSCert)) ctx.Printf("export DOCKER_TLS_VERIFY=%s;\n", FormatBool(host.TLSVerify, "true", "false")) } else { ctx.Printf("unset DOCKER_CERT_PATH;\n") ctx.Printf("unset DOCKER_TLS_VERIFY;\n") } }
func removeHost(ctx *cobra.Command, args []string) { if len(args) < 1 { ErrorExit(ctx, "Needs an argument <NAME> to remove") } name := args[0] config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } if config.Default == name { log.Fatal("You can't remove the default host.") } if _, err := config.GetHost(name); err != nil { log.Fatal(err) } hosts := []client.Host{} for _, host := range config.Hosts { if host.Name != name { hosts = append(hosts, host) } } config.Hosts = hosts if err := config.SaveConfig(configPath); err != nil { log.Fatal(err) } listHosts(ctx, args) }
func loginRegistry(ctx *cobra.Command, args []string) { reg := client.INDEX_SERVER if len(args) > 0 { reg = args[0] } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } ctx.Printf("Log in to a Docker registry at %s\n", reg) registry, _ := config.GetRegistry(reg) authConfig := api.AuthConfig{ ServerAddress: registry.Registry, } promptDefault := func(prompt string, configDefault string) { if configDefault == "" { ctx.Printf("%s: ", prompt) } else { ctx.Printf("%s (%s): ", prompt, configDefault) } } readInput := func() string { reader := bufio.NewReader(os.Stdin) line, _, err := reader.ReadLine() if err != nil { log.Fatal(err) } return string(line) } promptDefault("Username", registry.Username) authConfig.Username = readInput() if authConfig.Username == "" { authConfig.Username = registry.Username } ctx.Print("Password: "******"Email", registry.Email) authConfig.Email = readInput() if authConfig.Email == "" { authConfig.Email = registry.Email } docker, err := client.NewDockerClient(configPath, hostName, ctx.Out()) if err != nil { log.Fatal(err) } credentials, err := docker.Auth(&authConfig) if err != nil { log.Fatal(err) } registry.Username = authConfig.Username registry.Email = authConfig.Email registry.Credentials = credentials config.SetRegistry(registry) if err := config.SaveConfig(configPath); err != nil { log.Fatal(err) } ctx.Println("Login Succeeded!") listRegistries(ctx, args) }
func getHostInfo(ctx *cobra.Command, args []string) { if len(args) > 0 { hostName = args[0] } config, err := client.LoadConfig(configPath) if err != nil { log.Fatal(err) } host, err := config.GetHost(hostName) if err != nil { log.Fatal(err) } docker, err := client.NewDockerClient(configPath, hostName, ctx.Out()) if err != nil { log.Fatal(err) } info, err := docker.Info() if err != nil { log.Fatal(err) } if boolYAML || boolJSON { data := make([]interface{}, 2) data[0] = host data[1] = info if err := FormatPrint(ctx.Out(), data); err != nil { log.Fatal(err) } return } var items [][]string items = append(items, []string{ "Host", host.Name, }) items = append(items, []string{ "URL", host.URL, }) items = append(items, []string{ "Description", FormatNonBreakingString(host.Description), }) items = append(items, []string{ "TLS", FormatBool(host.TLS, "Supported", "No"), }) if host.TLS { items = append(items, []string{ FormatNonBreakingString(" CA Certificate file"), FormatNonBreakingString(host.TLSCaCert), }) items = append(items, []string{ FormatNonBreakingString(" Certificate file"), FormatNonBreakingString(host.TLSCert), }) items = append(items, []string{ FormatNonBreakingString(" Key file"), FormatNonBreakingString(host.TLSKey), }) items = append(items, []string{ FormatNonBreakingString(" Verify"), FormatBool(host.TLSVerify, "Required", "No"), }) } items = append(items, []string{ "Containers", strconv.Itoa(info.Containers), }) items = append(items, []string{ "Images", strconv.Itoa(info.Images), }) items = append(items, []string{ "Storage Driver", info.Driver, }) for _, pair := range info.DriverStatus { items = append(items, []string{ FormatNonBreakingString(fmt.Sprintf(" %s", pair[0])), FormatNonBreakingString(fmt.Sprintf("%s", pair[1])), }) } items = append(items, []string{ "Execution Driver", info.ExecutionDriver, }) items = append(items, []string{ "Kernel Version", info.KernelVersion, }) items = append(items, []string{ "Operating System", FormatNonBreakingString(info.OperatingSystem), }) items = append(items, []string{ "CPUs", strconv.Itoa(info.NCPU), }) items = append(items, []string{ "Total Memory", fmt.Sprintf("%s GB", FormatFloat(float64(info.MemTotal)/1000000000)), }) items = append(items, []string{ "Index Server Address", info.IndexServerAddress, }) items = append(items, []string{ "Memory Limit", FormatBool((info.MemoryLimit != 0) && (info.MemoryLimit != false), "Supported", "No"), }) items = append(items, []string{ "Swap Limit", FormatBool((info.SwapLimit != 0) && (info.SwapLimit != false), "Supported", "No"), }) items = append(items, []string{ "IPv4 Forwarding", FormatBool((info.IPv4Forwarding != 0) && (info.IPv4Forwarding != false), "Enabled", "Disabled"), }) items = append(items, []string{ "ID", info.ID, }) items = append(items, []string{ "Name", info.Name, }) items = append(items, []string{ "Labels", FormatNonBreakingString(strings.Join(info.Labels, ", ")), }) items = append(items, []string{ "Debug Mode", FormatBool((info.Debug != 0) && (info.Debug != false), "Yes", "No"), }) if (info.Debug != 0) && (info.Debug != false) { items = append(items, []string{ FormatNonBreakingString(" Events Listeners"), strconv.Itoa(info.NEventsListener), }) items = append(items, []string{ FormatNonBreakingString(" Fds"), strconv.Itoa(info.NFd), }) items = append(items, []string{ FormatNonBreakingString(" Goroutines"), strconv.Itoa(info.NGoroutines), }) items = append(items, []string{ FormatNonBreakingString(" Init Path"), info.InitPath, }) items = append(items, []string{ FormatNonBreakingString(" Init SHA1"), info.InitSha1, }) items = append(items, []string{ FormatNonBreakingString(" Docker Root Dir"), info.DockerRootDir, }) } PrintInTable(ctx.Out(), nil, items, 0, tablewriter.ALIGN_LEFT) }