Skip to content

Commit

Permalink
添加docker离线安装命令
Browse files Browse the repository at this point in the history
  • Loading branch information
ren_jw committed Oct 2, 2020
1 parent 74f2b02 commit 393136d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 20 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,21 @@

flags 可选 --offline=true --file=./v19.03.13.tar.gz (离线安装)

> 样例
> 在线安装样例
在线安装`docker`(确保宿主机可访问http://mirrors.aliyun.com)

easyctl install docker

> 离线安装样例
**适用于CentOS7**

[下载docker x86压缩包](https://download.docker.com/linux/static/stable/x86_64/)

执行命令安装(--offline --file为必须参数)

easyctl install docker --file=./docker-19.03.9.tgz --offline

## 安装nginx

Expand Down
41 changes: 35 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func init() {
installRedisCmd.Flags().StringVarP(&redisPassword, "password", "a", "redis", "Redis password")
installRedisCmd.Flags().StringVarP(&redisDataDir, "data", "d", "/var/lib/redis", "Redis persistent directory")
installRedisCmd.Flags().StringVarP(&redisLogDir, "log-file", "", "/var/log/redis", "Redis logfile directory")
installRedisCmd.Flags().StringVarP(&filePath, "file", "f", "", "redis-x-x-x.tar.gz path")
installRedisCmd.Flags().StringVarP(&filePath, "file", "f", "", "docker-x-x-x.tgz path")
installRedisCmd.Flags().StringVarP(&redisBinaryPath, "binary-path", "", "/usr/bin/", "redis-* binary file path")

installDockerCmd.Flags().StringVarP(&filePath, "file", "f", "", "redis-x-x-x.tar.gz path")
installDockerCmd.Flags().BoolVarP(&offline, "offline", "o", false, "offline mode")

installCmd.AddCommand(installDockerCmd)
installCmd.AddCommand(installNginxCmd)
installCmd.AddCommand(installRedisCmd)
Expand All @@ -61,9 +64,14 @@ var installDockerCmd = &cobra.Command{
Use: "docker [flags]",
Short: "install docker through easyctl",
Example: "\neasyctl install docker 在线安装docker" +
"\neasyctl install docker --offline --file=./v19.03.13.tar.gz 离线安装docker",
"\neasyctl install docker --offline --file=./docker-19.03.9.tgz 离线安装docker",
Run: func(cmd *cobra.Command, args []string) {
installDocker()
fmt.Println("dddddddddd", !offline)
if !offline {
installDockerOnline()
} else {
installDockerOffline()
}
},
}

Expand Down Expand Up @@ -93,8 +101,8 @@ var installRedisCmd = &cobra.Command{
},
}

// 安装docker
func installDocker() {
// 在线安装docker
func installDockerOnline() {
fmt.Println("检测内核...")
if !sys.AccessAliMirrors() {
panic(netConnectErr)
Expand Down Expand Up @@ -123,6 +131,27 @@ func installDocker() {

}

// 离线安装docker
func installDockerOffline() {

fmt.Println("离线安装docker...")
docker := "tar zxvf docker-*.tgz;mv docker/* /usr/bin/"
util.ExecuteCmdAcceptResult(docker)

// 配置系统服务
fmt.Println("[redis]配置redis系统服务...")
sys.ConfigService("docker")

sys.CloseSeLinux(true)
fmt.Println("[docker]启动docker...")
startRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartDocker)
fmt.Println("[docker]设置docker开机自启动...")
enableRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartDockerForever)
if startRe == nil && enableRe == nil {
util.PrintSuccessfulMsg("docker安装成功...")
}
}

// 安装nginx
func installNginx() {

Expand Down Expand Up @@ -166,7 +195,7 @@ func installRedisOnline() {
startRedis()
}

// 在线安装redis
// 离线安装redis
func installRedisOffline() {

gcc := "rpm -qa|grep \"^gcc\";echo $?"
Expand Down
38 changes: 38 additions & 0 deletions constant/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package constant

const (
Redhat7DockerServiceFilePath = "/usr/lib/systemd/system/docker.service"
)

// /usr/lib/systemd/system/docker.service
const Redhat7DockerServiceContent = "" +
"[Unit]\nDescription=Docker Application Container Engine\n" +
"Documentation=https://docs.docker.com\n" +
"After=network-online.target firewalld.service\n" +
"Wants=network-online.target\n" +
"[Service]\n" +
"Type=notify\n" +
"# the default is not to use systemd for cgroups because the delegate issues still\n" +
"# exists and systemd currently does not support the cgroup feature set required\n" +
"# for containers run by docker\n" +
"ExecStart=/usr/bin/dockerd\n" +
"ExecReload=/bin/kill -s HUP \n" +
"# Having non-zero Limit*s causes performance problems due to accounting overhead\n" +
"# in the kernel. We recommend using cgroups to do container-local accounting.\n" +
"LimitNOFILE=infinity\n" +
"LimitNPROC=infinity\n" +
"LimitCORE=infinity\n" +
"# Uncomment TasksMax if your systemd version supports it.\n" +
"# Only systemd 226 and above support this version.\n" +
"#TasksMax=infinity\n" +
"TimeoutStartSec=0\n" +
"# set delegate yes so that systemd does not reset the cgroups of docker containers\n" +
"Delegate=yes\n" +
"# kill only the docker process, not all processes in the cgroup\n" +
"KillMode=process\n" +
"# restart the docker process if it exits prematurely\n" +
"Restart=on-failure\n" +
"StartLimitBurst=3\n" +
"StartLimitInterval=60s\n" +
"[Install]\n" +
"WantedBy=multi-user.target"
4 changes: 3 additions & 1 deletion resources/redis.go → constant/redis.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package resources
package constant

const Redhat7RedisServiceFilePath = "/usr/lib/systemd/system/redis.service"

const Redhat7RedisServiceContent = "" +
"[Unit]\n" +
Expand Down
2 changes: 1 addition & 1 deletion resources/yum.go → constant/yum.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resources
package constant

const CentOSAliBaseYUMContent = "[base]\n" +
"name=CentOS-$releasever - Base - mirrors.aliyun.com\n" +
Expand Down
29 changes: 23 additions & 6 deletions sys/service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sys

import (
"easyctl/resources"
"easyctl/constant"
"easyctl/util"
"errors"
)
Expand All @@ -12,8 +12,8 @@ type Service struct {
}

const (
redis = "redis"
Redhat7RedisServiceFilePath = "/usr/lib/systemd/system/redis.service"
redis = "redis"
docker = "docker"
)

var unSupportSystemErr = errors.New("暂不支持当前系统...")
Expand All @@ -22,21 +22,38 @@ var unrecognizedServiceErr = errors.New("暂不识别的服务名称")
func ConfigService(serviceName string) {
switch serviceName {
case redis:
configRedisService()
configRedisService(constant.Redhat7RedisServiceFilePath, constant.Redhat7RedisServiceContent)
case docker:
configDockerService(constant.Redhat7DockerServiceFilePath, constant.Redhat7DockerServiceContent)
default:
panic(unrecognizedServiceErr)
}
}

func configRedisService() {
func configRedisService(path string, content string) {
systemType := SystemInfoObject.OSVersion.ReleaseType
mainNumber := SystemInfoObject.OSVersion.MainVersionNumber
//fmt.Println("----" + systemType + mainNumber)
if systemType != RedhatReleaseType {
panic(unSupportSystemErr)
}
if mainNumber == "7" {
util.CreateFile(Redhat7RedisServiceFilePath, resources.Redhat7RedisServiceContent)
util.CreateFile(path, content)
util.ExecuteCmd("systemctl daemon-reload")
} else {
panic(unSupportSystemErr)
}
}

func configDockerService(path string, content string) {
systemType := SystemInfoObject.OSVersion.ReleaseType
mainNumber := SystemInfoObject.OSVersion.MainVersionNumber
//fmt.Println("----" + systemType + mainNumber)
if systemType != RedhatReleaseType {
panic(unSupportSystemErr)
}
if mainNumber == "7" {
util.CreateFile(path, content)
util.ExecuteCmd("systemctl daemon-reload")
} else {
panic(unSupportSystemErr)
Expand Down
10 changes: 5 additions & 5 deletions sys/set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sys

import (
"easyctl/resources"
"easyctl/constant"
"easyctl/util"
"fmt"
"log"
Expand Down Expand Up @@ -52,7 +52,7 @@ func SetAliYUM() {
fmt.Println(err.Error())
}

_, baseWriteErr := baseRepoFile.Write([]byte(resources.CentOSAliBaseYUMContent))
_, baseWriteErr := baseRepoFile.Write([]byte(constant.CentOSAliBaseYUMContent))
if baseWriteErr != nil {
fmt.Println(baseWriteErr.Error())
fmt.Println("[failed] " + aliBaseEL7WriteErrMsg)
Expand All @@ -65,7 +65,7 @@ func SetAliYUM() {
fmt.Println(err.Error())
}

_, epelWriteErr := epelRepoFile.Write([]byte(resources.CentOSAliEpelYUMContent))
_, epelWriteErr := epelRepoFile.Write([]byte(constant.CentOSAliEpelYUMContent))
if epelWriteErr != nil {
fmt.Println(epelWriteErr.Error())
fmt.Println("[failed] " + aliEpelEL7WriteErrMsg)
Expand Down Expand Up @@ -95,7 +95,7 @@ func SetLocalYUM() {
fmt.Println(err.Error())
}

_, localWriteErr := localRepoFile.Write([]byte(resources.CentOSLocalYUMContent))
_, localWriteErr := localRepoFile.Write([]byte(constant.CentOSLocalYUMContent))
if localWriteErr != nil {
fmt.Println(localWriteErr.Error())
fmt.Println("[failed] " + localWriteErrMsg)
Expand All @@ -118,7 +118,7 @@ func SetNginxMirror() {
fmt.Println(err.Error())
}

_, nginxRepoFileWriteErr := nginxRepoFile.Write([]byte(resources.CentOSNginxMirrorContent))
_, nginxRepoFileWriteErr := nginxRepoFile.Write([]byte(constant.CentOSNginxMirrorContent))
if nginxRepoFileWriteErr != nil {
fmt.Println(nginxRepoFileWriteErr.Error())
fmt.Println("[failed] " + nginxRepoFileWriteErrMsg)
Expand Down

0 comments on commit 393136d

Please sign in to comment.