Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add molecule configuration for nginx role #58

Merged
merged 45 commits into from
Jan 19, 2024
Merged

Add molecule configuration for nginx role #58

merged 45 commits into from
Jan 19, 2024

Conversation

p-j-smith
Copy link
Contributor

@p-j-smith p-j-smith commented Jan 16, 2024

Fixes #50
FIxes #51
Fixes #52

  • add option to use own template for the nginx configuration (the template to be used can be selected using the nginx_conf_template variable). Define one template:
    1. a template for configuring nginx as a reverse proxy for XNAT with an optional additional default server (nginx_reverse_proxy.j2). A default server is created if nginx_add_default_server is true
  • use flat variable naming for the nginx role rather than nested
  • update mirsg.infrastructure.install_xnat group vars with new variable names. At the inventory level if nginx_use_ssl: true, we will need to set:
    • nginx_server_cert_cache: ssl.server_cert
    • nginx_server_key_cache: ssl.server_key
  • add a molecule configuration for mirsg.infrastructure.nginx. This configures nginx as a reverse proxy for a Flask app running on a Gunicorn server. Include a verify playbook to check we can connect to the server from the localhost via nginx
  • fix the molecule configuration for mirsg.infrastructure.install_xnat - the tomcat port was being published rather than the one for http connections, so nginx wasn't being used to connect from localhost even though it was configured. Add a verify playbook to check we can connect to the server from the localhost via nginx
  • add a workflow to run molecule test on the nginx role
  • add a role README

@p-j-smith p-j-smith marked this pull request as draft January 16, 2024 10:57
Comment on lines 14 to 18
server localhost:{{ nginx_upstream_port }};
}

server {
listen {{ dicom_port }};
listen {{ nginx_upstream_listen_port }};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what to call these variables, perhaps there's a better name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable names look fine to me. I wonder whether this role should XNAT agnostic though - maybe the template should live in the xnat role and here we provide a basic template of a reverse proxy conf (with TLS termination). This is how I would override for OMERO (rather than adding another conf here).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good point, I've tried to change it to make it more general now

Copy link
Contributor

@drmatthews drmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just some comments about mentioning XNAT and tomcat - I think we can make this role ignorant of both.

@@ -6,6 +6,7 @@ on:
- "roles/xnat_container_service/**"
- "playbooks/install_xnat.yml"
- "playbooks/install_container_service.yml"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- "playbooks/install_container_service.yml"
- "playbooks/install_container_service.yml"
- "playbooks/molecule/*_xnat"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the above to catch changes to the molecule configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the molecule config changes should be included with the line below (- "playbooks/molecule/**/xnat/**")

# Support for ipv6
ipv6_enabled: false

# nginx config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in the web.yml group vars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

| `nginx_group` | The OS group that will have ownership of the nginx service file and directory. Defaults to `root` |
| `nginx_log_folder` | The path to the nginx logs. Defaults to `/var/log/nginx` |
| `nginx_http_port` | The port to listen on for HTTP connections. Defaults to `80` |
| `nginx_https_port` | The port to listen on for HTTPS connections. Defaults to `80` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `nginx_https_port` | The port to listen on for HTTPS connections. Defaults to `80` |
| `nginx_https_port` | The port to listen on for HTTPS connections. Defaults to `443` |

@@ -12,6 +12,7 @@ firewalld_work_zone_open_services:
- http
- https
firewalld_public_zone_ports:
- "80"
- "8080"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to open 8080?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, I've removed it

| `nginx_http_port` | The port to listen on for HTTP connections. Defaults to `80` |
| `nginx_https_port` | The port to listen on for HTTPS connections. Defaults to `80` |
| `nginx_proxy_port` | The port to forward requests to. Defaults to `8080` (tomcat) |
| `nginx_root` | The path to search for static files. Defaults to `/usr/share/tomcat/webapps/ROOT` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is for convenience but I'm not sure about setting defaults for tomcat (or any specific web framework).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah definitely, agreed

| `nginx_https_port` | The port to listen on for HTTPS connections. Defaults to `80` |
| `nginx_proxy_port` | The port to forward requests to. Defaults to `8080` (tomcat) |
| `nginx_root` | The path to search for static files. Defaults to `/usr/share/tomcat/webapps/ROOT` |
| `nginx_conf_template` | The template to use for generating the NGINX config. See currently available [templates](templates/). Defaults to `nginx_xnat.j2`, which is used to configure NGINX as a reverse proxy for XNAT |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again I'm not sure about having an XNAT default. Can we have a basic reverse proxy config?

Comment on lines 14 to 18
server localhost:{{ nginx_upstream_port }};
}

