Merge pull request #47742 from nickgarber/develop__wip-issue-40975-read-salt-cloud-maps-from-the-pillar

[WIP] feat: read Salt-Cloud Maps from the Pillar
This commit is contained in:
Daniel Wallace 2018-05-29 12:37:48 -05:00 committed by GitHub
commit cf1e5d4c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 15 deletions

View File

@ -173,8 +173,8 @@ Pillar Configuration
====================
It is possible to configure cloud providers using pillars. This is only used when inside the cloud
module. You can setup a variable called ``cloud`` that contains your profile and provider to pass
that information to the cloud servers instead of having to copy the full configuration to every
module. You can setup a variable called ``cloud`` that contains your profile, provider, and map to
pass that information to the cloud servers instead of having to copy the full configuration to every
minion. In your pillar file, you would use something like this:
.. code-block:: yaml
@ -198,6 +198,26 @@ minion. In your pillar file, you would use something like this:
image: CentOS 7
script_args: git develop
maps:
my-dev-map:
ubuntu-openstack:
- dev-test01
- dev-test02
- dev-test03
- dev-test04
my-prd-map:
ubuntu-openstack:
- prd-web01
- prd-web02
minion:
id: custom-minion-id-app1-stack1-frontend
grains:
roles:
- webserver
deployment: datacenter4-openstack
- prod-db01
- prod-db02
Cloud Configurations
====================
@ -345,7 +365,7 @@ OpenStack
---------
Using Salt for OpenStack uses the `shade <https://docs.openstack.org/shade/latest/>` driver managed by the
openstack-infra team.
openstack-infra team.
This driver can be configured using the ``/etc/openstack/clouds.yml`` file with
`os-client-config <https://docs.openstack.org/os-client-config/latest/>`

View File

@ -88,9 +88,9 @@ Any top level data element from your profile may be overridden in the map file:
fedora_small:
- web1:
size: t2.micro
size: t2.micro
- web2:
size: t2.nano
size: t2.nano
As of Salt 2017.7.0, nested elements are merged, and can can be specified
individually without having to repeat the complete definition for each top

View File

@ -203,6 +203,10 @@ class CloudClient(object):
profile['profile'] = name
self.opts['profiles'].update({name: profile})
self.opts['providers'][provider][driver]['profiles'].update({name: profile})
for name, map_dct in six.iteritems(pillars.pop('maps', {})):
if 'maps' not in self.opts:
self.opts['maps'] = {}
self.opts['maps'][name] = map_dct
self.opts.update(pillars)
def _opts_defaults(self, **kwargs):
@ -356,7 +360,7 @@ class CloudClient(object):
def map_run(self, path=None, **kwargs):
'''
Pass in a location for a map to execute
To execute a map
'''
kwarg = {}
if path:
@ -1760,20 +1764,31 @@ class Map(Cloud):
def read(self):
'''
Read in the specified map file and return the map structure
Read in the specified map and return the map structure
'''
map_ = None
if self.opts.get('map', None) is None:
if self.opts.get('map_data', None) is None:
return {}
if self.opts.get('map_pillar', None) is None:
pass
elif self.opts.get('map_pillar') not in self.opts.get('maps'):
log.error(
'The specified map not found in pillar at \'cloud:maps:{0}\''.format(
self.opts['map_pillar'])
)
raise SaltCloudNotFound()
else:
# 'map_pillar' is provided, try to use it
map_ = self.opts['maps'][self.opts.get('map_pillar')]
else:
# 'map_data' is provided, try to use it
map_ = self.opts['map_data']
if not map_:
else:
# 'map' is provided, try to use it
local_minion_opts = copy.deepcopy(self.opts)
local_minion_opts['file_client'] = 'local'
self.minion = salt.minion.MasterMinion(local_minion_opts)
if not os.path.isfile(self.opts['map']):
if not (self.opts['map']).startswith('salt://'):
log.error(
@ -1802,10 +1817,13 @@ class Map(Cloud):
)
return {}
if 'include' in map_:
map_ = salt.config.include_config(
map_, self.opts['map'], verbose=False
)
if 'include' in map_:
map_ = salt.config.include_config(
map_, self.opts['map'], verbose=False
)
if not map_:
return {}
# Create expected data format if needed
for profile, mapped in six.iteritems(map_.copy()):

View File

@ -204,11 +204,21 @@ def map_run(path=None, **kwargs):
'''
Execute a salt cloud map file
Cloud Map data can be retrieved from several sources:
- a local file (provide the path to the file to the 'path' argument)
- a JSON-formatted map directly (provide the appropriately formatted to using the 'map_data' argument)
- the Salt Pillar (provide the map name of under 'pillar:cloud:maps' to the 'map_pillar' argument)
Note: Only one of these sources can be read at a time. The options are listed in their order of precedence.
CLI Examples:
.. code-block:: bash
salt minionname cloud.map_run /path/to/cloud.map
salt minionname cloud.map_run path=/path/to/cloud.map
salt minionname cloud.map_run map_pillar='<map_pillar>'
.. versionchanged:: 2018.3.1
salt minionname cloud.map_run map_data='<actual map data>'
'''
client = _get_client()