Skip to content

Commit f5bbc5d

Browse files
committed
Merge branch 'feature/uwsgi' into master
2 parents 0176cf0 + 321e287 commit f5bbc5d

15 files changed

+122
-57
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ install:
2323
script:
2424
- ansible-playbook tests/deploy.yml -i tests/inventory --syntax-check
2525
- ansible-playbook tests/deploy.yml -i tests/inventory
26-
- unbuffer ansible-playbook -vvv tests/deploy.yml -i tests/inventory >/tmp/idempotency.log
27-
2>&1
26+
- unbuffer ansible-playbook -vv tests/deploy.yml -i tests/inventory >/tmp/idempotency.log 2>&1
2827
- 'grep -A1 "PLAY RECAP" /tmp/idempotency.log | grep -qP "changed=0.*failed=0" &&
2928
(echo "Idempotence: PASS"; exit 0) || (echo "Idempotence: FAIL"; cat /tmp/idempotency.log;
3029
exit 1)'

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
lae.netbox
55
=========
66

7-
Installs and configures DigitalOcean's NetBox.
7+
Installs and configures DigitalOcean's [NetBox](https://github.com/digitalocean/netbox).
8+
9+
This role deploys NetBox inside of a virtualenv, uses uWSGI as its frontend
10+
(can be used standalone or behind a load balancer), works with both Python 2/3
11+
and has been tested across CentOS 7/Debian 8/Ubuntu 16.
812

913
Requirements
1014
------------
@@ -38,13 +42,21 @@ See the *Example Playbook* section for more information on configuring the DB.
3842
default) stored in `/srv/netbox/shared/generated_secret_key`. This will then be
3943
used by the role to configure NetBox unless `netbox_secret_key` is later defined.
4044

41-
`netbox_bind_address` is the address Gunicorn will be configured to listen on.
42-
4345
`netbox_allowed_hosts` is a list defining `ALLOWED_HOSTS`.
4446

4547
To load the initial data shipped by NetBox, set `netbox_load_initial_data` to
4648
true. Otherwise, this role will deploy NetBox with an empty slate.
4749

50+
Configure `netbox_uwsgi_socket` to either be a TCP address or UNIX socket to
51+
bind to. By default, this role will configure uWSGI to serve a full uWSGI HTTP
52+
web server. You can set `netbox_behind_load_balancer` to `true` to use an uWSGI
53+
socket (and you can also set `netbox_uwsgi_protocol` to `http` to configure
54+
uWSGI to use an HTTP-speaking socket instead).
55+
56+
By default, NetBox will be configured to output to `/srv/netbox/shared/application.log`
57+
and `/srv/netbox/shared/requests.log`. You can override these with a valid
58+
uWSGI logger by setting `netbox_uwsgi_logger` and `netbox_uwsgi_req_logger`.
59+
4860

4961
Example Playbook
5062
----------------
@@ -61,6 +73,7 @@ socket to authenticate with the Postgres server.
6173
vars:
6274
netbox_stable: true
6375
netbox_database_socket: "{{ postgresql_unix_socket_directories[0] }}"
76+
netbox_uwsgi_socket: "0.0.0.0:80"
6477
netbox_allowed_hosts:
6578
- netbox.idolactiviti.es
6679
postgresql_users:
@@ -77,8 +90,9 @@ installing NetBox on to authenticate with it over TCP:
7790
- lae.netbox
7891
vars:
7992
netbox_stable: true
93+
netbox_uwsgi_socket: "0.0.0.0:80"
8094
netbox_allowed_hosts:
81-
- netbox.idolactiviti.es
95+
- "{{ inventory_hostname }}"
8296
netbox_database_host: pg-netbox.idolactiviti.es
8397
netbox_database_port: 15432
8498
netbox_database_name: netbox_prod

defaults/main.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ netbox_git: false
88
netbox_git_version: develop
99
netbox_git_uri: "https://github.com/digitalocean/netbox.git"
1010

11+
netbox_behind_load_balancer: false
12+
1113
netbox_database: netbox
1214
netbox_database_user: netbox
1315
#netbox_database_password: changeme
@@ -16,7 +18,6 @@ netbox_database_port: 5432
1618
#netbox_database_socket: /var/run/postgresql
1719

