Blame cloudinit/config/cc_salt_minion.py

Packit Service a04d08
# Author: Jeff Bauer <jbauer@rubic.com>
Packit Service a04d08
#
Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service a04d08
"""
Packit Service a04d08
Salt Minion
Packit Service a04d08
-----------
Packit Service a04d08
**Summary:** set up and run salt minion
Packit Service a04d08
Packit Service a04d08
This module installs, configures and starts salt minion. If the ``salt_minion``
Packit Service a04d08
key is present in the config parts, then salt minion will be installed and
Packit Service a04d08
started. Configuration for salt minion can be specified in the ``conf`` key
Packit Service a04d08
under ``salt_minion``. Any conf values present there will be assigned in
Packit Service a04d08
``/etc/salt/minion``. The public and private keys to use for salt minion can be
Packit Service a04d08
specified with ``public_key`` and ``private_key`` respectively. Optionally if
Packit Service a04d08
you have a custom package name, service name or config directory you can
Packit Service a04d08
specify them with ``pkg_name``, ``service_name`` and ``config_dir``.
Packit Service a04d08
Packit Service a04d08
**Internal name:** ``cc_salt_minion``
Packit Service a04d08
Packit Service a04d08
**Module frequency:** per instance
Packit Service a04d08
Packit Service a04d08
**Supported distros:** all
Packit Service a04d08
Packit Service a04d08
**Config keys**::
Packit Service a04d08
Packit Service a04d08
    salt_minion:
Packit Service a04d08
        pkg_name: 'salt-minion'
Packit Service a04d08
        service_name: 'salt-minion'
Packit Service a04d08
        config_dir: '/etc/salt'
Packit Service a04d08
        conf:
Packit Service a04d08
            master: salt.example.com
Packit Service a04d08
        grains:
Packit Service a04d08
            role:
Packit Service a04d08
                - web
Packit Service a04d08
        public_key: |
Packit Service a04d08
            ------BEGIN PUBLIC KEY-------
Packit Service a04d08
            <key data>
Packit Service a04d08
            ------END PUBLIC KEY-------
Packit Service a04d08
        private_key: |
Packit Service a04d08
            ------BEGIN PRIVATE KEY------
Packit Service a04d08
            <key data>
Packit Service a04d08
            ------END PRIVATE KEY-------
Packit Service a04d08
"""
Packit Service a04d08
Packit Service a04d08
import os
Packit Service a04d08
Packit Service 751c4a
from cloudinit import safeyaml, subp, util
Packit Service 751c4a
from cloudinit.distros import rhel_util
Packit Service 751c4a
Packit Service a04d08
Packit Service a04d08
# Note: see https://docs.saltstack.com/en/latest/topics/installation/
Packit Service a04d08
# Note: see https://docs.saltstack.com/en/latest/ref/configuration/
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
class SaltConstants(object):
Packit Service a04d08
    """
Packit Service a04d08
    defines default distribution specific salt variables
Packit Service a04d08
    """
Packit Service a04d08
    def __init__(self, cfg):
Packit Service a04d08
Packit Service a04d08
        # constants tailored for FreeBSD
Packit Service a04d08
        if util.is_FreeBSD():
Packit Service a04d08
            self.pkg_name = 'py36-salt'
Packit Service a04d08
            self.srv_name = 'salt_minion'
Packit Service a04d08
            self.conf_dir = '/usr/local/etc/salt'
Packit Service a04d08
        # constants for any other OS
Packit Service a04d08
        else:
Packit Service a04d08
            self.pkg_name = 'salt-minion'
Packit Service a04d08
            self.srv_name = 'salt-minion'
Packit Service a04d08
            self.conf_dir = '/etc/salt'
Packit Service a04d08
Packit Service a04d08
        # if there are constants given in cloud config use those
Packit Service a04d08
        self.pkg_name = util.get_cfg_option_str(cfg, 'pkg_name',
Packit Service a04d08
                                                self.pkg_name)
Packit Service a04d08
        self.conf_dir = util.get_cfg_option_str(cfg, 'config_dir',
Packit Service a04d08
                                                self.conf_dir)
Packit Service a04d08
        self.srv_name = util.get_cfg_option_str(cfg, 'service_name',
Packit Service a04d08
                                                self.srv_name)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
def handle(name, cfg, cloud, log, _args):
Packit Service a04d08
    # If there isn't a salt key in the configuration don't do anything
Packit Service a04d08
    if 'salt_minion' not in cfg:
Packit Service a04d08
        log.debug(("Skipping module named %s,"
Packit Service a04d08
                   " no 'salt_minion' key in configuration"), name)
Packit Service a04d08
        return
Packit Service a04d08
Packit Service a04d08
    s_cfg = cfg['salt_minion']
Packit Service a04d08
    const = SaltConstants(cfg=s_cfg)
Packit Service a04d08
Packit Service a04d08
    # Start by installing the salt package ...
Packit Service a04d08
    cloud.distro.install_packages(const.pkg_name)
Packit Service a04d08
Packit Service a04d08
    # Ensure we can configure files at the right dir
Packit Service a04d08
    util.ensure_dir(const.conf_dir)
Packit Service a04d08
Packit Service a04d08
    # ... and then update the salt configuration
Packit Service a04d08
    if 'conf' in s_cfg:
Packit Service a04d08
        # Add all sections from the conf object to minion config file
Packit Service a04d08
        minion_config = os.path.join(const.conf_dir, 'minion')
Packit Service a04d08
        minion_data = safeyaml.dumps(s_cfg.get('conf'))
Packit Service a04d08
        util.write_file(minion_config, minion_data)
Packit Service a04d08
Packit Service a04d08
    if 'grains' in s_cfg:
Packit Service a04d08
        # add grains to /etc/salt/grains
Packit Service a04d08
        grains_config = os.path.join(const.conf_dir, 'grains')
Packit Service a04d08
        grains_data = safeyaml.dumps(s_cfg.get('grains'))
Packit Service a04d08
        util.write_file(grains_config, grains_data)
Packit Service a04d08
Packit Service a04d08
    # ... copy the key pair if specified
Packit Service a04d08
    if 'public_key' in s_cfg and 'private_key' in s_cfg:
Packit Service a04d08
        pki_dir_default = os.path.join(const.conf_dir, "pki/minion")
Packit Service a04d08
        if not os.path.isdir(pki_dir_default):
Packit Service a04d08
            pki_dir_default = os.path.join(const.conf_dir, "pki")
Packit Service a04d08
Packit Service a04d08
        pki_dir = s_cfg.get('pki_dir', pki_dir_default)
Packit Service a04d08
        with util.umask(0o77):
Packit Service a04d08
            util.ensure_dir(pki_dir)
Packit Service a04d08
            pub_name = os.path.join(pki_dir, 'minion.pub')
Packit Service a04d08
            pem_name = os.path.join(pki_dir, 'minion.pem')
Packit Service a04d08
            util.write_file(pub_name, s_cfg['public_key'])
Packit Service a04d08
            util.write_file(pem_name, s_cfg['private_key'])
Packit Service a04d08
Packit Service a04d08
    # we need to have the salt minion service enabled in rc in order to be
Packit Service a04d08
    # able to start the service. this does only apply on FreeBSD servers.
Packit Service a04d08
    if cloud.distro.osfamily == 'freebsd':
Packit Service 751c4a
        rhel_util.update_sysconfig_file(
Packit Service 751c4a
            '/etc/rc.conf', {'salt_minion_enable': 'YES'})
Packit Service a04d08
Packit Service a04d08
    # restart salt-minion. 'service' will start even if not started. if it
Packit Service a04d08
    # was started, it needs to be restarted for config change.
Packit Service 751c4a
    subp.subp(['service', const.srv_name, 'restart'], capture=False)
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab