Skip to content
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

Define a container that executes migration then exits. #17

Open
wants to merge 29 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4692530
Define a container that executes migration then exits.
bugslifesolutions Sep 6, 2020
7aa8438
Merge remote-tracking branch 'upstream/trunk' into trunk
bugslifesolutions Nov 1, 2020
784d97d
Fix permission error when starting entrypoint
bugslifesolutions Nov 2, 2020
1ab9b54
parameterize run_user
bugslifesolutions Jun 7, 2021
7902e76
Merge remote-tracking branch 'upstream/trunk' into trunk
bugslifesolutions Jun 7, 2021
5a47f73
Merge branch 'reactioncommerce:trunk' into trunk
bugslifesolutions Nov 22, 2021
8ffcdd9
Use latest compatible NodeJs Alpine base image
bugslifesolutions Dec 18, 2021
0240684
Kustomizable Kubernetes Job resource for Migrating
bugslifesolutions Dec 18, 2021
55210e1
Suspend job initially, otherwise MongoDB may not be ready!
bugslifesolutions Jun 26, 2022
c76e7bf
Merge branch 'trunk' of https://github.com/reactioncommerce/api-migra…
bugslifesolutions May 8, 2023
db8ad20
fix: ensure migration job is initially suspended.
bugslifesolutions May 8, 2023
a48d08a
feat: upgrade to node 14.20.1
bugslifesolutions May 8, 2023
82a3097
ci: api-migrations docker image
bugslifesolutions May 16, 2023
47f612c
ci: DL3002 warning: Last USER should not be root
bugslifesolutions May 16, 2023
b6e464a
ci: node-gyp python dependency
bugslifesolutions May 17, 2023
7e0fd95
ci: fix RUN: command not found
bugslifesolutions May 17, 2023
2d52aa0
k8: use latest image
bugslifesolutions May 17, 2023
f3d7157
fix: su-exec: setgroups: Operation not permitted
bugslifesolutions May 18, 2023
541a575
fix: use @bugslifesolutions and latest migration versions
bugslifesolutions Dec 16, 2023
709b664
fix: k8 violate PodSecurity "restricted:latest": allowPrivilegeEscala…
bugslifesolutions Dec 16, 2023
bc45975
fix: kubernetes cannot verify user is non-root
bugslifesolutions Dec 16, 2023
3bb6df7
version: use reaction api's next.5 prerelease packages
bugslifesolutions Dec 16, 2023
3aee819
ci: upgrade pnpm and node
bugslifesolutions Dec 16, 2023
32de8f9
ci: fix no such file or directory
bugslifesolutions Dec 16, 2023
ab4d123
ci: fix cannot find migration namespace undefined defined in
bugslifesolutions Dec 19, 2023
03f2915
ci: fix no such file or directory, open '/home/circleci/.local/share/…
bugslifesolutions Dec 19, 2023
c18be68
ci: fix try #2 no such file or directory, open '/home/circleci/.local…
bugslifesolutions Dec 19, 2023
fa9edb8
feat: fulfillment version 2
bugslifesolutions Feb 19, 2024
aa957a6
feat: simple schema migrations
bugslifesolutions Feb 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
version: 2.1

# The following stanza defines a map named defaults with a variable that may be
# inserted using the YAML merge (<<: *) key later in the file to save some
# typing. See http://yaml.org/type/merge.html for details.
defaults: &defaults
environment:
CI_SCRIPTS: "npx --quiet --package @reactioncommerce/[email protected]"
DOCKER_REPOSITORY: "gcr.io/appliance-shack-cluster2/reaction-api-migrations"
DOCKER_NAME: "reaction-api-migrations"
GLOBAL_CACHE_VERSION: “v3”
docker:
- image: cimg/node:18.18.2

install_pnpm: &install_pnpm
- run:
name: Install pnpm package manager
command: sudo npm i -g [email protected]

pnpm_install: &pnpm_install
- run:
name: Install PNPM dependencies
command: pnpm install -r

