Update list of EC2 Instances for the account / region

        Returns:
            `None`
504632
def update_instances(self):
        
        self.log.debug('Updating EC2Instances for {}/{}'.format(self.account.account_name, self.region))
        ec2 = self.session.resource('ec2', region_name=self.region)

        try:
            existing_instances = EC2Instance.get_all(self.account, self.region)
            instances = {}
            api_instances = {x.id: x for x in ec2.instances.all()}

            try:
                for instance_id, data in api_instances.items():
                    if data.instance_id in existing_instances:
                        instance = existing_instances[instance_id]

                        if data.state['Name'] not in ('terminated', 'shutting-down'):
                            instances[instance_id] = instance

                            # Add object to transaction if it changed
                            if instance.update(data):
                                self.log.debug('Updating info for instance {} in {}/{}'.format(
                                    instance.resource.resource_id,
                                    self.account.account_name,
                                    self.region
                                ))
                                db.session.add(instance.resource)
                    else:
                        # New instance, if its not in state=terminated
                        if data.state['Name'] in ('terminated', 'shutting-down'):
                            continue

                        tags = {tag['Key']: tag['Value'] for tag in data.tags or {}}
                        properties = {
                            'launch_date': to_utc_date(data.launch_time).isoformat(),
                            'state': data.state['Name'],
                            'instance_type': data.instance_type,
                            'public_ip': getattr(data, 'public_ip_address', None),
                            'public_dns': getattr(data, 'public_dns_address', None),
                            'created': isoformat(datetime.now())
                        }

                        instance = EC2Instance.create(
                            data.instance_id,
                            account_id=self.account.account_id,
                            location=self.region,
                            properties=properties,
                            tags=tags
                        )

                        instances[instance.resource.resource_id] = instance
                        self.log.debug('Added new EC2Instance {}/{}/{}'.format(
                            self.account.account_name,
                            self.region,
                            instance.resource.resource_id
                        ))

                # Check for deleted instances
                ik = set(list(instances.keys()))
                eik = set(list(existing_instances.keys()))

                for instanceID in eik - ik:
                    db.session.delete(existing_instances[instanceID].resource)
                    self.log.debug('Deleted EC2Instance {}/{}/{}'.format(
                        self.account.account_name,
                        self.region,
                        instanceID
                    ))

                db.session.commit()
            except:
                db.session.rollback()
                raise
        finally:
            del ec2
939081
def down_ec2(instance_id, region, access_key_id, secret_access_key):
    
    conn = connect_to_ec2(region, access_key_id, secret_access_key)
    # get the instance_id from the state file, and stop the instance
    instance = conn.stop_instances(instance_ids=instance_id)[0]
    while instance.state != "stopped":
        log_yellow("Instance state: %s" % instance.state)
        sleep(10)
        instance.update()
    log_green('Instance state: %s' % instance.state)
968674
def destroy_ec2(region, instance_id, access_key_id, secret_access_key):
    
    conn = connect_to_ec2(region, access_key_id, secret_access_key)

    data = get_ec2_info(instance_id=instance_id,
                        region=region,
                        access_key_id=access_key_id,
                        secret_access_key=secret_access_key,
                        username=None)

    instance = conn.terminate_instances(instance_ids=[data['id']])[0]
    log_yellow('destroying instance ...')
    while instance.state != "terminated":
        log_yellow("Instance state: %s" % instance.state)
        sleep(10)
        instance.update()
    volume_id = data['volume']
    if volume_id:
        destroy_ebs_volume(region, volume_id, access_key_id,
                           secret_access_key)
    os.unlink('data.json')
421448
def _update_limits_from_api(self):
        
        self.connect()
        logger.info("Querying EC2 DescribeAccountAttributes for limits")
        lims = self.conn.describe_account_limits()
        self.limits['Auto Scaling groups']._set_api_limit(
            lims['MaxNumberOfAutoScalingGroups'])
        self.limits['Launch configurations']._set_api_limit(
            lims['MaxNumberOfLaunchConfigurations'])
1369137
def update(self):
        
        ring = self._fetch()
        n_replicas = len(ring)
        replica_set = set([r[1] for r in self.replicas])
        self.ranges = []
        for n, (start, replica) in enumerate(ring):
            if replica in replica_set:
                end = ring[(n+1) % n_replicas][0] % RING_SIZE
                if start < end:
                    self.ranges.append((start, end))
                elif end < start:
                    self.ranges.append((start, RING_SIZE))
                    self.ranges.append((0, end))
                else:
                    self.ranges.append((0, RING_SIZE))
803813
def update_beanstalks(self):
        
        self.log.debug('Updating ElasticBeanStalk environments for {}/{}'.format(
            self.account.account_name,
            self.region
        ))
        ebclient = self.session.client('elasticbeanstalk', region_name=self.region)

        try:
            existing_beanstalks = BeanStalk.get_all(self.account, self.region)
            beanstalks = {}
            # region Fetch elastic beanstalks
            for env in ebclient.describe_environments()['Environments']:
                # Only get information for HTTP (non-worker) Beanstalks
                if env['Tier']['Type'] == 'Standard':
                    if 'CNAME' in env:
                        beanstalks[env['EnvironmentId']] = {
                            'id': env['EnvironmentId'],
                            'environment_name': env['EnvironmentName'],
                            'application_name': env['ApplicationName'],
                            'cname': env['CNAME']
                        }
                    else:
                        self.log.warning('Found a BeanStalk that does not have a CNAME: {} in {}/{}'.format(
                            env['EnvironmentName'],
                            self.account,
                            self.region
                        ))
                else:
                    self.log.debug('Skipping worker tier ElasticBeanstalk environment {}/{}/{}'.format(
                        self.account.account_name,
                        self.region,
                        env['EnvironmentName']
                    ))
            # endregion

            try:
                for data in beanstalks.values():
                    if data['id'] in existing_beanstalks:
                        beanstalk = existing_beanstalks[data['id']]
                        if beanstalk.update(data):
                            self.log.debug('Change detected for ElasticBeanStalk {}/{}/{}'.format(
                                self.account.account_name,
                                self.region,
                                data['id']
                            ))
                    else:
                        bid = data.pop('id')
                        tags = {}
                        BeanStalk.create(
                            bid,
                            account_id=self.account.account_id,
                            location=self.region,
                            properties=data,
                            tags=tags
                        )

                        self.log.debug('Added new ElasticBeanStalk {}/{}/{}'.format(
                            self.account.account_name,
                            self.region,
                            bid
                        ))
                db.session.commit()

                bk = set(beanstalks.keys())
                ebk = set(existing_beanstalks.keys())

                for resource_id in ebk - bk:
                    db.session.delete(existing_beanstalks[resource_id].resource)
                    self.log.debug('Deleted ElasticBeanStalk {}/{}/{}'.format(
                        self.account.account_name,
                        self.region,
                        resource_id
                    ))
                db.session.commit()
            except:
                db.session.rollback()

            return beanstalks
        finally:
            del ebclient
