-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from clingen-data-model/cloud-run-job-invocation
Cloud run job deploy and invocation
- Loading branch information
Showing
6 changed files
with
234 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,26 +10,6 @@ steps: | |
args: [ 'build', '.', '-t', 'clinvar-ftp-watcher:$COMMIT_SHA'] | ||
- name: 'gcr.io/cloud-builders/docker' | ||
args: [ 'tag', 'clinvar-ftp-watcher:$COMMIT_SHA', 'gcr.io/clingen-dev/clinvar-ftp-watcher:$COMMIT_SHA'] | ||
# - name: 'gcr.io/clingen-dev/git-image-updater' | ||
# secretEnv: ["GITHUB_TOKEN"] | ||
# args: | ||
# - '-c' | ||
# - | | ||
# git clone https://clingen-ci:[email protected]/clingen-data-model/architecture \ | ||
# && cd architecture \ | ||
# && git checkout -b image-update-$SHORT_SHA \ | ||
# && /usr/bin/yq eval -i ".genegraph_docker_image_tag = \"$COMMIT_SHA\"" ./helm/values/genegraph/values-dev.yaml \ | ||
# && date "+%Y-%m-%dT%H%M" > /workspace/DATETIME.txt \ | ||
# && /usr/bin/yq eval -i ".genegraph_data_version = \"$(tr -d '\n' < /workspace/DATETIME.txt):$COMMIT_SHA\"" ./helm/values/genegraph/values-dev.yaml \ | ||
# && git add -u \ | ||
# && git -c user.name="Clingen CI Automation" -c user.email="[email protected]" commit -m "bumping docker image for genegraph" \ | ||
# && git push origin image-update-$SHORT_SHA \ | ||
# && gh pr create --fill -l automation | ||
|
||
#availableSecrets: | ||
# secretManager: | ||
# - versionName: projects/clingen-dev/secrets/clingen-ci-github-token/versions/1 | ||
# env: GITHUB_TOKEN | ||
|
||
# push the images | ||
images: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Deploy Job | ||
run-name: Deploy ${{ github.event.workflow_run.head_branch }} by @${{ github.actor }} | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["CI"] | ||
types: | ||
- completed | ||
branches: | ||
- main | ||
- cloud-run-job-invocation | ||
|
||
permissions: | ||
id-token: write | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.workflow_run.head_branch }} | ||
|
||
- name: authenticate to google cloud | ||
id: "auth" | ||
uses: google-github-actions/auth@v2 | ||
with: | ||
workload_identity_provider: projects/522856288592/locations/global/workloadIdentityPools/clingen-actions-pool/providers/clingen-github-actions | ||
service_account: clinvar-ftp-watcher-deployment@clingen-dev.iam.gserviceaccount.com | ||
|
||
- name: setup gcloud sdk | ||
uses: google-github-actions/setup-gcloud@v2 | ||
|
||
- name: set env vars | ||
run: | | ||
export branch=${{ github.event.workflow_run.head_branch }} | ||
export commit=${{ github.event.workflow_run.head_sha }} | ||
echo "branch=$branch" >> $GITHUB_ENV | ||
echo "commit=$commit" >> $GITHUB_ENV | ||
echo "instance_name=clinvar-ftp-watcher-$branch" >> $GITHUB_ENV | ||
- name: build and deploy | ||
run: | | ||
bash misc/bin/deploy-job.sh | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -xeo pipefail | ||
|
||
if [ -z "$branch" ]; then | ||
branch=$(git rev-parse --abbrev-ref HEAD) | ||
else | ||
echo "branch set in environment" | ||
fi | ||
if [ -z "$commit" ]; then | ||
commit=$(git rev-parse HEAD) | ||
else | ||
echo "commit set in environment" | ||
fi | ||
|
||
echo "Branch: $branch" | ||
echo "Commit: $commit" | ||
|
||
set -u | ||
|
||
if [ "$branch" == "main" ]; then | ||
instance_name="clinvar-ftp-watcher" | ||
else | ||
instance_name="clinvar-ftp-watcher-${branch}" | ||
fi | ||
clinvar_ftp_watcher_bucket="clinvar-ftp-watcher" | ||
|
||
region="us-central1" | ||
# project=$(gcloud config get project) | ||
image=gcr.io/clingen-dev/clinvar-ftp-watcher:$commit | ||
deployment_service_account=clinvar-ftp-watcher-deployment@clingen-dev.iam.gserviceaccount.com | ||
|
||
|
||
if gcloud run jobs list --region us-central1 | awk '{print $2}' | grep "^$instance_name$" ; then | ||
echo "Cloud Run Job $instance_name already exists" | ||
echo "Deleting Cloud Run Job" | ||
gcloud run jobs delete $instance_name --region $region --quiet | ||
fi | ||
|
||
################################################################ | ||
# Build the image | ||
cloudbuild=.cloudbuild/docker-build-dev.cloudbuild.yaml | ||
|
||
tar --no-xattrs -c \ | ||
Dockerfile \ | ||
build.clj \ | ||
deps.edn \ | ||
misc \ | ||
src \ | ||
.cloudbuild \ | ||
| gzip --fast > archive.tar.gz | ||
|
||
gcloud builds submit \ | ||
--substitutions="COMMIT_SHA=${commit}" \ | ||
--config .cloudbuild/docker-build-dev.cloudbuild.yaml \ | ||
--gcs-log-dir=gs://${clinvar_ftp_watcher_bucket}/build/logs \ | ||
archive.tar.gz | ||
|
||
################################################################ | ||
# Deploy job | ||
|
||
gcloud run jobs create $instance_name \ | ||
--cpu=1 \ | ||
--image=$image \ | ||
--max-retries=0 \ | ||
--region=$region \ | ||
--service-account=$deployment_service_account \ | ||
--set-secrets=DX_JAAS_CONFIG=dx-prod-jaas:latest | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
(ns watcher.cloudrunjob | ||
"A place for GCP Cloud Run Job related artifacts." | ||
(:require [clojure.data.json :as json] | ||
[clojure.string :as str] | ||
[taoensso.timbre :refer [log info warn error]]) | ||
(:import [com.google.cloud.run.v2 JobsClient Execution JobName EnvVar | ||
RunJobRequest RunJobRequest$Overrides RunJobRequest$Overrides$ContainerOverride])) | ||
|
||
|
||
(def DEFAULT_GCP_PROJECT_ID "clingen-dev") | ||
(def DEFAULT_GCP_LOCATION "us-central1") | ||
(def DEFAULT_GCP_JOB_NAME "clinvar-ingest-main") | ||
|
||
;; TODO s/b a macro? | ||
(defn gcp-project-id [] | ||
(let [project-id (System/getenv "GCP_PROJECT_ID")] | ||
(if (nil? project-id) | ||
DEFAULT_GCP_PROJECT_ID | ||
project-id))) | ||
|
||
(defn gcp-location [] | ||
(let [location (System/getenv "GCP_LOCATION")] | ||
(if (nil? location) | ||
DEFAULT_GCP_LOCATION | ||
location))) | ||
|
||
(defn gcp-job-name [] | ||
(let [job-name (System/getenv "GCP_JOB_NAME")] | ||
(if (nil? job-name) | ||
DEFAULT_GCP_JOB_NAME | ||
job-name))) | ||
|
||
|
||
(defn normalize-keys [^clojure.lang.IPersistentMap payload] | ||
"Normalize the keys in payload map to be lower case and replace spaces with underscore" | ||
(reduce (fn [m [k v]] | ||
(assoc m (-> k | ||
str/lower-case | ||
(str/replace #" " "_")) | ||
v)) | ||
{} | ||
payload)) | ||
|
||
|
||
(defn envvar [k v] | ||
"Create EnvVars to use to pass as overrides to cloud run job. Note that all values are strings." | ||
(-> (EnvVar/newBuilder) | ||
(.setName k) | ||
(.setValue (if (= "java.lang.String" (type v)) v (str v))) | ||
.build)) | ||
|
||
|
||
(defn envars [^clojure.lang.IPersistentMap payload] | ||
"Creates normalized EnvVars for each argument in the payload map." | ||
(reduce (fn [l [k v]] | ||
(conj l (envvar k v))) | ||
[] | ||
(normalize-keys payload))) | ||
|
||
|
||
(defn overrides [^clojure.lang.IPersistentMap payload] | ||
"Create and populate the RunJobRequest with environment variable overrides." | ||
(-> (RunJobRequest$Overrides/newBuilder) | ||
(.addContainerOverrides (-> (RunJobRequest$Overrides$ContainerOverride/newBuilder) | ||
(.addAllEnv (envars payload)) | ||
.build)) | ||
.build)) | ||
|
||
(defn initiate-cloud-run-job [^clojure.lang.IPersistentMap payload] | ||
"Pass a ClinVar release payload map to initiate the google cloud run job to ingest." | ||
(let [jobs-client (JobsClient/create)] | ||
(try | ||
(let [job-name (-> (JobName/of (gcp-project-id) (gcp-location) (gcp-job-name)) | ||
.toString) | ||
overrides (overrides payload) | ||
run-job-request (-> (RunJobRequest/newBuilder) | ||
(.setName job-name) | ||
(.setOverrides overrides) | ||
.build)] | ||
(.runJobAsync jobs-client run-job-request)) | ||
(catch Exception e (throw e)) | ||
(finally (.close jobs-client))))) | ||
|
||
|
||
#_ (initiate-cloud-run-job | ||
{"Name" "ClinVarVariationRelease_2023-1209.xml.gz", | ||
"Size" 3192148421, | ||
"Released" "2023-12-10 01:49:23", | ||
"Last Modified" "2023-12-10 01:49:23", | ||
"Directory" "http://ftp.ncbi.nlm.nih.gov/pub/clinvar/xml/clinvar_variation/weekly_release", | ||
"Release Date" "2023-12-09"}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters