|
| 1 | +#!/usr/bin/python |
| 2 | +#coding=utf-8 |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +def do_generate_upstream_server_config(spark_host_info_path): |
| 7 | + |
| 8 | + format_tab = "\t" |
| 9 | + format_tab2 = format_tab*2 |
| 10 | + up_stream_place_template = format_tab+"upstream server_${hostname} {"+os.linesep + \ |
| 11 | + format_tab2 + "server ${host}:${port};" + os.linesep + \ |
| 12 | + format_tab + "}" |
| 13 | + server_place_holder_template = format_tab+"server {" + os.linesep + \ |
| 14 | + format_tab2 + "listen 80;" + os.linesep + \ |
| 15 | + format_tab2 + "server_name ${hostname};"+os.linesep + \ |
| 16 | + format_tab2 + "location / {" + os.linesep + \ |
| 17 | + format_tab2 + " proxy_pass http://server_${hostname};" + os.linesep + \ |
| 18 | + format_tab2 + "}"+os.linesep + \ |
| 19 | + format_tab + "}" |
| 20 | + spark_host_info_file = open(spark_host_info_path) |
| 21 | + host_info_lines = spark_host_info_file.readlines()[1:] |
| 22 | + |
| 23 | + up_stream_str = "" |
| 24 | + server_stream_str = "" |
| 25 | + |
| 26 | + spark_master_host_name = "spark_master" |
| 27 | + up_stream_master_item = up_stream_place_template.replace("${hostname}", spark_master_host_name)\ |
| 28 | + .replace("${host}", "127.0.0.1")\ |
| 29 | + .replace("${port}", "8080").replace("\t", "", 1) |
| 30 | + server_stream_master_item = \ |
| 31 | + server_place_holder_template.replace("${hostname}", spark_master_host_name).replace("\t", "", 1) |
| 32 | + |
| 33 | + up_stream_str += up_stream_master_item + os.linesep |
| 34 | + server_stream_str += server_stream_master_item+os.linesep |
| 35 | + |
| 36 | + for host_info in host_info_lines: |
| 37 | + host_info_list = host_info.split() |
| 38 | + up_stream_item = up_stream_place_template.replace("${hostname}", host_info_list[1].strip()) \ |
| 39 | + .replace("${host}", host_info_list[0].strip()) \ |
| 40 | + .replace("${port}", "8081") |
| 41 | + server_stream_item = server_place_holder_template.replace("${hostname}", host_info_list[1].strip()) |
| 42 | + |
| 43 | + up_stream_str += up_stream_item.rstrip() + os.linesep |
| 44 | + server_stream_str += server_stream_item.rstrip()+os.linesep |
| 45 | + return up_stream_str, server_stream_str |
| 46 | + |
| 47 | +def do_update_nginx_config_file(result_content, nginx_config_target_path): |
| 48 | + nginx_config_file = file(nginx_config_target_path, "w") |
| 49 | + nginx_config_file.write(result_content) |
| 50 | + |
| 51 | +def generate_config_file(spark_host_info_path,nginx_config_template_path, nginx_config_taget_path): |
| 52 | + |
| 53 | + up_stream_place_holder="${upstream_place_holder}" |
| 54 | + server_place_holder="${server_place_holder}" |
| 55 | + |
| 56 | + nginx_upstream_server_tuple = do_generate_upstream_server_config(spark_host_info_path) |
| 57 | + |
| 58 | + nginx_config_template_file = open(nginx_config_template_path) |
| 59 | + nginx_config_template_lines = nginx_config_template_file.readlines() |
| 60 | + result_content = "" |
| 61 | + for line in nginx_config_template_lines: |
| 62 | + result_content += line |
| 63 | + |
| 64 | + result_content = result_content.replace(up_stream_place_holder, nginx_upstream_server_tuple[0]) \ |
| 65 | + .replace(server_place_holder, nginx_upstream_server_tuple[1]) |
| 66 | + |
| 67 | + do_update_nginx_config_file(result_content, nginx_config_taget_path) |
| 68 | + |
0 commit comments