首页 > 其他分享 >go提醒操作

go提醒操作

时间:2023-05-10 22:14:40浏览次数:31  
标签:提醒 node cmd SSHConfig rf clean go 操作 CmdAsync

func Confirm(prompt string) bool {
var (
inputStr string
err error
)
_, err = fmt.Fprint(os.Stdout, prompt)
if err != nil {
logger.Error("fmt.Fprint err", err)
os.Exit(-1)
}

_, err = fmt.Scanf("%s", &inputStr)
if err != nil {
logger.Error("fmt.Scanf err", err)
os.Exit(-1)
}

return GetConfirmResult(inputStr)
}


func GetConfirmResult(str string) bool {
return YesRx.MatchString(str)
}


package install

import (
"fmt"
"os"
"strings"
"sync"

"github.com/fanux/sealos/ipvs"
"github.com/fanux/sealos/pkg/logger"
sshcmd "github.com/fanux/sealos/pkg/sshcmd/cmd"
)

type SealosClean struct {
SealosInstaller
cleanAll bool
}

// BuildClean clean the build resources.
func BuildClean(deleteNodes, deleteMasters []string) {
i := &SealosClean{cleanAll: false}
masters := MasterIPs
nodes := NodeIPs
//1. 删除masters
if len(deleteMasters) != 0 {
if !CleanForce { // false
prompt := fmt.Sprintf("clean command will clean masters [%s], continue clean (y/n)?", strings.Join(deleteMasters, ","))
result := Confirm(prompt)
if !result {
logger.Debug("clean masters command is skip")
goto node
}
}
//只清除masters
i.Masters = deleteMasters
}

//2. 删除nodes
node:
if len(deleteNodes) != 0 {
if !CleanForce { // flase
prompt := fmt.Sprintf("clean command will clean nodes [%s], continue clean (y/n)?", strings.Join(deleteNodes, ","))
result := Confirm(prompt)
if !result {
logger.Debug("clean nodes command is skip")
goto all
}
}
//只清除nodes
i.Nodes = deleteNodes
}
//3. 删除所有节点
all:
if len(deleteNodes) == 0 && len(deleteMasters) == 0 && CleanAll {
if !CleanForce { // flase
result := Confirm(`clean command will clean all masters and nodes, continue clean (y/n)?`)
if !result {
logger.Debug("clean all node command is skip")
goto end
}
}
// 所有master节点
i.Masters = masters
// 所有node节点
i.Nodes = nodes
i.cleanAll = true
}
end:
if len(i.Masters) == 0 && len(i.Nodes) == 0 {
logger.Warn("clean nodes and masters is empty,please check your args and config.yaml.")
os.Exit(-1)
}
i.CheckValid()
i.Clean()
if i.cleanAll {
logger.Info("if clean all and clean sealos config")
home, _ := os.UserHomeDir()
cfgPath := home + defaultConfigPath
sshcmd.Cmd("/bin/sh", "-c", "rm -rf "+cfgPath)
}
}

//Clean clean cluster.
func (s *SealosClean) Clean() {
var wg sync.WaitGroup
//s 是要删除的数据
//全局是当前的数据
if len(s.Nodes) > 0 {
//1. 再删除nodes
for _, node := range s.Nodes {
wg.Add(1)
go func(node string) {
defer wg.Done()
s.cleanNode(node)
}(node)
}
wg.Wait()
}
if len(s.Masters) > 0 {
//2. 先删除master
lock := sync.Mutex{}
for _, master := range s.Masters {
wg.Add(1)
go func(master string) {
lock.Lock()
defer lock.Unlock()
defer wg.Done()
s.cleanMaster(master)
}(master)
}
wg.Wait()
}
}

func (s *SealosClean) cleanNode(node string) {
cleanRoute(node)
clean(node)
//remove node
NodeIPs = SliceRemoveStr(NodeIPs, node)
if !s.cleanAll {
logger.Debug("clean node in master")
if len(MasterIPs) > 0 {
hostname := isHostName(MasterIPs[0], node)
cmd := "kubectl delete node %s"
_ = SSHConfig.CmdAsync(MasterIPs[0], fmt.Sprintf(cmd, strings.TrimSpace(hostname)))
}
}
}

func (s *SealosClean) cleanMaster(master string) {
clean(master)
//remove master
MasterIPs = SliceRemoveStr(MasterIPs, master)
if !s.cleanAll {
logger.Debug("clean node in master")
if len(MasterIPs) > 0 {
hostname := isHostName(MasterIPs[0], master)
cmd := "kubectl delete node %s"
_ = SSHConfig.CmdAsync(MasterIPs[0], fmt.Sprintf(cmd, strings.TrimSpace(hostname)))
}
//清空所有的nodes的数据
yaml := ipvs.LvsStaticPodYaml(VIP, MasterIPs, LvscareImage)
var wg sync.WaitGroup
for _, node := range NodeIPs {
wg.Add(1)
go func(node string) {
defer wg.Done()
_ = SSHConfig.CmdAsync(node, "rm -rf /etc/kubernetes/manifests/kube-sealyun-lvscare*")
_ = SSHConfig.CmdAsync(node, fmt.Sprintf("mkdir -p /etc/kubernetes/manifests && echo '%s' > /etc/kubernetes/manifests/kube-sealyun-lvscare.yaml", yaml))
}(node)
}
wg.Wait()
}
}

