forked from SamP20/hackspace-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 4
Build out dev container #11
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
Open
dAdil
wants to merge
9
commits into
bristolhackspace:wip/samp/sample-data
Choose a base branch
from
dAdil:Build-out-dev-container
base: wip/samp/sample-data
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83863f4
Add readme and new developer documentation
dAdil 3d5c6a4
Set port forwarding for postgres in devcontainer.json
dAdil 3e4f821
Build out the dev container to simplify development setup and flow
dAdil ed36be4
Add readme and new developer documentation
dAdil c737d6b
Set port forwarding for postgres in devcontainer.json
dAdil 4d45b60
Add sample dataset
SamP20 f131572
Merge branch 'main' of https://github.com/bristolhackspace/hackspace-…
dAdil a0bb8fa
Move towards Flask-Migrate for database schema management, pin dev co…
dAdil 8847e62
Merge branch 'wip/samp/sample-data' into Build-out-dev-container
dAdil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,14 @@ | ||
| // For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
| // README at: https://github.com/devcontainers/templates/tree/main/src/postgres | ||
| { | ||
| "name": "Python 3 & PostgreSQL", | ||
| "dockerComposeFile": "docker-compose.yml", | ||
| "service": "app", | ||
| "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}" | ||
|
|
||
| // Features to add to the dev container. More info: https://containers.dev/features. | ||
| // "features": {}, | ||
|
|
||
| // Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
| // This can be used to network with other containers or the host. | ||
| // "forwardPorts": [5000, 5432], | ||
|
|
||
| // Use 'postCreateCommand' to run commands after the container is created. | ||
| // "postCreateCommand": "pip install --user -r requirements.txt", | ||
|
|
||
| // Configure tool-specific properties. | ||
| // "customizations": {}, | ||
|
|
||
| // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. | ||
| // "remoteUser": "root" | ||
| "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", | ||
| "forwardPorts": [ 5432 ], | ||
| "customizations": { | ||
| "vscode": { | ||
| "extensions": [ | ||
| "ms-python.python" | ||
| ] | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,4 +129,5 @@ dmypy.json | |
| .pyre/ | ||
|
|
||
|
|
||
| *.csv | ||
| *.csv | ||
| last_migrated.txt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "Python Debugger: Flask", | ||
| "type": "debugpy", | ||
| "request": "launch", | ||
| "module": "flask", | ||
| "preLaunchTask": "Migrate", | ||
| "env": { | ||
| "FLASK_APP": "hackspace_mgmt/__init__.py", | ||
| "FLASK_DEBUG": "1" | ||
| }, | ||
| "args": [ | ||
| "run", | ||
| "--no-debugger", | ||
| "--no-reload" | ||
| ], | ||
| "jinja": true, | ||
| "autoStartBrowser": true, | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "Migrate", | ||
| "type": "shell", | ||
| "command": ". ${workspaceFolder}/migrate.sh" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,24 @@ | ||
| # hackspace-mgmt | ||
| Hackspace management portal | ||
| # Hackspace Portal | ||
|
|
||
| Welcome! | ||
|
|
||
| This repository contains the source code for the Hackspace Portal: a small website used to keep track of everything members are up to in the hackspace. | ||
|
|
||
| Whether you're a seasoned hacker, a novice tinkerer, or simply curious about the world of making, this portal is your gateway to a vibrant community of like-minded individuals (nice intro ChatGPT). | ||
|
|
||
| ## Purpose of the Portal | ||
|
|
||
| The portal serves as a point of administration for all members. It is commonly used for: | ||
|
|
||
| - **Membership Administration**: Enrol members, manage personal details, forum accounts and references to payment information. | ||
| - **Access Cards**: Associate access cards with members and revoke them remotely. | ||
| - **Inductions**: Pass a test to gain access to some of the more scary machines, then unlock them with your card. | ||
| - **Storage Labels**: Track short term storage labels. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| See the `docs` folder for some guidance on setting up your dev environment and creating your first user. | ||
|
|
||
| ## Contributing | ||
|
|
||
| We welcome contributions from all members of the community! Whether you're a seasoned developer or just getting started, there are plenty of ways to get involved. If you have ideas for new features, encounter any bugs, or simply want to help improve the platform, we'd love to hear from you. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Dev Environment | ||
|
|
||
| This is a somewhat straightforward python Flask app, backed by a Postgres database. | ||
|
|
||
| ### With the Dev Container | ||
|
|
||
| The repository has a dev container configured which you can use if you wish. This greatly simplifies the development environment setup process as it installs a relevant version of python, the app's dependencies, postgres server and database, some CLI tools and even some extensions into your an isolated workspace. | ||
|
|
||
| 1. Install a dev container capable IDE (we're using vscode by default), Git and Docker/podman/your container orchestration tool of choice. | ||
| 2. Ensure you have the Dev Containers extension installed. | ||
| 3. Clone and open the repository. Vscode will nudge you to open it in a dev container. | ||
| 4. Click "Run" in the debug pane. | ||
|
|
||
| Note: Both the dev container and postgres can be deleted and rebuilt as needed without losing your DB. They will automatically re-attach themselves to the `hackspace-mgmt_devcontainer_postgres-data` volume which is the important one not to lose as it contains the database itself. | ||
|
|
||
| ### Without the Dev Container | ||
|
|
||
| <details> | ||
| <summary>Expand</summary> | ||
|
|
||
| #### Requiments: | ||
| - Python 3.9+ | ||
| - PostgreSQL 14+ | ||
| - Some ability to run Postgres queries directly - pgAdmin is a good GUI option, while `psql` is a good CLI. Both are bundled with Postgres. | ||
| - Git | ||
|
|
||
| In a terminal, navigate to the `hackspace-mgmt` folder and create a virtual environment with `python3 -m venv .venv`. This environment can then be activated with `source .venv/bin/activate` or `.venv/Scripts/activate.ps1` depending on which OS/terminal you are using. | ||
|
|
||
| Update pip with `python -m pip install --upgrade pip`. | ||
|
|
||
| Install the requirements with `pip install -r requirements.txt`. | ||
|
|
||
| You should now be able to run the server with `flask --app hackspace_mgmt:create_app --debug run` or by launching via the vscode debug pane. | ||
|
|
||
| Navigate to `http://127.0.0.1:5000/admin/` and you should be able to see a bare admin page! | ||
|
|
||
| </details> | ||
|
|
||
| ### Database Setup | ||
|
|
||
| The database is nominally configured by running each of the SQL scripts inside the `migration` folder sequentially. Anytime a new one is added, this will need to be applied before the app is started. | ||
|
|
||
| If you're using a dev container, the `migrate.sh` script will be run prior to any debug session which does this automagically! | ||
|
|
||
| Tooling-wise, you may wish to use `psql` - a CLI for interacting with the datbase directly. The dev container comes with it installed, but because it is running in a container, you need to use `psql -h localhost -U postgres (-d hackspace) etc` to connect. | ||
|
|
||
| Separately, you may find it helpful to install `pgAdmin`. This is a free GUI tool for exploring Postgres, similar to SSMS. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Creating Your First User | ||
|
|
||
| In order to use the member facing page of the server, we will need to create a test user we can login with. | ||
|
|
||
| 1. Create a test card. From the `/admin` page, navigate to _Membership->Card_, then click _Create_. Enter 0 as the serial for now. This is an integer, but everything else uses the hexadecimal representation. Enter 123 for the _Number On Front_, leave everything else as it is and click _Save_. | ||
| 2. Create a test member. From the `/admin` page, navigate to _Membership->Member_, then click _Create_. Enter anything you want into the name and email fields. In the _Cards_ field, type the number 123 then select the entry that appears. Click _Save_ to finish. | ||
|
|
||
| ## Logging In | ||
|
|
||
| You should now be able to navigate to `http://127.0.0.1:5000`. This will show the login page that's displayed on the member portal in the Hackspace. | ||
|
|
||
| Behind the scenes, this page listens for keyboard input and will log you in when a valid card serial is "typed" in. To login with the test card, type _0 then Enter_. You will have to be quick as there is a 0.5s timeout after the page loads! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Create the hackspace DB if it doesn't already exist | ||
| psql -h localhost -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'hackspace'" | \ | ||
| grep -q 1 || psql -h localhost -U postgres -c "CREATE DATABASE hackspace" | ||
|
|
||
| # Create the schema_version file if it doesn't exist | ||
| if ! [ -e last_migrated.txt ] ; then | ||
| echo -n "-1" > last_migrated.txt | ||
| fi | ||
|
|
||
| last_migrated_file=$( cat last_migrated.txt ) | ||
| needs_migrating=false | ||
|
|
||
| for file in migration/* | ||
| do | ||
| if [ "$last_migrated_file" = file ] || [ "$last_migrated_file" = "-1" ] ; then | ||
| needs_migrating=true | ||
| fi | ||
|
|
||
| if [ "$needs_migrating" = true ] ; then | ||
| echo "Migrating $file" | ||
| psql -h localhost -U postgres -d hackspace -f "$file" | ||
| echo "$file" > last_migrated.txt | ||
| fi | ||
| done | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This started life as a 1-liner and then grew. It should have probably become a python script as soon as I added statefulness via
last_migrated.txt, but here we are :( If you feel strongly about it let me know and I'll convert it across.