Exemple #1
0
    def generate_file_diff(self, source_file_directory, target_file_directory):
        source_file_list = get_file_list(source_file_directory)
        target_file_list = get_file_list(target_file_directory)

        for filename in target_file_list:
            if '.txt' in filename and filename in source_file_list:
                target_file_path = os.path.join(target_file_directory, filename)
                source_file_path = os.path.join(source_file_directory, filename)

                if os.path.isfile(source_file_path) and os.path.isfile(target_file_path):
                    results = generate_file_diff(source_file_path, target_file_path)
                    # Are there any changes in the logs
                    insertion_count = results.count('ins style')
                    deletion_count = results.count('del style')

                    if insertion_count > 0 or deletion_count > 0:
                        results = results.replace(' ', ' ')

                        rep_dict = {"ins style": "ins style", "del style": "del style", "¶": ''}
                        results = multiple_replace(results, rep_dict)

                        source_filename = 'File 1: ' + filename + ' (created on ' + \
                                          get_datetime_string(get_file_timestamp(source_file_path)) + ')'
                        target_filename = 'File 2: ' + filename + ' (created on ' + \
                                          get_datetime_string(get_file_timestamp(target_file_path)) + ')'

                        # Add insertion and deletion status
                        html_code = source_filename + '<br>' + target_filename + '<br><br>' + \
                                    '<ins style="background:#e6ffe6;">Insertions</ins>:&nbsp;' + str(insertion_count) + \
                                    '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + \
                                    '<del style="background:#ffe6e6;">Deletions</del>:&nbsp;' + str(deletion_count) + \
                                    '<hr>'
                        diff_file_name = os.path.join(target_file_directory, filename + '.diff.html')
                        with open(diff_file_name, 'w') as fo:
                            fo.write('<pre>' + html_code + results + '</pre>')
Exemple #2
0
    def create_email_notification(self, db_session, logger, host, install_job):
        try:
            session_log_link = "log/hosts/{}/install_job_history/session_log/{}?file_path={}".format(
                urllib.quote(host.hostname), install_job.id, install_job.session_log)

            message = '<html><head></head><body>'
            if install_job.status == JobStatus.COMPLETED:
                message += 'The scheduled installation for host "' + host.hostname + '" has COMPLETED.<br><br>'
            elif install_job.status == JobStatus.FAILED:
                message += 'The scheduled installation for host "' + host.hostname + '" has FAILED.<br><br>'

            message += 'Scheduled Time: ' + \
                       get_datetime_string(install_job.scheduled_time) + \
                       ' UTC<br>'
            message += 'Start Time: ' + \
                       get_datetime_string(install_job.start_time) + \
                       ' UTC<br>'
            message += 'Install Action: ' + install_job.install_action + '<br><br>'

            message = self.check_command_file_diff(install_job, message)

            session_log_url = SystemOption.get(db_session).base_url + '/' + session_log_link

            message += 'For more information, click the link below<br><br>'
            message += session_log_url + '<br><br>'

            if install_job.packages is not None and len(install_job.packages) > 0:
                message += 'Followings are the software packages: <br><br>' + install_job.packages.replace(',','<br>')

            message += '</body></html>'

            create_email_job(db_session, logger, message, install_job.created_by)

        except Exception:
            logger.exception('create_email_notification() hit exception')
Exemple #3
0
    def generate_file_diff(self, source_file_directory, target_file_directory):
        source_file_list = get_file_list(source_file_directory)
        target_file_list = get_file_list(target_file_directory)

        for filename in target_file_list:
            if '.txt' in filename and filename in source_file_list:
                target_file_path = os.path.join(target_file_directory, filename)
                source_file_path = os.path.join(source_file_directory, filename)

                if os.path.isfile(source_file_path) and os.path.isfile(target_file_path):
                    results = generate_file_diff(source_file_path, target_file_path)
                    # Are there any changes in the logs
                    insertion_count = results.count('ins style')
                    deletion_count = results.count('del style')

                    if insertion_count > 0 or deletion_count > 0:
                        results = results.replace(' ', '&nbsp;')

                        rep_dict = {"ins&nbsp;style": "ins style", "del&nbsp;style": "del style", "&para;": ''}
                        results = multiple_replace(results, rep_dict)

                        source_filename = 'File 1: ' + filename + ' (created on ' + \
                                          get_datetime_string(get_file_timestamp(source_file_path)) + ')'
                        target_filename = 'File 2: ' + filename + ' (created on ' + \
                                          get_datetime_string(get_file_timestamp(target_file_path)) + ')'

                        # Add insertion and deletion status
                        html_code = source_filename + '<br>' + target_filename + '<br><br>' + \
                                    '<ins style="background:#e6ffe6;">Insertions</ins>:&nbsp;' + str(insertion_count) + \
                                    '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + \
                                    '<del style="background:#ffe6e6;">Deletions</del>:&nbsp;' + str(deletion_count) + \
                                    '<hr>'
                        diff_file_name = os.path.join(target_file_directory, filename + '.diff.html')
                        with open(diff_file_name, 'w') as fo:
                            fo.write('<pre>' + html_code + results + '</pre>')
