From bdbf312ccbbcaea3cad39368eb51cb9018e06978 Mon Sep 17 00:00:00 2001 From: ermisw Date: Fri, 28 Jan 2022 15:49:40 +0100 Subject: [PATCH] added upload data export to s3 bucket --- .gitignore | 1 + export.py | 56 ++++++++++++++++++++++++++++++++++------------- requirements.txt | 1 + templates/main.tf | 3 --- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 9600f64..0ed8d28 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +output/ .env .terraform.lock.hcl \ No newline at end of file diff --git a/export.py b/export.py index 5682682..66942b3 100644 --- a/export.py +++ b/export.py @@ -6,10 +6,12 @@ import shutil import hcl from dotenv import load_dotenv from glob import glob +#from git import Repo +import zipfile +import boto3 - - # [AA 2022.01.17] Set available resources +# [AA 2022.01.17] Set available resources if os.name == 'nt': @@ -114,19 +116,6 @@ def replacedata(p, maplist): print("[DEBUG]", "Replacing key values %s at %s." % (key, p)) data = data.replace(key, val) - # # [AA 2021.12.22] Files that require a replacement for test,int and e2e,prod - # if os.path.basename(p) in specificfiles[0:2]: - # for key, val in mappings[2][maplist].items(): - # print("[DEBUG]", "Replacing key values %s at %s." % (key, p)) - # data = data.replace(key, val) - - # # [AA 2021.12.22] Replace key value for {$url} and {$env} for corresponding hub - # if os.path.basename(p) in specificfiles[2]: - # for mapping in mappings[3:5]: - # for key, val in mapping.items(): - # print("[DEBUG]", "Replacing key values %s at %s." % (key, p)) - # data = data.replace(key, val[pos]) - # [AA 2022.01.19] Replace key value for {} # [AA 2021.12.14] Write data from memory into file @@ -217,6 +206,12 @@ def importStates(): # terraform import module.alerting_profiles.dynatrace_alerting_profiles.CD_ABC 9348098098safs9f8 os.chdir(cwd) +def zipdir(path, ziph):# ziph is zipfile handlefor root, dirs, files in os.walk(path):for file in files: + for root, dirs, files in os.walk(path): + for file in files: + ziph.write(os.path.join(root, file), + os.path.relpath(os.path.join(root, file), + os.path.join(path, '..'))) # [AA 2022.01.17] Arguments passed if(len(sys.argv) == 2): @@ -253,8 +248,39 @@ if(len(sys.argv) == 2): # [AA, EW 2022.01.17] Import the states for each module importStates() + + #remote = "https://atc.bmwgroup.net/bitbucket/scm/opapm/coco_apm_fullbackup.git" + + #repo = Repo.init(path="C:\\Users\\wiegee\\Documents\\Projekte\\BMW\\dev\\DynatraceFullBackUpData") + + #if len(repo.index.diff("HEAD")) != 0: + # repo.git.add('.') + # repo.git.commit(m="testing") + + #if len(repo.remotes)==0: + # repo.git.remote('add', 'origin', remote) + + #repo.git.push('-u', 'origin', 'master') + + #s3 = boto3.client('s3') + + # Upload the Zip file ATA.zip within the folder2 on S3with open('ATA.zip', 'rb') as data: + + #s3.upload_fileobj(data, 'first-us-east-1-bucket', 'ATA.zip')g + + zipf = zipfile.ZipFile(outputFolder+"/"+timestamp + "_" +env+'.zip', 'w', zipfile.ZIP_DEFLATED) + zipdir(targetFolder, zipf) + zipf.close() + + s3 = boto3.client('s3') + + with open(outputFolder+"/"+timestamp + "_" +env+'.zip', 'rb') as data: + s3.upload_fileobj(data, 'coco-dynatrace-tfstate', 'backups/'+timestamp + "_" +env+'.zip') #TODO: Make s3 bucket name configurable over environment variables + print("Finished!") sys.exit(0) + + except Exception as err: print("Exception occured: "+ str(err)) sys.exit(1) diff --git a/requirements.txt b/requirements.txt index 8684d5d..657b555 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ typing python-dotenv pyhcl subprocess32 +boto3==1.17.0 \ No newline at end of file diff --git a/templates/main.tf b/templates/main.tf index e6504e9..dd494dd 100644 --- a/templates/main.tf +++ b/templates/main.tf @@ -23,6 +23,3 @@ provider "dynatrace" { dt_env_url = "${var.{$env}_ENV_URL}" dt_api_token = "${var.{$env}_API_TOKEN}" } - - -