1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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/>.
17
"""Charm Helpers saltstack - declare the state of your machines.
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::
24
from charmhelpers.contrib.saltstack import (
31
install_salt_support()
32
update_machine_state('machine_states/dependencies.yaml')
33
update_machine_state('machine_states/installed.yaml')
36
and won't need to change (nor will its tests) when you change the machine
39
It's using a python package called salt-minion which allows various formats for
40
specifying resources, such as::
64
The docs for all the different state definitions are at:
65
http://docs.saltstack.com/ref/states/all/
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
76
# Copyright 2013 Canonical Ltd.
79
# Charm Helpers Developers <juju@lists.ubuntu.com>
82
import charmhelpers.contrib.templating.contexts
83
import charmhelpers.core.host
84
import charmhelpers.core.hookenv
87
salt_grains_path = '/etc/salt/grains'
90
def install_salt_support(from_ppa=True):
91
"""Installs the salt-minion helper for machine state.
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.
98
subprocess.check_call([
99
'/usr/bin/add-apt-repository',
101
'ppa:saltstack/salt',
103
subprocess.check_call(['/usr/bin/apt-get', 'update'])
104
# We install salt-common as salt-minion would run the salt-minion
106
charmhelpers.fetch.apt_install('salt-common')
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(
113
subprocess.check_call([