From 506b853e6fb5119b5ad4e38e79e9238b483bf578 Mon Sep 17 00:00:00 2001 From: Sebastian Rieger Date: Tue, 2 Jul 2019 22:15:45 +0200 Subject: [PATCH] Merge branch 'master' of gogs.informatik.hs-fulda.de:srieger/cloud-computing-msc-ai-examples # Conflicts: # aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc-designer.png # aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc.json # aws-cloudformation-demo/cloudcomp-counter-demo.json # aws-turnserver/stop.py # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc-designer.png # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo-with-vpc.json # example-projects/counter-demo/aws-cloudformation-demo/cloudcomp-counter-demo.json # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo-with-vpc-designer.png # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo-with-vpc.json # example-projects/counter-demo/aws-cloudformation/cloudcomp-counter-demo.json # example-projects/tug-of-war-in-the-clouds/stop.py # example-projects/turnserver/aws-boto3/start.py # example-projects/turnserver/aws-boto3/stop.py --- .idea/cloud-computing-msc-ai-examples.iml | 11 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .../aws-boto3-rds-db/start.py | 206 ++++++++++++++++++ .../aws-boto3-rds-db/stop.py | 57 +++++ .../create-loadbalancer.py | 86 ++++++++ .../start-and-add-new-target.py | 70 ++++++ .../aws-boto3-standalone-db/start.py | 134 ++++++------ .../aws-boto3-standalone-db/stop.py | 24 ++ .../web-content/cloud.php | 101 +++++++++ .../web-content/config.php | 3 + .../web-content/index.php | 36 +++ 13 files changed, 683 insertions(+), 66 deletions(-) create mode 100644 .idea/cloud-computing-msc-ai-examples.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py create mode 100644 example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py create mode 100644 example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py create mode 100644 example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py create mode 100644 example-projects/tug-of-war-in-the-clouds/web-content/cloud.php create mode 100644 example-projects/tug-of-war-in-the-clouds/web-content/config.php create mode 100644 example-projects/tug-of-war-in-the-clouds/web-content/index.php diff --git a/.idea/cloud-computing-msc-ai-examples.iml b/.idea/cloud-computing-msc-ai-examples.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/.idea/cloud-computing-msc-ai-examples.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3999087 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a4f1913 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py new file mode 100644 index 0000000..228d579 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py @@ -0,0 +1,206 @@ +import boto3 +from botocore.exceptions import ClientError + +region = 'eu-central-1' +availabilityZone = 'eu-central-1b' +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 \'cloud\';" | 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'/" + +client = boto3.setup_default_session(region_name=region) +ec2Client = boto3.client("ec2") +ec2Resource = boto3.resource('ec2') + +rdsClient = boto3.client("rds") + +response = ec2Client.describe_vpcs() +vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +subnet_id = ec2Client.describe_subnets( + Filters=[ + { + 'Name': 'availability-zone', 'Values': [availabilityZone] + } + ])['Subnets'][0]['SubnetId'] + +print("Deleting old instance...") +print("------------------------------------") + +response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}]) +print(response) +reservations = response['Reservations'] +for reservation in reservations: + for instance in reservation['Instances']: + if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": + response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']]) + print(response) + instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) + instanceToTerminate.wait_until_terminated() + +print("Deleting old DB instance...") +print("------------------------------------") + +try: + response = rdsClient.delete_db_instance( + DBInstanceIdentifier='tug-of-war-db1', + SkipFinalSnapshot=True, + DeleteAutomatedBackups=True + ) +except ClientError as e: + print(e) + +waiter = rdsClient.get_waiter('db_instance_deleted') +waiter.wait(DBInstanceIdentifier='tug-of-war-db1') + + + +print("Delete old security group...") +print("------------------------------------") + +try: + response = ec2Client.delete_security_group(GroupName='tug-of-war') +except ClientError as e: + print(e) + +print("Create security group...") +print("------------------------------------") + +try: + response = ec2Client.create_security_group(GroupName='tug-of-war', + Description='tug-of-war', + VpcId=vpc_id) + security_group_id = response['GroupId'] + print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id)) + + data = ec2Client.authorize_security_group_ingress( + GroupId=security_group_id, + IpPermissions=[ + {'IpProtocol': 'tcp', + 'FromPort': 3306, + 'ToPort': 3306, + 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, + {'IpProtocol': 'tcp', + 'FromPort': 22, + 'ToPort': 22, + 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, + {'IpProtocol': 'tcp', + 'FromPort': 80, + 'ToPort': 80, + 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, + {'IpProtocol': 'tcp', + 'FromPort': 443, + 'ToPort': 443, + 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]} + ]) + print('Ingress Successfully Set %s' % data) +except ClientError as e: + print(e) + +print("Running new DB instance...") +print("------------------------------------") + +response = rdsClient.create_db_instance(DBInstanceIdentifier="tug-of-war-db1", + AllocatedStorage=20, + DBName='tug_of_war', + # Engine='mariadb', + Engine='mysql', + # General purpose SSD + StorageType='gp2', + #StorageEncrypted=True, + AutoMinorVersionUpgrade=True, + # Set this to true later? + MultiAZ=False, + MasterUsername='cloud_tug_of_war', + MasterUserPassword='cloudcloud', + VpcSecurityGroupIds=[security_group_id], + #DBInstanceClass='db.m3.2xlarge', + DBInstanceClass='db.t3.micro', + Tags=[ + {'Key': 'Name', 'Value': 'tug-of-war-db1'}, + {'Key': 'tug-of-war', 'Value': 'db'} + ], +) + +waiter = rdsClient.get_waiter('db_instance_available') +waiter.wait(DBInstanceIdentifier='tug-of-war-db1') + +#dbArn = response['DBInstance'][0]['DBInstanceArn'] +dbEndpointAddress = response['DBInstance'][0]['Endpoint']['Address'] +dbEndpointPort = response['DBInstance'][0]['Endpoint']['Port'] + +print(dbEndpointAddress + ":" + dbEndpointPort) + +userDataWebServer = ('#!/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 httpd php php-mysql\n' + '\n' + 'service httpd start\n' + '\n' + 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n' + 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n' + 'cd /var/www/html\n' + 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n' + '\n' + '# change hostname of db connection\n' + 'sed -i s/localhost/' + dbEndpointAddress + '/g /var/www/html/config.php\n' + 'sed -i s/\\\'cloud\\\'/cloudcloud/g /var/www/html/config.php\n' + ) + +for i in range(1, 2): + print("Running new Web Server instance...") + print("------------------------------------") + + response = ec2Client.run_instances( + ImageId=imageId, + InstanceType=instanceType, + Placement={'AvailabilityZone': availabilityZone, }, + KeyName=keyName, + MinCount=1, + MaxCount=1, + UserData=userDataWebServer, + SecurityGroupIds=[ + security_group_id, + ], + + TagSpecifications=[ + { + 'ResourceType': 'instance', + 'Tags': [ + {'Key': 'Name', 'Value': 'tug-of-war-webserver' + i}, + {'Key': 'tug-of-war', 'Value': 'webserver'} + ], + } + ], + ) + + instanceIdWeb = response['Instances'][0]['InstanceId'] + + instance = ec2Resource.Instance(instanceIdWeb) + instance.wait_until_running() + instance.load() + + print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py new file mode 100644 index 0000000..ab42e83 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py @@ -0,0 +1,57 @@ +import time +import boto3 +from botocore.exceptions import ClientError + +region = 'eu-central-1' + +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...") +print("------------------------------------") + +try: + response = elbv2Client.describe_load_balancers(Names=['tug-of-war-loadbalancer']) + loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '') + response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn) +except ClientError as e: + print(e) + +time.sleep(5) + +try: + response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) + targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') + response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn) +except ClientError as e: + print(e) + + + +print("Deleting old instance...") +print("------------------------------------") + +response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}]) +print(response) +reservations = response['Reservations'] +for reservation in reservations: + for instance in reservation['Instances']: + if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": + response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']]) + print(response) + instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) + instanceToTerminate.wait_until_terminated() + +print("Delete old security group...") +print("------------------------------------") + +try: + response = ec2Client.delete_security_group(GroupName='tug-of-war') +except ClientError as e: + print(e) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py new file mode 100644 index 0000000..5d479cc --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py @@ -0,0 +1,86 @@ +import boto3 +from botocore.exceptions import ClientError + +region = 'eu-central-1' +availabilityZone = 'eu-central-1b' +subnet1 = 'subnet-41422b28' +subnet2 = 'subnet-5c5f6d16' +subnet3 = 'subnet-6f2ea214' + +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', '') + +elbv2Client = boto3.client('elbv2') + +response = elbv2Client.create_load_balancer( + Name='tug-of-war-loadbalancer', + Subnets=[ + subnet1, + subnet2, + subnet3, + ], + SecurityGroups=[ + security_group_id + ] +) + +loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '') +loadbalancer_dns = response.get('LoadBalancers', [{}])[0].get('DNSName', '') + +response = elbv2Client.create_target_group( + Name='tug-of-war-targetgroup', + Port=80, + Protocol='HTTP', + VpcId=vpc_id, +) + +targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') + +response = elbv2Client.create_listener( + DefaultActions=[ + { + 'TargetGroupArn': targetgroup_arn, + 'Type': 'forward', + }, + ], + LoadBalancerArn=loadbalancer_arn, + Port=80, + Protocol='HTTP', +) + +response = elbv2Client.modify_target_group_attributes( + TargetGroupArn=targetgroup_arn, + Attributes=[ + { + 'Key': 'stickiness.enabled', + 'Value': 'true' + }, + ] +) + +print("Registering instances...") +print("------------------------------------") + +response = ec2Client.describe_instances(Filters=[{'Name': 'tag:tug-of-war', 'Values': ['webserver']}]) +print(response) +reservations = response['Reservations'] +for reservation in reservations: + for instance in reservation['Instances']: + if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending": + response = elbv2Client.register_targets( + TargetGroupArn=targetgroup_arn, + Targets=[ + { + 'Id': instance['InstanceId'], + }, + ], + ) + +print('Load Balancer should be reachable at: ' + loadbalancer_dns) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py new file mode 100644 index 0000000..e9e7676 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py @@ -0,0 +1,70 @@ +import boto3 +from botocore.exceptions import ClientError + +region = 'eu-central-1' +availabilityZone = 'eu-central-1b' +subnet1 = 'subnet-41422b28' +subnet2 = 'subnet-5c5f6d16' +subnet3 = 'subnet-6f2ea214' + +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') + +for i in range(3, 4): + print("Running new Web Server instance...") + print("------------------------------------") + + response = ec2Client.run_instances( + ImageId=imageId, + InstanceType=instanceType, + Placement={'AvailabilityZone': availabilityZone, }, + KeyName=keyName, + MinCount=1, + MaxCount=1, + UserData=userDataWebServer, + SecurityGroupIds=[ + security_group_id, + ], + + TagSpecifications=[ + { + 'ResourceType': 'instance', + 'Tags': [ + {'Key': 'Name', 'Value': 'tug-of-war-webserver1'}, + {'Key': 'tug-of-war', 'Value': 'webserver'} + ], + } + ], + ) + + instanceIdWeb = response['Instances'][0]['InstanceId'] + + instance = ec2Resource.Instance(instanceIdWeb) + instance.wait_until_running() + instance.load() + + print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) + +try: + response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) + targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') +except ClientError as e: + print(e) + +print("Registering instance...") +print("------------------------------------") + +response = elbv2Client.register_targets( + TargetGroupArn=targetgroup_arn, + Targets=[ + { + 'Id': instanceIdWeb, + }, + ], +) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py index a93ca1f..63ae1d6 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py @@ -7,24 +7,24 @@ 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 \'cloud\';" | 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' - ) + '#!/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 \'cloud\';" | 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'/" @@ -116,7 +116,7 @@ response = ec2Client.run_instances( 'ResourceType': 'instance', 'Tags': [ {'Key': 'Name', 'Value': 'tug-of-war-db1'}, - {'Key': 'tug-of-war', 'Value': 'installed'} + {'Key': 'tug-of-war', 'Value': 'db'} ], } ], @@ -124,7 +124,7 @@ response = ec2Client.run_instances( instanceIdDB = response['Instances'][0]['InstanceId'] privateIpDB = response['Instances'][0]['PrivateIpAddress'] -#privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'] +# privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'] instance = ec2Resource.Instance(instanceIdDB) instance.wait_until_running() @@ -132,53 +132,55 @@ instance.wait_until_running() print(instanceIdDB) userDataWebServer = ('#!/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 httpd php php-mysql\n' - '\n' - 'service httpd start\n' - '\n' - 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n' - 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n' - 'cd /var/www/html\n' - 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n' - '\n' - '# change hostname of db connection\n' - 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\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 httpd php php-mysql\n' + '\n' + 'service httpd start\n' + '\n' + 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n' + 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n' + 'cd /var/www/html\n' + 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n' + '\n' + '# change hostname of db connection\n' + 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n' + ) + +for i in range(1, 3): + print("Running new Web Server instance...") + print("------------------------------------") + + response = ec2Client.run_instances( + ImageId=imageId, + InstanceType=instanceType, + Placement={'AvailabilityZone': availabilityZone, }, + KeyName=keyName, + MinCount=1, + MaxCount=1, + UserData=userDataWebServer, + SecurityGroupIds=[ + security_group_id, + ], + + TagSpecifications=[ + { + 'ResourceType': 'instance', + 'Tags': [ + {'Key': 'Name', 'Value': 'tug-of-war-webserver' + i}, + {'Key': 'tug-of-war', 'Value': 'webserver'} + ], + } + ], ) -print("Running new Web Server instance...") -print("------------------------------------") - -response = ec2Client.run_instances( - ImageId=imageId, - InstanceType=instanceType, - Placement={'AvailabilityZone': availabilityZone, }, - KeyName=keyName, - MinCount=1, - MaxCount=1, - UserData=userDataWebServer, - SecurityGroupIds=[ - security_group_id, - ], - - TagSpecifications=[ - { - 'ResourceType': 'instance', - 'Tags': [ - {'Key': 'Name', 'Value': 'tug-of-war-webserver1'}, - {'Key': 'tug-of-war', 'Value': 'installed'} - ], - } - ], -) + instanceIdWeb = response['Instances'][0]['InstanceId'] -instanceIdDB = response['Instances'][0]['InstanceId'] -privateIpDB = response['Instances'][0]['PrivateIpAddress'] -#privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'] + instance = ec2Resource.Instance(instanceIdWeb) + instance.wait_until_running() + instance.load() -instance = ec2Resource.Instance(instanceIdDB) -instance.wait_until_running() + print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py index 106b987..ab42e83 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py @@ -1,3 +1,4 @@ +import time import boto3 from botocore.exceptions import ClientError @@ -10,6 +11,29 @@ 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...") +print("------------------------------------") + +try: + response = elbv2Client.describe_load_balancers(Names=['tug-of-war-loadbalancer']) + loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '') + response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn) +except ClientError as e: + print(e) + +time.sleep(5) + +try: + response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) + targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') + response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn) +except ClientError as e: + print(e) + + + print("Deleting old instance...") print("------------------------------------") diff --git a/example-projects/tug-of-war-in-the-clouds/web-content/cloud.php b/example-projects/tug-of-war-in-the-clouds/web-content/cloud.php new file mode 100644 index 0000000..693a307 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/web-content/cloud.php @@ -0,0 +1,101 @@ + + + + + + +Choose your team!"; + ?> +
+ + +
+ + +

