def _get_health_data(self): timestamp = datetime.now(tz.tzutc()) env = elasticbeanstalk.get_environment(app_name=self.app_name, env_name=self.env_name) env_dict = elasticbeanstalk.get_environment_resources(self.env_name) env_dict = env_dict['EnvironmentResources'] load_balancers = env_dict.get('LoadBalancers', None) if load_balancers and len(load_balancers) > 0: load_balancer_name = env_dict.get('LoadBalancers')[0].get('Name') instance_states = elb.get_health_of_instances(load_balancer_name) else: instance_states = [] instance_ids = [i['Id'] for i in env_dict.get('Instances', [])] total_instances = len(instance_ids) total_in_service = len( [i for i in instance_states if i['State'] == 'InService']) env_data = { 'EnvironmentName': env.name, 'Color': env.health, 'Status': env.status, 'Total': total_instances, 'InService': total_in_service, 'Other': total_instances - total_in_service } data = {'environment': env_data, 'instances': []} # Get Instance Health for i in instance_states: instance = { 'id': i['InstanceId'], 'state': i['State'], 'description': i['Description'] } ec2_health = ec2.describe_instance(instance['id']) instance['health'] = ec2_health['State']['Name'] data['instances'].append(instance) # Get Health for instances not in Load Balancer yet for i in instance_ids: instance = {'id': i} if i not in [x['InstanceId'] for x in instance_states]: instance['description'] = 'N/A (Not registered ' \ 'with Load Balancer)' instance['state'] = 'n/a' ec2_health = ec2.describe_instance(i) instance['health'] = ec2_health['State']['Name'] data['instances'].append(instance) data['environment']['RefreshedAt'] = timestamp return data
def get_instance_health(self, instance_states): instance_healths = [] for instance_state in instance_states: instance = { 'id': instance_state['InstanceId'], 'state': instance_state['State'], 'description': instance_state['Description'] } ec2_health = ec2.describe_instance(instance['id']) instance['health'] = ec2_health['State']['Name'] instance_healths.append(instance) return instance_healths
def get_health_information_of_instance_not_associated_with_elb( self, ids_of_all_instances, instances_registered_with_elb): instance_healths = [] ids_of_all_instances = set(ids_of_all_instances) ids_of_instances_registered_with_elb = set( [x['InstanceId'] for x in instances_registered_with_elb]) for instance_id in list(ids_of_all_instances - ids_of_instances_registered_with_elb): instance = dict([('id', instance_id)]) instance['description'] = 'N/A (Not registered with Load Balancer)' instance['state'] = 'n/a' ec2_health = ec2.describe_instance(instance_id) instance['health'] = ec2_health['State']['Name'] instance_healths.append(instance) return instance_healths
def get_health_information_of_instance_not_associated_with_elb( self, ids_of_all_instances, instances_registered_with_elb ): instance_healths = [] ids_of_all_instances = set(ids_of_all_instances) ids_of_instances_registered_with_elb = set([x['InstanceId'] for x in instances_registered_with_elb]) for instance_id in list(ids_of_all_instances - ids_of_instances_registered_with_elb): instance = dict([('id', instance_id)]) instance['description'] = 'N/A (Not registered with Load Balancer)' instance['state'] = 'n/a' ec2_health = ec2.describe_instance(instance_id) instance['health'] = ec2_health['State']['Name'] instance_healths.append(instance) return instance_healths
def ssh_into_instance(instance_id, keep_open=False, force_open=False, custom_ssh=None, command=None): instance = ec2.describe_instance(instance_id) try: keypair_name = instance['KeyName'] except KeyError: raise NoKeypairError() try: ip = instance['PrivateIpAddress'] except KeyError: if 'PrivateIpAddress' in instance: ip = instance['PrivateIpAddress'] else: raise NotFoundError(strings['ssh.noip']) security_groups = instance['SecurityGroups'] user = '******' ssh_group = None has_restriction = False rule_existed_before = False group_id = None for group in security_groups: group_id = group['GroupId'] group = ec2.describe_security_group(group_id) for permission in group.get('IpPermissions', []): if permission.get('ToPort', None) == 22: ssh_group = group_id for rng in permission.get('IpRanges', []): ip_restriction = rng.get('CidrIp', None) if ip_restriction is not None: if ip_restriction != '0.0.0.0/0': has_restriction = True elif ip_restriction == '0.0.0.0/0': rule_existed_before = True if has_restriction and not force_open: io.log_warning(strings['ssh.notopening']) elif group_id: io.echo(strings['ssh.openingport']) ec2.authorize_ssh(ssh_group or group_id) io.echo(strings['ssh.portopen']) try: if custom_ssh: custom_ssh = custom_ssh.split() else: ident_file = _get_ssh_file(keypair_name) custom_ssh = ['ssh', '-i', ident_file] custom_ssh.extend([user + '@' + ip]) if command: custom_ssh.extend(command.split()) io.echo('INFO: Running ' + ' '.join(custom_ssh)) returncode = subprocess.call(custom_ssh) if returncode != 0: LOG.debug(custom_ssh[0] + ' returned exitcode: ' + str(returncode)) raise CommandError('An error occurred while running: ' + custom_ssh[0] + '.') except OSError: CommandError(strings['ssh.notpresent']) finally: if keep_open: pass elif (not has_restriction or force_open) and group_id and not rule_existed_before: ec2.revoke_ssh(ssh_group or group_id) io.echo(strings['ssh.closeport'])