Merge ~chris.sanders/charm-prometheus-ceph-exporter:lp/1777028 into ~prometheus-charmers/charm-prometheus-ceph-exporter/+git/prometheus-ceph-exporter-charm:master

Proposed by Chris Sanders
Status: Merged
Merged at revision: 5842acd063455c6e5b2075d8bcbd2bc9e88e0429
Proposed branch: ~chris.sanders/charm-prometheus-ceph-exporter:lp/1777028
Merge into: ~prometheus-charmers/charm-prometheus-ceph-exporter/+git/prometheus-ceph-exporter-charm:master
Diff against target: 136 lines (+38/-40)
1 file modified
reactive/prometheus-ceph-exporter.py (+38/-40)
Reviewer Review Type Date Requested Status
Xav Paice (community) Approve
Review via email: mp+348016@code.launchpad.net

Commit message

Cleaned up states in the charm

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Xav Paice (xavpaice) wrote :

LGTM, tests OK

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/prometheus-ceph-exporter.py b/reactive/prometheus-ceph-exporter.py
2index 61de4e3..431315b 100644
3--- a/reactive/prometheus-ceph-exporter.py
4+++ b/reactive/prometheus-ceph-exporter.py
5@@ -14,16 +14,19 @@
6
7 import yaml
8 import os
9+import time
10
11 from charmhelpers.core import host, hookenv
12 from charmhelpers.core.templating import render
13 from charms.reactive import (
14- when, when_any, set_state, remove_state
15+ when,
16+ when_not,
17+ set_state,
18+ remove_state,
19 )
20-from charms.reactive.helpers import any_file_changed, data_changed
21+from charms.reactive.helpers import any_file_changed
22 from charmhelpers.contrib.charmsupport import nrpe
23 from charmhelpers.contrib.network.ip import get_address_in_network
24-# from charms.layer import snap
25
26 from charmhelpers.fetch import (
27 apt_install,
28@@ -35,6 +38,10 @@ SNAP_DATA = '/var/snap/' + SNAP_NAME + '/current/'
29 PORT_DEF = 9128
30
31
32+class ServiceError(Exception):
33+ pass
34+
35+
36 def templates_changed(tmpl_list):
37 return any_file_changed(['templates/{}'.format(x) for x in tmpl_list])
38
39@@ -43,36 +50,22 @@ def validate_config(filename):
40 return yaml.safe_load(open(filename))
41
42
43-@when_any('snap.installed.prometheus-ceph-exporter',
44- 'ceph-exporter.do-reconfig-yaml')
45-def write_ceph_exporter_config_yaml():
46- # config = hookenv.config()
47- hookenv.open_port(PORT_DEF)
48- set_state('ceph-exporter.do-restart')
49- remove_state('ceph-exporter.do-reconfig-yaml')
50-
51-
52-@when('ceph-exporter.started')
53-def check_config():
54- set_state('ceph-exporter.do-check-reconfig')
55-
56+@when_not('ceph-libs-installed')
57+def install_libs():
58+ apt_install(['ceph-common', 'python-ceph'], fatal=True)
59+ set_state('ceph-libs-installed')
60
61-@when('ceph-exporter.do-check-reconfig')
62-def check_reconfig_ceph_exporter():
63- config = hookenv.config()
64- if data_changed('ceph-exporter.config', config):
65- set_state('ceph-exporter.do-reconfig-yaml')
66
67- remove_state('ceph-exporter.do-check-reconfig')
68-
69-
70-@when('ceph.connected')
71-def ceph_connected(ceph_client):
72- apt_install(['ceph-common', 'python-ceph'])
73+@when('snap.installed.prometheus-ceph-exporter')
74+@when_not('ports-open')
75+def open_port():
76+ hookenv.open_port(PORT_DEF)
77+ set_state('ports-open')
78
79
80 @when('ceph.available')
81-def ceph_ready(ceph_client):
82+@when_not('exporter.started')
83+def configure_exporter(ceph_client):
84 username = hookenv.config('username')
85 daemon_conf = os.path.join(os.sep, SNAP_DATA, 'daemon_arguments')
86 charm_ceph_conf = os.path.join(os.sep, SNAP_DATA, 'ceph.conf')
87@@ -104,18 +97,15 @@ def ceph_ready(ceph_client):
88 # Write out the daemon.arguments file
89 render('daemon_arguments', daemon_conf, daemon_context)
90
91-
92-@when('ceph-exporter.do-restart')
93-def restart_ceph_exporter():
94- if not host.service_running(SVC_NAME):
95- hookenv.log('Starting {}...'.format(SVC_NAME))
96- host.service_start(SVC_NAME)
97+ # Start ceph-exporter
98+ hookenv.log('Starting {}...'.format(SVC_NAME))
99+ host.service_start(SVC_NAME)
100+ time.sleep(10) # service is type=simple can't tell if it actually started
101+ if host.service_running(SVC_NAME):
102+ hookenv.status_set('active', 'Running')
103 else:
104- hookenv.log('Restarting {}, config file changed...'.format(SVC_NAME))
105- host.service_restart(SVC_NAME)
106- hookenv.status_set('active', 'Ready')
107- set_state('ceph-exporter.started')
108- remove_state('ceph-exporter.do-restart')
109+ raise ServiceError("Service didn't start: {}".format(SVC_NAME))
110+ set_state('exporter.started')
111
112
113 def get_exporter_host(interface='ceph-exporter'):
114@@ -148,13 +138,21 @@ def get_exporter_host(interface='ceph-exporter'):
115
116
117 # Relations
118-@when('ceph-exporter.started')
119+@when('exporter.started')
120 @when('ceph-exporter.available') # Relation name is "ceph-exporter"
121 def configure_ceph_exporter_relation(target):
122 hostname = get_exporter_host()
123 target.configure(PORT_DEF, hostname=hostname)
124
125
126+@when('exporter.started')
127+@when_not('ceph.connected')
128+def mon_relation_broken():
129+ host.service_stop(SVC_NAME)
130+ hookenv.status_set('blocked', 'No ceph-mon relation')
131+ remove_state('exporter.started')
132+
133+
134 @when('nrpe-external-master.available')
135 def update_nrpe_config(svc):
136 hostname = nrpe.get_nagios_hostname()

Subscribers

People subscribed via source and target branches