Merge ~xavpaice/charm-graylog:bug/1818615 into ~graylog-charmers/charm-graylog:master

Proposed by Xav Paice
Status: Merged
Approved by: Xav Paice
Approved revision: de6bd7915f570fff8d5c483a260e0a4af63e93ba
Merge reported by: Xav Paice
Merged at revision: de6bd7915f570fff8d5c483a260e0a4af63e93ba
Proposed branch: ~xavpaice/charm-graylog:bug/1818615
Merge into: ~graylog-charmers/charm-graylog:master
Diff against target: 139 lines (+52/-17)
3 files modified
config.yaml (+6/-3)
lib/charms/layer/graylog/utils.py (+42/-10)
reactive/graylog.py (+4/-4)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Xav Paice Pending
Canonical IS Reviewers Pending
Review via email: mp+379859@code.launchpad.net

This proposal supersedes a proposal from 2020-02-13.

Commit message

Determine default network interface

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

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

Revision history for this message
Haw Loeung (hloeung) : Posted in a previous version of this proposal
Revision history for this message
Xav Paice (xavpaice) wrote : Posted in a previous version of this proposal

couple of diff comments, plus Haw's review

review: Needs Fixing
Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
Haw Loeung (hloeung) wrote :

Feel free to toggle the 'Status' to 'Approved' and have the mergebot land it.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index d0d2649..4ac707e 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -35,16 +35,19 @@ options:
6 Example: http://10.0.0.1:9000/
7 rest_transport_uri:
8 type: string
9- default: ""
10+ default: "http://0.0.0.0:9001/api/"
11 description: |
12 If set, this will be promoted in the cluster discovery APIs. You will need
13 to define this if your Graylog server is running behind a HTTP proxy that
14- is rewriting the scheme, host name, or URI. This must not contain a
15- wildcard address (0.0.0.0).
16+ is rewriting the scheme, host name, or URI.
17
18 For Graylog 2, this usually takes the form http://10.0.0.1:9001/api/.
19 For Graylog 3 and higher, this is known as the 'http_publish_uri' and looks
20 like http://10.0.0.1:9000/.
21+
22+ If the ip address 0.0.0.0 is specified, the charm will attempt to deduct
23+ the network interface that is used for connections to the default gateway and
24+ use the associated ip address.
25 index_replicas:
26 type: int
27 default: 0
28diff --git a/lib/charms/layer/graylog/utils.py b/lib/charms/layer/graylog/utils.py
29index cf89b61..2d383dd 100644
30--- a/lib/charms/layer/graylog/utils.py
31+++ b/lib/charms/layer/graylog/utils.py
32@@ -1,23 +1,26 @@
33+import socket
34+from urllib.parse import urlparse
35+
36 from charmhelpers.core import hookenv
37 from charms.layer import snap
38
39-SNAP_NAME = 'graylog'
40+SNAP_NAME = "graylog"
41
42
43 def get_api_port():
44- return '9001' if is_v2() else '9000'
45+ return "9001" if is_v2() else "9000"
46
47
48 def get_api_url():
49- return 'http://127.0.0.1:{}/api/'.format(get_api_port())
50+ return "http://127.0.0.1:{}/api/".format(get_api_port())
51
52
53 def is_v2():
54 version = snap.get_installed_version(SNAP_NAME)
55 # If the snap isn't installed yet, base our version off the charm config
56 if not version:
57- version = hookenv.config('channel')
58- return True if version.startswith('2') else False
59+ version = hookenv.config("channel")
60+ return True if version.startswith("2") else False
61
62
63 def validate_api_uri(uri):
64@@ -27,16 +30,45 @@ def validate_api_uri(uri):
65 server hard codes '/api/' to the end of these options and should therefore
66 be removed if included in the charm config.
67 """
68- if not uri.endswith('/'):
69- uri += '/'
70+ if not uri.endswith("/"):
71+ uri += "/"
72
73 if is_v2():
74- if not uri.endswith('/api/'):
75+ if not uri.endswith("/api/"):
76 hookenv.log("Appending 'api/' to the configured REST API options")
77- uri += 'api/'
78+ uri += "api/"
79 else:
80- if uri.endswith('/api/'):
81+ if uri.endswith("/api/"):
82 hookenv.log("Removing 'api/' from the configurted REST API options")
83 uri = uri[:-4]
84
85+ parsed_url = urlparse(uri)
86+ if parsed_url.hostname == "0.0.0.0":
87+ uri = parsed_url._replace(
88+ netloc="{}:{}".format(get_ip_address(), parsed_url.port)
89+ ).geturl()
90+
91 return uri
92+
93+
94+def get_ip_address():
95+ """Determine the ip address that would be used for outgoing connection.
96+
97+ This function tries to determine the network ip address, that would be used
98+ when access servers in the internet. It uses a dummy UDP connection todo
99+ so.
100+ As the connection itself is not made, it is safe to use any address.
101+ """
102+
103+ # Start a socket via ipv4, using UDP
104+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
105+
106+ # Make dummy attempt to ntp servers from the pool.ntp.org, we hard code one
107+ # of them
108+
109+ sock.connect(("ntp.ubuntu.com", 123))
110+
111+ ip = sock.getsockname()[0]
112+ sock.close()
113+ hookenv.log("Found ip {} to use".format(ip))
114+ return ip
115diff --git a/reactive/graylog.py b/reactive/graylog.py
116index b3a6538..89a4843 100644
117--- a/reactive/graylog.py
118+++ b/reactive/graylog.py
119@@ -155,8 +155,8 @@ def configure_graylog():
120 db.set('web_listen_port', url.port)
121 hookenv.open_port(url.port)
122
123- if conf['rest_transport_uri']:
124- set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))
125+ # We always call set this, so it gets a sensible default
126+ set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))
127
128 if conf['web_endpoint_uri']:
129 set_conf('web_endpoint_uri', validate_api_uri(conf['web_endpoint_uri']))
130@@ -164,8 +164,8 @@ def configure_graylog():
131 if conf['web_listen_uri']:
132 url = urlparse(conf['web_listen_uri'])
133 set_conf('http_bind_address', url.netloc)
134- if conf['rest_transport_uri']:
135- set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))
136+ # We always call set this, so it gets a sensible default
137+ set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))
138 if conf['web_endpoint_uri']:
139 set_conf('http_external_uri', validate_api_uri(conf['web_endpoint_uri']))
140

Subscribers

People subscribed via source and target branches

to all changes: