From 7bd8b4f4d598e5c08a0f7ef86fb4ea5764fc8fac Mon Sep 17 00:00:00 2001 From: Mathew Nichols <64763125+MathewNichols@users.noreply.github.com> Date: Mon, 12 Sep 2022 10:47:50 +1200 Subject: [PATCH 1/9] Add cloud.yaml file for authentication --- clouds.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 clouds.yaml diff --git a/clouds.yaml b/clouds.yaml new file mode 100644 index 0000000..fdb30f9 --- /dev/null +++ b/clouds.yaml @@ -0,0 +1,19 @@ +# 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: "NICHMR1@student.op.ac.nz" + project_id: 8e0cb4b58cee49289ea45cd97ea6ef49 + project_name: "tom-clark" + user_domain_name: "Default" + region_name: "nz-por-1" + interface: "public" + identity_api_version: 3 \ No newline at end of file From 289cd7f893032a0a85ea262d58dbc759be53f07e Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Mon, 12 Sep 2022 00:40:21 +0000 Subject: [PATCH 2/9] Create and destroy a network --- assignment.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/assignment.py b/assignment.py index 28758e7..05a9314 100644 --- a/assignment.py +++ b/assignment.py @@ -1,9 +1,42 @@ import argparse import openstack +IMAGE = 'ubuntu-minimal-22.04-x86_64' +FLAVOUR = 'c1.c1r1' +NETWORK = '' +KEYPAIR = 'nichmr1-key' + + + def create(): ''' Create a set of Openstack resources ''' - pass + #Establish a connection to catalystcloud + conn = openstack.connect(cloud_name='catalystcloud') + + #Create a network + network = conn.network.create_network( + name='nichmr1-net' + ) + + print(network) + + subnet = conn.network.create_subnet( + name='nichmr1-net', + network_id=network.id, + ip_version='4', + cidr='192.168.50.0/24', + gateway_ip='192.168.50.1' + ) + + print(subnet) + #Create a router + + #Create a floating IP address + + + #Create three servers + + def run(): ''' Start a set of Openstack virtual machines @@ -21,7 +54,16 @@ def destroy(): ''' Tear down the set of Openstack resources produced by the create action ''' - pass + conn = openstack.connect(cloud_name='catalystcloud') + + #Delete Networking Stuff + network = conn.network.find_network( + 'nichmr1-net' + ) + + for subnet in network.subnet_ids: + conn.network.delete_subnet(subnet, ignore_missing=False) + conn.network.delete_network(network, ignore_missing=False) def status(): ''' Print a status report on the OpenStack From 11282db841b53195628e7323020701eb0863b6bd Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Thu, 15 Sep 2022 00:05:51 +0000 Subject: [PATCH 3/9] Create and destroy a router --- assignment.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/assignment.py b/assignment.py index 05a9314..5c1d556 100644 --- a/assignment.py +++ b/assignment.py @@ -18,7 +18,7 @@ def create(): name='nichmr1-net' ) - print(network) + print('Network created') subnet = conn.network.create_subnet( name='nichmr1-net', @@ -28,9 +28,17 @@ def create(): gateway_ip='192.168.50.1' ) - print(subnet) + print('Subnet created') + #Create a router + public_network = conn.network.find_network('public-net') + router = conn.network.create_router( + name='nichmr1-rtr', + external_gateway_info={'network_id': public_network.id} + ) + conn.network.add_interface_to_router(router, subnet.id) + print('Router created') #Create a floating IP address @@ -56,14 +64,24 @@ def destroy(): ''' conn = openstack.connect(cloud_name='catalystcloud') - #Delete Networking Stuff - network = conn.network.find_network( - 'nichmr1-net' - ) + #Get the stuff to delete + network = conn.network.find_network('nichmr1-net') + router = conn.network.find_router('nichmr1-rtr') + subnet = conn.network.find_subnet('nichmr1-net') + #Destroy the router + conn.network.remove_interface_from_router(router, subnet.id) + conn.network.delete_router(router) + print('Router Destroyed') + + #Destroy the Subnets for subnet in network.subnet_ids: conn.network.delete_subnet(subnet, ignore_missing=False) + print('Subnets Destroyed') + + #Destroy the Network conn.network.delete_network(network, ignore_missing=False) + print('Network Destroyed') def status(): ''' Print a status report on the OpenStack From af872f604f985fde750766fc0d5dd68f4463e47f Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Sun, 18 Sep 2022 22:42:54 +0000 Subject: [PATCH 4/9] created floating ip --- assignment.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/assignment.py b/assignment.py index 5c1d556..7b36ba1 100644 --- a/assignment.py +++ b/assignment.py @@ -21,7 +21,7 @@ def create(): print('Network created') subnet = conn.network.create_subnet( - name='nichmr1-net', + name='nichmr1-subnet', network_id=network.id, ip_version='4', cidr='192.168.50.0/24', @@ -39,8 +39,13 @@ def create(): conn.network.add_interface_to_router(router, subnet.id) print('Router created') + #Create a floating IP address + floating_ip = conn.network.create_ip( + floating_network_id=public_network.id + ) + print('Floating IP created') #Create three servers @@ -63,20 +68,19 @@ def destroy(): produced by the create action ''' conn = openstack.connect(cloud_name='catalystcloud') - + #Get the stuff to delete network = conn.network.find_network('nichmr1-net') router = conn.network.find_router('nichmr1-rtr') - subnet = conn.network.find_subnet('nichmr1-net') + subnet = conn.network.find_subnet('nichmr1-subnet') - #Destroy the router + #Destroy the Router conn.network.remove_interface_from_router(router, subnet.id) conn.network.delete_router(router) print('Router Destroyed') - #Destroy the Subnets - for subnet in network.subnet_ids: - conn.network.delete_subnet(subnet, ignore_missing=False) + #Destroy the Subnet + conn.network.delete_subnet(subnet, ignore_missing=False) print('Subnets Destroyed') #Destroy the Network From b0068dac161cd7e7bbddbb8b97e513fbbf4e663a Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Mon, 19 Sep 2022 00:18:46 +0000 Subject: [PATCH 5/9] create and delete servers --- assignment.py | 74 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/assignment.py b/assignment.py index 7b36ba1..d02b3f8 100644 --- a/assignment.py +++ b/assignment.py @@ -3,9 +3,10 @@ IMAGE = 'ubuntu-minimal-22.04-x86_64' FLAVOUR = 'c1.c1r1' -NETWORK = '' +NETWORK = 'nichmr1-net' +SECURITY_GROUP = 'assignment2' KEYPAIR = 'nichmr1-key' - +SERVERS = ['nichmr1-web','nichmr1-app', 'nichmr1-db'] def create(): @@ -48,7 +49,30 @@ def create(): print('Floating IP created') #Create three servers - + image = conn.compute.find_image(IMAGE) + flavour = conn.compute.find_flavor(FLAVOUR) + keypair = conn.compute.find_keypair(KEYPAIR) + security_group = conn.network.find_security_group(SECURITY_GROUP) + + for server in SERVERS: + if conn.compute.find_server(name_or_id=server)is None: + new_server = conn.compute.create_server( + name = server, + image_id = image.id, + flavor_id = flavour.id, + networks=[{"uuid": network.id}], + key_name=keypair.name, + security_groups=[{"name":security_group.name}] + ) + new_server = conn.compute.wait_for_server(new_server) + print(server + " created") + + if server == 'nichmr1-web': + web_server = conn.compute.find_server('nichmr1-web') + conn.compute.add_floating_ip_to_server(web_server, floating_ip.floating_ip_address) + print("Assigned floating IP to web server") + else: + print("ERROR: " + server + " already exists!") def run(): @@ -74,18 +98,48 @@ def destroy(): router = conn.network.find_router('nichmr1-rtr') subnet = conn.network.find_subnet('nichmr1-subnet') + #Destroy the Servers + for server in SERVERS: + if conn.compute.find_server(name_or_id=server) is not None: + server_to_delete = conn.compute.find_server(name_or_id=server) + #Remove and Destroy Floating IP + if server == 'nichmr1-web': + conn.network.delete_ip(conn.network.find_ip(conn.compute.get_server(server_to_delete).addresses['nichmr1-net'][1]["addr"])) + #nichmr1_web = conn.compute.get_server(server_to_delete) + #nichmr1_web_floating_ip = nichmr1_web['addresses'][network][1]['addr'] + #conn.compute.remove_floating_ip_from_server(nichmr1_web, nichmr1_web_floating_ip) + #ip_to_delete = conn.network.find_ip(nichmr1_web_floating_ip) + #conn.network.delete_ip(ip_to_delete, ignore_missing=False) + conn.compute.delete_server(server_to_delete) + print(server + " Destroyed") + else: + print("ERROR: " + server + " does not exist. Continuing...") + + #Destroy the Router - conn.network.remove_interface_from_router(router, subnet.id) - conn.network.delete_router(router) - print('Router Destroyed') + if router is not None: + conn.network.remove_interface_from_router(router, subnet.id) + conn.network.delete_router(router) + print('Router Destroyed') + else: + print("ERROR: Router does not exist. Continuing...") #Destroy the Subnet - conn.network.delete_subnet(subnet, ignore_missing=False) - print('Subnets Destroyed') + if subnet is not None: + conn.network.delete_subnet(subnet, ignore_missing=False) + print('Subnets Destroyed') + else: + print("ERROR: Subnet does not exist. Continuing...") + #Destroy the Network - conn.network.delete_network(network, ignore_missing=False) - print('Network Destroyed') + if network is not None: + conn.network.delete_network(network, ignore_missing=False) + print('Network Destroyed') + else: + print("ERROR: Network does not exist. Continuing...") + + def status(): ''' Print a status report on the OpenStack From 9c4c803c981ef7f1eb5cdaa1265572f5c1e3c275 Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Wed, 21 Sep 2022 22:35:10 +0000 Subject: [PATCH 6/9] Add checking for if resources already exist when creating and destroying. Made some variables global constansts for ease of use --- .assignment.py.swo | 0 assignment.py | 86 +++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 .assignment.py.swo diff --git a/.assignment.py.swo b/.assignment.py.swo new file mode 100644 index 0000000..e69de29 diff --git a/assignment.py b/assignment.py index d02b3f8..2e14f0d 100644 --- a/assignment.py +++ b/assignment.py @@ -3,7 +3,10 @@ IMAGE = 'ubuntu-minimal-22.04-x86_64' FLAVOUR = 'c1.c1r1' +PUBLIC_NETWORK = 'public-net' NETWORK = 'nichmr1-net' +SUBNET = 'nichmr1-subnet' +ROUTER = 'nichmr1-rtr' SECURITY_GROUP = 'assignment2' KEYPAIR = 'nichmr1-key' SERVERS = ['nichmr1-web','nichmr1-app', 'nichmr1-db'] @@ -15,38 +18,39 @@ def create(): conn = openstack.connect(cloud_name='catalystcloud') #Create a network - network = conn.network.create_network( - name='nichmr1-net' - ) - - print('Network created') - - subnet = conn.network.create_subnet( - name='nichmr1-subnet', - network_id=network.id, - ip_version='4', - cidr='192.168.50.0/24', - gateway_ip='192.168.50.1' + if conn.network.find_network(NETWORK) is None: + network = conn.network.create_network( + name = NETWORK + ) + print('Network ' + NETWORK + ' created') + else: + network = conn.network.find_network(NETWORK) + print('Network ' + NETWORK + ' already exists! Continuing...') + + #Create a subnet + if conn.network.find_subnet(SUBNET) is None: + subnet = conn.network.create_subnet( + name = SUBNET, + network_id = network.id, + ip_version = '4', + cidr = '192.168.50.0/24', + gateway_ip = '192.168.50.1' ) - - print('Subnet created') + print('Subnet ' + SUBNET + ' created') + else: + print('Subnet ' + SUBNET + ' already exists! Continuing...') #Create a router - public_network = conn.network.find_network('public-net') - router = conn.network.create_router( - name='nichmr1-rtr', + public_network = conn.network.find_network(PUBLIC_NETWORK) + if conn.network.find_router(ROUTER) is None: + router = conn.network.create_router( + name = ROUTER, external_gateway_info={'network_id': public_network.id} ) - conn.network.add_interface_to_router(router, subnet.id) - - print('Router created') - - #Create a floating IP address - floating_ip = conn.network.create_ip( - floating_network_id=public_network.id - ) - - print('Floating IP created') + conn.network.add_interface_to_router(router, subnet.id) + print('Router ' + ROUTER + ' created') + else: + print('Router ' + ROUTER + ' already exists! Continuing...') #Create three servers image = conn.compute.find_image(IMAGE) @@ -66,13 +70,20 @@ def create(): ) new_server = conn.compute.wait_for_server(new_server) print(server + " created") - + + #The web server need a floating IP associated with it if server == 'nichmr1-web': + #Create a floating IP address + floating_ip = conn.network.create_ip( + floating_network_id=public_network.id + ) + print("Floating IP created") + #Associate the floating IP to the web server web_server = conn.compute.find_server('nichmr1-web') conn.compute.add_floating_ip_to_server(web_server, floating_ip.floating_ip_address) print("Assigned floating IP to web server") else: - print("ERROR: " + server + " already exists!") + print("Server " + server + " already exists! Continuing...") def run(): @@ -94,22 +105,19 @@ def destroy(): conn = openstack.connect(cloud_name='catalystcloud') #Get the stuff to delete - network = conn.network.find_network('nichmr1-net') - router = conn.network.find_router('nichmr1-rtr') - subnet = conn.network.find_subnet('nichmr1-subnet') + network = conn.network.find_network(NETWORK) + router = conn.network.find_router(ROUTER) + subnet = conn.network.find_subnet(SUBNET) #Destroy the Servers for server in SERVERS: if conn.compute.find_server(name_or_id=server) is not None: server_to_delete = conn.compute.find_server(name_or_id=server) - #Remove and Destroy Floating IP + #Destroy Floating IP if server == 'nichmr1-web': - conn.network.delete_ip(conn.network.find_ip(conn.compute.get_server(server_to_delete).addresses['nichmr1-net'][1]["addr"])) - #nichmr1_web = conn.compute.get_server(server_to_delete) - #nichmr1_web_floating_ip = nichmr1_web['addresses'][network][1]['addr'] - #conn.compute.remove_floating_ip_from_server(nichmr1_web, nichmr1_web_floating_ip) - #ip_to_delete = conn.network.find_ip(nichmr1_web_floating_ip) - #conn.network.delete_ip(ip_to_delete, ignore_missing=False) + conn.network.delete_ip( + conn.network.find_ip( + conn.compute.get_server(server_to_delete).addresses['nichmr1-net'][1]["addr"])) conn.compute.delete_server(server_to_delete) print(server + " Destroyed") else: From 951bcea9b9b88dd6a81632303ce8e719eb6586a1 Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Wed, 21 Sep 2022 23:18:35 +0000 Subject: [PATCH 7/9] Can run and stop servers, with status checking --- assignment.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/assignment.py b/assignment.py index 2e14f0d..c67bde0 100644 --- a/assignment.py +++ b/assignment.py @@ -90,13 +90,33 @@ def run(): ''' Start a set of Openstack virtual machines if they are not already running. ''' - pass + conn = openstack.connect(cloud_name='catalystcloud') + for server in SERVERS: + if conn.compute.find_server(name_or_id=server) is not None: + server_to_run = conn.compute.find_server(name_or_id=server) + if conn.compute.get_server(server_to_run).status == 'SHUTOFF': + conn.compute.start_server(server_to_run) + print('Server ' + server + ' had been started') + else: + print('Server ' + server + ' is already running! Continuing...') + else: + print('Server ' + server + ' does not exist! Continuing...') def stop(): ''' Stop a set of Openstack virtual machines if they are running. ''' - pass + conn = openstack.connect(cloud_name='catalystcloud') + for server in SERVERS: + if conn.compute.find_server(name_or_id=server) is not None: + server_to_run = conn.compute.find_server(name_or_id=server) + if conn.compute.get_server(server_to_run).status == 'ACTIVE': + conn.compute.stop_server(server_to_run) + print('Server ' + server + ' had been stopped') + else: + print('Server ' + server + ' is already stopped! Continuing...') + else: + print('Server ' + server + ' does not exist! Continuing...') def destroy(): ''' Tear down the set of Openstack resources From 1a01d8d34cc577c7747114e7f1aa6b08e8bf20a0 Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Thu, 22 Sep 2022 00:20:56 +0000 Subject: [PATCH 8/9] Add status checking --- assignment.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/assignment.py b/assignment.py index c67bde0..416fc71 100644 --- a/assignment.py +++ b/assignment.py @@ -173,7 +173,17 @@ def status(): ''' Print a status report on the OpenStack virtual machines created by the create action. ''' - pass + conn = openstack.connect(cloud_name='catalystcloud') + for server in SERVERS: + if conn.compute.find_server(name_or_id=server) is not None: + server_to_check = conn.compute.find_server(name_or_id=server) + ips = conn.compute.server_ips(server_to_check) + print("\nName: " + server + "\nStatus: " + conn.compute.get_server(server_to_check).status + + "\nIP address: ") + for ip in ips: + print(ip.address) + else: + print('Server ' + server + ' does not exist! Continuing...') ### You should not modify anything below this line ### From 95a67b94daf47d78d154442c235eaf198b18a164 Mon Sep 17 00:00:00 2001 From: "Mathew.Nichols" Date: Wed, 28 Sep 2022 21:31:02 +0000 Subject: [PATCH 9/9] Final typo changes and code tidy up --- assignment.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/assignment.py b/assignment.py index 416fc71..2802a3e 100644 --- a/assignment.py +++ b/assignment.py @@ -69,7 +69,7 @@ def create(): security_groups=[{"name":security_group.name}] ) new_server = conn.compute.wait_for_server(new_server) - print(server + " created") + print(server + ' created') #The web server need a floating IP associated with it if server == 'nichmr1-web': @@ -77,13 +77,13 @@ def create(): floating_ip = conn.network.create_ip( floating_network_id=public_network.id ) - print("Floating IP created") + print('Floating IP created') #Associate the floating IP to the web server web_server = conn.compute.find_server('nichmr1-web') conn.compute.add_floating_ip_to_server(web_server, floating_ip.floating_ip_address) - print("Assigned floating IP to web server") + print('Assigned floating IP to web server') else: - print("Server " + server + " already exists! Continuing...") + print('Server " + server + " already exists! Continuing...') def run(): @@ -139,9 +139,9 @@ def destroy(): conn.network.find_ip( conn.compute.get_server(server_to_delete).addresses['nichmr1-net'][1]["addr"])) conn.compute.delete_server(server_to_delete) - print(server + " Destroyed") + print(server + ' Destroyed') else: - print("ERROR: " + server + " does not exist. Continuing...") + print('Server ' + server + ' does not exist. Continuing...') #Destroy the Router @@ -150,14 +150,14 @@ def destroy(): conn.network.delete_router(router) print('Router Destroyed') else: - print("ERROR: Router does not exist. Continuing...") + print('Router does not exist. Continuing...') #Destroy the Subnet if subnet is not None: conn.network.delete_subnet(subnet, ignore_missing=False) print('Subnets Destroyed') else: - print("ERROR: Subnet does not exist. Continuing...") + print('Subnet does not exist. Continuing...') #Destroy the Network @@ -165,7 +165,7 @@ def destroy(): conn.network.delete_network(network, ignore_missing=False) print('Network Destroyed') else: - print("ERROR: Network does not exist. Continuing...") + print('Network does not exist. Continuing...')