-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (68 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
type RunOption struct {
TargetVersion string
Command string
}
func main() {
args := os.Args[1:]
log.Println(args)
if len(os.Args) < 2 {
log.Fatalln("required at least two arguments")
}
command := os.Args[1]
targetVersion := os.Args[2]
log.Printf("command = %s\n", command)
log.Printf("target version = %s\n", targetVersion)
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
newImage := fmt.Sprintf("docker.io/oscarzhou/ubuntu:%s", targetVersion)
reader, err := cli.ImagePull(ctx, newImage, types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)
// stop the old agent container
// create the new container
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "oscarzhou/ubuntu:2",
// Cmd: []string{"cat", "version.txt", "&&", "cd", "/var/run", "&&", "ls"},
Cmd: []string{"ls", "/var/run"},
}, &container.HostConfig{
Binds: []string{
"/var/run/docker.sock:/var/run/docker.sock",
},
}, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}