74
def manager_from_config(cls, config):
74
def manager_from_config(cls, config, region=None):
75
75
"""Create an AWSAccount from a juju environment dict."""
76
yield cls(get_euca_env(config), config['region'])
76
euca_environ = get_euca_env(config)
78
region = config["region"]
79
client = ec2.connect_to_region(
80
region, aws_access_key_id=euca_environ['EC2_ACCESS_KEY'],
81
aws_secret_access_key=euca_environ['EC2_SECRET_KEY'])
82
yield cls(euca_environ, region, client)
78
def __init__(self, euca_environ, region):
84
def __init__(self, euca_environ, region, client):
79
85
self.euca_environ = euca_environ
80
86
self.region = region
82
def get_environ(self):
83
"""Return the environ to run euca in."""
84
environ = dict(os.environ)
85
environ.update(self.euca_environ)
89
def get_commandline(command, args):
90
"""Return the euca commandline."""
91
return ['euca-' + command] + args
93
def euca(self, command, args):
94
"""Run a euca-* command."""
95
commandline = self.get_commandline(command, args)
96
logging.info(' '.join(commandline))
97
return subprocess.check_call(commandline,
98
env=self.get_environ())
100
def get_euca_output(self, command, args):
101
"""Run a euca-* command and return its output."""
102
commandline = self.get_commandline(command, args)
103
logging.debug(' '.join(commandline))
104
return subprocess.check_output(commandline,
105
env=self.get_environ())
108
def iter_field_lists(lines):
109
"""Iterate through lists of fields for euca output."""
110
for line in lines.splitlines():
111
yield line.split('\t')
113
89
def iter_security_groups(self):
114
90
"""Iterate through security groups created by juju in this account.
116
:return: an itertator of (group-id, group-name) tuples.
92
:return: an iterator of (group-id, group-name) tuples.
118
lines = self.get_euca_output(
119
'describe-groups', ['--filter', 'description=juju group'])
120
for field in self.iter_field_lists(lines):
121
if field[:1] != ['GROUP']:
123
yield field[1], field[3]
94
groups = self.client.get_all_security_groups(
95
filters={'description': 'juju group'})
97
yield group.id, group.name
125
99
def iter_instance_security_groups(self, instance_ids=None):
126
100
"""List the security groups used by instances in this account.
128
102
:param instance_ids: If supplied, list only security groups used by
129
103
the specified instances.
130
:return: an itertator of (group-id, group-name) tuples.
104
:return: an iterator of (group-id, group-name) tuples.
132
106
logging.info('Listing security groups in use.')
133
connection = self.get_ec2_connection()
134
reservations = connection.get_all_instances(instance_ids=instance_ids)
107
reservations = self.client.get_all_instances(instance_ids=instance_ids)
135
108
for reservation in reservations:
136
109
for instance in reservation.instances:
137
110
for group in instance.groups:
146
119
for group in groups:
148
self.euca('delete-group', [group])
149
except subprocess.CalledProcessError:
120
deleted = self.client.delete_security_group(name=group)
150
122
failures.append(group)
153
def get_ec2_connection(self):
154
return ec2.connect_to_region(
155
self.region, aws_access_key_id=self.euca_environ['EC2_ACCESS_KEY'],
156
aws_secret_access_key=self.euca_environ['EC2_SECRET_KEY'],
159
125
def delete_detached_interfaces(self, security_groups):
160
126
"""Delete detached network interfaces for supplied groups.