Pull!!!

+

+ query($sql) as $row) + { + if (abs(intval($row["value"])) >= intval($row["max_value"])) + { + unset($_SESSION["team"]); + $_SESSION["finished"] = true; + } + } + + if (isset($_POST['pull'])) + { + if ($_SESSION["team"] == "plus") + { + print "You pulled for team plus (green)..."; + $sql = "UPDATE clouds SET value=value+1 WHERE cloud_id = " . $_GET['cloud_id']; + $result = $pdo->query($sql); + } + if ($_SESSION["team"] == "minus") + { + print "You pulled for team minus (blue)..."; + $sql = "UPDATE clouds SET value=value-1 WHERE cloud_id = " . $_GET['cloud_id']; + $result = $pdo->query($sql); + } + } + + $sql = "SELECT * FROM clouds WHERE cloud_id = " . $_GET['cloud_id']; + foreach ($pdo->query($sql) as $row) + { + print "

Value: " . $row["value"] . " (Goal: " . $row["max_value"] . ")

"; + } + + if (!isset($_SESSION["finished"])) + { + ?> +
+ +
+ game over"; + if (intval($row["value"] == intval($row["max_value"]))) + { + print "Team plus (green) won!"; + } + else + { + print "Team minus (blue) won!"; + } + session_destroy(); + } + } +} +?> +
+Leave Game diff --git a/example-projects/tug-of-war-in-the-clouds/web-content/config.php b/example-projects/tug-of-war-in-the-clouds/web-content/config.php new file mode 100644 index 0000000..01c3604 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/web-content/config.php @@ -0,0 +1,3 @@ + diff --git a/example-projects/tug-of-war-in-the-clouds/web-content/index.php b/example-projects/tug-of-war-in-the-clouds/web-content/index.php new file mode 100644 index 0000000..e223608 --- /dev/null +++ b/example-projects/tug-of-war-in-the-clouds/web-content/index.php @@ -0,0 +1,36 @@ + + + + +

Welcome to "tug of war in the clouds"!

+

+Choose and tug a cloud: +

+

+query($sql); +} + +$sql = "SELECT * FROM clouds"; +foreach ($pdo->query($sql) as $row) { + if (abs(intval($row['value'])) < intval($row['max_value'])) + { + echo "" . $row['name'] . " (score: " . $row['value'] . ", goal:" . $row['max_value'] . ")
"; + } +} +?> + +

+

+or form a new cloud: +

+Name: +Goal: + +
+

\ No newline at end of file