From 7d9db41f4bfd2dda48af9f384fa44bfcb54f0b82 Mon Sep 17 00:00:00 2001 From: paulb Date: Thu, 7 Oct 2021 09:44:14 +0200 Subject: [PATCH] feat: allow use of v2 AWS CLI Signed-off-by: paulb --- docs/reference/aws/README.md | 51 +++++++++++-- docs/reference/aws/ecr.md | 21 +++--- stdlib/aws/aws.cue | 138 ++++++++++++++++++++++++++--------- 3 files changed, 156 insertions(+), 54 deletions(-) diff --git a/docs/reference/aws/README.md b/docs/reference/aws/README.md index 658631cc..7aa3e013 100644 --- a/docs/reference/aws/README.md +++ b/docs/reference/aws/README.md @@ -12,16 +12,15 @@ import "alpha.dagger.io/aws" ## aws.#CLI -Re-usable aws-cli component - ### aws.#CLI Inputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*config.region* | `string` |AWS region | -|*config.accessKey* | `dagger.#Secret` |AWS access key | -|*config.secretKey* | `dagger.#Secret` |AWS secret key | -|*config.localMode* | `*false \| bool` |AWS localstack mode | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*config.region* | `string` |AWS region | +|*config.accessKey* | `dagger.#Secret` |AWS access key | +|*config.secretKey* | `dagger.#Secret` |AWS secret key | +|*config.localMode* | `*false \| bool` |AWS localstack mode | +|*version* | `*"1.18" \| string` |- | ### aws.#CLI Outputs @@ -43,3 +42,39 @@ AWS Config shared by all AWS packages ### aws.#Config Outputs _No output._ + +## aws.#V1 + +Configuration specific to CLI v1 + +### aws.#V1 Inputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*config.region* | `string` |AWS region | +|*config.accessKey* | `dagger.#Secret` |AWS access key | +|*config.secretKey* | `dagger.#Secret` |AWS secret key | +|*config.localMode* | `*false \| bool` |AWS localstack mode | +|*version* | `*"1.18" \| string` |- | + +### aws.#V1 Outputs + +_No output._ + +## aws.#V2 + +Configuration specific to CLI v2 + +### aws.#V2 Inputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*config.region* | `string` |AWS region | +|*config.accessKey* | `dagger.#Secret` |AWS access key | +|*config.secretKey* | `dagger.#Secret` |AWS secret key | +|*config.localMode* | `*false \| bool` |AWS localstack mode | +|*version* | `*"2.1.27" \| string` |- | + +### aws.#V2 Outputs + +_No output._ diff --git a/docs/reference/aws/ecr.md b/docs/reference/aws/ecr.md index a89eea5c..f6da7236 100644 --- a/docs/reference/aws/ecr.md +++ b/docs/reference/aws/ecr.md @@ -16,16 +16,17 @@ Convert ECR credentials to Docker Login format ### ecr.#Credentials Inputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*config.region* | `string` |AWS region | -|*config.accessKey* | `dagger.#Secret` |AWS access key | -|*config.secretKey* | `dagger.#Secret` |AWS secret key | -|*config.localMode* | `*false \| bool` |AWS localstack mode | -|*ctr.image.config.region* | `string` |AWS region | -|*ctr.image.config.accessKey* | `dagger.#Secret` |AWS access key | -|*ctr.image.config.secretKey* | `dagger.#Secret` |AWS secret key | -|*ctr.image.config.localMode* | `*false \| bool` |AWS localstack mode | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*config.region* | `string` |AWS region | +|*config.accessKey* | `dagger.#Secret` |AWS access key | +|*config.secretKey* | `dagger.#Secret` |AWS secret key | +|*config.localMode* | `*false \| bool` |AWS localstack mode | +|*ctr.image.config.region* | `string` |AWS region | +|*ctr.image.config.accessKey* | `dagger.#Secret` |AWS access key | +|*ctr.image.config.secretKey* | `dagger.#Secret` |AWS secret key | +|*ctr.image.config.localMode* | `*false \| bool` |AWS localstack mode | +|*ctr.image.version* | `*"1.18" \| string` |- | ### ecr.#Credentials Outputs diff --git a/stdlib/aws/aws.cue b/stdlib/aws/aws.cue index 9e5cf171..af0389a8 100644 --- a/stdlib/aws/aws.cue +++ b/stdlib/aws/aws.cue @@ -2,6 +2,7 @@ package aws import ( + "regexp" "alpha.dagger.io/dagger" "alpha.dagger.io/dagger/op" "alpha.dagger.io/alpine" @@ -19,10 +20,11 @@ import ( localMode: dagger.#Input & {*false | bool} } -// Re-usable aws-cli component -#CLI: { +// Configuration specific to CLI v1 +#V1: { config: #Config package: [string]: string | bool + version: dagger.#Input & {*"1.18" | string} #up: [ op.#Load & { @@ -31,52 +33,116 @@ import ( "package": bash: "=~5.1" "package": jq: "=~1.6" "package": curl: true - "package": "aws-cli": "=~1.18" + "package": "aws-cli": "=~\( version )" + if config.localMode != false { + package: "py3-pip": true + } + } + + }, + ] +} + +// Configuration specific to CLI v2 +#V2: { + config: #Config + package: [string]: string | bool + version: dagger.#Input & {*"2.1.27" | string} + + #up: [ + op.#Load & { + from: alpine.#Image & { + "package": package + "package": bash: "=~5.1" + "package": jq: "=~1.6" + "package": curl: true + "package": binutils: true if config.localMode != false { package: "py3-pip": true } } }, + //https://stackoverflow.com/a/61268529 op.#Exec & { - args: [ - "/bin/bash", - "--noprofile", - "--norc", - "-eo", - "pipefail", - "-c", - if config.localMode == false { + env: AWS_CLI_VERSION: version + args: ["/bin/bash", "--noprofile", "--norc", "-eo", "pipefail", "-c", + #""" + curl -sL https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub -o /etc/apk/keys/sgerrand.rsa.pub + curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-2.31-r0.apk + curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-bin-2.31-r0.apk + curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.31-r0/glibc-i18n-2.31-r0.apk + apk add --no-cache glibc-2.31-r0.apk glibc-bin-2.31-r0.apk glibc-i18n-2.31-r0.apk + /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8 + + curl -s https://awscli.amazonaws.com/awscli-exe-linux-x86_64-${AWS_CLI_VERSION}.zip -o awscliv2.zip + unzip awscliv2.zip > /dev/null + ./aws/install + rm -rf awscliv2.zip aws /usr/local/aws-cli/v2/*/dist/aws_completer /usr/local/aws-cli/v2/*/dist/awscli/data/ac.index \ + usr/local/aws-cli/v2/*/dist/awscli/examples glibc-*.apk + """#] + }, + ] +} + +#CLI: { + config: #Config + package: [string]: string | bool + version: dagger.#Input & {*"1.18" | string} + + _isV2: regexp.Match("^2.*$", version) + + #up: [ + op.#Load & { + if _isV2 == false { + from: #V1 & { + "config": config + "package": package + "version": version + } + } + if _isV2 == true { + from: #V2 & { + "config": config + "package": package + "version": version + } + } + + }, + op.#Exec & { + if config.localMode == false { + args: ["/bin/bash", "--noprofile", "--norc", "-eo", "pipefail", "-c", #""" - aws configure set aws_access_key_id "$(cat /run/secrets/access_key)" - aws configure set aws_secret_access_key "$(cat /run/secrets/secret_key)" + aws configure set aws_access_key_id "$(cat /run/secrets/access_key)" + aws configure set aws_secret_access_key "$(cat /run/secrets/secret_key)" - aws configure set default.region "$AWS_DEFAULT_REGION" - aws configure set default.cli_pager "" - aws configure set default.output "json" - """# - }, - if config.localMode != false { + aws configure set default.region "$AWS_DEFAULT_REGION" + aws configure set default.cli_pager "" + aws configure set default.output "json" + """#] + } + if config.localMode == true { + args: [ "/bin/bash", "--noprofile", "--norc", "-eo", "pipefail", "-c", #""" - # Download awscli v3 and override aws - pip install awscli-local==0.14 - mv /usr/bin/awslocal /usr/bin/aws + # Download awscli v3 and override aws + pip install awscli-local==0.14 + mv /usr/bin/awslocal /usr/bin/aws - # Configure - mkdir -p ~/.aws/ + # Configure + mkdir -p ~/.aws/ - # Set up ~/.aws/config - echo "[default]" > ~/.aws/config - echo "region = $AWS_DEFAULT_REGION" >> ~/.aws/config - echo "cli_pager =" >> ~/.aws/config - echo "output = json" >> ~/.aws/config + # Set up ~/.aws/config + echo "[default]" > ~/.aws/config + echo "region = $AWS_DEFAULT_REGION" >> ~/.aws/config + echo "cli_pager =" >> ~/.aws/config + echo "output = json" >> ~/.aws/config - # Set up ~/.aws/credentials - echo "[default]" > ~/.aws/credentials - echo "aws_access_key_id = $(cat /run/secrets/access_key)" >> ~/.aws/credentials - echo "aws_secret_access_key = $(cat /run/secrets/secret_key)" >> ~/.aws/credentials - """# - }, - ] + # Set up ~/.aws/credentials + echo "[default]" > ~/.aws/credentials + echo "aws_access_key_id = $(cat /run/secrets/access_key)" >> ~/.aws/credentials + echo "aws_secret_access_key = $(cat /run/secrets/secret_key)" >> ~/.aws/credentials + """#] + } mount: "/run/secrets/access_key": secret: config.accessKey mount: "/run/secrets/secret_key": secret: config.secretKey env: AWS_DEFAULT_REGION: config.region