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 25, 2019
1 parent da788c4 commit 337a0d5
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 45 deletions.
76 changes: 76 additions & 0 deletions cmd/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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")
}
80 changes: 80 additions & 0 deletions install/command.go
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 14 additions & 0 deletions install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package install

//username
var (
User string
Passwd string
Hosts []string
RegistryArr []string
DockerLib string
PkgUrl string
)

type DockerInstaller struct {
}
39 changes: 39 additions & 0 deletions install/send_package.go
Original file line number Diff line number Diff line change
@@ -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()
}
45 changes: 0 additions & 45 deletions install/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"crypto/tls"
"fmt"
"html/template"
"net"
"net/http"
"os"
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}

0 comments on commit 337a0d5

Please sign in to comment.