1.首先最终目录如下
其中main.py中的代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import os
import subprocess
import sys
import json
get_all_dist=eval(sys.argv[1])
all_class=get_all_dist['all_class']
file_code_type_name=get_all_dist['file_code_type_name']
file_code_type=get_all_dist['file_code_type']
file_code_type_yml=get_all_dist['file_code_type_yml']
file_code_type_two_layer=get_all_dist['file_code_type_two_layer']
file_code_type_split01=get_all_dist['file_code_type_split01']
file_code_type_split02=get_all_dist['file_code_type_split02']
file_code_type_split03=get_all_dist['file_code_type_split03']
file_code_type_split04=get_all_dist['file_code_type_split04']
ansible_file_site_name=get_all_dist['ansible_file_site_name']
ansible_file_hosts_name=get_all_dist['ansible_file_hosts_name']
deployment_config_path_name=get_all_dist['deployment_config_path_name']
deployment_base_path_name=get_all_dist['deployment_base_path_name']
ansible_file_group_path=get_all_dist['ansible_file_group_path']
ansible_file_group_all_path=get_all_dist['ansible_file_group_all_path']
site_type_name=get_all_dist['site_type_name']
all_key=get_all_dist['all_key']
ansible_cmd_layer01=get_all_dist['ansible_cmd_layer01']
ansible_cmd_layer02=get_all_dist['ansible_cmd_layer02']
ansible_cmd_layer03=get_all_dist['ansible_cmd_layer03']
from_name=get_all_dist['from_name']
import_star_name=get_all_dist['import_star_name']
import_name=get_all_dist['import_name']
ansible_cmd=ansible_cmd_layer01+ansible_file_site_name+ansible_cmd_layer02+ansible_file_hosts_name +ansible_cmd_layer03+all_key
xm_path=os.path.split(os.path.realpath(__file__))[0]
os.chdir(xm_path)
for import_all_class in os.listdir(all_class):
for import_all_class_two_layer in os.listdir(all_class+file_code_type_split02+import_all_class):
if import_all_class_two_layer.split(file_code_type_split01)[-1:][0] == file_code_type:
import_all_class_two_layer=import_all_class_two_layer.split(file_code_type_split01)[0]
if import_all_class_two_layer.rstrip(file_code_type_name).split(file_code_type_split04)[-1:][0] == file_code_type_two_layer:
exec(from_name+file_code_type_split03+all_class+file_code_type_split01+import_all_class+file_code_type_split01+import_all_class_two_layer+file_code_type_split03+import_star_name)
else:
exec(from_name+file_code_type_split03+all_class+file_code_type_split01+import_all_class+file_code_type_split03+import_name+file_code_type_split03+import_all_class_two_layer)
class main(object):
def __init__(self) -> None:
all_dict=fileinout.fileinout().cfg_to_json(deployment_config_path_name)
self.all_dict=all_dict
base_dict=fileinout.fileinout().cfg_to_json(deployment_base_path_name)
self.base_dict=base_dict
all_list=JsonToSite.JsonToSite().JsonToSite(self.all_dict)
self.all_list=all_list
def main(self):
importantinfo('-'*50+'welcome to qiqiao auto deployment'+'-'*50)
importantinfo('author:xiaoming')
importantinfo('环境检测中。。。。')
if self.all_dict['all']['debug_mode']=='prod':
check.check().check_conf(self.all_dict)
fileinout.fileinout().json_to_yaml(
self.all_list,
ansible_file_site_name
)
hosts.hosts(ansible_file_hosts_name).hosts_in_hosts(self.all_dict)
GroupVars.GroupVars().GroupVarsAll()
role_all_list=JsonToSite.JsonToSite().JsonToSite(
self.all_dict,
roles_all_host=True
)
Roles.Roles(self.all_dict).Roles(role_all_list)
init.init()._init()
SP = subprocess.Popen(
ansible_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True
)
for line in iter(SP.stdout.readline, ''):
ansibleinfo(line)
sys.stdout.flush()
SP.wait()
warning("以上unreachable以及failed为0表示安装成功!!\n","请手动调整时间同步NTP,确保时间的误差小于30s\n","finished!!!\n")
importantinfo('-'*50+'deployment is finished!!!!'+'-'*50)
def __del__(self) -> None:
SQLEinit.SQLEinit()._del()
if __name__ == "__main__":
main().main()
主流程代码如下:
common.py代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
class vars(object):
def __init__(self,*cf,**kwcf) -> None:
self.SQLEinit_db_name = main.get_all_dist['SQLEinit_db_name']
self.SSHClient_key = "etc/ssh/id_rsa"
self.SSHClientkey_pub = 'etc/ssh/id_rsa.pub'
self.SSHClientkey_keygen='echo -e "y\n"|ssh-keygen -t rsa -N "" -f '
self.SSHClientkey_dev_null=' >/dev/null'
self.SSHClientkey_ssh_connect="ssh-copy-id -i "
self.Roles_path_name='./xmansible/roles/'
self.Roles_j2='.j2'
self.Roles_templates_xm='code/file_templates/qiqiao_xmansible/'
self.Roles_yaml_xm='code/file_yaml/qiqiao_xmansible/'
self.Roles_templates_name='/templates/'
self.Roles_tasks_yml='/tasks/main.yml'
self.cfg_True='True'
self.cfg_isinstall='isinstall'
self.Roles_type_yml = main.get_all_dist['file_code_type_yml']
self.Roles_type_type_split01 = main.get_all_dist['file_code_type_split02']
self.Roles_file_name = ['tasks','templates','files']
self.GroupVars_list_split_01='-->xmlist<--'
self.GroupVars_xm_path='xm_path'
self.GroupVars_cfg_base='AYML'
self.cfg_hostname='hostname'
self.cfg_all='all'
self.cfg_ip='ip'
self.cfg_dnslanip='dnslanip'
self.cfg_dnswanip='dnswanip'
self.cfg_dns='dns'
self.cfg_dns_exists='dns_exists'
self.cfg_ssh_port='ssh_port'
self.cfg_ssh_root='ssh_root'
self.cfg_ssh_password='ssh_password'
self.cfg_init_all='init_all'
self.cfg_node_list='_node_list'
self.cfg_node_list_comma='_node_list_comma'
self.cfg_node_host_name_comma='_node_host_name_comma'
self.cfg_priority_id='priority_id'
self.cfg_site_name='site_name'
self.cfg_site_hosts='site_hosts'
self.cfg_site_role='site_role'
self.cfg_site_roles='site_roles'
self.cfg_debug_mode='debug_mode'
self.cfg_prod='prod'
self.file_chmod_w='w'
self.file_chmod_wt='wt'
self.file_char_type_utf='utf-8'
self.fileinout_test="test"
self.fileinout_test_key={'host':'wxqyh1'}
self.check_re_01='^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$'
self.check_license_cloudoffice_dir='license/verifyinfo'
self.check_license_qiqiao_dir='license/qiqiao_license.lic'
self.check_license_zt_dir='license/zhongtai_license.lic'
self.check_key_01='xiaoming'
self.SSHClient_expect_list=['(yes/no)','password:']
self.SSHClient_user_ip=' %s@%s'
class sql(object):
def __init__(self,*cf,**kwcf) -> None:
self.sql_SQLEinit_create_table_tags='''CREATE TABLE if not exists tags
(ID INT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
tags_type INT NOT NULL
);'''
self.sql_SQLEinit_insert_1_tags='''INSERT INTO tags (ID,name,tags_type) VALUES (1,'qiqiao',1);'''
self.sql_SQLEinit_insert_2_tags='''INSERT INTO tags (ID,name,tags_type) VALUES (2,'hosts',0);'''
self.sql_SQLEinit_select_1_tags='''SELECT * FROM tags;'''
self.sql_init_select_1_tags='''SELECT tags_type FROM tags where ID = 2 LIMIT 1;'''
self.sql_init_update_1_tags="""UPDATE tags SET tags_type=1 where ID = 2;"""
class shell_cmd(object):
def __init__(self,*cf,**kwcf) -> None:
self.init_yum_base="yum localinstall -y soft/centos75/rmp/base/*.rpm --skip-broken"
self.init_yum_ansible='yum localinstall -y soft/centos75/rmp/ansible/*.rpm --skip-broken'
self.init_count_grep_wc='rpm -qa|grep ansible|wc -l'
self.check_echo_01='echo -n xiaoming'
class info(object):
def __init__(self,*cf,**kwcf) -> None:
self.info_server_ok='服务测试正常'
self.info_server_name_01="服务器"
self.info_server_name_02="服务器:"
self.info_server_connect="--->server connect OK! "
self.info_yes='yes'
class error(object):
def __init__(self,*cf,**kwcf) -> None:
self.error_sql_01='sql语法错误'
self.error_host_name="主机名不对请检查!!"
self.error_priority_id="priority_id重复了"
self.error_cfg='亲爱的你的配置文件config.cfg写错了哦'
self.error_in_net_exists='内网IP不存在!!'
self.error_ip_list_check='IP列表重复请检查!!!'
self.error_hostname_same='主机名列表重复请检查!!!'
self.error_error='错误'
self.error_ssh_connect='ssh连接错误'
self.error_net_no='网络不通或者域名错误'
self.error_ip_dns_no='提供的域名与外网IP不能相互解析应请检查'
self.error_cloudoffice='license-verifyinfo不存在请检查'
self.error_qiqiao='license-qiqiao_license.lic不存在请检查'
self.error_zhongtai='license-zhongtai_license.lic不存在请检查'
self.error_port="端口不通请检查!"
self.error_xiaoming="亲爱滴!你的文件配置错误了!!\n代码报错由小明同学查看:"
self.error_01='错误了!!'
self.error_02='有问题请检查'
self.error_03='end_ssh_timeout'
class common(object):
def __init__(self,*cf,**kwcf) -> None:
self.split_name_01=']\n'
self.split_name_02='['
self.split_name_n='\n'
self.split_name_03=','
self.split_name_04=':'
self.split_name_05=' \n'
SQLEinit.py代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import sqlite3
from xmclass.common import common
class SQLEinit(common.vars,common.sql,common.error):
def __init__(self,*cf,**kwcf) -> None:
common.vars.__init__(self)
common.sql.__init__(self)
common.error.__init__(self)
self.sqlconnent = sqlite3.connect(self.SQLEinit_db_name)
def SQLEinit(self) -> None:
con = self.sqlconnent.cursor()
con.execute(
self.sql_SQLEinit_create_table_tags
)
try:
con.execute(
self.sql_SQLEinit_insert_tags
)
except Exception as e:...
try:
con.execute(
self.sql_SQLEinit_insert_2_tags
)
except Exception as e:...
cursor=con.execute(self.sql_SQLEinit_select_1_tags)
self.sqlconnent.commit()
def SQLESelect(self,sql_cmd:str) -> sqlite3.Cursor:
con = self.sqlconnent.cursor()
try:
cursor=con.execute(sql_cmd)
self.sqlconnent.commit()
return cursor
except Exception as e:
main.error(self.error_sql_01)
def _del(self) -> None:
self.sqlconnent.close()
GroupVars.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/22
import main
import copy
import os
from xmclass.common import common
class GroupVars(common.common,common.vars):
def __init__(self,*cf,**kwcf) -> None:
common.common.__init__(self)
common.vars.__init__(self)
self.data_json=copy.deepcopy(main.main().all_dict)
global dist_global_transform
dist_global_transform={}
self.list_split=self.GroupVars_list_split_01
def __del__(self) -> None:...
def GroupVarsAll(self,*cf,**kwcf)->None:
main.fileinout.fileinout().key_to_cfg(main.deployment_base_path_name,self.GroupVars_cfg_base,self.GroupVars_xm_path,main.xm_path)
main.fileinout.fileinout().json_to_yaml(main.main().base_dict[self.GroupVars_cfg_base],main.ansible_file_group_all_path)
def GroupVars(self,data_list_True,*cf,**kwcf) -> None:
for i in data_list_True:
dist_global_transform=copy.deepcopy(main.main().all_dict[i])
temp_dict={}
all_to_name=main.main().all_dict[i][self.cfg_hostname]
if all_to_name == self.cfg_all:
all_to_name=main.main().all_dict[self.cfg_all][self.cfg_hostname]
for j,k in dist_global_transform.items():
if k.isdigit():
temp_dict[j]=int(k)
elif self.list_split in k:
temp_dict[j]=[z for z in k.split(self.list_split)]
else:
temp_dict[j]=k
temp_dict[i+self.cfg_node_list]=self.split_name_03.join([
main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_ip])[main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_hostname]).index(i1)]
for i1 in main.comma_split(all_to_name)
])
temp_dict[i+self.cfg_node_list_comma]=[
main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_ip])[main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_hostname]).index(i1)]
for i1 in main.comma_split(all_to_name)
]
if main.main().all_dict[i][self.cfg_hostname]!=self.cfg_all:
temp_dict[i+self.cfg_node_host_name_comma]=[
i2 for i2 in main.comma_split(main.main().all_dict[i][self.cfg_hostname])
]
else:
temp_dict[i+self.cfg_node_host_name_comma]=[
i2 for i2 in main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_hostname])
]
main.fileinout.fileinout().json_to_yaml(temp_dict,main.ansible_file_group_path+i+main.file_code_type_yml)
main.fileinout.fileinout().json_to_yaml(main.main().all_dict[self.cfg_all],main.ansible_file_group_path+self.cfg_init_all+main.file_code_type_yml)
hosts.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import copy
from xmclass.common import common
class hosts(common.error,common.common):
def __init__(self,file_path,*cf,**kwcf) -> None:
common.error.__init__(self)
common.common.__init__(self)
common.vars.__init__(self)
self.f=open(file_path,self.file_chmod_w)
def __del__(self) -> None:
self.f.close()
def hosts_in_hosts(self,data_json,*cf,**kwcf)->None:
host_funcation=[self.cfg_all]
data_json_temp = copy.deepcopy(data_json)
del data_json_temp[self.cfg_all]
count=0
for i in data_json_temp:
if data_json_temp[i][self.cfg_hostname] in host_funcation or data_json_temp[i][self.cfg_hostname] in data_json[self.cfg_all][self.cfg_hostname]:
if data_json_temp[i][self.cfg_hostname] ==self.cfg_all:
hostname_var=data_json[self.cfg_all][self.cfg_hostname]
else:
hostname_var=data_json_temp[i][self.cfg_hostname]
else:
hostname_var=data_json_temp[i][self.cfg_hostname]
try:
ip_list=[
main.comma_split(data_json[self.cfg_all][self.cfg_ip])[main.comma_split(data_json[self.cfg_all][self.cfg_hostname]).index(var_ip)]
for var_ip in hostname_var.split(self.split_name_03)
]
except Exception as e:
main.error(self.error_host_name)
exit(1)
if count!=0:
self.f.write(self.split_name_n)
else:
count+=1
self.f.write(self.split_name_02+i+self.split_name_01)
for ip_to_one in ip_list:
self.f.write(ip_to_one+self.split_name_n)
Roles.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import os
import main
import copy
import shutil
from xmclass.common import common
class Roles(common.vars):
def __init__(self,data_json,*cf,**kwcf) -> None:
super().__init__()
self.data_json=copy.deepcopy(data_json)
def __del__(self) -> None:...
def Roles(self,data_list,*cf,**kwcf)->None:
data_list_True=[i for i in data_list if self.data_json[i][self.cfg_isinstall]==self.cfg_True]
for mkdir_var in data_list_True:
file_name=self.Roles_file_name
for file_name_var in file_name:
if not os.path.exists(self.Roles_path_name+mkdir_var+self.Roles_type_type_split01+file_name_var):
os.makedirs(self.Roles_path_name+mkdir_var+self.Roles_type_type_split01+file_name_var)
srcfile=self.Roles_yaml_xm+mkdir_var+self.Roles_type_yml
dstfile=self.Roles_path_name+mkdir_var+self.Roles_tasks_yml
shutil.copyfile(srcfile,dstfile)
for i in os.listdir(self.Roles_templates_xm+mkdir_var):
if self.Roles_j2 in i:
srcfile_templates=self.Roles_templates_xm+mkdir_var+self.Roles_type_type_split01+i
dstfile_templates=self.Roles_path_name+mkdir_var+self.Roles_templates_name+i
shutil.copyfile(srcfile_templates,dstfile_templates)
data_list_GroupVars=[i for i in data_list]
main.GroupVars.GroupVars().GroupVars(data_list_GroupVars)
JsonToSite.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import copy
from xmclass.common import common
class JsonToSite(object):
def __init__(self,*cf,**kwcf) -> None:
common.vars.__init__(self)
common.error.__init__(self)
def __del__(self) -> None:...
def JsonToSite(self,json_all,roles_all_host=False,*cf,**kwcf) -> list:
site_name_list=[]
for site_all in json_all:
for site_two in json_all[site_all]:
if main.site_type_name in site_two:
site_name_list.append(site_all)
site_name_list=list(set(site_name_list))
site_name_list.remove(self.cfg_all)
if roles_all_host:
return site_name_list
priority_id_list=[]
for i in site_name_list:
priority_id_list.append(int(json_all[i][self.cfg_priority_id]))
priority_id_list=main.bubbleSort(priority_id_list)
site_list_temp=[
{
json_all[self.cfg_all][self.cfg_priority_id]:json_all[site_var][self.cfg_priority_id],
json_all[self.cfg_all][self.cfg_isinstall]:json_all[site_var][self.cfg_isinstall],
json_all[self.cfg_all][self.cfg_site_name]:json_all[site_var][self.cfg_site_name],
json_all[self.cfg_all][self.cfg_site_hosts]:json_all[site_var][self.cfg_site_hosts],
json_all[self.cfg_all][self.cfg_site_roles]:[{json_all[self.cfg_all][self.cfg_site_role]:json_all[site_var][self.cfg_site_role]}]
}
for site_var in site_name_list
]
site_list=list()
for i in priority_id_list:
for j in site_list_temp:
if j[self.cfg_isinstall]==self.cfg_True:
if i == int(j[self.cfg_priority_id]):
site_list.append(j)
try:
for i in site_list:
del i[self.cfg_priority_id]
del i[self.cfg_isinstall]
except:
main.error(self.error_priority_id)
exit()
return site_list
check.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import main
import socket
import re
import os
from xmclass.common import common
class check(object):
def __init__(self,*cf,**kwcf) -> None:
common.vars.__init__(self)
common.error.__init__(self)
common.info.__init__(self)
common.shell_cmd.__init__(self)
common.common.__init__(self)
def __del__(self) -> None:...
def isIP(self,string_ip:str) -> bool:
p = re.compile(self.check_re_01)
if p.match(string_ip):
return True
else:
return False
def check_conf(self,json_all,*cf,**kwcf)->None:
if len(main.comma_split(json_all[self.cfg_all][self.cfg_ip])) != len(main.comma_split(json_all[self.cfg_all][self.cfg_hostname])):
main.error(self.error_cfg)
exit()
json_all[self.cfg_all][self.cfg_ip]
if json_all[self.cfg_all][self.cfg_dnslanip] not in main.comma_split(json_all[self.cfg_all][self.cfg_ip]):
main.error(self.error_in_net_exists)
exit()
for i in main.comma_split(json_all[self.cfg_all][self.cfg_ip]):
c=0
for j in main.comma_split(json_all[self.cfg_all][self.cfg_ip]):
if i==j:
c+=1
if c>1:
main.error(self.error_ip_list_check)
exit()
for i in main.comma_split(json_all[self.cfg_all][self.cfg_hostname]):
c=0
for j in main.comma_split(json_all[self.cfg_all][self.cfg_hostname]):
if i==j:
c+=1
if c>1:
main.error(self.error_hostname_same)
exit()
for i in main.comma_split(json_all[self.cfg_all][self.cfg_ip]):
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.settimeout(3)
try:
sk.connect((i,int(json_all[self.cfg_all][self.cfg_ssh_port])))
main.info(self.info_server_name_02+i+self.info_server_connect)
except Exception:
main.error(self.info_server_name_01+i+self.split_name_04+str(int(json_all[self.cfg_all][self.cfg_ssh_port]))+self.error_port)
sk.close()
exit()
sk.close()
if json_all[self.cfg_all][self.cfg_dns_exists]:
try:
if socket.gethostbyname(json_all[self.cfg_all][self.cfg_dns]) != json_all[self.cfg_all][self.cfg_dnswanip]:
main.error(self.error_ip_dns_no)
exit()
except Exception as e:
main.error(self.error_net_no)
exit()
for i in main.comma_split(json_all[self.cfg_all][self.cfg_ip]):
try:
if main.SSHClient.SSHClient().ssh_cmd(hostname=i, port=json_all[self.cfg_all][self.cfg_ssh_port], username=json_all[self.cfg_all][self.cfg_ssh_root], password=json_all[self.cfg_all][self.cfg_ssh_password],cmd=self.check_echo_01)!=self.check_key_01:
main.error(self.error_error)
exit()
else:
main.info(self.info_server_ok)
except Exception as e:
main.error(self.error_ssh_connect)
main.info(e)
exit()
if not os.path.exists(self.check_license_cloudoffice_dir):
main.error(self.error_cloudoffice)
exit()
if not os.path.exists(self.check_license_qiqiao_dir):
main.error(self.error_qiqiao)
exit()
if not os.path.exists(self.check_license_zt_dir):
main.error(self.error_zhongtai)
exit()
fileinout.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import yaml
import json
from collections import OrderedDict
import configparser
from xmclass.common import common
class fileinout(object):
def __init__(self,*cf,**kwcf) -> None:
common.vars.__init__(self)
def __del__(self) -> None:...
def site(self,data,*cf,**kwcf):
config = OrderedDict()
config[self.fileinout_test] = self.fileinout_test_key
return config
def yaml_ordered_dump(self,data, stream=None, Dumper=yaml.Dumper, **kwargs):
class OrderedDumper(Dumper):...
def _dict_representer(dumper, data):
return dumper.represent_mapping(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,data.items())
OrderedDumper.add_representer(OrderedDict, _dict_representer)
return yaml.dump(data, stream, OrderedDumper, **kwargs)
def export_config_yml_in_order(self,file_path,*cf,**kwcf)->None:
config = self.site()
# main.info(config)
with open(file_path, self.file_chmod_wt) as f:
self.yaml_ordered_dump(config, f, default_flow_style=False)
def json_to_yaml(self,data,file_path,*cf,**kwcf):
stra = json.dumps(data)
dyaml = yaml.load(stra, Loader=yaml.FullLoader)
stream = open(file_path, self.file_chmod_w)
yaml.safe_dump(
dyaml,
stream,
default_flow_style=False,
allow_unicode=True,
indent=2,
sort_keys=False
)
def yaml_to_json(self,file_path,*cf,**kwcf) -> json:
with open(file_path, encoding=self.file_char_type_utf) as file:
data = yaml.load(
file,
Loader=yaml.FullLoader
)
return(data)
def key_to_cfg(self,file_path,options,key,value,*cf,**kwcf) -> None:
old_json=self.cfg_to_json(file_path)
conf = configparser.ConfigParser()
cfgfile = open(file_path,self.file_chmod_w)
for section in old_json:
conf.add_section(section)
for section_var,option_json in old_json.items():
for option_json_key,option_json_value in option_json.items():
conf.set(section_var, option_json_key, option_json_value)
conf.set(options, key, value)
conf.write(cfgfile)
cfgfile.close()
def cfg_to_json(self,file_path,*cf,**kwcf) -> dict:
cfg_dict=dict()
config = configparser.ConfigParser()
config.read(file_path)
try:
for section in config.sections():
cfg_dict[section] = {
option:config.get(section, option)
for option in config.options(section)
}
except Exception as e:
main.error(self.error_xiaoming,e)
return cfg_dict
init.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import os
import copy
from xmclass.common import common
class init(common.sql,common.shell_cmd):
def __init__(self,*cf,**kwcf) -> None:
common.sql.__init__(self)
common.shell_cmd.__init__(self)
common.vars.__init__(self)
common.common.__init__(self)
self.all_dict=copy.deepcopy(main.main().all_dict)
self.ip_map_hostname={}
def __getitem__(self, item):
return self.ip_map_hostname[item]
def __setitem__(self, key, value):
self.ip_map_hostname[key]=value
def _init(self) -> None:
for i in main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_ip]):
self.ip_map_hostname[i]=main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_hostname])[main.comma_split(main.main().all_dict[self.cfg_all][self.cfg_ip]).index(i)]
main.SQLEinit.SQLEinit().SQLEinit()
if main.main().all_dict[self.cfg_all][self.cfg_debug_mode]==self.cfg_prod:
main.SSHClient.SSHClient().SSHPClient(self.all_dict).private_key()
SCursor=main.SQLEinit.SQLEinit().SQLESelect(self.sql_init_select_1_tags)
for c_var in SCursor:
hosts=c_var[0]
if hosts==0:
if main.main().all_dict[self.cfg_all][self.cfg_debug_mode]==self.cfg_prod:
main.SQLEinit.SQLEinit().SQLESelect(
self.sql_init_update_1_tags
)
os.system(self.init_yum_base)
if int(os.popen(self.init_count_grep_wc).readlines()[0].strip(self.split_name_05))==0:
os.system(self.init_yum_ansible)
def __del__(self) -> None:...
tools_star.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
class Color(object):
def __init__(self,content,)->None:
self.content = content
def classdebug(self)->None:
print('\033[0;35;999m'+self.content+'\033[0m')
def classinfo(self)->None:
print('\033[0;34;999m'+self.content+'\033[0m')
def classwarning(self)->None:
print('\033[0;33;999m'+self.content+'\033[0m')
def classerror(self)->None:
print('\033[1;31;999m\033[4;31;999m'+self.content+'\033[0m\033[0m')
def decorator_comand_parameter(*dcp,**kwdcp):
def decorator_funcation(funcation):
def call_funcation(self,*cf,**kwcf)->None:
# globals()['__builtins__'].__dict__['print'](dcp,kwdcp,cf,kwcf,'---- 扩展功能 ----')
return funcation(self,*cf,**kwcf)
return call_funcation
return decorator_funcation
def debug(content,*argsdebug,**kwdebug)->None:
print('\033[0;35;999m'+str(content)+'\033[0m',end=' ')
if argsdebug:
for i in argsdebug:
print('\033[0;35;999m'+str(i)+'\033[0m',end=' ')
print('')
def info(content,*argsinfo,**kwinfo)->None:
print('\033[0;34;999m'+str(content)+'\033[0m',end=' ')
if argsinfo:
for i in argsinfo:
print('\033[0;34;999m'+str(i)+'\033[0m',end=' ')
print('')
def importantinfo(content,*argsimportantinfo,**kwimportantinfo)->None:
print('\033[0;36;999m'+str(content)+'\033[0m',end=' ')
if argsimportantinfo:
for i in argsimportantinfo:
print('\033[0;36;999m'+str(i)+'\033[0m',end=' ')
print('')
def ansibleinfo(content,*argsansibleinfo,**kwansibleinfo)->None:
print('\033[0;35;999m'+str(content)+'\033[0m',end=' ')
if argsansibleinfo:
for i in argsansibleinfo:
print('\033[0;35;999m'+str(i)+'\033[0m',end=' ')
print('')
def warning(content,*argswarning,**kwwarning)->None:
print('\033[0;33;999m'+str(content)+'\033[0m',end=' ')
if argswarning:
for i in argswarning:
print('\033[0;33;999m'+str(i)+'\033[0m',end=' ')
print('')
def error(content,*argserror,**kwerror)->None:
print('\033[1;31;999m\033[4;31;999m'+str(content)+'\033[0m\033[0m',end=' ')
if argserror:
for i in argserror:
print('\033[0;31;999m'+str(i)+'\033[0m',end=' ')
print('')
def comma_split(string_split:str)->list:
return string_split.split(',')
def bubbleSort(a:list) -> list:
"""冒泡算法"""
n = len(a)
for i in range(n):
for j in range(0, n-i-1):
if a[j] > a[j+1] :
a[j], a[j+1] = a[j+1], a[j]
return a
SSHClient.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author chentongming
# @date 2021/11/11
import main
import paramiko
import os
import pexpect
from xmclass.common import common
class SSHClient(common.vars):
def __init__(self,*cf,**kwcf) -> None:
super().__init__()
self.client = paramiko.SSHClient()
class SSHPClient(common.vars):
def __init__(self, all_dict):
super().__init__()
common.error.__init__(self)
common.info.__init__(self)
self.all_dict=all_dict
self.host_list=main.comma_split(self.all_dict[self.cfg_all][self.cfg_ip])
self.port_string = self.all_dict[self.cfg_all][self.cfg_ssh_port]
self.user_string = self.all_dict[self.cfg_all][self.cfg_ssh_root]
self.password = self.all_dict[self.cfg_all][self.cfg_ssh_password]
self.key=self.SSHClient_key
self.key_pub=self.SSHClientkey_pub
self.keu_cmd=self.SSHClientkey_keygen+self.key+self.SSHClientkey_dev_null
self.ssh_connect=self.SSHClientkey_ssh_connect
self.count=0
self.temp_count=0
def private_key_distribute(self,*cf,**kwcf) -> None:
for ip_var in self.host_list:
expect_list = self.SSHClient_expect_list
#此处有bug(如果客户没有了ssh-copy-id需要获取一个)
spawn_cmd=self.ssh_connect+self.key_pub + self.SSHClient_user_ip % (self.user_string,ip_var)
pe = pexpect.spawn(spawn_cmd)
try:
while True:
idindex = pe.expect(expect_list)
if idindex == 0:
pe.sendline(self.info_yes)
elif idindex == 1:
pe.sendline(self.password)
except pexpect.TIMEOUT:
main.error(self.error_03)
exit()
except pexpect.EOF:
pass
# main.info('end_ssh_key')
except Exception as e:
main.error(self.error_02)
exit()
def private_key(self,*cf,**kwcf) -> None:
try:
if self.temp_count==0:
if not os.path.exists(self.key):
os.system(self.keu_cmd)
else:
os.remove(self.key)
os.remove(self.key_pub)
os.system(self.keu_cmd)
self.temp_count+=1
self.private_key_distribute()
for i in self.host_list:
private=paramiko.RSAKey.from_private_key_file(self.key)
ts = paramiko.Transport((i, int(self.port_string)))
ts.connect(username=self.user_string, pkey=private)
ts.close()
except Exception as e:
self.private_key_distribute()
if self.count>=2:
main.error(self.error_01)
exit()
else:
self.count+=1
self.private_key()
def __del__(self)->None:...
def ssh_cmd(self,hostname,port,username,password,cmd,*cf,**kwcf) -> str:
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname=hostname, port=port, username=username, password=password,timeout=10)
stdin, stdout, stderr = self.client.exec_command(cmd)
return stdout.read().decode()
def __del__(self) -> None:
self.client.close()