|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2021 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# hack script for preparing AWS to run cluster-api-provider-openstack e2e |
| 18 | + |
| 19 | +set -x -o errexit -o nounset -o pipefail |
| 20 | + |
| 21 | +function cloud_init { |
| 22 | + AWS_REGION=${AWS_REGION:-"eu-central-1"} |
| 23 | + AWS_ZONE=${AWS_ZONE:-"eu-central-1a"} |
| 24 | + # AMIs: |
| 25 | + # * capa-ami-ubuntu-20.04-1.20.4-00-1613898574 id: ami-0120656d38c206057 |
| 26 | + # * ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20210223 id: ami-0767046d1677be5a0 |
| 27 | + AWS_AMI=${AWS_AMI:-"ami-0767046d1677be5a0"} |
| 28 | + # Choose via: https://eu-central-1.console.aws.amazon.com/ec2/v2/home?region=eu-central-1#InstanceTypes: |
| 29 | + AWS_MACHINE_TYPE=${AWS_MACHINE_TYPE:-"c5.metal"} |
| 30 | + AWS_NETWORK_NAME=${AWS_NETWORK_NAME:-"${CLUSTER_NAME}-mynetwork"} |
| 31 | + # prepare with: |
| 32 | + # * create key pair: |
| 33 | + # aws ec2 create-key-pair --key-name capo-e2e --query 'KeyMaterial' --region "${AWS_REGION}" --output text > ~/.ssh/aws-capo-e2e |
| 34 | + # * add to local agent and generate public key: |
| 35 | + # ssh-add ~/.ssh/aws-capo-e2e |
| 36 | + # ssh-keygen -y -f ~/.ssh/aws-capo-e2e > ~/.ssh/aws-capo-e2e.pub |
| 37 | + AWS_KEY_PAIR=${AWS_KEY_PAIR:-"capo-e2e"} |
| 38 | + # disable pagination of AWS cli |
| 39 | + export AWS_PAGER="" |
| 40 | + |
| 41 | + echo "Using: AWS_REGION: ${AWS_REGION} AWS_NETWORK_NAME: ${AWS_NETWORK_NAME}" |
| 42 | +} |
| 43 | + |
| 44 | +function init_infrastructure() { |
| 45 | + if [[ ${AWS_NETWORK_NAME} != "default" ]]; then |
| 46 | + if [[ $(aws ec2 describe-vpcs --filters Name=tag:Name,Values=capo-e2e-mynetwork --region="${AWS_REGION}" --query 'length(*[0])') = "0" ]]; |
| 47 | + then |
| 48 | + aws ec2 create-vpc --cidr-block "$PRIVATE_NETWORK_CIDR" --tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=${AWS_NETWORK_NAME}}]" --region="${AWS_REGION}" |
| 49 | + AWS_VPC_ID=$(aws ec2 describe-vpcs --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].VpcId' --output text) |
| 50 | + |
| 51 | + aws ec2 create-subnet --cidr-block "$PRIVATE_NETWORK_CIDR" --vpc-id "${AWS_VPC_ID}" --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=${AWS_NETWORK_NAME}}]" --region "${AWS_REGION}" --availability-zone "${AWS_ZONE}" |
| 52 | + AWS_SUBNET_ID=$(aws ec2 describe-subnets --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].SubnetId' --output text) |
| 53 | + # It's also the route table of the VPC |
| 54 | + AWS_SUBNET_ROUTE_TABLE_ID=$(aws ec2 describe-route-tables --filters "Name=vpc-id,Values=${AWS_VPC_ID}" --region "${AWS_REGION}" --query '*[0].RouteTableId' --output text) |
| 55 | + |
| 56 | + aws ec2 create-security-group --group-name "${AWS_NETWORK_NAME}" --description "${AWS_NETWORK_NAME}" --vpc-id "${AWS_VPC_ID}" --tag-specifications "ResourceType=security-group,Tags=[{Key=Name,Value=${AWS_NETWORK_NAME}}]" --region="${AWS_REGION}" |
| 57 | + AWS_SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].GroupId' --output text) |
| 58 | + |
| 59 | + aws ec2 authorize-security-group-ingress --group-id "${AWS_SECURITY_GROUP_ID}" --protocol tcp --port 22 --cidr 0.0.0.0/0 --region="${AWS_REGION}" |
| 60 | + |
| 61 | + # Documentation to enable internet access for subnet: |
| 62 | + # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#TroubleshootingInstancesConnectionTimeout |
| 63 | + aws ec2 create-internet-gateway --tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=${AWS_NETWORK_NAME}}]" --region="${AWS_REGION}" |
| 64 | + aws ec2 attach-internet-gateway --internet-gateway-id "${AWS_INTERNET_GATEWAY_ID}" --vpc-id "${AWS_VPC_ID}" --region="${AWS_REGION}" |
| 65 | + AWS_INTERNET_GATEWAY_ID=$(aws ec2 describe-internet-gateways --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].InternetGatewayId' --output text) |
| 66 | + |
| 67 | + aws ec2 create-route --route-table-id "${AWS_SUBNET_ROUTE_TABLE_ID}" --destination-cidr-block 0.0.0.0/0 --gateway-id "${AWS_INTERNET_GATEWAY_ID}" --region "${AWS_REGION}" |
| 68 | + aws ec2 create-route --route-table-id "${AWS_SUBNET_ROUTE_TABLE_ID}" --destination-ipv6-cidr-block ::/0 --gateway-id "${AWS_INTERNET_GATEWAY_ID}" --region "${AWS_REGION}" |
| 69 | + fi |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +function create_vm { |
| 74 | + local name=$1 && shift |
| 75 | + local ip=$1 && shift |
| 76 | + local userdata=$1 && shift |
| 77 | + local public=$1 && shift # Unused by AWS |
| 78 | + |
| 79 | + if [[ $(aws ec2 describe-instances --filters Name=tag:Name,Values="${name}" --region="${AWS_REGION}" --query 'length(*[0])') = "0" ]]; |
| 80 | + then |
| 81 | + AWS_SUBNET_ID=$(aws ec2 describe-subnets --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].SubnetId' --output text) |
| 82 | + AWS_SECURITY_GROUP_ID=$(aws ec2 describe-security-groups --filters Name=tag:Name,Values=capo-e2e-mynetwork --region "${AWS_REGION}" --query '*[0].GroupId' --output text) |
| 83 | + |
| 84 | + # /dev/sda1 is renamed to /dev/nvme0n1 by AWS |
| 85 | + aws ec2 run-instances --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${name}}]" \ |
| 86 | + --region "${AWS_REGION}" \ |
| 87 | + --placement "AvailabilityZone=${AWS_ZONE}" \ |
| 88 | + --image-id "${AWS_AMI}" \ |
| 89 | + --instance-type "${AWS_MACHINE_TYPE}" \ |
| 90 | + --block-device-mappings 'DeviceName=/dev/sda1,Ebs={VolumeSize=300}' \ |
| 91 | + --subnet-id "${AWS_SUBNET_ID}" \ |
| 92 | + --private-ip-address "${ip}" \ |
| 93 | + --count 1 \ |
| 94 | + --associate-public-ip-address \ |
| 95 | + --security-group-ids "${AWS_SECURITY_GROUP_ID}" \ |
| 96 | + --key-name "${AWS_KEY_PAIR}" \ |
| 97 | + --user-data "file://${userdata}" \ |
| 98 | + --no-paginate |
| 99 | + fi |
| 100 | + |
| 101 | + # wait a bit so the server has time to get a public ip |
| 102 | + sleep 30 |
| 103 | +} |
| 104 | + |
| 105 | +function get_public_ip { |
| 106 | + aws ec2 describe-instances --filters "Name=tag:Name,Values=${CLUSTER_NAME}-controller" --region "${AWS_REGION}" \ |
| 107 | + --query 'Reservations[*].Instances[*].PublicIpAddress' --output text |
| 108 | +} |
| 109 | + |
| 110 | +function get_mtu { |
| 111 | + # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/network_mtu.html |
| 112 | + echo 1300 |
| 113 | +} |
| 114 | + |
| 115 | +function get_ssh_public_key { |
| 116 | + cat "${SSH_PUBLIC_KEY_FILE}" |
| 117 | +} |
| 118 | + |
| 119 | +function get_ssh_private_key_file { |
| 120 | + echo "${SSH_PRIVATE_KEY_FILE}" |
| 121 | +} |
| 122 | + |
| 123 | +function cloud_cleanup { |
| 124 | + echo Not implemented |
| 125 | + exit 1 |
| 126 | +} |
0 commit comments