MCPcopy
hub / github.com/spacecloud-io/space-cloud / DockerStart

Function DockerStart

space-cli/cmd/modules/operations/start.go:17–171  ·  view source on GitHub ↗

DockerStart restarts the services which might have been stopped for any reason

(clusterName string)

Source from the content-addressed store, hash-verified

15
16// DockerStart restarts the services which might have been stopped for any reason
17func DockerStart(clusterName string) error {
18 ctx := context.Background()
19
20 // Create a docker client
21 docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
22 if err != nil {
23 return utils.LogError("Unable to initialize docker client", err)
24 }
25
26 // Get the hosts file
27 hosts, err := txeh.NewHosts(&txeh.HostsConfig{ReadFilePath: utils.GetSpaceCloudHostsFilePath(clusterName), WriteFilePath: utils.GetSpaceCloudHostsFilePath(clusterName)})
28 if err != nil {
29 return utils.LogError("Unable to open hosts file", err)
30 }
31
32 // Start the add ons first
33 argsAddOns := filters.Arg("label", "app=addon")
34 argsNetwork := filters.Arg("network", utils.GetNetworkName(clusterName))
35 addOnContainers, err := docker.ContainerList(ctx, types.ContainerListOptions{Filters: filters.NewArgs(argsNetwork, argsAddOns), All: true})
36 if err != nil {
37 return utils.LogError("Unable to list space-cloud core containers", err)
38 }
39 for _, container := range addOnContainers {
40 // First start the container
41 if err := docker.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
42 return utils.LogError(fmt.Sprintf("Unable to start container (%s)", container.ID), err)
43 }
44
45 // Get the container's info
46 info, err := docker.ContainerInspect(ctx, container.ID)
47 if err != nil {
48 return utils.LogError(fmt.Sprintf("Unable to inspect container (%s)", container.ID), err)
49 }
50
51 hostName := utils.GetServiceDomain(info.Config.Labels["service"], info.Config.Labels["name"])
52
53 // Remove the domain from the hosts file
54 hosts.RemoveHost(hostName)
55
56 // Add it back with the new ip address
57 hosts.AddHost(info.NetworkSettings.Networks[utils.GetNetworkName(clusterName)].IPAddress, hostName)
58 }
59
60 // Save the hosts file before continuing
61 if err := hosts.Save(); err != nil {
62 return utils.LogError("Could not save hosts file after updating add on containers", err)
63 }
64
65 // Second step is to start the gateway and runner. We'll need the runner's ip address in the next step
66 var runnerIP string
67 argsSC := filters.Arg("label", "app=space-cloud")
68 argsNetwork = filters.Arg("network", utils.GetNetworkName(clusterName))
69 scContainers, err := docker.ContainerList(ctx, types.ContainerListOptions{Filters: filters.NewArgs(argsNetwork, argsSC), All: true})
70 if err != nil {
71 return utils.LogError("Unable to list space-cloud core containers", err)
72 }
73
74 for _, container := range scContainers {

Callers 1

actionStartFunction · 0.85

Calls 1

ErrorMethod · 0.65

Tested by

no test coverage detected