new-openstackcheck-mk2/main.py

124 lines
3.3 KiB
Python

import os, sys,time
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
from myOpenstackApp.logger import log
import myOpenstackApp
testProjectName="SmokeTest"
testProjectExists=False
testProject=""
testNetwork=""
testSubnet=""
timingText=""
cleanupResourcesOnCompletion=True
y=myOpenstackApp.OSC
y.initalise()
y.ks.getCatalog()
# print(y.something())
# print(y._keystone)
log.info("Starting")
tic = time.perf_counter()
allProjects=y.ks.getAllProjects()
toc = time.perf_counter()
timingText +=(f"Queried keystone in {toc - tic:0.4f} seconds\n")
def sendFailureNotice(e,timingText):
log.error("JOB FAILED\n\n" + str(e))
log.error(timingText)
exit()
def cleanUp():
log.info("Cleaning up")
log.info("Deleting project")
y.ks.deleteProject(testProject['id'])
#Delete VM
#Delete router
#Delete Network
#Delete project
pass
try:
#Does my test project ID already exist?
for _project in allProjects['projects']:
if _project['name']==testProjectName:
testProject=_project
testProjectExists=True
if testProjectExists:
log.error("Test project already exists with ID " + testProject['id'])
else:
# Create test project
log.info("{} project does not exist, creating".format(testProjectName))
testProject=y.ks.createProject(testProjectName,"Description goes here")['project']
log.info("Test project created with ID " + testProject['id'])
toc = time.perf_counter()
timingText +=(f"Queried projects in {toc - tic:0.4f} seconds\n")
except Exception as e:
log.error("Error managing project" + str(e))
cleanUp()
sendFailureNotice(e,timingText)
try:
#Does my test network exist?
allNetworks=y.neutron.getAllNetworks(testProject['id'])
log.info(allNetworks)
#Does my test network ID already exist?
for _network in allNetworks['networks']:
if _network['name']==testProjectName+"_network":
testNetwork=_network
if testNetwork != "":
log.info("Test network already exists with ID " + testNetwork['id'])
else:
# Create test network
log.info("{} network does not exist, creating".format(testProjectName))
testNetwork=y.neutron.createNetwork(testProjectName+"_network",testProject['id'],"Test network description")['network']
log.info("Test network created with ID " + testNetwork['id'])
#Create subnet
log.info("Creating new subnet")
testSubnet=y.neutron.createSubnet(testProjectName + "_subnet",testNetwork['id'])
log.debug(testSubnet)
#Create a test router
log.info("Creating new router")
testRouter=y.neutron.createRouter(testProjectName + "_router",os.getenv("EXTERNAL_NETWORK_ID"))
log.debug(testRouter)
toc = time.perf_counter()
timingText +=(f"Queried neutron in {toc - tic:0.4f} seconds")
except Exception as e:
log.error("Error managing network" + str(e))
toc = time.perf_counter()
timingText +=(f"Neutron failed after {toc - tic:0.4f} seconds\n")
cleanUp()
sendFailureNotice(e,timingText)
#Create
#Project
#Network
#Subnet
#Router
#bind router to network
#Volume from image
#Vm from volume
if cleanupResourcesOnCompletion:
cleanUp()
toc = time.perf_counter()
log.info(f"Total runtime {toc - tic:0.4f} seconds")