jobs:
install-dependencies:
<<: *defaults
steps:
- checkout
- restore_cache:
keys:
- reaction-v8-node-modules-{{ checksum "package.json" }}-{{ checksum "pnpm-lock.yaml" }}-{{ checksum ".nvmrc" }}
- <<: *install_pnpm
- <<: *pnpm_install
- save_cache:
key: reaction-v8-node-modules-{{ checksum "package.json" }}-{{ checksum "pnpm-lock.yaml" }}-{{ checksum ".nvmrc" }}
paths:
- node_modules

dockerfile-lint:
<<: *defaults
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Lint Dockerfiles
command: ${CI_SCRIPTS} lint-dockerfiles
release:
<<: *defaults
steps:
- checkout
- <<: *install_pnpm
- <<: *pnpm_install
- run:
name: Authenticate with registry
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- run:
name: Push the latest version to npm
command: pnpm publish -r

docker-build-push:
<<: *defaults
steps:
- checkout
- run:
name: Wait for packages to be propagated
command: sleep 120
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Build production Docker image
command: |
VERSION=$(cat ./package.json | grep -m 1 version | sed 's/[^0-9.]//g')
docker build --progress plain --no-cache -t ${DOCKER_REPOSITORY}:${VERSION} -t ${DOCKER_REPOSITORY}:latest -f ./Dockerfile .
- run:
name: Login to gcr.io docker registry
command: echo ${GCR_JSON_KEY} | docker login -u _json_key --password-stdin https://gcr.io
- run:
name: Push production Docker image
command: |
VERSION=$(cat ./package.json | grep -m 1 version | sed 's/[^0-9.]//g')
docker push ${DOCKER_REPOSITORY}:${VERSION}
docker push ${DOCKER_REPOSITORY}:latest

workflows:
version: 2
build_and_test:
jobs:
- install-dependencies
- dockerfile-lint:
requires:
- install-dependencies
- release:
context: reaction-publish-semantic-release
filters:
branches:
only:
- trunk
requires:
- dockerfile-lint
- docker-build-push:
context: reaction-publish-docker
filters:
branches:
only:
- trunk
- main
requires:
- dockerfile-lint
17 changes: 17 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.build
.build.*
.npm
.git
.husky
.github

# This is necessary so plugins can use git repos as npm deps.
# Leave this here unless you understand the implications.
# See this issue for details:
# https://github.com/reactioncommerce/reaction/pull/5118
!.git/modules/imports/plugins/custom

.build.log
Dockerfile
docs
**/node_modules
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

MONGO_URL=mongodb://mongo.reaction.localhost:27017/reaction
REACTION_SHOULD_ENCODE_IDS=false
5 changes: 5 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
engine-strict=true
auto-install-peers=true
strict-peer-dependencies=false
publish-branch=trunk
prefer-workspace-packages=true
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.18.2
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Largely based on https://github.com/reactioncommerce/docker-base/blob/trunk/images/node-dev/12.14.1-v3/Dockerfile
FROM node:18.18.2-alpine

# hadolint ignore=DL3018
RUN apk --no-cache --update add bash curl less shadow su-exec tini vim make py3-pip g++ git
SHELL ["/bin/bash", "-o", "pipefail", "-o", "errexit", "-u", "-c"]

# Allow yarn/npm to create ./node_modules
RUN mkdir -p /usr/local/src/app && chown node:node /usr/local/src/app

# Install latest NPM
# RUN npm install -g npm@latest

WORKDIR /usr/local/src/app

COPY --chown=node:node ./ /usr/local/src/app

RUN npm i -g [email protected] && pnpm install && chmod +x /usr/local/src/app/scripts/entrypoint.sh

