Skip to content

Commit

Permalink
添加redis在线安装命令
Browse files Browse the repository at this point in the history
  • Loading branch information
ren_jw committed Oct 2, 2020
1 parent fededbc commit 0c5d0dc
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 28 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [install 安装](#install指令集)
* [docker](#安装docker)
* [nginx](#安装nginx)
* [redis](#安装redis)
* [set 设置](#set指令集)
* [dns 域名解析](#配置dns域名解析)
* [hosname 主机名](#配置主机名)
Expand Down Expand Up @@ -137,6 +138,33 @@
在线安装`nginx`(确保宿主机可访问http://mirrors.aliyun.com)

easyctl install nginx

## 安装redis

> 格式
easyctl install redis [flags]

flag

Flags:
-b, --bind string Redis bind address (default "0.0.0.0")
-d, --data string Redis persistent directory (default "/var/lib/redis")
-h, --help help for redis
-l, --log-file string Redis logfile directory (default "/var/log/redis")
-o, --offline offline mode
-a, --password string Redis password (default "redis")
-p, --port string Redis listen port (default "6379")

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

easyctl install redis

参数定制

easyctl install redis --bind=192.168.131.36 --data=/var/lib/redis --port=6380 --password=redis567

# search指令集

Expand Down
149 changes: 121 additions & 28 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,37 @@ package cmd
import (
"easyctl/sys"
"easyctl/util"
"errors"
"fmt"
"github.com/spf13/cobra"
)

var installValidArgs = []string{"docker", "nginx"}
const RedisConfigPath = "/etc/redis.conf"

var installValidArgs = []string{"docker", "nginx", "redis"}
var (
Offline bool
RedisPort string
RedisBindIP string
RedisPassword string
RedisDataDir string
RedisLogDir string
netConnectErr error
installErr error
)

func init() {
netConnectErr = errors.New("网络连接异常,请选择离线方式安装...")
installErr = errors.New("程序安装失败...")
installRedisCmd.Flags().BoolVarP(&Offline, "offline", "o", false, "offline mode")
installRedisCmd.Flags().StringVarP(&RedisPort, "port", "p", "6379", "Redis listen port")
installRedisCmd.Flags().StringVarP(&RedisBindIP, "bind", "b", "0.0.0.0", "Redis bind address")
installRedisCmd.Flags().StringVarP(&RedisPassword, "password", "a", "redis", "Redis password")
installRedisCmd.Flags().StringVarP(&RedisDataDir, "data", "d", "/var/lib/redis", "Redis persistent directory")
installRedisCmd.Flags().StringVarP(&RedisDataDir, "log-file", "l", "/var/log/redis", "Redis logfile directory")
installCmd.AddCommand(installDockerCmd)
installCmd.AddCommand(installNginxCmd)
installCmd.AddCommand(installRedisCmd)
rootCmd.AddCommand(installCmd)
}

Expand Down Expand Up @@ -49,46 +71,117 @@ var installNginxCmd = &cobra.Command{
},
}

// install redis命令
var installRedisCmd = &cobra.Command{
Use: "redis [flags]",
Short: "install redis through easyctl",
Example: "\neasyctl install redis 在线安装redis" +
"\neasyctl install redis --offline=true --file=./redis-5.0.5.tar.gz 离线安装redis",
Run: func(cmd *cobra.Command, args []string) {
if !Offline {
installRedisOnline()
}
},
}

// 安装docker
func installDocker() {
fmt.Println("检测内核...")
if !sys.AccessAliMirrors() {
panic(netConnectErr)
}

sys.SetAliYUM()
cmd := "yum -y install yum-utils device-mapper-persistent-data lvm2;" +
install := "yum -y install yum-utils device-mapper-persistent-data lvm2;" +
"yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo;" +
"yum makecache fast;" +
"yum -y install docker-ce"
if sys.AccessAliMirrors() {
_, re := util.ExecuteCmdAcceptResult(cmd)
if re != nil {
fmt.Println(re.Error())
}
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安装成功...")
}

//
_, re := util.ExecuteCmdAcceptResult(install)
if re != nil {
fmt.Println(re.Error())
}

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() {

sys.SetNginxMirror()
cmd := "yum -y install nginx"
if sys.AccessAliMirrors() {
_, re := util.ExecuteCmdAcceptResult(cmd)
if re != nil {
fmt.Println(re.Error())
}
sys.CloseSeLinux(true)
fmt.Println("[nginx]启动nginx...")
startRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartNginx)
fmt.Println("[nginx]设置nginx开机自启动...")
enableRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartNginxForever)
if startRe == nil && enableRe == nil {
util.PrintSuccessfulMsg("nginx安装成功...")
}

if !sys.AccessAliMirrors() {
panic(netConnectErr)
}
_, re := util.ExecuteCmdAcceptResult(cmd)
if re != nil {
panic(installErr)
}

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

}

// 在线安装redis
func installRedisOnline() {

installCmd := "yum -y install redis"
if !sys.AccessAliMirrors() {
panic(netConnectErr)
}

sys.SetAliYUM()
_, re := util.ExecuteCmdAcceptResult(installCmd)
if re != nil {
panic(installErr)
}

configRedis()
fmt.Println("[redis]启动redis...")
startRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartRedis)
fmt.Println("[redis]配置redis为系统服务...")
enableRe, _ := util.ExecuteCmd(sys.SystemInfoObject.ServiceAction.StartRedisForever)
if startRe == nil && enableRe == nil {
util.PrintSuccessfulMsg("redis安装成功...")
}
}

// 配置redis
func configRedis() {
modifyBgsaveErr := fmt.Sprintf("sed -i \"s#stop-writes-on-bgsave-error yes#stop-writes-on-bgsave-error no#g\" %s", RedisConfigPath)
modifyDaemonCmd := fmt.Sprintf("sed -i 's#daemonize no#daemonize yes#g' %s", RedisConfigPath)
modifyBindCmd := fmt.Sprintf("sed -i \"s#port 6379#port %s#g\" %s", RedisPort, RedisConfigPath)
modifyListenPortCmd := fmt.Sprintf("sed -i \"s#bind 127.0.0.1#bind %s#g\" %s", RedisBindIP, RedisConfigPath)
modifyPassword := fmt.Sprintf("echo \"requirepass %s\" >> %s", RedisPassword, RedisConfigPath)
modifyDataDir := fmt.Sprintf("sed -i \"s#dir ./#dir %s#g\" %s", RedisDataDir, RedisConfigPath)
modifyLogDir := fmt.Sprintf("sed -i \"s#logfile \\\"\\\"#logfile %s/redis.log#g\" %s", RedisLogDir, RedisConfigPath)
fmt.Println("[redis]配置redis...")
util.ExecuteCmd(modifyBgsaveErr)
util.ExecuteCmd(modifyDaemonCmd)
util.ExecuteCmd(modifyBindCmd)
util.ExecuteCmd(modifyListenPortCmd)
util.ExecuteCmd(modifyPassword)
util.ExecuteCmd(modifyDataDir)
util.ExecuteCmd(modifyLogDir)
fmt.Println("[redis]创建redis工作目录...")
util.ExecuteCmd(fmt.Sprintf("mkdir -p %s;mkdir -p %s", RedisLogDir, RedisDataDir))
fmt.Println("[redis]配置overcommit_memory...")
util.ExecuteCmd("echo \"vm.overcommit_memory = 1\" >> /etc/sysctl.conf;sysctl -p")
}
4 changes: 4 additions & 0 deletions sys/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type ServiceActionCommand struct {
StartDockerForever string // 永久开启docker服务
StartNginx string // 开启nginx服务
StartNginxForever string // 永久开启nginx服务
StartRedis string // 开启redis服务
StartRedisForever string // 永久开启redis服务
}

type OSVersion struct {
Expand Down Expand Up @@ -123,6 +125,8 @@ func (system *SystemInfo) loadRedhatManageServiceCmd() {
system.ServiceAction.StartDockerForever = packageRedhatService(version, "docker", start, true)
system.ServiceAction.StartNginx = packageRedhatService(version, "nginx", start, false)
system.ServiceAction.StartNginxForever = packageRedhatService(version, "nginx", start, true)
system.ServiceAction.StartRedis = packageRedhatService(version, "redis", start, false)
system.ServiceAction.StartRedisForever = packageRedhatService(version, "redis", start, true)
}

func (system *SystemInfo) loadRunLevel() {
Expand Down

0 comments on commit 0c5d0dc

Please sign in to comment.