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
1=== modified file 'config.yaml'
2--- config.yaml 2016-05-18 16:31:08 +0000
3+++ config.yaml 2016-09-13 21:43:17 +0000
4@@ -1,4 +1,17 @@
5 options:
6+ install_method:
7+ default: "snap"
8+ type: string
9+ description: |
10+ How to install prometheus-snmp-exporter package. If set to "apt"
11+ the package will be installed using apt-get.
12+ If set to "snap", empty string or unset snap package will be installed
13+ snap_channel:
14+ default: "stable"
15+ type: string
16+ description: |
17+ If install_method is set to "snap" this option controlls channel name.
18+ Supported values are: "stable", "candidate", "beta" and "edge"
19 install_sources:
20 default: '[ "ppa:canonical-bootstack/prometheus" ]'
21 type: string
22
23=== modified file 'reactive/prometheus-snmp-exporter.py'
24--- reactive/prometheus-snmp-exporter.py 2016-05-18 15:27:05 +0000
25+++ reactive/prometheus-snmp-exporter.py 2016-09-13 21:43:17 +0000
26@@ -1,4 +1,5 @@
27 import yaml
28+import subprocess
29
30 from charmhelpers import fetch
31 from charmhelpers.core import host, hookenv
32@@ -9,10 +10,8 @@
33 from charms.reactive.helpers import any_file_changed, data_changed
34
35
36-SVCNAME = 'prometheus-snmp-exporter'
37 PKGNAMES = ['prometheus-snmp-exporter']
38 PORT_DEF = 9116
39-SNMP_EXPORTER_YML = '/etc/prometheus/snmp.yaml'
40 SNMP_EXPORTER_YML_TMPL = 'snmp.yaml.j2'
41
42
43@@ -20,33 +19,50 @@
44 return any_file_changed(['templates/{}'.format(x) for x in tmpl_list])
45
46
47-@when('snmp-exporter.do-install')
48+@when_not('snmp-exporter.installed')
49 def install_packages():
50 fetch.configure_sources()
51- fetch.apt_update()
52- fetch.apt_install(PKGNAMES)
53- remove_state('snmp-exporter.do-install')
54-
55-
56-def validate_config():
57- return yaml.safe_load(open(SNMP_EXPORTER_YML))
58-
59-
60-@when('alertmanager.do-reconfig-yaml')
61-def write_alertmanager_config_yaml():
62- config = hookenv.config()
63+ config = hookenv.config()
64+ source = config.get('install_method', 'snap')
65+ channel = '--channel={}'.format(config.get('snap_channel', 'stable'))
66+ if source == 'snap':
67+ # Switch to check_call once LP1622782 is resolved
68+ subprocess.call(['snap', 'install'] + PKGNAMES + [channel])
69+ set_state('snmp-exporter.installed')
70+ elif source == 'apt':
71+ fetch.apt_update()
72+ fetch.apt_install(PKGNAMES)
73+ set_state('snmp-exporter.installed')
74+ else:
75+ hookenv.status_set('blocked',
76+ 'Unsupported install_method: {}'.format(source))
77+ raise Exception('Unsupported install_method: {}'.format(source))
78+
79+
80+def validate_config(filename):
81+ return yaml.safe_load(open(filename))
82+
83+
84+@when('snmp-exporter.installed')
85+@when('snmp-exporter.do-reconfig-yaml')
86+def write_snmp_exporter_config_yaml():
87+ config = hookenv.config()
88+ if config.get('install_method', 'snap') == 'snap':
89+ config_file = '/var/snap/prometheus-snmp-exporter/current/snmp.yml'
90+ else:
91+ config_file = '/etc/prometheus/snmp.yaml'
92 render(source=SNMP_EXPORTER_YML_TMPL,
93- target=SNMP_EXPORTER_YML,
94+ target=config_file,
95 context={'config': config}
96 )
97- validate_config()
98+ validate_config(config_file)
99 hookenv.open_port(PORT_DEF)
100 set_state('snmp-exporter.do-restart')
101 remove_state('snmp-exporter.do-reconfig-yaml')
102
103
104 @when_not('snmp-exporter.started')
105-def setup_alertmanager():
106+def setup_snmp_exporter():
107 hookenv.status_set('maintenance', 'Installing software')
108 install_packages()
109 set_state('snmp-exporter.do-check-reconfig')
110@@ -58,14 +74,14 @@
111
112
113 @when('snmp-exporter.do-check-reconfig')
114-def check_reconfig_alertmanager():
115+def check_reconfig_snmp_exporter():
116 config = hookenv.config()
117- install_opts = ('install_sources', 'install_keys')
118+ install_opts = ('install_sources', 'install_keys', 'install_method')
119 if any(config.changed(opt) for opt in install_opts):
120- set_state('snmp-exporter.do-install')
121+ remove_state('snmp-exporter.installed')
122
123 if data_changed('snmp-exporter.config', config):
124- set_state('alertmanager.do-reconfig-yaml')
125+ set_state('snmp-exporter.do-reconfig-yaml')
126
127 if templates_changed([SNMP_EXPORTER_YML_TMPL]):
128 set_state('snmp-exporter.do-reconfig-yaml')
129@@ -74,13 +90,18 @@
130
131
132 @when('snmp-exporter.do-restart')
133-def restart_alertmanager():
134- if not host.service_running(SVCNAME):
135- hookenv.log('Starting {}...'.format(SVCNAME))
136- host.service_start(SVCNAME)
137- else:
138- hookenv.log('Restarting {}, config file changed...'.format(SVCNAME))
139- host.service_restart(SVCNAME)
140+def restart_snmp_exporter():
141+ config = hookenv.config()
142+ if config.get('install_method', 'snap'):
143+ svcname = 'snap.prometheus-snmp-exporter.snmp-exporter'
144+ else:
145+ svcname = 'prometheus-snmp-exporter'
146+ if not host.service_running(svcname):
147+ hookenv.log('Starting {}...'.format(svcname))
148+ host.service_start(svcname)
149+ else:
150+ hookenv.log('Restarting {}, config file changed...'.format(svcname))
151+ host.service_restart(svcname)
152 hookenv.status_set('active', 'Ready')
153 set_state('snmp-exporter.started')
154 remove_state('snmp-exporter.do-restart')
155
156=== modified file 'templates/snmp.yaml.j2'
157--- templates/snmp.yaml.j2 2016-05-27 10:14:37 +0000
158+++ templates/snmp.yaml.j2 2016-09-13 21:43:17 +0000
159@@ -1,7 +1,9 @@
160 ---
161 # Default module: interface stats and uptime.
162 default:
163- community: {{ config.snmp_community }}
164+ version: 2
165+ auth:
166+ community: {{ config.snmp_community }}
167 walk:
168 - 1.3.6.1.2.1.1.3
169 - 1.3.6.1.2.1.2

Subscribers

People subscribed via source and target branches

to all changes: