Example #1
0
    def generate_service(svr_name, svr_index, install_prefix, section_name,
                         **ext_options):
        project.set_server_inst(config.items(section_name), svr_name,
                                svr_index)
        common.print_color.cprintf_stdout([
            common.print_color.print_style.FC_YELLOW,
            common.print_color.print_style.FW_BOLD
        ], 'start to generate etc and script of {0}-{1}\r\n', svr_name,
                                          svr_index)

        install_abs_prefix = os.path.normpath(
            os.path.join(script_dir, '..', '..', install_prefix))

        if not os.path.exists(os.path.join(install_abs_prefix, 'etc')):
            os.makedirs(os.path.join(install_abs_prefix, 'etc'))
        if not os.path.exists(os.path.join(install_abs_prefix, 'bin')):
            os.makedirs(os.path.join(install_abs_prefix, 'bin'))

        def generate_template(temp_dir,
                              temp_path,
                              out_dir,
                              out_path,
                              all_content_script=None):
            gen_in_path = os.path.join(temp_dir, temp_path)
            gen_out_path = os.path.join(install_abs_prefix, out_dir, out_path)
            if os.path.exists(gen_in_path):
                project.render_to(temp_path,
                                  gen_out_path,
                                  project_install_prefix=os.path.relpath(
                                      '.',
                                      os.path.join(install_prefix, out_dir)),
                                  **ext_options)
                if all_content_script is not None and all_content_script in all_service_temps:
                    all_service_temps[all_content_script]['content'].append("""
# ==================== {0} ==================== 
if [ $# -eq 0 ] || [ "0" == "$(is_in_server_list {1} $*)" ]; then 
    bash {2}
fi
                    """.format(project.get_server_full_name(),
                               project.get_server_full_name(),
                               os.path.relpath(gen_out_path, script_dir)))

        custom_rule_file_path = os.path.join(script_dir, 'helper',
                                             'custom_template_rules', svr_name)
        if os.path.exists(custom_rule_file_path):  # 自定义服务配置列表
            rules_data = project.render_string(
                codecs.open(custom_rule_file_path, mode='r',
                            encoding='utf-8').read(), **ext_options)
            for rule_line in rules_data.splitlines():
                rule_data = rule_line.strip()
                if not rule_data:
                    continue
                if rule_data[0:1] == "#" or rule_data[0:1] == ";":
                    continue
                mat_res = custom_tmpl_rule.match(rule_data)
                if mat_res is None:
                    common.print_color.cprintf_stderr([
                        common.print_color.print_style.FC_RED,
                        common.print_color.print_style.FW_BOLD
                    ], '"{0}" is not a valid custom service rule.\r\n',
                                                      rule_line)
                    continue
                tmpl_dir = mat_res.group('DIR').strip()
                tmpl_src = mat_res.group('SRC').strip()
                tmpl_dst = mat_res.group('DST').strip()
                if mat_res.group('GLOBAL') is not None:
                    tmpl_global = mat_res.group('GLOBAL').strip()
                else:
                    tmpl_global = None
                generate_template(os.path.join(project_template_dir, tmpl_dir),
                                  tmpl_src, os.path.dirname(tmpl_dst),
                                  os.path.basename(tmpl_dst), tmpl_global)

        else:  # 标准服务配置
            # etc
            generate_template(etc_template_dir, '{0}.conf'.format(svr_name),
                              'etc',
                              '{0}-{1}.conf'.format(svr_name, svr_index))

            # scripts
            generate_template(script_template_dir, 'start.sh', 'bin',
                              'start-{0}.sh'.format(svr_index),
                              'restart_all.sh')
            generate_template(script_template_dir, 'stop.sh', 'bin',
                              'stop-{0}.sh'.format(svr_index), 'stop_all.sh')
            generate_template(script_template_dir, 'reload.sh', 'bin',
                              'reload-{0}.sh'.format(svr_index),
                              'reload_all.sh')
            generate_template(script_template_dir, 'debug.sh', 'bin',
                              'debug-{0}.sh'.format(svr_index))
            generate_template(script_template_dir, 'run.sh', 'bin',
                              'run-{0}.sh'.format(svr_index))
