diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9ad8b36fee4af524a7d7cf9f8124bdd68d5f31f8..29d0979a47f3e3cd893ffd1b3ee13f363a1744c2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,3 +1,6 @@ +variables: + IMAGE_NAME: ${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA} + # Do not run for merge requests workflow: rules: @@ -12,6 +15,7 @@ include: stages: - init - build + - trigger #### # Templates @@ -43,7 +47,7 @@ Build Image Haz: DOCKER_BUILD_ARGS: | BUILD_IMAGE=${DEVOPS_REGISTRY}usgs/java:11 FROM_IMAGE=${DEVOPS_REGISTRY}usgs/java:11 - ci_job_token=${CI_JOB_TOKEN} + CI_JOB_TOKEN=${CI_JOB_TOKEN} UPSTREAM_PATH: ghsc/nshmp/nshmp-haz-v2 Build Image WS: @@ -54,7 +58,7 @@ Build Image WS: DOCKER_BUILD_ARGS: | BUILD_IMAGE=${DEVOPS_REGISTRY}usgs/java:11 FROM_IMAGE=${DEVOPS_REGISTRY}usgs/java:11 - ci_job_token=${CI_JOB_TOKEN} + CI_JOB_TOKEN=${CI_JOB_TOKEN} DOCKERFILE: ws.Dockerfile UPSTREAM_PATH: ghsc/nshmp/nshmp-haz-v2 @@ -137,3 +141,38 @@ Unit Tests: script: - ./gradlew check - cat ${JACOCO_HTML_DIR}/index.html + +#### +# Stage: trigger +#### + +Trigger nshmp-haz-ws CDK: + needs: + - Build Image WS + rules: + - + if: !reference [.development-env, if] + variables: !reference [.development-env, variables] + when: manual + allow_failure: true + - !reference [.staging-env] + - !reference [.production-env] + script: + - apk add curl + - | + if [ "${ENVIRONMENT}" == 'production' ]; then + REF="production"; + fi + - | + curl --request POST \ + --form token=${NSHMP_HAZ_WS_CDK_TRIGGER_TOKEN} \ + --form ref=${REF} \ + --form "variables[description]=Triggered by nshmp-haz" \ + --form "variables[ENVIRONMENT]=${ENVIRONMENT}" \ + --form "variables[NSHMP_HAZ_WS_IMAGE]=${IMAGE_NAME}" \ + --form "variables[TRIGGER_PASSCODE]=${NSHMP_HAZ_WS_CDK_TRIGGER_TOKEN}" \ + "https://${PRIVATE_GITLAB}/api/v4/projects/${NSHMP_HAZ_WS_CDK_PROJECT_ID}/trigger/pipeline" + stage: trigger + variables: + REF: main + UPSTREAM_PATH: ghsc/nshmp/nshmp-haz-v2 diff --git a/Dockerfile b/Dockerfile index 8fe300315d5f2fc1cd21dd23013a3af787d7c2cf..d1e5a2b6f6afc4154b9e2e91ec4ee5bf6a61c924 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,15 +31,16 @@ FROM ${BUILD_IMAGE} as builder ARG builder_workdir ARG libs_dir + +# TODO +# Remove once nshmp-lib is public ARG git_username ARG git_password -ARG gitlab_token=null -ARG ci_job_token=null +ARG GITLAB_TOKEN=null +ARG CI_JOB_TOKEN=null ENV GIT_NSHMP_USERNAME ${git_username} ENV GIT_NSHMP_PASSWORD ${git_password} -ENV GITLAB_TOKEN ${gitlab_token} -ENV CI_JOB_TOKEN ${ci_job_token} WORKDIR ${builder_workdir} diff --git a/gradle.properties b/gradle.properties index 52e9cd3a8988f24d29c387b9e685c6a41c932572..05bbb8d30359538c9280b621f542f5af0487916f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ junitVersion = 5.5.2 micronautVersion = 2.4.1 mnPluginVersion = 1.4.2 nodeVersion = 3.0.1 -nshmpLibVersion = 0.5.0 +nshmpLibVersion = 0.5.1 nshmpWsUtilsVersion = 0.1.2 shadowVersion = 5.2.0 spotbugsVersion = 4.2.4 diff --git a/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java b/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java deleted file mode 100644 index dcdf890cbee25489ebd82b2a897498ad7ca8498d..0000000000000000000000000000000000000000 --- a/src/main/java/gov/usgs/earthquake/nshmp/www/ContextPathCookie.java +++ /dev/null @@ -1,39 +0,0 @@ -package gov.usgs.earthquake.nshmp.www; - -import java.time.Duration; - -import org.reactivestreams.Publisher; - -import io.micronaut.context.annotation.Requires; -import io.micronaut.context.annotation.Value; -import io.micronaut.core.async.publisher.Publishers; -import io.micronaut.http.HttpMethod; -import io.micronaut.http.HttpRequest; -import io.micronaut.http.MutableHttpResponse; -import io.micronaut.http.annotation.Filter; -import io.micronaut.http.cookie.Cookie; -import io.micronaut.http.filter.HttpServerFilter; -import io.micronaut.http.filter.ServerFilterChain; - -/** - * Add context path as a cookie for Swagger to use. - * - * @author U.S. Geological Survey - */ -@Requires(property = "micronaut.server.context-path") -@Filter(methods = { HttpMethod.GET, HttpMethod.HEAD }, - patterns = { "/**" }) -public class ContextPathCookie implements HttpServerFilter { - private final Cookie contextPathCookie; - - ContextPathCookie(@Value("${micronaut.server.context-path}") String contextPath) { - this.contextPathCookie = Cookie.of("contextPath", contextPath).maxAge(Duration.ofMinutes(2L)); - } - - @Override - public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, - ServerFilterChain chain) { - return Publishers.map(chain.proceed(request), response -> response.cookie(contextPathCookie)); - } - -} diff --git a/src/main/resources/swagger/index.js b/src/main/resources/swagger/index.js index f9288b53567722b966a326280bb6cf541549057b..06e5a1326e3df94485c3d6879f3d3d773b4dcecf 100644 --- a/src/main/resources/swagger/index.js +++ b/src/main/resources/swagger/index.js @@ -1,24 +1,38 @@ window.onload = function() { - const extract = function(v) { - return decodeURIComponent( - v.replace( - /(?:(?:^|.*;\s*)contextPath\s*\=\s*([^;]*).*$)|^.*$/, - '$1' - ) - ); - }; - - const contextPath = extract(document.cookie); + let contextPath = window.location.pathname; + contextPath = contextPath.endsWith('/') ? contextPath.slice(0, -1) : contextPath; const ui = SwaggerUIBundle({ - url: `${contextPath}/swagger`, + url: `./swagger`, dom_id: '#swagger-ui', tagsSorter: 'alpha', presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset], - plugins: [SwaggerUIBundle.plugins.DownloadUrl], + plugins: [SwaggerUIBundle.plugins.DownloadUrl, updateContextPath(contextPath)], validatorUrl: null, deepLinking: true }); window.ui = ui; }; + +function updateContextPath(contextPath) { + return { + statePlugins: { + spec: { + wrapActions: { + updateJsonSpec: (oriAction) => (...args) => { + const [spec] = args; + if (spec && spec.paths) { + const newPaths = {}; + Object.entries(spec.paths).forEach( + ([path, value]) => (newPaths[contextPath + path] = value) + ); + spec.paths = newPaths; + } + oriAction(...args); + }, + }, + }, + }, + }; +} diff --git a/ws.Dockerfile b/ws.Dockerfile index 2fca9f0bf1833baf223a708cf2cc38ec945ae3b5..bcc535beaa4c1bdd9dcf3ca7e903a2a69c7d476d 100644 --- a/ws.Dockerfile +++ b/ws.Dockerfile @@ -16,11 +16,6 @@ ARG BUILD_IMAGE=usgs/java:11 ARG FROM_IMAGE=usgs/java:11 -ARG project=nshmp-haz-v2 -ARG builder_workdir=/app/${project} -ARG libs_dir=${builder_workdir}/build/libs -ARG jar_file=${libs_dir}/${project}.jar - #### # Builder image: Build jar file. #### @@ -30,12 +25,10 @@ ARG builder_workdir # TODO # Remove once nshmp-lib is public -ARG gitlab_token=null -ARG ci_job_token=null -ENV GITLAB_TOKEN ${gitlab_token} -ENV CI_JOB_TOKEN ${ci_job_token} +ARG GITLAB_TOKEN=null +ARG CI_JOB_TOKEN=null -WORKDIR ${builder_workdir} +WORKDIR /app COPY . . @@ -48,27 +41,23 @@ FROM ${FROM_IMAGE} LABEL maintainer="Peter Powers <pmpowers@usgs.gov>, Brandon Clayton <bclayton@usgs.gov>" -ARG libs_dir -ARG builder_workdir -ARG project - -ENV PROJECT ${project} -ENV CONTEXT_PATH "/" -ENV BASIN_SERVICE_URL "https://staging-earthquake.usgs.gov/nshmp/ws/data/basin" +ENV CONTEXT_PATH="/" +ENV BASIN_SERVICE_URL="https://earthquake.usgs.gov/nshmp/ws/data/basin" ENV JAVA_XMS="2g" ENV JAVA_XMX="8g" +ENV MODELS_DIRECTORY="/models" WORKDIR /app -COPY --from=builder ${libs_dir}/* ./ +COPY --from=builder /app/build/libs/nshmp-haz-v2.jar . -VOLUME [ "/models" ] +VOLUME [ "${MODELS_DIRECTORY}" ] EXPOSE 8080 -ENTRYPOINT java -jar "${PROJECT}.jar" \ +ENTRYPOINT java -jar nshmp-haz-v2.jar \ "-Xms${JAVA_XMS}" \ "-Xmx${JAVA_XMX}" \ "-Dmicronaut.server.context-path=${CONTEXT_PATH}" \ --basin-service-url="${BASIN_SERVICE_URL}" \ - --models="/models"; + --models="${MODELS_DIRECTORY}";