python单实例安装脚本
import os
import sys
import configparser
import subprocess
from subprocess import Popen,PIPE
import command
import logging
import getpass
from multiprocessing import Process
import time
import jkdb.settings as settings
from sql.models import InstanceGroup,InstanceInfo
from .ssh_client import MySshClient
logging.basicConfig(filename='install.log',level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class MySQL_Single_Install(object):
def __init__(self,ip,port):
self.ip=ip
self.port=port
def check_mysqlbasedir_exists(self):
cmd='ssh %s ls /usr/local/mysql/|wc -l'%(self.ip)
(status,res)=subprocess.getstatusoutput(cmd)
if int(res)>0:
logger.info('mysql basedir is exists,you can install mysql install directly')
else:
mysql_software='/home/software/mysql-8.0.22-linux-glibc2.12-x86_64.tar.xz'
mid_software='/usr/local/mysql/mysql-8.0.22-linux-glibc2.12-x86_64/'
mid2_software = '/usr/local/mysql/mysql-8.0.22-linux-glibc2.12-x86_64/*'
subprocess.getstatusoutput('ssh %s mkdir -p /usr/local/mysql'%(self.ip))
if status==0:
logger.info('mysql basedir create success')
else:
logger.error('mysql basedir create failed')
sys.exit()
cmd='scp -r %s %s:/home/software'%(mysql_software,self.ip)
(status, res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('scp software to target machine success')
(status,res)=subprocess.getstatusoutput('ssh %s tar xvf %s -C /usr/local/mysql;mv %s /usr/local/mysql;rm -rf %s'%(self.ip,mysql_software,mid2_software,mid_software))
if status==0:
logger.info("mysql base dir software untar succeed")
else:
logger.error("mysql base dir software untar failed")
sys.exit()
(status,res)=subprocess.getstatusoutput('ssh %s mv %s /usr/local/mysql'%(self.ip,mid2_software))
if status==0:
logger.info("mysql base dir install succedd")
else:
logger.error("mysql base dir install failed")
sys.exit()
(status, res) = subprocess.getstatusoutput('ssh %s rm -rf %s' % (self.ip, mid_software))
if status == 0:
logger.info("mysql base dir software del succedd")
else:
logger.error("mysql base dir software del failed")
sys.exit()
else:
logger.error('please check target dir is exists or the privilege is normal')
sys.exit()
def check_mysql_user(self):
cmd='ssh %s cat /etc/passwd|grep mysql|wc -l'%(self.ip)
(status,res)=subprocess.getstatusoutput(cmd)
if int(res)>0:
logger.warning("mysql user is exists in target machine")
else:
cmd='ssh %s groupadd mysql;useradd -g mysql mysql'%(self.ip)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info("mysql user add success in target machine")
else:
logger.error("mysql user cannot be added in target machine")
sys.exit()
def check_datadir(self):
datadir='/storage'
cmd='ssh %s ls %s|grep mysql%s|wc -l'%(self.ip,datadir,self.port)
(status,res)=subprocess.getstatusoutput(cmd)
if int(res)>0:
logger.error('%s mysql %s datadir already exists'%(self.ip,self.port))
sys.exit()
else:
cmd='ssh %s mkdir -p %s/mysql%s'%(self.ip,datadir,self.port)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('mysql datadir create success')
else:
logger.error('mysql datadir create falied')
sys.exit()
def dir_chown(self):
datadir = '/storage/mysql%s'%(self.port)
cmd='ssh %s chown -R mysql:mysql %s;chown -R mysql:mysql /usr/local/mysql;'%(self.ip,datadir)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('mysql install user have privilegs for some directories')
else:
logger.error('mysql install user Granted Permissions failed for some directories')
sys.exit()
cmd = 'ssh %s chmod -R 755 %s /usr/local/mysql' % (self.ip, datadir)
(status, res) = subprocess.getstatusoutput(cmd)
if status == 0:
logger.info('mysql install user have read write privilegs for some directories')
else:
logger.error('mysql install user Granted read write Permissions failed for some directories')
sys.exit()
def conf_defined(self):
cnf=configparser.ConfigParser()
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
print(BASE_DIR)
cnf.read(settings.MySQL_CONF_DIR)
cnf.set('client','port',self.port)
cnf.set('client','socket','/storage/mysql%s/mysql%s.sock'%(self.port,self.port))
cnf.set('mysqld','port',self.port)
cnf.set('mysqld', 'socket', '/storage/mysql%s/mysql%s.sock' % (self.port, self.port))
cnf.set('mysqld','datadir','/storage/mysql%s'%(self.port))
serverid=subprocess.getoutput('date +%s')
cnf.set('mysqld','server-id',serverid)
confilename='%s_%s_my%s.cnf'%(self.ip,self.port,self.port)
cnf.write(open(confilename,'w'))
cmd='scp %s %s:/etc'%(confilename,self.ip)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('scp mycnf file success')
# time.sleep(100)
else:
logger.error('scp mycnf file failed')
sys.exit()
cmd='ssh %s mv /etc/%s_%s_my%s.cnf /etc/my%s.cnf'%(self.ip,self.ip,self.port,self.port,self.port)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('target my**.cnf file rename success')
else:
logger.error('target my**.cnf file rename failed')
sys.exit()
def install_mysql(self):
my_ssh_client = MySshClient()
cmd='/usr/local/mysql/bin/mysqld --initialize --user=mysql --lower-case-table-names=1 --basedir=/usr/local/mysql --datadir=/storage/mysql%s>/tmp/%s_%s_install.log 2>&1'%(self.port,self.ip,self.port)
print(cmd)
res=my_ssh_client.ssh_interactive(self.ip,cmd)
def get_temporarypassword(self):
cmd="ssh %s cat /tmp/%s_%s_install.log |grep temporary|awk -F '@' '{print $2}'|cut -d ':' -f2-"%(self.ip,self.ip,self.port)
(status,res)=subprocess.getstatusoutput(cmd)
if status==0:
logger.info('mysql install temporary password get succeed')
print(res)
return res
else:
logger.error('mysql install temporary password get faild')
sys.exit()
def start_mysql(self):
my_ssh_client = MySshClient()
cmd = '/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my%s.cnf >/dev/null 2>&1 &'%(self.port)
print(cmd)
my_ssh_client.ssh_interactive(self.ip,cmd)
cmd = 'ssh %s ps -ef|grep mysql|grep %s|wc -l' % (self.ip, self.port)
print(cmd)
(status, res) = subprocess.getstatusoutput(cmd)
if int(res)>0:
logger.info('mysql %s is started,next step is reset root password' % (self.port))
else:
logger.error('mysql %s start failed,please check error.log' % (db_port))
sys.exit()
def get_mysqlsocket(self):
cmd = "ssh %s ps -ef|grep socket|grep %s|awk -F 'socket=' '{print $2}'|awk '{print $1}'|head -1"%(self.ip,self.port)
print(cmd)
(status,socket)=subprocess.getstatusoutput(cmd)
print(socket,type(socket))
return socket
def check_installtempfile(self):
my_ssh_client = MySshClient()
res = my_ssh_client.ssh_interactive(self.ip, 'ls /tmp/|grep mysqlinstalltemp.txt|wc -l')
if int(res) > 0:
logger.error('mysql repeat exec tempfile is exists,please drop it')
sys.exit()
else:
pass
def reset_password(self):
tmp_pwd = self.get_temporarypassword().lstrip()
socket = self.get_mysqlsocket().strip()
my_ssh_client = MySshClient()
cmd = "/usr/local/mysql/bin/mysql -uroot -p'%s' -S %s --connect-expired-password -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY 'test123' PASSWORD EXPIRE NEVER;\">/tmp/mysqlinstalltemp.txt 2>&1" % (tmp_pwd, socket)
res=my_ssh_client.ssh_interactive(self.ip, cmd)
def create_extruser(self):
my_ssh_client = MySshClient()
socket = self.get_mysqlsocket().strip()
cmd="mysql -uroot -p'test123' -S %s -e \"CREATE USER 'dbadmin'@'%%' IDENTIFIED BY 'dbadmin';ALTER USER 'dbadmin'@'%%' IDENTIFIED BY 'dbadmin' PASSWORD EXPIRE NEVER;ALTER USER 'dbadmin'@'%%' IDENTIFIED WITH mysql_native_password BY 'dbadmin';grant all privileges on *.* to 'dbadmin'@'%%';CREATE USER 'repl'@'192.168.56.%%' identified by 'repl2data';GRANT replication slave,replication client ON *.* TO 'repl'@'192.168.56.%%';ALTER USER 'repl'@'192.168.56.%%' IDENTIFIED BY 'repl2data' PASSWORD EXPIRE NEVER;ALTER USER 'repl'@'192.168.56.%%' IDENTIFIED WITH mysql_native_password BY 'repl2data';flush privilegs\";ifconfig"%(socket)
print(cmd)
my_ssh_client.ssh_interactive(self.ip,cmd)
def install_plugin(self):
my_ssh_client = MySshClient()
socket = self.get_mysqlsocket().strip()
cmd = "mysql -uroot -p'test123' -S %s -e\"INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';\"" % (socket)
print(cmd)
res=my_ssh_client.ssh_interactive(self.ip,cmd)
print(res)
if res!='no vaild data':
logger.info('mysql semi sync plugin master and slave all install succeed')
else:
logger.error('mysql semi sync plugin master and slave install failed')
sys.exit()
def set_osenv(self):
cmd="ssh %s cat /root/.bash_profile |grep 'PATH='|grep mysql|grep bin|wc -l"%(self.ip)
print(cmd)
(status,res)=subprocess.getstatusoutput(cmd)
if int(res)==0:
my_ssh_client = MySshClient()
cmd="sed -i '/PATH=/ s/$/\:\/usr\/local\/mysql\/bin/' /root/.bash_profile"
print(cmd)
my_ssh_client.ssh_interactive(self.ip,cmd)
logger.info('mysql env add success')
else:
logger.info('mysql evn already add before')
def full_single_install(ip,port):
msi = MySQL_Single_Install(ip=ip, port=port)
try:
my_ssh_client = MySshClient()
msi.check_mysqlbasedir_exists()
msi.check_mysql_user()
msi.check_datadir()
msi.dir_chown()
msi.conf_defined()
msi.install_mysql()
msi.start_mysql()
msi.check_installtempfile()
msi.reset_password()
res = my_ssh_client.ssh_interactive(ip, 'cat /tmp/mysqlinstalltemp.txt |grep ERROR|wc -l')
print(res)
count = 0
while True:
if int(res) > 0 and count <= 5:
logger.info('again repead rest password')
msi.reset_password()
res = my_ssh_client.ssh_interactive(ip, 'cat /tmp/mysqlinstalltemp.txt |grep ERROR|wc -l')
count += 1
elif count > 5 and int(res) > 0:
logger.error('mysql root password reset failed')
else:
# time.sleep(10)
my_ssh_client.ssh_interactive(ip, 'rm -rf /tmp/mysqlinstalltemp.txt')
logger.info('mysql root password reset success')
break
msi.create_extruser()
msi.install_plugin()
msi.set_osenv()
return '200'
except Exception as e:
print(e)
return '400'
if __name__=='__main__':
full_single_install('192.168.56.103','3307')