Skip to content

Commit

Permalink
Merge pull request #318 from lucernae/fix-313
Browse files Browse the repository at this point in the history
Improve docker recipes and initialization scripts
  • Loading branch information
meomancer authored Jan 18, 2021
2 parents 8d42ff5 + a3cb318 commit 6a5d859
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions deployment/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ WORKDIR /home/web/django_project
ADD deployment/docker/entrypoint.sh /entrypoint.sh
ADD deployment/docker/initialize.py /initialize.py
ADD deployment/docker/uwsgi.conf /uwsgi.conf
ENTRYPOINT ["/entrypoint.sh"]
CMD ["uwsgi", "--ini", "/uwsgi.conf"]
42 changes: 31 additions & 11 deletions deployment/docker/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,22 @@
)
app.skip_authorization = True
_host = os.getenv('HTTPS_HOST', "") if os.getenv('HTTPS_HOST', "") != "" else os.getenv('HTTP_HOST')
_port = os.getenv('HTTPS_PORT') if os.getenv('HTTPS_HOST', "") != "" else os.getenv('HTTP_PORT')
if _port and _port not in ("80", "443"):
_port = os.getenv('HTTPS_PORT') if os.getenv('HTTPS_HOST', "") != "" else os.getenv('HTTP_PORT', '80')
# default port is 80
_protocols = {
"80": "http://",
"443": "https://"
}
if _port not in _protocols:
redirect_uris = [
'http://{}:{}/geoserver'.format(_host, _port),
'http://{}:{}/geoserver/index.html'.format(_host, _port),
]
else:
# Make sure protocol string match with GeoServer Redirect URL's protocol string
redirect_uris = [
'http://{}/geoserver'.format(_host),
'http://{}/geoserver/index.html'.format(_host),
'{}{}/geoserver'.format(_protocols[_port], _host),
'{}{}/geoserver/index.html'.format(_protocols[_port], _host),
]

app.redirect_uris = "\n".join(redirect_uris)
Expand All @@ -127,7 +133,16 @@

print("-----------------------------------------------------")
print("5. Loading fixtures")
call_command('loaddata', 'initial_data')

# Disable fixtures loading in prod by including environment variable:
# INITIAL_FIXTURES=False
import ast
# To conform with original behaviour of GeoNode, we need to set it to True
# as default value
_load_initial_fixtures = ast.literal_eval(
os.getenv('INITIAL_FIXTURES', 'True'))
if _load_initial_fixtures:
call_command('loaddata', 'initial_data')


#########################################################
Expand Down Expand Up @@ -155,14 +170,15 @@

print("-----------------------------------------------------")
print("8. Waiting for GeoServer")
_geoserver_host = os.getenv('GEOSERVER_LOCATION', 'http://geoserver:8080/geoserver')
for _ in range(60*5):
try:
requests.head("http://geoserver:8080/geoserver")
requests.head("{}".format(_geoserver_host))
break
except ConnectionError:
time.sleep(1)
else:
requests.head("http://geoserver:8080/geoserver")
requests.head("{}".format(_geoserver_host))

#########################################################
# 9. Securing GeoServer
Expand All @@ -171,10 +187,14 @@
print("-----------------------------------------------------")
print("9. Securing GeoServer")


geoserver_admin_username = os.getenv('GEOSERVER_ADMIN_USER')
geoserver_admin_password = os.getenv('GEOSERVER_ADMIN_PASSWORD')

# Getting the old password
try:
r1 = requests.get('http://geoserver:8080/geoserver/rest/security/masterpw.json',
auth=(admin_username, admin_password))
r1 = requests.get('{}/rest/security/masterpw.json'.format(_geoserver_host),
auth=(geoserver_admin_username, geoserver_admin_password))
except requests.exceptions.ConnectionError:
print("Unable to connect to GeoServer. Make sure GeoServer is started and accessible.")
exit(1)
Expand All @@ -185,8 +205,8 @@
print("Randomizing master password")
new_password = uuid.uuid4().hex
data = json.dumps({"oldMasterPassword": old_password, "newMasterPassword": new_password})
r2 = requests.put('http://geoserver:8080/geoserver/rest/security/masterpw.json', data=data,
headers={'Content-Type': 'application/json'}, auth=(admin_username, admin_password))
r2 = requests.put('{}/rest/security/masterpw.json'.format(_geoserver_host), data=data,
headers={'Content-Type': 'application/json'}, auth=(geoserver_admin_username, geoserver_admin_password))
r2.raise_for_status()
else:
print("Master password was already changed. No changes made.")
Expand Down

0 comments on commit 6a5d859

Please sign in to comment.