Skip to content

Commit

Permalink
Registry is running in docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanthdeejay committed Dec 9, 2024
1 parent c635026 commit 0394d7d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 21 deletions.
40 changes: 40 additions & 0 deletions .env.dev_docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
APT_ENV="dev_docker"
DB_DRIVER="postgres"
DB_HOST="postgres_docker_local"
DB_NAME="apt_registry_development"
DB_USER="dev_user"
DB_PASSWORD="password"
DB_PORT=5432
DB_USE_SSL=false
COOKIE_HASH_KEY='y0b6|UBJQ(N$KB)jAJYL-aj=:q?;yK64^TPch0=|1XNnv{X@QrL#?80u$1]LcBF'
COOKIE_BLOCK_KEY='4Qdnm4acxfAILGEFQ3jUj0PoLbMWbyMm'
COOKIE_DOMAIN="localhost"
SESSION_MAX_AGE=43200
SESSION_COOKIE_NAME="aptrust_session"
FLASH_COOKIE_NAME="aptrust_flash"
PREFS_COOKIE_NAME="aptrust_prefs"
HTTPS_COOKIES=false
NSQ_URL='http://localhost:4151'
BATCH_DELETION_KEY="00000000-0000-0000-0000-000000000000"
EMAIL_SERVICE_TYPE="SMTP"
MAINTENANCE_MODE=false
ENABLE_TWO_FACTOR_SMS=false
SNS_ENDPOINT=""
AWS_SNS_USER=""
AWS_SNS_PWD=""
ENABLE_TWO_FACTOR_AUTHY=false
OTP_EXPIRATION="15m"
EMAIL_ENABLED=false
EMAIL_FROM_ADDRESS="[email protected]"
REDIS_DEFAULT_DB=0
REDIS_PASSWORD=""
REDIS_URL="localhost:6379"
RETENTION_MINIMUM_GLACIER=90
RETENTION_MINIMUM_GLACIER_DEEP=180
RETENTION_MINIMUM_WASABI=90
RETENTION_MINIMUM_STANDARD=0
LOG_FILE="~/tmp/logs/registry_dev.log"
LOG_LEVEL=0
LOG_CALLER=false
LOG_TO_CONSOLE=true
LOG_SQL=false
3 changes: 2 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DOCKER_ENV="dev_docker"
DB_DRIVER="postgres"
DB_HOST="postgres"
DB_HOST="postgres_docker_local"
DB_NAME="apt_registry_development"
DB_USER="dev_user"
DB_PASSWORD="password"
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ RUN go mod download

COPY . .

