1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
func delIngress(ctx context.Context, dockerClient *client.Client, config Config) {
networks, err := dockerClient.NetworkList(ctx, types.NetworkListOptions{})
if err != nil {
panic(err)
}
for _, network := range networks {
if network.Name == "ingress" {
sugarLogger.Infoln("ingress 网络存在 ")
err = dockerClient.NetworkRemove(ctx, "ingress")
if err != nil {
panic(err)
}
sugarLogger.Infoln("ingress 已经被移除 ")
}
}
sugarLogger.Infoln("正在重启dockerd, 请等待...")
reloadDocker(config)
}
func rebuildIngress(ctx context.Context, dockerClient *client.Client, config Config) {
// 创建一个overlay网络,名为ingress,subnet为10.0.0.1/20
networkName := "ingress"
ipaconfig := network.IPAMConfig{
Subnet: config.Ingress.Subnet,
Gateway: config.Ingress.Gateway,
}
networkcrt := types.NetworkCreate{
Driver: "overlay",
Scope: "swarm",
EnableIPv6: false,
IPAM: &network.IPAM{
Driver: "default",
Config: []network.IPAMConfig{
ipaconfig,
},
},
Internal: false,
Attachable: false,
Ingress: true,
ConfigOnly: false,
ConfigFrom: &network.ConfigReference{
Network: "",
},
Options: map[string]string{
"com.docker.network.driver.overlay.vxlanid_list": "4098",
"com.docker.network.mtu": "1400",
},
Labels: map[string]string{},
}
resp, err := dockerClient.NetworkCreate(ctx, networkName, networkcrt)
if err != nil {
panic(err)
}
sugarLogger.Infof("Ingress网络%s创建成功 ", resp.ID)
sugarLogger.Infoln("正在重启dockerd, 请等待...")
reloadDocker(config)
}
func reloadDocker(config Config) {
for _, host := range config.Host {
// 设置 SSH 客户端配置
sshConfig := &ssh.ClientConfig{
User: host.Username,
Auth: []ssh.AuthMethod{
ssh.Password(host.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// 连接到远程 SSH 服务器
conn, err := ssh.Dial("tcp", host.IP+":"+strconv.Itoa(host.Port), sshConfig)
if err != nil {
sugarLogger.Fatalf("SSH远程连接失败 : %s", err)
}
defer conn.Close()
// 在 SSH 连接上创建一个新会话
session, err := conn.NewSession()
if err != nil {
sugarLogger.Fatalf("创建 session 失败: %s", err)
}
defer session.Close()
// 运行命令重启Docker服务
cmd := "systemctl restart docker"
if err := session.Run(cmd); err != nil {
sugarLogger.Fatalf("执行CMD命令失败: %s", err)
}
sugarLogger.Infoln(host.IP, "docker已重启 ")
}
}
|