Skip to content

Commit

Permalink
代码完成
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Jul 31, 2019
1 parent 160e7c2 commit 05fdaae
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 49 deletions.
56 changes: 49 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# container-install

### Install
### Install Docker
install container from url:
location file:
```shell script
container-install install
--docker true
--docker T
--host 172.16.213.131
--host 172.16.213.132
--user root
Expand All @@ -18,7 +18,7 @@ install container from url:
remote url:
```shell script
container-install install
--docker true
--docker T
--host 172.16.213.131
--host 172.16.213.132
--user root
Expand All @@ -28,22 +28,64 @@ install container from url:
--lib /var/lib/docker
--pkg-url https://download.docker.com/linux/static/stable/x86_64/docker-18.09.4.tgz
```
### UnInstall

### Install Containerd
install container from url:
location file:
```shell script
container-install install
--docker F
--host 172.16.213.131
--host 172.16.213.132
--user root
--passwd admin
--registry 127.0.0.1
--registry 127.0.0.2
--lib /var/lib/containerd
--pkg-url /root/containerd-1.2.7.tgz
```
remote url:
```shell script
container-install install
--docker F
--host 172.16.213.131
--host 172.16.213.132
--user root
--passwd admin
--registry 127.0.0.1
--registry 127.0.0.2
--lib /var/lib/containerd
--pkg-url https://github.com/containerd/containerd/releases/download/v1.2.7/containerd-1.2.7.linux-amd64.tar.gz
```

### UnInstall Docker
uninstall container:
```shell script
container-install uninstall
--docker true
--docker T
--host 172.16.213.131
--host 172.16.213.132
--user root
--passwd admin
--docker-lib /var/lib/docker
```

### UnInstall Containerd
uninstall container:
```shell script
container-install uninstall
--docker F
--host 172.16.213.131
--host 172.16.213.132
--user root
--passwd admin
--docker-lib /var/lib/containerd
```

### Print Download Url
print download url for docker:
```shell script
container-install print --docker true
container-install print --docker T
```

the docker Newest version is v19.03.0.
Expand Down Expand Up @@ -82,7 +124,7 @@ https://download.docker.com/linux/static/stable/x86_64/docker-19.03.0.tgz
the containerd Newest version is v1.2.7
ex:
```shell script
cuisongliu@cuisongliu-PC:~$ go run main.go print --docker false
cuisongliu@cuisongliu-PC:~$ go run main.go print --docker F
https://github.com/containerd/containerd/releases/download/v1.1.0/containerd-1.1.0.linux-amd64.tar.gz
https://github.com/containerd/containerd/releases/download/v1.1.1/containerd-1.1.1.linux-amd64.tar.gz
https://github.com/containerd/containerd/releases/download/v1.1.2/containerd-1.1.2.linux-amd64.tar.gz
Expand Down
2 changes: 2 additions & 0 deletions install/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type stepInterface interface {
Version(host string)
Uninstall(host string)
Print()

//
lib() string
serviceFile() []byte
configFile() []byte
}
133 changes: 105 additions & 28 deletions install/command/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,59 @@ func NewContainerd() stepInterface {

const containerdFileName = "containerd.tgz"

func (d Containerd) SendPackage(host string) {
func (d *Containerd) lib() string {
if Lib == "" {
return "/var/lib/containerd"
} else {
return Lib
}
}
func (d *Containerd) SendPackage(host string) {
SendPackage(host, PkgUrl, containerdFileName)
}

func (d Containerd) Tar(host string) {
func (d *Containerd) Tar(host string) {
cmd := fmt.Sprintf("tar --strip-components=1 -xvzf /root/%s -C /usr/local/bin", containerdFileName)
Cmd(host, cmd)
}

func (d Containerd) Config(host string) {
cmd := "mkdir -p " + Lib
func (d *Containerd) Config(host string) {
cmd := "mkdir -p " + d.lib()
Cmd(host, cmd)
cmd = "mkdir -p /etc/containerd"
Cmd(host, cmd)
cmd = "containerd config default > /etc/containerd/config.toml"
//cmd = "echo \"" + string(d.configFile()) + "\" > /etc/docker/daemon.json"
//cmd = "containerd config default > /etc/containerd/config.toml"
cmd = "echo \"" + string(d.configFile()) + "\" > /etc/containerd/config.toml"
Cmd(host, cmd)
}

func (d Containerd) Enable(host string) {
func (d *Containerd) Enable(host string) {
cmd := "echo \"" + string(d.serviceFile()) + "\" > /usr/lib/systemd/system/containerd.service"
Cmd(host, cmd)
cmd = "systemctl enable containerd.service && systemctl restart containerd.service"
Cmd(host, cmd)
}

func (d Containerd) Version(host string) {
func (d *Containerd) Version(host string) {
cmd := "containerd --version"
Cmd(host, cmd)
logger.Warn("pull docker hub command. ex: ctr images pull docker.io/library/alpine:3.8")
logger.Warn("pull http registry command. ex: ctr images pull 10.0.45.222/library/alpine:3.8 --plain-http")
}

func (d Containerd) Uninstall(host string) {
func (d *Containerd) Uninstall(host string) {
cmd := "systemctl stop containerd.service && systemctl disable containerd.service"
Cmd(host, cmd)
cmd = "rm -rf /usr/local/bin/ctr && rm -rf /usr/local/bin/containerd* "
Cmd(host, cmd)
cmd = "rm -rf /var/lib/containerd && rm -rf /etc/containerd/* "
Cmd(host, cmd)
if Lib != "" {
cmd = "rm -rf " + Lib
if d.lib() != "" {
cmd = "rm -rf " + d.lib()
Cmd(host, cmd)
}
}

func (d Containerd) serviceFile() []byte {
func (d *Containerd) serviceFile() []byte {
var templateText = string(`[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
Expand All @@ -83,32 +91,101 @@ WantedBy=multi-user.target
`)
return []byte(templateText)
}
func (d Containerd) configFile() []byte {
var templateText = string(`{
\"registry-mirrors\": [
\"http://373a6594.m.daocloud.io\"
],
{{if len .DOCKER_REGISTRY}}
\"insecure-registries\":
[{{range $i,$v :=.DOCKER_REGISTRY}}{{if eq $i 0}}\"{{$v}}\"{{else}},\"{{$v}}\"{{end}}{{end}}],
{{end}}
\"graph\":\"{{.DOCKER_LIB}}\"
}`)
func (d *Containerd) configFile() []byte {
var templateText = string(`root = \"{{.CONTAINERD_LIB}}\"
state = \"/run/containerd\"
oom_score = 0
[grpc]
address = \"/run/containerd/containerd.sock\"
uid = 0
gid = 0
max_recv_message_size = 16777216
max_send_message_size = 16777216
[debug]
address = \"\"
uid = 0
gid = 0
level = \"\"
[metrics]
address = \"\"
grpc_histogram = false
[cgroup]
path = \"\"
[plugins]
[plugins.cgroups]
no_prometheus = false
[plugins.cri]
stream_server_address = \"127.0.0.1\"
stream_server_port = \"0\"
enable_selinux = false
sandbox_image = \"k8s.gcr.io/pause:3.1\"
stats_collect_period = 10
systemd_cgroup = false
enable_tls_streaming = false
max_container_log_line_size = 16384
[plugins.cri.containerd]
snapshotter = \"overlayfs\"
no_pivot = false
[plugins.cri.containerd.default_runtime]
runtime_type = \"io.containerd.runtime.v1.linux\"
runtime_engine = \"\"
runtime_root = \"\"
[plugins.cri.containerd.untrusted_workload_runtime]
runtime_type = \"\"
runtime_engine = \"\"
runtime_root = \"\"
[plugins.cri.cni]
bin_dir = \"/opt/cni/bin\"
conf_dir = \"/etc/cni/net.d\"
conf_template = \"\"
[plugins.cri.registry]
[plugins.cri.registry.mirrors]
[plugins.cri.registry.mirrors.\"docker.io\"]
endpoint = [\"https://registry-1.docker.io\"]
{{range .CONTAINERD_REGISTRY -}}[plugins.cri.registry.mirrors.\"{{.}}\"]
endpoint = [\"{{.}}\"]
{{end -}}
[plugins.cri.x509_key_pair_streaming]
tls_cert_file = \"\"
tls_key_file = \"\"
[plugins.diff-service]
default = [\"walking\"]
[plugins.linux]
shim = \"containerd-shim\"
runtime = \"runc\"
runtime_root = \"\"
no_shim = false
shim_debug = false
[plugins.opt]
path = \"/opt/containerd\"
[plugins.restart]
interval = \"10s\"
[plugins.scheduler]
pause_threshold = 0.02
deletion_threshold = 0
mutation_threshold = 100
schedule_delay = \"0s\"
startup_delay = \"100ms\"
`)
tmpl, err := template.New("text").Parse(templateText)
if err != nil {
logger.Error("template parse failed:", err)
panic(1)
}
var envMap = make(map[string]interface{})
envMap["DOCKER_REGISTRY"] = RegistryArr
envMap["DOCKER_LIB"] = Lib
envMap["ZERO"] = 0
envMap["CONTAINERD_REGISTRY"] = RegistryArr
envMap["CONTAINERD_LIB"] = d.lib()
var buffer bytes.Buffer
_ = tmpl.Execute(&buffer, envMap)
return buffer.Bytes()
}

func (d Containerd) Print() {
func (d *Containerd) Print() {
urlPrefix := "https://github.com/containerd/containerd/releases/download/v%s/containerd-%s.linux-amd64.tar.gz"
versions := []string{
"1.1.0",
Expand Down
35 changes: 21 additions & 14 deletions install/command/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,58 @@ func NewDocker() stepInterface {

const dockerFileName = "docker.tgz"

func (d Docker) SendPackage(host string) {
func (d *Docker) lib() string {
if Lib == "" {
return "/var/lib/docker"
} else {
return Lib
}
}

func (d *Docker) SendPackage(host string) {
SendPackage(host, PkgUrl, dockerFileName)
}

func (d Docker) Tar(host string) {
func (d *Docker) Tar(host string) {
cmd := fmt.Sprintf("tar --strip-components=1 -xvzf /root/%s -C /usr/local/bin", dockerFileName)
Cmd(host, cmd)
}

func (d Docker) Config(host string) {
cmd := "mkdir -p " + Lib
func (d *Docker) Config(host string) {
cmd := "mkdir -p " + d.lib()
Cmd(host, cmd)
cmd = "mkdir -p /etc/docker"
Cmd(host, cmd)
cmd = "echo \"" + string(d.configFile()) + "\" > /etc/docker/daemon.json"
Cmd(host, cmd)
}

func (d Docker) Enable(host string) {
func (d *Docker) Enable(host string) {
cmd := "echo \"" + string(d.serviceFile()) + "\" > /usr/lib/systemd/system/docker.service"
Cmd(host, cmd)
cmd = "systemctl enable docker.service && systemctl restart docker.service"
Cmd(host, cmd)
}

func (d Docker) Version(host string) {
func (d *Docker) Version(host string) {
cmd := "docker version"
Cmd(host, cmd)
}

func (d Docker) Uninstall(host string) {
func (d *Docker) Uninstall(host string) {
cmd := "systemctl stop docker.service && systemctl disable docker.service"
Cmd(host, cmd)
cmd = "rm -rf /usr/local/bin/runc && rm -rf /usr/local/bin/ctr && rm -rf /usr/local/bin/containerd* "
Cmd(host, cmd)
cmd = "rm -rf /usr/local/bin/docker* && rm -rf /var/lib/docker && rm -rf /etc/docker/* "
Cmd(host, cmd)
if Lib != "" {
cmd = "rm -rf " + Lib
if d.lib() != "" {
cmd = "rm -rf " + d.lib()
Cmd(host, cmd)
}
}

func (d Docker) serviceFile() []byte {
func (d *Docker) serviceFile() []byte {
var templateText = string(`[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
Expand All @@ -82,7 +90,7 @@ WantedBy=multi-user.target
`)
return []byte(templateText)
}
func (d Docker) configFile() []byte {
func (d *Docker) configFile() []byte {
var templateText = string(`{
\"registry-mirrors\": [
\"http://373a6594.m.daocloud.io\"
Expand All @@ -100,14 +108,13 @@ func (d Docker) configFile() []byte {
}
var envMap = make(map[string]interface{})
envMap["DOCKER_REGISTRY"] = RegistryArr
envMap["DOCKER_LIB"] = Lib
envMap["ZERO"] = 0
envMap["DOCKER_LIB"] = d.lib()
var buffer bytes.Buffer
_ = tmpl.Execute(&buffer, envMap)
return buffer.Bytes()
}

func (d Docker) Print() {
func (d *Docker) Print() {
urlPrefix := "https://download.docker.com/linux/static/stable/x86_64/docker-%s.tgz"
versions := []string{
"17.03.0-ce",
Expand Down

0 comments on commit 05fdaae

Please sign in to comment.