def repackage_war(self, host_id=None, module=None, path=None): storage = Config.get_config('env', 'storage')['value'] if storage[-1:] != '/': storage += '/' path_list = self.server.server_path[int(host_id)] try: m = Prod_Module.objects.get(id=int(module)) except Exception as e: raise DeployError('042') return False dep_src_path = storage + self.prod + '/' + self.prod + '-' + self.branch + '/' + self.prod + '-' + self.version + '/' + m.name + '/' dep_src = dep_src_path + path['src_name'] des_src_folder = path['src_name'][:path['src_name'].rfind('.war')] pak_path = self.java_package('extract', pak_path=dep_src, work_folder=self.temp + des_src_folder) if pak_path: self.deploy_conf_local(host_id, module, self.temp + des_src_folder) if not os.path.isdir(self.temp + 'war_files/'): os.makedirs(self.temp + 'war_files/') pak_path = self.java_package('create', pak_path=self.temp + 'war_files/' + path['src_name'], work_folder=self.temp + des_src_folder) UTILS.close_db() return pak_path
def new_repackage_war(self, host_id=None, module=None, path=None, src_war=None): storage = Config.get_config('env', 'storage')['value'] if storage[-1:] != '/': storage += '/' try: m = Prod_Module.objects.get(id=int(module)) p = Dep_Path.objects.get(host_id=host_id, module=path) confs = p.dpaths.all() except Exception as e: raise DeployError('042') return False if confs.__len__() == 0: dep_src = storage + self.prod + '/' + self.prod + '-' + self.branch + '/' + self.prod + '-' + self.version + '/' + m.name + '/' + src_war return dep_src dep_src = storage + self.prod + '/' + self.prod + '-' + self.branch + '/' + self.prod + '-' + self.version + '/' + m.name + '/' + src_war des_src_folder = src_war[:src_war.rfind('.war')] pak_path = self.java_package('extract', pak_path=dep_src, work_folder=self.temp + des_src_folder) if pak_path: self.new_deploy_conf_local(confs, module, self.temp + des_src_folder) if not os.path.isdir(self.temp + 'war_files/'): os.makedirs(self.temp + 'war_files/') pak_path = self.java_package('create', pak_path=self.temp + 'war_files/' + src_war, work_folder=self.temp + des_src_folder) UTILS.close_db() return pak_path
def deploy_conf(self, host_id=None, module=None, path=None): if host_id is None: raise DeployError('001') return False if module is None: raise DeployError('034') return False if path is None: raise DeployError('034') return False try: m = Prod_Module.objects.get(id=module) except Exception as e: raise DeployError('042') return False if self.pid is None or self.prod is None or self.vid is None or self.version is None: raise DeployError('036') return False conf_files = self.get_conf_list(module) if conf_files is None: LOG('deploy').warning('未找到 ' + m.name + ' 对应配置文件', self.dep_status_id) return False storage = Config.get_config('configuration', 'storage')['value'] if storage[-1:] != '/': storage += '/' if self.temp is not None and os.path.isdir(self.temp): set = SETTINGS(m.name, self.server, host_id) for conf in conf_files: conf_name = conf.filename prod = conf.prod.name common = storage + prod + '/' + m.name + '/' + conf_name + '__0' host = storage + prod + '/' + m.name + '/' + conf_name + '__' + str( self.server.id) combined = self.temp + conf_name if set.get_combined_file(common=common, host=host, combined=combined): if int(host_id) in list(self.server.server_path.keys()): path_list = self.server.server_path[int(host_id)] else: raise DeployError('001') return False des_file = path if des_file[-1:] != '/': des_file += '/' des_file += conf_name try: self.File_transfer(host_id, 'push', des_file, combined) except DeployError as e: if self.dep_status_id is not None: LOG('deploy').error(e.get_msg(), self.dep_status_id) return False else: return False return True
def generate_nav_tabs(self, activate=None): Config.load_config_type('ui_nav') navigation = Config()['ui_nav'] nav_index = list(navigation.keys()) nav_index.sort() tab_list = [] title = '' if len(nav_index) > 0: title = navigation[nav_index[0]]['value'] for index in nav_index: name = navigation[index]['value'] code_name = navigation[index]['configuration'] if activate == code_name: row = '<li class=\"active\"><a herf=\"#\">' + name + '</a></li>' title = navigation[index]['value'] else: row = '<li><a href=\"?m=' + code_name + '\">' + name + '</a></li>' tab_list.append(row) return (title, tab_list)
def deploy_start(self): temp = Config.get_config('env', 'temp')['value'] if temp[-1:] != '/': temp += '/' temp += 't' + str(self.dep_status_id) + '/' if self.temp is None: os.makedirs(temp) self.temp = temp LOG('deploy').info('开始部署', self.dep_status_id) return True
def apply_reset_pass(request): if request is None: return (False, json.JSONEncoder().encode({ 'status': 'FAILED', 'msg': 'HTTP request失效' })) if 'u' not in request.GET: return (False, json.JSONEncoder().encode({ 'status': 'FAILED', 'msg': '信息不全' })) username = request.GET.get('u') try: user = Auth.objects.get(username=username) except ObjectDoesNotExist: return (False, json.JSONEncoder().encode({ 'status': 'FAILED', 'msg': '无法找到指定用户' })) try: now = datetime.datetime.now() user.secret = now user.save() print((user.password + str(user.secret))[:-7]) status, reset_string = LOGIN.hash_password( user.id, user.password + str(user.secret)[:-7]) bsp_url = Config.get_config('env', 'bsp_url')['value'] url = 'http://' + bsp_url + '/user/?a=snp&u=' + user.username + '&s=' + reset_string print(url) subject = '自动化平台密码重置' context = {'url': url} cc = [] EMAIL.mail(subject=subject, mode='reset', html_context=context, to_mail=[user.email], cc=cc, text_content='使用该地址找回密码: ' + url) except Exception as e: return (False, json.JSONEncoder().encode({ 'status': 'FAILED', 'msg': '申请重置密码发生异常: ' + str(str(e) + '\n' + traceback.format_exc()) })) return (True, json.JSONEncoder().encode({ 'status': 'OK', 'msg': '重置密码申请成功,请查收邮件' }))
def db_flyway(self, module=None, mode=None): storage = Config.get_config(type='env', name='storage')['value'] if storage[-1:] != '/': storage += '/' #storage = /data/nfs/ try: m = Prod_Module.objects.get(id=int(module)) except Exception as e: raise DeployError('042') return False product = Product.objects.filter(id=self.pid) db = Dep_Sql.objects.get(prod=product) jdbc = 'jdbc:mysql://' + db.db_host + ':' + db.db_port + '/' + db.db_name db_path = 'filesystem:' + storage + self.prod + '/' + self.prod + '-' + self.branch + '/' + self.prod + '-' + self.version + '/' + m.name + '/db' shell = [ 'sudo', conf.FLYWAYPATH + 'flyway', 'migrate', '-url=' + jdbc, '-user='******'-password='******'-locations=' + db_path, '-baselineOnMigrate=true' ] output, err = Popen(shell, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() validate = [ 'sudo', conf.VFLYWAYPATH + 'flyway', 'validate', '-url=' + jdbc, '-user='******'-password='******'-locations=' + db_path ] t = threading.Thread(target=self.db_flyway_validate, args=(validate, )) t.start() if len(err) > 0: LOG('deploy').error('执行SQL出错:' + str(err.decode('utf-8')), self.dep_status_id) shell = [ 'sudo', conf.FLYWAYPATH + 'flyway', 'repair', '-url=' + jdbc, '-user='******'-password='******'-locations=' + db_path ] output2, err2 = Popen(shell, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate() if len(err2) > 0: LOG('deploy').error('执行恢复出错:' + str(err2.decode('utf-8')), self.dep_status_id) else: LOG('deploy').info('执行恢复成功:' + str(output2.decode('utf-8')), self.dep_status_id) return False else: LOG('deploy').info('执行SQL成功: ' + str(output.decode('utf-8')), self.dep_status_id) return True
def deploy_conf_local(self, host_id=None, module=None, des=None): storage = Config.get_config('configuration', 'storage')['value'] if storage[-1:] != '/': storage += '/' try: m = Prod_Module.objects.get(id=module) except Exception as e: raise DeployError('042') return False conf_files = self.get_conf_list(module) if des[-1:] != '/': des += '/' if self.temp is not None and os.path.isdir(self.temp): set = SETTINGS(m.name, self.server, host_id) for conf in conf_files: conf_name = conf.filename prod = conf.prod.name common = storage + prod + '/' + m.name + '/' + conf_name + '__0' host = storage + prod + '/' + m.name + '/' + conf_name + '__' + str( self.server.id) combined = self.temp + conf_name if not os.path.isfile(common): raise DeployError('033') return False des_path = conf.path if des_path[0] != '/': des_path = des + des_path if des_path[-1] != '/': des_path += '/' if not os.path.isfile(host): LOG('deploy').warning('未找到主机专属配置文件,使用通用配置', self.dep_status_id) shutil.copy(common, des_path + conf_name) else: if set.get_combined_file(common=common, host=host, combined=combined): shutil.copy(combined, des_path) return True else: return False
def new_deploy_conf_local(self, confs=None, module=None, des=None): storage = Config.get_config('configuration', 'storage')['value'] if storage[-1:] != '/': storage += '/' try: m = Prod_Module.objects.get(id=module) except Exception as e: raise DeployError('042') return False if des[-1:] != '/': des += '/' if self.temp is not None and os.path.isdir(self.temp): for con in confs: con_src = storage + m.prod.name + '/' + m.name + '/' + str( con.dpath_id) + '/' + con.pconf.filename des_path = des + con.path if des_path[-1] != '/': des_path += '/' if os.path.isfile(con_src): shutil.copy(con_src, des_path) return True else: return False
def create_db(request): if request is None: return (False, json.JSONEncoder().encode({'status': 'FAILED', 'msg': 'HTTP request失效'})) uid = conf.USER_ANONYMOUS if 'login' in request.session.keys() and request.session['login']: if 'uid' in request.session.keys(): uid = request.session['uid'] else: request.session['login'] = False request.session['uid'] = conf.USER_ANONYMOUS request.session['user'] = conf.USER_ANONYMOUS_NAME try: auth = Authentication(uid) except: uid = conf.USER_ANONYMOUS Config.load_config_type('dbc_cmd') Config.load_config_type('ui_dbc') cmds = Config()['dbc_cmd'] cmd_index = list(cmds.keys()) cmd_index.sort() dbc = Config()['ui_dbc'] db_user = dbc['db_user']['value'] db_pass = dbc['db_user']['configuration'] r = request.POST.get('request') j = json.loads(r) dbs = j['dbs'] db_name = j['db_name'] sqls = [] for index in cmd_index: cmd = cmds[index]['value'].replace("${db_name}", db_name) cmd = cmd.replace("${db_user}", db_user) cmd = cmd.replace("${db_pass}", db_pass) sqls.append(cmd) databases = DBC.objects.filter(id__in=dbs) success_db = "" for db in databases: db_info = { 'db_type': db.type, 'db_host': db.db_host, 'db_port': db.db_port, 'db_user': db.db_user, 'db_pass': db.db_pass, 'db_name': None, 'ssh_port': db.ssh_port, 'ssh_user': db.ssh_user, 'ssh_pass': db.ssh_pass, 'ssh_key': db.ssh_key, } for sql in sqls: try: result = SQL.run_sql_cmd(db_info, sql) except SQLError as e: LOG('execdb', uid=uid).error(e.get_msg()) return (False, json.JSONEncoder().encode({'status': 'FAILED', 'msg': '数据库创建失败:' + e.get_msg()})) except Exception as e: return (False, json.JSONEncoder().encode({'status': 'FAILED', 'msg': '数据库创建失败:' + e.args[0]})) if not result: return (False, json.JSONEncoder().encode({'status': 'FAILED', 'msg': '数据库创建失败:run_sql_cmd'})) elif result is None: return (False, json.JSONEncoder().encode({'status': 'FAILED', 'msg': '数据库创建失败:不支持所选数据库类型'})) else: if len(result) > 3: print('Create DB SQL other result:') print(result) success_db += '[' + db.db_host + '] ' LOG('execdb', uid=uid).info('数据库' + db_name + '@' + db.db_host + '创建成功') return (True, json.JSONEncoder().encode({'status': 'OK', 'msg': '数据库 ' + db_name + '@' + success_db + '创建成功'}))
def TomCod(self, host_id=None, module=None, port='9999', src_war='', action='push'): if host_id is None: raise DeployError('001') return False if module is None: raise DeployError('034') return False if port is None: raise DeployError('034') return False if self.pid is None or self.prod is None or self.vid is None or self.version is None: raise DeployError('036') return False storage = Config.get_config(type='env', name='storage')['value'] if storage[-1:] != '/': storage += '/' if int(host_id) in list(self.server.server_host.keys()): host = self.server.server_host[int(host_id)] else: raise DeployError('001') return False try: m = Prod_Module.objects.get(id=int(module)) except Exception as e: raise DeployError('042') return False ssh = SSHHelper(hostname=host['host'], port=int(host['port']), username=host['user'], password=host['pass'], key_file=host['pkey']) #检查远程服务器上tomcat安装包 try: local_md5 = self.__local_md5(conf.local_dir + 'tomcat.tar.gz') remote_md5 = self.__remote_md5( host_id, conf.remote_dir + 'source/tomcat.tar.gz') if local_md5 != remote_md5: shell = 'if [ ! -d \"' + conf.remote_dir + 'source' + '\" ]; then sudo mkdir -p ' + conf.remote_dir + 'source' + '; fi; sudo chown ' + \ host['user'] + ':' + host['user'] + ' ' + conf.remote_dir + 'source' + ';' ssh.run_command(shell) scp = self.File_transfer( host_id, 'push', conf.remote_dir + 'source/tomcat.tar.gz', conf.local_dir + 'tomcat.tar.gz') if scp: cmd = 'if [ ! -d \"' + conf.remote_dir + 'source/tomcat.tar.gz' + '\" ]; then cd ' + conf.remote_dir + 'source; tar zxf tomcat.tar.gz; fi' outputs, errs = ssh.run_command(cmd) if len(errs) == 0: LOG('deploy').info('校验Tomcat|完成' + str(host['host']), self.dep_status_id) else: print(errs) LOG('deploy').info('校验Tomcat|失败' + str(host['host']), self.dep_status_id) else: LOG('deploy').info('校验Tomcat|完成' + str(host['host']), self.dep_status_id) except Exception as e: print(e) LOG('deploy').error('校验Tomcat失败,请检查|' + str(host['host']), self.dep_status_id) return False #创建指定端口号的tomcat try: tname = '/data/applications/tomcat' + str(port) tsource = '/data/applications/source' create = 'if [ -d \"' + tname + '\" ]; then sudo kill -9 `ps -ef | grep ' + tname + ' | grep -v grep | awk \'{print $2}\'`;' + \ 'sudo mv ' + tname + '/webapps ' + tsource + ' && sudo mv ' + tname + '/logs ' + tsource + ' && sudo rm -rf ' + tname + '; fi;' + \ 'sudo cp -rf /data/applications/source/tomcat ' + tname + ';' + \ 'if [ -d "/data/applications/source/logs" ]; then sudo mv /data/applications/source/logs ' + tname + ' && sudo mv /data/applications/source/webapps ' + tname + '; fi;' + \ 'sudo rm -rf ' + tname + '/webapps/' + src_war[0:-4] + '*; sudo chown -R ' + host['user'] + ':' + \ host['user'] + ' ' + tname + ' && ' + \ 'sed -i \'s/8654/' + str(int(port) - 1357) + '/\' ' + tname + '/conf/server.xml;' + \ 'sed -i \'s/8800/' + str(port) + '/\' ' + tname + '/conf/server.xml;' + \ 'sed -i \'s/8429/' + str(int(port) - 2357) + '/\' ' + tname + '/conf/server.xml' outputs, errs = ssh.run_command(create) if len(errs) == 0: LOG('deploy').info( '创建Tomcat|成功' + str(host['host']) + '|tomcat' + str(port), self.dep_status_id) else: LOG('deploy').error( '创建Tomcat|失败' + str(host['host']) + '|tomcat' + str(port) + str(errs), self.dep_status_id) temp = 'ps -ef | grep ' + tname + ' | grep -v grep | awk \'{print $2}\'' outputs, errs = ssh.run_command(temp) LOG('deploy').info( '检查进程' + str(host['host']) + '|tomcat' + str(port) + '|' + str(outputs) + str(errs), self.dep_status_id) except Exception as e: LOG('deploy').error( '创建Tomcat|发生异常' + str(host['host']) + '|tomcat' + str(port) + str(e), self.dep_status_id) return False #部署代码启动tomcat try: if self.dep_status_id is not None: LOG('deploy').info( '部署代码|' + str(host['host']) + '|tomcat' + str(port), self.dep_status_id) dep_src = self.new_repackage_war(host_id, module, port, src_war) if os.path.isfile(dep_src): scp = self.File_transfer( host_id, 'push', conf.remote_dir + 'tomcat' + str(port) + '/webapps/' + src_war, dep_src) if scp: LOG('deploy').info( '部署代码|传输完成' + str(host['host']) + '|tomcat' + str(port), self.dep_status_id) start = 'cd ' + conf.remote_dir + 'tomcat' + str( port) + '/bin; sudo ./startup.sh' outputs, errs = ssh.run_command(start) if len(errs) == 0: LOG('deploy').info( '启动Tomcat|成功' + str(host['host']) + '|tomcat' + str(port), self.dep_status_id) else: LOG('deploy').error( '启动Tomcat|失败' + str(host['host']) + '|tomcat' + str(port), self.dep_status_id) else: LOG('deploy').error('部署代码|传输失败', self.dep_status_id) else: LOG('deploy').error('部署代码|失败,请检查部署包是否存在', self.dep_status_id) except Exception as e: LOG('deploy').error( '部署代码|失败' + str(host['host']) + '|tomcat' + str(port) + str(e), self.dep_status_id) return False
def deploy_code(self, host_id=None, module=None, path=None, deploy_local=None): if host_id is None: raise DeployError('001') return False if module is None: raise DeployError('034') return False if path is None: raise DeployError('034') return False if self.pid is None or self.prod is None or self.vid is None or self.version is None: raise DeployError('036') return False storage = Config.get_config(type='env', name='storage')['value'] if storage[-1:] != '/': storage += '/' if int(host_id) in list(self.server.server_host.keys()): host = self.server.server_host[int(host_id)] else: raise DeployError('001') return False try: m = Prod_Module.objects.get(id=int(module)) except Exception as e: raise DeployError('042') return False if self.dep_status_id is not None: LOG('deploy').info('部署代码' + path['des_name'], self.dep_status_id) dep_src_path = storage + self.prod + '/' + self.prod + '-' + self.branch + '/' + self.prod + '-' + self.version + '/' + m.name + '/' dep_src = dep_src_path + path['src_name'] if os.path.isfile(dep_src): if deploy_local == 'Y': src = self.repackage_war(host_id, module, path) else: src = dep_src ssh = SSHHelper(hostname=host['host'], port=int(host['port']), username=host['user'], password=host['pass'], key_file=host['pkey']) dep_des = path['des_path'] if dep_des[-1:] != '/': dep_des += '/' stage_folder = dep_des + 'staging/' stage_file = stage_folder + path['des_name'] dep_des += 'webapps/' + path['des_name'] if path['work_mod'] == 'WAR': rm_path = dep_des[:-4] cmd = 'if [ ! -d \"' + stage_folder + '\" ]; then sudo mkdir -p ' + stage_folder + '; fi; sudo chown ' + \ host['user'] + ':' + host['user'] + ' ' + stage_folder + ';' ssh.run_command(cmd) try: self.File_transfer(host_id, 'push', stage_file, src) except (DeployError, SQLError) as e: if self.dep_status_id is not None: LOG('deploy').error(e.get_msg(), self.dep_status_id) return False else: ssh.run_command('sudo rm -r ' + rm_path + '; sudo rm ' + dep_des + '; sudo mv ' + stage_file + ' ' + dep_des + ';') elif path['work_mod'] == 'PHP': pass else: #raise DeployError('031') print(dep_src + ' not found.') #return True return True
def get_private_key(self): rsa_keyfile = Config.get_config(type='env', name='rsa_keyfile') if rsa_keyfile is None: return None else: return rsa_keyfile['configuration']
def get_public_key(self): rsa_keyfile = Config.get_config(type='env', name='rsa_keyfile') if rsa_keyfile is None: return None else: public_key_file = rsa_keyfile['value']
def get_conf(self, type): Config.load_config_type(type) return Config()[type]
def __init__(self, request, type=None): self.request = request if type is not None: self.type = type Config.load_config_type('ui_' + type) self.conf = Config()['ui_' + type]