Exemple #4
0
def send_install_status_email(install_job):
    db_session = DBSession
    username = install_job.created_by

    system_option = SystemOption.get(db_session)
    if not system_option.enable_email_notify:
        return
    
    smtp_server = get_smtp_server(db_session)
    if smtp_server is None:
        logger.error('mailer: SMTP Server has not been specified')
        return
    
    user = get_user(db_session, username)
    if user is None:
        logger.error('mailer: Unable to locate user "%s"' % username)
        return
    
    host = get_host(db_session, install_job.host_id)
    if host is None:
        logger.error('mailer: Unable to locate host id "%s"' % str(install_job.host_id))
        return
    
    message = '<html><head><body>'
    if install_job.status == JobStatus.COMPLETED:
        message += 'The scheduled installation for host "' + host.hostname + '" has COMPLETED<br><br>'
    elif install_job.status == JobStatus.FAILED:
        message += 'The scheduled installation for host "' + host.hostname + '" has FAILED<br><br>'
    
    message += 'Scheduled Time: ' + get_datetime_string(install_job.scheduled_time) + ' (UTC)<br>'    
    message += 'Start Time: ' + get_datetime_string(install_job.start_time) + ' (UTC)<br>'
    message += 'Install Action: ' + install_job.install_action + '<br><br>'
    
    session_log_url = system_option.base_url + '/' + get_session_log_link(host, install_job)
    
    message += 'For more information, click the link below<br><br>'
    message += session_log_url + '<br><br>'
    
    if install_job.packages is not None and len(install_job.packages) > 0:
        message += 'Followings are the software packages: <br><br>' + install_job.packages.replace(',','<br>')
        
    message += '</body></head></html>'
    
    sendmail(
        server=smtp_server.server, 
        server_port=smtp_server.server_port,
        sender=smtp_server.sender,
        recipient=user.email,
        message=message,
        use_authentication=smtp_server.use_authentication,
        username=smtp_server.username,
        password=smtp_server.password,
        secure_connection=smtp_server.secure_connection)
 def write_header_info(self):
     self.ws.write(0, 2, 'Software Conformance Report', XLSWriter.style_title)
     report_datetime = get_datetime_string(self.conformance_report.created_time) + \
         ' UTC' if self.locale_datetime is None else self.locale_datetime
         
     self.ws.write(1, 2, report_datetime, XLSWriter.style_center )
     
     self.ws.write(4, 0, 'Summary: ', XLSWriter.style_bold)
     self.ws.write(6, 0, 'Total Hosts: %d' % len(self.conformance_report.hostnames.split(',')),
                   XLSWriter.style_summary)
     self.ws.write(7, 0, 'Match Criteria: ' + (self.conformance_report.match_criteria + ' packages').title(),
                   XLSWriter.style_summary)
     self.ws.write(8, 0, 'Results:', XLSWriter.style_summary)
     
     if self.conformance_report.host_not_in_conformance == 0:
         self.ws.write(9, 0, "     All hosts are in complete conformance", XLSWriter.style_summary)
     else:
         self.ws.write(9, 0, "     %d %s in complete conformance (see the 'Missing Packages' column)"
             % (self.conformance_report.host_not_in_conformance,
                 "hosts are not" if self.conformance_report.host_not_in_conformance > 1 else "host is not"),
                 XLSWriter.style_summary)
                     
     if self.conformance_report.host_out_dated_inventory > 0:
         self.ws.write(10, 0, "     %d %s failed last software inventory retrieval (see '*' in the 'Is Conformed' column)"
             % (self.conformance_report.host_out_dated_inventory,
                 "hosts" if self.conformance_report.host_out_dated_inventory > 1 else "host"),
                 XLSWriter.style_summary)
