Python升级命令解析

#!/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)


转载于:https://my.oschina.net/mingfu/blog/527622

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值