Example #2
0
    def generate_service(svr_name, svr_index, install_prefix, section_name, **ext_options):
        project.set_server_inst(config.items(
            section_name), svr_name, svr_index)
        common.print_color.cprintf_stdout([common.print_color.print_style.FC_YELLOW, common.print_color.print_style.FW_BOLD],
                                          'start to generate etc and script of {0}-{1}\r\n', svr_name, svr_index)

        install_abs_prefix = os.path.normpath(os.path.join(script_dir, '..', '..', install_prefix))

        if not os.path.exists(os.path.join(install_abs_prefix, 'etc')):
            os.makedirs(os.path.join(install_abs_prefix, 'etc'))
        if not os.path.exists(os.path.join(install_abs_prefix, 'bin')):
            os.makedirs(os.path.join(install_abs_prefix, 'bin'))

        def generate_template(temp_dir, temp_path, out_dir, out_path, all_content_script=None):
            gen_in_path = os.path.join(temp_dir, temp_path)
            gen_out_path = os.path.join(install_abs_prefix, out_dir, out_path)
            if os.path.exists(gen_in_path):
                project.render_to(temp_path, gen_out_path, project_install_prefix=os.path.relpath(
                    '.', os.path.join(install_prefix, out_dir)), **ext_options)
                if all_content_script is not None and all_content_script in all_service_temps:
                    all_service_temps[all_content_script]['content'].append("""
# ==================== {0} ==================== 
if [ $# -eq 0 ] || [ "0" == "$(is_in_server_list {1} $*)" ]; then 
    bash {2}
fi
                    """.format(
                        project.get_server_full_name(),
                        project.get_server_full_name(),
                        os.path.relpath(gen_out_path, script_dir)
                    ))

        custom_rule_file_path = os.path.join(script_dir, 'helper', 'custom_template_rules', svr_name)
        if os.path.exists(custom_rule_file_path): # 自定义服务配置列表
            rules_data = project.render_string(
                codecs.open(custom_rule_file_path, mode='r', encoding='utf-8').read(), 
                **ext_options
            )
            for rule_line in rules_data.splitlines():
                rule_data = rule_line.strip()
                if not rule_data:
                    continue
                if rule_data[0:1] == "#" or rule_data[0:1] == ";":
                    continue
                mat_res = custom_tmpl_rule.match(rule_data)
                if mat_res is None:
                    common.print_color.cprintf_stderr(
                        [common.print_color.print_style.FC_RED, common.print_color.print_style.FW_BOLD], '"{0}" is not a valid custom service rule.\r\n', rule_line)
                    continue
                tmpl_dir = mat_res.group('DIR').strip()
                tmpl_src = mat_res.group('SRC').strip()
                tmpl_dst = mat_res.group('DST').strip()
                if mat_res.group('GLOBAL') is not None:
                    tmpl_global = mat_res.group('GLOBAL').strip()
                else:
                    tmpl_global = None
                generate_template(os.path.join(project_template_dir, tmpl_dir), tmpl_src, os.path.dirname(tmpl_dst), os.path.basename(tmpl_dst), tmpl_global)

        else: # 标准服务配置
            # etc
            generate_template(etc_template_dir, '{0}.conf'.format(
                svr_name), 'etc', '{0}-{1}.conf'.format(svr_name, svr_index))

            # scripts
            generate_template(script_template_dir, 'start.sh', 'bin',
                            'start-{0}.sh'.format(svr_index), 'restart_all.sh')
            generate_template(script_template_dir, 'stop.sh', 'bin',
                            'stop-{0}.sh'.format(svr_index), 'stop_all.sh')
            generate_template(script_template_dir, 'reload.sh', 'bin',
                            'reload-{0}.sh'.format(svr_index), 'reload_all.sh')
            generate_template(script_template_dir, 'debug.sh',
                            'bin', 'debug-{0}.sh'.format(svr_index))
            generate_template(script_template_dir, 'run.sh',
                            'bin', 'run-{0}.sh'.format(svr_index))
Example #3
0
                             gateway_index,
                             install_prefix,
                             'server.atgateway',
                             for_server_name=svr_name,
                             for_server_index=svr_index,
                             for_server_id=for_server_id,
                             for_server_type_id=for_server_type_id)

    # custom global rules
    project_install_abs_path = os.path.normpath(
        os.path.join(script_dir, '..', '..'))
    for custom_global_rule_file in glob.glob(
            os.path.join(script_dir, 'helper', 'custom_global_rules', '*')):
        if os.path.exists(custom_global_rule_file):  # ×Ô¶¨ÒåÈ«¾ÖÄ£°åÁбí
            rules_data = project.render_string(
                codecs.open(custom_global_rule_file,
                            mode='r',
                            encoding='utf-8').read())
            for rule_line in rules_data.splitlines():
                rule_data = rule_line.strip()
                if not rule_data:
                    continue
                if rule_data[0:1] == "#" or rule_data[0:1] == ";":
                    continue
                mat_res = custom_global_rule.match(rule_data)
                if mat_res is None:
                    common.print_color.cprintf_stderr([
                        common.print_color.print_style.FC_RED,
                        common.print_color.print_style.FW_BOLD
                    ], '"{0}" is not a valid global rule.\r\n', rule_line)
                    continue
                tmpl_src = mat_res.group('SRC').strip()
Example #4
0
            gateway_index = project.get_server_gateway_index(svr_name, svr_index, 'atgateway')
            for_server_id = project.get_server_proc_id(svr_name, svr_index)
            for_server_type_id = project.get_server_type_id(svr_name)
            generate_service('atgateway', gateway_index, install_prefix, 'server.atgateway',
                for_server_name=svr_name,
                for_server_index=svr_index,
                for_server_id=for_server_id,
                for_server_type_id=for_server_type_id
            )

    # custom global rules
    project_install_abs_path = os.path.normpath(os.path.join(script_dir, '..', '..'))
    for custom_global_rule_file in glob.glob(os.path.join(script_dir, 'helper', 'custom_global_rules', '*')):
        if os.path.exists(custom_global_rule_file): # ×Ô¶¨ÒåÈ«¾ÖÄ£°åÁбí
            rules_data = project.render_string(
                codecs.open(custom_global_rule_file, mode='r', encoding='utf-8').read()
            )
            for rule_line in rules_data.splitlines():
                rule_data = rule_line.strip()
                if not rule_data:
                    continue
                if rule_data[0:1] == "#" or rule_data[0:1] == ";":
                    continue
                mat_res = custom_global_rule.match(rule_data)
                if mat_res is None:
                    common.print_color.cprintf_stderr(
                        [common.print_color.print_style.FC_RED, common.print_color.print_style.FW_BOLD], '"{0}" is not a valid global rule.\r\n', rule_line)
                    continue
                tmpl_src = mat_res.group('SRC').strip()
                tmpl_dst = mat_res.group('DST').strip()
                if not tmpl_src or not tmpl_dst: