|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/docker/docker/api/types/container" |
| 13 | + "github.com/docker/docker/api/types/image" |
| 14 | + "github.com/docker/docker/client" |
| 15 | + "github.com/docker/go-connections/nat" |
| 16 | + "github.com/microcks/microcks-cli/pkg/config" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +func NewStartCommand() *cobra.Command { |
| 21 | + var ( |
| 22 | + hostIP string = "0.0.0.0" |
| 23 | + name string |
| 24 | + hostPort string |
| 25 | + imageName string |
| 26 | + autoRemove bool |
| 27 | + driver string |
| 28 | + ) |
| 29 | + var startCmd = &cobra.Command{ |
| 30 | + Use: "start", |
| 31 | + Short: "start microcks instance", |
| 32 | + Example: `# Start a Microcks instance |
| 33 | +microcks start |
| 34 | +
|
| 35 | +# Define your port (by default 8585) |
| 36 | +microcks start --port [Port you want] |
| 37 | +
|
| 38 | +# Define your driver (by default docker) |
| 39 | +microcks start --driver [driver you wnat either 'docker' or 'podman'] |
| 40 | +
|
| 41 | +# Define name of your microcks container/instance |
| 42 | +microcks start --name [name of you container/instance]`, |
| 43 | + Run: func(cmd *cobra.Command, args []string) { |
| 44 | + cfg, err := config.EnsureConfig(config.ConfigPath) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + log.Fatalf("Error loading config: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + cfg.Instance.Driver = driver |
| 51 | + |
| 52 | + cli, err := createClient(cfg.Instance.Driver) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + fmt.Println(err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + defer cli.Close() |
| 60 | + |
| 61 | + if cfg.Instance.Status == "Running" { |
| 62 | + fmt.Println("Microcks is already running.") |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if cfg.Instance.Status == "Stopped" || cfg.Instance.Status == "Created" { |
| 67 | + if err := startContainer(cfg.Instance.ContainerID, cli); err != nil { |
| 68 | + fmt.Errorf("failed to start container: %v", err) |
| 69 | + } |
| 70 | + fmt.Println("Microcks started successfully...") |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + cfg.Instance.Name = name |
| 75 | + cfg.Instance.Image = imageName |
| 76 | + cfg.Instance.Port = hostPort |
| 77 | + cfg.Instance.AutoRemove = autoRemove |
| 78 | + |
| 79 | + containerID, err := createContainer(cfg, hostIP, cli) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + log.Fatalf("Failed to create a container: %v", err) |
| 83 | + return |
| 84 | + } |
| 85 | + cfg.Instance.ContainerID = containerID |
| 86 | + cfg.Instance.Status = "Created" |
| 87 | + |
| 88 | + if err := startContainer(cfg.Instance.ContainerID, cli); err != nil { |
| 89 | + fmt.Errorf("failed to start container: %v", err) |
| 90 | + return |
| 91 | + } |
| 92 | + cfg.Instance.Status = "Running" |
| 93 | + err = config.SaveConfig(config.ConfigPath, cfg) |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + log.Fatalf("Failed to save config: %v", err) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Printf("Microcks started successfully...") |
| 101 | + }, |
| 102 | + } |
| 103 | + startCmd.Flags().StringVar(&name, "name", "microcks", "name for you microcks instance") |
| 104 | + startCmd.Flags().StringVar(&hostPort, "port", "8585", "") |
| 105 | + startCmd.Flags().StringVar(&imageName, "image", "quay.io/microcks/microcks-uber:latest-native", "image which will be used to create a container") |
| 106 | + startCmd.Flags().BoolVar(&autoRemove, "rm", false, "mimic of '--rm' flag of dokcer to automatically remove the container when it exits") |
| 107 | + startCmd.Flags().StringVar(&driver, "driver", "docker", "use --driver to change driver from docker to podman") |
| 108 | + return startCmd |
| 109 | +} |
| 110 | + |
| 111 | +func createClient(driver string) (*client.Client, error) { |
| 112 | + |
| 113 | + if driver != "docker" { |
| 114 | + out, err := exec.Command("podman", "machine", "inspect", "--format", "{{.ConnectionInfo.PodmanSocket.Path}}").Output() |
| 115 | + if err != nil { |
| 116 | + fmt.Println(err) |
| 117 | + } |
| 118 | + |
| 119 | + err = os.Setenv("DOCKER_HOST", "unix://"+strings.TrimSpace(string(out))) |
| 120 | + if err != nil { |
| 121 | + fmt.Println(err) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return cli, nil |
| 132 | +} |
| 133 | + |
| 134 | +func createContainer(cfg *config.Config, hostIP string, cli *client.Client) (string, error) { |
| 135 | + ctx := context.Background() |
| 136 | + |
| 137 | + // Define exposed port and bindings |
| 138 | + exposedPort, _ := nat.NewPort("tcp", "8080") |
| 139 | + portBindings := nat.PortMap{ |
| 140 | + exposedPort: []nat.PortBinding{ |
| 141 | + { |
| 142 | + HostIP: hostIP, |
| 143 | + HostPort: cfg.Instance.Port, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + out, err := cli.ImagePull(ctx, cfg.Instance.Image, image.PullOptions{}) |
| 149 | + if err != nil { |
| 150 | + return "", err |
| 151 | + } |
| 152 | + defer out.Close() |
| 153 | + io.Copy(os.Stdout, out) |
| 154 | + |
| 155 | + resp, err := cli.ContainerCreate( |
| 156 | + ctx, |
| 157 | + &container.Config{ |
| 158 | + Image: cfg.Instance.Image, |
| 159 | + ExposedPorts: nat.PortSet{exposedPort: struct{}{}}, |
| 160 | + }, |
| 161 | + &container.HostConfig{ |
| 162 | + PortBindings: portBindings, |
| 163 | + AutoRemove: cfg.Instance.AutoRemove, |
| 164 | + }, nil, nil, cfg.Instance.Name) |
| 165 | + |
| 166 | + if err != nil { |
| 167 | + return "", err |
| 168 | + } |
| 169 | + |
| 170 | + return resp.ID, nil |
| 171 | +} |
| 172 | + |
| 173 | +func startContainer(cotainerID string, cli *client.Client) error { |
| 174 | + ctx := context.Background() |
| 175 | + |
| 176 | + if err := cli.ContainerStart(ctx, cotainerID, container.StartOptions{}); err != nil { |
| 177 | + panic(err) |
| 178 | + } |
| 179 | + |
| 180 | + return nil |
| 181 | +} |
0 commit comments