Skip to content

Commit e308cf2

Browse files
feat: added support for GCS for image exporter plugin (#4625)
* added support for gcp for tar plugin * refactoring * change in arch code * change in image * refactoring * changes * chnage in file number * tar plugin changes * changes in description * changes in variable name * renamed the file * renamed the file --------- Co-authored-by: Kamal Acharya <[email protected]>
1 parent 69cbce8 commit e308cf2

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DELETE FROM plugin_step_variable where name='GcpServiceAccountJson';
2+
DELETE FROM plugin_step_variable where name='GcpProjectName';
3+
UPDATE plugin_step_variable SET description='Provide which cloud storage provider you want to use: "aws" for Amazon S3 or "azure" for Azure Blob Storage' WHERE name='CloudProvider';
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
UPDATE plugin_pipeline_script SET script=E'#!/bin/bash
2+
set -eo pipefail
3+
if [[ $GcpServiceAccountJson ]]
4+
then
5+
echo $GcpServiceAccountJson > output.txt
6+
cat output.txt| base64 -d > gcloud.json
7+
fi
8+
architecture=$(uname -m)
9+
export platform=$(echo $CI_CD_EVENT | jq --raw-output .commonWorkflowRequest.ciBuildConfig.dockerBuildConfig.targetPlatform)
10+
echo $platform
11+
arch
12+
if [[ $platform == "linux/arm64,linux/amd64" ]] ; then
13+
platform=$Platform
14+
elif [[ $platform == "linux/arm64" ]]
15+
then
16+
platform="arm64"
17+
elif [[ $platform == "linux/amd64" ]]
18+
then
19+
platform="amd64"
20+
else
21+
if [[ $architecture == "x86_64" ]]
22+
then
23+
platform="amd64"
24+
else
25+
platform="arm64"
26+
fi
27+
fi
28+
echo $platform
29+
CloudProvider=$(echo "$CloudProvider" | awk \'{print tolower($0)}\')
30+
current_timestamp=$(date +%s)
31+
if [[ -z $FilePrefix ]]
32+
then
33+
file=$ContainerImage.tar
34+
file=$(echo $file | tr \'/\' \'_\')
35+
else
36+
file=$FilePrefix-$ContainerImage.tar
37+
file=$(echo $file | tr \'/\' \'_\')
38+
fi
39+
echo $file
40+
future_timestamp=$((current_timestamp + $Expiry * 60))
41+
future_date=$(date -u -d@"$future_timestamp" +"%Y-%m-%dT%H:%M:%SZ")
42+
aws_secs=$(($Expiry * 60))
43+
gcp_secs="${Expiry}m"
44+
docker pull --platform linux/$platform $ContainerImage
45+
docker save $ContainerImage > $file
46+
ls
47+
if [ $CloudProvider == "azure" ]
48+
then
49+
docker run --network=host --rm -v $(pwd):/data mcr.microsoft.com/azure-cli /bin/bash -c " az storage blob upload --account-name $AzureAccountName --account-key $AzureAccountKey --container-name $BucketName --name $file --file data/$file"
50+
echo "docker run --rm mcr.microsoft.com/azure-cli /bin/bash -c " az storage blob generate-sas --account-name $AzureAccountName --account-key $AzureAccountKey --container-name $BucketName --name $file --permissions r --expiry $future_date""
51+
sas_token=$(docker run --network=host --rm mcr.microsoft.com/azure-cli /bin/bash -c " az storage blob generate-sas --account-name $AzureAccountName --account-key $AzureAccountKey --container-name $BucketName --name $file --permissions r --expiry $future_date")
52+
token=$sas_token
53+
echo $token
54+
token=$(echo $sas_token| tr -d \'"\')
55+
echo $token
56+
link=https://$AzureAccountName.blob.core.windows.net/$BucketName/$file?$token
57+
fi
58+
if [ $CloudProvider == "aws" ]
59+
then
60+
echo "aws command"
61+
docker run --network=host --rm -v $(pwd):/data -e AWS_ACCESS_KEY_ID=$AwsAccessKey -e AWS_SECRET_ACCESS_KEY=$AwsSecretKey public.ecr.aws/aws-cli/aws-cli:latest s3 cp /data/$file s3://$BucketName --region $AwsRegion
62+
link=$(docker run --network=host --rm -v $(pwd):/data -e AWS_ACCESS_KEY_ID=$AwsAccessKey -e AWS_SECRET_ACCESS_KEY=$AwsSecretKey public.ecr.aws/aws-cli/aws-cli:latest s3 presign s3://$BucketName/$file --region $AwsRegion --expires-in $aws_secs )
63+
fi
64+
if [ $CloudProvider == "gcp" ]
65+
then
66+
echo "gcp command"
67+
docker run --network=host --rm -v $(pwd):/data quay.io/devtron/test:69a6cb4fb76e /bin/bash -c "gcloud auth activate-service-account --key-file=data/gcloud.json;gcloud config set project $GcpProjectName; gcloud storage ls;gsutil cp data/$file gs://$BucketName/ ; gcloud storage ls gs://$BucketName/;"
68+
link=$(docker run --network=host --rm -v $(pwd):/data quay.io/devtron/test:69a6cb4fb76e /bin/bash -c "gcloud auth activate-service-account --key-file=data/gcloud.json;gcloud config set project $GcpProjectName; gsutil signurl -d $gcp_secs data/gcloud.json gs://$BucketName/$file "| awk \'{print $NF}\' )
69+
fi
70+
echo "***Copy the below link to download the tar file***"
71+
echo $link
72+
' WHERE id=(select script_id from plugin_step where id=(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Container Image Exporter v1.0.0' and ps."index"=1 and ps.deleted=false));
73+
74+
75+
INSERT INTO "plugin_step_variable" ("id", "plugin_step_id", "name", "format", "description", "is_exposed", "allow_empty_value", "variable_type", "value_type", "variable_step_index", "deleted", "created_on", "created_by", "updated_on", "updated_by") VALUES
76+
(nextval('id_seq_plugin_step_variable'), (SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Container Image Exporter v1.0.0' and ps."index"=1 and ps.deleted=false), 'GcpServiceAccountJson','STRING','Provide Google cloud service account json creds in base64 format/Use scoped variables',true,true,'INPUT','NEW',1 ,'f','now()', 1, 'now()', 1),
77+
(nextval('id_seq_plugin_step_variable'), (SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='Container Image Exporter v1.0.0' and ps."index"=1 and ps.deleted=false), 'GcpProjectName','STRING','Specify Google Account Project Name',true,true,'INPUT','NEW',1 ,'f','now()', 1, 'now()', 1);
78+
79+
UPDATE plugin_step_variable SET description='Provide which cloud storage provider you want to use: "aws" for Amazon S3 or "azure" for Azure Blob Storage or "gcp" for Google Cloud Storage' WHERE name='CloudProvider';
80+
81+
UPDATE plugin_step_variable SET description='If you would like to add a prefix to the exported image files name, enter it here.' WHERE name='FilePrefix';

0 commit comments

Comments
 (0)