Beispiel #1
0
def init_schedule_form(db_session, http_request, get=False):
    hosts = get_asr9k_host_list(db_session)
    if hosts is None:
        abort(404)

    db_session = DBSession()

    schedule_form = ScheduleMigrationForm(http_request.form)
    fill_regions(db_session, schedule_form.region.choices)
    fill_hardware_audit_version(schedule_form.hardware_audit_version.choices)
    fill_custom_command_profiles(db_session, schedule_form.custom_command_profile.choices)

    if get:
        # Initialize the hidden fields
        schedule_form.hidden_server_name.data = ''
        schedule_form.hidden_server.data = -1
        schedule_form.hidden_server_directory.data = ''
        schedule_form.hidden_pending_downloads.data = ''
        schedule_form.hidden_region.data = -1
        schedule_form.hidden_hosts.data = ''
        schedule_form.hidden_software_packages.data = ''
        schedule_form.hidden_edit.data = 'False'
        schedule_form.hidden_config_filename.data = ''
        schedule_form.hidden_override_hw_req.data = '0'
        schedule_form.hidden_dependency.data = ''
        schedule_form.hidden_hardware_audit_version.data = ''
    return schedule_form
Beispiel #2
0
def init_schedule_form(db_session, http_request, get=False):
    hosts = get_asr9k_host_list(db_session)
    if hosts is None:
        abort(404)

    db_session = DBSession()

    schedule_form = ScheduleMigrationForm(http_request.form)
    fill_regions(db_session, schedule_form.region.choices)
    fill_hardware_audit_version(schedule_form.hardware_audit_version.choices)
    fill_custom_command_profiles(db_session,
                                 schedule_form.custom_command_profile.choices)

    if get:
        # Initialize the hidden fields
        schedule_form.hidden_server_name.data = ''
        schedule_form.hidden_server.data = -1
        schedule_form.hidden_server_directory.data = ''
        schedule_form.hidden_pending_downloads.data = ''
        schedule_form.hidden_region.data = -1
        schedule_form.hidden_hosts.data = ''
        schedule_form.hidden_software_packages.data = ''
        schedule_form.hidden_edit.data = 'False'
        schedule_form.hidden_config_filename.data = ''
        schedule_form.hidden_override_hw_req.data = '0'
        schedule_form.hidden_dependency.data = ''
        schedule_form.hidden_hardware_audit_version.data = ''
    return schedule_form
Beispiel #3
0
def home():
    conformance_form = ConformanceForm(request.form)
    conformance_report_dialog_form = ConformanceReportDialogForm(request.form)
    make_conform_dialog_form = MakeConformDialogForm(request.form)
    fill_custom_command_profiles(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

    return render_template('conformance/index.html',
                           conformance_form=conformance_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,
                           system_option=SystemOption.get(DBSession()))
Beispiel #4
0
def home():
    conformance_form = ConformanceForm(request.form)
    conformance_report_dialog_form = ConformanceReportDialogForm(request.form)
    make_conform_dialog_form = MakeConformDialogForm(request.form)
    fill_custom_command_profiles(
        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

    return render_template(
        'conformance/index.html',
        conformance_form=conformance_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,
        system_option=SystemOption.get(DBSession()))
Beispiel #5
0
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()))
Beispiel #6
0
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)
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
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)
Beispiel #10
0
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()))
Beispiel #11
0
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="")
Beispiel #12
0
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="")