从这个Python结构程序中,我应该打印EC2实例的细节。
@task
def ec2_print_instance(full=False):
"""
Print EC2 instance info.
:param full: Print all attributes, or just the most useful ones? Defaults
to ``False``.
"""
instancewrapper = Ec2InstanceWrapper.get_from_host_string()
print 'Instance:', instancewrapper['id']
print_ec2_instance(instancewrapper.instance, full=full)
上面的方法调用以下类EC2InstanceWrapper。
class Ec2InstanceWrapper(object):
def __init__(self, instance):
self.instance = instance
def __getitem__(self, key):
return getattr(self.instance, key)
def __str__(self):
return 'Ec2InstanceWrapper:{0}'.format(self)
def add_instance_to_env(self):
if not 'ec2instances' in env:
env['ec2instances'] = {}
env['ec2instances'][self.get_ssh_uri()] = self
if not env.key_filename:
env.key_filename = []
key_filename = self.get_ssh_key_filename()
if not key_filename in env.key_filename:
env.key_filename.append(key_filename)
@classmethod
def get_from_host_string(cls):
"""
If an instance has been registered in ``fabric.api.env`` using
:meth:`add_instance_to_env`, this method can be used to get
the instance identified by ``fabric.api.env.host_string``.
"""
return env.ec2instances[env.host_string]
如果我运行此程序,则获取AttributeError:ec2instances。 我需要使用add_instance_to_env方法将我的主机添加到env。怎么做?