diff --git a/cmd/cluster.go b/cmd/cluster.go index 03e582d4..7c1a2181 100644 --- a/cmd/cluster.go +++ b/cmd/cluster.go @@ -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 } diff --git a/cmd/cluster/cluster.go b/cmd/cluster/cluster.go index a800dcc2..a11da6d2 100644 --- a/cmd/cluster/cluster.go +++ b/cmd/cluster/cluster.go @@ -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 +} diff --git a/hack/manifests/scripts/startupnode.sh b/hack/manifests/scripts/startupnode.sh new file mode 100755 index 00000000..0e69c09d --- /dev/null +++ b/hack/manifests/scripts/startupnode.sh @@ -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 diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8dae5d05..bec0dcd9 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -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) }