func getRemote() (remote.Remote, error) { config, err := config.ParseConfig("vanguard.yml") if err != nil { return nil, err } return config.GetRemote() }
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 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 buildMissingImages(c *cli.Context, force bool) { label := c.Args().First() config, err := config.ParseConfig("vanguard.yml") if err != nil { fmt.Println(err) return } messages := []string{} for _, server := range config.Servers { for _, cont := range server.Containers { _, err = container.GetImageId(cont.Image, cont.Tag) if force || err != nil { if cont.Dockerfile != "" && (label == "" || cont.Label == label) { _, err := container.BuildImage(cont.Dockerfile, cont.Image) if err != nil { messages = append(messages, fmt.Sprintf("%v image had errors: %v ", cont.Image, err)) } else { messages = append(messages, fmt.Sprintf("%v image has been built successfully ", cont.Image)) } } } } } if len(messages) == 0 { fmt.Println("No images to build") } else { for _, message := range messages { fmt.Println(message) } } }
func getHostname(hostname string) string { if hostname == "" { hostname = "127.0.0.1" } cfg, _ := config.ParseConfig("vanguard.yml") if cfg != nil { for _, server := range cfg.Servers { if server.Label == hostname { hostname = server.Hostname } } } return hostname }
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() }