Exemple #1
0
def salt_reload_minion(hostname):  # 重启salt-minion
    try:
        obj = models.Servers.objects.get(
            hostname=hostname)  # 2是saltstack服务端主机在表中的id
        # hostname = str(obj.ip)    # 默认是unicode需要转换
        webssh.hostname = str(
            obj.hostname)  # 主机名,没有做dns解析或修改/etc/hosts文件,则需要使用主机IP
        webssh.port = obj.serverpassword.port  # 查看类型是int,不需要转换
        webssh.username = str(obj.serverpassword.username)
        webssh.password = str(obj.serverpassword.password)
        command = 'systemctl restart salt-minion'
        ret = webssh.ssh_command(command)
        time.sleep(1)
        command1 = 'ps -ef|grep salt-minion|wc -l'
        result = webssh.ssh_command(command1)
        time.sleep(2)
        # print result
        if result >= 3:
            return True
        else:
            print '%s重启salt-minion失败。' % hostname
            return False
    except:
        print '%s重启salt-minion失败。' % hostname
        return False
Exemple #2
0
def salt_minion(hostname):  # 客户端安装salt-minion
    try:
        obj = models.Servers.objects.get(hostname=hostname)
        # hostname = str(obj.ip)    # 默认是unicode需要转换
        webssh.hostname = str(
            obj.hostname)  # 主机名,没有做dns解析或修改/etc/hosts文件,则需要使用主机IP
        webssh.port = obj.serverpassword.port  # 查看类型是int,不需要转换
        webssh.username = str(obj.serverpassword.username)
        webssh.password = str(obj.serverpassword.password)
        sed_master = 'sed -i "s/#master: salt/master: %s/" /etc/salt/minion' % 'saltstack'
        sed_id = 'sed -i "s/#id:/id: %s/" /etc/salt/minion' % hostname
        command = [
            'yum -y install wget',
            'mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup_`date +%F`',
            'mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup_`date +%F`',
            'wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo',
            'yum -y install salt-minion',
            'sed -i "s/#default_include/default_include/g" /etc/salt/minion',
            sed_master, sed_id, 'systemctl enable salt-minion',
            'systemctl restart salt-minion'
        ]  # 一定要做hosts解析;master: saltstack ==> slatsatck服务端的主机名或IP; id: client1 ==> 客户端的主机名
        # command = ["mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup_`date +%F`", 'mkdir aaa']
        for i in command:
            ret = webssh.ssh_command(i)
        # ret = webssh.ssh_command(command)
        time.sleep(2)
        command1 = 'ps -ef|grep salt-minion|wc -l'
        result = webssh.ssh_command(command1)
        # print result
        if result >= 3:
            return True
        else:
            return False
    except:
        print '%s主机无法连接,安装已停止。请检查主机连接信息。' % hostname
        return False
Exemple #3
0
def web_ssh_excute(
        hostname,
        command):  # paramiko实现远程执行命令,当salt不能使用时备用;前端发起ajax请求后台实现远程执行命令函数
    try:
        obj = models.Servers.objects.get(hostname=hostname)
        # hostname = str(obj.ip)    # 默认是unicode需要转换
        webssh.hostname = str(
            obj.hostname)  # 主机名,没有做dns解析或修改/etc/hosts文件,则需要使用主机IP
        webssh.port = obj.serverpassword.port  # 查看类型是int,不需要转换
        webssh.username = str(obj.serverpassword.username)
        webssh.password = str(obj.serverpassword.password)
        command = command
        ret = webssh.ssh_command(command)
        result = {hostname: ret}
        time.sleep(0.5)
        print '%s 执行命令 %s 完毕......' % (hostname, command)
        return result
    except:
        ret = '主机无法连接,执行命令请求已停止。请检查主机连接信息。'
        result = {hostname: ret}
        print '%s 主机无法连接,执行命令请求已停止。请检查主机连接信息。' % hostname
        return result
Exemple #4
0
def shutwodn_server(hostname):  # 关闭主机
    try:
        obj = models.Servers.objects.get(hostname=hostname)
        # hostname = str(obj.ip)    # 默认是unicode需要转换
        webssh.hostname = str(
            obj.hostname)  # 主机名,没有做dns解析或修改/etc/hosts文件,则需要使用主机IP
        webssh.port = obj.serverpassword.port  # 查看类型是int,不需要转换
        webssh.username = str(obj.serverpassword.username)
        webssh.password = str(obj.serverpassword.password)
        command1 = 'shutdown -h now'
        result = webssh.ssh_command(command1)
        time.sleep(1)
        print '%s正在关机中......' % hostname
        return True
        # print result
        # if result >= 3:
        #     return True
        # else:
        #     return False
    except:
        print '%s主机关机失败。' % hostname
        return False