From e23030e288d31a3e07f62086e7dbec3045978703 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Fri, 26 Jul 2019 13:55:05 +0800 Subject: [PATCH] docker install files --- .drone.yml | 13 +-------- README.md | 30 ++++++++++++++++++-- cmd/install.go | 19 +++++++------ cmd/print.go | 55 ++++++++++++++++++------------------ cmd/root.go | 43 +--------------------------- cmd/uninstall.go | 20 ++++++------- install/command.go | 45 ++++++++++++++++++++++------- install/command_test.go | 36 +++++++++++++++++++++++ install/install.go | 41 +++++++++++++++++++++++++++ install/send_package.go | 29 +++++++++---------- install/send_package_test.go | 16 +++++++++++ 11 files changed, 218 insertions(+), 129 deletions(-) create mode 100644 install/command_test.go create mode 100644 install/send_package_test.go diff --git a/.drone.yml b/.drone.yml index c59f6d1..0868002 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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: diff --git a/README.md b/README.md index 665d3a1..953d013 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmd/install.go b/cmd/install.go index 64edbb4..261bed3 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "github.com/cuisongliu/docker-install/install" "github.com/spf13/cobra" ) @@ -23,15 +23,10 @@ import ( // 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() }, } @@ -39,6 +34,12 @@ 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.: diff --git a/cmd/print.go b/cmd/print.go index b645d2c..a075f01 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -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 { diff --git a/cmd/root.go b/cmd/root.go index 40f728a..e1801c3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,9 +18,7 @@ import ( "fmt" "os" - homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" - "github.com/spf13/viper" ) var cfgFile string @@ -28,13 +26,7 @@ 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) { }, @@ -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()) - } } diff --git a/cmd/uninstall.go b/cmd/uninstall.go index d204314..1764373 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -15,7 +15,7 @@ package cmd import ( - "fmt" + "github.com/cuisongliu/docker-install/install" "github.com/spf13/cobra" ) @@ -23,23 +23,21 @@ import ( // 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") diff --git a/install/command.go b/install/command.go index 017af05..e115d77 100644 --- a/install/command.go +++ b/install/command.go @@ -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) } @@ -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 { @@ -58,12 +68,14 @@ 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 { @@ -71,10 +83,23 @@ func dockerConfig(registryArr []string, dir string) []byte { 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() +} diff --git a/install/command_test.go b/install/command_test.go new file mode 100644 index 0000000..65b31bd --- /dev/null +++ b/install/command_test.go @@ -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)) +} diff --git a/install/install.go b/install/install.go index 636c157..21bae01 100644 --- a/install/install.go +++ b/install/install.go @@ -1,5 +1,7 @@ package install +import "sync" + //username var ( User string @@ -10,5 +12,44 @@ var ( PkgUrl string ) +const fileName = "docker.tgz" + +func NewDockerInstaller() *DockerInstaller { + return &DockerInstaller{} +} + type DockerInstaller struct { } + +type DockerInstallInterface interface { + Install() + UnInstall() +} + +func (s *DockerInstaller) Install() { + var wg sync.WaitGroup + for _, host := range Hosts { + wg.Add(1) + go func(host string) { + defer wg.Done() + sendPackage(PkgUrl) + tarDocker(host) + configDocker(host) + enableDocker(host) + versionDocker(host) + }(host) + } + wg.Wait() +} + +func (s *DockerInstaller) UnInstall() { + var wg sync.WaitGroup + for _, host := range Hosts { + wg.Add(1) + go func(host string) { + defer wg.Done() + uninstallDocker(host) + }(host) + } + wg.Wait() +} diff --git a/install/send_package.go b/install/send_package.go index d2b96f2..deef314 100644 --- a/install/send_package.go +++ b/install/send_package.go @@ -2,38 +2,35 @@ package install import ( "fmt" - "path" "strings" "sync" ) -//SendPackage is -func (s *DockerInstaller) SendPackage(url string) { - pkg := path.Base(url) +func sendPackage(url string) { //only http isHttp := strings.HasPrefix(url, "http") - wgetCommand := "" + downloadCmd := "" if isHttp { - wgetParam := "" + downloadParam := "" if strings.HasPrefix(url, "https") { - wgetParam = "--no-check-certificate" + downloadParam = "--no-check-certificate" } - wgetCommand = fmt.Sprintf(" wget %s ", wgetParam) + downloadCmd = fmt.Sprintf(" wget %s -O %s", downloadParam, fileName) } - remoteCmd := fmt.Sprintf("cd /root && %s %s ", wgetCommand, url) - kubeLocal := fmt.Sprintf("/root/%s", pkg) + remoteCmd := fmt.Sprintf("cd /root && %s %s ", downloadCmd, url) + localFile := fmt.Sprintf("/root/%s", fileName) var wm sync.WaitGroup - for _, master := range Hosts { + for _, host := range Hosts { wm.Add(1) - go func(master string) { + go func(host string) { defer wm.Done() if isHttp { - go WatchFileSize(master, kubeLocal, GetFileSize(url)) - Cmd(master, remoteCmd) + go WatchFileSize(host, localFile, GetFileSize(url)) + Cmd(host, remoteCmd) } else { - Copy(master, url, kubeLocal) + Copy(host, url, localFile) } - }(master) + }(host) } wm.Wait() } diff --git a/install/send_package_test.go b/install/send_package_test.go new file mode 100644 index 0000000..e13907a --- /dev/null +++ b/install/send_package_test.go @@ -0,0 +1,16 @@ +package install + +import "testing" + +func TestDockerInstaller_SendPackage(t *testing.T) { + User = "root" + Passwd = "admin" + Hosts = []string{"172.16.213.131"} + sendPackage("https://download.docker.com/linux/static/stable/x86_64/docker-19.03.0.tgz") +} +func TestDockerInstaller_SendPackageLocalFile(t *testing.T) { + User = "root" + Passwd = "admin" + Hosts = []string{"172.16.213.131"} + sendPackage("/home/cuisongliu/Documents/kubernetes-doc/docker-19.03.0.tgz") +}