Skip to content

Commit a8fe032

Browse files
authored
Merge pull request #21 from aarranz/feature/allow-user-flag
Allow to execute WireCloud image using a custom user
2 parents 3a519ca + 2c5aca3 commit a8fe032

8 files changed

Lines changed: 132 additions & 4 deletions

1.2/docker-compose-custom-user.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "3"
2+
3+
services:
4+
5+
wirecloud:
6+
restart: always
7+
image: fiware/wirecloud:1.2
8+
# If you want to use a user from the host, provide here the id of the
9+
# user, e.g. `export WIRECLOUD_USER=$(id -u username)`
10+
user: ${WIRECLOUD_USER:-0}
11+
ports:
12+
- 80:8000
13+
environment:
14+
- DEBUG=True
15+
volumes:
16+
- ./wirecloud-data:/opt/wirecloud_instance/data
17+
- ./wirecloud-static:/var/www/static

1.2/docker-entrypoint.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ case "$1" in
2525
manage.py migrate --fake-initial
2626
manage.py populate
2727

28-
gosu wirecloud /usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
28+
# allow the container to be started with `--user`
29+
if [ "$(id -u)" = '0' ]; then
30+
gosu wirecloud /usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
31+
else
32+
/usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
33+
fi
2934
;;
3035
esac

1.2/manage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/usr/bin/env bash
22
cd /opt/wirecloud_instance
3-
gosu wirecloud python manage.py $@
3+
if [ "$(id -u)" = '0' ]; then
4+
gosu wirecloud python manage.py $@
5+
else
6+
python manage.py $@
7+
fi

1.2/tests.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
55

6+
import grp
7+
import pwd
68
import os
79
import shutil
810
import time
@@ -212,5 +214,41 @@ def test_login_should_redirect_to_idm(self):
212214
self.assertEqual(parameters['redirect_uri'], ['http://localhost/complete/fiware/'])
213215

214216

217+
class CustomUserTests(unittest.TestCase, WireCloudTests):
218+
219+
@classmethod
220+
def setUpClass(cls):
221+
print("\n################################################################################\n")
222+
print("#")
223+
print("# Initializing custom user test case")
224+
print("#\n")
225+
226+
sh.adduser("mycustomuser", system=True, group=True, shell="/bin/bash")
227+
uid = pwd.getpwnam("mycustomuser").pw_uid
228+
gid = grp.getgrnam("mycustomuser").gr_gid
229+
os.mkdir('wirecloud-data', 0o700)
230+
os.chown('wirecloud-data', uid, gid)
231+
os.mkdir('wirecloud-static', 0o700)
232+
os.chown('wirecloud-static', uid, gid)
233+
234+
env = {}
235+
env.update(os.environ)
236+
env["WIRECLOUD_USER"] = "{}".format(uid)
237+
sh.docker_compose("-f", "docker-compose-custom-user.yml", "up", d=True, remove_orphans=True, _env=env, _fg=True)
238+
wait_until_running()
239+
print()
240+
241+
@classmethod
242+
def tearDownClass(cls):
243+
print()
244+
print("#")
245+
print("# Removing containers and volumes")
246+
print("#\n")
247+
sh.docker_compose.down(remove_orphans=True, v=True, _fg=True)
248+
shutil.rmtree('wirecloud-data')
249+
shutil.rmtree('wirecloud-static')
250+
print()
251+
252+
215253
if __name__ == "__main__":
216254
unittest.main(verbosity=2)

dev/docker-compose-custom-user.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "3"
2+
3+
services:
4+
5+
wirecloud:
6+
restart: always
7+
image: fiware/wirecloud:dev
8+
# If you want to use a user from the host, provide here the id of the
9+
# user, e.g. `export WIRECLOUD_USER=$(id -u username)`
10+
user: ${WIRECLOUD_USER:-0}
11+
ports:
12+
- 80:8000
13+
environment:
14+
- DEBUG=True
15+
volumes:
16+
- ./wirecloud-data:/opt/wirecloud_instance/data
17+
- ./wirecloud-static:/var/www/static

dev/docker-entrypoint.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ case "$1" in
2525
manage.py migrate --fake-initial
2626
manage.py populate
2727

28-
gosu wirecloud /usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
28+
# allow the container to be started with `--user`
29+
if [ "$(id -u)" = '0' ]; then
30+
gosu wirecloud /usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
31+
else
32+
/usr/local/bin/gunicorn wirecloud_instance.wsgi:application --forwarded-allow-ips "${FORWARDED_ALLOW_IPS}" -w 2 -b :8000
33+
fi
2934
;;
3035
esac

dev/manage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/usr/bin/env bash
22
cd /opt/wirecloud_instance
3-
gosu wirecloud python manage.py $@
3+
if [ "$(id -u)" = '0' ]; then
4+
gosu wirecloud python manage.py $@
5+
else
6+
python manage.py $@
7+
fi

dev/tests.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
55

6+
import grp
7+
import pwd
68
import os
79
import shutil
810
import time
@@ -212,5 +214,41 @@ def test_login_should_redirect_to_idm(self):
212214
self.assertEqual(parameters['redirect_uri'], ['http://localhost/complete/fiware/'])
213215

214216

217+
class CustomUserTests(unittest.TestCase, WireCloudTests):
218+
219+
@classmethod
220+
def setUpClass(cls):
221+
print("\n################################################################################\n")
222+
print("#")
223+
print("# Initializing custom user test case")
224+
print("#\n")
225+
226+
sh.adduser("mycustomuser", system=True, group=True, shell="/bin/bash")
227+
uid = pwd.getpwnam("mycustomuser").pw_uid
228+
gid = grp.getgrnam("mycustomuser").gr_gid
229+
os.mkdir('wirecloud-data', 0o700)
230+
os.chown('wirecloud-data', uid, gid)
231+
os.mkdir('wirecloud-static', 0o700)
232+
os.chown('wirecloud-static', uid, gid)
233+
234+
env = {}
235+
env.update(os.environ)
236+
env["WIRECLOUD_USER"] = "{}".format(uid)
237+
sh.docker_compose("-f", "docker-compose-custom-user.yml", "up", d=True, remove_orphans=True, _env=env, _fg=True)
238+
wait_until_running()
239+
print()
240+
241+
@classmethod
242+
def tearDownClass(cls):
243+
print()
244+
print("#")
245+
print("# Removing containers and volumes")
246+
print("#\n")
247+
sh.docker_compose.down(remove_orphans=True, v=True, _fg=True)
248+
shutil.rmtree('wirecloud-data')
249+
shutil.rmtree('wirecloud-static')
250+
print()
251+
252+
215253
if __name__ == "__main__":
216254
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)