Merge pull request #46715 from pakdel/ec2_image_name

Salt Cloud EC2: Image Name
This commit is contained in:
Mike Place 2018-04-09 11:34:58 -06:00 committed by GitHub
commit c3bcc27712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View File

@ -721,6 +721,28 @@ them have never been used, much less tested, by the Salt Stack team.
.. __: https://aws.amazon.com/marketplace
NOTE: If ``image`` of a profile does not start with ``ami-``, latest
image with that name will be used. For example, to create a CentOS 7
profile, instead of using the AMI like ``image: ami-1caef165``, we
can use its name like ``image: 'CentOS Linux 7 x86_64 HVM EBS ENA 1803_01'``.
We can also use a pattern like below to get the latest CentOS 7:
.. code-block:: yaml
profile-id:
provider: provider-name
subnetid: subnet-XXXXXXXX
image: 'CentOS Linux 7 x86_64 HVM EBS *'
size: m1.medium
ssh_username: centos
securitygroupid:
- sg-XXXXXXXX
securitygroupname:
- AnotherSecurityGroup
- AndThirdSecurityGroup
show_image
==========
This is a function that describes an AMI on EC2. This will give insight as to

View File

@ -1191,6 +1191,33 @@ def get_tenancy(vm_):
)
def get_imageid(vm_):
'''
Returns the ImageId to use
'''
image = config.get_cloud_config_value(
'image', vm_, __opts__, search_global=False
)
if image.startswith('ami-'):
return image
# a poor man's cache
if not hasattr(get_imageid, 'images'):
get_imageid.images = {}
elif image in get_imageid.images:
return get_imageid.images[image]
params = {'Action': 'DescribeImages',
'Filter.0.Name': 'name',
'Filter.0.Value.0': image}
# Query AWS, sort by 'creationDate' and get the last imageId
_t = lambda x: datetime.datetime.strptime(x['creationDate'], '%Y-%m-%dT%H:%M:%S.%fZ')
image_id = sorted(aws.query(params, location=get_location(),
provider=get_provider(), opts=__opts__, sigver='4'),
lambda i, j: cmp(_t(i), _t(j))
)[-1]['imageId']
get_imageid.images[image] = image_id
return image_id
def _get_subnetname_id(subnetname):
'''
Returns the SubnetId of a SubnetName to use
@ -1774,7 +1801,7 @@ def request_instance(vm_=None, call=None):
# Normal instances should have no prefix.
spot_prefix = ''
image_id = vm_['image']
image_id = get_imageid(vm_)
params[spot_prefix + 'ImageId'] = image_id
userdata = None