python的mako、jinja2模板库,确实好用!这里做个笔记,好记性不如烂笔头。
#!/usr/bin/env python #encoding=utf-8 import sys,yaml # 配置文件使用yaml格式 from mako.template import Template # 加载mako库的Template Class from jinja2 import Environment,FileSystemLoader # 加载jinja2的Environment,FileSystemLoader Class
‘‘‘
解析配置文件,返回值是一个dict
‘‘‘
def parse_pxe_config(filename): config = yaml.load(file(filename,‘r‘)) return config
‘‘‘
这个函数实现的功能
举例来说,掩码前缀为27转换为255.255.255.224
‘‘‘
def int2mask(mask_str): mask_int = int(mask_str) mask_array = [‘0‘ for i in xrange(32)] # python for循环,吊吧!一口气生成32个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)] # 每8位组成一个list元素 temp_mask = [str(int(i,2)) for i in temp_mask] # int(i,2) 二进制转换为十进制 return ‘.‘.join(temp_mask)
‘‘‘
根据网关地址和掩码前缀得到管理网网段
‘‘‘
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)) 十进制转化为二进制 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)
‘‘‘
利用python模板库渲染配置文件
‘‘‘
def create_pxe_config(config): # 读取配置文件中的配置项 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( # 创建一个Template对象 filename=‘./pxe_template/dhcpd.conf‘, module_directory=‘/tmp/mako_modules‘ # 为了提高性能,从文件加载的 Template 还可以在文件系统中将生成的模块缓存为一般的Python模块文件, ) # 下次同样参数的Template 创建时,自动重用/tmp/mako_modules/目录下的模块文件。 dhcp_content = dhcp_template.render( # 传给 Template 的文本参数被编译为一个Python模块。模块包含一个 render_body() 函数,它产生模板的输出。 manage_gateway = manage_gateway, # 调用render() 方法时,Mako建立了一个模板的运行环境,并调用 render_body() 函数,把输出保存到缓冲,返回它的字符串内容 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‘) # 生成dhcpd.conf配置文件 fp.write(dhcp_content) if fp != None: fp.close() ip_mac_template = Template( # ip-mac 绑定 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‘) # 以append方式打开文件 fp.write(‘\n‘+ip_mac_content) # 追加内容 env = Environment( loader = FileSystemLoader(‘./pxe_template‘) # 这个类的实例被用于存储配置信息, 全局对象, 从文件系统或其他位置加载模板,使用的加载器loader是FileSystemLoader类型, ) # 可以加载的模板是当前工作目录下的templates目录下的模板文件 ks_template = env.get_template("puppet.cfg") # 使用env的模板环境加载名为puppet.cfg的模板文件. ks_content = ks_template.render( # 渲染模板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 config.yaml" sys.exit(-1) config = parse_pxe_config(sys.argv[1]) create_pxe_config(config)
参考链接
http://www.yeolar.com/note/2012/08/26/mako-usage/
http://blog.csdn.net/lgg201/article/details/4647471
本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1566384
原文地址:http://iceyao.blog.51cto.com/9426658/1566384