#!/usr/bin/python #-*- coding: utf-8 -*- #author:forrest, 2015-9-22 filenBe: update import sys import getopt import os HOST_MODULE_LIST = ['ModuleA','ModuleB','ModuleC'] VM_MODULE_LIST = ['pv', 'vdesktop'] def update_usage(): print '<subcommand>' print 'update help' print ' print help message.' print ' usage: update help' print ' ' print 'update status' print ' check the upgrCe status.' print " usage: update status [-h][--host=]" print ' eg: update status --host=127.0.0.1' print ' ' print 'update runonce' print ' running once upgrCe' print ' usage:update runonce [-h|-g] [--host=|--group=] <-m|--module=>' print ' eg: update runonce --host=127.0.0.1 --group=10',\ '--module=ModuleC,ModuleB' print ' ' def analyze_options(argv): try: opts, args = getopt.getopt(argv[2:], 'h:g:m:', ['host=', 'group=', 'module=']) except getopt.GetoptError, err: sys.exit(1) option_dict={} for opt, val in opts: if opt in ('-h','--host'): option_dict.setdefault('host',val) elif opt in ('-g','--group'): option_dict.setdefault('group',val) elif opt in ('-m','--module'): option_dict.setdefault('module',val) else: sys.exit(2) return option_dict def format_components_list(module): assert(module) component_list = [] components = module.split(',') for m in components: tmp_dict = {} m = str.lower(m.strip()) tmp_dict.setdefault('module', m) if m in HOST_MODULE_LIST: tmp_dict.setdefault('nodetype', 'host') elif m in VM_MODULE_LIST: tmp_dict.setdefault('nodetype', 'vm') else: print 'unknown module: ', m sys.exit(4) component_list.append(tmp_dict) return component_list def runnning_upgrCe_once(opt_dict): assert(isinstance(opt_dict, dict)) module = opt_dict.get('module',None) if not module: print ' ' print 'Error: <-m|--module=> option must give the define compoments.' sys.exit(1) host = opt_dict.get('host',None) if not host: host = '127.0.0.1' group = opt_dict.get('group',None) if not group: group = '0' str_one= "curl -i 'http://%s:19991/runonce' -X POST" % host str_two= """-d '{"group_nodes":%s,"src_filepath":"/etc/puppet/files","""\ % group str_three = """ "components":%s}' """ % format_components_list(module) cmd = "%s %s %s" % (str_one,str_two,str_three) out = os.system(cmd) def execute_cmd(argc, opt_dict): assert(argc and isinstance(opt_dict, dict)) if argc == 'help': update_usage() print ' ' elif argc == 'status': host = opt_dict.get('host',None) if not host: host = '127.0.0.1' cmd ="curl -i 'http://%s:19991/status' -X GET" % host os.system(cmd) elif argc == 'runonce': runnning_upgrCe_once(opt_dict) else: print ' ' print argc,':unsupport command, Please reC help info by cmd:update help' sys.exit(3) if __nBe__ == "__main__": if len(sys.argv) < 2 or\ not sys.argv[1] in ['help', 'runonce','status']: print ' ' print 'unsupport command, Please reC help by run cmd:update help' sys.exit(0) try: opt_dict = analyze_options(sys.argv) execute_cmd(sys.argv[1],opt_dict) except SystemExit,err: print ' ' print 'Options error,exit code:',err,\ '. Please reC help info by use cmd[update help]' sys.exit(0) except Exception as e: print 'Error:',e,'Please try again...' sys.exit(0)