Browse Source

added code section comments, added config param for VPC to support starting in envs with multiple vpcs

master
Sebastian Rieger 5 years ago
parent
commit
aa7160f656
  1. 25
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py
  2. 24
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py
  3. 65
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py
  4. 25
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py

25
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py

@ -1,19 +1,38 @@
import boto3
from botocore.exceptions import ClientError
################################################################################################
#
# Configuration Parameters
#
################################################################################################
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
vpc_id = 'vpc-eedd4187'
subnet1 = 'subnet-41422b28'
subnet2 = 'subnet-5c5f6d16'
subnet3 = 'subnet-6f2ea214'
# if you only have one VPC, vpc_id can be retrieved using:
#
# response = ec2Client.describe_vpcs()
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
################################################################################################
#
# boto3 code
#
################################################################################################
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}])
security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')

24
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py

@ -1,20 +1,38 @@
import boto3
from botocore.exceptions import ClientError
################################################################################################
#
# Configuration Parameters
#
################################################################################################
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
vpc_id = 'vpc-eedd4187'
imageId = 'ami-0cc293023f983ed53'
instanceType = 't3.nano'
keyName = 'srieger-pub'
# if you only have one VPC, vpc_id can be retrieved using:
#
# response = ec2Client.describe_vpcs()
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
################################################################################################
#
# boto3 code
#
################################################################################################
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
elbv2Client = boto3.client('elbv2')
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}])

65
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py

@ -1,39 +1,38 @@
import boto3
from botocore.exceptions import ClientError
################################################################################################
#
# Configuration Parameters
#
################################################################################################
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
vpc_id = 'vpc-eedd4187'
imageId = 'ami-0cc293023f983ed53'
instanceType = 't3.nano'
keyName = 'srieger-pub'
userDataDB = ('#!/bin/bash\n'
'#!/bin/bash\n'
'# extra repo for RedHat rpms\n'
'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
'# essential tools\n'
'yum install -y joe htop git\n'
'# mysql\n'
'yum install -y mariadb mariadb-server\n'
'\n'
'service mariadb start\n'
'\n'
'echo "create database cloud_tug_of_war" | mysql -u root\n'
'\n'
'echo "create table clouds ( cloud_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, value INT, max_value INT, PRIMARY KEY (cloud_id))" | mysql -u root cloud_tug_of_war\n'
'\n'
'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
'echo "FLUSH PRIVILEGES" | mysql -u root\n'
)
# convert with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
# if you only have one VPC, vpc_id can be retrieved using:
#
# response = ec2Client.describe_vpcs()
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
################################################################################################
#
# boto3 code
#
################################################################################################
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
subnet_id = ec2Client.describe_subnets(
Filters=[
{
@ -97,6 +96,28 @@ try:
except ClientError as e:
print(e)
userDataDB = ('#!/bin/bash\n'
'#!/bin/bash\n'
'# extra repo for RedHat rpms\n'
'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
'# essential tools\n'
'yum install -y joe htop git\n'
'# mysql\n'
'yum install -y mariadb mariadb-server\n'
'\n'
'service mariadb start\n'
'\n'
'echo "create database cloud_tug_of_war" | mysql -u root\n'
'\n'
'echo "create table clouds ( cloud_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, value INT, max_value INT, PRIMARY KEY (cloud_id))" | mysql -u root cloud_tug_of_war\n'
'\n'
'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
'echo "FLUSH PRIVILEGES" | mysql -u root\n'
)
# convert user-data from script with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
print("Running new DB instance...")
print("------------------------------------")

25
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py

@ -2,15 +2,34 @@ import time
import boto3
from botocore.exceptions import ClientError
################################################################################################
#
# Configuration Parameters
#
################################################################################################
region = 'eu-central-1'
vpc_id = 'vpc-eedd4187'
# if you only have one VPC, vpc_id can be retrieved using:
#
# response = ec2Client.describe_vpcs()
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
################################################################################################
#
# boto3 code
#
################################################################################################
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
elbv2Client = boto3.client('elbv2')
print("Deleting load balancer and deps...")

Loading…
Cancel
Save