diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..ee74734 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +4.1.0 diff --git a/infrastructure/aws/ami/continuous_integration/packer-conf.json b/infrastructure/aws/ami/continuous_integration/packer-conf.json deleted file mode 100644 index 45514f9..0000000 --- a/infrastructure/aws/ami/continuous_integration/packer-conf.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "variables": { - "aws_access_key": "", - "aws_secret_key": "", - "region": "us-east-2" - }, - "builders": [{ - "type": "amazon-ebs", - "access_key": "{{user `aws_access_key`}}", - "secret_key": "{{user `aws_secret_key`}}", - "region": "us-east-2", - "source_ami": "ami-05512b79a46f1fabb", - "instance_type": "t2.small", - "ssh_username": "ec2-user", - "ami_name": "continuous-integration {{timestamp}}" - }], - "provisioners": [ - { - "type": "shell", - "script": "./prepare.sh" - } - ] -} diff --git a/infrastructure/aws/ami/continuous_integration/packer-conf.pkr.hcl b/infrastructure/aws/ami/continuous_integration/packer-conf.pkr.hcl new file mode 100644 index 0000000..a4a5faa --- /dev/null +++ b/infrastructure/aws/ami/continuous_integration/packer-conf.pkr.hcl @@ -0,0 +1,63 @@ +# This file was autogenerated by the 'packer hcl2_upgrade' command. We +# recommend double checking that everything is correct before going forward. We +# also recommend treating this file as disposable. The HCL2 blocks in this +# file can be moved to other files. For example, the variable blocks could be +# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be +# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at +# once they also need to be in the same folder. 'packer inspect folder/' +# will describe to you what is in that folder. + +# Avoid mixing go templating calls ( for example ```{{ upper(`string`) }}``` ) +# and HCL2 calls (for example '${ var.string_value_example }' ). They won't be +# executed together and the outcome will be unknown. + +# All generated input variables will be of 'string' type as this is how Packer JSON +# views them; you can change their type later on. Read the variables type +# constraints documentation +# https://www.packer.io/docs/templates/hcl_templates/variables#type-constraints for more info. +variable "aws_access_key" { + type = string +} + +variable "aws_secret_key" { + type = string +} + +variable "bazel_version" { + type = string +} + +variable "region" { + type = string + default = "us-east-2" +} + +# "timestamp" template function replacement +locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") } + +# source blocks are generated from your builders; a source can be referenced in +# build blocks. A build block runs provisioner and post-processors on a +# source. Read the documentation for source blocks here: +# https://www.packer.io/docs/templates/hcl_templates/blocks/source +source "amazon-ebs" "autogenerated_1" { + access_key = "${var.aws_access_key}" + ami_name = "continuous-integration ${local.timestamp}" + instance_type = "t2.small" + region = "us-east-2" + secret_key = "${var.aws_secret_key}" + source_ami = "ami-05512b79a46f1fabb" + ssh_username = "ec2-user" +} + +# a build block invokes sources and runs provisioning steps on them. The +# documentation for build blocks can be found here: +# https://www.packer.io/docs/templates/hcl_templates/blocks/build +build { + sources = ["source.amazon-ebs.autogenerated_1"] + + provisioner "shell" { + environment_vars = [format("BAZEL_VERSION=%s", var.bazel_version)] + script = "./prepare.sh" + } + +} diff --git a/infrastructure/aws/ami/continuous_integration/prepare.sh b/infrastructure/aws/ami/continuous_integration/prepare.sh index 127096a..9bf61b2 100644 --- a/infrastructure/aws/ami/continuous_integration/prepare.sh +++ b/infrastructure/aws/ami/continuous_integration/prepare.sh @@ -2,27 +2,31 @@ set -o errexit set -o pipefail +set -o nounset -BAZEL_VERSION="3.3.0" +## Install Bazel and the other project's dependencies +main() { + local bazel_version + bazel_version="${BAZEL_VERSION}" -## Install Bazel's and Project's dependencies + # NOTE: Amazon Linux 2 uses 'yum' not 'apt-get' + sudo yum groupinstall -y "Development Tools" + sudo yum install -y zip unzip + # Copied from https://github.com/buildkite/elastic-ci-stack-for-aws/pull/583/files + sudo yum install -y python3-pip python3 python3-setuptools + sudo yum install -y java-1.8.0-openjdk-devel -# NOTE: Amazon Linux 2 uses 'yum' not 'apt-get' -sudo yum groupinstall -y "Development Tools" -sudo yum install -y zip unzip -# Copied from https://github.com/buildkite/elastic-ci-stack-for-aws/pull/583/files -sudo yum install -y python3-pip python3 python3-setuptools -sudo yum install -y java-1.8.0-openjdk-devel + # Install Node + curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh" | bash + # shellcheck disable=SC1090 + . ~/.nvm/nvm.sh + nvm install node -# Node -curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash -# shellcheck disable=SC1090 -. ~/.nvm/nvm.sh -nvm install node + ## Install Bazel from source + curl -sSL -O \ + "https://github.com/bazelbuild/bazel/releases/download/${bazel_version}/bazel-${bazel_version}-installer-linux-x86_64.sh" + chmod +x "bazel-${bazel_version}-installer-linux-x86_64.sh" + sudo "./bazel-${bazel_version}-installer-linux-x86_64.sh" +} -## Install Bazel from source - -curl -sSL -O \ - "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" -chmod +x "bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" -sudo "./bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh" +main "$@" diff --git a/infrastructure/aws/ami/continuous_integration/rebuild.sh b/infrastructure/aws/ami/continuous_integration/rebuild.sh index 36084f1..f55dcbd 100755 --- a/infrastructure/aws/ami/continuous_integration/rebuild.sh +++ b/infrastructure/aws/ami/continuous_integration/rebuild.sh @@ -4,7 +4,23 @@ set -o errexit set -o nounset set -o pipefail -packer build \ +REPO_ROOT="$(git rev-parse --show-toplevel)" + +main() { + local bazel_version + local bazelversion_dotfile_path + + bazelversion_dotfile_path="${REPO_ROOT}/.bazelversion" + bazel_version="$(tr -d '\n' < "${bazelversion_dotfile_path}")" + + cd "${REPO_ROOT}/infrastructure/aws/ami/continuous_integration/" + + packer build \ -var "aws_access_key=${AWS_ACCESS_KEY_ID}" \ -var "aws_secret_key=${AWS_SECRET_ACCESS_KEY}" \ - "packer-conf.json" + -var "bazel_version=${bazel_version}" \ + "./packer-conf.pkr.hcl" +} + + +main "$@"