From 815e400fa9575ddc9c3522c7b8ec6a836ce9f098 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 5 Sep 2022 23:41:17 +0000 Subject: [PATCH 01/17] Implemented all the code from the other files to make it into one --- assignment.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ clouds.yaml | 20 +++++++++++ 2 files changed, 115 insertions(+) create mode 100644 clouds.yaml diff --git a/assignment.py b/assignment.py index 28758e7..d97aef9 100644 --- a/assignment.py +++ b/assignment.py @@ -1,32 +1,127 @@ import argparse import openstack +conn = openstack.connect(cloud_name="catalystcloud") + +IMAGE = "ubuntu-22.04-x86_64" +FLAVOUR = "c1.c1r1" +NETWORK = "private-net" +KEYPAIR = "huar2-key" +SERVERNAME = "huar2-server" + def create(): ''' Create a set of Openstack resources ''' + server = conn.compute.find_server(SERVERNAME) + if not server: + image = conn.compute.find_image(IMAGE) + flavour = conn.compute.find_flavor(FLAVOUR) + network = conn.network.find_network(NETWORK) + keypair = conn.compute.find_keypair(KEYPAIR) + + server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": "lab5-secgrp"}]) + + print("Creating VM: " + SERVERNAME) + server = conn.compute.wait_for_server(server) + print("VM Created: " + SERVERNAME) + + public_net = conn.network.find_network("public-net") + floating_ip = conn.network.create_ip(floating_network_id=public_net.id) + + print("Floating IP address: " + floating_ip.floating_ip_address) + conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) + print("Floating IP address added to " + SERVERNAME) + else: + print("Server already exists, no action taken") pass def run(): ''' Start a set of Openstack virtual machines if they are not already running. ''' + server = conn.compute.find_server(SERVERNAME) + if server: + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("Current status: " + serverStatus) + if serverStatus == "SHUTOFF": + conn.compute.start_server(serverDetails) + print("Starting server") + conn.compute.wait_for_server(serverDetails, status='ACTIVE') + print("Server started") + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("New status: " + serverStatus) + else: + print("Server already active, no action taken") + else: + print("Server not found") pass def stop(): ''' Stop a set of Openstack virtual machines if they are running. ''' + server = conn.compute.find_server(SERVERNAME) + if server: + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("Current status: " + serverStatus) + if serverStatus == "ACTIVE": + conn.compute.stop_server(serverDetails) + print("Server stopping") + conn.compute.wait_for_server(serverDetails, status='SHUTOFF') + print("Server stopped") + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("New status: " + serverStatus) + else: + print("Server already stopped, no action taken") + else: + print("Server not found") pass def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' + SERVER = conn.compute.find_server(SERVERNAME) + + if SERVER: + server = conn.compute.get_server(SERVER) + floating_ip = server["addresses"][NETWORK][1]["addr"] + + print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) + conn.compute.remove_floating_ip_from_server(server, floating_ip) + + server = conn.compute.delete_server(SERVER) + print("Server deleted") + + ip_address=conn.network.find_ip(floating_ip) + conn.network.delete_ip(ip_address) + print("Floating IP address " + floating_ip + " released") + else: + print("Server not found") pass def status(): ''' Print a status report on the OpenStack virtual machines created by the create action. ''' + server = conn.compute.find_server(SERVERNAME) + if server: + serverDetails = conn.compute.get_server(server) + private_ip = server["addresses"][NETWORK][0]["addr"] + if SERVERNAME == "huar2-server": + floating_ip = server["addresses"][NETWORK][1]["addr"] + else: + floating_ip = "not found" + serverStatus = serverDetails.status + print("Server name: " + SERVERNAME) + print("Current status: " + serverStatus) + print("Private IP: " + private_ip) + print("Floating IP: " + floating_ip) + else: + print("Server not found") pass diff --git a/clouds.yaml b/clouds.yaml new file mode 100644 index 0000000..d5422be --- /dev/null +++ b/clouds.yaml @@ -0,0 +1,20 @@ +# This is a clouds.yaml file, which can be used by OpenStack tools as a source +# of configuration on how to connect to a cloud. If this is your only cloud, +# just put this file in ~/.config/openstack/clouds.yaml and tools like +# python-openstackclient will just work with no further config. (You will need +# to add your password to the auth section) +# If you have more than one cloud account, add the cloud entry to the clouds +# section of your existing file and you can refer to them by name with +# OS_CLOUD=catalystcloud or --os-cloud=catalystcloud +clouds: + catalystcloud: + auth: + auth_url: https://api.nz-por-1.catalystcloud.io:5000 + username: "HUAR2@student.op.ac.nz" + password: "W2mlrr@12888" + project_id: 8e0cb4b58cee49289ea45cd97ea6ef49 + project_name: "tom-clark" + user_domain_name: "Default" + region_name: "nz-hlz-1" + interface: "public" + identity_api_version: 3 \ No newline at end of file From 972ea44a4ef1dc49ba31ffe9458c9e8adc0b7253 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 5 Sep 2022 23:50:57 +0000 Subject: [PATCH 02/17] Removed gitignore file --- .gitignore | 2 ++ clouds.yaml | 20 -------------------- 2 files changed, 2 insertions(+), 20 deletions(-) delete mode 100644 clouds.yaml diff --git a/.gitignore b/.gitignore index 894a44c..dd100ce 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ venv.bak/ # mypy .mypy_cache/ + +clouds.yaml diff --git a/clouds.yaml b/clouds.yaml deleted file mode 100644 index d5422be..0000000 --- a/clouds.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# This is a clouds.yaml file, which can be used by OpenStack tools as a source -# of configuration on how to connect to a cloud. If this is your only cloud, -# just put this file in ~/.config/openstack/clouds.yaml and tools like -# python-openstackclient will just work with no further config. (You will need -# to add your password to the auth section) -# If you have more than one cloud account, add the cloud entry to the clouds -# section of your existing file and you can refer to them by name with -# OS_CLOUD=catalystcloud or --os-cloud=catalystcloud -clouds: - catalystcloud: - auth: - auth_url: https://api.nz-por-1.catalystcloud.io:5000 - username: "HUAR2@student.op.ac.nz" - password: "W2mlrr@12888" - project_id: 8e0cb4b58cee49289ea45cd97ea6ef49 - project_name: "tom-clark" - user_domain_name: "Default" - region_name: "nz-hlz-1" - interface: "public" - identity_api_version: 3 \ No newline at end of file From 348cc4e7266007df8e58f68b898e2a1083f53688 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 6 Sep 2022 00:15:59 +0000 Subject: [PATCH 03/17] Added network --- assignment.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/assignment.py b/assignment.py index d97aef9..3385093 100644 --- a/assignment.py +++ b/assignment.py @@ -5,21 +5,30 @@ IMAGE = "ubuntu-22.04-x86_64" FLAVOUR = "c1.c1r1" -NETWORK = "private-net" +NETWORK = "huar2-net" KEYPAIR = "huar2-key" SERVERNAME = "huar2-server" +SUBNET = "huar2-subnet" def create(): ''' Create a set of Openstack resources ''' server = conn.compute.find_server(SERVERNAME) - if not server: - image = conn.compute.find_image(IMAGE) - flavour = conn.compute.find_flavor(FLAVOUR) - network = conn.network.find_network(NETWORK) - keypair = conn.compute.find_keypair(KEYPAIR) - + image = conn.compute.find_image(IMAGE) + flavour = conn.compute.find_flavor(FLAVOUR) + network = conn.network.find_network(NETWORK) + keypair = conn.compute.find_keypair(KEYPAIR) + if not network: + new_network = conn.network.create_network(name=NETWORK) + print(new_network) + new_subnet = conn.network.create_subnet( + name=SUBNET, + network_id=new_network.id, + ip_version='4', + cidr='192.168.50.0/24', + gateway_ip='192.168.50.1') + print(new_subnet) + '''if not server: server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": "lab5-secgrp"}]) - print("Creating VM: " + SERVERNAME) server = conn.compute.wait_for_server(server) print("VM Created: " + SERVERNAME) @@ -32,6 +41,8 @@ def create(): print("Floating IP address added to " + SERVERNAME) else: print("Server already exists, no action taken") + ''' + pass def run(): @@ -84,7 +95,15 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' - SERVER = conn.compute.find_server(SERVERNAME) + print("Delete Network:") + + network = conn.network.find_network(NETWORK) + + for subnet in network.subnet_ids: + conn.network.delete_subnet(subnet, ignore_missing=False) + conn.network.delete_network(network, ignore_missing=False) + + '''SERVER = conn.compute.find_server(SERVERNAME) if SERVER: server = conn.compute.get_server(SERVER) @@ -100,7 +119,7 @@ def destroy(): conn.network.delete_ip(ip_address) print("Floating IP address " + floating_ip + " released") else: - print("Server not found") + print("Server not found")''' pass def status(): From 7f68e2ce8951adb4d7fa79ca5fc9ee893468f4d0 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 6 Sep 2022 02:16:18 +0000 Subject: [PATCH 04/17] Added router --- assignment.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/assignment.py b/assignment.py index 3385093..5bdeaa7 100644 --- a/assignment.py +++ b/assignment.py @@ -9,6 +9,14 @@ KEYPAIR = "huar2-key" SERVERNAME = "huar2-server" SUBNET = "huar2-subnet" +ROUTER = "huar2-router" + +WEBSERVER = "huar2-web" +APPSERVER = "huar2-server" +DBSERVER = "huar2-db" +SERVERS = [WEBSERVER, APPSERVER, DBSERVER] + +public_net = conn.network.find_network("public-net") def create(): ''' Create a set of Openstack resources ''' @@ -18,15 +26,20 @@ def create(): network = conn.network.find_network(NETWORK) keypair = conn.compute.find_keypair(KEYPAIR) if not network: + print("Creating network: " + NETWORK) new_network = conn.network.create_network(name=NETWORK) - print(new_network) + print("Network " + NETWORK + " created") + print("Creating subnet: " + SUBNET) new_subnet = conn.network.create_subnet( name=SUBNET, network_id=new_network.id, ip_version='4', cidr='192.168.50.0/24', gateway_ip='192.168.50.1') - print(new_subnet) + print("Subnet created") + new_router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id}) + else: + print("Network not created") '''if not server: server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": "lab5-secgrp"}]) print("Creating VM: " + SERVERNAME) @@ -95,13 +108,14 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' - print("Delete Network:") - network = conn.network.find_network(NETWORK) for subnet in network.subnet_ids: + print("Deleting subnet ID: " + subnet) conn.network.delete_subnet(subnet, ignore_missing=False) + print("Deleting network: " + NETWORK) conn.network.delete_network(network, ignore_missing=False) + print("Network " + NETWORK + " deleted") '''SERVER = conn.compute.find_server(SERVERNAME) From 9f65ec7800b8227b3b669eb2c82ed2dba936d8b5 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 6 Sep 2022 05:55:27 +0000 Subject: [PATCH 05/17] Modified router --- assignment.py | 1 + 1 file changed, 1 insertion(+) diff --git a/assignment.py b/assignment.py index 5bdeaa7..d1691cb 100644 --- a/assignment.py +++ b/assignment.py @@ -38,6 +38,7 @@ def create(): gateway_ip='192.168.50.1') print("Subnet created") new_router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id}) + conn.network.add_interface_to_router(new_router.id, new_subnet.id) else: print("Network not created") '''if not server: From 5994c0c2fdcd9209ebb3a5f72b81af594f9281e7 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 6 Sep 2022 06:08:13 +0000 Subject: [PATCH 06/17] Completed destroying router --- assignment.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/assignment.py b/assignment.py index d1691cb..b41eb75 100644 --- a/assignment.py +++ b/assignment.py @@ -110,14 +110,24 @@ def destroy(): produced by the create action ''' network = conn.network.find_network(NETWORK) - - for subnet in network.subnet_ids: - print("Deleting subnet ID: " + subnet) - conn.network.delete_subnet(subnet, ignore_missing=False) - print("Deleting network: " + NETWORK) - conn.network.delete_network(network, ignore_missing=False) - print("Network " + NETWORK + " deleted") - + router = conn.network.find_router(ROUTER) + if network: + for subnet in network.subnet_ids: + conn.network.remove_interface_from_router(router.id, subnet) + print("Deleting subnet ID: " + subnet) + conn.network.delete_subnet(subnet, ignore_missing=False) + print("Deleting network: " + NETWORK) + conn.network.delete_network(network, ignore_missing=False) + print("Network " + NETWORK + " deleted") + else: + print("Network not found") + + if router: + print("Deleting router " + ROUTER) + conn.network.delete_router(router) + print("Router deleted") + else: + print("Router not found") '''SERVER = conn.compute.find_server(SERVERNAME) if SERVER: From 339067935979f53a4d8e957b99c73d93be7334b8 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 7 Sep 2022 07:41:19 +0000 Subject: [PATCH 07/17] Implemented server --- assignment.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/assignment.py b/assignment.py index b41eb75..0bee02c 100644 --- a/assignment.py +++ b/assignment.py @@ -10,6 +10,7 @@ SERVERNAME = "huar2-server" SUBNET = "huar2-subnet" ROUTER = "huar2-router" +SECGROUP = "assignment2" WEBSERVER = "huar2-web" APPSERVER = "huar2-server" @@ -24,30 +25,32 @@ def create(): image = conn.compute.find_image(IMAGE) flavour = conn.compute.find_flavor(FLAVOUR) network = conn.network.find_network(NETWORK) + router = conn.network.find_router(ROUTER) keypair = conn.compute.find_keypair(KEYPAIR) if not network: print("Creating network: " + NETWORK) - new_network = conn.network.create_network(name=NETWORK) + network = conn.network.create_network(name=NETWORK) print("Network " + NETWORK + " created") print("Creating subnet: " + SUBNET) - new_subnet = conn.network.create_subnet( + subnet = conn.network.create_subnet( name=SUBNET, - network_id=new_network.id, + network_id=network.id, ip_version='4', cidr='192.168.50.0/24', gateway_ip='192.168.50.1') print("Subnet created") - new_router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id}) - conn.network.add_interface_to_router(new_router.id, new_subnet.id) else: - print("Network not created") - '''if not server: - server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": "lab5-secgrp"}]) + print("Network exists, therefore not created") + if not router: + router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id}) + conn.network.add_interface_to_router(router.id, subnet.id) + else: + print("Router exists, therefore not created") + if not server: + server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) print("Creating VM: " + SERVERNAME) server = conn.compute.wait_for_server(server) print("VM Created: " + SERVERNAME) - - public_net = conn.network.find_network("public-net") floating_ip = conn.network.create_ip(floating_network_id=public_net.id) print("Floating IP address: " + floating_ip.floating_ip_address) @@ -55,8 +58,6 @@ def create(): print("Floating IP address added to " + SERVERNAME) else: print("Server already exists, no action taken") - ''' - pass def run(): From bb32c361db0be25223438eb137ad372684b14b91 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 8 Sep 2022 04:53:43 +0000 Subject: [PATCH 08/17] Created three servers --- assignment.py | 67 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/assignment.py b/assignment.py index 0bee02c..f5ea12e 100644 --- a/assignment.py +++ b/assignment.py @@ -13,7 +13,7 @@ SECGROUP = "assignment2" WEBSERVER = "huar2-web" -APPSERVER = "huar2-server" +APPSERVER = "huar2-app" DBSERVER = "huar2-db" SERVERS = [WEBSERVER, APPSERVER, DBSERVER] @@ -21,7 +21,7 @@ def create(): ''' Create a set of Openstack resources ''' - server = conn.compute.find_server(SERVERNAME) + #server = conn.compute.find_server(SERVERNAME) image = conn.compute.find_image(IMAGE) flavour = conn.compute.find_flavor(FLAVOUR) network = conn.network.find_network(NETWORK) @@ -46,18 +46,21 @@ def create(): conn.network.add_interface_to_router(router.id, subnet.id) else: print("Router exists, therefore not created") - if not server: - server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) - print("Creating VM: " + SERVERNAME) - server = conn.compute.wait_for_server(server) - print("VM Created: " + SERVERNAME) - floating_ip = conn.network.create_ip(floating_network_id=public_net.id) - - print("Floating IP address: " + floating_ip.floating_ip_address) - conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) - print("Floating IP address added to " + SERVERNAME) - else: - print("Server already exists, no action taken") + for server_name in SERVERS: + server = conn.compute.find_server(server_name) + if not server: + server = conn.compute.create_server(name=server_name, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) + print("Creating VM: " + server_name) + server = conn.compute.wait_for_server(server) + print("VM Created: " + server_name) + if server_name == WEBSERVER: + floating_ip = conn.network.create_ip(floating_network_id=public_net.id) + + print("Floating IP address: " + floating_ip.floating_ip_address) + conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) + print("Floating IP address added to " + server_name) + else: + print("Server already exists, no action taken") pass def run(): @@ -110,6 +113,24 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' + SERVER = conn.compute.find_server(SERVERNAME) + + if SERVER: + server = conn.compute.get_server(SERVER) + floating_ip = server["addresses"][NETWORK][1]["addr"] + + print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) + conn.compute.remove_floating_ip_from_server(server, floating_ip) + + server = conn.compute.delete_server(SERVER) + print("Server deleted") + + ip_address=conn.network.find_ip(floating_ip) + conn.network.delete_ip(ip_address) + print("Floating IP address " + floating_ip + " released") + else: + print("Server not found") + network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) if network: @@ -129,23 +150,7 @@ def destroy(): print("Router deleted") else: print("Router not found") - '''SERVER = conn.compute.find_server(SERVERNAME) - if SERVER: - server = conn.compute.get_server(SERVER) - floating_ip = server["addresses"][NETWORK][1]["addr"] - - print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) - conn.compute.remove_floating_ip_from_server(server, floating_ip) - - server = conn.compute.delete_server(SERVER) - print("Server deleted") - - ip_address=conn.network.find_ip(floating_ip) - conn.network.delete_ip(ip_address) - print("Floating IP address " + floating_ip + " released") - else: - print("Server not found")''' pass def status(): @@ -166,7 +171,7 @@ def status(): print("Private IP: " + private_ip) print("Floating IP: " + floating_ip) else: - print("Server not found") + print('Server "' + SERVERNAME + '" not found') pass From 3899d4f71d1238b5543356375e0502ea2ea36b41 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 12 Sep 2022 03:15:29 +0000 Subject: [PATCH 09/17] Added multiple servers --- assignment.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/assignment.py b/assignment.py index f5ea12e..454ddf5 100644 --- a/assignment.py +++ b/assignment.py @@ -60,7 +60,7 @@ def create(): conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) print("Floating IP address added to " + server_name) else: - print("Server already exists, no action taken") + print('Server "' + server_name + '" already exists, no action taken') pass def run(): @@ -157,21 +157,23 @@ def status(): ''' Print a status report on the OpenStack virtual machines created by the create action. ''' - server = conn.compute.find_server(SERVERNAME) - if server: - serverDetails = conn.compute.get_server(server) - private_ip = server["addresses"][NETWORK][0]["addr"] - if SERVERNAME == "huar2-server": - floating_ip = server["addresses"][NETWORK][1]["addr"] + for server_name in SERVERS: + server = conn.compute.find_server(server_name) + if server: + serverDetails = conn.compute.get_server(server) + private_ip = server["addresses"][NETWORK][0]["addr"] + if server_name == WEBSERVER: + floating_ip = server["addresses"][NETWORK][1]["addr"] + else: + floating_ip = "not found" + serverStatus = serverDetails.status + print("------------------------------------------") + print("Server name: " + server_name) + print("Current status: " + serverStatus) + print("Private IP: " + private_ip) + print("Floating IP: " + floating_ip) else: - floating_ip = "not found" - serverStatus = serverDetails.status - print("Server name: " + SERVERNAME) - print("Current status: " + serverStatus) - print("Private IP: " + private_ip) - print("Floating IP: " + floating_ip) - else: - print('Server "' + SERVERNAME + '" not found') + print('Server "' + server_name + '" not found') pass From 0758210a8b64dc3f41ae625c1930cdb204c7f163 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 13 Sep 2022 23:21:56 +0000 Subject: [PATCH 10/17] Added deletion of servers --- assignment.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/assignment.py b/assignment.py index 454ddf5..d6f0057 100644 --- a/assignment.py +++ b/assignment.py @@ -113,23 +113,25 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' - SERVER = conn.compute.find_server(SERVERNAME) + for server_name in SERVERS: + SERVER = conn.compute.find_server(server_name) - if SERVER: - server = conn.compute.get_server(SERVER) - floating_ip = server["addresses"][NETWORK][1]["addr"] + if SERVER: + server = conn.compute.get_server(SERVER) - print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) - conn.compute.remove_floating_ip_from_server(server, floating_ip) + if server_name == WEBSERVER: + floating_ip = server["addresses"][NETWORK][1]["addr"] + print("Disallowcating floating IP " + floating_ip + " from " + server_name) + conn.compute.remove_floating_ip_from_server(server, floating_ip) + ip_address=conn.network.find_ip(floating_ip) + conn.network.delete_ip(ip_address) + print("Floating IP address " + floating_ip + " released") - server = conn.compute.delete_server(SERVER) - print("Server deleted") + server = conn.compute.delete_server(SERVER) + print('Server "' + server_name + '" deleted') - ip_address=conn.network.find_ip(floating_ip) - conn.network.delete_ip(ip_address) - print("Floating IP address " + floating_ip + " released") - else: - print("Server not found") + else: + print('Server "' + server_name + '" not found, therefore no action taken') network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) From 0a451c7e64dca9b903d11bad21ae4bfcbe5e84e6 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 13 Sep 2022 23:36:39 +0000 Subject: [PATCH 11/17] Modified start and stop functions --- assignment.py | 66 +++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/assignment.py b/assignment.py index d6f0057..800f9b1 100644 --- a/assignment.py +++ b/assignment.py @@ -7,7 +7,6 @@ FLAVOUR = "c1.c1r1" NETWORK = "huar2-net" KEYPAIR = "huar2-key" -SERVERNAME = "huar2-server" SUBNET = "huar2-subnet" ROUTER = "huar2-router" SECGROUP = "assignment2" @@ -67,46 +66,55 @@ def run(): ''' Start a set of Openstack virtual machines if they are not already running. ''' - server = conn.compute.find_server(SERVERNAME) - if server: - serverDetails = conn.compute.get_server(server) - serverStatus = serverDetails.status - print("Current status: " + serverStatus) - if serverStatus == "SHUTOFF": - conn.compute.start_server(serverDetails) - print("Starting server") - conn.compute.wait_for_server(serverDetails, status='ACTIVE') - print("Server started") + for SERVERNAME in SERVERS: + server = conn.compute.find_server(SERVERNAME) + print("------------------------------------------") + print('Server: ' + SERVERNAME) + if server: serverDetails = conn.compute.get_server(server) serverStatus = serverDetails.status - print("New status: " + serverStatus) + print("Current status: " + serverStatus) + if serverStatus == "SHUTOFF": + conn.compute.start_server(serverDetails) + print("Starting server") + conn.compute.wait_for_server(serverDetails, status='ACTIVE') + print("Server started") + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("New status: " + serverStatus) + else: + print("Server already active, no action taken") else: - print("Server already active, no action taken") - else: - print("Server not found") + print("Server not found") + print("------------------------------------------") pass def stop(): ''' Stop a set of Openstack virtual machines if they are running. ''' - server = conn.compute.find_server(SERVERNAME) - if server: - serverDetails = conn.compute.get_server(server) - serverStatus = serverDetails.status - print("Current status: " + serverStatus) - if serverStatus == "ACTIVE": - conn.compute.stop_server(serverDetails) - print("Server stopping") - conn.compute.wait_for_server(serverDetails, status='SHUTOFF') - print("Server stopped") + for SERVERNAME in SERVERS: + server = conn.compute.find_server(SERVERNAME) + print("------------------------------------------") + print('Server: ' + SERVERNAME) + server = conn.compute.find_server(SERVERNAME) + if server: serverDetails = conn.compute.get_server(server) serverStatus = serverDetails.status - print("New status: " + serverStatus) + print("Current status: " + serverStatus) + if serverStatus == "ACTIVE": + conn.compute.stop_server(serverDetails) + print("Server stopping") + conn.compute.wait_for_server(serverDetails, status='SHUTOFF') + print("Server stopped") + serverDetails = conn.compute.get_server(server) + serverStatus = serverDetails.status + print("New status: " + serverStatus) + else: + print("Server already stopped, no action taken") else: - print("Server already stopped, no action taken") - else: - print("Server not found") + print("Server not found") + print("------------------------------------------") pass def destroy(): From bfa75ddc02833236b42ad5a9088320b014ee7ebe Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 14 Sep 2022 01:20:59 +0000 Subject: [PATCH 12/17] Formatted the for loops --- assignment.py | 52 ++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/assignment.py b/assignment.py index 800f9b1..79bf61a 100644 --- a/assignment.py +++ b/assignment.py @@ -27,6 +27,7 @@ def create(): router = conn.network.find_router(ROUTER) keypair = conn.compute.find_keypair(KEYPAIR) if not network: + print("------------------------------------------") print("Creating network: " + NETWORK) network = conn.network.create_network(name=NETWORK) print("Network " + NETWORK + " created") @@ -45,21 +46,23 @@ def create(): conn.network.add_interface_to_router(router.id, subnet.id) else: print("Router exists, therefore not created") - for server_name in SERVERS: - server = conn.compute.find_server(server_name) + for SERVERNAME in SERVERS: + server = conn.compute.find_server(SERVERNAME) + print("------------------------------------------") if not server: - server = conn.compute.create_server(name=server_name, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) - print("Creating VM: " + server_name) + server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) + print("Creating VM: " + SERVERNAME) server = conn.compute.wait_for_server(server) - print("VM Created: " + server_name) - if server_name == WEBSERVER: + print("VM Created") + if SERVERNAME == WEBSERVER: floating_ip = conn.network.create_ip(floating_network_id=public_net.id) print("Floating IP address: " + floating_ip.floating_ip_address) conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) - print("Floating IP address added to " + server_name) + print("Floating IP address added to " + SERVERNAME) else: - print('Server "' + server_name + '" already exists, no action taken') + print('Server "' + SERVERNAME + '" already exists, no action taken') + print("------------------------------------------") pass def run(): @@ -121,26 +124,28 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' - for server_name in SERVERS: - SERVER = conn.compute.find_server(server_name) - + for SERVERNAME in SERVERS: + SERVER = conn.compute.find_server(SERVERNAME) + print("------------------------------------------") + print('Server: ' + SERVERNAME) if SERVER: server = conn.compute.get_server(SERVER) - if server_name == WEBSERVER: + if SERVERNAME == WEBSERVER: floating_ip = server["addresses"][NETWORK][1]["addr"] - print("Disallowcating floating IP " + floating_ip + " from " + server_name) + print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) conn.compute.remove_floating_ip_from_server(server, floating_ip) ip_address=conn.network.find_ip(floating_ip) conn.network.delete_ip(ip_address) print("Floating IP address " + floating_ip + " released") server = conn.compute.delete_server(SERVER) - print('Server "' + server_name + '" deleted') + print('Server "' + SERVERNAME + '" deleted') else: - print('Server "' + server_name + '" not found, therefore no action taken') + print('Server "' + SERVERNAME + '" not found, therefore no action taken') + print("------------------------------------------") network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) if network: @@ -153,37 +158,38 @@ def destroy(): print("Network " + NETWORK + " deleted") else: print("Network not found") - + print("------------------------------------------") if router: print("Deleting router " + ROUTER) conn.network.delete_router(router) print("Router deleted") else: print("Router not found") - + print("------------------------------------------") pass def status(): ''' Print a status report on the OpenStack virtual machines created by the create action. ''' - for server_name in SERVERS: - server = conn.compute.find_server(server_name) + for SERVERNAME in SERVERS: + server = conn.compute.find_server(SERVERNAME) if server: serverDetails = conn.compute.get_server(server) private_ip = server["addresses"][NETWORK][0]["addr"] - if server_name == WEBSERVER: + if SERVERNAME == WEBSERVER: floating_ip = server["addresses"][NETWORK][1]["addr"] else: - floating_ip = "not found" + floating_ip = "no floating IP" serverStatus = serverDetails.status print("------------------------------------------") - print("Server name: " + server_name) + print("Server name: " + SERVERNAME) print("Current status: " + serverStatus) print("Private IP: " + private_ip) print("Floating IP: " + floating_ip) else: - print('Server "' + server_name + '" not found') + print('Server "' + SERVERNAME + '" not found') + print("------------------------------------------") pass From b4086156e160b0fc823d8c36e3c2fcaf9acda502 Mon Sep 17 00:00:00 2001 From: raymondhua Date: Wed, 21 Sep 2022 13:01:03 +1200 Subject: [PATCH 13/17] Fixed bug with router and network --- assignment.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/assignment.py b/assignment.py index 79bf61a..0686ebc 100644 --- a/assignment.py +++ b/assignment.py @@ -144,18 +144,15 @@ def destroy(): else: print('Server "' + SERVERNAME + '" not found, therefore no action taken') - - print("------------------------------------------") network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) + print("------------------------------------------") if network: for subnet in network.subnet_ids: - conn.network.remove_interface_from_router(router.id, subnet) + if router: + conn.network.remove_interface_from_router(router.id, subnet) print("Deleting subnet ID: " + subnet) conn.network.delete_subnet(subnet, ignore_missing=False) - print("Deleting network: " + NETWORK) - conn.network.delete_network(network, ignore_missing=False) - print("Network " + NETWORK + " deleted") else: print("Network not found") print("------------------------------------------") @@ -165,6 +162,11 @@ def destroy(): print("Router deleted") else: print("Router not found") + if network: + conn.network.delete_network(network, ignore_missing=False) + print("Network " + NETWORK + " deleted") + else: + print("Network not found") print("------------------------------------------") pass From f5543b17e1dfd8fc86459089f7931867462c3cb2 Mon Sep 17 00:00:00 2001 From: raymondhua Date: Wed, 21 Sep 2022 17:36:09 +1200 Subject: [PATCH 14/17] Fixed bug with subnet --- .gitignore | 1 + assignment.py | 47 ++++++++++++++++++++--------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index dd100ce..7f2dccd 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ parts/ sdist/ var/ wheels/ +venv/ *.egg-info/ .installed.cfg *.egg diff --git a/assignment.py b/assignment.py index 0686ebc..b45ebf8 100644 --- a/assignment.py +++ b/assignment.py @@ -16,18 +16,19 @@ DBSERVER = "huar2-db" SERVERS = [WEBSERVER, APPSERVER, DBSERVER] +NEWLINE = "--------------------------------------------------------" + public_net = conn.network.find_network("public-net") def create(): ''' Create a set of Openstack resources ''' - #server = conn.compute.find_server(SERVERNAME) image = conn.compute.find_image(IMAGE) flavour = conn.compute.find_flavor(FLAVOUR) network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) keypair = conn.compute.find_keypair(KEYPAIR) if not network: - print("------------------------------------------") + print(NEWLINE) print("Creating network: " + NETWORK) network = conn.network.create_network(name=NETWORK) print("Network " + NETWORK + " created") @@ -48,7 +49,7 @@ def create(): print("Router exists, therefore not created") for SERVERNAME in SERVERS: server = conn.compute.find_server(SERVERNAME) - print("------------------------------------------") + print(NEWLINE) if not server: server = conn.compute.create_server(name=SERVERNAME, image_id=image.id, flavor_id=flavour.id, networks=[{"uuid": network.id}], key_name=keypair.name, security_groups=[{"name": SECGROUP}]) print("Creating VM: " + SERVERNAME) @@ -62,7 +63,7 @@ def create(): print("Floating IP address added to " + SERVERNAME) else: print('Server "' + SERVERNAME + '" already exists, no action taken') - print("------------------------------------------") + print(NEWLINE) pass def run(): @@ -71,7 +72,7 @@ def run(): ''' for SERVERNAME in SERVERS: server = conn.compute.find_server(SERVERNAME) - print("------------------------------------------") + print(NEWLINE) print('Server: ' + SERVERNAME) if server: serverDetails = conn.compute.get_server(server) @@ -89,7 +90,7 @@ def run(): print("Server already active, no action taken") else: print("Server not found") - print("------------------------------------------") + print(NEWLINE) pass def stop(): @@ -98,9 +99,8 @@ def stop(): ''' for SERVERNAME in SERVERS: server = conn.compute.find_server(SERVERNAME) - print("------------------------------------------") + print(NEWLINE) print('Server: ' + SERVERNAME) - server = conn.compute.find_server(SERVERNAME) if server: serverDetails = conn.compute.get_server(server) serverStatus = serverDetails.status @@ -117,7 +117,7 @@ def stop(): print("Server already stopped, no action taken") else: print("Server not found") - print("------------------------------------------") + print(NEWLINE) pass def destroy(): @@ -126,36 +126,32 @@ def destroy(): ''' for SERVERNAME in SERVERS: SERVER = conn.compute.find_server(SERVERNAME) - print("------------------------------------------") + print(NEWLINE) print('Server: ' + SERVERNAME) if SERVER: server = conn.compute.get_server(SERVER) - if SERVERNAME == WEBSERVER: floating_ip = server["addresses"][NETWORK][1]["addr"] print("Disallowcating floating IP " + floating_ip + " from " + SERVERNAME) conn.compute.remove_floating_ip_from_server(server, floating_ip) - ip_address=conn.network.find_ip(floating_ip) + ip_address = conn.network.find_ip(floating_ip) conn.network.delete_ip(ip_address) print("Floating IP address " + floating_ip + " released") - - server = conn.compute.delete_server(SERVER) - print('Server "' + SERVERNAME + '" deleted') - + conn.compute.delete_server(SERVER) + print('Server deleted') else: - print('Server "' + SERVERNAME + '" not found, therefore no action taken') + print("Server not found, therefore no action taken") network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) - print("------------------------------------------") + print(NEWLINE) if network: for subnet in network.subnet_ids: - if router: - conn.network.remove_interface_from_router(router.id, subnet) print("Deleting subnet ID: " + subnet) conn.network.delete_subnet(subnet, ignore_missing=False) + print("Subnet deleted") else: print("Network not found") - print("------------------------------------------") + print(NEWLINE) if router: print("Deleting router " + ROUTER) conn.network.delete_router(router) @@ -165,9 +161,7 @@ def destroy(): if network: conn.network.delete_network(network, ignore_missing=False) print("Network " + NETWORK + " deleted") - else: - print("Network not found") - print("------------------------------------------") + print(NEWLINE) pass def status(): @@ -184,17 +178,16 @@ def status(): else: floating_ip = "no floating IP" serverStatus = serverDetails.status - print("------------------------------------------") + print(NEWLINE) print("Server name: " + SERVERNAME) print("Current status: " + serverStatus) print("Private IP: " + private_ip) print("Floating IP: " + floating_ip) else: print('Server "' + SERVERNAME + '" not found') - print("------------------------------------------") + print(NEWLINE) pass - ### You should not modify anything below this line ### if __name__ == '__main__': parser = argparse.ArgumentParser() From 542d6688a5a533ebb4d8606d772b0e4fac7c598d Mon Sep 17 00:00:00 2001 From: raymondhua Date: Wed, 21 Sep 2022 17:52:07 +1200 Subject: [PATCH 15/17] Added new print statements --- assignment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignment.py b/assignment.py index b45ebf8..79949fe 100644 --- a/assignment.py +++ b/assignment.py @@ -89,7 +89,7 @@ def run(): else: print("Server already active, no action taken") else: - print("Server not found") + print("Server not found, no action taken") print(NEWLINE) pass From 87c9f067083f330bffc824fa577d27a8996041a4 Mon Sep 17 00:00:00 2001 From: raymondhua Date: Sun, 16 Oct 2022 15:07:06 +1300 Subject: [PATCH 16/17] Finished assignment 2 --- assignment.py | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/assignment.py b/assignment.py index 79949fe..6d84371 100644 --- a/assignment.py +++ b/assignment.py @@ -16,6 +16,8 @@ DBSERVER = "huar2-db" SERVERS = [WEBSERVER, APPSERVER, DBSERVER] +GATEWAYADDRESS = '192.168.50.1' + NEWLINE = "--------------------------------------------------------" public_net = conn.network.find_network("public-net") @@ -27,8 +29,8 @@ def create(): network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) keypair = conn.compute.find_keypair(KEYPAIR) + print(NEWLINE) if not network: - print(NEWLINE) print("Creating network: " + NETWORK) network = conn.network.create_network(name=NETWORK) print("Network " + NETWORK + " created") @@ -38,13 +40,16 @@ def create(): network_id=network.id, ip_version='4', cidr='192.168.50.0/24', - gateway_ip='192.168.50.1') + gateway_ip=GATEWAYADDRESS) print("Subnet created") else: print("Network exists, therefore not created") + print(NEWLINE) if not router: + print("Creating router: " + ROUTER) router = conn.network.create_router(name=ROUTER, external_gateway_info={"network_id": public_net.id}) conn.network.add_interface_to_router(router.id, subnet.id) + print("Router created and attached to subnet: " + SUBNET) else: print("Router exists, therefore not created") for SERVERNAME in SERVERS: @@ -57,7 +62,6 @@ def create(): print("VM Created") if SERVERNAME == WEBSERVER: floating_ip = conn.network.create_ip(floating_network_id=public_net.id) - print("Floating IP address: " + floating_ip.floating_ip_address) conn.compute.add_floating_ip_to_server(server, floating_ip.floating_ip_address) print("Floating IP address added to " + SERVERNAME) @@ -90,7 +94,6 @@ def run(): print("Server already active, no action taken") else: print("Server not found, no action taken") - print(NEWLINE) pass def stop(): @@ -113,7 +116,7 @@ def stop(): serverDetails = conn.compute.get_server(server) serverStatus = serverDetails.status print("New status: " + serverStatus) - else: + elif serverStatus == "SHUTOFF": print("Server already stopped, no action taken") else: print("Server not found") @@ -143,24 +146,39 @@ def destroy(): print("Server not found, therefore no action taken") network = conn.network.find_network(NETWORK) router = conn.network.find_router(ROUTER) + subnet = conn.network.find_subnet(SUBNET) print(NEWLINE) - if network: - for subnet in network.subnet_ids: - print("Deleting subnet ID: " + subnet) - conn.network.delete_subnet(subnet, ignore_missing=False) - print("Subnet deleted") - else: - print("Network not found") - print(NEWLINE) + if network and subnet: + ports = conn.network.ports(network_id=network.id, subnet_id=subnet.id, ip_address=GATEWAYADDRESS) + if ports: + print("Deleting ports from subnet") + for port in ports: + if port.fixed_ips[0]['ip_address'] == GATEWAYADDRESS: + print("Removing interface from router") + conn.network.remove_interface_from_router(router, subnet_id=subnet.id, port_id=port.id) + print("Interface removed") + else: + conn.network.delete_port(port.id, ignore_missing=True) + else: + print("Ports not found") if router: print("Deleting router " + ROUTER) conn.network.delete_router(router) print("Router deleted") else: - print("Router not found") + print('Router "' + ROUTER + '" not found') + if subnet: + print("Deleting subnet ID: " + subnet.id) + conn.network.delete_subnet(subnet, ignore_missing=False) + print("Subnet deleted") + else: + print('Subnet "' + SUBNET + '" not found') + print(NEWLINE) if network: conn.network.delete_network(network, ignore_missing=False) print("Network " + NETWORK + " deleted") + else: + print('Network "' + NETWORK + '" not found') print(NEWLINE) pass From 1f3055262331f89d8f954a759594d6ed97ad2408 Mon Sep 17 00:00:00 2001 From: raymondhua Date: Sun, 16 Oct 2022 15:16:39 +1300 Subject: [PATCH 17/17] Finished assignment 2 --- assignment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignment.py b/assignment.py index 6d84371..4772af4 100644 --- a/assignment.py +++ b/assignment.py @@ -119,7 +119,7 @@ def stop(): elif serverStatus == "SHUTOFF": print("Server already stopped, no action taken") else: - print("Server not found") + print("Server not found, no action taken") print(NEWLINE) pass