From 22f5034b010543a7632c15cc7cdcb9f6c709742c Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 08:17:25 -0800 Subject: [PATCH 01/26] Update checklicense_openpr.py --- tools/checklicense_openpr.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/checklicense_openpr.py b/tools/checklicense_openpr.py index 2c3b0e6d..10943c79 100644 --- a/tools/checklicense_openpr.py +++ b/tools/checklicense_openpr.py @@ -16,6 +16,7 @@ import os import sys +import glob import json import shutil import requests @@ -65,6 +66,11 @@ def licensecheck(GITHUB_REPOSITORY, TOKEN, pr, commentcheck): # Download all prf files locally into ./tools/temp/ folder in the same directory structure downloadprfiles(prfiles) + print(os.getcwd()+'/temp') + print(glob.glob(os.getcwd()+'/temp/*')) + print(glob.glob(os.getcwd()+'/temp/*/*')) + print(glob.glob(os.getcwd()+'/temp/*/*/*')) + # Run lisence check on the downloaded files in temp directory pr_no_license_files = boilerplate(os.getcwd()+'/temp') @@ -163,4 +169,4 @@ def commentpr(GITHUB_REPOSITORY, pr, comment, TOKEN): if len(sys.argv) == 2: main(sys.argv[1]) else: - main('All') + main('All') \ No newline at end of file From 16966dde05af336b9641f484715a2a38e6a12401 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 08:21:47 -0800 Subject: [PATCH 02/26] Update checklicense_openpr.py --- tools/checklicense_openpr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/checklicense_openpr.py b/tools/checklicense_openpr.py index 10943c79..4a9b43da 100644 --- a/tools/checklicense_openpr.py +++ b/tools/checklicense_openpr.py @@ -66,10 +66,10 @@ def licensecheck(GITHUB_REPOSITORY, TOKEN, pr, commentcheck): # Download all prf files locally into ./tools/temp/ folder in the same directory structure downloadprfiles(prfiles) - print(os.getcwd()+'/temp') - print(glob.glob(os.getcwd()+'/temp/*')) - print(glob.glob(os.getcwd()+'/temp/*/*')) - print(glob.glob(os.getcwd()+'/temp/*/*/*')) + # print(os.getcwd()+'/temp') + # print(glob.glob(os.getcwd()+'/temp/*')) + # print(glob.glob(os.getcwd()+'/temp/*/*')) + # print(glob.glob(os.getcwd()+'/temp/*/*/*')) # Run lisence check on the downloaded files in temp directory pr_no_license_files = boilerplate(os.getcwd()+'/temp') @@ -111,6 +111,7 @@ def prcommentcheck(GITHUB_REPOSITORY, pr): def boilerplate(local_temp): pr_no_license_files = [] allfiles = check_boilerplate.main(local_temp) + print(allfiles) for x in range(len(allfiles)): pr_no_license_files.append(allfiles[x].replace(local_temp+'/', "")) # print(pr_no_license_files) From 1913b9c725406a52f5cd3c5d9ec3a2fda2052443 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 08:24:42 -0800 Subject: [PATCH 03/26] Update checklicense_openpr.py --- tools/checklicense_openpr.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/checklicense_openpr.py b/tools/checklicense_openpr.py index 4a9b43da..96a97515 100644 --- a/tools/checklicense_openpr.py +++ b/tools/checklicense_openpr.py @@ -111,10 +111,12 @@ def prcommentcheck(GITHUB_REPOSITORY, pr): def boilerplate(local_temp): pr_no_license_files = [] allfiles = check_boilerplate.main(local_temp) - print(allfiles) - for x in range(len(allfiles)): - pr_no_license_files.append(allfiles[x].replace(local_temp+'/', "")) - # print(pr_no_license_files) + try: + for x in range(len(allfiles)): + pr_no_license_files.append(allfiles[x].replace(local_temp+'/', "")) + # print(pr_no_license_files) + except: + print("All files have the Apache 2.0 Lisence") return pr_no_license_files def pr_files(GITHUB_REPOSITORY,pr): From deb2a4d1235eed67ea866cc8cf13a8a9131854e0 Mon Sep 17 00:00:00 2001 From: Bjorn De Bakker Date: Fri, 12 Nov 2021 17:50:54 +0100 Subject: [PATCH 04/26] Fixes #12 --- modules/data_science/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/data_science/README.md b/modules/data_science/README.md index 5715592b..4f8beb97 100644 --- a/modules/data_science/README.md +++ b/modules/data_science/README.md @@ -58,4 +58,4 @@ NOTE: `variables.tf` would list some defaults. If you would like to override or ## Access RAD Lab Data Science Notebooks -Follow the instructions under [gcp-ai-nootbook-tools Readme](./scripts/gcp-ai-notebook-tools/README.md). \ No newline at end of file +Follow the instructions under [gcp-ai-nootbook-tools Readme](../../scripts/gcp-ai-notebook-tools). \ No newline at end of file From e16210fff5d4dfc46e83b8f2075d2bbeceb66d79 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 10:26:53 -0800 Subject: [PATCH 05/26] Update build-tf-plan.yml --- .github/workflows/build-tf-plan.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-tf-plan.yml b/.github/workflows/build-tf-plan.yml index 5a2c4268..b1eba5ba 100644 --- a/.github/workflows/build-tf-plan.yml +++ b/.github/workflows/build-tf-plan.yml @@ -36,8 +36,11 @@ jobs: readarray -t added_modified_files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.added_modified }}')" for added_modified_file in "${added_modified_files[@]}"; do echo "File Added/Modified : ${added_modified_file}" + if [[ "${added_modified_file}" == *".tf"* ]]; then + TFFILE=${added_modified_file} + fi done - DIR="$(dirname ${added_modified_files[0]})" + DIR="$(dirname ${TFFILE})" echo "ENV_DIR=${DIR}" >> $GITHUB_ENV continue-on-error: true @@ -46,8 +49,11 @@ jobs: readarray -t removed_files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.removed }}')" for removed_file in ${removed_files[@]}; do echo "File Removed : ${removed_file}" + if [[ "${removed_file}" == *".tf"* ]]; then + TFFILE=${removed_file} + fi done - DIR="$(dirname ${removed_files[0]})" + DIR="$(dirname ${TFFILE})" echo "ENV_DIR=${DIR}" >> $GITHUB_ENV continue-on-error: true From 81a9d1538fbd12b7bddabb545acf7028952503d7 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 10:49:38 -0800 Subject: [PATCH 06/26] Update build-tf-plan.yml --- .github/workflows/build-tf-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-tf-plan.yml b/.github/workflows/build-tf-plan.yml index b1eba5ba..433d46c1 100644 --- a/.github/workflows/build-tf-plan.yml +++ b/.github/workflows/build-tf-plan.yml @@ -61,7 +61,7 @@ jobs: uses: hashicorp/setup-terraform@v1 with: cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} - terraform_version: 0.15.0 + terraform_version: 1.0.11 - name: Set environment run: | From 9e5e6c17b15eaa34329bcbec824107fe84773af0 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 10:50:36 -0800 Subject: [PATCH 07/26] Update notifications.yml --- .github/workflows/notifications.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/notifications.yml b/.github/workflows/notifications.yml index ed1a48d5..ad3c646c 100644 --- a/.github/workflows/notifications.yml +++ b/.github/workflows/notifications.yml @@ -3,7 +3,7 @@ name: BUILD - RAD Lab Notifications on: issues: - types: [opened, reopened] + types: [opened] pull_request: branches: - main @@ -152,4 +152,4 @@ jobs: ] } ] - }' \ No newline at end of file + }' From 8935886bb2df674bf2d65e38a94fa69ef7a1fc93 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Fri, 12 Nov 2021 11:28:28 -0800 Subject: [PATCH 08/26] Update build-tf-plan.yml --- .github/workflows/build-tf-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-tf-plan.yml b/.github/workflows/build-tf-plan.yml index 433d46c1..8c29e7ce 100644 --- a/.github/workflows/build-tf-plan.yml +++ b/.github/workflows/build-tf-plan.yml @@ -114,7 +114,7 @@ jobs: id: plan run: terraform plan -out workspace.plan env: - TF_VAR_billing_account: 'ABCD-EFGH-IJKL-MNOP' + TF_VAR_billing_account_id: 'ABCD-EFGH-IJKL-MNOP' TF_VAR_parent: 'organizations/1234567890' # - name: Post Plan From f491556daf38e2b4cfbcf509e2ed0611b40a8d51 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:10:24 -0800 Subject: [PATCH 09/26] Updating Workflow --- .github/workflows/tf-plan.yml | 82 +++++++++++++++ tools/tf-plan.py | 182 ++++++++++++++++++++++++++++++++++ 2 files changed, 264 insertions(+) create mode 100644 .github/workflows/tf-plan.yml create mode 100644 tools/tf-plan.py diff --git a/.github/workflows/tf-plan.yml b/.github/workflows/tf-plan.yml new file mode 100644 index 00000000..87afb602 --- /dev/null +++ b/.github/workflows/tf-plan.yml @@ -0,0 +1,82 @@ +# .github/workflows/tf-plan.yml + +# Copyright 2021 Google LLC +# +# 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 +# +# https://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. + +name: '[NEW] BUILD - Terraform Plan' + +on: + pull_request: + types: [opened, reopened] + paths: + - 'examples/**/*.tf' + workflow_dispatch: + inputs: + PRNumber: + description: 'PR #' + required: true + +jobs: + terraform: + name: 'Terraform' + runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TF_IN_AUTOMATION: true + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: setup python + uses: actions/setup-python@v2 + with: + python-version: 3.8 #install the python needed + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v1 + with: + cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} + terraform_version: 1.0.11 + + - name: Set environment + run: | + echo "GOOGLE_APPLICATION_CREDENTIALS=${GITHUB_WORKSPACE}/.github/workflows/fake-key.json" >> $GITHUB_ENV + echo $GOOGLE_APPLICATION_CREDENTIALS # This will print the file path in GOOGLE_APPLICATION_CREDENTIALS variable + # cat $GOOGLE_APPLICATION_CREDENTIALS # This will print the file content + + - name: install python packages + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + working-directory: tools + + - name: execute py script (auto) # run the tf-plan.py + id: tf-plan-auto + if: github.event_name == 'pull_request' + run: | + python tf-plan.py ${{ github.event.number }} + working-directory: tools + + - name: execute py script (manual) # run the tf-plan.py + id: tf-plan-manual + if: github.event_name == 'workflow_dispatch' + run: | + if [[ "${{ github.event.inputs.PRNumber }}" =~ ^[0-9]+$ ]] + then + python tf-plan.py ${{ github.event.inputs.PRNumber }} + else + echo "INVALID PR Number ENTERED..." + exit 1 + fi + working-directory: tools \ No newline at end of file diff --git a/tools/tf-plan.py b/tools/tf-plan.py new file mode 100644 index 00000000..432214b3 --- /dev/null +++ b/tools/tf-plan.py @@ -0,0 +1,182 @@ +#!/usr/bin/env python3 + +# Copyright 2021 Google LLC +# +# 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 +# +# https://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. + +import os +import sys +import json +import shutil +import requests +from python_terraform import Terraform + +def main(PR): + + TOKEN = os.getenv('GITHUB_TOKEN') + GITHUB_WORKSPACE = os.getenv('GITHUB_WORKSPACE') + GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY') + + + # Get Added / Modified files in PR + modified_files, modified_files_raw, removed_files = pr_files(GITHUB_REPOSITORY, PR) + + # Get Working directories to run TF Plan on + working_directories = get_updated_modules(modified_files, removed_files) + + # Loop through all the identified working directories + # Deleting added/modified & removed files + try: + for dir in working_directories: + + print("----------> RUN FOR: " + dir) + # Copying main directory + shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir) + + # Deleting added/modified & removed files + for mfile in modified_files: + if os.path.exists(os.getcwd()+'/temp/'+mfile): + print("Deleting file: " + mfile) + os.remove(os.getcwd()+'/temp/'+mfile) + + for rfile in removed_files: + if os.path.exists(os.getcwd()+'/temp/'+rfile): + print("Deleting file: " + rfile) + os.remove(os.getcwd()+'/temp/'+rfile) + + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) + + # Loop through all the identified working directories + # Download added/modified files + try: + for dir in working_directories: + + # Download added/modified files + for file in modified_files: + + if dir in file: + for raw in modified_files_raw: + + if file in raw: + print("Downloading file: " + raw) + downloadprfiles(raw, file, os.getcwd()+'/temp/'+dir) + break + + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) + + # Loop through all the identified working directories + # Run Terraform Plan + try: + for dir in working_directories: + comment, status = tf(os.getcwd() + '/temp/' + dir) + commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) + if(status == 'fail'): + sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) + +def pr_files(GITHUB_REPOSITORY,pr): + removed_files = [] + modified_files = [] + modified_files_raw = [] + try: + response = requests.get('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/pulls/'+ str(pr) +'/files') + for file in response.json(): + if(file['status'] == 'removed'): + print("Removed File: " + file['filename']) + removed_files.append(file['filename']) + else: + print("Added/Modified File: " + file['filename']) + modified_files.append(file['filename']) + modified_files_raw.append(file['raw_url']) + + return modified_files, modified_files_raw, removed_files + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + +def downloadprfiles(raw, file, path): + + # print(path) + if not os.path.exists(path): + os.makedirs(path) + + # print('Beginning file download with requests') + r = requests.get(raw) + with open(path + '/' + os.path.basename(file), 'wb') as f: + f.write(r.content) + + # Retrieve HTTP meta-data + # print(r.status_code) + # print(r.headers['content-type']) + # print(r.encoding) + + +def get_updated_modules(modified_files, removed_files): + modified_files_dir = [] + removed_files_dir = [] + + for file in modified_files: + modified_files_dir.append(os.path.dirname(file)) + + for file in removed_files: + removed_files_dir.append(os.path.dirname(file)) + + working_directories = modified_files_dir + removed_files_dir + working_directories = list(set(working_directories)) + + print("Working Directories:") + print(working_directories) + + return working_directories + + +def tf(dir): + tr = Terraform(working_dir=dir) + + return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False) + return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account':'ABCD-EFGH-IJKL-MNOP'}) + + if(return_code_init == 1): + comment = 'Terraform Init FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') + status = 'fail' + if(return_code_plan == 1): + comment = 'Terraform Plan FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') + status = 'fail' + else: + comment = 'Terraform Init & Terraform Plan SUCCESSFUL!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') + status = 'pass' + + return comment, status + + +def commentpr(GITHUB_REPOSITORY, pr, comment, TOKEN): + headers = {'Authorization': f'token {TOKEN}', 'Accept': 'application/vnd.github.v3+json'} + # print(comment) + data = {"body":comment} + try: + response = requests.post('https://api.github.com/repos/'+ GITHUB_REPOSITORY +'/issues/'+ str(pr) +'/comments', data=json.dumps(data), headers=headers) + # print(response.text) + except requests.exceptions.RequestException as e: + raise SystemExit(e) + +if __name__ == '__main__': + + if len(sys.argv) != 2: + raise SystemExit('No PR passed.') + main(sys.argv[1]) \ No newline at end of file From 00eac02f1ceb0d82342f71908917ec55bcb0d276 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:12:09 -0800 Subject: [PATCH 10/26] Update requirements.txt --- tools/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/requirements.txt b/tools/requirements.txt index 8eae9967..c8d7883a 100644 --- a/tools/requirements.txt +++ b/tools/requirements.txt @@ -1,4 +1,5 @@ click python-dateutil pytz -requests \ No newline at end of file +requests +python-terraform>=0.10.1 \ No newline at end of file From bced9b4b027c08127bf5426587b053d4ac0f885d Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:23:09 -0800 Subject: [PATCH 11/26] Update tf-plan.py --- tools/tf-plan.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 432214b3..193b7087 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -142,8 +142,13 @@ def get_updated_modules(modified_files, removed_files): print("Working Directories:") print(working_directories) + + modules = [x for x in working_directories if 'module/' in x] + modules = [x for x in working_directories if x.count('/') == 1] + print("Modules Updated:") + print(modules) - return working_directories + return modules def tf(dir): From 0c39a098605454d9cfa863497b0aaee657e0b8d5 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:27:53 -0800 Subject: [PATCH 12/26] Update tf-plan.py --- tools/tf-plan.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 193b7087..57def2ce 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -83,7 +83,7 @@ def main(PR): try: for dir in working_directories: comment, status = tf(os.getcwd() + '/temp/' + dir) - commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) + # commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) if(status == 'fail'): sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) except requests.exceptions.RequestException as e: @@ -143,8 +143,7 @@ def get_updated_modules(modified_files, removed_files): print("Working Directories:") print(working_directories) - modules = [x for x in working_directories if 'module/' in x] - modules = [x for x in working_directories if x.count('/') == 1] + modules = [x for x in working_directories if x.startswith('module/') and x.count('/') == 1] print("Modules Updated:") print(modules) From d8b7e23a1de3a7cfe6e19434de2fb1ff1693d0bd Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:30:13 -0800 Subject: [PATCH 13/26] Update tf-plan.py --- tools/tf-plan.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 57def2ce..4f25757b 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -143,7 +143,8 @@ def get_updated_modules(modified_files, removed_files): print("Working Directories:") print(working_directories) - modules = [x for x in working_directories if x.startswith('module/') and x.count('/') == 1] + modules = [x for x in working_directories if x.startswith('module/')] + modules = [x for x in modules if x.count('/') == 1] print("Modules Updated:") print(modules) From e9ecc8036661811d6103afdf074853e1a0834213 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:30:28 -0800 Subject: [PATCH 14/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 4f25757b..31bbf96f 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -143,7 +143,7 @@ def get_updated_modules(modified_files, removed_files): print("Working Directories:") print(working_directories) - modules = [x for x in working_directories if x.startswith('module/')] + modules = [x for x in working_directories if x.startswith('modules/')] modules = [x for x in modules if x.count('/') == 1] print("Modules Updated:") print(modules) From a2514a2fb77d53ba28e2a8dcdb35d0dfb2d01446 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:46:02 -0800 Subject: [PATCH 15/26] Update workflow --- .github/workflows/tf-plan.yml | 8 ++++---- tools/tf-plan.py | 35 ++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.github/workflows/tf-plan.yml b/.github/workflows/tf-plan.yml index 87afb602..bace9018 100644 --- a/.github/workflows/tf-plan.yml +++ b/.github/workflows/tf-plan.yml @@ -17,10 +17,10 @@ name: '[NEW] BUILD - Terraform Plan' on: - pull_request: - types: [opened, reopened] - paths: - - 'examples/**/*.tf' + # pull_request: + # types: [opened, reopened] + # paths: + # - 'modules/**/*.tf' workflow_dispatch: inputs: PRNumber: diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 31bbf96f..00547159 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -38,21 +38,26 @@ def main(PR): # Deleting added/modified & removed files try: for dir in working_directories: - - print("----------> RUN FOR: " + dir) - # Copying main directory - shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir) - - # Deleting added/modified & removed files - for mfile in modified_files: - if os.path.exists(os.getcwd()+'/temp/'+mfile): - print("Deleting file: " + mfile) - os.remove(os.getcwd()+'/temp/'+mfile) - - for rfile in removed_files: - if os.path.exists(os.getcwd()+'/temp/'+rfile): - print("Deleting file: " + rfile) - os.remove(os.getcwd()+'/temp/'+rfile) + print("----------> RUN FOR: " + dir) + + + try: + # IF MODULE EXISTS: Copying main directory in temp folder + shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir) + + # Deleting added/modified & removed files + for mfile in modified_files: + if os.path.exists(os.getcwd()+'/temp/'+mfile): + print("Deleting file: " + mfile) + os.remove(os.getcwd()+'/temp/'+mfile) + + for rfile in removed_files: + if os.path.exists(os.getcwd()+'/temp/'+rfile): + print("Deleting file: " + rfile) + os.remove(os.getcwd()+'/temp/'+rfile) + except: + # IF MODULE DONOT EXISTS: Creating temp module folder + os.makedirs(os.getcwd()+'/temp/'+dir) except requests.exceptions.RequestException as e: print('No working directory with TF configs in PR.') From b4517293ff42d180c7941c1e264d62b1d50cb0d6 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:48:17 -0800 Subject: [PATCH 16/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 00547159..b860a61b 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -160,7 +160,7 @@ def tf(dir): tr = Terraform(working_dir=dir) return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False) - return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account':'ABCD-EFGH-IJKL-MNOP'}) + return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account_id':'ABCD-EFGH-IJKL-MNOP'}) if(return_code_init == 1): comment = 'Terraform Init FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') From 5cf8b92fb7cb4f03c63ab8a276feaa77f639e1d5 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 10:50:23 -0800 Subject: [PATCH 17/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index b860a61b..7698dafc 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -160,7 +160,7 @@ def tf(dir): tr = Terraform(working_dir=dir) return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False) - return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account_id':'ABCD-EFGH-IJKL-MNOP'}) + return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890'}) if(return_code_init == 1): comment = 'Terraform Init FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') From 151f616ecec11be7483ce129e24995148bb38530 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 11:03:47 -0800 Subject: [PATCH 18/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 7698dafc..a34f2dd6 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -160,7 +160,7 @@ def tf(dir): tr = Terraform(working_dir=dir) return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False) - return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'parent':'organizations/1234567890', 'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890'}) + return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'}) if(return_code_init == 1): comment = 'Terraform Init FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') From a923436fb5599910d942242bd10ddab430f52111 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 11:28:58 -0800 Subject: [PATCH 19/26] Update tf-plan.py --- tools/tf-plan.py | 152 +++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 72 deletions(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index a34f2dd6..1cdf8373 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -17,83 +17,91 @@ import os import sys import json +import glob import shutil import requests from python_terraform import Terraform def main(PR): - TOKEN = os.getenv('GITHUB_TOKEN') - GITHUB_WORKSPACE = os.getenv('GITHUB_WORKSPACE') - GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY') - + TOKEN = os.getenv('GITHUB_TOKEN') + GITHUB_WORKSPACE = os.getenv('GITHUB_WORKSPACE') + GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY') + + + # Get Added / Modified files in PR + modified_files, modified_files_raw, removed_files = pr_files(GITHUB_REPOSITORY, PR) + + # Get Working directories to run TF Plan on + working_directories = get_updated_modules(modified_files, removed_files) + + # Loop through all the identified working directories + # Deleting added/modified & removed files + try: + for dir in working_directories: + print("----------> RUN FOR: " + dir) + + + try: + # IF MODULE EXISTS: Copying main directory in temp folder + shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir) + + # Deleting added/modified & removed files + for mfile in modified_files: + if os.path.exists(os.getcwd()+'/temp/'+mfile): + print("Deleting file: " + mfile) + os.remove(os.getcwd()+'/temp/'+mfile) + + for rfile in removed_files: + if os.path.exists(os.getcwd()+'/temp/'+rfile): + print("Deleting file: " + rfile) + os.remove(os.getcwd()+'/temp/'+rfile) + except: + # IF MODULE DONOT EXISTS: Creating temp module folder + os.makedirs(os.getcwd()+'/temp/'+dir) + + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) - # Get Added / Modified files in PR - modified_files, modified_files_raw, removed_files = pr_files(GITHUB_REPOSITORY, PR) - - # Get Working directories to run TF Plan on - working_directories = get_updated_modules(modified_files, removed_files) - - # Loop through all the identified working directories - # Deleting added/modified & removed files - try: - for dir in working_directories: - print("----------> RUN FOR: " + dir) - - - try: - # IF MODULE EXISTS: Copying main directory in temp folder - shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir) - - # Deleting added/modified & removed files - for mfile in modified_files: - if os.path.exists(os.getcwd()+'/temp/'+mfile): - print("Deleting file: " + mfile) - os.remove(os.getcwd()+'/temp/'+mfile) - - for rfile in removed_files: - if os.path.exists(os.getcwd()+'/temp/'+rfile): - print("Deleting file: " + rfile) - os.remove(os.getcwd()+'/temp/'+rfile) - except: - # IF MODULE DONOT EXISTS: Creating temp module folder - os.makedirs(os.getcwd()+'/temp/'+dir) - - except requests.exceptions.RequestException as e: - print('No working directory with TF configs in PR.') - raise SystemExit(e) - - # Loop through all the identified working directories - # Download added/modified files - try: - for dir in working_directories: - - # Download added/modified files - for file in modified_files: - - if dir in file: - for raw in modified_files_raw: - - if file in raw: - print("Downloading file: " + raw) - downloadprfiles(raw, file, os.getcwd()+'/temp/'+dir) - break - - except requests.exceptions.RequestException as e: - print('No working directory with TF configs in PR.') - raise SystemExit(e) - - # Loop through all the identified working directories - # Run Terraform Plan - try: - for dir in working_directories: - comment, status = tf(os.getcwd() + '/temp/' + dir) - # commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) - if(status == 'fail'): - sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) - except requests.exceptions.RequestException as e: - print('No working directory with TF configs in PR.') - raise SystemExit(e) + # Loop through all the identified working directories + # Download added/modified files + try: + for dir in working_directories: + + # Download added/modified files + for file in modified_files: + + if dir in file: + for raw in modified_files_raw: + + if file in raw: + print("Downloading file: " + raw) + downloadprfiles(raw, file, os.getcwd()+'/temp/'+dir) + break + + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) + + + # Loop through all the identified working directories + # Run Terraform Plan + try: + for dir in working_directories: + + print('****************************') + print(glob.glob(os.getcwd() + '/temp/' + dir+'/*')) + print('****************************') + print(glob.glob(os.getcwd() + '/temp/' + dir+'/*/*')) + + comment, status = tf(os.getcwd() + '/temp/' + dir) + # commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) + if(status == 'fail'): + sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) + except requests.exceptions.RequestException as e: + print('No working directory with TF configs in PR.') + raise SystemExit(e) def pr_files(GITHUB_REPOSITORY,pr): removed_files = [] @@ -145,8 +153,8 @@ def get_updated_modules(modified_files, removed_files): working_directories = modified_files_dir + removed_files_dir working_directories = list(set(working_directories)) - print("Working Directories:") - print(working_directories) +# print("Working Directories:") +# print(working_directories) modules = [x for x in working_directories if x.startswith('modules/')] modules = [x for x in modules if x.count('/') == 1] From 16e32404ed2c8b48478849d00cec5ca6c1edc8fd Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 11:38:42 -0800 Subject: [PATCH 20/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 1cdf8373..e598b038 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -78,7 +78,7 @@ def main(PR): if file in raw: print("Downloading file: " + raw) downloadprfiles(raw, file, os.getcwd()+'/temp/'+dir) - break + break except requests.exceptions.RequestException as e: print('No working directory with TF configs in PR.') From 8894a0445574aac079708cb7bf0f4729909c5ae5 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 11:50:41 -0800 Subject: [PATCH 21/26] Update tf-plan.py --- tools/tf-plan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index e598b038..fc209dfe 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -77,7 +77,7 @@ def main(PR): if file in raw: print("Downloading file: " + raw) - downloadprfiles(raw, file, os.getcwd()+'/temp/'+dir) + downloadprfiles(raw, file, os.getcwd()+'/temp/'+os.path.dirname(file)) break except requests.exceptions.RequestException as e: From 577581276baed3cd2c3c4350da67c2e490a8de3d Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 11:58:52 -0800 Subject: [PATCH 22/26] Updating Workflows --- .github/workflows/build-tf-plan.yml | 126 ------------------ .github/workflows/check-license.yml | 2 +- .../{tf-plan.yml => check-tf-plan.yml} | 12 +- tools/tf-plan.py | 10 +- 4 files changed, 12 insertions(+), 138 deletions(-) delete mode 100644 .github/workflows/build-tf-plan.yml rename .github/workflows/{tf-plan.yml => check-tf-plan.yml} (93%) diff --git a/.github/workflows/build-tf-plan.yml b/.github/workflows/build-tf-plan.yml deleted file mode 100644 index 8c29e7ce..00000000 --- a/.github/workflows/build-tf-plan.yml +++ /dev/null @@ -1,126 +0,0 @@ -# .github/workflows/build-tf-plan.yml - -name: 'BUILD - Terraform Plan' - -on: - pull_request: - types: [opened, reopened] - paths: - - 'modules/**/*.tf' - push: - branches: - - main - paths: - - 'modules/**/*.tf' - workflow_dispatch: - -jobs: - terraform: - name: 'Terraform' - runs-on: ubuntu-latest - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - TF_IN_AUTOMATION: true - steps: - - name: Checkout - uses: actions/checkout@v2 - - - id: files - uses: jitterbit/get-changed-files@v1 - with: - format: 'json' - continue-on-error: true - - - name: Get added and modified files - run: | - readarray -t added_modified_files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.added_modified }}')" - for added_modified_file in "${added_modified_files[@]}"; do - echo "File Added/Modified : ${added_modified_file}" - if [[ "${added_modified_file}" == *".tf"* ]]; then - TFFILE=${added_modified_file} - fi - done - DIR="$(dirname ${TFFILE})" - echo "ENV_DIR=${DIR}" >> $GITHUB_ENV - continue-on-error: true - - - name: Get removed files - run: | - readarray -t removed_files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.removed }}')" - for removed_file in ${removed_files[@]}; do - echo "File Removed : ${removed_file}" - if [[ "${removed_file}" == *".tf"* ]]; then - TFFILE=${removed_file} - fi - done - DIR="$(dirname ${TFFILE})" - echo "ENV_DIR=${DIR}" >> $GITHUB_ENV - continue-on-error: true - - - name: Setup Terraform - uses: hashicorp/setup-terraform@v1 - with: - cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} - terraform_version: 1.0.11 - - - name: Set environment - run: | - echo "GOOGLE_APPLICATION_CREDENTIALS=${GITHUB_WORKSPACE}/.github/workflows/fake-key.json" >> $GITHUB_ENV - echo $GOOGLE_APPLICATION_CREDENTIALS # This will print the file path in GOOGLE_APPLICATION_CREDENTIALS variable - # cat $GOOGLE_APPLICATION_CREDENTIALS # This will print the file content - - - name: Terraform Format - working-directory: ${{ env.ENV_DIR }} - id: fmt - run: terraform fmt -check -recursive - continue-on-error: true - - # - name: Post Format - # if: always() && github.ref != 'refs/heads/master' && (steps.fmt.outcome == 'success' || steps.fmt.outcome == 'failure') - # uses: robburger/terraform-pr-commenter@v1 - # with: - # commenter_type: fmt - # commenter_input: ${{ format('{0}{1}', steps.fmt.outputs.stdout, steps.fmt.outputs.stderr) }} - # commenter_exitcode: ${{ steps.fmt.outputs.exitcode }} - - - name: Terraform Init - working-directory: ${{ env.ENV_DIR }} - id: init - run: terraform init - - # - name: Post Init - # if: always() && github.ref != 'refs/heads/master' && (steps.init.outcome == 'success' || steps.init.outcome == 'failure') - # uses: robburger/terraform-pr-commenter@v1 - # with: - # commenter_type: init - # commenter_input: ${{ format('{0}{1}', steps.init.outputs.stdout, steps.init.outputs.stderr) }} - # commenter_exitcode: ${{ steps.init.outputs.exitcode }} - - - name: Terraform Validate - working-directory: ${{ env.ENV_DIR }} - id: validate - run: terraform validate - - # - name: Post Validate - # if: always() && github.ref != 'refs/heads/master' && (steps.validate.outcome == 'success' || steps.validate.outcome == 'failure') - # uses: robburger/terraform-pr-commenter@v1 - # with: - # commenter_type: validate - # commenter_input: ${{ format('{0}{1}', steps.validate.outputs.stdout, steps.validate.outputs.stderr) }} - # commenter_exitcode: ${{ steps.validate.outputs.exitcode }} - - - name: Terraform Plan - working-directory: ${{ env.ENV_DIR }} - id: plan - run: terraform plan -out workspace.plan - env: - TF_VAR_billing_account_id: 'ABCD-EFGH-IJKL-MNOP' - TF_VAR_parent: 'organizations/1234567890' - - # - name: Post Plan - # if: always() && github.ref != 'refs/heads/master' && (steps.plan.outcome == 'success' || steps.plan.outcome == 'failure') - # uses: robburger/terraform-pr-commenter@v1 - # with: - # commenter_type: plan - # commenter_input: ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }} - # commenter_exitcode: ${{ steps.plan.outputs.exitcode }} \ No newline at end of file diff --git a/.github/workflows/check-license.yml b/.github/workflows/check-license.yml index cc22a9d9..eb3c2090 100644 --- a/.github/workflows/check-license.yml +++ b/.github/workflows/check-license.yml @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: 'BUILD - Check Lisence Boilerplate' +name: 'CHECK - Check Lisence Boilerplate' on: schedule: diff --git a/.github/workflows/tf-plan.yml b/.github/workflows/check-tf-plan.yml similarity index 93% rename from .github/workflows/tf-plan.yml rename to .github/workflows/check-tf-plan.yml index bace9018..fdd69f2e 100644 --- a/.github/workflows/tf-plan.yml +++ b/.github/workflows/check-tf-plan.yml @@ -1,4 +1,4 @@ -# .github/workflows/tf-plan.yml +# .github/workflows/check-tf-plan.yml # Copyright 2021 Google LLC # @@ -14,13 +14,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: '[NEW] BUILD - Terraform Plan' +name: 'CHECK - Terraform Plan' on: - # pull_request: - # types: [opened, reopened] - # paths: - # - 'modules/**/*.tf' + pull_request: + types: [opened, reopened] + paths: + - 'modules/**/*.tf' workflow_dispatch: inputs: PRNumber: diff --git a/tools/tf-plan.py b/tools/tf-plan.py index fc209dfe..5ddbf8ac 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -90,13 +90,13 @@ def main(PR): try: for dir in working_directories: - print('****************************') - print(glob.glob(os.getcwd() + '/temp/' + dir+'/*')) - print('****************************') - print(glob.glob(os.getcwd() + '/temp/' + dir+'/*/*')) + # print('****************************') + # print(glob.glob(os.getcwd() + '/temp/' + dir+'/*')) + # print('****************************') + # print(glob.glob(os.getcwd() + '/temp/' + dir+'/*/*')) comment, status = tf(os.getcwd() + '/temp/' + dir) - # commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) + commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) if(status == 'fail'): sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) except requests.exceptions.RequestException as e: From ec4320be4a1ce79d7ed5157f6177f26448970acf Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 12:06:53 -0800 Subject: [PATCH 23/26] Update tf-plan.py --- tools/tf-plan.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 5ddbf8ac..30f3245c 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -171,14 +171,13 @@ def tf(dir): return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'}) if(return_code_init == 1): - comment = 'Terraform Init FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') + comment = 'Terraform Init FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') status = 'fail' if(return_code_plan == 1): - comment = 'Terraform Plan FAILED!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') + comment = 'Terraform Plan FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') status = 'fail' else: - comment = 'Terraform Init & Terraform Plan SUCCESSFUL!\nFor Module: ' + dir.replace(os.getenv('TERRAFORM_CLI_PATH')+'/', '') - status = 'pass' + comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') return comment, status From 279a6af5868ead21acfa4d4a4a2eb556e43c521f Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 12:11:38 -0800 Subject: [PATCH 24/26] Update Workflows --- .github/workflows/check-license.yml | 2 +- tools/tf-plan.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-license.yml b/.github/workflows/check-license.yml index eb3c2090..eef3969e 100644 --- a/.github/workflows/check-license.yml +++ b/.github/workflows/check-license.yml @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: 'CHECK - Check Lisence Boilerplate' +name: 'CHECK - License Boilerplate' on: schedule: diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 30f3245c..d1eb44dc 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -99,6 +99,7 @@ def main(PR): commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) if(status == 'fail'): sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) + except requests.exceptions.RequestException as e: print('No working directory with TF configs in PR.') raise SystemExit(e) @@ -171,13 +172,13 @@ def tf(dir): return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'}) if(return_code_init == 1): - comment = 'Terraform Init FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') + comment = 'Terraform Init FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' status = 'fail' if(return_code_plan == 1): - comment = 'Terraform Plan FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') + comment = 'Terraform Plan FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' status = 'fail' else: - comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(os.getcwd()+'/temp/', '' +'** !') + comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' return comment, status From 3934ead069033f2ca6431804380db08dfc80d3e5 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 12:18:18 -0800 Subject: [PATCH 25/26] Update tf-plan.py --- tools/tf-plan.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index d1eb44dc..719997e2 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -171,14 +171,15 @@ def tf(dir): return_code_init, stdout_init, stderr_init = tr.init_cmd(capture_output=False) return_code_plan, stdout_plan, stderr_plan = tr.plan_cmd(capture_output=False,var={'billing_account_id':'ABCD-EFGH-IJKL-MNOP', 'organization_id':'1234567890', 'random_id': '1234'}) + path = os.getcwd()+'/temp/' if(return_code_init == 1): - comment = 'Terraform Init FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' + comment = 'Terraform Init FAILED for: **' + dir.replace(path, '') + '** !' status = 'fail' if(return_code_plan == 1): - comment = 'Terraform Plan FAILED for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' + comment = 'Terraform Plan FAILED for: **' + dir.replace(path, '') + '** !' status = 'fail' else: - comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(os.getcwd()+'/temp/', '') + '** !' + comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(path, '') + '** !' return comment, status From 7748b88cf9be3e8049458bb5204e68639021a290 Mon Sep 17 00:00:00 2001 From: Mukul Gupta Date: Mon, 15 Nov 2021 12:27:50 -0800 Subject: [PATCH 26/26] Update tf-plan.py --- tools/tf-plan.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/tf-plan.py b/tools/tf-plan.py index 719997e2..0261d160 100644 --- a/tools/tf-plan.py +++ b/tools/tf-plan.py @@ -95,7 +95,11 @@ def main(PR): # print('****************************') # print(glob.glob(os.getcwd() + '/temp/' + dir+'/*/*')) + # Running Terraform Init & Terraform Plan comment, status = tf(os.getcwd() + '/temp/' + dir) + comment = comment + ' for: **' + dir + '** !' + + # Commenting on the PR commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN) if(status == 'fail'): sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir) @@ -173,13 +177,14 @@ def tf(dir): path = os.getcwd()+'/temp/' if(return_code_init == 1): - comment = 'Terraform Init FAILED for: **' + dir.replace(path, '') + '** !' + comment = 'Terraform Init FAILED' status = 'fail' if(return_code_plan == 1): - comment = 'Terraform Plan FAILED for: **' + dir.replace(path, '') + '** !' + comment = 'Terraform Plan FAILED' status = 'fail' else: - comment = 'Terraform Init & Terraform Plan SUCCESSFUL for: **' + dir.replace(path, '') + '** !' + comment = 'Terraform Init & Terraform Plan SUCCESSFUL' + status = 'pass' return comment, status