Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
107 lines (79 sloc) 2.76 KB
#!/usr/bin/python
import os
import sys
import shutil
from subprocess import call
from charms.reactive import (
hook
)
from charmhelpers.core.templating import render
from charmhelpers.core import hookenv
from charmhelpers.fetch import (
apt_install,
apt_update
)
from charmhelpers.fetch.archiveurl import (
ArchiveUrlFetchHandler
)
TRUSTY_PUPPET_DEB = 'puppetlabs-release-trusty.deb'
PUPPET_APT_URL = 'https://apt.puppetlabs.com'
TRUSTY_PUPPET_DEB_URL = '%s/%s' % (PUPPET_APT_URL, TRUSTY_PUPPET_DEB)
TEMP_DIR = '/tmp'
TRUSTY_PUPPET_DEB_TEMP = '%s/%s' % (TEMP_DIR, TRUSTY_PUPPET_DEB)
PUPPET_CONF = 'puppet.conf'
PUPPET_CONF_DIR = '/etc/puppet'
PUPPET_SSL_DIR = '/etc/puppet/ssl'
PUPPET_CONF_PATH = '%s/%s' % (PUPPET_CONF_DIR, PUPPET_CONF)
config = hookenv.config()
PUPPET_VERSION = config['puppet-version']
PUPPET_CONF_CTXT = {
'environment': config['environment'],
'puppet_server': config['puppet-server']
}
PUPPET_AGENT_PKGS = [('puppet-common=%s' % PUPPET_VERSION),
('puppet=%s' % PUPPET_VERSION)]
def render_puppet_conf(ctx):
"""Render pupept conf
"""
if os.path.exists(PUPPET_CONF_PATH):
os.remove(PUPPET_CONF_PATH)
render(source=PUPPET_CONF,
target=PUPPET_CONF_PATH,
owner='root',
perms=0o644,
context=ctx)
@hook('install'):
def install_puppet_agent():
""" Install puppet-agent
"""
# Download and install trusty puppet deb
hookenv.status_set('maintenance',
'Configuring trusty puppet apt sources')
aufh = ArchiveUrlFetchHandler()
aufh.download(TRUSTY_PUPPET_DEB_URL, TRUSTY_PUPPET_DEB_TEMP)
dpkg_trusty_puppet_deb = 'dpkg -i %s' % TRUSTY_PUPPET_DEB_TEMP
call(dpkg_trusty_puppet_deb.split(), shell=False)
apt_update()
#Clean up
rm_trusty_puppet_deb = 'rm %s' % TRUSTY_PUPPET_DEB_TEMP
call(rm_trusty_puppet_deb.split(), shell=False)
# Install puppet-agent
hookenv.status_set('maintenance',
'Installing puppet-agent %s' % PUPPET_VERSION)
apt_install(PUPPET_AGENT_PKGS)
render_puppet_conf(PUPPET_CONF_CTXT)
hookenv.status_set('active', 'Puppet-agent: %s installed.' % PUPPET_VERSION)
@hook('config-changed'):
def puppet_agent_config_changed():
"""React to config-changed
"""
if config.changed('puppet-server'):
hookenv.status_set('maintenance',
'Reconfiguring puppet-agent for new puppetmaster.')
if os.path.isdir(PUPPET_SSL_DIR):
shutil.rmtree(PUPPET_SSL_DIR)
render_puppet_conf(PUPPET_CONF_CTXT)
if config.changed('puppet-version'):
hookenv.status_set('maintenance',
'Installing puppet %s.' % PUPPET_VERSION)
apt_install(PUPPET_AGENT_PKGS)