~canonical-ci-engineering/charms/trusty/core-image-publisher/trunk

« back to all changes in this revision

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

  • Committer: Celso Providelo
  • Date: 2015-03-25 04:13:43 UTC
  • Revision ID: celso.providelo@canonical.com-20150325041343-jw05jaz6jscs3c8f
fork of core-image-watcher

Show diffs side-by-side

added added

removed removed

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