USER node
ENTRYPOINT ["tini", "--", "/usr/local/src/app/scripts/entrypoint.sh"]
LABEL com.reactioncommerce.name="reaction-api-migrations"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This repo was created by following [these instructions](https://github.com/react

## Prerequisites

You must have NodeJS 14.17.4 installed globally.
You must have NodeJS installed globally.

## Local Development Usage

Expand Down
47 changes: 47 additions & 0 deletions docker-compose-mongo-with-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This docker-compose file is used to run the project in Docker.
# The local files are mounted into the created container.
#
# Usage:
# ln -s docker-compose.dev.yml docker-compose.override.yml
# docker-compose up [-d]
#
# To go back to running the published image:
# rm docker-compose.override.yml

version: "3.4"

networks:
reaction:
external:
name: reaction.localhost

services:
mongo:
image: mongo:5.0
command: mongod --oplogSize 128 --replSet rs0 --storageEngine=wiredTiger
networks:
default:
reaction:
ports:
- "27017:27017"
volumes:
- mongo-db4:/data/db
healthcheck: # re-run rs.initiate() after startup if it failed.
test: test $$(echo "rs.status().ok || rs.initiate().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s
restart: always


migration:
image: bugslifesolutions2/reaction-api-migrations:latest
depends_on:
- mongo
env_file:
- ./.env
networks:
default:
reaction:

volumes:
mongo-db4:
47 changes: 47 additions & 0 deletions docker-compose.circleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This docker-compose file is used to run the project in Docker.
# The local files are mounted into the created container.
#
# Usage:
# ln -s docker-compose.dev.yml docker-compose.override.yml
# docker-compose up [-d]
#
# To go back to running the published image:
# rm docker-compose.override.yml

version: "3.9"

networks:
reaction:
name: reaction.localhost
external: true

services:
api:
image: gcr.io/appliance-shack-cluster2/reaction-api-migrations:latest
depends_on:
- mongo
env_file:
- ./.env
networks:
- default
- reaction
ports:
- "3000:3000"

mongo:
image: mongo:5.0
command: mongod --oplogSize 128 --replSet rs0 --storageEngine=wiredTiger
networks:
- default
- reaction
ports:
- "27017:27017"
volumes:
- mongo-db4:/data/db
healthcheck: # re-run rs.initiate() after startup if it failed.
test: test $$(echo "rs.status().ok || rs.initiate().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s

volumes:
mongo-db4:
62 changes: 62 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This docker-compose file is used to run the project's published image
#
# Usage: docker-compose up [-d]
#
# See comment in docker-compose.dev.yml if you want to run for development.

version: "3.9"

networks:
reaction:
name: reaction.localhost
external: true

services:
api:
image: gcr.io/appliance-shack-cluster2/reaction-api-migrations:latest
depends_on:
- mongo
env_file:
- ./.env
networks:
- default
- reaction
ports:
- "3000:3000"
healthcheck:
test: "${DOCKER_HEALTHCHECK_TEST:-curl localhost:3000/}"
interval: "60s"
timeout: "3s"
start_period: "5s"
retries: 3
restart: always

mongo:
image: mongo:5.0
command: mongod --oplogSize 128 --replSet rs0 --storageEngine=wiredTiger
networks:
- default
- reaction
ports:
- "27017:27017"
volumes:
- mongo-db4:/data/db
healthcheck: # re-run rs.initiate() after startup if it failed.
test: test $$(echo "rs.status().ok || rs.initiate().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s
restart: always


migration:
image: reaction-api-migrations:latest
depends_on:
- mongo
env_file:
- ./.env
networks:
default:
reaction:

volumes:
mongo-db4:
25 changes: 25 additions & 0 deletions k8/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: batch/v1
kind: Job
metadata:
name: reactioncommerce-api-migrations-job
spec:
template:
spec:
containers:
- name: migrate
image: gcr.io/appliance-shack-cluster2/reaction-api-migrations:latest
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1000
capabilities:
drop: ["ALL"]
envFrom:
- secretRef:
name: reaction
restartPolicy: Never
securityContext:
seccompProfile:
type: RuntimeDefault
backoffLimit: 4
suspend: true
2 changes: 2 additions & 0 deletions kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- ./k8/job.yaml
Loading