From 0394d7d0616cd2bdde3deb4e0ecbdac85f6a47f3 Mon Sep 17 00:00:00 2001 From: Jay Dungavath Date: Mon, 9 Dec 2024 11:49:48 -0500 Subject: [PATCH] Registry is running in docker container --- .env.dev_docker | 40 ++++++++++++++++++++++++++++++++++++++++ .env.local | 3 ++- Dockerfile | 5 +++++ common/config.go | 1 + db/testutil.go | 12 ++++++------ docker-compose.yml | 16 +++++++++------- entrypoint.sh | 25 ++++++++++++++++++++----- registry | 10 ++++++++-- 8 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 .env.dev_docker diff --git a/.env.dev_docker b/.env.dev_docker new file mode 100644 index 0000000..2f35dee --- /dev/null +++ b/.env.dev_docker @@ -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="help@aptrust.org" +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 \ No newline at end of file diff --git a/.env.local b/.env.local index 62313c8..5295033 100644 --- a/.env.local +++ b/.env.local @@ -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" diff --git a/Dockerfile b/Dockerfile index 0a7c1b1..9bee40a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/common/config.go b/common/config.go index b0e14a4..5056896 100644 --- a/common/config.go +++ b/common/config.go @@ -20,6 +20,7 @@ import ( var allowedConfigs = []string{ "ci", "dev", + "dev_docker", "docker", "integration", "production", diff --git a/db/testutil.go b/db/testutil.go index bb37d98..83f0410 100644 --- a/db/testutil.go +++ b/db/testutil.go @@ -2,7 +2,6 @@ package db import ( "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -23,6 +22,7 @@ var SafeEnvironments = []string{ "integration", "test", "travis", + "dev_docker", } // LoadOrder lists the names of tables for which we have fixture data @@ -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)) } @@ -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 { @@ -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 } @@ -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") && diff --git a/docker-compose.yml b/docker-compose.yml index 1f3d950..ea35b78 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" @@ -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: diff --git a/entrypoint.sh b/entrypoint.sh index b9f5ea8..af8e4d0 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 "$@" diff --git a/registry b/registry index 5cfe435..0dd5ed5 100755 --- a/registry +++ b/registry @@ -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" @@ -36,6 +40,7 @@ elif [[ "$OSTYPE" == "darwin"* ]]; then fi fi +echo "DIR is $DIR; OSTYPE is $OSTYPE" # ---------------------------------------------------------------------- # @@ -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=$! @@ -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