【问题标题】:Boto script freezing when trying to deploy and configure EC2 instances尝试部署和配置 EC2 实例时,Boto 脚本冻结
【发布时间】:2011-11-29 21:22:32
【问题描述】:

我在 Python 中使用 boto 来自动化我的一些 EC2 工作流。

这个问题很奇怪 - 脚本似乎在分配一个简单变量时冻结,但它在后台继续运行。最终脚本将所有内容打印出来。

当我在 iPython 中逐行重复脚本时,没有问题,也没有冻结或等待(超出您在使用 AWS 时所期望的)。当我将其作为 Python 脚本运行时,输出似乎会冻结,直到脚本完成。

脚本:

def deploy_web_db_ami(a_key, a_pri, db_vol_id, db_vol_zone, db_vol_mnt):
    '''deploys a fresh instance for using as the web / db server.  
    '''

    #Connect to the EC2
    print "Connecting to AWS"
    conn = EC2Connection(a_key, a_pri)

    # get a ref to the image we want to install
    print "Searching for desired AMI image"
    image = conn.get_all_images(image_ids='ami-fd589594')


    print "Spinning up instance. . ."
    # Launch the image using our desired settings.
    reservation = image[0].run(key_name='my_kp', 
                               security_groups=['ssh'],
                               instance_type='t1.micro)
    print("we get this far before output freezes")
    ins = reservation.instances[0]


    print "Waiting for instance to move from pending to running. ",
    while (ins.state.lower() == u'pending'):
        print ". ",
        ins.update()
        time.sleep(5)

    time.sleep(5) # in case ssh is not up yet
    print "Instance %s's state changed to: %s" (ins.id, ins.state)
    if ins.state.lower() == u'running':
        # instance is up and running
        # Attach the db EBS volume
        print "Attaching DB EBS volume."
        conn.attach_volume(db_vol_id, ins.id, db_vol_mnt)
        p_dns = ins.dns_name
    else:
        print "ERROR - INSTANCE NOT RUNNING.:: %s" % ins.state

    print "All done!"
    return (ins.id, p_dns)

def total_web_deploy():
    deploy_web_db_ami('xxx', 'xxx', 'the id', 'the zone', '/mnt/sdf')
    some_other_functions(). .. 

我正在使用fab total_web_deploy从命令行运行脚本

输出将如下所示:

Connecting to AWS
Search for desired AMI image
Spinning up instance. . .
we get this far before output freezes

然后,我们将不得不等待实例和所有内容都完成,然后才能打印出脚本的其余部分。不过,它显然一直在后台工作。

Waiting for instance to move from pending to running.  .  .  .  .  .  .  .  .  .  .  Instance i-95c389f6's state changed to: running
Attaching DB EBS volume.
All done!

有什么想法吗?

编辑我已经澄清了这个问题。

【问题讨论】:

    标签: python amazon-ec2 amazon-web-services boto


    【解决方案1】:

    也许您的输出正在被缓冲。

    尝试直接写入 stderr 以排除这种情况:

    import sys
    sys.stderr.write("Equivalent message here\n")
    

    或者:

    import sys
    sys.stdout.write("Equivalent message here\n")
    sys.stdout.flush()
    

    【讨论】:

    • 是的,这就是问题所在。 . .谢谢!
    猜你喜欢
    • 2016-01-23
    • 2021-07-20
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多