Skip to content

Commit

Permalink
docker install files
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Jul 26, 2019
1 parent 337a0d5 commit e23030e
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 129 deletions.
13 changes: 1 addition & 12 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,12 @@ steps:
commands:
- go build -o docker-install

- name: publish-hub
image: plugins/docker
settings:
repo: cuisongliu/docker-install
tags: ${DRONE_TAG=latest}
username:
from_secret: docker-hub-user
dockerfile: Dockerfile
password:
from_secret: docker-hub-pass

- name: publish
image: plugins/github-release
settings:
api_key:
from_secret: git-release-token
files: sealos
files: docker-install
title: ${DRONE_TAG}
note: Note.md
when:
Expand Down
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# docker-install

install
### Install
install docker from url:
```shell script
docker-install install
--host 172.16.213.131
--user root
--passwd admin
--registry 127.0.0.1
--docker-lib /var/lib/docker
--pkg-url /root/docker-19.0.3.tgz
```

uninstall
### UnInstall
uninstall docker:
```shell script
docker-install uninstall
--host 172.16.213.131
--user root
--passwd admin
--docker-lib /var/lib/docker
```

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

the Newest version is v19.03.0.
19 changes: 10 additions & 9 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,31 @@
package cmd

import (
"fmt"
"github.com/cuisongliu/docker-install/install"

"github.com/spf13/cobra"
)

// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "install docker for url",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("install called")
install := install.NewDockerInstaller()
install.Install()
},
}

func init() {
rootCmd.AddCommand(installCmd)

// Here you will define your flags and configuration settings.
installCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
installCmd.Flags().StringVar(&install.Passwd, "passwd", "admin", "servers user password for ssh")
installCmd.Flags().StringSliceVar(&install.Hosts, "host", []string{}, "docker install hosts")
installCmd.Flags().StringSliceVar(&install.RegistryArr, "registry", []string{"127.0.0.1"}, "docker's registry ip")
installCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "https://download.docker.com/linux/static/stable/x86_64/docker-19.03.0.tgz download offline docker url, or file localtion ex. /root/docker.tgz")
installCmd.Flags().StringVar(&install.DockerLib, "docker-lib", "/var/lib/docker", "docker store location")

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
Expand Down
55 changes: 28 additions & 27 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,36 @@ const urlPrefix = "https://download.docker.com/linux/static/stable/x86_64/docker

