def upload(): openstack = request.form region = openstack['region'] session = get_valid_session(openstack) nova = NovaClient(session=session.session, version=2, region=region) glance = GlanceClient(session=session.session, version=2, region=region) heat = HeatClient(version=1, endpoint=session.get_endpoint("heat", region), token=session.token) image_list = [] if nova.get_status() and glance.get_status() and heat.get_status(): LOG.info("Connection to all services are established") image_file = request.files['file'] if image_file and allowed_file(image_file.filename): filename = add_uuid(secure_filename(image_file.filename)) if not os.path.exists(temp_location): os.mkdir(temp_location) imagepath = os.path.join(temp_location, filename) LOG.info("Saving file to: " + imagepath + " ...") image_file.save(imagepath) LOG.info("Upload OK, saved file " + imagepath) else: raise Exception("Extension is not allowed") LOG.info("Extracting ova file to " + temp_location + "file ...") ovf_file = extract_file(filename, temp_location) folder = temp_location+ovf_file.replace(".ovf", "") try: LOG.info("Parsing virtual information data from ovf file ...") parsed_file = transform_parsed_vms(folder + '/'+image_file.filename.replace("ova", "ovf")) valid_vms = [] space_available, message = nova.check_quota(parsed_file["vms"]) if space_available: LOG.info(message) for vm in parsed_file["vms"]: flavor = nova.get_best_flavor([int(vm.cpu), int(vm.ram), from_bytes_to_gb(vm.disk)]) image_id = glance.upload_image(vm.image, folder + '/' + vm.image) point = GeneratedVM(name=vm.name, flavor=flavor, image=image_id, networks=vm.network["internal_network"], sec_groups=vm.network["NAT"]) image_list.append(image_id) valid_vms.append(point) LOG.info("Generating template file ...") heat.create_stack(name=openstack['stack_name'], body=yaml.dump(generate_template(valid_vms))) else: raise Exception(message) except: glance.remove_list_of_images(image_list) clean(folder) raise Exception("Couldn't create heat template") clean(folder) return str({"Ok"}) else: LOG.error("Couldn't establish connection with all services")
def status(): openstack = request.form region = openstack['region'] if request.method == 'POST': credentials_status = False import_status = False export_status = False session = get_valid_session(openstack) LOG.info("Getting nova client connection") nova = NovaClient(session=session.session, version=2, region=region) LOG.info("Getting glance client connection") glance = GlanceClient(session=session.session, version=2, region=region) LOG.info("Getting heat client connection") heat = HeatClient(version=1, endpoint=session.get_endpoint("heat", region), token=session.token) if nova.get_status() and glance.get_status() and heat.get_status(): credentials_status = True if credentials_status and openstack['method'] == 'import': import_status = True if credentials_status and openstack['method'] == 'export': export_status = True response = {"credentials_status": credentials_status, "import_status": import_status, "export_status": export_status} return json.dumps(response)
args.region, args.endpoint) if keystone is None: print 'CRITICAL: Could not create keystone context' sys.exit(STATE_CRITICAL) if keystone.valid() is False: print 'CRITICAL: Keystone context is invalid' sys.exit(STATE_CRITICAL) nova_url = None if args.nova_url is not None: nova_url = args.nova_url nova = NovaClient(keystone, nova_url) if nova is None: print 'CRITICAL: Could not create nova context' sys.exit(STATE_CRITICAL) flavors = nova.get_flavors() if flavors is None: print 'CRITICAL: Did not get any flavors data' sys.exit(STATE_CRITICAL) if 'flavors' in flavors: count = len(flavors['flavors']) if count > 0:
args.region, args.endpoint) if keystone is None: print 'CRITICAL: Could not create keystone context' sys.exit(STATE_CRITICAL) if keystone.valid() is False: print 'CRITICAL: Keystone context is invalid' sys.exit(STATE_CRITICAL) nova_url = None if args.nova_url is not None: nova_url = args.nova_url nova = NovaClient(keystone, nova_url) if nova is None: print 'CRITICAL: Could not create nova context' sys.exit(STATE_CRITICAL) service = nova.get_service(args.host, args.binary) if service is None: print ('CRITICAL: Could not retrieve status ' 'for %s on %s') % (args.binary, args.host) sys.exit(STATE_CRITICAL) if service['status'] == 'enabled': status = 'CRITICAL' status_code = STATE_CRITICAL