diff --git a/cmd/print.go b/cmd/print.go new file mode 100644 index 0000000..b645d2c --- /dev/null +++ b/cmd/print.go @@ -0,0 +1,76 @@ +// Copyright © 2019 NAME HERE +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +const urlPrefix = "https://download.docker.com/linux/static/stable/x86_64/docker-%s.tgz" + +// printCmd represents the print command +var printCmd = &cobra.Command{ + Use: "print", + 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", + } + + for _, v := range versions { + println(fmt.Sprintf(urlPrefix, v)) + } + + }, +} + +func init() { + rootCmd.AddCommand(printCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // printCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // printCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/install/command.go b/install/command.go new file mode 100644 index 0000000..017af05 --- /dev/null +++ b/install/command.go @@ -0,0 +1,80 @@ +package install + +import ( + "bytes" + "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(host, cmd) +} +func configDocker(host string) { + cmd := "mkdir -p " + DockerLib + Cmd(host, cmd) + cmd = "echo \"" + string(dockerConfig(RegistryArr, DockerLib)) + "\" > /etc/docker/daemon.json" + Cmd(host, cmd) +} +func enableDocker(host string) { + cmd := "echo \"" + string(dockerServiceFile()) + "\" > /usr/lib/systemd/system/docker.service" + Cmd(host, cmd) + cmd = "systemctl enable docker.service && systemctl restart docker.service" + 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) +} + +func dockerServiceFile() []byte { + var templateText = string(`[Unit] +Description=Docker Application Container Engine +Documentation=https://docs.docker.com +After=network.target + +[Service] +Type=notify +ExecStart=/usr/local/bin/dockerd +ExecReload=/bin/kill -s HUP $MAINPID +LimitNOFILE=infinity +LimitNPROC=infinity +LimitCORE=infinity +TimeoutStartSec=0 +Delegate=yes +KillMode=process + +[Install] +WantedBy=multi-user.target +`) + return []byte(templateText) +} + +func dockerConfig(registryArr []string, dir string) []byte { + var templateText = string(`{ + "registry-mirrors": [ + "http://373a6594.m.daocloud.io" + ], + "insecure-registries": + [{{.DOCKER_REGISTRY}}], + "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_LIB"] = dir + var buffer bytes.Buffer + _ = tmpl.Execute(&buffer, envMap) + return buffer.Bytes() +} diff --git a/install/install.go b/install/install.go new file mode 100644 index 0000000..636c157 --- /dev/null +++ b/install/install.go @@ -0,0 +1,14 @@ +package install + +//username +var ( + User string + Passwd string + Hosts []string + RegistryArr []string + DockerLib string + PkgUrl string +) + +type DockerInstaller struct { +} diff --git a/install/send_package.go b/install/send_package.go new file mode 100644 index 0000000..d2b96f2 --- /dev/null +++ b/install/send_package.go @@ -0,0 +1,39 @@ +package install + +import ( + "fmt" + "path" + "strings" + "sync" +) + +//SendPackage is +func (s *DockerInstaller) SendPackage(url string) { + pkg := path.Base(url) + //only http + isHttp := strings.HasPrefix(url, "http") + wgetCommand := "" + if isHttp { + wgetParam := "" + if strings.HasPrefix(url, "https") { + wgetParam = "--no-check-certificate" + } + wgetCommand = fmt.Sprintf(" wget %s ", wgetParam) + } + remoteCmd := fmt.Sprintf("cd /root && %s %s ", wgetCommand, url) + kubeLocal := fmt.Sprintf("/root/%s", pkg) + var wm sync.WaitGroup + for _, master := range Hosts { + wm.Add(1) + go func(master string) { + defer wm.Done() + if isHttp { + go WatchFileSize(master, kubeLocal, GetFileSize(url)) + Cmd(master, remoteCmd) + } else { + Copy(master, url, kubeLocal) + } + }(master) + } + wm.Wait() +} diff --git a/install/utils.go b/install/utils.go index f1f2122..43c7e8d 100644 --- a/install/utils.go +++ b/install/utils.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/tls" "fmt" - "html/template" "net" "net/http" "os" @@ -18,13 +17,6 @@ import ( "golang.org/x/crypto/ssh" ) -//username -var ( - User string - Passwd string - Version string -) - const oneMBByte = 1024 * 1024 func AddrReformat(host string) string { @@ -227,40 +219,3 @@ func SftpConnect(user, password, host string) (*sftp.Client, error) { return sftpClient, nil } - -//Template is -func Template(masters []string, vip string, version string) []byte { - var templateText = string(`apiVersion: kubeadm.k8s.io/v1beta1 -kind: ClusterConfiguration -kubernetesVersion: {{.Version}} -controlPlaneEndpoint: "apiserver.cluster.local:6443" -networking: - podSubnet: 100.64.0.0/10 -apiServer: - certSANs: - - 127.0.0.1 - - apiserver.cluster.local - {{range .Masters -}} - - {{.}} - {{end -}} - - {{.VIP}} ---- -apiVersion: kubeproxy.config.k8s.io/v1alpha1 -kind: KubeProxyConfiguration -mode: "ipvs" -ipvs: - excludeCIDRs: - - "{{.VIP}}/32"`) - 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["VIP"] = vip - envMap["Masters"] = masters - envMap["Version"] = version - var buffer bytes.Buffer - _ = tmpl.Execute(&buffer, envMap) - return buffer.Bytes() -}