COPY db/schema.sql /docker-entrypoint-initdb.d/
COPY db/migrations/*.sql /docker-entrypoint-initdb.d/migrations/

RUN apk add --no-cache postgresql-client

RUN go build -o /main

#Environment VARS for deployment
Expand Down
1 change: 1 addition & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var allowedConfigs = []string{
"ci",
"dev",
"dev_docker",
"docker",
"integration",
"production",
Expand Down
12 changes: 6 additions & 6 deletions db/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand All @@ -23,6 +22,7 @@ var SafeEnvironments = []string{
"integration",
"test",
"travis",
"dev_docker",
}

// LoadOrder lists the names of tables for which we have fixture data
Expand Down Expand Up @@ -184,7 +184,7 @@ func loadSchema(db *pg.DB) error {
file := filepath.Join("db", "schema.sql")
ddl, err := common.LoadRelativeFile(file)
if err != nil {
return fmt.Errorf("File %s: %v", file, err)
return fmt.Errorf("file %s: %v", file, err)
}
return runTransaction(db, string(ddl))
}
Expand All @@ -200,9 +200,9 @@ func runMigrations(db *pg.DB) error {
file := info.Name()
if info.Mode().IsRegular() && strings.HasSuffix(file, ".sql") {
absPath := filepath.Join(dir, file)
ddl, err := ioutil.ReadFile(absPath)
ddl, err := os.ReadFile(absPath)
if err != nil {
return fmt.Errorf("File %s: %v", file, err)
return fmt.Errorf("file %s: %v", file, err)
}
err = runTransaction(db, string(ddl))
if err != nil {
Expand Down Expand Up @@ -247,7 +247,7 @@ func loadCSVFile(db *pg.DB, table string) error {
sql := fmt.Sprintf(`copy "%s" from '%s' csv header`, table, file)
err := runTransaction(db, sql)
if err != nil {
err = fmt.Errorf(`Error executing "%s": %v`, sql, err)
err = fmt.Errorf(`error executing "%s": %v`, sql, err)
}
return err
}
Expand Down Expand Up @@ -347,7 +347,7 @@ func panicOnWrongEnv() {
//
// Don't like the kludgy, ugly code below? See how much you
// like restoring 250GB of deleted data.
if ctx.Config.DB.Host != "localhost" {
if ctx.Config.DB.Host != "localhost" && ctx.Config.DB.Host != "postgres_docker_local" {
panic("Cannot run destructive DB operations against external servers.")
}
if !strings.HasSuffix(ctx.Config.DB.Name, "_development") &&
Expand Down
16 changes: 9 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ services:
volumes:
- ./shared/postgres_data:/var/lib/postgresql/data
- ./shared:/app/shared
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- app_network
app_network:
aliases:
- postgres_docker_local
ports:
- "5432:5432"

Expand All @@ -23,21 +25,21 @@ services:
depends_on:
- postgres
volumes:
- ./bin/linux:/app/bin
- ./bin/linux:/app/bin/linux
- ./shared:/app/shared
- ./entrypoint.sh:/entrypoint.sh
- ./.env.local:/app/.env
environment:
- APT_ENV=integration
- APT_ENV=dev_docker
- DB_DRIVER=postgres
- DB_HOST=postgres
- DB_HOST=postgres_docker_local
- DB_NAME=apt_registry_development
- DB_USER=dev_user
- DB_PASSWORD=password
- DB_PORT=5432
# entrypoint: ["/bin/sh", "/entrypoint.sh"]
entrypoint: ["/bin/sh", "/entrypoint.sh"]
# command: sh -c "sleep 20 && APT_ENV=test ./registry test ; APT_ENV=dev ./registry start"
command: sh -c "APT_ENV=dev /app/registry serve"
command: sh -c 'echo "Changing directory to /app" && cd /app && echo "Setting APT_ENV to dev_docker" && APT_ENV=dev_docker && echo "Running registry serve" && ./registry serve'
networks:
- app_network
ports:
Expand Down
25 changes: 20 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
#!/bin/sh
# Copy all executables from ./bin to /usr/local/bin
cp ./bin/linux/nsqadmin /usr/local/bin/
cp ./bin/linux/nsqd /usr/local/bin/
cp ./bin/linux/nsqlookupd /usr/local/bin/
cp ./bin/linux/redis-cli /usr/local/bin/
cp ./bin/linux/redis-server /usr/local/bin/
# cp ./bin/linux/nsqadmin /usr/local/bin/
# cp ./bin/linux/nsqd /usr/local/bin/
# cp ./bin/linux/nsqlookupd /usr/local/bin/
# cp ./bin/linux/redis-cli /usr/local/bin/
# cp ./bin/linux/redis-server /usr/local/bin/

# Wait for Postgres to be ready
until PGPASSWORD=$DB_PASSWORD pg_isready -h $DB_HOST -p $DB_PORT -U $DB_USER; do
echo "Waiting for Postgres..."
sleep 2
done

# Run schema and migration files
echo "Running schema.sql"
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f /docker-entrypoint-initdb.d/schema.sql
for file in /docker-entrypoint-initdb.d/migrations/*.sql; do
echo "Running migration $file"
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$file"
done

# Execute the original command
exec "$@"

10 changes: 8 additions & 2 deletions registry
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ fi
# ----------------------------------------------------------------------
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
DIR="./bin/linux"
echo "DIR is $DIR"
elif [[ "$OSTYPE" == "linux-musl"* ]]; then
DIR="./bin/linux"
echo "DIR is $DIR"
elif [[ "$OSTYPE" == "darwin"* ]]; then
if [[ `uname -p` == "arm" ]]; then
DIR="./bin/osx/arm64"
Expand All @@ -36,6 +40,7 @@ elif [[ "$OSTYPE" == "darwin"* ]]; then
fi
fi

echo "DIR is $DIR; OSTYPE is $OSTYPE"

# ----------------------------------------------------------------------
#
Expand Down Expand Up @@ -102,8 +107,6 @@ NSQ_PID=$!

if [[ $TRAVIS != "true" && $1 != "tests" && "$APT_ENV" != "integration" ]]; then
echo "Starting NSQ Lookup daemon"
echo "DIR is set to: $DIR"
eval "ls -l $DIR"
eval "$DIR/nsqlookupd -http-address=127.0.0.1:4161 &"
NSQ_LOOKUPD_PID=$!

Expand Down Expand Up @@ -135,6 +138,9 @@ elif [[ $1 == "serve" ]]; then
if [[ "$APT_ENV" == "integration" ]]; then
echo "Loading integration fixtures..."
APT_ENV=$APT_ENV go run loader/load_fixtures.go
elif [[ "$APT_ENV" == "dev_docker" ]]; then
echo "Loading dev_docker fixtures..."
APT_ENV=$APT_ENV go run loader/load_fixtures.go
fi
echo "Starting registry app..."
APT_ENV=$APT_ENV go run $BUILD_TAGS registry.go
Expand Down

0 comments on commit 0394d7d

Please sign in to comment.