~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to cloudinit/CloudConfig/cc_salt_minion.py

  • Committer: Scott Moser
  • Date: 2012-07-06 21:24:18 UTC
  • mfrom: (1.1.26)
  • Revision ID: smoser@ubuntu.com-20120706212418-zwcpwxsdodi0pms3
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vi: ts=4 expandtab
2
 
#
3
 
#    Author: Jeff Bauer <jbauer@rubic.com>
4
 
#
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.
8
 
#
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.
13
 
#
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/>.
16
 
 
17
 
import os
18
 
import os.path
19
 
import subprocess
20
 
import cloudinit.CloudConfig as cc
21
 
import yaml
22
 
 
23
 
 
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:
27
 
        return
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):
46
 
            os.makedirs(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'])
53
 
        os.umask(cumask)
54
 
 
55
 
    # Start salt-minion
56
 
    subprocess.check_call(['service', 'salt-minion', 'start'])