3
# Author: Jeff Bauer <jbauer@rubic.com>
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
import cloudinit.CloudConfig as cc
24
def handle(_name, cfg, _cloud, _log, _args):
25
# If there isn't a salt key in the configuration don't do anything
26
if 'salt_minion' not in cfg:
28
salt_cfg = cfg['salt_minion']
29
# Start by installing the salt package ...
30
cc.install_packages(("salt",))
31
config_dir = '/etc/salt'
32
if not os.path.isdir(config_dir):
33
os.makedirs(config_dir)
34
# ... and then update the salt configuration
35
if 'conf' in salt_cfg:
36
# Add all sections from the conf object to /etc/salt/minion
37
minion_config = os.path.join(config_dir, 'minion')
38
yaml.dump(salt_cfg['conf'],
39
file(minion_config, 'w'),
40
default_flow_style=False)
41
# ... copy the key pair if specified
42
if 'public_key' in salt_cfg and 'private_key' in salt_cfg:
43
pki_dir = '/etc/salt/pki'
44
cumask = os.umask(077)
45
if not os.path.isdir(pki_dir):
47
pub_name = os.path.join(pki_dir, 'minion.pub')
48
pem_name = os.path.join(pki_dir, 'minion.pem')
49
with open(pub_name, 'w') as f:
50
f.write(salt_cfg['public_key'])
51
with open(pem_name, 'w') as f:
52
f.write(salt_cfg['private_key'])
56
subprocess.check_call(['service', 'salt-minion', 'start'])