func clean(host string) {
cmd := "kubeadm reset -f " + vlogToStr()
_ = SSHConfig.CmdAsync(host, cmd)
cmd = `sed -i '/kubectl/d;/sealos/d' /root/.bashrc`
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "modprobe -r ipip && lsmod"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf ~/.kube/ && rm -rf /etc/kubernetes/"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf /etc/systemd/system/kubelet.service.d && rm -rf /etc/systemd/system/kubelet.service"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf /usr/bin/kube* && rm -rf /usr/bin/crictl"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf /etc/cni && rm -rf /opt/cni"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf /var/lib/etcd && rm -rf /var/etcd"
_ = SSHConfig.CmdAsync(host, cmd)
cmd = fmt.Sprintf("sed -i \"/%s/d\" /etc/hosts ", APIServer)
_ = SSHConfig.CmdAsync(host, cmd)
cmd = "rm -rf ~/kube"
_ = SSHConfig.CmdAsync(host, cmd)
//clean pki certs
cmd = "rm -rf /etc/kubernetes/pki"
_ = SSHConfig.CmdAsync(host, cmd)
//clean sealos in /usr/bin/ except exec sealos
cmd = "ps -ef |grep -v 'grep'|grep sealos >/dev/null || rm -rf /usr/bin/sealos"
_ = SSHConfig.CmdAsync(host, cmd)
}

func cleanRoute(node string) {
// clean route
cmdRoute := fmt.Sprintf("sealos route --host %s", IPFormat(node))
status := SSHConfig.CmdToString(node, cmdRoute, "")
if status != "ok" {
// 删除为 vip创建的路由。
delRouteCmd := fmt.Sprintf("sealos route del --host %s --gateway %s", VIP, IPFormat(node))
SSHConfig.CmdToString(node, delRouteCmd, "")
}
}

标签:提醒,node,cmd,SSHConfig,rf,clean,go,操作,CmdAsync
From: https://www.cnblogs.com/cheyunhua/p/17389474.html

相关文章

  • go的IO读取与写入
    func(s*SealosInstaller)appendAPIServer()error{etcHostPath:="/etc/hosts"etcHostMap:=fmt.Sprintf("%s%s",IPFormat(s.Masters[0]),APIServer)file,err:=os.OpenFile(etcHostPath,os.O_RDWR|os.O_APPEND,0666)iferr!=......
  • 【pytorch】理解张量,了解张量的创建和操作
    深度学习的核心是卷积,卷积的核心是张量(Tensor)理解TensorTensor可以简单理解为是标量、向量、矩阵的高维扩展。你可以把张量看作多维数组,但相较于ndarray,Tensor包含了grad、requires_grad、grad_fn、device等属性,是为服务于神经网络而设计的类型,标量可以看作是零维张量、......
  • Hibernate 基本操作、懒加载以及缓存
    前言上一篇咱们介绍了Hibernate以及写了一个Hibernate的工具类,快速入门体验了一波Hibernate的使用,我们只需通过Session对象就能实现数据库的操作了。现在,这篇介绍使用Hibernate进行基本的CRUD、懒加载以及缓存的知识。提示:如果你还没看上一篇,那么建议你看完上一篇......
  • pycharm操作git
    1.pycharm拉取项目: 1_2.pycharm拉取项目: 2.pycahrm切换到dev分支,及gitbasehere打开也是同步的切换: 3.git_base_here中切换分支,pycharm也跟着切换: 4.pycharm提交代码:步骤1: 5.pycharm操作git_步骤2:点击上传版本库和提交到远程仓库按钮,commitandpush: 6.pycahrm......
  • ubuntu server版操作系统安装步骤记录
    组内有一个项目支持的时centos的操作系统,后续因为有ubuntu的需求,需要测一下ubuntun操作系统的兼容性,测试前期需要手动为测试服务器装ubuntu的操作系统,因此,这里记录一下自己装ubuntun操作系统的步骤由于项目使用的是ubuntu是18.04.6版本,这里就只记录的是此版本的安装步骤1、下载u......
  • python redis 操作
    目的:验证redis集群模式,高可用场景insert.pyfromredisclusterimportClusterBlockingConnectionPool,RedisCluster,ClusterConnectionPooldefinit():startup_nodes=[{'host':'10.12.16.50','port':6379},{......
  • 麻了,一个操作把MySQL主从复制整崩了
    前言最近公司某项目上反馈mysql主从复制失败,被运维部门记了一次大过,影响到了项目的验收推进,那么究竟是什么原因导致的呢?而主从复制的原理又是什么呢?本文就对排查分析的过程做一个记录。主从复制原理我们先来简单了解下MySQL主从复制的原理。主库master服务器会将SQL记录......
  • 2.2 算术和逻辑操作
    加载有效地址leal指令也称为加载有效地址(loadeffectiveaddress)指令,它实际上是movl指令的变形。它的指令形式是从存储器读数据到寄存器,但实际上它根本没有引用存储器。它的第一个操作数看上去是一个存储器引用,但该指令并不是从指定的位置读取数据,而是将有效地址写入到目的操......
  • 操作系统常见的三种调度模式
    1、高级调度,也叫作业调度,决定把外存上处于后备队列中的哪些作业调入内存,并为它们创建进程、分配必要的资源,排入就绪队列。数据结构有后备队列,数据元素为JCB(作业控制块)。2、中级调度,也叫作交换调度,为提高内存利用率和缓解内存紧张而引入。决定把哪些进程挂起并从内存交换到外存,又......
  • 【备忘】go build 使用代理解决部分模块或依赖无法下载的问题
    1、查看go版本goversion2、如果版本为1.13及以上goenv-wGO111MODULE=ongoenv-wGOPROXY=https://goproxy.io,direct低于1.13版本$env:GO111MODULE="on"$env:GOPROXY="https://goproxy.io"......