def start_service(): for i in IPS.get('master'): ip, port = parse_address(i) ssh = SSHConnect(ip, int(port)) ssh.run( 'systemctl daemon-reload && systemctl enable kube-apiserver && systemctl start kube-apiserver && ' 'systemctl status kube-apiserver && systemctl enable kube-controller-manager && ' 'systemctl start kube-controller-manager && systemctl status kube-controller-manager && ' 'systemctl enable kube-scheduler && systemctl start kube-scheduler && systemctl status kube-scheduler' )
def start_service(): for i in IPS.get('etcd'): ip,port = parse_address(i) cmd = 'mkdir -p mkdir /var/lib/etcd && systemctl daemon-reload && systemctl enable etcd ' \ '&& systemctl start etcd && systemctl status etcd' if not check_is_localip(ip): ssh = SSHConnect(ip,int(port)) ssh.run(cmd) else: exec_shell(cmd)
def set_kubectl(): for i in IPS.get('master'): ip, port = parse_address(i) ssh = SSHConnect(ip, int(port)) ssh.run( 'source /etc/profile && kubectl config set-cluster kubernetes ' '--certificate-authority=/opt/kubernetes/ssl/ca.pem --embed-certs=true ' '--server=https://{}:6443'.format(ip)) ssh.run( 'source /etc/profile && kubectl config set-credentials admin ' '--client-certificate=/opt/kubernetes/ssl/admin.pem --embed-certs=true ' '--client-key=/opt/kubernetes/ssl/admin-key.pem') ssh.run( 'source /etc/profile && kubectl config set-context kubernetes --cluster=kubernetes --user=admin' ) ssh.run('source /etc/profile && kubectl config use-context kubernetes')
def check_health(): etcd_endpoints = get_etcd_endpoints() cmd = 'source /etc/profile && etcdctl --endpoints={} --ca-file=/opt/kubernetes/ssl/ca.pem ' \ '--cert-file=/opt/kubernetes/ssl/etcd.pem --key-file=/opt/kubernetes/ssl/etcd-key.pem ' \ 'cluster-health'.format(etcd_endpoints) for i in IPS.get('etcd'): ip,port = parse_address(i) if not check_is_localip(ip): ssh = SSHConnect(ip,int(port)) ssh.run(cmd) ssh.close() else: exec_shell(cmd)
def init(): ips = get_all_ip(IPS) scripts_dir = os.path.join(CONFIG.PROJECT_DIR, 'utils', 'scripts', 'k8s', 'init') for ipa in ips: if not check_rsa_secret(): create_rsa_secret() ssh_copy_id(ipa) ip, port = parse_address(ipa) print('init {}...'.format(ip)) if not check_is_localip(ip): ssh = SSHConnect(host=ip, port=int(port)) for filename in os.listdir(scripts_dir): f = FileModify(os.path.join(scripts_dir, filename)) ssh.run(f.content()) ssh.close() else: for filename in os.listdir(scripts_dir): f = FileModify(os.path.join(scripts_dir, filename)) exec_shell(f.content())
import os import re # 单位秒, 86400s = 1天 backup_keep_time = 86400 * 3 local_host = '172.18.73.128' local_backup_dir = '/backups/nexus' host = '172.18.73.129' port = 65503 remote_backup_dir = '/backups/nexus' dir = '/var/lib/docker/volumes/bd01a4f32e58cc69a3ad888c6c621a37a72b43e3e964352117d30e112fb5a931/_data/storage' now = int(time.time()) ssh = SSHConnect(host=host, port=port, password='******') ssh.run('mkdir -p {}'.format(remote_backup_dir)) ssh.run('tar -zcf {}/nexus.tar.gz {}'.format(remote_backup_dir, dir)) scp_cmd = 'scp -P {} {}/nexus.tar.gz {}:{}/nexus_{}.tar.gz'.format( port, remote_backup_dir, local_host, local_backup_dir, now) ssh.run(scp_cmd) ssh.run('rm -rf {}'.format(remote_backup_dir)) ssh.close() exec_shell('mkdir -p {}'.format(local_backup_dir)) for dirname in os.listdir(local_backup_dir): t1 = re.findall(r'nexus_(.*?).tar.gz', dirname)[0] times = int(time.time()) - int(t1) if times >= backup_keep_time: os.remove(os.path.join(local_backup_dir, dirname))
from utils.common import exec_shell, SSHConnect import time import os import re # 单位秒, 86400s = 1天 backup_keep_time = 86400 local_backup_dir = '/backups/svn' host = '172.18.196.237' port = 65503 remote_backup_dir = '/productdir/data/backups' now = int(time.time()) ssh = SSHConnect(host=host, port=port) ssh.run( 'tar -zcf {0}/svnrepos.tar.gz {0}/../svnrepos'.format(remote_backup_dir)) ssh.close() scp_cmd = 'scp -P {} {}:{}/svnrepos.tar.gz {}/svnrepos_{}.tar.gz'.format( port, host, remote_backup_dir, local_backup_dir, now) exec_shell('mkdir -p {}'.format(local_backup_dir)) exec_shell(scp_cmd) for dirname in os.listdir(local_backup_dir): t1 = re.findall(r'svnrepos_(.*?).tar.gz', dirname)[0] times = int(time.time()) - int(t1) if times >= backup_keep_time: os.remove(os.path.join(local_backup_dir, dirname))
def check_health(): for i in IPS.get('master'): ip, port = parse_address(i) ssh = SSHConnect(ip, int(port)) ssh.run('source /etc/profile && kubectl get cs')
# FileName = xwiki_back.py from utils.common import exec_shell,SSHConnect import time import os import re # 单位秒, 86400s = 1天 backup_keep_time = 86400 * 3 local_backup_dir = '/backups/xwiki' host = '172.18.196.237' port = 65503 remote_backup_dir = '/usr/local/xwiki_backup' now = int(time.time()) ssh = SSHConnect(host=host,port=port) ssh.run('tar -zcf {0}/xwiki.tar.gz {0}/../xwiki9.2'.format(remote_backup_dir)) ssh.close() scp_cmd = 'scp -P {} {}:{}/xwiki.tar.gz {}/xwiki_{}.tar.gz'.format(port,host,remote_backup_dir,local_backup_dir,now) exec_shell('mkdir -p {}'.format(local_backup_dir)) exec_shell(scp_cmd) for dirname in os.listdir(local_backup_dir): t1 = re.findall(r'xwiki_(.*?).tar.gz', dirname)[0] times = int(time.time()) - int(t1) if times >= backup_keep_time: os.remove(os.path.join(local_backup_dir,dirname))
from utils.common import exec_shell, SSHConnect import time import os import re # 单位秒 backup_keep_time = 86400 local_backup_dir = '/backups/gitlab' host = '172.18.196.237' port = 65503 remote_backup_dir = '/var/opt/gitlab/backups' now = str(int(time.time()))[:-3] ssh = SSHConnect(host=host, port=port) ssh.run('/opt/gitlab/bin/gitlab-rake gitlab:backup:create') ssh.close() scp_cmd = 'scp -P {} {}:{}/{}* {}'.format(port, host, remote_backup_dir, now, local_backup_dir) exec_shell('mkdir -p {}'.format(local_backup_dir)) exec_shell(scp_cmd) for dirname in os.listdir(local_backup_dir): t1 = re.findall(r'(.*?)_.*', dirname)[0] times = int(time.time()) - int(t1) if times >= backup_keep_time: os.remove(os.path.join(local_backup_dir, dirname))
from utils.common import SSHConnect, exec_shell import time import os import re import datetime # 备份保留时间,单位:天 backup_keep_time = 2 host = '172.18.196.243' port = 22 remote_backup_dir = '/mnt/wwwroot/history_version' local_backup_dir = '/backups/static' ssh = SSHConnect(host=host, port=port) ssh.run( 'tar -zcf {}/source.tar.gz /mnt/wwwroot/source'.format(remote_backup_dir)) ssh.close() today = time.strftime('%Y%m%d', time.localtime(time.time())) exec_shell('mkdir -p /backups/static') scp_cmd = 'scp {}:{}/source.tar.gz {}/source_{}.tar.gz'.format( host, remote_backup_dir, local_backup_dir, today) exec_shell(scp_cmd) for dirname in os.listdir(local_backup_dir): t1 = re.findall(r'source_(.+?).tar.gz', dirname)[0] time1 = datetime.datetime.strptime(t1, '%Y%m%d') time_dif = datetime.datetime.today() - time1 times = time_dif.days if times >= backup_keep_time: