Merge lp:~mskalka/juju-ci-tools/update-nh-charm into lp:juju-ci-tools/repository

Proposed by Michael Skalka on 2017-03-15
Status: Merged
Merged at revision: 85
Proposed branch: lp:~mskalka/juju-ci-tools/update-nh-charm
Merge into: lp:juju-ci-tools/repository
Diff against target: 148 lines (+66/-31)
5 files modified
charms/network-health/actions/unit-info.py (+1/-29)
charms/network-health/files/token.txt (+1/-0)
charms/network-health/metadata.yaml (+1/-1)
charms/network-health/reactive/network-health.py (+25/-1)
charms/network-health/scripts/simple-server.py (+38/-0)
To merge this branch: bzr merge lp:~mskalka/juju-ci-tools/update-nh-charm
Reviewer Review Type Date Requested Status
Christopher Lee (community) 2017-03-15 Approve on 2017-03-16
Review via email: mp+319995@code.launchpad.net

Description of the change

Adds a simple HTTP server to the NH charm and spins it off as a fire and forget process during charm install.

The intention is for other charms to be able to curl the NH charm address on port 80 and return the contents of token.txt for a better network test than ping. This is done as a bit of a hack, and I'm not entirely happy with it, but it functions for now. Suggestions for improvement highly welcome.

To post a comment you must log in.
86. By Michael Skalka on 2017-03-16

removed faulty dns logic

Christopher Lee (veebers) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charms/network-health/actions/unit-info.py'
2--- charms/network-health/actions/unit-info.py 2017-03-08 17:28:01 +0000
3+++ charms/network-health/actions/unit-info.py 2017-03-16 17:47:45 +0000
4@@ -9,8 +9,7 @@
5
6 def main():
7 unit_interfaces = interfaces()
8- dns_info = dns(unit_interfaces)
9- action_set({'dns': dns_info, 'interfaces': unit_interfaces})
10+ action_set({'interfaces': unit_interfaces})
11
12
13 def interfaces():
14@@ -46,32 +45,5 @@
15 return interfaces
16
17
18-def dns(interfaces):
19- link_dns = {}
20- for interface, info in interfaces.items():
21- link_dns[interface] = {'ipv4': None, 'ipv6': None}
22- if info.get('inet'):
23- link_dns[interface]['ipv4'] = get_dns(interface)
24- if info.get('inet6'):
25- link_dns[interface]['ipv6'] = get_dns(interface, ipv6=True)
26- return link_dns
27-
28-
29-def get_dns(interface, ipv6=False):
30- raw = None
31- if ipv6:
32- ver = '6'
33- else:
34- ver = '4'
35- cmd = 'nmcli device show {} | grep IP{}.DNS'.format(interface, ver)
36- try:
37- raw = subprocess.check_output(cmd, shell=True).decode("utf-8")
38- except subprocess.CalledProcessError as e:
39- print('Could not get dns due to error:\n {}'.format(e))
40- if raw:
41- return raw.split()[1]
42- return None
43-
44-
45 if __name__ == "__main__":
46 main()
47
48=== added directory 'charms/network-health/files'
49=== added file 'charms/network-health/files/token.txt'
50--- charms/network-health/files/token.txt 1970-01-01 00:00:00 +0000
51+++ charms/network-health/files/token.txt 2017-03-16 17:47:45 +0000
52@@ -0,0 +1,1 @@
53+pass
54
55=== modified file 'charms/network-health/metadata.yaml'
56--- charms/network-health/metadata.yaml 2017-02-17 20:28:32 +0000
57+++ charms/network-health/metadata.yaml 2017-03-16 17:47:45 +0000
58@@ -7,8 +7,8 @@
59 - misc
60 subordinate: true
61 series:
62+ - xenial
63 - trusty
64- - xenial
65 - precise
66
67 requires:
68
69=== modified file 'charms/network-health/reactive/network-health.py'
70--- charms/network-health/reactive/network-health.py 2017-02-17 20:28:32 +0000
71+++ charms/network-health/reactive/network-health.py 2017-03-16 17:47:45 +0000
72@@ -1,7 +1,31 @@
73-#!/usr/bin/python
74+#!/usr/bin/python3
75 from charms.reactive import when, when_not, set_state
76+from charmhelpers.core.hookenv import (
77+ status_set,
78+ open_port,
79+ log,
80+ unit_private_ip,
81+ INFO
82+ )
83+import subprocess
84+import os
85
86
87 @when_not('network-health.installed')
88 def install_network_health():
89+ status_set('active', 'Started')
90 set_state('network-health.installed')
91+
92+
93+@when_not('network-health.simple-http-started')
94+def start_simple_http():
95+ script = 'scripts.simple-server'
96+ file_path = os.path.join(os.environ['CHARM_DIR'], 'files/token.txt')
97+ port = 80
98+ ip = unit_private_ip()
99+ log('Starting simple http server on: {}:{}'.format(
100+ ip, port), INFO)
101+ os.system('sudo python3 -m {} --file-path {} --port {} >> '
102+ '/tmp/server-output.log &'.format(script, file_path, port))
103+ open_port(port)
104+ set_state('network-health.simple-http-started')
105
106=== added directory 'charms/network-health/scripts'
107=== added file 'charms/network-health/scripts/simple-server.py'
108--- charms/network-health/scripts/simple-server.py 1970-01-01 00:00:00 +0000
109+++ charms/network-health/scripts/simple-server.py 2017-03-16 17:47:45 +0000
110@@ -0,0 +1,38 @@
111+#!/usr/bin/python3
112+import argparse
113+import http.server
114+import socketserver
115+import os
116+
117+SERVE_FILE_PATH = 'SIMPLE_HTTP_SERVER_INDEX_FILE'
118+
119+
120+class SimpleRequestHandler(http.server.SimpleHTTPRequestHandler):
121+ """Simple request handler that always returns file supplied by env var."""
122+ def translate_path(self, path):
123+ return os.environ[SERVE_FILE_PATH]
124+
125+
126+def parse_args(argv):
127+ parser = argparse.ArgumentParser(description="Simple http server.")
128+ parser.add_argument('--file-path', help='Path to file to serve.')
129+ parser.add_argument(
130+ '--port', default=8000, type=int, help='Port to serve on.')
131+
132+ return parser.parse_args()
133+
134+
135+def main(argv=None):
136+ args = parse_args(argv)
137+ server_details = ("", args.port)
138+ Handler = SimpleRequestHandler
139+ os.environ[SERVE_FILE_PATH] = args.file_path
140+ httpd = socketserver.TCPServer(server_details, Handler)
141+ try:
142+ httpd.serve_forever()
143+ except KeyboardInterrupt:
144+ print('Caught keyboard interrupt. Exiting.')
145+
146+
147+if __name__ == '__main__':
148+ main()

Subscribers

People subscribed via source and target branches