Skip to content

Commit

Permalink
Improved devcontainer & created auto import for the database export
Browse files Browse the repository at this point in the history
Signed-off-by: JL102 <[email protected]>
  • Loading branch information
JL102 committed Jan 26, 2024
1 parent dbaf401 commit 0adf046
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [3000, 27017],
"forwardPorts": [3000, 27017],
"portsAttributes": {
"3000": {
"label": "primary",
Expand All @@ -37,7 +37,7 @@
},

// Use 'postCreateCommand' to run commands after the container is created.
"postAttachCommand": "bash .devcontainer/setup.sh",
"postCreateCommand": "bash .devcontainer/setup.sh",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
Expand Down
90 changes: 45 additions & 45 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
args:
# Update 'VARIANT' to pick an LTS version of Node.js: 18, 16, 14.
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: 18-buster
volumes:
- ..:/workspace:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db

# Uncomment the next line to use a non-root user for all processes.
# user: node

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

db:
image: mongo:latest
restart: unless-stopped
volumes:
# - mongodb-data:/data/db
- /var/lib/docker/codespacemount/workspace/scoutradioz-db:/data/db

# Uncomment to change startup options
# environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: example
# MONGO_INITDB_DATABASE: app

# Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

# volumes:
# mongodb-data:
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
args:
# Update 'VARIANT' to pick an LTS version of Node.js: 18, 16, 14.
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: 18-buster
volumes:
- ..:/workspace:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db

# Uncomment the next line to use a non-root user for all processes.
# user: node

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

db:
image: mongo:latest
restart: unless-stopped
volumes:
- mongodb-data:/data/db
# - /var/lib/docker/codespacemount/workspace/scoutradioz-db:/data/db

# Uncomment to change startup options
# environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: example
# MONGO_INITDB_DATABASE: app

# Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
mongodb-data:
14 changes: 14 additions & 0 deletions .devcontainer/import-database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Import database
echo "Unzipping database export..."
mkdir -p /tmp/db-export
unzip .devcontainer/db_export.zip -d /tmp/db-export/

echo "Dropping existing database..."
mongosh app --eval "db.dropDatabase();"

echo "Restoring database export..."
mongorestore /tmp/db-export --db app

rm -r /tmp/db-export
16 changes: 9 additions & 7 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/bin/bash

# Import database if it doesn't already exist
if [[ -d "/workspaces/scoutradioz-dbdump" && $(mongosh --eval "db.getMongo().getDBNames().indexOf('app')" --quiet) -lt 0 ]]; then
mongorestore "/workspaces/scoutradioz-dbdump" --db app
if [[ $(mongosh --eval "db.getMongo().getDBNames().indexOf('app')" --quiet) -lt 0 ]]; then
bash .devcontainer/import-database.sh
else
echo "MongoDB 'app' database already exists, not restoring"
fi

# Copy sample databases.json file if it doesn't already exist
dbsfile=primary/databases.json
if [ ! -f $dbsfile ]; then
touch $dbsfile
printf "{
dbsfiles=(primary/databases.json upload/databases.json voyager/src/databases.json)
for dbsfile in "${dbsfiles[@]}"; do
if [ ! -f $dbsfile ]; then
touch $dbsfile
printf "{
\t\"dev\": {
\t\t\"url\": \"mongodb://localhost:27017/app\"
\t},
Expand All @@ -20,6 +21,7 @@ if [ ! -f $dbsfile ]; then
\t}
}" >> $dbsfile
fi
done

# Install node modules
yarn run setup
yarn setup
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Set core.autocrlf to true for the rest of the repository
* text=auto eol=auto

# Set core.autocrlf to false for the .devcontainer folder
.devcontainer/* -text eol=lf

0 comments on commit 0adf046

Please sign in to comment.