def add_remove_ad_group(request, username=None, add_group=True): """ Add/Remove the groups from specified username :param request: :param username: :param add_group: :return: """ #get the list of group from html groups = request.POST.getlist('groups') try: ad.add_remove_ad_groups(db_service.get_local_ad(), username, groups, add_group) except Exception as e: return list_active_directory_group(request, username, error_message=e.message) user = db_service.get_user(username) fullname = user.full_name.split() subject = "SOL: add/remove in AD Group" add_remove = "assigned " if add_group else "unassigned " group_info = tabulate(["Group Name : ", groups]) message = "Hi " + fullname[0] + ", \n\tYour account's AD configuration has been modified, you are " + add_remove + \ " to below groups.\n" + group_info + "\nPlease contact Administrator in case of any issue.\n\n" sol_email.send_mail(receiver=user.email_id, subject=subject, message=message) return list_active_directory_group( request, username, message="Groups processed successfully.")
def delete_user(request, username=None): """ Delete user from AD as well as from SOL db :param request :param username wants ro delete :return render to list_users page with message or error_message """ # pass username with local ad details to delete user from AD try: ad.delete_user(db_service.get_local_ad(), username) except Exception as e: list_sol_users(request, error_message=e.message) #delete the user from SOL database user = db_service.delete_user(username) fullname = user.full_name.split() subject = 'Thank you for using ServiceOnline' message = "Hi " + fullname[0] + ",\n\tRecently your account has been deleted from ServiceOnline, now you " \ "will not be able to access ServiceOnline. Thank you for using our services. " \ "In case if you again want access of ServiceOnline, please get in touch with " \ "Administrator." sol_email.send_mail(receiver=user.email_id, subject=subject, message=message) return list_sol_users(request, message="User deleted successfully.")
def change_user_status(request, username=None): """ Change AD user's status i.e activate or deactivate Change the active flag form SOL db :param request :param username wants to create it in AD :return render to list_users page with message or error_message """ # pass username and local ad details to change the AD user's status try: ad.change_status(db_service.get_local_ad(), username) except Exception as e: list_sol_users(request, error_message="Unable to (de)active user due to : " + e.message) # change that user status from SOL database db_service.change_user_status(username) user = db_service.get_user(username) fullname = user.full_name.split() subject = 'SOL: Activation/Deactivation from SOL' user_status = "Successfully Activated" if user.active else "Successfully Deactivated" message = "Hi " + fullname[0] + ",\n\tYour account has been " + user_status + \ ", Please contact Administrator in case of any issue." sol_email.send_mail(receiver=user.email_id, subject=subject, message=message) return list_sol_users(request, message="User (de)activated successfully.")
def reminder_expiry(): logger.info('Running reminder expiry @ ' + str(datetime_obj.datetime.now())) try: instances = db_service.get_created_instances(all_created=True) if not instances: logger.info('Ending expiry reminder job as there are no instance') return for instance in instances: logger.info('Checking expiry details for : ' + instance.instance_name) expiry_date = datetime.strptime(str(instance.doe), '%Y-%m-%d') creation_date = datetime.strptime(str(instance.doc), '%Y-%m-%d') life_of_vm = abs(expiry_date - creation_date).days today_date = datetime.strptime(str(datetime_obj.date.today()), '%Y-%m-%d') remaining_days_to_expire = abs(expiry_date - today_date).days if remaining_days_to_expire == life_of_vm / 2 or remaining_days_to_expire <= 7: logger.debug("virtual machine '" + instance.instance_name + "' is going to expire in " + str(remaining_days_to_expire) + " days. Hence sending reminder email.") subject = 'SOL (reminder): Instance ' + instance.instance_name + ' expiry alert (Remaining Day : ' + \ str(remaining_days_to_expire) + ')' 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]]) user = instance.user.username vm_user = db_service.get_user(user.strip()) message = "Hi " + vm_user.full_name + ", \n\tYour virtual machine '" + instance.instance_name + \ "' is going to expire in " + str(remaining_days_to_expire) + \ " days.\nPlease check Virtual machine Details below,\n\n" + vm_information_table sol_email.send_mail(receiver=vm_user.email_id, subject=subject, message=message) logger.debug('Expiry alert sent successfully to ' + vm_user.email_id) except IOError as e: logger.error( 'Exception while executing Instance Expiry Reminder Job:' + e.message) logger.error(e)
def instance_action(request, instance_id, action): try: hypervisor = request.session[constants.SELECTED_HYPERVISOR_OBJ] hypervisor[constants.PROJECT_ID] = request.session[ constants.SELECTED_PROJECT]['id'] adapter = factory.get_adapter(hypervisor[constants.TYPE], hypervisor) if action == "start": adapter.start_instance(instance_id) return manage_instances( request, message="Start instance request submitted successfully.") elif action == "stop": adapter.stop_instance(instance_id) return manage_instances( request, message="Stop instance request submitted successfully.") elif action == "modify": # can be implemented in future. return elif action == "console": vnc_url = adapter.load_console(instance_id) return HttpResponseRedirect(vnc_url) elif action == "delete": adapter.delete_instance(instance_id) instance = db_service.get_created_instances( instance_id=instance_id) subject = 'SOL (deleted): Instance ' + instance.instance_name + ' has deleted.' vm_table = services.get_instance_table( instance.instance_name, instance.project.hypervisor.host, instance.project.name, instance.doc, instance.doe, "Deleted By", request.session[constants.USER][constants.USER_FIRST_NAME]) message = "Hi " + instance.user.full_name + ", \n\tYour virtual machine " + instance.instance_name + \ " has deleted. Please, check Virtual machine Details below,\n\n" + vm_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_id) return manage_instances( request, message="Delete instance request submitted successfully.") except Exception as e: return manage_instances(request, error_message=e.message)
def create_user(request): """ create user method is used to create a user in AD and in sol db. GET request will load the template and POST request will create a user. :param request: :return: """ # in case if it's GET request redirect to create user page. if request.method == constants.GET: return render(request, constants.CREATE_USER_TEMPLATE) try: # get user details from auth AD and pass username user_detail = ad.retrieve_user_details(db_service.get_auth_ad(), request.POST["id"]) user_detail[constants.USER_PASSWORD] = request.POST["new_password"] # pass the user's details taken from auth AD to create user in local AD ad.create_user(db_service.get_local_ad(), user_detail) except Exception as e: return list_sol_users(request, error_message=str(e)) # add that created user in SOL database db_service.create_user(user_detail) subject = " User(created): Welcome to ServiceOnline." users_information_table = tabulate( [["Name : ", user_detail[constants.USER_FULL_NAME]], ["EmailAddress : ", user_detail[constants.USER_EMAIL]], ["Username : "******"Password:"******"Hi " + fname + ", \n\tWelcome to ServiceOnline. \nRecently Administrator created your account on " \ "ServiceOnline. Please find the access details below, \n\n" + users_information_table + \ "\n\nIn case of any access related issue, please get in touch with Administrator." sol_email.send_mail(receiver=user_detail[constants.USER_EMAIL], subject=subject, message=message) return list_sol_users(request, message="User created successfully.")
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)
def create_instance(request, modify=False): if request.method == constants.GET: try: images, networks, flavors = get_image_flavor_network_details( request) request.session[constants.IMAGES] = images request.session[constants.NETWORKS] = networks request.session[constants.FLAVORS] = flavors except Exception as e: return render(request, constants.INSTANCES_TEMPLATE, {constants.ERROR_MESSAGE: e.message}) return render(request, constants.CREATE_INSTANCE_TEMPLATE, {'button_name': 'Request Server'}) instance_id = request.POST['request_id'] request_type = request.POST[ 'request_type'] if 'request_type' in request.POST else None if request_type == "approve" or modify: try: instance = get_instance(request, instance_id) hypervisor = request.session[constants.SELECTED_HYPERVISOR_OBJ] hypervisor[constants.PROJECT_ID] = request.session[ constants.SELECTED_PROJECT]['id'] adapter = factory.get_adapter(hypervisor[constants.TYPE], hypervisor) server_id = adapter.create_server(instance['name'], instance['image_id'], instance['flavor_id'], instance['network_id']) db_service.update_requested_instance(request_id=instance_id, instance_id=server_id) subject = 'SOL (created): Instance ' + instance[ 'name'] + ' has created.' vm_table = services.get_instance_table( instance['name'], instance['host'], instance['project'], instance['doc'], instance['doe'], 'Approved By', request.session[constants.USER][constants.USER_FIRST_NAME]) message = "Hi " + instance['user_f_name'] + ", \n\tYour virtual machine " + \ instance['name'] + " has created. Please, check Virtual machine Details below,\n\n" + \ vm_table + "\n\nFor any issue, please get in touch with Administrator." sol_email.send_mail(receiver=instance.user.email_id, subject=subject, message=message) return instance_request( request, load_instance=True, message="Requested instance created successfully.") except Exception as e: return instance_request(request, load_instance=True, error_message=e.message) elif request_type == "reject": instance = get_instance(request, instance_id) db_service.remove_instance(instance_id) subject = 'SOL (Rejected): Instance ' + instance[ 'name'] + ' has been rejected.' vm_table = services.get_instance_table( instance['name'], instance['host'], instance['project'], instance['doc'], instance['doe'], 'Rejected By', request.session[constants.USER][constants.USER_FIRST_NAME]) message = "Hi " + instance['user_f_name'] + ", \n\tYour virtual machine " + \ instance['name'] + " has been rejected. Please, check Virtual machine Details below,\n\n" + \ vm_table + "\n\nFor any issue, please get in touch with Administrator." sol_email.send_mail(receiver=instance.user.email_id, subject=subject, message=message) return instance_request(request, load_instance=True, message="Request rejected successfully.") else: instance = get_instance(request, instance_id) return render( request, constants.CREATE_INSTANCE_TEMPLATE, { 'instance': instance, 'modify': True, 'button_name': 'Modify & Approve' })