Python's mako, jinja2 Template Library, really good! Here to make a note, a good memory than a bad pen. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0061.gif "alt=" j_0061.gif "/>
#!/usr/bin/env python #encoding =utf-8import sys,yaml # configuration file using Yaml format from mako.template import Templa TE # loading Mako Library's template classfrom jinja2 import environment,filesystemloader # load JINJA2 environment,filesystemloader C Lass
‘‘‘
parsing the configuration file, the return value is a dict
‘‘‘
def parse_pxe_config (filename): config = yaml.load (file (filename, ' r ')) return config
‘‘‘
Functions implemented by this function
For example, a mask prefix of 27 is converted to 255.255.255.224
‘‘‘
def int2mask (mask_str): mask_int = Int (mask_str) Mask_array = [' 0 ' for I in xrange (+)] # Python for loop, hang it! A breath generates 32 elements of the list for I in Xrange (mask_int): mask_array[i] = ' 1 ' temp_mask = ['. Join (mask_array[i*8: (i +1) *8]) for I in Xrange (4)] # every 8 bits make up a list element temp_mask = [str (int (i,2)) for i in Temp_mask] # int (i,2) binary converted to decimal Return '. '. Join (Temp_mask)
‘‘‘
Management Network segment based on gateway address and mask prefix
‘‘‘
Def get_manage_net (GATEWAY,PREFIX_STR): prefix = int (PREFIX_STR) int2bin = [bin (int (i,10)). Split (' 0b ') [1] for i in Gateway.split ('. ')] # bin (int (i,10)) decimal conversion to binary for i in xrange (4 ): if len (Int2bin[i]) < 8: int2bin[i] = ' 0 ' * (8 - len (int2bin[i)) + int2bin[i] int2bin = ". Join (Int2bin) int2bin = int2bin[0: prefix]+ ' 0 ' * (32-prefix) int2bin_list = [int2bin[8*i: (i+1) *8] for i in xrange (4)] manage_net = [str (int (i,2)) for i in int2bin_list] return '. Join (Manage_net)
‘‘‘
render a configuration file using the Python Template Library
‘‘‘
Def create_pxe_config (config):# read configuration items in the configuration file system_common = config[' System common '] manage_prefix = system_common[' Manage_prefix '] manage_gateway = system_common[' Manage_gateway '] Manage_mask = int2mask (Manage_prefix) manage_net = get_manage_net ( Manage_gateway,manage_prefix) dns = system_common[' DNS '] fqdn = system_common[' FQDN '] repo_url = system_common[' repo_ URL '] password = system_common[' password '] manage_nic = system_common[' manage_nic '] storage_nic = system_common[' storage _nic '] public_nic = system_common[' public_nic '] data_ Nic = system_common[' data_nic '] deploy_node = config[' Deploy node '] deploy_node_ip = deploy_node[' IP '] deploy_node_hostname = deploy_node[' hostname '] dhcp_range_start = deploy_node[' dhcp_range_start '] dhcp_range_end = deploy_node[' Dhcp_range_end '] compute_node = config[' Compute node '] dhcp_template = template ( # Create a Template object filEname= './pxe_template/dhcpd.conf ', module_ directory= '/tmp/mako_modules ' # to improve performance, load from file Template You can also cache the generated module as a generic Python module file in the file system, ) # next time the same parameter template is created, the module files in the/tmp/mako_modules/directory are automatically reused. &nBsp; dhcp_content = dhcp_template.render ( # the text parameters passed to Template are compiled into a python module. The module contains a render_body () function that produces the output of the template. manage_gateway = manage_gateway, # call Render () method, Mako establishes a template's run environment and calls the Render_body () function, save output to buffer, return its string contents dns = dns, manage_mask = manage_mask, fqdn = fqdn, deploy_node_ip = deploy_node_ip, manage_net = manage_net, dhcp_range_start = dhcp_range_start, dhcp_range_end = dhcp_range _end ) fp = open ('./pxe_config/dhcpd.conf ', ' W ') # Generate dhcpd.conf configuration file fp.write (dhcp_content) if fp != none: fp.close () ip_mac_template = template ( # ip-mac Bindings filename= './ Pxe_template/ip_mac.conf ', module_directory= '/tmp/ Mako_modules ' ) for compute in compute_node: ip_mac_content = Ip_mac_template.render ( compute_hostname = compute[' hostname '], manage_mac = compute[' Manage_mac '], manage_ip = compute[' Manage_ip '] ) fp = open ('./pxe_config/dhcpd.conf ', ' a ' ) # Open File fp.write (' \ n ' +ip_mac_content) in Append mode # Additional content env = environment ( loader = filesystemloader ('./pxe_template ') # instances of this class are used to store configuration information, Global objects, loading templates from the file system or other locations, The loader used by loader is a filesystemloader type, ) # The template that can be loaded is the template file under the Templates directory under the current working directory ks_template = env.get_template ("Puppet.cfg" ) # Use the env template environment to load a template file named Puppet.cfg. ks_content = ks_ Template.render ( # Render Template template repo_url = repo_url, password = password, deploy_node_hostname = deploy_node_hostname, fqdn = fqdn, deploy_node_ip = deploy_node_ip, public_nic = public_nic, storage_nic = storage_nic, data_nic = data_nic, manage_gateway = manage_gateway, manage_mask = manage_mask ) fp = file ('./pxe_config/puppet.cfg ', ' W ') fp.write (ks_content) if fp != none: fp.close ()
if __name__ = = ' __main__ ': If Len (sys.argv)! = 2 or sys.argv[1]! = ' Config.yaml ': print "usage:pxe-init.py confi G.yaml "Sys.exit ( -1) config = Parse_pxe_config (sys.argv[1]) create_pxe_config (config)
Reference links
http://www.yeolar.com/note/2012/08/26/mako-usage/
http://blog.csdn.net/lgg201/article/details/4647471
This article is from the "The-way-to-cloud" blog, make sure to keep this source http://iceyao.blog.51cto.com/9426658/1566384
Python Template Rendering configuration file