server {
listen {{ dicom_port }};
listen {{ nginx_upstream_listen_port }};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable names look fine to me. I wonder whether this role should XNAT agnostic though - maybe the template should live in the xnat role and here we provide a basic template of a reverse proxy conf (with TLS termination). This is how I would override for OMERO (rather than adding another conf here).

@p-j-smith p-j-smith requested a review from drmatthews January 17, 2024 20:16
Copy link
Contributor

@drmatthews drmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes, looks good otherwise. I just had one additional question about the provisioner block in the base config files. If a role doesn't need to set any group vars or doesn't have a prepare.yml playbook we'll get an error. Should we move the provisioner block to the role/playbook molecule configs (although this would mean repetition since many of our roles do require group vars and a prepare playbook; doesn't have to be this PR).

playbooks/group_vars/web.yml Outdated Show resolved Hide resolved
playbooks/molecule/resources/xnat/verify.yml Outdated Show resolved Hide resolved
roles/nginx/README.md Outdated Show resolved Hide resolved
roles/nginx/defaults/main.yml Outdated Show resolved Hide resolved
roles/nginx/defaults/main.yml Outdated Show resolved Hide resolved
roles/nginx/defaults/main.yml Outdated Show resolved Hide resolved
@p-j-smith
Copy link
Contributor Author

. I just had one additional question about the provisioner block in the base config files. If a role doesn't need to set any group vars or doesn't have a prepare.yml playbook we'll get an error. Should we move the provisioner block to the role/playbook molecule configs (although this would mean repetition since many of our roles do require group vars and a prepare playbook; doesn't have to be this PR).

I think if there's no prepare playbook then molecule just silently skips that stage rather than failing. Not sure about the group vars though - I can check that and open a separate PR if that is an issue

@p-j-smith p-j-smith self-assigned this Jan 18, 2024
@p-j-smith p-j-smith requested a review from drmatthews January 18, 2024 13:19
@drmatthews
Copy link
Contributor

I think if there's no prepare playbook then molecule just silently skips that stage rather than failing. Not sure about the group vars though

Having a role without group vars definitely causes an error. I haven't tried having group vars and no prepare but would it skip if we've set a path but the file doesn't exist?

Anyway, let's look at it separately.

Copy link
Contributor

@drmatthews drmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies to keep dragging this out, I spotted something I didn't ask last time. Also, I think we should just have the one template for the nginx conf. It looks like the only difference is the server block that sets the default_server in nginx_reverse_proxy_as_default.j2. This is the template that is used in the tests but it isn't the default. Perhaps it should be the default, renamed to nginx_reverse_proxy.j2?


# nginx config
nginx_diffie_helman_size_bits: 2048
nginx_conf_template: nginx_reverse_proxy_as_default.j2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for confusion, and for not asking this in the last review, but why are we using nginx_reverse_proxy_as_default.j2 in the tests rather than nginx_reverse_proxy.j2?

@@ -0,0 +1,7 @@
---
nginx_conf_template: nginx_reverse_proxy_as_default.j2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not nginx_reverse_proxy.j2?

Copy link
Contributor Author

@p-j-smith p-j-smith Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's the same issue @paddyroddy was having in #44 in nginx_reverse_proxy.j2 - there is an additional default server, so any traffic to hosts that do not match nginx_server_name are routed to this default server. So in the verify task for this role:

hosts: localhost
tasks:
- name: Get server status
ansible.builtin.uri:
url: http://localhost:8080
method: GET
validate_certs: false
return_content: true
register: response

the host passed to nginx will be localhost, and as this doesn't match nginx_server_name the request will be sent to the default server (which returns nothing). In the test we could set the correct host through a header (-H 'Host: {{.nginx_server_name }}'), and that would work fine. However, I don't think it's possible to do this in a web browser, so if we run the converge step locally we wouldn't be able to access the web UI. To get around this, @paddyroddy is currently setting the hostname for the webserver to be localhost

roles/nginx/templates/nginx_reverse_proxy_as_default.j2 Outdated Show resolved Hide resolved
@p-j-smith
Copy link
Contributor Author

Apologies to keep dragging this out, I spotted something I didn't ask last time.

no worries! Sorry I should have explained what was going on earlier. Hopefully my comment above sheds some light on why the additional default server isn't used in the tests

Also, I think we should just have the one template for the nginx conf. It looks like the only difference is the server block that sets the default_server in nginx_reverse_proxy_as_default.j2. This is the template that is used in the tests but it isn't the default. Perhaps it should be the default, renamed to nginx_reverse_proxy.j2?

Yeah that is probably a better idea. We could have an nginx_add_default_server boolean variable, and if true then the default server is added. I think it should default to true as that's the current behaviour, but the tests will need to set it to false

Remove nginx config template that doesn't define an additional default server
This is now handled in one template witht he nginx_add_default_server variable
@p-j-smith p-j-smith requested a review from drmatthews January 19, 2024 11:32
Copy link
Contributor

@drmatthews drmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - nice work!

@p-j-smith p-j-smith merged commit 2e26c03 into main Jan 19, 2024
7 checks passed
@p-j-smith p-j-smith deleted the tests/nginx branch January 19, 2024 11:47
@paddyroddy
Copy link
Member

Can we do a release (soon)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants