devops日常笔记

devops日常笔记

部署上线

研发人员会在bitbucket上提交pr(pull requests),根据上线需求进行代码merge(合并),然后在pipelines进行deploy操作,如果有对配置文件的修改,需要运维人员登录至服务器上,修改配置文件后,再进行deploy操作。如果已经deploy了后,发现需要进行配置文件的修改,两种方式:
1.修改后,保存并退出,在bitbucket上点击redeploy,重新部署上线
2.修改后,保存并退出,直接在服务器上重启部署的war包,重启代码如下:

./run.sh xxxxx.war restart

数据库

对数据库的修改一定要谨慎,一般研发人员会在文档里面说明,修改什么,增加什么,删除什么。无论如何,在操作之前都最好是做一次备份,以便修改错误后恢复。

脚本

上周部门负责人让我运行Python脚本,给所有服务器添加一个运维人员的账号并赋予sudo权限。内容包含创建用户和删除用户以及改变

首先,这个脚本如下,需要的根据实际情况进行修改:

#!/usr/bin/env python
# coding: utf-8
import paramiko
import random
import sys

hosts = [{
    'server': 'xxxxxxxx1',
    'lanip': 'xxx.xxx.xxx.xxx',
    'wanip': 'xxx.xxx.xxx.xxx'
},{
    'server': 'xxxxxxxx2',
    'lanip': 'xxx.xxx.xxx.xxx',
    'wanip': 'xxx.xxx.xxx.xxx'
}]

ROOT_PASSWORD = ""

def randomPassword(length):
    letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=/\.+-'
    result_str = ''.join(random.choice(letters) for i in range(length))
    return result_str

def addLinuxUser(server, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    ssh.connect(hostname=server, username="root", password=ROOT_PASSWORD)
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("useradd -g root " + username)
    print(ssh_stdout.read().decode())
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("passwd " + username)
    ssh_stdin.write(password + "\n")
    ssh_stdin.flush()
    ssh_stdin.write(password + "\n")
    ssh_stdin.flush()
    print(ssh_stdout.read().decode())
    ssh.close()
    print("server: " + server + " user: " + username + " created!")

def addUsers(username, password):
    for host in hosts:
        wanip = host['wanip']
        if len(wanip) > 0:
            addLinuxUser(wanip, username, password)

def delLinuxUser(server, username):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    ssh.connect(hostname=server, username="root", password=ROOT_PASSWORD)
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("userdel " + username)
    print(ssh_stdout.read().decode())
    ssh.close()
    print("server: " + server + " user: " + username + " deleted!")

def delUsers(username):
    for host in hosts:
        wanip = host['wanip']
        if len(wanip) > 0:
            delLinuxUser(wanip, username)

def changeLinuxUserPass(server, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    ssh.connect(hostname=server, username="root", password=ROOT_PASSWORD)
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("passwd " + username)
    ssh_stdin.write(password + "\n")
    ssh_stdin.flush()
    ssh_stdin.write(password + "\n")
    ssh_stdin.flush()
    print(ssh_stdout.read().decode())
    ssh.close()
    print("server: " + server + " user: " + username + " created!")

def changeUserPass(username, password):
    for host in hosts:
        wanip = host['wanip']
        if len(wanip) > 0:
            changeLinuxUserPass(wanip, username, password)

def printHelp():
    print("usage: python linux_user.py [addUser|delUser|changePass] username [password]")

def addUser(argv):
    if len(argv) < 1:
        printHelp()
        return
    username = argv[0]
    password = randomPassword(16)
    print("create user: " + username + " password: " + password)
    addUsers(username, password)

def delUser(argv):
    if len(argv) < 1:
        printHelp()
        return
    username = argv[1]
    if username=='root':
        print("You can't delete root user")
        return
    
def changePass(argv):
    if len(argv) < 2:
        printHelp()
        return
    username = argv[0]
    password = argv[1]
    if username == 'root':
        print("You can't delete root user")
        return
    changeUserPass(username, password)

def main(argv):
    if len(argv)<1:
        printHelp()
        return
    action = argv[0].lower()
    if action=='adduser':
        addUser(argv[1:])
    elif action=='deluser':
        delUser(argv[1:])
    elif action=='changepass':
        changePass(argv[1:])
    else:
        printHelp()

if __name__ == "__main__":
    main(sys.argv[1:])

脚本写完了,如何运行呢,首先你得安装了Python和pip,然后再安装paramiko

pip install paramiko

或者

pip3 install paramiko

这些都弄完后就可以执行脚本了,如添加一个test账户则

python xxxx.py addUser test

密码是随机生成,很复杂,会在脚本执行后的第一二行显示。
然后就可以用创建的账号登录了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值