def get_session_log(id): try: if not can_install(g.api_user): return failed_response('Not Authorized', return_code=HTTP_NOT_AUTHORIZED) return api_install.api_get_session_log(id) except Exception as e: return failed_response(e.message)
def host_schedule_install(hostname): """ Individual host schedule install """ if not can_install(current_user): abort(401) return handle_schedule_install_form(request=request, db_session=DBSession(), hostname=hostname)
def create_install_job(): try: if request.method == 'POST': if not can_install(g.api_user): return failed_response('Not Authorized', return_code=HTTP_NOT_AUTHORIZED) return api_install.api_create_install_request(request) elif request.method == 'GET': return api_install.api_get_install_request(request) except Exception as e: return failed_response(e.message)
def home(): if not can_install(current_user): abort(401) absolute_path = os.path.abspath('.') csm_repository_path = absolute_path.replace( NAME_CSM + '/' + NAME_CSMSERVER, NAME_CSM_DATA + '/' + NAME_REPOSITORY) return render_template('host/download_dashboard.html', csm_repository_path=csm_repository_path, system_option=SystemOption.get(DBSession()))
def api_resubmit_download_jobs(): if not can_install(current_user): abort(401) user_id = request.args.get("user_id") server_id = request.args.get("server_id") server_directory = request.args.get("server_directory") db_session = DBSession() download_jobs = db_session.query(DownloadJob).filter(and_(DownloadJob.user_id == user_id, DownloadJob.server_id == server_id, DownloadJob.server_directory == server_directory)) for download_job in download_jobs: download_job.status = None download_job.status_time = None db_session.commit() return jsonify({'status': 'OK'})
def api_resubmit_download_jobs(): if not can_install(current_user): abort(401) user_id = request.args.get("user_id") server_id = request.args.get("server_id") server_directory = request.args.get("server_directory") db_session = DBSession() download_jobs = db_session.query(DownloadJob).filter( and_(DownloadJob.user_id == user_id, DownloadJob.server_id == server_id, DownloadJob.server_directory == server_directory)) for download_job in download_jobs: download_job.status = None download_job.status_time = None db_session.commit() return jsonify({'status': 'OK'})
def resubmit_download_job(id): if not can_install(current_user): abort(401) db_session = DBSession() download_job = db_session.query(DownloadJob).filter( DownloadJob.id == id).first() if download_job is None: abort(404) try: # Download jobs that are in progress cannot be deleted. download_job.status = None download_job.status_time = None db_session.commit() return jsonify({'status': 'OK'}) except: logger.exception('resubmit_download_job() hit exception') return jsonify({'status': 'Failed: check system logs for details'})
def home(): if not can_install(current_user): abort(401) msg = '' # Software Profile import if request.method == 'POST': file = request.files['file'] if file: if not allowed_file(file.filename): msg = "Incorrect file format -- " + file.filename + " must be .json" else: file_path = os.path.join(get_temp_directory(), "software_profiles.json") file.save(file_path) failed = "" with open(file_path, 'r') as f: try: s = json.load(f) except: msg = "Incorrect file format -- " + file.filename + " must be a valid JSON file." flash(msg, 'import_feedback') return redirect(url_for(".home")) if "CSM Server:Software Profile" not in s.keys(): msg = file.filename + " is not in the correct Software Profile format." else: db_session = DBSession profiles = [p for (p,) in DBSession().query(SoftwareProfile.name).all()] d = s["CSM Server:Software Profile"] for software_profile_name in d.keys(): name = '' if software_profile_name in profiles: name = software_profile_name # Will keep appending ' - copy' until it hits a unique name while name in profiles: name += " - copy" msg += software_profile_name + ' -> ' + name + '\n' profiles.append(name) if len(name) < 100 and len(software_profile_name) < 100: try: profile = SoftwareProfile( name=name if name else software_profile_name, packages=d[software_profile_name], created_by=current_user.username ) db_session.add(profile) db_session.commit() except: failed += software_profile_name + '\n' else: failed += software_profile_name + ' (name too long)\n' if msg: msg = "The following profiles already exist and will try to be imported under modified " \ "names:\n\n" + msg + '\n' if failed: msg += 'The following profiles failed to import:\n\n' + failed elif failed: msg = 'The following profiles failed to import:\n\n' + failed else: msg = "Software Profile import was successful!" # delete file os.remove(file_path) flash(msg, 'import_feedback') return redirect(url_for(".home")) db_session = DBSession() conformance_form = ConformanceForm(request.form) assign_software_profile_to_hosts_form = AssignSoftwareProfileToHostsForm(request.form) view_host_software_profile_form = ViewHostSoftwareProfileForm(request.form) conformance_report_dialog_form = ConformanceReportDialogForm(request.form) make_conform_dialog_form = MakeConformDialogForm(request.form) fill_custom_command_profiles(db_session, make_conform_dialog_form.custom_command_profile.choices) select_server_form = SelectServerForm(request.form) export_conformance_report_form = ExportConformanceReportForm(request.form) export_conformance_report_form.include_host_packages.data = True export_conformance_report_form.exclude_conforming_hosts.data = True return render_template('conformance/index.html', conformance_form=conformance_form, assign_software_profile_to_hosts_form=assign_software_profile_to_hosts_form, view_host_software_profile_form=view_host_software_profile_form, conformance_report_dialog_form=conformance_report_dialog_form, install_actions=[InstallAction.PRE_UPGRADE, InstallAction.INSTALL_ADD, InstallAction.INSTALL_ACTIVATE, InstallAction.POST_UPGRADE, InstallAction.INSTALL_COMMIT, InstallAction.ALL], make_conform_dialog_form=make_conform_dialog_form, export_conformance_report_form=export_conformance_report_form, select_server_form=select_server_form, server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(DBSession()))
def batch_schedule_install(): """ For batch scheduled installation. """ if not can_install(current_user): abort(401) db_session = DBSession() form = ScheduleInstallForm(request.form) # Fills the selections fill_regions(db_session, form.region.choices) fill_dependencies(form.dependency.choices) fill_custom_command_profiles(db_session, form.custom_command_profile.choices) return_url = get_return_url(request, 'home') if request.method == 'POST': # and form.validate(): """ f = request.form for key in f.keys(): for value in f.getlist(key): print(key,":",value) """ # Retrieves from the multi-select box hostnames = form.hidden_selected_hosts.data.split(',') install_action = form.install_action.data custom_command_profile_ids = [str(i) for i in form.custom_command_profile.data] if hostnames is not None: for hostname in hostnames: host = get_host(db_session, hostname) if host is not None: db_session = DBSession() scheduled_time = form.scheduled_time_UTC.data software_packages = form.software_packages.data.split() server_id = form.hidden_server.data server_directory = form.hidden_server_directory.data pending_downloads = form.hidden_pending_downloads.data.split() # If only one install_action, accept the selected dependency if any dependency = 0 if len(install_action) == 1: # No dependency when it is 0 (a digit) if not form.dependency.data.isdigit(): prerequisite_install_job = get_last_install_action(db_session, form.dependency.data, host.id) if prerequisite_install_job is not None: dependency = prerequisite_install_job.id create_or_update_install_job(db_session=db_session, host_id=host.id, install_action=install_action[0], custom_command_profile_ids=custom_command_profile_ids, scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, dependency=dependency, created_by=current_user.username) else: # The dependency on each install action is already indicated in the implicit ordering # in the selector. If the user selected Pre-Upgrade and Install Add, Install Add (successor) # will have Pre-Upgrade (predecessor) as the dependency. dependency = 0 for one_install_action in install_action: new_install_job = create_or_update_install_job(db_session=db_session, host_id=host.id, install_action=one_install_action, scheduled_time=scheduled_time, custom_command_profile_ids=custom_command_profile_ids, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, dependency=dependency, created_by=current_user.username) dependency = new_install_job.id return redirect(url_for(return_url)) else: # Initialize the hidden fields form.hidden_server.data = -1 form.hidden_server_name.data = '' form.hidden_server_directory.data = '' form.hidden_pending_downloads.data = '' return render_template('schedule_install.html', form=form, system_option=SystemOption.get(db_session), server_time=datetime.datetime.utcnow(), return_url=return_url)
def batch_schedule_install(): """ For batch scheduled installation. """ if not can_install(current_user): abort(401) db_session = DBSession() form = ScheduleInstallForm(request.form) # Fills the selections fill_regions(db_session, form.region.choices) fill_dependencies(form.dependency.choices) fill_custom_command_profiles(db_session, form.custom_command_profile.choices) return_url = get_return_url(request, 'home') if request.method == 'POST': # and form.validate(): """ f = request.form for key in f.keys(): for value in f.getlist(key): print(key,":",value) """ # Retrieves from the multi-select box hostnames = form.hidden_selected_hosts.data.split(',') install_action = form.install_action.data custom_command_profile_ids = [ str(i) for i in form.custom_command_profile.data ] if hostnames is not None: for hostname in hostnames: host = get_host(db_session, hostname) if host is not None: db_session = DBSession() scheduled_time = form.scheduled_time_UTC.data software_packages = form.software_packages.data.split() server_id = form.hidden_server.data server_directory = form.hidden_server_directory.data pending_downloads = form.hidden_pending_downloads.data.split( ) # If only one install_action, accept the selected dependency if any dependency = 0 if len(install_action) == 1: # No dependency when it is 0 (a digit) if not form.dependency.data.isdigit(): prerequisite_install_job = get_last_install_action( db_session, form.dependency.data, host.id) if prerequisite_install_job is not None: dependency = prerequisite_install_job.id create_or_update_install_job( db_session=db_session, host_id=host.id, install_action=install_action[0], custom_command_profile_ids= custom_command_profile_ids, scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, dependency=dependency, created_by=current_user.username) else: # The dependency on each install action is already indicated in the implicit ordering # in the selector. If the user selected Pre-Upgrade and Install Add, Install Add (successor) # will have Pre-Upgrade (predecessor) as the dependency. dependency = 0 for one_install_action in install_action: new_install_job = create_or_update_install_job( db_session=db_session, host_id=host.id, install_action=one_install_action, scheduled_time=scheduled_time, custom_command_profile_ids= custom_command_profile_ids, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, dependency=dependency, created_by=current_user.username) dependency = new_install_job.id return redirect(url_for(return_url)) else: # Initialize the hidden fields form.hidden_server.data = -1 form.hidden_server_name.data = '' form.hidden_server_directory.data = '' form.hidden_pending_downloads.data = '' return render_template('schedule_install.html', form=form, system_option=SystemOption.get(db_session), server_time=datetime.datetime.utcnow(), return_url=return_url)
def home(): if not can_install(current_user): abort(401) msg = '' # Software Profile import if request.method == 'POST': file = request.files['file'] if file: if not allowed_file(file.filename): msg = "Incorrect file format -- " + file.filename + " must be .json" else: file_path = os.path.join(get_temp_directory(), "software_profiles.json") file.save(file_path) failed = "" with open(file_path, 'r') as f: try: s = json.load(f) except: msg = "Incorrect file format -- " + file.filename + " must be a valid JSON file." flash(msg, 'import_feedback') return redirect(url_for(".home")) if "CSM Server:Software Profile" not in s.keys(): msg = file.filename + " is not in the correct Software Profile format." else: db_session = DBSession profiles = [ p for (p, ) in DBSession().query( SoftwareProfile.name).all() ] d = s["CSM Server:Software Profile"] for software_profile_name in d.keys(): name = '' if software_profile_name in profiles: name = software_profile_name # Will keep appending ' - copy' until it hits a unique name while name in profiles: name += " - copy" msg += software_profile_name + ' -> ' + name + '\n' profiles.append(name) if len(name) < 100 and len( software_profile_name) < 100: try: profile = SoftwareProfile( name=name if name else software_profile_name, packages=d[software_profile_name], created_by=current_user.username) db_session.add(profile) db_session.commit() except: failed += software_profile_name + '\n' else: failed += software_profile_name + ' (name too long)\n' if msg: msg = "The following profiles already exist and will try to be imported under modified " \ "names:\n\n" + msg + '\n' if failed: msg += 'The following profiles failed to import:\n\n' + failed elif failed: msg = 'The following profiles failed to import:\n\n' + failed else: msg = "Software Profile import was successful!" # delete file os.remove(file_path) flash(msg, 'import_feedback') return redirect(url_for(".home")) db_session = DBSession() conformance_form = ConformanceForm(request.form) assign_software_profile_to_hosts_form = AssignSoftwareProfileToHostsForm( request.form) view_host_software_profile_form = ViewHostSoftwareProfileForm(request.form) conformance_report_dialog_form = ConformanceReportDialogForm(request.form) make_conform_dialog_form = MakeConformDialogForm(request.form) fill_custom_command_profiles( db_session, make_conform_dialog_form.custom_command_profile.choices) select_server_form = SelectServerForm(request.form) export_conformance_report_form = ExportConformanceReportForm(request.form) export_conformance_report_form.include_host_packages.data = True export_conformance_report_form.exclude_conforming_hosts.data = True return render_template( 'conformance/index.html', conformance_form=conformance_form, assign_software_profile_to_hosts_form= assign_software_profile_to_hosts_form, view_host_software_profile_form=view_host_software_profile_form, conformance_report_dialog_form=conformance_report_dialog_form, install_actions=[ InstallAction.PRE_UPGRADE, InstallAction.INSTALL_ADD, InstallAction.INSTALL_ACTIVATE, InstallAction.POST_UPGRADE, InstallAction.INSTALL_COMMIT, InstallAction.ALL ], make_conform_dialog_form=make_conform_dialog_form, export_conformance_report_form=export_conformance_report_form, select_server_form=select_server_form, server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(DBSession()))
def migration(): # only operator and above can schedule migration if not can_install(current_user): render_template('user/not_authorized.html', user=current_user) # temp_user_dir = create_temp_user_directory(current_user.username) # config_file_path = os.path.normpath(os.path.join(temp_user_dir, "config_conversion")) # create_directory(config_file_path) # current_app.config['UPLOAD_FOLDER'] = config_file_path # print "current_app.config.get('UPLOAD_FOLDER') = " + current_app.config.get('UPLOAD_FOLDER') db_session = DBSession() return_url = get_return_url(request, 'install_dashboard.home') schedule_form = init_schedule_form(db_session, request, get=request.method == 'GET') config_form = init_config_form(db_session, request, get=request.method == 'GET') if request.method == 'POST': print(str(config_form.data)) if config_form.hidden_submit_config_form.data == "True": return convert_config(db_session, request, 'asr9k_64_migrate/migration.html', schedule_form) # Retrieves from the multi-select box hostnames = schedule_form.hidden_hosts.data.split(',') install_action = schedule_form.install_action.data if hostnames is not None: print(str(schedule_form.data)) print(str(hostnames)) dependency_list = schedule_form.hidden_dependency.data.split(',') index = 0 for hostname in hostnames: host = get_host(db_session, hostname) if host is not None: db_session = DBSession() scheduled_time = schedule_form.scheduled_time_UTC.data software_packages = schedule_form.hidden_software_packages.data.split() server_id = schedule_form.hidden_server.data server_directory = schedule_form.hidden_server_directory.data custom_command_profile_ids = [str(i) for i in schedule_form.custom_command_profile.data] if InstallAction.MIGRATION_AUDIT in install_action: host.context[0].data['hardware_audit_version'] = \ schedule_form.hidden_hardware_audit_version.data if InstallAction.PRE_MIGRATE in install_action: host.context[0].data['config_filename'] = schedule_form.hidden_config_filename.data host.context[0].data['override_hw_req'] = schedule_form.hidden_override_hw_req.data # If the dependency is a previous job id, it's non-negative int string. if int(dependency_list[index]) >= 0: dependency = dependency_list[index] # In this case, the dependency is '-1', which means no dependency for the first install action else: dependency = 0 # The dependency for the first install action depends on dependency_list[index]. # The dependency for each install action following first is indicated by the implicit # ordering in the selector. If the user selected Pre-Migrate and Migrate, # Migrate (successor) will have Pre-Migrate (predecessor) as the dependency. for i in xrange(0, len(install_action)): new_install_job = create_or_update_install_job(db_session=db_session, host_id=host.id, install_action=install_action[i], scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, custom_command_profile_ids=custom_command_profile_ids, dependency=dependency) print("dependency for install_action = {} is {}".format(install_action[i], str(dependency))) dependency = new_install_job.id index += 1 return redirect(url_for(return_url)) else: return render_template('asr9k_64_migrate/migration.html', schedule_form=schedule_form, install_action=get_install_migrations_dict(), server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(db_session), config_form=config_form, input_filename="", err_msg="", )
def migration(): # only operator and above can schedule migration if not can_install(current_user): render_template('user/not_authorized.html', user=current_user) # temp_user_dir = create_temp_user_directory(current_user.username) # config_file_path = os.path.normpath(os.path.join(temp_user_dir, "config_conversion")) # create_directory(config_file_path) # current_app.config['UPLOAD_FOLDER'] = config_file_path # print "current_app.config.get('UPLOAD_FOLDER') = " + current_app.config.get('UPLOAD_FOLDER') db_session = DBSession() return_url = get_return_url(request, 'install_dashboard.home') schedule_form = init_schedule_form(db_session, request, get=request.method == 'GET') config_form = init_config_form(db_session, request, get=request.method == 'GET') if request.method == 'POST': print(str(config_form.data)) if config_form.hidden_submit_config_form.data == "True": return convert_config(db_session, request, 'asr9k_64_migrate/migration.html', schedule_form) # Retrieves from the multi-select box hostnames = schedule_form.hidden_hosts.data.split(',') install_action = schedule_form.install_action.data if hostnames is not None: print(str(schedule_form.data)) print(str(hostnames)) dependency_list = schedule_form.hidden_dependency.data.split(',') index = 0 for hostname in hostnames: host = get_host(db_session, hostname) if host is not None: db_session = DBSession() scheduled_time = schedule_form.scheduled_time_UTC.data software_packages = schedule_form.hidden_software_packages.data.split( ) server_id = schedule_form.hidden_server.data server_directory = schedule_form.hidden_server_directory.data custom_command_profile_ids = [ str(i) for i in schedule_form.custom_command_profile.data ] if InstallAction.MIGRATION_AUDIT in install_action: host.context[0].data['hardware_audit_version'] = \ schedule_form.hidden_hardware_audit_version.data if InstallAction.PRE_MIGRATE in install_action: host.context[0].data[ 'config_filename'] = schedule_form.hidden_config_filename.data host.context[0].data[ 'override_hw_req'] = schedule_form.hidden_override_hw_req.data # If the dependency is a previous job id, it's non-negative int string. if int(dependency_list[index]) >= 0: dependency = dependency_list[index] # In this case, the dependency is '-1', which means no dependency for the first install action else: dependency = 0 # The dependency for the first install action depends on dependency_list[index]. # The dependency for each install action following first is indicated by the implicit # ordering in the selector. If the user selected Pre-Migrate and Migrate, # Migrate (successor) will have Pre-Migrate (predecessor) as the dependency. for i in xrange(0, len(install_action)): new_install_job = create_or_update_install_job( db_session=db_session, host_id=host.id, install_action=install_action[i], scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, custom_command_profile_ids= custom_command_profile_ids, dependency=dependency) print( "dependency for install_action = {} is {}".format( install_action[i], str(dependency))) dependency = new_install_job.id index += 1 return redirect(url_for(return_url)) else: return render_template( 'asr9k_64_migrate/migration.html', schedule_form=schedule_form, install_action=get_install_migrations_dict(), server_time=datetime.datetime.utcnow(), system_option=SystemOption.get(db_session), config_form=config_form, input_filename="", err_msg="", )