|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/user" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + "slices" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/docker/docker/api/types/container" |
| 16 | + dockerClient "github.com/docker/docker/client" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +const pythonImage = "arduino-pythonenv" |
| 21 | + |
| 22 | +func main() { |
| 23 | + docker, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv) |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + defer docker.Close() |
| 28 | + |
| 29 | + rootCmd := &cobra.Command{ |
| 30 | + Use: "app", |
| 31 | + Short: "A CLI to manage the Python app", |
| 32 | + } |
| 33 | + |
| 34 | + rootCmd.AddCommand( |
| 35 | + &cobra.Command{ |
| 36 | + Use: "stop", |
| 37 | + Short: "Stop the Python app", |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + stopHandler(docker, args[0]) |
| 40 | + }, |
| 41 | + }, |
| 42 | + &cobra.Command{ |
| 43 | + Use: "start", |
| 44 | + Short: "Start the Python app", |
| 45 | + Run: func(cmd *cobra.Command, args []string) { |
| 46 | + startHandler(docker, args[0]) |
| 47 | + }, |
| 48 | + }, |
| 49 | + &cobra.Command{ |
| 50 | + Use: "logs", |
| 51 | + Short: "Show the logs of the Python app", |
| 52 | + Run: func(cmd *cobra.Command, args []string) { |
| 53 | + logsHandler(docker, args[0]) |
| 54 | + }, |
| 55 | + }, |
| 56 | + &cobra.Command{ |
| 57 | + Use: "list", |
| 58 | + Short: "List all running Python apps", |
| 59 | + Run: func(cmd *cobra.Command, args []string) { |
| 60 | + listHandler(docker) |
| 61 | + }, |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + if err := rootCmd.Execute(); err != nil { |
| 66 | + log.Panic(err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func startHandler(docker *dockerClient.Client, appPath string) { |
| 71 | + ctx := context.Background() |
| 72 | + |
| 73 | + app := filepath.Base(appPath) |
| 74 | + |
| 75 | + absAppPath, err := filepath.Abs(appPath) |
| 76 | + if err != nil { |
| 77 | + log.Panic(err) |
| 78 | + } |
| 79 | + |
| 80 | + // Map user to avoid permission issues. |
| 81 | + // MacOS and Windows uses a VM so we don't need to map the user. |
| 82 | + var userMapping string |
| 83 | + if runtime.GOOS == "linux" { |
| 84 | + user, err := user.Current() |
| 85 | + if err != nil { |
| 86 | + log.Panic("cannot get linux user: %w", err) |
| 87 | + } |
| 88 | + userMapping = user.Uid + ":" + user.Gid |
| 89 | + } |
| 90 | + |
| 91 | + name := appToContainerName(app) |
| 92 | + resp, err := docker.ContainerCreate(ctx, &container.Config{ |
| 93 | + Image: pythonImage, |
| 94 | + User: userMapping, |
| 95 | + }, &container.HostConfig{ |
| 96 | + Binds: []string{absAppPath + ":/app"}, |
| 97 | + AutoRemove: true, |
| 98 | + }, nil, nil, name) |
| 99 | + if err != nil { |
| 100 | + log.Panic(err) |
| 101 | + } |
| 102 | + |
| 103 | + if err := docker.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil { |
| 104 | + log.Panic(err) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Println("Container started with ID:", resp.ID) |
| 108 | +} |
| 109 | + |
| 110 | +func stopHandler(docker *dockerClient.Client, appPath string) { |
| 111 | + ctx := context.Background() |
| 112 | + |
| 113 | + app := filepath.Base(appPath) |
| 114 | + |
| 115 | + name := appToContainerName(app) |
| 116 | + id, err := findContainer(ctx, docker, name) |
| 117 | + if err != nil { |
| 118 | + log.Panic(err) |
| 119 | + } |
| 120 | + |
| 121 | + if err := docker.ContainerRemove(ctx, id, container.RemoveOptions{Force: true}); err != nil { |
| 122 | + log.Panic(err) |
| 123 | + } |
| 124 | + |
| 125 | + fmt.Println("Container stopped and removed") |
| 126 | +} |
| 127 | + |
| 128 | +func logsHandler(docker *dockerClient.Client, appPath string) { |
| 129 | + ctx := context.Background() |
| 130 | + |
| 131 | + app := filepath.Base(appPath) |
| 132 | + |
| 133 | + name := appToContainerName(app) |
| 134 | + id, err := findContainer(ctx, docker, name) |
| 135 | + if err != nil { |
| 136 | + log.Panic(err) |
| 137 | + } |
| 138 | + |
| 139 | + out, err := docker.ContainerLogs(ctx, id, container.LogsOptions{ |
| 140 | + ShowStdout: true, |
| 141 | + ShowStderr: true, |
| 142 | + Follow: true, |
| 143 | + }) |
| 144 | + if err != nil { |
| 145 | + log.Panic(err) |
| 146 | + } |
| 147 | + defer out.Close() |
| 148 | + |
| 149 | + if _, err := io.Copy(os.Stdout, out); err != nil { |
| 150 | + log.Panic(err) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func listHandler(docker *dockerClient.Client) { |
| 155 | + ctx := context.Background() |
| 156 | + |
| 157 | + resp, err := docker.ContainerList(ctx, container.ListOptions{ |
| 158 | + All: true, |
| 159 | + }) |
| 160 | + if err != nil { |
| 161 | + log.Panic(err) |
| 162 | + } |
| 163 | + |
| 164 | + for _, container := range resp { |
| 165 | + if strings.HasPrefix(container.Names[0], "/arduino_") && strings.HasSuffix(container.Names[0], "_python") { |
| 166 | + fmt.Println(containerToAppName(container.Names[0])) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func appToContainerName(app string) string { |
| 172 | + app = strings.Trim(app, "/\n ") |
| 173 | + app = strings.ReplaceAll(app, "/", "_") |
| 174 | + return fmt.Sprintf("arduino_%s_python", app) |
| 175 | +} |
| 176 | + |
| 177 | +func containerToAppName(name string) string { |
| 178 | + name = strings.TrimPrefix(name, "/") |
| 179 | + name = strings.TrimPrefix(name, "arduino_") |
| 180 | + name = strings.TrimSuffix(name, "_python") |
| 181 | + return name |
| 182 | +} |
| 183 | + |
| 184 | +func findContainer(ctx context.Context, docker *dockerClient.Client, containerName string) (string, error) { |
| 185 | + resp, err := docker.ContainerList(ctx, container.ListOptions{ |
| 186 | + All: true, |
| 187 | + }) |
| 188 | + if err != nil { |
| 189 | + return "", err |
| 190 | + } |
| 191 | + |
| 192 | + idx := slices.IndexFunc(resp, func(container container.Summary) bool { |
| 193 | + return container.Names[0] == "/"+containerName // Container name is prefixed with a slash |
| 194 | + }) |
| 195 | + if idx == -1 { |
| 196 | + return "", fmt.Errorf("container not found") |
| 197 | + } |
| 198 | + |
| 199 | + return resp[idx].ID, nil |
| 200 | +} |
0 commit comments