def delete_firewall(self, network_name, subnetwork_name): """ Delete the firewall corresponding to the service described by "network_name" and "subnetwork_name". """ firewalls = self.driver.ex_list_firewalls() tag = "%s-%s" % (network_name, subnetwork_name) for firewall in firewalls: if not firewall.source_tags and not firewall.target_tags: continue if firewall.source_tags: if tag in firewall.source_tags: logger.info("Deleting firewall %s because of source tag: %s", firewall, tag) self.driver.ex_destroy_firewall(firewall) continue if firewall.target_tags: if tag in firewall.target_tags: logger.info("Deleting firewall %s because of target tag: %s", firewall, tag) self.driver.ex_destroy_firewall(firewall) continue
def create(self, name, service): """ Create new image named "name" from "service". """ instances = [ instance for subnetwork in service.subnetworks for instance in subnetwork.instances ] for gce_node in self.driver.list_nodes(): if gce_node.uuid == instances[0].instance_id: node = gce_node if not node: raise BadEnvironmentStateException( "Could not find instance in service %s" % service) if len(node.extra["disks"]) != 1: raise DisallowedOperationException( "Only support exactly one volume for image create: %s" % node.extra["disks"]) logger.info("Stopping node: %s", node.name) self.driver.ex_stop_node(node) logger.info("Creating image from service: %s", service.name) volume = self.driver.ex_get_volume( node.extra["disks"][0]["deviceName"]) image = self.driver.ex_create_image(name, volume) logger.info("Created image: %s", image.id) return Image(name=image.name, image_id=image.id, created_at=str(image.extra["creationTimestamp"]))
def destroy(self, service): """ Destroy a service described by "service". """ logger.debug('Destroying service: %s', service) destroy_results = [] for node in self.driver.list_nodes(): metadata = node.extra.get("metadata", {}).get("items", []) node_network_name = None node_subnetwork_name = None for item in metadata: logger.debug("Found metadata item %s for node %s", item, node) if item["key"] == "network": node_network_name = item["value"] if item["key"] == "subnetwork": node_subnetwork_name = item["value"] if (service.network.name == node_network_name and service.name == node_subnetwork_name): logger.info('Destroying instance: %s', node.name) destroy_results.append(self.driver.destroy_node(node)) subnetwork_destroy = self.subnetwork.destroy(service.network.name, service.name) self.firewalls.delete_firewall(service.network.name, service.name) return {"Subnetwork": subnetwork_destroy, "Instances": destroy_results}
def create(self, network, service_name, blueprint, template_vars, count): """ Create a service in "network" named "service_name" with blueprint file at "blueprint". """ logger.debug( 'Creating service %s, %s with blueprint %s and ' 'template_vars %s', network.name, service_name, blueprint, template_vars) self.subnetwork.create(network.name, service_name, blueprint=blueprint) instances_blueprint = ServiceBlueprint.from_file(blueprint) az_count = instances_blueprint.availability_zone_count() availability_zones = list( itertools.islice(self._get_availability_zones(), az_count)) if len(availability_zones) < az_count: raise DisallowedOperationException( "Do not have %s availability zones: %s" % (az_count, availability_zones)) instance_count = az_count if count: instance_count = count def get_image(image_specifier): images = [ image for image in self.driver.list_images() if re.match(image_specifier, image.name) ] if not images: raise DisallowedOperationException( "Could not find image named %s" % image_specifier) if len(images) > 1: raise DisallowedOperationException( "Found multiple images for specifier %s: %s" % (image_specifier, images)) return images[0] image = get_image(instances_blueprint.image()) instance_type = get_fitting_instance(self, instances_blueprint) for availability_zone, instance_num in zip( itertools.cycle(availability_zones), range(0, instance_count)): full_subnetwork_name = "%s-%s" % (network.name, service_name) instance_name = "%s-%s" % (full_subnetwork_name, instance_num) metadata = [{ "key": "startup-script", "value": instances_blueprint.runtime_scripts(template_vars) }, { "key": "network", "value": network.name }, { "key": "subnetwork", "value": service_name }] logger.info('Creating instance %s in zone %s', instance_name, availability_zone.name) self.driver.create_node(instance_name, instance_type, image, location=availability_zone, ex_network=network.name, ex_subnetwork=full_subnetwork_name, external_ip="ephemeral", ex_metadata=metadata, ex_tags=[full_subnetwork_name]) return self.get(network, service_name)