Exemple #6
0
    def write_header_info(self):
        self.ws.write(0, 2, 'Software Conformance Report', self.style_title)
        report_datetime = get_datetime_string(self.conformance_report.created_time) + \
            ' UTC' if self.locale_datetime is None else self.locale_datetime
            
        self.ws.write(1, 2, report_datetime, self.style_center)

        self.ws.write(4, 0, 'Summary: ', self.style_bold)

        total_hosts = 0 if is_empty(self.conformance_report.hostnames) else \
            len(self.conformance_report.hostnames.split(','))

        self.ws.write(6, 0, 'Total Hosts: %d' % total_hosts, self.style_summary)
        self.ws.write(7, 0, 'Match Criteria: ' + (self.conformance_report.match_criteria + ' packages').title(),
                      self.style_summary)
        self.ws.write(8, 0, 'Results:', self.style_summary)
        
        if self.conformance_report.host_not_in_conformance == 0:
            self.ws.write(9, 0, "     All hosts are in complete conformance", self.style_summary)
        else:
            self.ws.write(9, 0, "     %d %s in complete conformance (see the 'Missing Packages' column)"
                % (self.conformance_report.host_not_in_conformance,
                    "hosts are not" if self.conformance_report.host_not_in_conformance > 1 else "host is not"),
                    self.style_summary)
                        
        if self.conformance_report.host_out_dated_inventory > 0:
            self.ws.write(10, 0, "     %d %s failed last software inventory retrieval (see '*' in the 'Is Conformed' column)"
                % (self.conformance_report.host_out_dated_inventory,
                    "hosts" if self.conformance_report.host_out_dated_inventory > 1 else "host"),
                    self.style_summary)
Exemple #7
0
def download_system_logs():
    db_session = DBSession()
    logs = db_session.query(Log) \
        .order_by(Log.created_time.desc())

    contents = ''
    for log in logs:
        contents += get_datetime_string(log.created_time) + ' UTC\n'
        contents += log.level + ':' + log.msg + '\n'
        if log.trace is not None:
            contents += log.trace + '\n'
        contents += '-' * 70 + '\n'

    # Create a file which contains the size of the image file.
    temp_user_dir = create_temp_user_directory(current_user.username)
    log_file_path = os.path.normpath(os.path.join(temp_user_dir,
                                                  "system_logs"))

    create_directory(log_file_path)
    make_file_writable(log_file_path)

    log_file = open(os.path.join(log_file_path, 'system_logs'), 'w')
    log_file.write(contents)
    log_file.close()

    return send_file(os.path.join(log_file_path, 'system_logs'),
                     as_attachment=True)
Exemple #8
0
def download_system_logs():
    db_session = DBSession()
    logs = db_session.query(Log) \
        .order_by(Log.created_time.desc())

    contents = ''
    for log in logs:
        contents += get_datetime_string(log.created_time) + ' UTC\n'
        contents += log.level + ':' + log.msg + '\n'
        if log.trace is not None:
            contents += log.trace + '\n'
        contents += '-' * 70 + '\n'

    # Create a file which contains the size of the image file.
    temp_user_dir = create_temp_user_directory(current_user.username)
    log_file_path = os.path.normpath(os.path.join(temp_user_dir, "system_logs"))

    create_directory(log_file_path)
    make_file_writable(log_file_path)

    log_file = open(os.path.join(log_file_path, 'system_logs'), 'w')
    log_file.write(contents)
    log_file.close()

    return send_file(os.path.join(log_file_path, 'system_logs'), as_attachment=True)
Exemple #9
0
def fill_dependency_from_host_install_jobs(choices, install_jobs, current_install_job_id):
    # Remove all the existing entries
    del choices[:]
    choices.append((-1, 'None'))
    
    for install_job in install_jobs:
        if install_job.id != current_install_job_id:
            choices.append((install_job.id, '%s - %s' % (install_job.install_action, 
                            get_datetime_string(install_job.scheduled_time))))
Exemple #10
0
def fill_dependency_from_host_install_jobs(choices, install_jobs, current_install_job_id):
    # Remove all the existing entries
    del choices[:]
    choices.append((-1, 'None'))
    
    for install_job in install_jobs:
        if install_job.id != current_install_job_id:
            choices.append((install_job.id, '%s - %s' % (install_job.install_action, 
                            get_datetime_string(install_job.scheduled_time))))
