Merge lp:~jacekn/charms/xenial/prometheus-snmp-exporter/layer-prometheus-snmp-exporter-snap into lp:~canonical-is-sa/charms/xenial/prometheus-snmp-exporter/layer-prometheus-snmp-exporter

Proposed by Jacek Nykis on 2016-09-13
Status: Merged
Merged at revision: 5
Proposed branch: lp:~jacekn/charms/xenial/prometheus-snmp-exporter/layer-prometheus-snmp-exporter-snap
Merge into: lp:~canonical-is-sa/charms/xenial/prometheus-snmp-exporter/layer-prometheus-snmp-exporter
Diff against target: 169 lines (+66/-30)
3 files modified
config.yaml (+13/-0)
reactive/prometheus-snmp-exporter.py (+50/-29)
templates/snmp.yaml.j2 (+3/-1)
To merge this branch: bzr merge lp:~jacekn/charms/xenial/prometheus-snmp-exporter/layer-prometheus-snmp-exporter-snap
Reviewer Review Type Date Requested Status
Stuart Bishop 2016-09-13 Approve on 2016-09-13
Review via email: mp+305663@code.launchpad.net

Description of the change

Add snap package support

To post a comment you must log in.
Stuart Bishop (stub) wrote :

A couple of issues inline, but nothing that will require a second review. Mostly good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'config.yaml'
--- config.yaml 2016-05-18 16:31:08 +0000
+++ config.yaml 2016-09-13 21:43:17 +0000
@@ -1,4 +1,17 @@
1options:1options:
2 install_method:
3 default: "snap"
4 type: string
5 description: |
6 How to install prometheus-snmp-exporter package. If set to "apt"
7 the package will be installed using apt-get.
8 If set to "snap", empty string or unset snap package will be installed
9 snap_channel:
10 default: "stable"
11 type: string
12 description: |
13 If install_method is set to "snap" this option controlls channel name.
14 Supported values are: "stable", "candidate", "beta" and "edge"
2 install_sources:15 install_sources:
3 default: '[ "ppa:canonical-bootstack/prometheus" ]'16 default: '[ "ppa:canonical-bootstack/prometheus" ]'
4 type: string17 type: string
518
=== modified file 'reactive/prometheus-snmp-exporter.py'
--- reactive/prometheus-snmp-exporter.py 2016-05-18 15:27:05 +0000
+++ reactive/prometheus-snmp-exporter.py 2016-09-13 21:43:17 +0000
@@ -1,4 +1,5 @@
1import yaml1import yaml
2import subprocess
23
3from charmhelpers import fetch4from charmhelpers import fetch
4from charmhelpers.core import host, hookenv5from charmhelpers.core import host, hookenv
@@ -9,10 +10,8 @@
9from charms.reactive.helpers import any_file_changed, data_changed10from charms.reactive.helpers import any_file_changed, data_changed
1011
1112
12SVCNAME = 'prometheus-snmp-exporter'
13PKGNAMES = ['prometheus-snmp-exporter']13PKGNAMES = ['prometheus-snmp-exporter']
14PORT_DEF = 911614PORT_DEF = 9116
15SNMP_EXPORTER_YML = '/etc/prometheus/snmp.yaml'
16SNMP_EXPORTER_YML_TMPL = 'snmp.yaml.j2'15SNMP_EXPORTER_YML_TMPL = 'snmp.yaml.j2'
1716
1817
@@ -20,33 +19,50 @@
20 return any_file_changed(['templates/{}'.format(x) for x in tmpl_list])19 return any_file_changed(['templates/{}'.format(x) for x in tmpl_list])
2120
2221
23@when('snmp-exporter.do-install')22@when_not('snmp-exporter.installed')
24def install_packages():23def install_packages():
25 fetch.configure_sources()24 fetch.configure_sources()
26 fetch.apt_update()25 config = hookenv.config()
27 fetch.apt_install(PKGNAMES)26 source = config.get('install_method', 'snap')
28 remove_state('snmp-exporter.do-install')27 channel = '--channel={}'.format(config.get('snap_channel', 'stable'))
2928 if source == 'snap':
3029 # Switch to check_call once LP1622782 is resolved
31def validate_config():30 subprocess.call(['snap', 'install'] + PKGNAMES + [channel])
32 return yaml.safe_load(open(SNMP_EXPORTER_YML))31 set_state('snmp-exporter.installed')
3332 elif source == 'apt':
3433 fetch.apt_update()
35@when('alertmanager.do-reconfig-yaml')34 fetch.apt_install(PKGNAMES)
36def write_alertmanager_config_yaml():35 set_state('snmp-exporter.installed')
37 config = hookenv.config()36 else:
37 hookenv.status_set('blocked',
38 'Unsupported install_method: {}'.format(source))
39 raise Exception('Unsupported install_method: {}'.format(source))
40
41
42def validate_config(filename):
43 return yaml.safe_load(open(filename))
44
45
46@when('snmp-exporter.installed')
47@when('snmp-exporter.do-reconfig-yaml')
48def write_snmp_exporter_config_yaml():
49 config = hookenv.config()
50 if config.get('install_method', 'snap') == 'snap':
51 config_file = '/var/snap/prometheus-snmp-exporter/current/snmp.yml'
52 else:
53 config_file = '/etc/prometheus/snmp.yaml'
38 render(source=SNMP_EXPORTER_YML_TMPL,54 render(source=SNMP_EXPORTER_YML_TMPL,
39 target=SNMP_EXPORTER_YML,55 target=config_file,
40 context={'config': config}56 context={'config': config}
41 )57 )
42 validate_config()58 validate_config(config_file)
43 hookenv.open_port(PORT_DEF)59 hookenv.open_port(PORT_DEF)
44 set_state('snmp-exporter.do-restart')60 set_state('snmp-exporter.do-restart')
45 remove_state('snmp-exporter.do-reconfig-yaml')61 remove_state('snmp-exporter.do-reconfig-yaml')
4662
4763
48@when_not('snmp-exporter.started')64@when_not('snmp-exporter.started')
49def setup_alertmanager():65def setup_snmp_exporter():
50 hookenv.status_set('maintenance', 'Installing software')66 hookenv.status_set('maintenance', 'Installing software')
51 install_packages()67 install_packages()
52 set_state('snmp-exporter.do-check-reconfig')68 set_state('snmp-exporter.do-check-reconfig')
@@ -58,14 +74,14 @@
5874
5975
60@when('snmp-exporter.do-check-reconfig')76@when('snmp-exporter.do-check-reconfig')
61def check_reconfig_alertmanager():77def check_reconfig_snmp_exporter():
62 config = hookenv.config()78 config = hookenv.config()
63 install_opts = ('install_sources', 'install_keys')79 install_opts = ('install_sources', 'install_keys', 'install_method')
64 if any(config.changed(opt) for opt in install_opts):80 if any(config.changed(opt) for opt in install_opts):
65 set_state('snmp-exporter.do-install')81 remove_state('snmp-exporter.installed')
6682
67 if data_changed('snmp-exporter.config', config):83 if data_changed('snmp-exporter.config', config):
68 set_state('alertmanager.do-reconfig-yaml')84 set_state('snmp-exporter.do-reconfig-yaml')
6985
70 if templates_changed([SNMP_EXPORTER_YML_TMPL]):86 if templates_changed([SNMP_EXPORTER_YML_TMPL]):
71 set_state('snmp-exporter.do-reconfig-yaml')87 set_state('snmp-exporter.do-reconfig-yaml')
@@ -74,13 +90,18 @@
7490
7591
76@when('snmp-exporter.do-restart')92@when('snmp-exporter.do-restart')
77def restart_alertmanager():93def restart_snmp_exporter():
78 if not host.service_running(SVCNAME):94 config = hookenv.config()
79 hookenv.log('Starting {}...'.format(SVCNAME))95 if config.get('install_method', 'snap'):
80 host.service_start(SVCNAME)96 svcname = 'snap.prometheus-snmp-exporter.snmp-exporter'
81 else:97 else:
82 hookenv.log('Restarting {}, config file changed...'.format(SVCNAME))98 svcname = 'prometheus-snmp-exporter'
83 host.service_restart(SVCNAME)99 if not host.service_running(svcname):
100 hookenv.log('Starting {}...'.format(svcname))
101 host.service_start(svcname)
102 else:
103 hookenv.log('Restarting {}, config file changed...'.format(svcname))
104 host.service_restart(svcname)
84 hookenv.status_set('active', 'Ready')105 hookenv.status_set('active', 'Ready')
85 set_state('snmp-exporter.started')106 set_state('snmp-exporter.started')
86 remove_state('snmp-exporter.do-restart')107 remove_state('snmp-exporter.do-restart')
87108
=== modified file 'templates/snmp.yaml.j2'
--- templates/snmp.yaml.j2 2016-05-27 10:14:37 +0000
+++ templates/snmp.yaml.j2 2016-09-13 21:43:17 +0000
@@ -1,7 +1,9 @@
1---1---
2# Default module: interface stats and uptime.2# Default module: interface stats and uptime.
3default:3default:
4 community: {{ config.snmp_community }}4 version: 2
5 auth:
6 community: {{ config.snmp_community }}
5 walk:7 walk:
6 - 1.3.6.1.2.1.1.38 - 1.3.6.1.2.1.1.3
7 - 1.3.6.1.2.1.29 - 1.3.6.1.2.1.2

Subscribers

People subscribed via source and target branches

to all changes: