def load_report_data(): logger.debug("Getting report data from all hypervisors.") hypervisors = load_hypervisors_with_soluser() timestamp = datetime.utcnow() for hypervisor in hypervisors: adapter = factory.get_adapter(hypervisor[constants.TYPE], hypervisor) adapter.generate_admin_auth() load_hypervisors_stats(adapter, timestamp) load_tenant_wise_report(adapter, timestamp)
def create_hypervisor(request): if request.method == constants.GET: return render(request, constants.CREATE_HYPERVISOR_TEMPLATE) hypervisor_type = request.POST['hypervisor_type'] protocol = request.POST['protocol'] host = request.POST['host'] port = request.POST['port'] if hypervisor_type == "openstack": domain = request.POST['domain'] username = request.POST['username'] password = request.POST['password'] if hypervisor_type == "openstack": adapter = factory.get_adapter( hypervisor_type, { constants.PROTOCOL: protocol, constants.HOST: host, constants.PORT: port, constants.DOMAIN: domain, 'username': username, 'password': password }) user_detail = adapter.create_sol_user() if constants.ERROR_MESSAGE in user_detail: return hypervisor_management( request, error_message=user_detail[constants.ERROR_MESSAGE]) hypervisor = db_service.create_hypervisor(hypervisor_type, protocol, host, port) user = db_service.get_user(constants.HYPERVISOR_SOLUSER_NAME) if not user: user = db_service.create_user({ constants.USERNAME: constants.HYPERVISOR_SOLUSER_NAME, constants.USER_EMAIL: constants.HYPERVISOR_SOLUSER_EMAIL, constants.USER_FULL_NAME: constants.HYPERVISOR_SOLUSER_NAME }) db_service.save_user_credentials(user.username, hypervisor.host, domain, constants.HYPERVISOR_SOLUSER_NAME, user_detail['user_password']) db_service.update_hypervisor_user_id(user.username, hypervisor.host, user_detail['user_id']) elif hypervisor_type == "vCenter": hypervisor = db_service.create_hypervisor(hypervisor_type, protocol, host, port) return hypervisor_management(request, message='Hypervisor added successfully.')
def hypervisor_user_management(request, username=None, message=None, error_message=None): if request.method == constants.POST: username = request.POST[constants.USERNAME] host = request.POST[constants.HOST] hypervisor = db_service.get_hypervisor(host) domain, sol_username, password = db_service.get_user_creds( host, constants.HYPERVISOR_SOLUSER_NAME) user = db_service.get_user(username) try: adapter = factory.get_adapter( hypervisor.type, { constants.PROTOCOL: hypervisor.protocol, constants.HOST: hypervisor.host, constants.PORT: hypervisor.port, constants.DOMAIN: domain, constants.USERNAME: sol_username, constants.PASSWORD: password }) adapter.generate_admin_auth() if 'user_id' in request.POST: user_id = request.POST['user_id'] adapter.delete_user(user_id) user_id = None message = "User deleted successfully." else: user_id = adapter.create_user(user.username, request.POST['password'], user.email_id, user.full_name) message = "User created successfully." db_service.update_hypervisor_user_id(username, host, user_id) except Exception as e: error_message = e.message return render( request, constants.HYPERVISOR_USER_MGMT_TEMPLATE, { 'mappings': db_service.get_user_hypervisor_mapping(username), constants.USERNAME: username, constants.MESSAGE: message, constants.ERROR_MESSAGE: error_message })
def hypervisor_management(request, message=None, error_message=None): if 'hypervisor_id' in request.POST: hypervisor_id = request.POST['hypervisor_id'] db_hypervisor = db_service.get_hypervisor(id=hypervisor_id) hypervisor_type = db_hypervisor.type if hypervisor_type == "openstack": hypervisor = { constants.TYPE: db_hypervisor.type, constants.PROTOCOL: db_hypervisor.protocol, constants.HOST: db_hypervisor.host, constants.PORT: db_hypervisor.port, constants.DOMAIN: request.POST[constants.DOMAIN], constants.USERNAME: request.POST[constants.USERNAME], constants.PASSWORD: request.POST[constants.PASSWORD] } else: hypervisor = { constants.TYPE: db_hypervisor.type, constants.PROTOCOL: db_hypervisor.protocol, constants.HOST: db_hypervisor.host, constants.PORT: db_hypervisor.port } if hypervisor[constants.TYPE] == "openstack": sol_user_id = db_service.get_sol_user_id( hypervisor_id=hypervisor_id) try: if hypervisor[constants.TYPE] == "openstack": adapter = factory.get_adapter(hypervisor[constants.TYPE], hypervisor) adapter.generate_admin_auth() adapter.delete_user(sol_user_id) db_service.delete_hypervisor(hypervisor_id) message = "Hypervisor removed successfully." except Exception as e: error_message = e.message return render( request, constants.HYPERVISORS_TEMPLATE, { 'hypervisors': db_service.load_hypervisors(), constants.MESSAGE: message, constants.ERROR_MESSAGE: error_message })
def load_projects(request, host=None): if request.method == constants.GET: request.session[constants.SELECTED_HYPERVISOR] = host return render(request, constants.HYPERVISOR_ADMIN_LOGIN_TEMPLATE) hypervisor = db_service.get_hypervisor( request.session[constants.SELECTED_HYPERVISOR]) try: adapter = factory.get_adapter( hypervisor.type, { constants.PROTOCOL: hypervisor.protocol, constants.HOST: hypervisor.host, constants.PORT: hypervisor.port, constants.DOMAIN: request.POST[constants.DOMAIN], constants.USERNAME: request.POST[constants.USERNAME], constants.PASSWORD: request.POST[constants.PASSWORD] }) token, _, _ = adapter.generate_admin_auth() request.session[constants.DOMAIN] = request.POST[constants.DOMAIN] request.session[constants.SELECTED_HYPERVISOR_OBJ] = { constants.PROTOCOL: hypervisor.protocol, constants.HOST: hypervisor.host, constants.PORT: hypervisor.port, constants.DOMAIN: request.POST[constants.DOMAIN], constants.TOKEN: token, constants.TYPE: hypervisor.type, constants.USERNAME: request.POST[constants.USERNAME], constants.PASSWORD: request.POST[constants.PASSWORD] } result = adapter.get_all_projects() request.session[constants.PROJECTS] = result return hypervisor_management(request) except SOLException as se: return hypervisor_management(request, error_message=se.get_message()) except Exception as e: return hypervisor_management(request, error_message=e.message)
def auto_vm_deletion(): logger.info('Running delete expired instace @ ' + str(datetime_obj.datetime.now())) try: instances = db_service.get_created_instances(all_created=True) if not instances: logger.info("No instances found. Exiting vm deletion job.") return for instance in instances: logger.info('Checking expiry details for : ' + instance.instance_name) expiry_date = datetime.strptime(str(instance.doe), '%Y-%m-%d') today_date = datetime.strptime(str(datetime_obj.date.today()), '%Y-%m-%d') if expiry_date <= today_date: domain, username, password = db_service.get_user_creds( instance.project.hypervisor.host, constants.HYPERVISOR_SOLUSER_NAME) if not domain: logger.error("Unable to delete instance : " + instance.instance_name + "as SOLUSER for hypervisor : " + instance.project.hypervisor.host + " Does not exist.") continue hypervisor = { constants.TYPE: instance.project.hypervisor.type, constants.PROTOCOL: instance.project.hypervisor.protocol, constants.HOST: instance.project.hypervisor.host, constants.PORT: instance.project.hypervisor.port, constants.DOMAIN: domain, constants.USERNAME: username, constants.PASSWORD: password, constants.PROJECT_ID: instance.project.project_id } logger.debug( "virtual machine '" + instance.instance_name + "' has expired. Hence, deleting the virtual machine.") adapter = factory.get_adapter(hypervisor[constants.TYPE], hypervisor) adapter.generate_admin_auth() user_roles = adapter.get_user_roles_project( hypervisor[constants.PROJECT_ID]) for user in user_roles: if user[constants. USERNAME] == constants.HYPERVISOR_SOLUSER_NAME: break else: sol_user_id = db_service.get_sol_user_id( hypervisor[constants.HOST]) role_ids = [] roles = adapter.get_roles() for role in roles: role_ids.append(role[constants.ROLE_ID]) adapter.assign_roles(role_ids, sol_user_id, hypervisor[constants.PROJECT_ID]) adapter.delete_instance(instance.instance_id) subject = 'SOL (deleted): Instance ' + instance.instance_name + ' has deleted.' vm_information_table = tabulate( [["Instance Name : ", instance.instance_name], ["Hypervisor : ", instance.project.hypervisor.host], ["Project : ", instance.project.name], ["Date of Creation:", instance.doc], ["Expiry Date:", instance.doe], ["Deleted by:", "Service Online"]]) message = "Hi " + instance.user.full_name + ", \n\tYour virtual machine " + \ instance.instance_name + " has deleted. Please, check Virtual " \ "machine Details below,\n\n" + vm_information_table + \ "\n\nFor any issue, please get in touch with Administrator." sol_email.send_mail(receiver=instance.user.email_id, subject=subject, message=message) db_service.remove_instance(instance_id=instance.instance_id) logger.debug('Instance ' + instance.instance_name + ' has deleted') except Exception as e: logger.error( 'Exception while executing deleting expired instances job: ' + e.message) logger.error(e)