~nskaggs/juju-ci-tools/add-essential-operations

« back to all changes in this revision

Viewing changes to substrate.py

  • Committer: seman.said at canonical
  • Date: 2016-02-29 17:53:13 UTC
  • mfrom: (1241.1.5 clean-all-regions)
  • Revision ID: seman.said@canonical.com-20160229175313-qw5pty8ydrcc94a7
Updated cleaning up resources in all AWS regions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
 
72
72
    @classmethod
73
73
    @contextmanager
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)
 
77
        if region is None:
 
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)
77
83
 
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
81
 
 
82
 
    def get_environ(self):
83
 
        """Return the environ to run euca in."""
84
 
        environ = dict(os.environ)
85
 
        environ.update(self.euca_environ)
86
 
        return environ
87
 
 
88
 
    @staticmethod
89
 
    def get_commandline(command, args):
90
 
        """Return the euca commandline."""
91
 
        return ['euca-' + command] + args
92
 
 
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())
99
 
 
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())
106
 
 
107
 
    @staticmethod
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')
 
87
        self.client = client
112
88
 
113
89
    def iter_security_groups(self):
114
90
        """Iterate through security groups created by juju in this account.
115
91
 
116
 
        :return: an itertator of (group-id, group-name) tuples.
 
92
        :return: an iterator of (group-id, group-name) tuples.
117
93
        """
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']:
122
 
                continue
123
 
            yield field[1], field[3]
 
94
        groups = self.client.get_all_security_groups(
 
95
            filters={'description': 'juju group'})
 
96
        for group in groups:
 
97
            yield group.id, group.name
124
98
 
125
99
    def iter_instance_security_groups(self, instance_ids=None):
126
100
        """List the security groups used by instances in this account.
127
101
 
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.
131
105
        """
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:
144
117
        """
145
118
        failures = []
146
119
        for group in groups:
147
 
            try:
148
 
                self.euca('delete-group', [group])
149
 
            except subprocess.CalledProcessError:
 
120
            deleted = self.client.delete_security_group(name=group)
 
121
            if not deleted:
150
122
                failures.append(group)
151
123
        return failures
152
124
 
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'],
157
 
        )
158
 
 
159
125
    def delete_detached_interfaces(self, security_groups):
160
126
        """Delete detached network interfaces for supplied groups.
161
127
 
163
129
        :return: A collection of security groups which still have interfaces in
164
130
            them.
165
131
        """
166
 
        connection = self.get_ec2_connection()
167
 
        interfaces = connection.get_all_network_interfaces(
 
132
        interfaces = self.client.get_all_network_interfaces(
168
133
            filters={'status': 'available'})
169
134
        unclean = set()
170
135
        for interface in interfaces:
178
143
                                'InvalidNetworkInterfaceID.NotFound'):
179
144
                            raise
180
145
                        logging.info(
181
 
                            'Failed to delete interface {}'.format(
182
 
                                interface.id))
 
146
                            'Failed to delete interface {!r}. {}'.format(
 
147
                                interface.id, e.message))
183
148
                        unclean.update(g.id for g in interface.groups)
184
149
                    break
185
150
        return unclean
222
187
    def iter_security_groups(self):
223
188
        """Iterate through security groups created by juju in this account.
224
189
 
225
 
        :return: an itertator of (group-id, group-name) tuples.
 
190
        :return: an iterator of (group-id, group-name) tuples.
226
191
        """
227
192
        return ((g.id, g.name) for g in self.client.security_groups.list()
228
193
                if g.description == 'juju group')
232
197
 
233
198
        :param instance_ids: If supplied, list only security groups used by
234
199
            the specified instances.
235
 
        :return: an itertator of (group-id, group-name) tuples.
 
200
        :return: an iterator of (group-id, group-name) tuples.
236
201
        """
237
202
        group_names = set()
238
203
        for server in self.client.servers.list():