313566
def update(name, launch_config_name, availability_zones, min_size, max_size,
           desired_capacity=None, load_balancers=None, default_cooldown=None,
           health_check_type=None, health_check_period=None,
           placement_group=None, vpc_zone_identifier=None, tags=None,
           termination_policies=None, suspended_processes=None,
           scaling_policies=None, scheduled_actions=None,
           notification_arn=None, notification_types=None,
           region=None, key=None, keyid=None, profile=None):
    '''
    
    '''

    conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
    conn3 = _get_conn_autoscaling_boto3(region=region, key=key, keyid=keyid,
                                        profile=profile)
    if not conn:
        return False, "failed to connect to AWS"
    if isinstance(availability_zones, six.string_types):
        availability_zones = salt.utils.json.loads(availability_zones)
    if isinstance(load_balancers, six.string_types):
        load_balancers = salt.utils.json.loads(load_balancers)
    if isinstance(vpc_zone_identifier, six.string_types):
        vpc_zone_identifier = salt.utils.json.loads(vpc_zone_identifier)
    if isinstance(tags, six.string_types):
        tags = salt.utils.json.loads(tags)
    if isinstance(termination_policies, six.string_types):
        termination_policies = salt.utils.json.loads(termination_policies)
    if isinstance(suspended_processes, six.string_types):
        suspended_processes = salt.utils.json.loads(suspended_processes)
    if isinstance(scheduled_actions, six.string_types):
        scheduled_actions = salt.utils.json.loads(scheduled_actions)

    # Massage our tagset into  add / remove lists
    # Use a boto3 call here b/c the boto2 call doeesn't implement filters
    current_tags = conn3.describe_tags(Filters=[{'Name': 'auto-scaling-group',
                                      'Values': [name]}]).get('Tags', [])
    current_tags = [{'key': t['Key'],
                     'value': t['Value'],
                     'resource_id': t['ResourceId'],
                     'propagate_at_launch': t.get('PropagateAtLaunch', False)}
                          for t in current_tags]
    add_tags = []
    desired_tags = []
    if tags:
        tags = __utils__['boto3.ordered'](tags)
        for tag in tags:
            try:
                key = tag.get('key')
            except KeyError:
                log.error('Tag missing key.')
                return False, "Tag {0} missing key".format(tag)
            try:
                value = tag.get('value')
            except KeyError:
                log.error('Tag missing value.')
                return False, "Tag {0} missing value".format(tag)
            propagate_at_launch = tag.get('propagate_at_launch', False)
            _tag = {'key': key,
                    'value': value,
                    'resource_id': name,
                    'propagate_at_launch': propagate_at_launch}
            if _tag not in current_tags:
                add_tags.append(_tag)
            desired_tags.append(_tag)
    delete_tags = [t for t in current_tags if t not in desired_tags]

    retries = 30
    while True:
        try:
            _asg = autoscale.AutoScalingGroup(
                connection=conn,
                name=name, launch_config=launch_config_name,
                availability_zones=availability_zones,
                min_size=min_size, max_size=max_size,
                desired_capacity=desired_capacity, load_balancers=load_balancers,
                default_cooldown=default_cooldown,
                health_check_type=health_check_type,
                health_check_period=health_check_period,
                placement_group=placement_group, tags=add_tags,
                vpc_zone_identifier=vpc_zone_identifier,
                termination_policies=termination_policies)
            if notification_arn and notification_types:
                conn.put_notification_configuration(_asg, notification_arn, notification_types)
            _asg.update()
            # Seems the update call doesn't handle tags, so we'll need to update
            # that separately.
            if add_tags:
                log.debug('Adding/updating tags from ASG: %s', add_tags)
                conn.create_or_update_tags([autoscale.Tag(**t) for t in add_tags])
            if delete_tags:
                log.debug('Deleting tags from ASG: %s', delete_tags)
                conn.delete_tags([autoscale.Tag(**t) for t in delete_tags])
            # update doesn't handle suspended_processes either
            # Resume all processes
            _asg.resume_processes()
            # suspend any that are specified.  Note that the boto default of empty
            # list suspends all; don't do that.
            if suspended_processes:
                _asg.suspend_processes(suspended_processes)
            log.info('Updated ASG %s', name)
            # ### scaling policies
            # delete all policies, then recreate them
            for policy in conn.get_all_policies(as_group=name):
                conn.delete_policy(policy.name, autoscale_group=name)
            _create_scaling_policies(conn, name, scaling_policies)
            # ### scheduled actions
            # delete all scheduled actions, then recreate them
            for scheduled_action in conn.get_all_scheduled_actions(as_group=name):
                conn.delete_scheduled_action(
                    scheduled_action.name, autoscale_group=name
                )
            _create_scheduled_actions(conn, name, scheduled_actions)
            return True, ''
        except boto.exception.BotoServerError as e:
            if retries and e.code == 'Throttling':
                log.debug('Throttled by AWS API, retrying in 5 seconds...')
                time.sleep(5)
                retries -= 1
                continue
            log.error(e)
            msg = 'Failed to update ASG {0}'.format(name)
            log.error(msg)
            return False, six.text_type(e)