// printCmd represents the print command
var printCmd = &cobra.Command{
Use: "print",
Use: "print",
Short: "print docker download url",
Run: func(cmd *cobra.Command, args []string) {
versions := []string{
"docker-17.03.0-ce.tgz",
"docker-17.03.1-ce.tgz",
"docker-17.03.2-ce.tgz",
"docker-17.06.0-ce.tgz",
"docker-17.06.1-ce.tgz",
"docker-17.06.2-ce.tgz",
"docker-17.09.0-ce.tgz",
"docker-17.09.1-ce.tgz",
"docker-17.12.0-ce.tgz",
"docker-17.12.1-ce.tgz",
"docker-18.03.0-ce.tgz",
"docker-18.03.1-ce.tgz",
"docker-18.06.0-ce.tgz",
"docker-18.06.1-ce.tgz",
"docker-18.06.2-ce.tgz",
"docker-18.06.3-ce.tgz",
"docker-18.09.0.tgz",
"docker-18.09.1.tgz",
"docker-18.09.2.tgz",
"docker-18.09.3.tgz",
"docker-18.09.4.tgz",
"docker-18.09.5.tgz",
"docker-18.09.6.tgz",
"docker-18.09.7.tgz",
"docker-18.09.8.tgz",
"docker-19.03.0.tgz",
"17.03.0-ce",
"17.03.1-ce",
"17.03.2-ce",
"17.06.0-ce",
"17.06.1-ce",
"17.06.2-ce",
"17.09.0-ce",
"17.09.1-ce",
"17.12.0-ce",
"17.12.1-ce",
"18.03.0-ce",
"18.03.1-ce",
"18.06.0-ce",
"18.06.1-ce",
"18.06.2-ce",
"18.06.3-ce",
"18.09.0",
"18.09.1",
"18.09.2",
"18.09.3",
"18.09.4",
"18.09.5",
"18.09.6",
"18.09.7",
"18.09.8",
"19.03.0",
}

for _, v := range versions {
Expand Down
43 changes: 1 addition & 42 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,15 @@ import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "docker-install",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "docker install for bin file",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -50,40 +42,7 @@ func Execute() {
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.docker-install.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".docker-install" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".docker-install")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
20 changes: 9 additions & 11 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@
package cmd

import (
"fmt"
"github.com/cuisongliu/docker-install/install"

"github.com/spf13/cobra"
)

// uninstallCmd represents the uninstall command
var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "uninstall docker and delete dockerLib files",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("uninstall called")
install := install.NewDockerInstaller()
install.UnInstall()
},
}

func init() {
rootCmd.AddCommand(uninstallCmd)

uninstallCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
uninstallCmd.Flags().StringVar(&install.Passwd, "passwd", "admin", "servers user password for ssh")
uninstallCmd.Flags().StringSliceVar(&install.Hosts, "host", []string{}, "docker install hosts")
uninstallCmd.Flags().StringVar(&install.DockerLib, "docker-lib", "/var/lib/docker", "docker store location")
// Here you will define your flags and configuration settings.

//uninstallCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// uninstallCmd.PersistentFlags().String("foo", "", "A help for foo")
Expand Down
45 changes: 35 additions & 10 deletions install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package install

import (
"bytes"
"fmt"
"github.com/wonderivan/logger"
"strings"
"text/template"
)

func tarDocker(host string) {
cmd := "tar --strip-components=1 -xvzf /root/docker.tgz -C /usr/local/bin"
cmd := fmt.Sprintf("tar --strip-components=1 -xvzf /root/%s -C /usr/local/bin", fileName)
Cmd(host, cmd)
}
func configDocker(host string) {
cmd := "mkdir -p " + DockerLib
Cmd(host, cmd)
cmd = "mkdir -p /etc/docker"
Cmd(host, cmd)
cmd = "echo \"" + string(dockerConfig(RegistryArr, DockerLib)) + "\" > /etc/docker/daemon.json"
Cmd(host, cmd)
}
Expand All @@ -24,13 +27,20 @@ func enableDocker(host string) {
Cmd(host, cmd)
}

func versionDocker(host string) {
cmd := "docker version"
Cmd(host, cmd)
}

func uninstallDocker(host string) {
cmd := "systemctl stop docker.service && systemctl disable docker.service"
Cmd(host, cmd)
cmd = "rm -rf /usr/local/bin/docker* && rm -rf /var/lib/docker && rm -rf /etc/docker/* "
Cmd(host, cmd)
cmd = "rm -rf " + DockerLib
Cmd(host, cmd)
if DockerLib != "" {
cmd = "rm -rf " + DockerLib
Cmd(host, cmd)
}
}

func dockerServiceFile() []byte {
Expand Down Expand Up @@ -58,23 +68,38 @@ WantedBy=multi-user.target

func dockerConfig(registryArr []string, dir string) []byte {
var templateText = string(`{
"registry-mirrors": [
"http://373a6594.m.daocloud.io"
\"registry-mirrors\": [
\"http://373a6594.m.daocloud.io\"
],
"insecure-registries":
[{{.DOCKER_REGISTRY}}],
"graph":"{{.DOCKER_LIB}}"
{{if len .DOCKER_REGISTRY}}
\"insecure-registries\":
[{{range $i,$v :=.DOCKER_REGISTRY}}{{if eq $i 0}}\"{{$v}}\"{{else}},\"{{$v}}\"{{end}}{{end}}],
{{end}}
\"graph\":\"{{.DOCKER_LIB}}\"
}`)
tmpl, err := template.New("text").Parse(templateText)
if err != nil {
logger.Error("template parse failed:", err)
panic(1)
}
var envMap = make(map[string]interface{})
registry := strings.Join(registryArr, ",")
envMap["DOCKER_REGISTRY"] = registry
envMap["DOCKER_REGISTRY"] = registryArr
envMap["DOCKER_LIB"] = dir
envMap["ZERO"] = 0
var buffer bytes.Buffer
_ = tmpl.Execute(&buffer, envMap)
return buffer.Bytes()
}

func registryJoin(registryArr []string) string {
var sb strings.Builder
for i, v := range registryArr {
if i != 0 {
sb.Write([]byte(","))
}
sb.Write([]byte("\""))
sb.Write([]byte(v))
sb.Write([]byte("\""))
}
return sb.String()
}
36 changes: 36 additions & 0 deletions install/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package install

import "testing"

func Test_InstallDocker(t *testing.T) {
User = "root"
Passwd = "admin"
host := "172.16.213.131"
RegistryArr = []string{}
DockerLib = "/var/lib/docker"
tarDocker(host)
configDocker(host)
enableDocker(host)
versionDocker(host)
}

func Test_UnInstallDocker(t *testing.T) {
User = "root"
Passwd = "admin"
host := "172.16.213.131"
DockerLib = "/var/lib/docker"
uninstallDocker(host)
versionDocker(host)
}

func Test_RegistryJoin(t *testing.T) {
hosts := []string{"172.16.213.131", "172.16.213.131"}
t.Log(registryJoin(hosts))
}

func Test_DockerConfig(t *testing.T) {
RegistryArr = []string{"127.0.0.1", "127.0.0.2"}
DockerLib = "/var/lib/docker"
s := dockerConfig(RegistryArr, DockerLib)
t.Log(string(s))
}
Loading

0 comments on commit e23030e

Please sign in to comment.