From 09c7ea778b0d00a6b5515a03259c24fe63b34f98 Mon Sep 17 00:00:00 2001 From: kabeer27 <32016558+kabeer27@users.noreply.github.com> Date: Wed, 1 Jul 2020 06:13:08 +0000 Subject: [PATCH] Added deploy file and restructured cloud function directory (#4057) * Adding deploy.sh and restructuring directory * Integrating review changes * Passed project id as an argument to each command Co-authored-by: Kabeer Seth --- infra/build/functions/sync/deploy.sh | 55 +++++++++++++++++++ .../build/functions/{sync.py => sync/main.py} | 0 .../{sync_test.py => sync/main_test.py} | 8 +-- infra/build/functions/sync/requirements.txt | 1 + 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100755 infra/build/functions/sync/deploy.sh rename infra/build/functions/{sync.py => sync/main.py} (100%) rename infra/build/functions/{sync_test.py => sync/main_test.py} (98%) create mode 120000 infra/build/functions/sync/requirements.txt diff --git a/infra/build/functions/sync/deploy.sh b/infra/build/functions/sync/deploy.sh new file mode 100755 index 000000000..4581945d6 --- /dev/null +++ b/infra/build/functions/sync/deploy.sh @@ -0,0 +1,55 @@ +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +JOB_TOPIC=schedule-project-sync +SCHEDULER_JOB=sync-scheduler +JOB_SCHEDULE="*/30 * * * *" +MESSAGE="Start Sync" +ENTRY_POINT=sync + +if [ "$1" ]; then + PROJECT_ID=$1 +else + echo -e "\n Usage ./deploy.sh my-project-name"; exit; +fi + +# Checking if the given pubsub topic exists +if ! gcloud pubsub topics describe $JOB_TOPIC --project $PROJECT_ID ; + then + gcloud pubsub topics create $JOB_TOPIC \ + --project $PROJECT_ID +fi +# Checking if the given scheduler job exists +if gcloud scheduler jobs describe $SCHEDULER_JOB --project $PROJECT_ID ; + then + gcloud scheduler jobs update pubsub sync-scheduler \ + --schedule "$JOB_SCHEDULE" \ + --topic $JOB_TOPIC \ + --message-body "$MESSAGE" \ + --project $PROJECT_ID + else + gcloud scheduler jobs create pubsub sync-scheduler \ + --schedule "$JOB_SCHEDULE" \ + --topic $JOB_TOPIC \ + --message-body "$MESSAGE" \ + --project $PROJECT_ID +fi + +gcloud functions deploy sync \ + --entry-point $ENTRY_POINT \ + --trigger-topic $JOB_TOPIC \ + --runtime python37 \ + --project $PROJECT_ID diff --git a/infra/build/functions/sync.py b/infra/build/functions/sync/main.py similarity index 100% rename from infra/build/functions/sync.py rename to infra/build/functions/sync/main.py diff --git a/infra/build/functions/sync_test.py b/infra/build/functions/sync/main_test.py similarity index 98% rename from infra/build/functions/sync_test.py rename to infra/build/functions/sync/main_test.py index 021d5d166..2fba6db74 100644 --- a/infra/build/functions/sync_test.py +++ b/infra/build/functions/sync/main_test.py @@ -23,10 +23,10 @@ import threading from google.cloud import ndb -from sync import sync_projects -from sync import get_projects -from sync import get_access_token -from sync import Project +from main import sync_projects +from main import get_projects +from main import get_access_token +from main import Project _EMULATOR_TIMEOUT = 20 _DATASTORE_READY_INDICATOR = b'is now running' diff --git a/infra/build/functions/sync/requirements.txt b/infra/build/functions/sync/requirements.txt new file mode 120000 index 000000000..dc833dd4b --- /dev/null +++ b/infra/build/functions/sync/requirements.txt @@ -0,0 +1 @@ +../requirements.txt \ No newline at end of file