1820
#netbox_secret_key:
19-
netbox_bind_address: "127.0.0.1:8001"
2021
netbox_allowed_hosts:
2122
- localhost
2223
- 127.0.0.1
@@ -36,4 +37,10 @@ netbox_current_path: "{{ netbox_home }}/current"
3637
netbox_shared_path: "{{ netbox_home }}/shared"
3738
netbox_python: 3
3839

40+
netbox_uwsgi_socket: "127.0.0.1:8000"
41+
netbox_uwsgi_protocol: uwsgi
42+
netbox_uwsgi_processes: "{{ ansible_processor_vcpus }}"
43+
netbox_uwsgi_logger: "file:{{ netbox_shared_path }}/application.log"
44+
netbox_uwsgi_req_logger: "file:{{ netbox_shared_path }}/requests.log"
45+
3946
netbox_load_initial_data: false

handlers/main.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
---
22
# handlers file for lae.netbox
3-
- name: restart netbox
3+
- name: reload netbox
44
systemd:
55
name: netbox.service
6+
state: reloaded
7+
8+
- name: reload netbox and unit file
9+
systemd:
10+
name: netbox.service
11+
state: reloaded
612
daemon_reload: yes
7-
state: restarted

tasks/configure_gunicorn.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

tasks/deploy_netbox.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
dest: "{{ netbox_shared_path }}/configuration.py"
2626
mode: 0640
2727
notify:
28-
- restart netbox
28+
- reload netbox
2929

3030
- name: Symlink NetBox configuration file into the active NetBox release
3131
file:
@@ -53,5 +53,3 @@
5353
virtualenv: "{{ netbox_virtualenv_path }}"
5454
when:
5555
- netbox_load_initial_data
56-
57-
- include: configure_gunicorn.yml

tasks/install_via_git.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
dest: "{{ netbox_git_path }}"
66
version: "{{ netbox_git_version }}"
77
notify:
8-
- restart netbox
8+
- reload netbox
99

1010
- name: Symlink git repository to current NetBox directory
1111
file:

tasks/install_via_stable.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
creates: "{{ netbox_stable_path }}"
77
remote_src: True
88
notify:
9-
- restart netbox
9+
- reload netbox
1010

1111
- name: Symlink stable release to current NetBox directory
1212
file:

tasks/main.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,28 @@
5454
become: True
5555
become_user: "{{ netbox_user }}"
5656

57-
- name: Install systemd unit file for NetBox
57+
- name: Install uWSGI via pip
58+
pip:
59+
name: uwsgi
60+
state: latest
61+
executable: "{{ netbox_pip3_binary if (netbox_python == 3) else netbox_pip2_binary }}"
62+
63+
- name: Configure uWSGI NetBox application
64+
template:
65+
src: uwsgi.ini.j2
66+
dest: "{{ netbox_shared_path }}/uwsgi.ini"
67+
notify:
68+
- reload netbox
69+
70+
- name: Install uWSGI/NetBox unit file
5871
template:
5972
src: netbox.service.j2
6073
dest: /lib/systemd/system/netbox.service
61-
mode: 0644
6274
notify:
63-
- restart netbox
75+
- reload netbox and unit file
6476

65-
- name: Start and enable NetBox
66-
systemd:
67-
name: netbox.service
77+
- name: Start and enable uWSGI/NetBox
78+
service:
79+
name: netbox
6880
state: started
6981
enabled: yes

templates/configuration.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# {{ ansible_managed }}
12
ALLOWED_HOSTS = {{ netbox_allowed_hosts | to_json }}
23
DATABASE = {
34
'NAME': '{{ netbox_database }}',

templates/gunicorn_config.py.j2

Lines changed: 0 additions & 5 deletions
This file was deleted.

templates/netbox.service.j2

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
# {{ ansible_managed }}
12
[Unit]
2-
Description=NetBox datacenter management application
3+
Description=NetBox IPAM/DCIM tool
34
Documentation=http://netbox.readthedocs.io/en/{{ 'latest' if netbox_git else 'stable' }}/
4-
After=network.target
5+
After=syslog.target
56

67
[Service]
8+
ExecStart=/usr/bin/env uwsgi --ini {{ netbox_shared_path }}/uwsgi.ini
9+
ExecReload=/bin/kill -HUP $MAINPID
10+
ExecStop=/bin/kill -INT $MAINPID
711
User={{ netbox_user }}
812
Group={{ netbox_group }}
9-
ExecStart={{ netbox_virtualenv_path }}/bin/gunicorn -c {{ netbox_shared_path }}/gunicorn_config.py netbox.wsgi
10-
WorkingDirectory={{ netbox_current_path }}/netbox
11-
Restart=always
12-
KillMode=process
13+
Restart=on-failure
14+
SuccessExitStatus=15 17 29 30
15+
KillSignal=SIGQUIT
16+
Type=notify
17+
StandardError=syslog
18+
NotifyAccess=all
19+
PrivateTmp=yes
20+
ProtectSystem=full
21+
ReadWriteDirectories={{ netbox_shared_path }}
22+
{% if netbox_database_socket is defined %}
23+
ReadWriteDirectories={{ netbox_database_socket }}
24+
{% endif %}
25+
DeviceAllow=/dev/null rw
26+
DeviceAllow=/dev/urandom r
27+
DeviceAllow=/dev/zero r
28+
ProtectHome=yes
29+
NoNewPrivileges=yes
1330

1431
[Install]
1532
WantedBy=multi-user.target

templates/uwsgi.ini.j2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# {{ ansible_managed }}
2+
[uwsgi]
3+
master=true
4+
{% if netbox_behind_load_balancer %}
5+
protocol={{ netbox_uwsgi_protocol }}
6+
socket={{ netbox_uwsgi_socket }}
7+
{% else %}
8+
http={{ netbox_uwsgi_socket }}
9+
{% endif %}
10+
uid={{ netbox_user }}
11+
gid={{ netbox_group }}
12+
processes={{ netbox_uwsgi_processes }}
13+
module=netbox.wsgi
14+
virtualenv={{ netbox_virtualenv_path }}
15+
chdir={{ netbox_current_path }}/netbox
16+
static-map=/static={{ netbox_current_path }}/netbox/static
17+
logger={{ netbox_uwsgi_logger }}
18+
req-logger={{ netbox_uwsgi_req_logger }}
19+
20+
# vim: ft=dosini

tests/group_vars/netbox

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
ansible_ssh_user: root
3-
netbox_bind_address: "{{ ansible_default_ipv4.address }}:8080"
3+
netbox_uwsgi_socket: "0.0.0.0:8080"
44
netbox_allowed_hosts:
5-
- "{{ ansible_default_ipv4.address }}"
5+
- "{{ inventory_hostname }}"
66
netbox_database_socket: "{{ postgresql_unix_socket_directories[0] }}"
77
postgresql_users:
88
- name: "{{ netbox_database_user }}"

tests/test.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
---
22
- hosts: netbox
33
tasks:
4-
- name: Print out test environment definitions for each container
5-
debug:
6-
var: "{{ item }}"
7-
with_items:
8-
- netbox_stable
9-
- netbox_git
10-
- netbox_python
11-
- name: Ensure that NetBox returns a successful HTTP response
12-
uri:
13-
url: "http://{{ netbox_bind_address }}"
14-
- name: Print out NetBox/gunicorn log
15-
shell: journalctl --no-pager -xu netbox.service
4+
- block:
5+
- name: Print out test environment definitions for each container
6+
debug:
7+
var: "{{ item }}"
8+
with_items:
9+
- netbox_stable
10+
- netbox_git
11+
- netbox_python
12+
- name: Ensure that NetBox returns a successful HTTP response
13+
uri:
14+
url: "http://{{ inventory_hostname }}:8080"
15+
- name: uWSGI/NetBox service status
16+
shell: "systemctl status netbox.service"
17+
changed_when: false
18+
- name: uWSGI/NetBox service journal
19+
shell: "journalctl --no-pager -xu netbox.service"
20+
changed_when: false
21+
- name: NetBox application log
22+
shell: cat /srv/netbox/shared/application.log
23+
changed_when: false
24+
ignore_errors: yes

0 commit comments

Comments
 (0)