def get_download_job_json_dict(db_session, download_jobs): rows = [] for download_job in download_jobs: if isinstance(download_job, DownloadJob) or isinstance( download_job, DownloadJobHistory): row = dict() row['download_job_id'] = download_job.id row['image_name'] = download_job.cco_filename row['scheduled_time'] = download_job.scheduled_time server = get_server_by_id(db_session, download_job.server_id) if server is not None: row['server_repository'] = server.hostname if not is_empty(download_job.server_directory): row['server_repository'] = row['server_repository'] + \ '<br><span style="color: Gray;"><b>Sub-directory:</b></span> ' + \ download_job.server_directory else: row['server_repository'] = UNKNOWN row['status'] = download_job.status row['status_time'] = download_job.status_time row['created_by'] = download_job.created_by if download_job.trace is not None: row['trace'] = download_job.id rows.append(row) return {'data': rows}
def region_create(): if not can_create(current_user): abort(401) form = RegionForm(request.form) if request.method == 'POST' and form.validate(): db_session = DBSession() region = get_region(db_session, form.region_name.data) if region is not None: return render_template('region/edit.html', form=form, duplicate_error=True) # Compose a list of server hostnames server_names = [ get_server_by_id(db_session, id).hostname for id in request.form.getlist('selected-servers') ] try: create_or_update_region(db_session=db_session, region_name=form.region_name.data, server_repositories=",".join(server_names), created_by=current_user.username) except Exception as e: db_session.rollback() logger.exception("region_create() encountered an exception: " + e.message) return redirect(url_for('home')) return render_template('region/edit.html', form=form)
def get_install_job_info(db_session, install_job, utc_offset): row = dict() if install_job.__class__.__name__ == 'InstallJob': row[KEY_ID] = install_job.id server = get_server_by_id(db_session, install_job.server_id) row[KEY_SERVER_REPOSITORY] = server.hostname if server else "" row[KEY_SERVER_DIRECTORY] = install_job.server_directory if install_job.server_directory else "" custom_command_profile_names = [] if install_job.custom_command_profile_ids: for custom_command_profile_id in install_job.custom_command_profile_ids.split(','): custom_command_profile = get_custom_command_profile_by_id(db_session, custom_command_profile_id) if custom_command_profile is not None: custom_command_profile_names.append(custom_command_profile.profile_name) row[KEY_CUSTOM_COMMAND_PROFILE] = custom_command_profile_names else: row[KEY_ID] = install_job.install_job_id row[KEY_INSTALL_ACTION] = install_job.install_action row[KEY_DEPENDENCY] = install_job.dependency if install_job.dependency else "" if utc_offset and verify_utc_offset(utc_offset): if install_job.scheduled_time: row[KEY_SCHEDULED_TIME] = get_local_time(install_job.scheduled_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_SCHEDULED_TIME] = "" if install_job.start_time: row[KEY_START_TIME] = get_local_time(install_job.start_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_START_TIME] = "" if install_job.status_time: row[KEY_STATUS_TIME] = get_local_time(install_job.status_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_STATUS_TIME] = "" else: row[KEY_SCHEDULED_TIME] = install_job.scheduled_time if install_job.scheduled_time else "" row[KEY_START_TIME] = install_job.start_time if install_job.start_time else "" row[KEY_STATUS_TIME] = install_job.status_time if install_job.status_time else "" row[KEY_SOFTWARE_PACKAGES] = install_job.packages.split(',') if install_job.packages else [] row[KEY_STATUS] = install_job.status if install_job.status else JobStatus.SCHEDULED row[KEY_TRACE] = install_job.trace if install_job.trace else "" row[KEY_CREATED_BY] = install_job.created_by if install_job.created_by else "" row[KEY_HOSTNAME] = get_host_by_id(db_session, install_job.host_id).hostname if install_job.host_id else "" return row
def upload_config_to_server_repository(): server_id = request.args.get('server_id', -1, type=int) server_directory = request.args.get('server_directory', '', type=str) filename = request.args.get('filename', '', type=str) if server_id == -1: logger.error('No server repository selected.') return jsonify(status='No server repository selected.') db_session = DBSession() server = get_server_by_id(db_session, server_id) if not server: logger.error('Selected server repository not found in database.') return jsonify( status='Selected server repository not found in database.') if not server_directory: server_directory = None if not filename: logger.error('No filename selected.') return jsonify(status='No filename selected.') config_conversion_path = get_config_conversion_path() stripped_filename = get_stripped_filename(filename) output_iox = stripped_filename + ".iox" output_cal = stripped_filename + ".cal" status = upload_files_to_server_repository( os.path.join(config_conversion_path, output_iox), server, server_directory, output_iox) if status == "OK": status = upload_files_to_server_repository( os.path.join(config_conversion_path, output_cal), server, server_directory, output_cal) return jsonify(status=status)
def upload_config_to_server_repository(): server_id = request.args.get('server_id', -1, type=int) server_directory = request.args.get('server_directory', '', type=str) filename = request.args.get('filename', '', type=str) if server_id == -1: logger.error('No server repository selected.') return jsonify(status='No server repository selected.') db_session = DBSession() server = get_server_by_id(db_session, server_id) if not server: logger.error('Selected server repository not found in database.') return jsonify(status='Selected server repository not found in database.') if not server_directory: server_directory = None if not filename: logger.error('No filename selected.') return jsonify(status='No filename selected.') config_conversion_path = get_config_conversion_path() stripped_filename = get_stripped_filename(filename) output_iox = stripped_filename + ".iox" output_cal = stripped_filename + ".cal" status = upload_files_to_server_repository(os.path.join(config_conversion_path, output_iox), server, server_directory, output_iox) if status == "OK": status = upload_files_to_server_repository(os.path.join(config_conversion_path, output_cal), server, server_directory, output_cal) return jsonify(status=status)
def handle_schedule_install_form(request, db_session, hostname, install_job=None): host = get_host(db_session, hostname) if host is None: abort(404) return_url = get_return_url(request, 'host_dashboard.home') form = HostScheduleInstallForm(request.form) # Retrieves all the install jobs for this host. This will allow # the user to select which install job this install job can depend on. install_jobs = db_session.query(InstallJob).filter( InstallJob.host_id == host.id).order_by(InstallJob.scheduled_time.asc()).all() region_servers = host.region.servers # Returns all server repositories if the region does not have any server repository designated. if is_empty(region_servers): region_servers = get_server_list(db_session) # Fills the selections fill_servers(form.server_dialog_server.choices, region_servers) fill_servers(form.server_modal_dialog_server.choices, region_servers) fill_servers(form.cisco_dialog_server.choices, region_servers, False) fill_dependency_from_host_install_jobs(form.dependency.choices, install_jobs, (-1 if install_job is None else install_job.id)) fill_custom_command_profiles(db_session, form.custom_command_profile.choices) if request.method == 'POST': if install_job is not None: # In Edit mode, the install_action UI on HostScheduleForm is disabled (not allow to change). # Thus, there will be no value returned by form.install_action.data. So, re-use the existing ones. install_action = [install_job.install_action] else: install_action = form.install_action.data 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() custom_command_profile_ids = [str(i) for i in form.custom_command_profile.data] # install_action is a list object which may contain multiple install actions. # If only one install_action, accept the selected dependency if any if len(install_action) == 1: dependency = int(form.dependency.data) create_or_update_install_job(db_session=db_session, host_id=host.id, install_action=install_action[0], scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, custom_command_profile_ids=custom_command_profile_ids, dependency=dependency, created_by=current_user.username, install_job=install_job) 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, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, custom_command_profile_ids=custom_command_profile_ids, dependency=dependency, created_by=current_user.username, install_job=install_job) dependency = new_install_job.id return redirect(url_for(return_url, hostname=hostname)) elif request.method == 'GET': # Initialize the hidden fields form.hidden_server.data = -1 form.hidden_selected_hosts.data = '' form.hidden_server_name.data = '' form.hidden_server_directory.data = '' form.hidden_pending_downloads.data = '' form.hidden_edit.data = install_job is not None # In Edit mode if install_job is not None: form.install_action.data = install_job.install_action if install_job.server_id is not None: form.hidden_server.data = install_job.server_id server = get_server_by_id(db_session, install_job.server_id) if server is not None: form.hidden_server_name.data = server.hostname form.hidden_server_directory.data = '' if install_job.server_directory is None else install_job.server_directory form.hidden_pending_downloads.data = '' if install_job.pending_downloads is None else install_job.pending_downloads # Form a line separated list for the textarea if install_job.packages is not None: form.software_packages.data = '\n'.join(install_job.packages.split(',')) form.dependency.data = str(install_job.dependency) if install_job.scheduled_time is not None: form.scheduled_time_UTC.data = get_datetime_string(install_job.scheduled_time) if install_job.custom_command_profile_ids: ids = [int(id) for id in install_job.custom_command_profile_ids.split(',')] form.custom_command_profile.data = ids return render_template('host/schedule_install.html', form=form, system_option=SystemOption.get(db_session), host=host, server_time=datetime.datetime.utcnow(), install_job=install_job, return_url=return_url)
def handle_schedule_install_form(request, db_session, hostname, install_job=None): host = get_host(db_session, hostname) if host is None: abort(404) return_url = get_return_url(request, 'host_dashboard.home') form = HostScheduleInstallForm(request.form) # Retrieves all the install jobs for this host. This will allow # the user to select which install job this install job can depend on. install_jobs = db_session.query(InstallJob).filter( InstallJob.host_id == host.id).order_by( InstallJob.scheduled_time.asc()).all() region_servers = host.region.servers # Returns all server repositories if the region does not have any server repository designated. if is_empty(region_servers): region_servers = get_server_list(db_session) # Fills the selections fill_servers(form.server_dialog_server.choices, region_servers) fill_servers(form.server_modal_dialog_server.choices, region_servers) fill_servers(form.cisco_dialog_server.choices, region_servers, False) fill_dependency_from_host_install_jobs( form.dependency.choices, install_jobs, (-1 if install_job is None else install_job.id)) fill_custom_command_profiles(db_session, form.custom_command_profile.choices) if request.method == 'POST': if install_job is not None: # In Edit mode, the install_action UI on HostScheduleForm is disabled (not allow to change). # Thus, there will be no value returned by form.install_action.data. So, re-use the existing ones. install_action = [install_job.install_action] else: install_action = form.install_action.data 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() custom_command_profile_ids = [ str(i) for i in form.custom_command_profile.data ] # install_action is a list object which may contain multiple install actions. # If only one install_action, accept the selected dependency if any if len(install_action) == 1: dependency = int(form.dependency.data) create_or_update_install_job( db_session=db_session, host_id=host.id, install_action=install_action[0], scheduled_time=scheduled_time, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, custom_command_profile_ids=custom_command_profile_ids, dependency=dependency, created_by=current_user.username, install_job=install_job) 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, software_packages=software_packages, server_id=server_id, server_directory=server_directory, pending_downloads=pending_downloads, custom_command_profile_ids=custom_command_profile_ids, dependency=dependency, created_by=current_user.username, install_job=install_job) dependency = new_install_job.id return redirect(url_for(return_url, hostname=hostname)) elif request.method == 'GET': # Initialize the hidden fields form.hidden_server.data = -1 form.hidden_selected_hosts.data = '' form.hidden_server_name.data = '' form.hidden_server_directory.data = '' form.hidden_pending_downloads.data = '' form.hidden_edit.data = install_job is not None # In Edit mode if install_job is not None: form.install_action.data = install_job.install_action if install_job.server_id is not None: form.hidden_server.data = install_job.server_id server = get_server_by_id(db_session, install_job.server_id) if server is not None: form.hidden_server_name.data = server.hostname form.hidden_server_directory.data = '' if install_job.server_directory is None else install_job.server_directory form.hidden_pending_downloads.data = '' if install_job.pending_downloads is None else install_job.pending_downloads # Form a line separated list for the textarea if install_job.packages is not None: form.software_packages.data = '\n'.join( install_job.packages.split(',')) form.dependency.data = str(install_job.dependency) if install_job.scheduled_time is not None: form.scheduled_time_UTC.data = get_datetime_string( install_job.scheduled_time) if install_job.custom_command_profile_ids: ids = [ int(id) for id in install_job.custom_command_profile_ids.split(',') ] form.custom_command_profile.data = ids return render_template('host/schedule_install.html', form=form, system_option=SystemOption.get(db_session), host=host, server_time=datetime.datetime.utcnow(), install_job=install_job, return_url=return_url)
def handle_schedule_install_form(request, db_session, hostname, install_job=None): host = get_host(db_session, hostname) if host is None: abort(404) return_url = get_return_url(request, 'host_dashboard.home') schedule_form = ScheduleMigrationForm(request.form) # Fills the selections fill_servers(schedule_form.server_dialog_server.choices, host.region.servers, include_local=False) fill_custom_command_profiles(db_session, schedule_form.custom_command_profile.choices) fill_hardware_audit_version(schedule_form.hardware_audit_version.choices) if request.method == 'POST': if install_job is not None: # In Edit mode, the install_action UI on HostScheduleForm is disabled (not allowed to change). # Thus, there will be no value returned by form.install_action.data. So, re-use the existing ones. install_action = [ install_job.install_action ] else: install_action = schedule_form.install_action.data 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 # install_action is a list object which can only contain one install action # at this editing time, accept the selected dependency if any dependency = int(schedule_form.hidden_dependency.data) create_or_update_install_job(db_session=db_session, host_id=host.id, install_action=install_action[0], 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, install_job=install_job) return redirect(url_for(return_url, hostname=hostname)) elif request.method == 'GET': # Initialize the hidden fields schedule_form.hidden_server.data = -1 schedule_form.hidden_server_name.data = '' schedule_form.hidden_server_directory.data = '' schedule_form.hidden_pending_downloads.data = '' schedule_form.hidden_edit.data = install_job is not None schedule_form.hidden_region.data = str(host.region.name) fill_default_region(schedule_form.region.choices, host.region) schedule_form.hidden_hosts.data = hostname schedule_form.hidden_dependency.data = '' # In Edit mode if install_job is not None: schedule_form.install_action.data = install_job.install_action if install_job.custom_command_profile_ids: ids = [str(id) for id in install_job.custom_command_profile_ids.split(',')] schedule_form.custom_command_profile.data = ids schedule_form.hidden_override_hw_req.data = host.context[0].data.get('override_hw_req') schedule_form.hidden_config_filename.data = host.context[0].data.get('config_filename') schedule_form.hidden_hardware_audit_version.data = host.context[0].data.get('hardware_audit_version') if install_job.server_id is not None: schedule_form.hidden_server.data = install_job.server_id server = get_server_by_id(db_session, install_job.server_id) if server is not None: schedule_form.hidden_server_name.data = server.hostname schedule_form.hidden_server_directory.data = '' if install_job.server_directory is None \ else install_job.server_directory schedule_form.hidden_pending_downloads.data = '' if install_job.pending_downloads is None \ else install_job.pending_downloads # Form a line separated list for the textarea if install_job.packages is not None: schedule_form.hidden_software_packages.data = install_job.packages if install_job.dependency is not None: schedule_form.hidden_dependency.data = str(install_job.dependency) else: schedule_form.hidden_dependency.data = '-1' if install_job.scheduled_time is not None: schedule_form.scheduled_time_UTC.data = get_datetime_string(install_job.scheduled_time) return render_template('asr9k_64_migrate/migration.html', schedule_form=schedule_form, system_option=SystemOption.get(db_session), host=host, server_time=datetime.datetime.utcnow(), install_job=install_job, return_url=return_url, install_action=get_install_migrations_dict(), input_filename="", err_msg="")
def handle_schedule_install_form(request, db_session, hostname, install_job=None): host = get_host(db_session, hostname) if host is None: abort(404) return_url = get_return_url(request, 'host_dashboard.home') schedule_form = ScheduleMigrationForm(request.form) # Fills the selections fill_servers(schedule_form.server_dialog_server.choices, host.region.servers, include_local=False) fill_custom_command_profiles(db_session, schedule_form.custom_command_profile.choices) fill_hardware_audit_version(schedule_form.hardware_audit_version.choices) if request.method == 'POST': if install_job is not None: # In Edit mode, the install_action UI on HostScheduleForm is disabled (not allowed to change). # Thus, there will be no value returned by form.install_action.data. So, re-use the existing ones. install_action = [install_job.install_action] else: install_action = schedule_form.install_action.data 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 # install_action is a list object which can only contain one install action # at this editing time, accept the selected dependency if any dependency = int(schedule_form.hidden_dependency.data) create_or_update_install_job( db_session=db_session, host_id=host.id, install_action=install_action[0], 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, install_job=install_job) return redirect(url_for(return_url, hostname=hostname)) elif request.method == 'GET': # Initialize the hidden fields schedule_form.hidden_server.data = -1 schedule_form.hidden_server_name.data = '' schedule_form.hidden_server_directory.data = '' schedule_form.hidden_pending_downloads.data = '' schedule_form.hidden_edit.data = install_job is not None schedule_form.hidden_region.data = str(host.region.name) fill_default_region(schedule_form.region.choices, host.region) schedule_form.hidden_hosts.data = hostname schedule_form.hidden_dependency.data = '' # In Edit mode if install_job is not None: schedule_form.install_action.data = install_job.install_action if install_job.custom_command_profile_ids: ids = [ str(id) for id in install_job.custom_command_profile_ids.split(',') ] schedule_form.custom_command_profile.data = ids schedule_form.hidden_override_hw_req.data = host.context[ 0].data.get('override_hw_req') schedule_form.hidden_config_filename.data = host.context[ 0].data.get('config_filename') schedule_form.hidden_hardware_audit_version.data = host.context[ 0].data.get('hardware_audit_version') if install_job.server_id is not None: schedule_form.hidden_server.data = install_job.server_id server = get_server_by_id(db_session, install_job.server_id) if server is not None: schedule_form.hidden_server_name.data = server.hostname schedule_form.hidden_server_directory.data = '' if install_job.server_directory is None \ else install_job.server_directory schedule_form.hidden_pending_downloads.data = '' if install_job.pending_downloads is None \ else install_job.pending_downloads # Form a line separated list for the textarea if install_job.packages is not None: schedule_form.hidden_software_packages.data = install_job.packages if install_job.dependency is not None: schedule_form.hidden_dependency.data = str( install_job.dependency) else: schedule_form.hidden_dependency.data = '-1' if install_job.scheduled_time is not None: schedule_form.scheduled_time_UTC.data = get_datetime_string( install_job.scheduled_time) return render_template('asr9k_64_migrate/migration.html', schedule_form=schedule_form, system_option=SystemOption.get(db_session), host=host, server_time=datetime.datetime.utcnow(), install_job=install_job, return_url=return_url, install_action=get_install_migrations_dict(), input_filename="", err_msg="")
def get_install_job_info(db_session, install_job, utc_offset): row = dict() if install_job.__class__.__name__ == 'InstallJob': row[KEY_ID] = install_job.id server = get_server_by_id(db_session, install_job.server_id) row[KEY_SERVER_REPOSITORY] = server.hostname if server else "" row[KEY_SERVER_DIRECTORY] = install_job.server_directory if install_job.server_directory else "" custom_command_profile_names = [] if install_job.custom_command_profile_ids: for custom_command_profile_id in install_job.custom_command_profile_ids.split( ','): custom_command_profile = get_custom_command_profile_by_id( db_session, custom_command_profile_id) if custom_command_profile is not None: custom_command_profile_names.append( custom_command_profile.profile_name) row[KEY_CUSTOM_COMMAND_PROFILE] = custom_command_profile_names else: row[KEY_ID] = install_job.install_job_id row[KEY_INSTALL_ACTION] = install_job.install_action row[KEY_DEPENDENCY] = install_job.dependency if install_job.dependency else "" if utc_offset and verify_utc_offset(utc_offset): if install_job.scheduled_time: row[KEY_SCHEDULED_TIME] = get_local_time( install_job.scheduled_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_SCHEDULED_TIME] = "" if install_job.start_time: row[KEY_START_TIME] = get_local_time( install_job.start_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_START_TIME] = "" if install_job.status_time: row[KEY_STATUS_TIME] = get_local_time( install_job.status_time, utc_offset).strftime("%m-%d-%Y %I:%M %p") else: row[KEY_STATUS_TIME] = "" else: row[KEY_SCHEDULED_TIME] = install_job.scheduled_time if install_job.scheduled_time else "" row[KEY_START_TIME] = install_job.start_time if install_job.start_time else "" row[KEY_STATUS_TIME] = install_job.status_time if install_job.status_time else "" row[KEY_SOFTWARE_PACKAGES] = install_job.packages.split( ',') if install_job.packages else [] row[KEY_STATUS] = install_job.status if install_job.status else JobStatus.SCHEDULED row[KEY_TRACE] = install_job.trace if install_job.trace else "" row[KEY_CREATED_BY] = install_job.created_by if install_job.created_by else "" row[KEY_HOSTNAME] = get_host_by_id( db_session, install_job.host_id).hostname if install_job.host_id else "" return row