Given an apt-offline.sig file, launch an AWS EC2 instance to download the corresponding packages and upload the resulting bundle to the configured S3 bucket:
$ download-apt-packages apt-offline.sig
#! /bin/bash | |
(( $# < 1 )) && { 1>&2 echo "Syntax: $0 <apt-offline.sig>" ; exit 1 ; } | |
[email protected] | |
IMAGE_ID=ami-de8fb135 | |
SUBNET_ID=subnet-83148dce | |
SECURITY_GROUP_IDS=sg-03b9799b5d1b185c1 | |
INSTANCE_TYPE=t2.micro | |
IAM_PROFILE=s3-sns-full-access | |
ARG_SIG_FILE="$1" | |
interpolate-vars() { | |
ARGS=("$@") | |
echo "APTOFFLINE_SIG=\"$(cat ${ARG_SIG_FILE} | gzip - | base64 -w0)\""| python <(cat <<'EOF' | |
import sys | |
data = sys.stdin.read() | |
with open(sys.argv[1]) as f: | |
section = 0 | |
while True: | |
line = f.readline() | |
if not line: | |
break | |
if line.find('--CUT--') != -1: | |
section += 1 | |
if section == 1: | |
sys.stdout.write(data) | |
elif section != 1: | |
sys.stdout.write(line) | |
EOF | |
) "${ARGS[@]}" | |
} | |
interpolate-vars init.sh > init.sh.tmp | |
aws ec2 run-instances \ | |
--key-name ${KEY_NAME} \ | |
--associate-public-ip-address \ | |
--instance-initiated-shutdown-behavior terminate \ | |
--image-id ${IMAGE_ID} \ | |
--subnet-id ${SUBNET_ID} \ | |
--security-group-ids ${SECURITY_GROUP_IDS} \ | |
--instance-type ${INSTANCE_TYPE} \ | |
--iam-instance-profile Name=${IAM_PROFILE} \ | |
--count 1 \ | |
--user-data file://init.sh.tmp |
#! /bin/bash | |
set -x | |
S3_REGION=eu-central-1 | |
S3_BUCKET=romuloceccon | |
SNS_TOPIC="arn:aws:sns:eu-central-1:447480018313:aptoffline-completed" | |
# --CUT-- begin custom section | |
APTOFFLINE_SIG="Cg==" | |
# --CUT-- end custom section | |
WORKDIR="/root/download" | |
SIG_FILE="${WORKDIR}/apt-offline.sig" | |
BUNDLE_BASENAME="apt-offline-bundle-$(date -u +%Y%m%d%H%M).zip" | |
BUNDLE_NAME="${WORKDIR}/${BUNDLE_BASENAME}" | |
SUBJECT="S3 file ${BUNDLE_BASENAME}" | |
mkdir -p ${WORKDIR} | |
echo "${APTOFFLINE_SIG}"| base64 -d | gzip -d - > ${SIG_FILE} | |
apt-get update | |
apt-get install -y apt-offline awscli | |
aws configure set region ${S3_REGION} | |
apt-offline get --bundle ${BUNDLE_NAME} ${SIG_FILE} | |
aws s3 cp ${BUNDLE_NAME} s3://${S3_BUCKET}/ --storage-class STANDARD_IA | |
S3_RESULT="$?" | |
BUNDLE_SIZE=$(stat -c %s ${BUNDLE_NAME}) | |
aws sns publish --topic-arn ${SNS_TOPIC} --subject "${SUBJECT}" --message \ | |
$'file size: '${BUNDLE_SIZE}$'\naws s3 cp result: '${S3_RESULT} | |
shutdown -h now | |
exit 0 |