remotecmd.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:drift 作者: dgnorth 项目源码 文件源码
def run_command(args):
    cmd = args.cmd
    if not cmd:
        print "Please enter command to run. Example: kitrun.py remotecmd \"ls -l\""
        return
    tier_config = get_tier_config()
    service_info = get_service_info()
    tier = tier_config["tier"]
    region = tier_config["region"]
    service_name = service_info["name"]
    public = args.public

    pem_file = None
    for deployable in tier_config["deployables"]:
        if deployable["name"] == service_name:
            pem_file = deployable["ssh_key"]
            break
    else:
        print "Service {} not found in tier config for {}".format(service_name, tier)
        sys.exit(1)
    print "\n*** EXECUTING REMOTE COMMAND '{}' ON SERVICE '{}' / TIER '{}' IN REGION '{}'\n".format(cmd, service_name, tier, region)

    filters = {
            'tag:service-name': service_name,
            "instance-state-name": "running",
            "tag:tier": tier,
        }
    print "Finding ec2 instances in region %s from filters: %s" % (region, filters)
    instances = get_ec2_instances(region, filters=filters)
    if not instances:
        print "Found no running ec2 instances with tag service-name={}".format(service_name)
        return

    for ec2 in instances:
        if not public:
            ip_address = ec2.private_ip_address
        else:
            ip_address = ec2.ip_address
        print "*** Running '{}' on {}...".format(cmd, ip_address)

        env.host_string = ip_address
        env.user = EC2_USERNAME
        env.key_filename = '~/.ssh/{}'.format(pem_file)
        run(cmd)
        print
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号