From 3c9e953c41f126169f122c5983c5898e974af9cf Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 12 Nov 2022 17:12:41 -0800 Subject: [PATCH] 2022-11-12 05:12:41pm --- ci/release/build.sh | 20 +++++-- ci/release/builders/init_aws.sh | 98 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 6 deletions(-) create mode 100755 ci/release/builders/init_aws.sh diff --git a/ci/release/build.sh b/ci/release/build.sh index 68faa6d50..3ddbd47de 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -5,7 +5,7 @@ cd -- "$(dirname "$0")/../.." help() { cat </d2-.tar.gz @@ -17,9 +17,10 @@ Flags: --rebuild: By default build.sh will avoid rebuilding finished assets if they already exist but if you changed something and need to force rebuild, use this flag. ---local: By default build.sh uses \$TSTRUCT_MACOS_BUILDER and \$TSTRUCT_LINUX_BUILDER - to build the release archives. It's required for now due to the following - issue: https://github.com/terrastruct/d2/issues/31 +--local: By default build.sh uses \$TSTRUCT_MACOS_AMD64_BUILDER, + \$TSTRUCT_MACOS_ARM64_BUILDER, \$TSTRUCT_LINUX_AMD64_BUILDER and + \$TSTRUCT_LINUX_ARM64_BUILDER to build the release archives. It's required for + now due to the following issue: https://github.com/terrastruct/d2/issues/31 With --local, build.sh will cross compile locally. warning: This is only for testing purposes, do not use in production! EOF @@ -27,6 +28,12 @@ EOF build() { HW_BUILD_DIR="$BUILD_DIR/$OS/$ARCH/d2-$VERSION" + ARCHIVE="$BUILD_DIR/d2-$OS-$ARCH-$VERSION.tar.gz" + + if [ -e "$ARCHIVE" -a -z "${REBUILD-}" ]; then + log "skipping as already built at $ARCHIVE" + return 0 + fi sh_c mkdir -p "$HW_BUILD_DIR" sh_c rsync --recursive --perms --delete \ @@ -41,7 +48,7 @@ build() { sh_c go build -ldflags "-X oss.terrastruct.com/d2/lib/version.Version=$VERSION" \ -o "$HW_BUILD_DIR/bin/d2" ./cmd/d2 - sh_c tar czf "$BUILD_DIR/d2-$OS-$ARCH-$VERSION.tar.gz" "$HW_BUILD_DIR" + sh_c tar czf "$ARCHIVE" "$HW_BUILD_DIR" } main() { @@ -54,7 +61,8 @@ main() { HW_BUILD_DIR \ REBUILD \ LOCAL \ - DRYRUN + DRYRUN \ + ARCHIVE VERSION="$(git_describe_ref)" BUILD_DIR="ci/release/build/$VERSION" while :; do diff --git a/ci/release/builders/init_aws.sh b/ci/release/builders/init_aws.sh new file mode 100755 index 000000000..0f85ddfec --- /dev/null +++ b/ci/release/builders/init_aws.sh @@ -0,0 +1,98 @@ +#!/bin/sh +set -eu +cd -- "$(dirname "$0")/../../.." +. ./ci/sub/lib.sh + +help() { + cat </dev/null \ + | jq -r .SecurityGroups[0].GroupId) + if [ -z "$SG_ID" ]; then + SG_ID=$(sh_c aws ec2 create-security-group \ + --group-name ssh \ + --description ssh \ + --vpc-id "$VPC_ID" | jq -r .GroupId) + fi + + header security-group-ingress + SG_RULES_COUNT=$(aws ec2 describe-security-groups --group-names ssh \ + | jq -r '.SecurityGroups[0].IpPermissions | length') + if [ "$SG_RULES_COUNT" -eq 0 ]; then + sh_c aws ec2 authorize-security-group-ingress \ + --group-id "$SG_ID" \ + --protocol tcp \ + --port 22 \ + --cidr 0.0.0.0/0 >/dev/null + fi + + header linux-amd64 + if ! aws ec2 describe-instances \ + "--query=Reservations[*].Instances[?State.Name!='terminated']" \ + | grep -q d2-builder-linux-amd64; then + sh_c aws ec2 run-instances \ + --image-id=ami-0d593311db5abb72b \ + --count=1 \ + --instance-type=t2.micro \ + --security-groups=ssh \ + "--key-name=$KEY_NAME" \ + --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=d2-builder-linux-amd64}]' \ + 'ResourceType=volume,Tags=[{Key=Name,Value=d2-builder-linux-amd64}]' >/dev/null + fi + + header linux-arm64 + if ! aws ec2 describe-instances \ + "--query=Reservations[*].Instances[?State.Name!='terminated']" \ + | grep -q d2-builder-linux-arm64; then + sh_c aws ec2 run-instances \ + --image-id=ami-0efabcf945ffd8831 \ + --count=1 \ + --instance-type=t4g.nano \ + --security-groups=ssh \ + "--key-name=$KEY_NAME" \ + --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=d2-builder-linux-arm64}]' \ + 'ResourceType=volume,Tags=[{Key=Name,Value=d2-builder-linux-arm64}]' >/dev/null + fi +} + +main "$@"