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
diff --git a/config.yaml b/config.yaml
index d0d2649..4ac707e 100644
--- a/config.yaml
+++ b/config.yaml
@@ -35,16 +35,19 @@ options:
35 Example: http://10.0.0.1:9000/35 Example: http://10.0.0.1:9000/
36 rest_transport_uri:36 rest_transport_uri:
37 type: string37 type: string
38 default: ""38 default: "http://0.0.0.0:9001/api/"
39 description: |39 description: |
40 If set, this will be promoted in the cluster discovery APIs. You will need40 If set, this will be promoted in the cluster discovery APIs. You will need
41 to define this if your Graylog server is running behind a HTTP proxy that41 to define this if your Graylog server is running behind a HTTP proxy that
42 is rewriting the scheme, host name, or URI. This must not contain a42 is rewriting the scheme, host name, or URI.
43 wildcard address (0.0.0.0).
4443
45 For Graylog 2, this usually takes the form http://10.0.0.1:9001/api/.44 For Graylog 2, this usually takes the form http://10.0.0.1:9001/api/.
46 For Graylog 3 and higher, this is known as the 'http_publish_uri' and looks45 For Graylog 3 and higher, this is known as the 'http_publish_uri' and looks
47 like http://10.0.0.1:9000/.46 like http://10.0.0.1:9000/.
47
48 If the ip address 0.0.0.0 is specified, the charm will attempt to deduct
49 the network interface that is used for connections to the default gateway and
50 use the associated ip address.
48 index_replicas:51 index_replicas:
49 type: int52 type: int
50 default: 053 default: 0
diff --git a/lib/charms/layer/graylog/utils.py b/lib/charms/layer/graylog/utils.py
index cf89b61..2d383dd 100644
--- a/lib/charms/layer/graylog/utils.py
+++ b/lib/charms/layer/graylog/utils.py
@@ -1,23 +1,26 @@
1import socket
2from urllib.parse import urlparse
3
1from charmhelpers.core import hookenv4from charmhelpers.core import hookenv
2from charms.layer import snap5from charms.layer import snap
36
4SNAP_NAME = 'graylog'7SNAP_NAME = "graylog"
58
69
7def get_api_port():10def get_api_port():
8 return '9001' if is_v2() else '9000'11 return "9001" if is_v2() else "9000"
912
1013
11def get_api_url():14def get_api_url():
12 return 'http://127.0.0.1:{}/api/'.format(get_api_port())15 return "http://127.0.0.1:{}/api/".format(get_api_port())
1316
1417
15def is_v2():18def is_v2():
16 version = snap.get_installed_version(SNAP_NAME)19 version = snap.get_installed_version(SNAP_NAME)
17 # If the snap isn't installed yet, base our version off the charm config20 # If the snap isn't installed yet, base our version off the charm config
18 if not version:21 if not version:
19 version = hookenv.config('channel')22 version = hookenv.config("channel")
20 return True if version.startswith('2') else False23 return True if version.startswith("2") else False
2124
2225
23def validate_api_uri(uri):26def validate_api_uri(uri):
@@ -27,16 +30,45 @@ def validate_api_uri(uri):
27 server hard codes '/api/' to the end of these options and should therefore30 server hard codes '/api/' to the end of these options and should therefore
28 be removed if included in the charm config.31 be removed if included in the charm config.
29 """32 """
30 if not uri.endswith('/'):33 if not uri.endswith("/"):
31 uri += '/'34 uri += "/"
3235
33 if is_v2():36 if is_v2():
34 if not uri.endswith('/api/'):37 if not uri.endswith("/api/"):
35 hookenv.log("Appending 'api/' to the configured REST API options")38 hookenv.log("Appending 'api/' to the configured REST API options")
36 uri += 'api/'39 uri += "api/"
37 else:40 else:
38 if uri.endswith('/api/'):41 if uri.endswith("/api/"):
39 hookenv.log("Removing 'api/' from the configurted REST API options")42 hookenv.log("Removing 'api/' from the configurted REST API options")
40 uri = uri[:-4]43 uri = uri[:-4]
4144
45 parsed_url = urlparse(uri)
46 if parsed_url.hostname == "0.0.0.0":
47 uri = parsed_url._replace(
48 netloc="{}:{}".format(get_ip_address(), parsed_url.port)
49 ).geturl()
50
42 return uri51 return uri
52
53
54def get_ip_address():
55 """Determine the ip address that would be used for outgoing connection.
56
57 This function tries to determine the network ip address, that would be used
58 when access servers in the internet. It uses a dummy UDP connection todo
59 so.
60 As the connection itself is not made, it is safe to use any address.
61 """
62
63 # Start a socket via ipv4, using UDP
64 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
65
66 # Make dummy attempt to ntp servers from the pool.ntp.org, we hard code one
67 # of them
68
69 sock.connect(("ntp.ubuntu.com", 123))
70
71 ip = sock.getsockname()[0]
72 sock.close()
73 hookenv.log("Found ip {} to use".format(ip))
74 return ip
diff --git a/reactive/graylog.py b/reactive/graylog.py
index b3a6538..89a4843 100644
--- a/reactive/graylog.py
+++ b/reactive/graylog.py
@@ -155,8 +155,8 @@ def configure_graylog():
155 db.set('web_listen_port', url.port)155 db.set('web_listen_port', url.port)
156 hookenv.open_port(url.port)156 hookenv.open_port(url.port)
157157
158 if conf['rest_transport_uri']:158 # We always call set this, so it gets a sensible default
159 set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))159 set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))
160160
161 if conf['web_endpoint_uri']:161 if conf['web_endpoint_uri']:
162 set_conf('web_endpoint_uri', validate_api_uri(conf['web_endpoint_uri']))162 set_conf('web_endpoint_uri', validate_api_uri(conf['web_endpoint_uri']))
@@ -164,8 +164,8 @@ def configure_graylog():
164 if conf['web_listen_uri']:164 if conf['web_listen_uri']:
165 url = urlparse(conf['web_listen_uri'])165 url = urlparse(conf['web_listen_uri'])
166 set_conf('http_bind_address', url.netloc)166 set_conf('http_bind_address', url.netloc)
167 if conf['rest_transport_uri']:167 # We always call set this, so it gets a sensible default
168 set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))168 set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))
169 if conf['web_endpoint_uri']:169 if conf['web_endpoint_uri']:
170 set_conf('http_external_uri', validate_api_uri(conf['web_endpoint_uri']))170 set_conf('http_external_uri', validate_api_uri(conf['web_endpoint_uri']))
171171

Subscribers

People subscribed via source and target branches

to all changes: