func bindingsCommandFunc(host string) { hosts := []string{} if host != "" { hosts = append(hosts, host) } else { host = "127.0.0.1" cfg, _ := config.ParseConfig("vanguard.yml") if cfg != nil && len(cfg.Servers) > 0 { host = cfg.Servers[0].Hostname } vClient := client.NewClient(host) remoteHosts, err := vClient.Hosts() if err != nil { fmt.Println(err) return } for _, remoteHost := range remoteHosts { hosts = append(hosts, remoteHost.ExternalIp) } } for _, host := range hosts { vClient := client.NewClient(host) bindings, err := vClient.Bindings() if err != nil { fmt.Println(err) return } for _, binding := range bindings { fmt.Println(host + ":" + binding.String()) } } }
func hideCommandFunc(c *cli.Context) { host := "127.0.0.1" port := c.Args().First() slice := strings.Split(c.Args()[0], ":") if len(slice) == 2 { host = slice[0] port = slice[1] } bindingHost := "" bindingPort := "" if len(c.Args()) == 2 { slice := strings.Split(c.Args()[1], ":") if len(slice) == 2 { bindingHost = slice[0] bindingPort = slice[1] } } vClient := client.NewClient(host) err := vClient.Hide(port, bindingHost, bindingPort) if err != nil { fmt.Println(err) return } bindingsCommandFunc(host) }
func exposeCommandFunc(c *cli.Context) { if len(c.Args()) != 2 { showExposeErrorMessage() return } slice := strings.Split(c.Args()[0], ":") if len(slice) == 0 || len(slice) > 2 { showExposeErrorMessage() return } host := "127.0.0.1" port := slice[0] if len(slice) == 2 { host = slice[0] port = slice[1] } slice = strings.Split(c.Args()[1], ":") if len(slice) != 2 { showExposeErrorMessage() return } internalHost := slice[0] internalPort := slice[1] vClient := client.NewClient(host) portbinding, err := vClient.Expose(port, internalHost, internalPort) if err != nil { fmt.Println(err) return } fmt.Println(portbinding) }
func stopCommandFunc(c *cli.Context) { containerId := c.Args().First() hostname := "127.0.0.1" if len(c.Args()) > 1 { hostname = getHostname(c.Args()[1]) } if containerId == "all" { cfg, _ := config.ParseConfig("vanguard.yml") if cfg != nil && hostname == "127.0.0.1" { for _, server := range cfg.Servers { err := stopAllOnHost(server.Hostname) if err != nil { fmt.Println(err) return } } } else { err := stopAllOnHost(hostname) if err != nil { fmt.Println(err) return } } return } vClient := client.NewClient(hostname) err := vClient.Stop(containerId) if err != nil { fmt.Println(err) return } fmt.Println("success") }
func hostCommandFunc() { vClient := client.NewClient("127.0.0.1") host, err := vClient.Host() if err != nil { fmt.Println(err) return } fmt.Println(host) }
func (self *ContainerManager) CustomExpose(host, port, remoteIp, remotePort string) error { vClient := self.Clients[host] if vClient == nil { self.Clients[host] = client.NewClient(host) vClient = self.Clients[host] } binding, err := vClient.Expose(port, remoteIp, remotePort) if err != nil { return err } fmt.Println(host + ":" + binding.String()) return nil }
func hostsCommandFunc(c *cli.Context) { host := c.Args().First() if host == "" { host = "127.0.0.1" } vClient := client.NewClient(host) hosts, err := vClient.Hosts() if err != nil { fmt.Println(err) return } for _, host := range hosts { fmt.Println(host) } }
func (self *ContainerManager) GetRunningContainers(host string) ([]*container.Container, error) { if self.RunningContainers[host] != nil { return self.RunningContainers[host], nil } c := self.Clients[host] if c == nil { self.Clients[host] = client.NewClient(host) c = self.Clients[host] } containers, err := c.Index(true) if err != nil { return nil, err } self.RunningContainers[host] = containers return containers, nil }
func psCommandFunc(c *cli.Context, check bool) { hostname := c.Args().First() if hostname != "" { hostname = getHostname(hostname) } else { hostname = "127.0.0.1" } vClient := client.NewClient(hostname) containers, err := vClient.Index(check) if err != nil { fmt.Println(err) return } for _, c := range containers { fmt.Println(c) } }
func clearBindingsCommandFunc(host string) { if host == "" { host = "127.0.0.1" } vClient := client.NewClient(host) bindings, err := vClient.Bindings() if err != nil { fmt.Println(err) return } for _, binding := range bindings { err := vClient.Hide(binding.Port, "", "") if err != nil { fmt.Println(err) return } } }
func runCommandFunc(c *cli.Context) { name := c.Args().First() if name == "" { fmt.Println("No image has been specified") return } hostname := "127.0.0.1" command := "" if len(c.Args()) > 1 { firstArgument := c.Args()[1] if !strings.Contains(firstArgument, "/") { hostname = getHostname(c.Args()[1]) } else { command = firstArgument if len(c.Args()) > 2 { hostname = getHostname(c.Args()[2]) } } } vClient := client.NewClient(hostname) variables := make(map[string]string) for _, envVariable := range c.StringSlice("e") { envVar := strings.Split(envVariable, "=") if len(envVar) == 2 { variables[envVar[0]] = envVar[1] } } dnsServers := []string{} for _, dns := range c.StringSlice("dns") { dnsServers = append(dnsServers, dns) } volumes := []string{} for _, volume := range c.StringSlice("v") { volumes = append(volumes, volume) } container, err := vClient.Run(name, name, "", "", variables, dnsServers, volumes, command, "") if err != nil { fmt.Println(err) return } fmt.Println(container) }
func stopAllOnHost(hostname string) error { vClient := client.NewClient(hostname) containers, err := vClient.Index(true) if err != nil { return err } for _, container := range containers { err = vClient.Stop(container.ContainerId) if err != nil { return err } fmt.Println("stopped " + container.String()) } bindings, err := vClient.Bindings() for _, binding := range bindings { err = vClient.Hide(binding.Port, "", "") if err != nil { return err } } return nil }
func upCommandFunc(c *cli.Context) { config, err := config.ParseConfig("vanguard.yml") if err != nil { fmt.Println(err) return } tiers, err := config.GetTiers() if err != nil { fmt.Println(err) return } err = config.PullRemoteFiles(c.Bool("replace-files")) if err != nil { fmt.Println(err) return } if c.Bool("dry") { ShowTiers(tiers) return } manager := &ContainerManager{ EnvVariables: make(map[string]string), Clients: make(map[string]*client.Client), RunningContainers: make(map[string][]*container.Container), UsedContainerNames: make(map[string][]string), } for _, server := range config.Servers { c := client.NewClient(server.Hostname) bindings, err := c.Bindings() for _, binding := range bindings { err = c.Hide(binding.Port, "", "") if err != nil { fmt.Println(err) return } } if server.Label != "" { host, err := c.Host() if err == nil && host.InternalIp != "" { manager.EnvVariables[server.Label] = host.InternalIp } } if server.Expose == nil { continue } for _, expose := range server.Expose { arr := strings.Split(expose, ":") if len(arr) != 3 { continue } manager.CustomExpose(server.Hostname, arr[0], arr[1], arr[2]) } } for _, tier := range tiers { for _, server := range tier.Servers { for _, cont := range server.Containers { manager.Manage(server.Hostname, cont) if err != nil { fmt.Println(err) return } } } } manager.StopUnused() }