-3

这是脚本的摘录(未经测试)

def start_custer():
    try:
        myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
    except IndexError:
        conn.restore_from_cluster_snapshot('vi-mar5-deliveryreport-new', mysnapidentifier, availability_zone='us-east-1a')

def stop_cluster():
    try:    
        myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
        conn.delete_cluster(myidentifier, skip_final_cluster_snapshot=False, final_cluster_snapshot_identifier=myvar)
    except:
        print "error"
  1. 这些功能在技术上(语法上)是否正确?

  2. 调用 python 脚本时如何调用它们?我需要一次启动或停止集群,而不是两者。

4

4 回答 4

4

对于您的第二个问题,我将通过以下方式解析命令行argparse

import argparse

parser = argparse.ArgumentParser(description="Manage the cluster")
parser.add_argument("action", choices=["stop", "start"],
                    help="Action to perform")

args = parser.parse_args()
if args.action == "start":
    start_cluster()
if args.action == "stop":
    stop_cluster()
于 2013-10-24T16:29:05.913 回答
2

其他人已经向您展示了执行此操作的最佳方法,但为了记录,您也可以从命令行执行此操作:

python -c "import cluster; cluster.start_cluster()"

(假设您的模块文件已命名cluster.py——如果没有,请相应地调整import语句)

这不像自己解析命令行那样用户友好,但它会在紧要关头完成。

于 2013-10-24T17:47:19.907 回答
1

我在您的脚本中添加了一个 main 函数,用于检查命令行参数,然后在没有提供有效参数时提示您:

import sys

def start_custer():
    try:
        myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
    except IndexError:
        conn.restore_from_cluster_snapshot('vi-mar5-deliveryreport-new', mysnapidentifier, availability_zone='us-east-1a')

def stop_cluster():
    try:    
        myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
        conn.delete_cluster(myidentifier, skip_final_cluster_snapshot=False, final_cluster_snapshot_identifier=myvar)
    except:
        print "error"

def main():
    valid_args, proc = ['start','stop'], None

    # check if cmd line args were passed in (>1 as sys.argv[0] is name of program)
    if len(sys.argv) > 1:
        if sys.argv[1].lower() in valid_args:
            proc = sys.argv[1].lower()

    # if a valid arg was passed in this has been stored in proc, if not prompt user
    while not proc or proc not in valid_args:
        print "\nPlease state which procedure you want to call, valid options are:", valid_args
        proc = raw_input('>>> ').lower()

        # prompt user if invalid
        if proc not in valid_args:
            print proc, 'is not a valid selection.'

    if proc == 'start':
        start_custer()
    elif proc == 'stop':
        stop_cluster()

# this makes the script automatically call main when starting up
if __name__ == '__main__':
    main()

您可以从命令行调用它,例如,如果您与文件位于同一目录(例如名为 cluster_ctrl.py),只需:

python cluster_ctrl.py start
于 2013-10-24T16:43:01.493 回答
1

1)这是Syntactically正确的,如果您在conn某处定义并导入它!

2)

def stop_cluster():
    ## Your code

def fun():
    ## your code

if __name__ == "__main__":
    import sys
    globals()[sys.argv[1]]()

用法:

   python2.7 test_syn.py fun
于 2013-10-24T16:28:31.913 回答