Beispiel #1
0
 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
Beispiel #2
0
 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
Beispiel #3
0
 def dep_finish(self, test_url):
     #if self.temp != None:
     #	if os.path.exists(self.temp):
     #		shutil.rmtree(self.temp)
     if test_url is None or len(test_url) < 1:
         if self.dep_status_id is not None:
             LOG('deploy').info('部署成功', self.dep_status_id)
         return True
     if self.dep_status_id is not None:
         LOG('deploy').info('测试服务', self.dep_status_id)
     if UTILS.check_service(test_url, self.dep_status_id):
         if self.dep_status_id is not None:
             LOG('deploy').info('部署成功', self.dep_status_id)
         return True
     else:
         if self.dep_status_id is not None:
             LOG('deploy').info('部署失败, 服务未启动', self.dep_status_id)
         return False
Beispiel #4
0
 def __init__(self, s_id=None, s_name=None):
     if s_id is None and s_name is None:
         return False
     s = SERVER(s_id=s_id, s_name=s_name)
     if s.is_ready():
         self.server = s
     else:
         LOG('deploy').error('Cannot create deploy instance for [' +
                             str(s_id) + ': ' + s_name + '].')
Beispiel #5
0
 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
Beispiel #6
0
 def incre_db_update(self, database=None, work_folder=None):
     if database is None or len(database) == 0:
         raise DeployError('028')
         return False
     if work_folder is None or len(database) == 0:
         raise DeployError('029')
         return False
     if self.dep_status_id is not None:
         LOG('deploy').info('开始进行数据库增量升级', self.dep_status_id)
     if database in self.server.server_db:
         db_info = self.server.get_db_info(database)
     else:
         raise DeployError('030', database)
         return False
     sql_list = SQL.get_sql_files(work_folder)
     done_index = SQL.sql_done_index(SQL.get_db_version(db_info))
     sql_dict = {}
     sql_index = []
     for sql in sql_list:
         sql_db, sql_v, sql_desc = SQL.get_sqlfile_info(sql)
         s_index = int(sql_v.split('.')[0]) * 1000 + int(
             sql_v.split('.')[1])
         sql_dict[s_index] = sql
         sql_index.append(s_index)
     sql_index = SQL.rm_done_sql(sql_index=sql_index, done_index=done_index)
     sql_index.sort()
     sql_done = []
     for index in sql_index:
         try:
             output = SQL.run_sql_file(db_info,
                                       work_folder + sql_dict[index])
         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
         if (output is not None) and (output != False):
             if not self.dbu_log(db_info, sql_dict[index]):
                 return False
             sql_done.append(sql_dict[index])
     if self.dep_status_id is not None:
         LOG('deploy').info('数据库增量升级完成, 执行SQL: ' + str(sql_done),
                            self.dep_status_id)
     return True
Beispiel #7
0
 def db_flyway_validate(self, shell):
     times = [1, 3, 5, 10, 20, 30, 30, 30, 30, 60, 60, 60]
     for t in times:
         time.sleep(t * 60)
         output, err = Popen(shell, stdin=PIPE, stdout=PIPE,
                             stderr=PIPE).communicate()
         if len(err) > 0:
             LOG('deploy').error('数据库升级失败:' + str(err.decode('utf-8')),
                                 self.dep_status_id)
             dep_status = Dep_Status.objects.get(id=self.dep_status_id)
             dep_status.dep_status = 'F'
             LOG('deploy').info('部署失败', self.dep_status_id)
             dep_status.save()
             break
         else:
             LOG('deploy').info('数据库升级完成:' + str(output.decode('utf-8')),
                                self.dep_status_id)
             dep_status = Dep_Status.objects.get(id=self.dep_status_id)
             dep_status.dep_status = 'Y'
             dep_status.save()
             LOG('deploy').info('部署成功', self.dep_status_id)
             break
Beispiel #8
0
    def dbu_log(self, db_info, sql):
        version = sql.split('_')[1]
        desc = sql.split('_')[2][:-4]
        version_v = version.split('.')
        script = 'INSERT INTO `db_version` (`version`, `version_p`, `version_s`, `user`, `status`) VALUES (\'' + version + '\', \'' + \
                 version_v[0] + '\', \
			\''       + version_v[1] + '\', \'auto\', \'' + desc + '\');'
        try:
            output = SQL.run_sql_cmd(db_info, script)
        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
        return True