Exemple #11
0
def api_get_conformance_report_datetime(id):
    conformance_report_datetime = None
    db_session = DBSession()

    conformance_report = get_conformance_report_by_id(db_session, id)
    if conformance_report is not None:
        conformance_report_datetime = get_datetime_string(conformance_report.created_time)

    return jsonify(**{'data': [
        {'conformance_report_datetime': conformance_report_datetime}
    ]})
Exemple #12
0
def api_get_cco_lookup_time():
    system_option = SystemOption.get(DBSession())
    if system_option.cco_lookup_time is not None:
        return jsonify(
            **{
                'data': [{
                    'cco_lookup_time':
                    get_datetime_string(system_option.cco_lookup_time)
                }]
            })
    else:
        return jsonify({'status': 'Failed'})
Exemple #13
0
def api_get_conformance_report_datetime(id):
    conformance_report_datetime = None
    db_session = DBSession()

    conformance_report = get_conformance_report_by_id(db_session, id)
    if conformance_report is not None:
        conformance_report_datetime = get_datetime_string(
            conformance_report.created_time)

    return jsonify(**{
        'data': [{
            'conformance_report_datetime': conformance_report_datetime
        }]
    })
Exemple #14
0
def home():
    if current_user.privilege != UserPrivilege.ADMIN:
        abort(401)

    db_session = DBSession()

    smtp_form = SMTPForm(request.form)
    admin_console_form = AdminConsoleForm(request.form)

    smtp_server = get_smtp_server(db_session)
    system_option = SystemOption.get(db_session)

    fill_user_privileges(admin_console_form.ldap_default_user_privilege.choices)

    if request.method == 'POST' and \
        smtp_form.validate() and \
        admin_console_form.validate():

        if smtp_server is None:
            smtp_server = SMTPServer()
            db_session.add(smtp_server)

        smtp_server.server = smtp_form.server.data
        smtp_server.server_port = smtp_form.server_port.data if len(smtp_form.server_port.data) > 0 else None
        smtp_server.sender = smtp_form.sender.data
        smtp_server.use_authentication = smtp_form.use_authentication.data
        smtp_server.username = smtp_form.username.data
        if len(smtp_form.password.data) > 0:
            smtp_server.password = smtp_form.password.data
        smtp_server.secure_connection = smtp_form.secure_connection.data

        system_option.inventory_threads = admin_console_form.num_inventory_threads.data
        system_option.install_threads = admin_console_form.num_install_threads.data
        system_option.download_threads = admin_console_form.num_download_threads.data
        system_option.can_schedule = admin_console_form.can_schedule.data
        system_option.can_install = admin_console_form.can_install.data
        system_option.enable_email_notify = admin_console_form.enable_email_notify.data
        system_option.enable_inventory = admin_console_form.enable_inventory.data

        # The LDAP UI may be hidden if it is not supported.
        # In this case, the flag is not set.
        if not is_empty(admin_console_form.enable_ldap_auth.data):
            system_option.enable_ldap_auth = admin_console_form.enable_ldap_auth.data
            system_option.ldap_server_url = admin_console_form.ldap_server_url.data
            system_option.ldap_default_user_privilege = admin_console_form.ldap_default_user_privilege.data
            system_option.ldap_server_distinguished_names = admin_console_form.ldap_server_distinguished_names.data.strip()

        system_option.inventory_hour = admin_console_form.inventory_hour.data
        system_option.inventory_history_per_host = admin_console_form.inventory_history_per_host.data
        system_option.download_history_per_user = admin_console_form.download_history_per_user.data
        system_option.install_history_per_host = admin_console_form.install_history_per_host.data
        system_option.total_system_logs = admin_console_form.total_system_logs.data
        system_option.enable_default_host_authentication = admin_console_form.enable_default_host_authentication.data
        system_option.default_host_authentication_choice = admin_console_form.default_host_authentication_choice.data
        system_option.enable_cco_lookup = admin_console_form.enable_cco_lookup.data
        system_option.use_utc_timezone = admin_console_form.use_utc_timezone.data
        system_option.default_host_username = admin_console_form.default_host_username.data

        if len(admin_console_form.default_host_password.data) > 0:
            system_option.default_host_password = admin_console_form.default_host_password.data

        system_option.enable_user_credential_for_host = admin_console_form.enable_user_credential_for_host.data

        db_session.commit()

        return redirect(url_for('home'))
    else:

        admin_console_form.num_inventory_threads.data = system_option.inventory_threads
        admin_console_form.num_install_threads.data = system_option.install_threads
        admin_console_form.num_download_threads.data = system_option.download_threads
        admin_console_form.can_schedule.data = system_option.can_schedule
        admin_console_form.can_install.data = system_option.can_install
        admin_console_form.enable_email_notify.data = system_option.enable_email_notify
        admin_console_form.enable_ldap_auth.data = system_option.enable_ldap_auth
        admin_console_form.ldap_server_url.data = system_option.ldap_server_url
        admin_console_form.ldap_default_user_privilege.data = system_option.ldap_default_user_privilege
        admin_console_form.ldap_server_distinguished_names.data = system_option.ldap_server_distinguished_names
        admin_console_form.enable_inventory.data = system_option.enable_inventory
        admin_console_form.inventory_hour.data = system_option.inventory_hour
        admin_console_form.inventory_history_per_host.data = system_option.inventory_history_per_host
        admin_console_form.download_history_per_user.data = system_option.download_history_per_user
        admin_console_form.install_history_per_host.data = system_option.install_history_per_host
        admin_console_form.total_system_logs.data = system_option.total_system_logs
        admin_console_form.enable_default_host_authentication.data = system_option.enable_default_host_authentication
        admin_console_form.default_host_authentication_choice.data = system_option.default_host_authentication_choice
        admin_console_form.default_host_username.data = system_option.default_host_username
        admin_console_form.enable_cco_lookup.data = system_option.enable_cco_lookup
        admin_console_form.use_utc_timezone.data = system_option.use_utc_timezone
        admin_console_form.cco_lookup_time.data = get_datetime_string(system_option.cco_lookup_time)
        admin_console_form.enable_user_credential_for_host.data = system_option.enable_user_credential_for_host

        if not is_empty(system_option.default_host_password):
            admin_console_form.default_host_password_placeholder = 'Use Password on File'
        else:
            admin_console_form.default_host_password_placeholder = 'No Password Specified'

        if smtp_server is not None:
            smtp_form.server.data = smtp_server.server
            smtp_form.server_port.data = smtp_server.server_port
            smtp_form.sender.data = smtp_server.sender
            smtp_form.use_authentication.data = smtp_server.use_authentication
            smtp_form.username.data = smtp_server.username
            smtp_form.secure_connection.data = smtp_server.secure_connection
            if not is_empty(smtp_server.password):
                smtp_form.password_placeholder = 'Use Password on File'
            else:
                smtp_form.password_placeholder = 'No Password Specified'

        return render_template('admin/index.html',
                               admin_console_form=admin_console_form,
                               smtp_form=smtp_form,
                               system_option=SystemOption.get(db_session),
                               is_ldap_supported=is_ldap_supported())
