-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchieh-cli.go
298 lines (271 loc) · 9.11 KB
/
chieh-cli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Author: Chieh Tsai
package main
import (
"fmt"
"log"
"os"
"os/exec"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Commands = []cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List the files in the present directory.",
Category: "list",
Action: func(c *cli.Context) error {
// command_list
args := []string{
"ls -lah",
"df -h"}
for i := 0; i < 2; i++ {
cmd := exec.Command("bash", "-c", args[i])
// cmd := exec.Command("ls", "-lah")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
return nil
},
},
{
Name: "install",
Aliases: []string{"i", "I", "Install"},
Usage: "To install some packages regarding to native Linux tools.",
Category: "install",
Subcommands: []cli.Command{
{
Name: "all",
Usage: "Directly install all packages from my personal list.",
Category: "install",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "no-gpu",
Usage: "Without installing the GPU driver.",
},
},
Action: func(c *cli.Context) error {
// sudo apt-get update
cmd := exec.Command("sudo", "apt-get", "update")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
// sudo apt -y install vim net-tools git ca-certificates openssh-server python3-pip
basic_tool_list := []string{
"vim",
"net-tools",
"git",
// "ca-certificates",
"openssh-server",
"python3-pip",
}
var TestToolWord string
for btl:=0; btl< len(basic_tool_list); btl++ {
switch basic_tool_list[btl]{
case "python3-pip":
TestToolWord = "python3"
case "openssh-server":
TestToolWord = "ssh"
case "net-tools":
TestToolWord = "ifconfig"
default:
TestToolWord = basic_tool_list[btl]
}
_, err := exec.LookPath(TestToolWord)
if err != nil {
cmd = exec.Command("sudo", "apt-get", "-y", "install", basic_tool_list[btl])
fmt.Printf("We are going to install the %s tool.", basic_tool_list[btl])
out, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
}
// Start install docker env
fmt.Println("------------------------------------------")
fmt.Println("Checking Docker tool...")
_, err = exec.LookPath("docker")
if err != nil {
fmt.Println("\n>> Start to install the environment of Docker")
docker_list := []string{
"sudo apt install -y apt-transport-https curl gnupg-agent software-properties-common",
"curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -",
"sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable\"",
"sudo apt update",
"sudo apt install -y docker-ce docker-ce-cli containerd.io",
"sudo usermod -aG docker $USER",
"sudo chmod 777 /var/run/docker.sock",
}
for i:=0; i<len(docker_list); i++{
cmd = exec.Command("bash", "-c", docker_list[i])
out, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
fmt.Println("\nDone for the installation of Docker.\n")
} else{
fmt.Println("Already installed Docker.")
}
//
fmt.Println("------------------------------------------")
fmt.Println("Checking Docker-Compose tool...")
_, err = exec.LookPath("docker-compose")
if err != nil {
fmt.Println("Start to install Docker Compose tool.\n")
docker_compose_list := []string{
"sudo curl -L \"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose",
"sudo chmod +x /usr/local/bin/docker-compose",
}
for i:=0; i<len(docker_compose_list); i++{
cmd = exec.Command("bash", "-c", docker_compose_list[i])
out, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
fmt.Println("\nDone for the installation of Docker Compose tool.\n")
} else {
fmt.Println("Already installed Docker-Compose.")
}
if c.Bool("no-gpu") != true {
fmt.Println("Start to install GPU driver...")
// Start install nvidia-driver
fmt.Println("------------------------------------------")
fmt.Println("Checking GPU status ...")
_, err = exec.LookPath("nvidia-smi")
if err != nil {
fmt.Println("\n>> Start to install the GPU driver Version 470 and nvidia-container-toolkit.")
gpu_driver_list := []string{
"sudo sed -i -e 's/tw.archive.ubuntu.com/free.nchc.org.tw/' /etc/apt/sources.list",
"sudo add-apt-repository -y ppa:graphics-drivers/ppa",
"sudo apt update",
"sudo apt install -y nvidia-driver-470-server",
"curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -",
"distribution=$(. /etc/os-release;echo $ID$VERSION_ID)",
"curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list",
"sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit",
}
for i:=0; i<len(gpu_driver_list); i++{
cmd = exec.Command("bash", "-c", gpu_driver_list[i])
out, err = cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(string(out))
}
fmt.Println("\nDone for the installation of GPU driver.\n")
} else{
fmt.Println("Already installed nvidia-smi.")
}
}
return nil
},
},
{
Name: "remove",
Usage: "remove an existing",
Category: "install",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "serve"},
&cli.BoolFlag{Name: "option"},
&cli.StringFlag{Name: "message"},
},
Action: func(c *cli.Context) error {
// fmt.Println("removed task template: ", c.Args().First())
fmt.Println("serve:", c.Bool("serve"))
fmt.Println("option:", c.Bool("option"))
fmt.Println("message:", c.String("message"))
return nil
},
},
},
},
{
Name: "pull",
Aliases: []string{"p", "Pull"},
Usage: "Pull the docker image. Notice: please install Docker first.",
Category: "pull",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "u",
Usage: "Provide your Docker username for login use.",
Required: true,
},
},
Action: func(c *cli.Context) error {
if len(c.String("u")) == 0 {
fmt.Println("Please add an option '-u' for providing your username.")
os.Exit(0)
}
_, err := exec.LookPath("docker")
if err != nil {
fmt.Println("Please install Docker first. Or check your Docker environment path.")
os.Exit(0)
}
var pwd string
fmt.Printf("Your user name is %s\n", c.String("u"))
fmt.Print("Please type the password: ")
fmt.Print("\033[8m") // Hide input
fmt.Scan(&pwd)
fmt.Print("\033[28m") // Show input
// fmt.Printf("Here is your password: %s\n", pwd)
// Check the docker login
docker_login := "docker login -u " + c.String("u") + " -p " + pwd
fmt.Println(docker_login)
cmd := exec.Command("bash", "-c", docker_login)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Login failure. Please check the uersname and password.")
os.Exit(0)
// log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf(">> %s\n", string(out))
// Pull the docker image
image_list := []string{
"nvcr.io/nvidia/tritonserver:21.03-py3",
"minio/minio:RELEASE.2022-03-24T00-43-44Z",
"postgres",
}
ch := make(chan string)
for image:=0; image<len(image_list); image++{
go DockerPull(image_list[image], ch)
<-ch
}
fmt.Printf("\nDone for the pulling of whole docker images.\n")
return nil
},
},
}
app.Name = "chieh-cli"
app.Usage = "install some basic packages for Linux Ubuntu OS."
app.Description = "It is a Chieh's personal CLI tool for setting environment on Linux."
app.Version = "0.1.0"
err := app.Run(os.Args)
if err != nil {
fmt.Println("Please check --help to get more usage information.")
os.Exit(0)
// log.Fatal(err)
}
}
func DockerPull(s string, c chan string) {
docker_pull_image := "docker pull " + s
fmt.Println(docker_pull_image)
cmd := exec.Command("bash", "-c", docker_pull_image)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Pull failure. ", s, " Please check the image.\n")
c <- "FINISH"
}
fmt.Printf(">> Done for pulling the %s image.\n", string(out))
c <- "FINISH"
}