~brad-marshall/charms/trusty/apache2-wsgi/fix-haproxy-relations

« back to all changes in this revision

Viewing changes to hooks/lib/charmhelpers/contrib/saltstack/__init__.py

  • Committer: Robin Winslow
  • Date: 2014-05-27 14:00:44 UTC
  • Revision ID: robin.winslow@canonical.com-20140527140044-8rpmb3wx4djzwa83
Add all files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Charm Helpers saltstack - declare the state of your machines.
 
2
 
 
3
This helper enables you to declare your machine state, rather than
 
4
program it procedurally (and have to test each change to your procedures).
 
5
Your install hook can be as simple as:
 
6
 
 
7
{{{
 
8
from charmhelpers.contrib.saltstack import (
 
9
    install_salt_support,
 
10
    update_machine_state,
 
11
)
 
12
 
 
13
 
 
14
def install():
 
15
    install_salt_support()
 
16
    update_machine_state('machine_states/dependencies.yaml')
 
17
    update_machine_state('machine_states/installed.yaml')
 
18
}}}
 
19
 
 
20
and won't need to change (nor will its tests) when you change the machine
 
21
state.
 
22
 
 
23
It's using a python package called salt-minion which allows various formats for
 
24
specifying resources, such as:
 
25
 
 
26
{{{
 
27
/srv/{{ basedir }}:
 
28
    file.directory:
 
29
        - group: ubunet
 
30
        - user: ubunet
 
31
        - require:
 
32
            - user: ubunet
 
33
        - recurse:
 
34
            - user
 
35
            - group
 
36
 
 
37
ubunet:
 
38
    group.present:
 
39
        - gid: 1500
 
40
    user.present:
 
41
        - uid: 1500
 
42
        - gid: 1500
 
43
        - createhome: False
 
44
        - require:
 
45
            - group: ubunet
 
46
}}}
 
47
 
 
48
The docs for all the different state definitions are at:
 
49
    http://docs.saltstack.com/ref/states/all/
 
50
 
 
51
 
 
52
TODO:
 
53
  * Add test helpers which will ensure that machine state definitions
 
54
    are functionally (but not necessarily logically) correct (ie. getting
 
55
    salt to parse all state defs.
 
56
  * Add a link to a public bootstrap charm example / blogpost.
 
57
  * Find a way to obviate the need to use the grains['charm_dir'] syntax
 
58
    in templates.
 
59
"""
 
60
# Copyright 2013 Canonical Ltd.
 
61
#
 
62
# Authors:
 
63
#  Charm Helpers Developers <juju@lists.ubuntu.com>
 
64
import subprocess
 
65
 
 
66
import charmhelpers.contrib.templating.contexts
 
67
import charmhelpers.core.host
 
68
import charmhelpers.core.hookenv
 
69
 
 
70
 
 
71
salt_grains_path = '/etc/salt/grains'
 
72
 
 
73
 
 
74
def install_salt_support(from_ppa=True):
 
75
    """Installs the salt-minion helper for machine state.
 
76
 
 
77
    By default the salt-minion package is installed from
 
78
    the saltstack PPA. If from_ppa is False you must ensure
 
79
    that the salt-minion package is available in the apt cache.
 
80
    """
 
81
    if from_ppa:
 
82
        subprocess.check_call([
 
83
            '/usr/bin/add-apt-repository',
 
84
            '--yes',
 
85
            'ppa:saltstack/salt',
 
86
        ])
 
87
        subprocess.check_call(['/usr/bin/apt-get', 'update'])
 
88
    # We install salt-common as salt-minion would run the salt-minion
 
89
    # daemon.
 
90
    charmhelpers.fetch.apt_install('salt-common')
 
91
 
 
92
 
 
93
def update_machine_state(state_path):
 
94
    """Update the machine state using the provided state declaration."""
 
95
    charmhelpers.contrib.templating.contexts.juju_state_to_yaml(
 
96
        salt_grains_path)
 
97
    subprocess.check_call([
 
98
        'salt-call',
 
99
        '--local',
 
100
        'state.template',
 
101
        state_path,
 
102
    ])