gen_inventory.py 5.11 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-

""""
   generate ansible inventory files from auto-deploy: http://git.yoho.cn/yohoops/auto_deploy/tree/master/scripts/iptable
"""


import json
import os
from subprocess import call
from jinja2 import Environment, FileSystemLoader
import collections
import time 
import sys
import get_iplist

# clone deploy project to local
HOME = os.path.expanduser("~")
PATH = os.path.join(HOME, ".tmp/auto_deploy")
if os.path.isdir(PATH):
    call("echo 'start to remove local git files'", shell=True)
    call("rm -rf %s" % PATH, shell=True)

call("mkdir -p %s"  % PATH, shell=True)
call("git clone  --recursive git@git.yoho.cn:yohoops/auto_deploy.git  %s" % PATH, shell=True)


# get iptables info from git files.
azs_mappings = {"qcloud": "az1",   "t6": "t6",  "containers": "containers"}
host_group = { "nginx": "java-nginx",
               "yoho_gateway": "java-gateway",
               "yoho_users": "java-users",
               "yohobuy_resources": "java-resources",
               "yohobuy_order" : "java-order",
               "yohobuy_promotion": "java-promotion",
               "yohobuy_product": "java-product",
               "yoho_message": "java-message",
               "yoho_wallet": "java-wallet",
               "yoho_sns": "java-sns",

               "yohobuy_brower": "java-brower",
               "uic": "java-uic",
               "yohobuy_wechat": "java-wechat",
               "yoho_erp_gateway": "java-erpgateway",

               "yohobuy_activity": "java-activity",
               "yohobuy_bigdata": "java-bigdata",
               "yohobuy_risk": "java-risk",

                "yohobuy_union": "java-union",
                
                "yoho_search_service":  "search-service",
                "ufo_platform": "java-ufo-platform",
                "yohoufo_fore": "java-ufo-fore",
                "yoho_social": "java-social",
                "platform_cms": "java-platform-cms",

                "yohobuy_shops": "java-shops",
                "yohobuy_portal_gateway": "java-portal-gateway",
                "yohobuy_extendstore": "java-extendstore",
                "yoho_inbox": "java-inbox",
                "yohobuy_crm": "java-crm",
                "yoho_reviewed": "java-review",
                "yoho_message_controller": "java-messagecontroller"
             }
containers_host_group = { "media-containers": "media-containers",
                          "web-containers": "web-containers",
                          "yoho-java-container": "yoho-java-container",
                          "yoho-ops-container": "yoho-ops-container",
                          "yoho-web-containers-new": "yoho-web-containers-new"
                        }
# all available zone groups
all_groups = {}
ClusterIds = {"yoho-java-container": "cls-rzb97zht","yoho-ops-container": "cls-gscif1o5","media-containers": "cls-qzkxrhg7","web-containers": "cls-ro6kl3cp","yoho-web-containers-new": "cls-ht4c6jkh"}
for _az in azs_mappings.keys():
    groups = {}
    all_groups[azs_mappings[_az]] = groups
    if _az == "containers":
        for line in ClusterIds.keys():
            for deploy_prj_name in containers_host_group.keys():
                if line == deploy_prj_name:
                    g_hosts = []
                    groups[containers_host_group[deploy_prj_name]] = g_hosts
                    result = get_iplist.ip_inqure(ClusterIds[line])
                    ip_s =  result['data']['data']['nodes']
                    for ip in ip_s:
                        g_hosts.append(ip['lanIp'].strip())
    else:
        file = open(os.path.join(PATH, "scripts/iptable/", _az, "app-iptable.sh"),  'r')
        for line in file:
            for deploy_prj_name in host_group.keys():
                if line.strip().startswith(deploy_prj_name + "_" + _az):
                    g_hosts = []
                    groups[host_group[deploy_prj_name]] = g_hosts
                    i_1 = line.index("(")
                    i_2 = line.index(")")
                    sub = line[i_1 + 1 : i_2]
                    for user_ip in sub.split("master@"):
                        if user_ip.strip() != '':
                            g_hosts.append(user_ip.strip())


        file.close()

print("get all inventory info success. \n")
print(all_groups)

# generate az inventory files
temp_dir = os.path.join(os.getcwd(), "scripts", "inventory", "templates")
env = Environment(loader=FileSystemLoader(temp_dir))
print("load jinj2 template env at dir: %s" %temp_dir)

for _az in azs_mappings.keys():
    az = azs_mappings[_az]

    # backup old file and open new file
    hosts = os.path.join("./inventories", az, "hosts")
    #call("mv %s %s" % (hosts, os.path.join("./inventories", az, "hosts.bak")), shell=True)
    file = open(hosts, 'w')

    # write template, sort the dict by key
    sorted_dict = collections.OrderedDict(sorted(all_groups[az].items(),  key=lambda t: t[0]))

    #add time
    ntime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

    template = env.get_template(os.path.basename('inventory-%s.j2' % az))
    file.write(template.render(groups = sorted_dict , time_now = ntime))
    file.close()

print "success generate ansible inventory files ! ."