Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Allow /version to be defined by environment variables #1015

Merged
merged 4 commits into from
Nov 29, 2022

Conversation

AetherUnbound
Copy link
Contributor

@AetherUnbound AetherUnbound commented Nov 29, 2022

Fixes

This is necessary for continued work on https://github.com/WordPress/openverse-infrastructure/issues/175

Description

This PR makes a few changes to the nginx target image for the API in order to prevent the need for mounting any files into the docker container. Previously, the /version was configured to return the contents of a version.json file. This file was created during deployment and mounted into the nginx image:

https://github.com/WordPress/openverse-infrastructure/blob/369d7858636797de276dbfc299b90b822a5ac527/modules/services/openverse-api/init.tpl#L79-L85

The issue with this is that we are trying to avoid the need for mounting anything into our API-related containers. This was the last necessary item that was being mounted. You can see that this endpoint is not served by the API by running just up and visiting http://localhost:50280/version.

I've altered the image to construct the response from /version using environment variables rather than reading from a file on-disk. This involved a few changes to the environment substitution setup, which I was unfamiliar with (there are seemingly no references to NGINX_ENVSUBST_FILTER on GitHub where it's being defined by anyone other than us!). I added a few more comments to try and provide reference to what was being used, and where.

I also addressed an issue wherein we were erroneously referencing variables inside the nginx template with \$ rather than just $. This was an artifact of constructing the config inside of the init.tpl file during deployment (which required that we escape the $ character since it was used for its own interpolation). This was producing logs that looked like the following (note the extra \ characters before field values):

{"time_local":"\29/Nov/2022:00:33:08 +0000","remote_addr":"\172.17.0.1","remote_user":"\","request":"\GET / HTTP/1.1","status": "\400","host_header": "\localhost","body_bytes_sent":\155,"request_time":"\0.020","http_referrer":"\","http_user_agent":"\Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0","upstream_response_time":\0.020,"http_x_forwarded_for":"\"}

Lastly, I added a just recipe for running the nginx image locally with some suggestions on how to test that it works.

Testing Instructions

  1. Before checking out this branch, verify that the API returns a 404 on http://localhost:50280/version after running just up
  2. Run just nginx
  3. Visit http://localhost:9090/static/admin/css/base.css and verify that results are returned (this should be from api.openverse.engineering)
  4. Visit http://localhost:9090/version and verify that you receive something similar to the following:
{

    "release": "47521cd5b288c35afaf0f996015e8122bbc1ccf8",
    "environment": "local"

}

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

Sorry, something went wrong.

We can remove the _api-up dependency from collectstatic since it's already called in the dj recipe
These were an artifact of having to create the nginx config within the init.tpl for the API, which required escaping the $. These are no longer needed and actually ended up producing erroneous output.
@AetherUnbound AetherUnbound requested a review from a team as a code owner November 29, 2022 01:40
@AetherUnbound AetherUnbound added 🐳 tech: docker Requires familiarity with Docker 💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟨 priority: medium Not blocking but should be addressed soon labels Nov 29, 2022
@AetherUnbound
Copy link
Contributor Author

@sarayourfriend I've added you as a reviewer since you were the original author of #990 and are most familiar with the code!

@github-actions
Copy link

github-actions bot commented Nov 29, 2022

API Developer Docs Preview: Ready

https://wordpress.github.io/openverse-api/_preview/1015

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Ah, thank you for catching this! I totally forgot about this requirement when working on the original issue. Thanks for fixing the logging as well, I had not noticed that.

ENV NGINX_ENVSUBST_FILTER="DJANGO_UPSTREAM_URL"
# Only environment variables with this prefix will be available in the template
ENV NGINX_ENVSUBST_FILTER="DJANGO_NGINX_"
ENV DJANGO_NGINX_ENVIRONMENT="local"
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll need to be sure to pass this environment variable in the ECS configuration as well, once we get there. That at least is constant for the environment, so not an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, same with the DJANGO_NGINX_GIT_REVISION!

@sarayourfriend
Copy link
Contributor

Regarding NGINX_ENVSUBST_FILTER, btw... I think I ended up having to read the various entrypoint scripts that the Nginx image comes with to discover this! It is not documented in the README for the image on docker hub 😢

@AetherUnbound
Copy link
Contributor Author

Regarding NGINX_ENVSUBST_FILTER, btw... I think I ended up having to read the various entrypoint scripts that the Nginx image comes with to discover this! It is not documented in the README for the image on docker hub 😢

That's exactly what I did as well! And what I ended up having to link to. I even had to play around with awk a bit locally to figure out if we could use a prefix or if we had to match the whole variable name 😅 definitely leaves something to be desired!

Copy link
Contributor

@stacimc stacimc left a comment

Choose a reason for hiding this comment

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

Thank you for the testing instructions! LGTM 😄

@AetherUnbound AetherUnbound merged commit c865f80 into main Nov 29, 2022
@AetherUnbound AetherUnbound deleted the feature/nginx-improvements branch November 29, 2022 03:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟨 priority: medium Not blocking but should be addressed soon 🐳 tech: docker Requires familiarity with Docker
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants