def delete_instance(self, instance): def _delete_security_group(inst): security_group = inst.metadata.get('security_groups', '') if security_group: manager = SecurityGroup(self.driver) timeout = 10.0 # wait a maximum of 10 seconds before forcing the security group delete start_time = time.time() instance_deleted = False while instance_deleted == False and (time.time() - start_time) < timeout: tmp_inst = self.driver.shell.nova_manager.servers.findall(id=inst.id) if not tmp_inst: instance_deleted = True time.sleep(.5) manager.delete_security_group(security_group) multiclient = MultiClient() tenant = self.driver.shell.auth_manager.tenants.find(id=instance.tenant_id) self.driver.shell.nova_manager.connect(tenant=tenant.name) args = {'name': instance.name, 'id': instance.id} instances = self.driver.shell.nova_manager.servers.findall(**args) security_group_manager = SecurityGroup(self.driver) for instance in instances: # destroy instance self.driver.shell.nova_manager.servers.delete(instance) # deleate this instance's security groups multiclient.run(_delete_security_group, instance) return 1
def _delete_security_group(inst): security_group = inst.metadata.get('security_groups', '') if security_group: manager = SecurityGroup(self.driver) timeout = 10.0 # wait a maximum of 10 seconds before forcing the security group delete start_time = time.time() instance_deleted = False while instance_deleted == False and (time.time() - start_time) < timeout: tmp_inst = self.driver.shell.nova_manager.servers.findall(id=inst.id) if not tmp_inst: instance_deleted = True time.sleep(.5) manager.delete_security_group(security_group)
def create_security_group(self, slicename, fw_rules=[]): # use default group by default group_name = 'default' if isinstance(fw_rules, list) and fw_rules: # Each sliver get's its own security group. # Keep security group names unique by appending some random # characters on end. random_name = "".join([random.choice(string.letters+string.digits) for i in xrange(6)]) group_name = slicename + random_name security_group = SecurityGroup(self.driver) security_group.create_security_group(group_name) for rule in fw_rules: security_group.add_rule_to_group(group_name, protocol = rule.get('protocol'), cidr_ip = rule.get('cidr_ip'), port_range = rule.get('port_range'), icmp_type_code = rule.get('icmp_type_code')) # Open ICMP by default security_group.add_rule_to_group(group_name, protocol = "icmp", cidr_ip = "0.0.0.0/0", icmp_type_code = "-1:-1") return group_name
def add_rule_to_security_group(self, group_name, **kwds): security_group = SecurityGroup(self.driver) security_group.add_rule_to_group(group_name=group_name, protocol=kwds.get('protocol'), cidr_ip =kwds.get('cidr_ip'), icmp_type_code = kwds.get('icmp_type_code'))