Skip to content

Commit

Permalink
feat(cluster): support startup cluster
Browse files Browse the repository at this point in the history
support startup cluster

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Oct 13, 2023
1 parent d192378 commit d1ea04b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newCmdCluster(f factory.Factory) *cobra.Command {
clusterCmd.AddCommand(cluster.CleanCommand(f))
clusterCmd.AddCommand(cluster.StatusCommand(f))
clusterCmd.AddCommand(cluster.StopCommand(f))
clusterCmd.AddCommand(cluster.StartUPCommand(f))
clusterCmd.AddCommand(storage.NewCmdStorage(f))
return clusterCmd
}
19 changes: 19 additions & 0 deletions cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,22 @@ func StopCommand(f factory.Factory) *cobra.Command {
}
return stop
}

func StartUPCommand(f factory.Factory) *cobra.Command {
myCluster := cluster.NewCluster(f)
log := f.GetLog()
stop := &cobra.Command{
Use: "startup",
Short: "startup cluster",
Version: "3.0.9",
RunE: func(cmd *cobra.Command, args []string) error {
status, _ := confirm.Confirm("Are you sure to start cluster")
if status {
return myCluster.StartUP()
}
log.Donef("cancel start cluster")
return nil
},
}
return stop
}
15 changes: 15 additions & 0 deletions hack/manifests/scripts/startupnode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
# Copyright (c) 2021-2023 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved.
# Use of this source code is covered by the following dual licenses:
# (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2)
# (2) Affero General Public License 3.0 (AGPL 3.0)
# license that can be found in the LICENSE file.

[ $(id -u) -eq 0 ] || exec sudo $0 $@

for service in /etc/systemd/system/k3s*.service; do
[ -s $service ] && systemctl enable $(basename $service)
[ -s $service ] && systemctl start $(basename $service)
done

exit 0
32 changes: 26 additions & 6 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,21 +529,41 @@ func (c *Cluster) Stop() error {
for _, ip := range ips {
c.log.Debugf("stop node %s", ip)
wg.Add(1)
go c.stopNode(ip, sshClient, &wg)
go c.actionNode(ip, "stop", common.GetCustomScripts("hack/manifests/scripts/stopnode.sh"), sshClient, &wg)
}
wg.Wait()
c.log.Done("stop cluster success")
return nil
}

func (c *Cluster) stopNode(ip string, sshClient ssh.Interface, wg *sync.WaitGroup) {
// StartUP 启动集群
func (c *Cluster) StartUP() error {
c.log.Info("startup cluster")
cfg, _ := config.LoadConfig()
ips := cfg.GetIPs()
if len(ips) == 0 {
ips = append(ips, exnet.LocalIPs()[0])
}
sshClient := ssh.NewSSHClient(&cfg.Global.SSH, true)
var wg sync.WaitGroup
for _, ip := range ips {
c.log.Debugf("startup node %s", ip)
wg.Add(1)
go c.actionNode(ip, "startup", common.GetCustomScripts("hack/manifests/scripts/startupnode.sh"), sshClient, &wg)
}
wg.Wait()
c.log.Done("startup cluster success")
return nil
}

func (c *Cluster) actionNode(ip, action, script string, sshClient ssh.Interface, wg *sync.WaitGroup) {
defer wg.Done()
c.log.StartWait(fmt.Sprintf("start stop node: %s", ip))
err := sshClient.CmdAsync(ip, common.GetCustomScripts("hack/manifests/scripts/stopnode.sh"))
c.log.StartWait(fmt.Sprintf("start %s node: %s", action, ip))
err := sshClient.CmdAsync(ip, script)
c.log.StopWait()
if err != nil {
c.log.Warnf("stop node %s failed, reason: %v", ip, err)
c.log.Warnf("%s node %s failed, reason: %v", action, ip, err)
return
}
c.log.Donef("stop node %s success", ip)
c.log.Donef("%s node %s success", action, ip)
}

0 comments on commit d1ea04b

Please sign in to comment.