Пример #1
0
def exec_sonar_v2(publish_name, git_repository, publish_tag, mail_to):
    app_name = git_repository.split('/')[-1].replace(".git", "")
    print(app_name)
    ### 删除已经存在的,然后创建
    app_path = os.path.join('/var/www/version/', app_name)
    os.system('mkdir -p {}'.format(app_path))
    cmd1 = 'cd {} && rm -rf *'.format(app_path)
    os.system(cmd1)
    ## 代码获取完毕
    cmd = "cd {} && git clone -b {} {}".format(app_path, publish_tag,
                                               git_repository)
    print(cmd)

    sub1 = subprocess.Popen(cmd,
                            shell=True,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    stdout, stderr = sub1.communicate()
    ret = sub1.returncode
    if ret != 0:
        print(stdout, stderr)
        exit(-2)

    ## 写入扫描文件
    print("start checking code")
    sonar_file = os.path.join(app_path, app_name, "sonar-project.properties")
    with open(sonar_file, 'w') as f:
        f.write("sonar.projectKey={key}\n"
                "sonar.projectName={name}\n"
                "sonar.projectVersion=1.0\n"
                "sonar.sources=.\n"
                "sonar.login={sonar_login}\n".format(
                    key=app_name,
                    name=publish_name,
                    sonar_login=sonar_info.get('sonar_login')))

    sub2 = subprocess.Popen(
        "cd {}/{} && /usr/local/sonar-scanner/bin/sonar-scanner".format(
            app_path, app_name),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    stdout, stderr = sub2.communicate()

    for i in stdout.decode('utf-8').split('\n'):
        print(i)

    print("mail_to: ", mail_to)
    msg = "代码检查完毕!\n项目:{}\n应用:{}\n标签:{}\n用户名: shinezone \n密码:shinezone \n详情:{}{}".format(
        publish_name, app_name, publish_tag, sonar_info.get('sonar_domain'),
        app_name)
    obj = API()

    result = obj.send_mail_for_api(mail_to, '代码检查', msg)
    print(result)
    print("successful")
Пример #2
0
def main(publish_name, db_file):
    obj = API()
    result = obj.get_publish_name_info(publish_name)
    publish_hosts_api, mail_to = result[0]['publish_hosts_api'], result[0]['mail_to']
    if publish_hosts_api:
        res = obj.get_api_info(publish_hosts_api)
        for db in res['db_list']:
            if db['db_type'] == 'MySQL' and db['db_role'] == 'master':
                db_info = db['db_info']
                exec_inception_v2(db_info, db_file)
Пример #3
0
def main(publish_name, flow_id):
    """
    :param publish_name: 发布应用的名称
    :param flow_id: 订单ID
    :return:
    """
    file_name = '/tmp/publish_{}.json'.format(flow_id)
    obj = API()
    data = obj.get_publish_name_info(publish_name)
    data_save(file_name, data)
Пример #4
0
 def login(self):
     obj = API()
     result = obj.get_api_info(api_settings['docker_registry_info_api'])
     for i in result:
         if i['registry_url'] == self.docker_registry:
             login_cmd = "docker login -u {} -p {} {}".format(
                 i['user_name'], i['password'], i['registry_url'])
             print(login_cmd)
             exec_status, exec_output = exec_shell(login_cmd)
             if exec_status == 0:
                 print('[Success]: docker login {} OK...'.format(
                     self.docker_registry))
             else:
                 print('[Error]: docker login {} failed ...\n[Error]: {}'.
                       format(self.docker_registry, exec_output))
                 exit(-1)
Пример #5
0
def main(publish_name, db_name, sqls):
    if publish_name == 'PUBLISH_NAME' or db_name == 'DB_NAME':
        print("[Error:] PUBLISH_NAME DB_NAME 不能为空")
        exit(-1)

    obj = API()
    result = obj.get_publish_name_info(publish_name)
    publish_hosts_api, mail_to = result[0]['publish_hosts_api'], result[0]['mail_to']

    print(publish_hosts_api, mail_to)
    all_msg = ''
    if publish_hosts_api:
        res = obj.get_api_info(publish_hosts_api)
        for db in res['db_list']:
            if db['db_type'] == 'MySQL' and db['db_role'] == 'master':
                db_info = db['db_info']
                conf_file, db_host = get_conf(db_name, db_info)
                print('db host is {}'.format(db_host))
                print("conf_file  is " + conf_file)
                all_msg = all_msg + 'db host is {}\n'.format(db_host)
                all_msg = all_msg + "conf_file is {}\n".format(conf_file)
                result_msg = exec_sqladvisor_v2(conf_file, sqls)
                print(result_msg)
                all_msg = all_msg + result_msg

        ### 发送邮件
        msg = "SQL优化完毕!\n库名:{}\n语句:{}\n{}".format(db_name, sqls, all_msg)
        obj = API()

        print(obj.send_mail_for_api(mail_to, 'SQL索引优化', msg))
        print("successful")
    else:
        print('[Error:] git repository is none !!!')
Пример #6
0
def get_config_info(publish_name):
    obj = API()
    result = obj.get_publish_name_info(publish_name)
    return result[0]['repository'], result[0]['mail_to']