Exemple #15
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)
Exemple #16
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)
Exemple #17
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="")
Exemple #18
0
def home():
    if current_user.privilege != UserPrivilege.ADMIN:
        abort(401)

    db_session = DBSession()

    smtp_form = SMTPForm(request.form)
    admin_console_form = AdminConsoleForm(request.form)

    smtp_server = get_smtp_server(db_session)
    system_option = SystemOption.get(db_session)

    fill_user_privileges(
        admin_console_form.ldap_default_user_privilege.choices)

    if request.method == 'POST' and \
        smtp_form.validate() and \
        admin_console_form.validate():

        if smtp_server is None:
            smtp_server = SMTPServer()
            db_session.add(smtp_server)

        smtp_server.server = smtp_form.server.data
        smtp_server.server_port = smtp_form.server_port.data if len(
            smtp_form.server_port.data) > 0 else None
        smtp_server.sender = smtp_form.sender.data
        smtp_server.use_authentication = smtp_form.use_authentication.data
        smtp_server.username = smtp_form.username.data
        if len(smtp_form.password.data) > 0:
            smtp_server.password = smtp_form.password.data
        smtp_server.secure_connection = smtp_form.secure_connection.data

        system_option.inventory_threads = admin_console_form.num_inventory_threads.data
        system_option.install_threads = admin_console_form.num_install_threads.data
        system_option.download_threads = admin_console_form.num_download_threads.data
        system_option.can_schedule = admin_console_form.can_schedule.data
        system_option.can_install = admin_console_form.can_install.data
        system_option.enable_email_notify = admin_console_form.enable_email_notify.data
        system_option.enable_inventory = admin_console_form.enable_inventory.data

        # The LDAP UI may be hidden if it is not supported.
        # In this case, the flag is not set.
        if not is_empty(admin_console_form.enable_ldap_auth.data):
            system_option.enable_ldap_auth = admin_console_form.enable_ldap_auth.data
            system_option.ldap_server_url = admin_console_form.ldap_server_url.data
            system_option.ldap_default_user_privilege = admin_console_form.ldap_default_user_privilege.data
            system_option.ldap_server_distinguished_names = admin_console_form.ldap_server_distinguished_names.data.strip(
            )

        system_option.inventory_hour = admin_console_form.inventory_hour.data
        system_option.inventory_history_per_host = admin_console_form.inventory_history_per_host.data
        system_option.download_history_per_user = admin_console_form.download_history_per_user.data
        system_option.install_history_per_host = admin_console_form.install_history_per_host.data
        system_option.total_system_logs = admin_console_form.total_system_logs.data
        system_option.enable_default_host_authentication = admin_console_form.enable_default_host_authentication.data
        system_option.default_host_authentication_choice = admin_console_form.default_host_authentication_choice.data
        system_option.enable_cco_lookup = admin_console_form.enable_cco_lookup.data
        system_option.use_utc_timezone = admin_console_form.use_utc_timezone.data
        system_option.default_host_username = admin_console_form.default_host_username.data

        if len(admin_console_form.default_host_password.data) > 0:
            system_option.default_host_password = admin_console_form.default_host_password.data

        system_option.enable_user_credential_for_host = admin_console_form.enable_user_credential_for_host.data

        db_session.commit()

        return redirect(url_for('home'))
    else:

        admin_console_form.num_inventory_threads.data = system_option.inventory_threads
        admin_console_form.num_install_threads.data = system_option.install_threads
        admin_console_form.num_download_threads.data = system_option.download_threads
        admin_console_form.can_schedule.data = system_option.can_schedule
        admin_console_form.can_install.data = system_option.can_install
        admin_console_form.enable_email_notify.data = system_option.enable_email_notify
        admin_console_form.enable_ldap_auth.data = system_option.enable_ldap_auth
        admin_console_form.ldap_server_url.data = system_option.ldap_server_url
        admin_console_form.ldap_default_user_privilege.data = system_option.ldap_default_user_privilege
        admin_console_form.ldap_server_distinguished_names.data = system_option.ldap_server_distinguished_names
        admin_console_form.enable_inventory.data = system_option.enable_inventory
        admin_console_form.inventory_hour.data = system_option.inventory_hour
        admin_console_form.inventory_history_per_host.data = system_option.inventory_history_per_host
        admin_console_form.download_history_per_user.data = system_option.download_history_per_user
        admin_console_form.install_history_per_host.data = system_option.install_history_per_host
        admin_console_form.total_system_logs.data = system_option.total_system_logs
        admin_console_form.enable_default_host_authentication.data = system_option.enable_default_host_authentication
        admin_console_form.default_host_authentication_choice.data = system_option.default_host_authentication_choice
        admin_console_form.default_host_username.data = system_option.default_host_username
        admin_console_form.enable_cco_lookup.data = system_option.enable_cco_lookup
        admin_console_form.use_utc_timezone.data = system_option.use_utc_timezone
        admin_console_form.cco_lookup_time.data = get_datetime_string(
            system_option.cco_lookup_time)
        admin_console_form.enable_user_credential_for_host.data = system_option.enable_user_credential_for_host

        if not is_empty(system_option.default_host_password):
            admin_console_form.default_host_password_placeholder = 'Use Password on File'
        else:
            admin_console_form.default_host_password_placeholder = 'No Password Specified'

        if smtp_server is not None:
            smtp_form.server.data = smtp_server.server
            smtp_form.server_port.data = smtp_server.server_port
            smtp_form.sender.data = smtp_server.sender
            smtp_form.use_authentication.data = smtp_server.use_authentication
            smtp_form.username.data = smtp_server.username
            smtp_form.secure_connection.data = smtp_server.secure_connection
            if not is_empty(smtp_server.password):
                smtp_form.password_placeholder = 'Use Password on File'
            else:
                smtp_form.password_placeholder = 'No Password Specified'

        return render_template('admin/index.html',
                               admin_console_form=admin_console_form,
                               smtp_form=smtp_form,
                               system_option=SystemOption.get(db_session),
                               is_ldap_supported=is_ldap_supported())
Exemple #19
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="")
Exemple #20
0
def api_get_cco_lookup_time():
    system_option = SystemOption.get(DBSession())
    if system_option.cco_lookup_time is not None:
        return jsonify(**{'data': [{'cco_lookup_time': get_datetime_string(system_option.cco_lookup_time)}]})
    else:
        return jsonify({'status': 'Failed'})