Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (65 sloc) 2.11 KB
import os
import socket
from charmhelpers.core.templating import render
from charmhelpers.core.host import (
service_restart,
service_running,
service_start,
)
from charms.reactive import (
when,
when_not,
set_state
)
from charmhelpers.core.hookenv import (
config,
status_set,
)
import charms.apt
DD_AGENT_DIR = os.path.join('/', 'etc', 'dd-agent')
@when_not('apt.installed.datadog-agent')
def install_dd_agent():
"""Set the datadog-agent pkg from apt
"""
charms.apt.queue_install(['datadog-agent'])
charms.apt.install_queued()
set_state('datadog.agent.installed')
@when('apt.installed.datadog-agent')
@when_not('datadog.agent.configured')
def write_dd_agent_config():
"""Write out the datadog-agent config
"""
dd_example_conf = os.path.join(DD_AGENT_DIR, 'datadog.conf.example')
dd_conf = os.path.join(DD_AGENT_DIR, 'datadog.conf')
# Check for and remove conf
if os.path.exists(dd_conf):
os.remove(dd_conf)
# Check for and remove example conf
if os.path.exists(dd_example_conf):
os.remove(dd_example_conf)
# Collect vars for config template
dd_agent_ctxt = {'hostname': socket.gethostname(),
'api_key': config('api-key')}
# Render datadog.conf
render(source='datadog.conf.tmpl', target=dd_conf, owner='dd-agent',
group='root', perms=0o640, context=dd_agent_ctxt)
# Set datadog.agent.configured state
set_state('datadog.agent.configured')
@when('datadog.agent.configured')
@when_not('datadog.agent.available')
def start_restart_dd_agent():
"""Start or restart datadog agent
"""
if config('no-start'):
# If the no-start config is true, we are done
status_set('active', 'Datadog agent installed')
else:
# Ensure datadog agent is running
if service_running('datadog-agent'):
service_restart('datadog-agent')
else:
service_start('datadog-agent')
# Set status
status_set('active', 'Datadog agent installed and running')
# Set datadog available state
set_state('datadog.agent.available')