def _run(self, transport, command): channel = transport.open_session() try: channel.exec_command(command) # We don't want a stdin channel.makefile('wb', -1).close() # FIXME: It would be better to block with select exit_status_ready = channel.exit_status_ready() while not exit_status_ready: while channel.recv_ready(): print(channel.recv(1024)) while channel.recv_stderr_ready(): print(channel.recv_stderr(1024)) time.sleep(1) exit_status_ready = channel.exit_status_ready() while channel.recv_ready(): print(channel.recv(1024)) while channel.recv_stderr_ready(): print(channel.recv_stderr(1024)) exit_code = channel.recv_exit_status() if exit_code != 0: raise errors.Error( "Bundle deployment failed with exit code: {}".format( exit_code)) finally: channel.close()
def run(self): vpc = self.runner.get_plan(self.resource.vpc) if not vpc: return for i in range(120): interfaces = self.plan.client.describe_network_interfaces(Filters=[ { "Name": "vpc-id", "Values": [vpc.resource_id] }, { "Name": "subnet-id", "Values": [self.plan.resource_id] }, ]).get('NetworkInterfaces', []) if not interfaces: return for interface in interfaces: if not self.check_interface(interface): raise errors.Error( "Subnet {} cannot be deleted until network interface {} ({}) is removed\n{}" .format( self.plan.resource_id, interface["NetworkInterfaceId"], interface.get("Description", "No description available"), interface)) time.sleep(1)
def get_actions(self): if not fuselage: raise errors.Error( "You need the fuselage package to use the fuselage_bundle resource" ) yield Deployment(self)
def get_serializer(self, runner): plan = runner.get_plan(self.adapts) # Annoyingly we have to get antother client (different API) to get info # on teh EC2 instances in our asg client = plan.session.create_client( service_name="ec2", region_name=plan.session.region, aws_access_key_id=plan.session.access_key_id, aws_secret_access_key=plan.session.secret_access_key, aws_session_token=plan.session.session_token, ) reservations = client.describe_instances(InstanceIds=[ i["InstanceId"] for i in plan.object.get("Instances", []) ], ).get("Reservations", []) instances = [] for reservation in reservations: instances.extend(reservation.get("Instances", [])) if len(instances) == 0: raise errors.Error("No instances available in {}".format( self.adapts)) if hasattr( self.parent, "proxy" ) and self.parent.proxy and self.parent.proxy.instance and self.parent.proxy.instance.adapts.subnets[ 0].vpc == self.adapts.subnets[0].vpc: for instance in instances: if 'PrivateIpAddress' in instance: return serializers.Const(instance['PrivateIpAddress']) for instance in instances: for k in ('PublicDnsName', 'PublicIpAddress'): if k in instance and instance[k]: return serializers.Const(instance[k]) raise errors.Error( "No instances available in {} with ip address".format(self.adapts))
def run(self): self.suspend_processes() try: self.scale() try: # self.remove_from_balancer() self.terminate_instance() if not self.wait_for_healthy_asg(): raise errors.Error( "Auto scaling group {} is not returning to a healthy state" .format(self.resource.name)) finally: self.unscale() finally: self.resume_processes()
def describe_object(self): if self.get_action: logger.debug( "Trying to find AWS object for resource {} using {}".format( self.resource, self.get_action)) try: result = getattr(self.client, self.get_action)(**{ self.key: self.resource.name }) except ClientError as e: if e.response['Error']['Code'] == self.get_notfound_exception: return {} raise return result[self.get_key] logger.debug( "Trying to find AWS object for resource {} using {}".format( self.resource, self.describe_action)) filters = self.get_describe_filters() if not filters: logger.debug( "Could not generate valid filters - this generally means we've determined the object cant exist!" ) return {} logger.debug("Filters are: {}".format(filters)) try: results = getattr(self.client, self.describe_action)(**filters) except ClientError as e: if e.response['Error']['Code'] == self.describe_notfound_exception: return {} raise objects = results[self.describe_list_key] if len(objects) > 1: raise errors.Error("Expecting to find one {}, but found {}".format( self.resource, len(objects))) if len(objects) == 1: logger.debug("Found object {}".format(objects[0][self.key])) return objects[0] return {}
def run(self): self.plan.object = self.plan.describe_object() if not self.plan.object: raise errors.Error("Object creation failed")
def wrap(cls, parent, resource): for adapter in cls.__subclasses__(): if adapter.input and isinstance(resource, adapter.input): return adapter(parent, adapts=resource) raise errors.Error("Cannot turn {} into a {}".format(resource, cls))
def get_actions(self): if not paramiko: raise errors.Error( "Paramiko library is required to perform operations involving ssh" ) return []
def render(self, runner, object): target = self.inner.render(runner, object) target_plan = runner.get_service(target, "takeoff::build-workspace") if self.property not in target_plan.object: raise errors.Error("{} not available".format(self.property)) return target_plan.object[self.property]