Beispiel #9
0
 def get_server_info(self):
     try:
         server = Dep_Server.objects.get(id=self.server_id)
     except Exception as e:
         LOG('default').error('[服务器管理]获取服务器信息错误:\n' + str(e) + '\n' +
                              traceback.format_exc())
         return []
     hosts = server.hosts.all()
     databases = server.databases.all()
     server_info = []
     server_info.append('\"id\": \"' + str(server.id) + '\",')
     server_info.append('\"name\": \"' + str(server.server) + '\",')
     server_info.append('\"active\": \"' + str(server.active) + '\",')
     server_info.append('\"hosts\": [')
     for h in hosts:
         server_info.append('{')
         server_info.append('\"id\": \"' + str(h.id) + '\",')
         server_info.append('\"host_address\": \"' + h.host + '\",')
         server_info.append('\"ssh_port\": \"' + h.ssh_port + '\",')
         server_info.append('\"ssh_user\": \"' + h.ssh_user + '\",')
         server_info.append('\"ssh_pass\": \"' + h.ssh_pass + '\",')
         server_info.append('\"ssh_key\": \"' + h.ssh_file + '\",')
         server_info.append('\"paths\": [')
         paths = h.paths.all()
         for p in paths:
             server_info.append('{')
             server_info.append('\"id\": \"' + str(p.id) + '\",')
             server_info.append('\"module\": \"' + p.module + '\",')
             server_info.append('\"src_name\": \"' + p.src_name + '\",')
             server_info.append('\"des_path\": \"' + p.des_path + '\",')
             server_info.append('\"des_name\": \"' + p.des_name + '\",')
             server_info.append('},')
         server_info.append('],')
         server_info.append('},')
     server_info.append('],')
     server_info.append('\"databases\": [')
     for db in databases:
         server_info.append('{')
         server_info.append('\"id\": \"' + str(db.id) + '\",')
         server_info.append('\"db_module\": \"' + db.database + '\",')
         server_info.append('\"db_host\": \"' + db.db_host + '\",')
         server_info.append('\"db_port\": \"' + db.db_port + '\",')
         server_info.append('\"db_name\": \"' + db.db_name + '\",')
         server_info.append('\"db_user\": \"' + db.db_user + '\",')
         server_info.append('\"db_pass\": \"' + db.db_pass + '\",')
         server_info.append('},')
     server_info.append('],')
     return server_info
Beispiel #10
0
 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
Beispiel #11
0
 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 + '创建成功'}))
Beispiel #12
0
 def deploy(self, host_dict=None, command_dict=None):
     server = Dep_Server.objects.filter(id=self.server.id)
     if len(server) > 0:
         server = server[0]
     else:
         return False
     prev_deps = Dep_Status.objects.filter(server=server, on_server='Y')
     for dep in prev_deps:
         dep.on_server = 'N'
         dep.save()
     branch = Prod_Branch.objects.filter(id=self.bid)
     if len(branch) > 0:
         branch = branch[0]
     else:
         return False
     prod = Product.objects.filter(id=self.pid)
     if len(prod) > 0:
         prod = prod[0]
     else:
         return False
     ver = Prod_Version.objects.filter(id=self.vid)
     if len(ver) > 0:
         ver = ver[0]
     else:
         return False
     u = Auth.objects.filter(id=self.uid)
     if len(u) > 0:
         u = u[0]
     else:
         return False
     dep_status = Dep_Status(server=server,
                             prod=prod,
                             branch=branch,
                             version=ver,
                             dep_user=u,
                             dep_time=datetime.datetime.now(),
                             description='',
                             dep_status='D',
                             on_server='Y')
     dep_status.save()
     self.dep_status_id = dep_status.id
     if host_dict is None:
         host_dict = self.server.server_host
     if command_dict is None:
         command_dict = self.server.server_action
     host_dict[0] = ''
     for host_id in list(host_dict.keys()):
         if host_id in list(command_dict.keys()):
             host_command_list = command_dict[host_id]
         else:
             if 0 not in list(command_dict.keys()):
                 host_command_list = []
             else:
                 host_command_list = command_dict[0]
         if host_id not in list(self.server.server_path.keys()):
             paths = []
         else:
             paths = self.server.server_path[host_id]
         result_dict = {}
         for command in host_command_list:
             if command['prod_id'] == 0 or command['prod_id'] == self.pid:
                 cmd = command['operation']
                 try:
                     if cmd == 'dep_start':
                         result_dict[
                             command['sequence']] = self.deploy_start()
                     elif cmd == 'tomcat':
                         if command['param2'] in paths:
                             result_dict[command['sequence']] = self.Tomcat(host_id=host_id,
                                                                            action=command['param1'],
                                                                            work_folder=paths[command['param2']][
                                                                                'des_path'],
                                                                            script=command['param3'], \
                                                                            pid_path=command['param4'],
                                                                            force=command['param5'])
                         else:
                             LOG('deploy').warning(
                                 '主机 ' + str(host_id) + ' 未找到Tomcat工作路径 ' +
                                 command['param2'], self.dep_status_id)
                     elif cmd == 'sftp':
                         result_dict[
                             command['sequence']] = self.File_transfer(
                                 host_id=host_id,
                                 action=command['param1'],
                                 remote_file=command['param2'],
                                 local_file=command['param3'])
                     elif cmd == 'operate_file':
                         result_dict[
                             command['sequence']] = self.operate_file(
                                 action=command['param1'],
                                 file_src=command['param2'],
                                 file_des=command['param3'])
                     elif cmd == 'tar':
                         result_dict[command['sequence']] = self.tar_file(
                             action=command['param1'],
                             tar_path=command['param2'],
                             work_folder=command['param3'])
                     elif cmd == 'jar':
                         result_dict[
                             command['sequence']] = self.java_package(
                                 action=command['param1'],
                                 pak_path=command['param2'],
                                 work_folder=command['param3'],
                                 Manifest=command['param4'])
                     elif cmd == 'db_update':
                         #							result_dict[command['sequence']] = self.incre_db_update(database = command['param1'], work_folder = command['param2'])
                         result_dict[command['sequence']] = self.db_flyway(
                             module=command['param1'],
                             mode=command['param2'])
                     elif cmd == 'conf':
                         if command['param2'] in paths:
                             result_dict[
                                 command['sequence']] = self.deploy_conf(
                                     host_id=host_id,
                                     module=command['param1'],
                                     path=paths[
                                         command['param2']]['des_path'])
                         else:
                             LOG('deploy').warning(
                                 '主机 ' + str(host_id) + ' 未找到配置文件工作路径 ' +
                                 command['param2'], self.dep_status_id)
                     elif cmd == 'deploy':
                         if command['param2'] in paths:
                             result_dict[
                                 command['sequence']] = self.deploy_code(
                                     host_id=host_id,
                                     module=command['param1'],
                                     path=paths[command['param2']],
                                     deploy_local=command['param3'])
                         else:
                             LOG('deploy').warning(
                                 '主机 ' + str(host_id) + ' 未找到部署工作路径 ' +
                                 command['param2'], self.dep_status_id)
                     elif cmd == 'tomcod':
                         result_dict[command['sequence']] = self.TomCod(
                             host_id=host_id,
                             module=command['param1'],
                             port=command['param2'],
                             src_war=command['param3'],
                             action='push')
                     elif cmd == 'dep_finish':
                         test_result = self.dep_finish(
                             test_url=command['param1'])
                         result_dict[command['sequence']] = test_result
                         if not test_result:
                             dep_status = Dep_Status.objects.get(
                                 id=self.dep_status_id)
                             dep_status.dep_status = 'F'
                             dep_status.save()
                             return False
                 except (DeployError, SQLError) as e:
                     if self.dep_status_id is not None:
                         LOG('deploy').error(e.get_msg(),
                                             self.dep_status_id)
                     dep_status = Dep_Status.objects.get(
                         id=self.dep_status_id)
                     dep_status.dep_status = 'F'
                     dep_status.save()
                     return False
     dep_status = Dep_Status.objects.get(id=self.dep_status_id)
     dep_status.dep_status = 'Y'
     dep_status.save()
     return True