1086109
def update_storage(kwargs=None, conn=None, call=None):
    '''
    
    '''
    if call != 'function':
        raise SaltCloudSystemExit(
            'The show_storage function must be called with -f or --function.'
        )

    if not conn:
        conn = get_conn()

    if kwargs is None:
        kwargs = {}

    if 'name' not in kwargs:
        raise SaltCloudSystemExit('A name must be specified as "name"')

    data = conn.update_storage_account(
        service_name=kwargs['name'],
        label=kwargs.get('label', None),
        description=kwargs.get('description', None),
        extended_properties=kwargs.get('extended_properties', None),
        geo_replication_enabled=kwargs.get('geo_replication_enabled', None),
        account_type=kwargs.get('account_type', 'Standard_GRS'),
    )
    return show_storage(kwargs={'name': kwargs['name']}, call='function')
41299
def save_ec2_state_locally(instance_id,
                           region,
                           username,
                           access_key_id,
                           secret_access_key):
    
    # retrieve the IP information from the instance
    data = get_ec2_info(instance_id,
                        region,
                        access_key_id,
                        secret_access_key,
                        username)
    return _save_state_locally(data)
203158
def update(self, instance, condition):
        
        item = self.dbi.get(condition)
        if item is None:
            return None
        item.update(instance.as_dict())
        self.dbi.update(item, condition)
        return item.eid
514379
def update(self, volume_id, **kwargs):
        
        # These arguments are allowed
        self.allowed('update', kwargs,
                     ['status', 'instance_id',
                      'mountpoint', 'ip', 'initiator', 'session_ip',
                      'session_initiator'])
        # Remove parameters that are None
        params = self.unused(kwargs)
        return self.http_post('/volumes/%s/export' % volume_id, params=params)
787544
def update(cluster)
      deployed = primary_deployment(cluster)
      Ecs.update_service(cluster.name, name, register(deployed))
      name
    end
394158
def run(self):
        
        # Loop through all accounts that are marked as enabled
        accounts = list(AWSAccount.get_all(include_disabled=False).values())
        for account in accounts:
            self.log.debug('Updating VPC Flow Logs for {}'.format(account))

            self.session = get_aws_session(account)
            role_arn = self.confirm_iam_role(account)
            # region specific
            for aws_region in AWS_REGIONS:
                try:
                    vpc_list = VPC.get_all(account, aws_region).values()
                    need_vpc_flow_logs = [x for x in vpc_list if x.vpc_flow_logs_status != 'ACTIVE']

                    for vpc in need_vpc_flow_logs:
                        if self.confirm_cw_log(account, aws_region, vpc.id):
                            self.create_vpc_flow_logs(account, aws_region, vpc.id, role_arn)
                        else:
                            self.log.info('Failed to confirm log group for {}/{}'.format(
                                account,
                                aws_region
                            ))

                except Exception:
                    self.log.exception('Failed processing VPCs for {}/{}.'.format(
                        account,
                        aws_region
                    ))

            db.session.commit()
709772
def list_ec2(region, filter_by_kwargs):
    """"""
    conn = boto.ec2.connect_to_region(region)
    instances = conn.get_only_instances()
    return lookup(instances, filter_by=filter_by_kwargs)
689960
def set_instances(name, instances, test=False, region=None, key=None, keyid=None,
                         profile=None):
    '''
    
    '''
    ret = True
    current = set([i['instance_id'] for i in get_instance_health(name, region, key, keyid, profile)])
    desired = set(instances)
    add = desired - current
    remove = current - desired
    if test:
        return bool(add or remove)
    if remove:
        if deregister_instances(name, list(remove), region, key, keyid, profile) is False:
            ret = False
    if add:
        if register_instances(name, list(add), region, key, keyid, profile) is False:
            ret = False
    return ret
1005818
def list_instances(self, filter=None):
        
        gce = self._connect()

        try:
            request = gce.instances().list(
                project=self._project_id, filter=filter, zone=self._zone)
            response = self._execute_request(request)
            self._check_response(response)
        except (HttpError, CloudProviderError) as e:
            raise InstanceError("could not retrieve all instances on the "
                                "cloud: ``" % e)

        if response and 'items' in response:
            return response['items']
        else:
            return list()
1225633
def update_user(self, instance, user, name=None, password=None, host=None):
        
        return instance.update_user(user, name=name, password=password,
                host=host)