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

Proposed by Michael Skalka
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) Approve
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

removed faulty dns logic

Revision history for this message
Christopher Lee (veebers) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charms/network-health/actions/unit-info.py'
--- charms/network-health/actions/unit-info.py 2017-03-08 17:28:01 +0000
+++ charms/network-health/actions/unit-info.py 2017-03-16 17:47:45 +0000
@@ -9,8 +9,7 @@
99
10def main():10def main():
11 unit_interfaces = interfaces()11 unit_interfaces = interfaces()
12 dns_info = dns(unit_interfaces)12 action_set({'interfaces': unit_interfaces})
13 action_set({'dns': dns_info, 'interfaces': unit_interfaces})
1413
1514
16def interfaces():15def interfaces():
@@ -46,32 +45,5 @@
46 return interfaces45 return interfaces
4746
4847
49def dns(interfaces):
50 link_dns = {}
51 for interface, info in interfaces.items():
52 link_dns[interface] = {'ipv4': None, 'ipv6': None}
53 if info.get('inet'):
54 link_dns[interface]['ipv4'] = get_dns(interface)
55 if info.get('inet6'):
56 link_dns[interface]['ipv6'] = get_dns(interface, ipv6=True)
57 return link_dns
58
59
60def get_dns(interface, ipv6=False):
61 raw = None
62 if ipv6:
63 ver = '6'
64 else:
65 ver = '4'
66 cmd = 'nmcli device show {} | grep IP{}.DNS'.format(interface, ver)
67 try:
68 raw = subprocess.check_output(cmd, shell=True).decode("utf-8")
69 except subprocess.CalledProcessError as e:
70 print('Could not get dns due to error:\n {}'.format(e))
71 if raw:
72 return raw.split()[1]
73 return None
74
75
76if __name__ == "__main__":48if __name__ == "__main__":
77 main()49 main()
7850
=== added directory 'charms/network-health/files'
=== added file 'charms/network-health/files/token.txt'
--- charms/network-health/files/token.txt 1970-01-01 00:00:00 +0000
+++ charms/network-health/files/token.txt 2017-03-16 17:47:45 +0000
@@ -0,0 +1,1 @@
1pass
02
=== modified file 'charms/network-health/metadata.yaml'
--- charms/network-health/metadata.yaml 2017-02-17 20:28:32 +0000
+++ charms/network-health/metadata.yaml 2017-03-16 17:47:45 +0000
@@ -7,8 +7,8 @@
7 - misc7 - misc
8subordinate: true8subordinate: true
9series:9series:
10 - xenial
10 - trusty11 - trusty
11 - xenial
12 - precise12 - precise
1313
14requires:14requires:
1515
=== modified file 'charms/network-health/reactive/network-health.py'
--- charms/network-health/reactive/network-health.py 2017-02-17 20:28:32 +0000
+++ charms/network-health/reactive/network-health.py 2017-03-16 17:47:45 +0000
@@ -1,7 +1,31 @@
1#!/usr/bin/python1#!/usr/bin/python3
2from charms.reactive import when, when_not, set_state2from charms.reactive import when, when_not, set_state
3from charmhelpers.core.hookenv import (
4 status_set,
5 open_port,
6 log,
7 unit_private_ip,
8 INFO
9 )
10import subprocess
11import os
312
413
5@when_not('network-health.installed')14@when_not('network-health.installed')
6def install_network_health():15def install_network_health():
16 status_set('active', 'Started')
7 set_state('network-health.installed')17 set_state('network-health.installed')
18
19
20@when_not('network-health.simple-http-started')
21def start_simple_http():
22 script = 'scripts.simple-server'
23 file_path = os.path.join(os.environ['CHARM_DIR'], 'files/token.txt')
24 port = 80
25 ip = unit_private_ip()
26 log('Starting simple http server on: {}:{}'.format(
27 ip, port), INFO)
28 os.system('sudo python3 -m {} --file-path {} --port {} >> '
29 '/tmp/server-output.log &'.format(script, file_path, port))
30 open_port(port)
31 set_state('network-health.simple-http-started')
832
=== added directory 'charms/network-health/scripts'
=== added file 'charms/network-health/scripts/simple-server.py'
--- charms/network-health/scripts/simple-server.py 1970-01-01 00:00:00 +0000
+++ charms/network-health/scripts/simple-server.py 2017-03-16 17:47:45 +0000
@@ -0,0 +1,38 @@
1#!/usr/bin/python3
2import argparse
3import http.server
4import socketserver
5import os
6
7SERVE_FILE_PATH = 'SIMPLE_HTTP_SERVER_INDEX_FILE'
8
9
10class SimpleRequestHandler(http.server.SimpleHTTPRequestHandler):
11 """Simple request handler that always returns file supplied by env var."""
12 def translate_path(self, path):
13 return os.environ[SERVE_FILE_PATH]
14
15
16def parse_args(argv):
17 parser = argparse.ArgumentParser(description="Simple http server.")
18 parser.add_argument('--file-path', help='Path to file to serve.')
19 parser.add_argument(
20 '--port', default=8000, type=int, help='Port to serve on.')
21
22 return parser.parse_args()
23
24
25def main(argv=None):
26 args = parse_args(argv)
27 server_details = ("", args.port)
28 Handler = SimpleRequestHandler
29 os.environ[SERVE_FILE_PATH] = args.file_path
30 httpd = socketserver.TCPServer(server_details, Handler)
31 try:
32 httpd.serve_forever()
33 except KeyboardInterrupt:
34 print('Caught keyboard interrupt. Exiting.')
35
36
37if __name__ == '__main__':
38 main()

Subscribers

People subscribed via source and target branches