Beispiel #13
0
 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
Beispiel #14
0
 def Tomcat(self,
            host_id=None,
            action=None,
            work_folder=None,
            script=None,
            pid_path=None,
            force=None):
     if action == 'stop':
         a_mod = '关闭'
     else:
         a_mod = '启动'
     if self.dep_status_id is not None:
         LOG('deploy').info('开始 ' + a_mod + ' Tomcat', self.dep_status_id)
     if host_id is None:
         raise DeployError('001')
         return False
     if action is None:
         raise DeployError('002')
         return False
     if self.server is None:
         raise DeployError('003')
         return False
     host = self.server.server_host[host_id]
     if work_folder[-1:] != '/':
         work_folder += '/'
     ssh = SSHHelper(hostname=host['host'],
                     port=int(host['port']),
                     username=host['user'],
                     password=host['pass'],
                     key_file=host['pkey'])
     pid_script = ''
     act_msg = None
     if action == 'start':
         shell_script = pid_script + 'cd ' + work_folder + '; sudo -E ' + script
         act_msg = '启动tomcat'
     if action == 'stop':
         if force is not None and force != '0':
             shell_script = 'sudo kill -9 `ps -ef | grep ' + work_folder + ' | grep -v grep | awk \'{print $2}\'`'
         else:
             shell_script = pid_script + 'cd ' + work_folder + '; sudo -E ' + script
         act_msg = '关闭tomcat'
     #print(shell_script)
     outputs, errs = ssh.run_command(shell_script)
     if len(errs) == 0:
         if self.dep_status_id is not None and act_msg is not None:
             LOG('deploy').info(act_msg + ': ' + str(outputs),
                                self.dep_status_id)
         return True
     else:
         errs_output = str(errs)
         if re.search('sudo: unable to resolve host',
                      errs_output) is not None:
             #if re.search('sudo: unable to resolve host', errs_output) is not None:
             if self.dep_status_id is not None and act_msg is not None:
                 LOG('deploy').warning('命令执行有错误发生: ' + errs_output,
                                       self.dep_status_id)
                 LOG('deploy').info(act_msg + ': ' + str(outputs),
                                    self.dep_status_id)
             return True
         if action == 'stop':
             if self.dep_status_id is not None and act_msg is not None:
                 LOG('deploy').info(act_msg + ': ' + str(errs),
                                    self.dep_status_id)
             return True
         raise DeployError('004', str(errs))
         return False
Beispiel #15
0
 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