def api_get_host_software_profile_list(id): rows = [] dt_params = DataTableParams(request) db_session = DBSession() software_profile = get_software_profile_by_id(db_session, id) if not software_profile: response = dict() response['draw'] = dt_params.draw response['recordsTotal'] = 0 response['recordsFiltered'] = 0 response['data'] = rows return jsonify(**response) clauses = [] if len(dt_params.search_value): criteria = '%' + dt_params.search_value + '%' clauses.append(Host.hostname.like(criteria)) clauses.append(Region.name.like(criteria)) clauses.append(Host.roles.like(criteria)) clauses.append(Host.platform.like(criteria)) clauses.append(Host.software_platform.like(criteria)) clauses.append(Host.software_version.like(criteria)) query = db_session.query(Host).join(Region, Host.region_id == Region.id) total_count = query.filter(Host.software_profile_id == id).count() filtered_count = query.filter(and_(Host.software_profile_id == id), or_(*clauses)).count() columns = [ getattr(Host.hostname, dt_params.sort_order)(), getattr(Region.name, dt_params.sort_order)(), getattr(Host.roles, dt_params.sort_order)(), getattr(Host.platform, dt_params.sort_order)(), getattr(Host.software_platform, dt_params.sort_order)(), getattr(Host.software_version, dt_params.sort_order)() ] hosts = query.order_by(columns[dt_params.column_order])\ .filter(and_(Host.software_profile_id == id), or_(*clauses))\ .slice(dt_params.start_length, dt_params.start_length + dt_params.display_length).all() for host in hosts: row = dict() row['hostname'] = host.hostname row['region'] = '' if host.region is None else host.region.name row['roles'] = host.roles row['platform'] = host.platform row['software_platform'] = host.software_platform row['software_version'] = host.software_version rows.append(row) response = dict() response['draw'] = dt_params.draw response['recordsTotal'] = total_count response['recordsFiltered'] = filtered_count response['data'] = rows return jsonify(**response)
def api_get_host_software_profile_list(id): rows = [] dt_params = DataTableParams(request) db_session = DBSession() software_profile = get_software_profile_by_id(db_session, id) if not software_profile: response = dict() response['draw'] = dt_params.draw response['recordsTotal'] = 0 response['recordsFiltered'] = 0 response['data'] = rows return jsonify(**response) clauses = [] if len(dt_params.search_value): criteria = '%' + dt_params.search_value + '%' clauses.append(Host.hostname.like(criteria)) clauses.append(Region.name.like(criteria)) clauses.append(Host.roles.like(criteria)) clauses.append(Host.platform.like(criteria)) clauses.append(Host.software_platform.like(criteria)) clauses.append(Host.software_version.like(criteria)) query = db_session.query(Host).join(Region, Host.region_id == Region.id) total_count = query.filter(Host.software_profile_id == id).count() filtered_count = query.filter(and_(Host.software_profile_id == id), or_(*clauses)).count() columns = [getattr(Host.hostname, dt_params.sort_order)(), getattr(Region.name, dt_params.sort_order)(), getattr(Host.roles, dt_params.sort_order)(), getattr(Host.platform, dt_params.sort_order)(), getattr(Host.software_platform, dt_params.sort_order)(), getattr(Host.software_version, dt_params.sort_order)()] hosts = query.order_by(columns[dt_params.column_order])\ .filter(and_(Host.software_profile_id == id), or_(*clauses))\ .slice(dt_params.start_length, dt_params.start_length + dt_params.display_length).all() for host in hosts: row = dict() row['hostname'] = host.hostname row['region'] = '' if host.region is None else host.region.name row['roles'] = host.roles row['platform'] = host.platform row['software_platform'] = host.software_platform row['software_version'] = host.software_version rows.append(row) response = dict() response['draw'] = dt_params.draw response['recordsTotal'] = total_count response['recordsFiltered'] = filtered_count response['data'] = rows return jsonify(**response)
def api_get_software_profile(id): rows = [] db_session = DBSession() software_profile = get_software_profile_by_id(db_session, id) if software_profile: for package in software_profile.packages.split(','): rows.append({'package': package}) return jsonify(**{'data': rows})
def api_run_conformance_report(): db_session = DBSession() software_profile_id = request.form['software_profile_id'] match_criteria = request.form['match_criteria'] hostnames = request.form.getlist('selected_hosts[]') software_profile = get_software_profile_by_id(db_session, software_profile_id) if not software_profile: return jsonify({'status': 'Unable to locate the software profile %d' % software_profile_id}) return run_conformance_report(db_session, software_profile, match_criteria, hostnames)
def api_run_conformance_report_that_match_host_software_profile(): db_session = DBSession() software_profile_id = request.form['software_profile_id'] match_criteria = request.form['match_criteria'] software_profile = get_software_profile_by_id(db_session, software_profile_id) if not software_profile: return jsonify({'status': 'Unable to locate the software profile %d' % software_profile_id}) hostnames = [] hosts = get_hosts_by_software_profile_id(db_session, software_profile.id) for host in hosts: hostnames.append(host.hostname) return run_conformance_report(db_session, software_profile, match_criteria, hostnames)
def api_run_conformance_report(): db_session = DBSession() software_profile_id = request.form['software_profile_id'] match_criteria = request.form['match_criteria'] hostnames = request.form.getlist('selected_hosts[]') software_profile = get_software_profile_by_id(db_session, software_profile_id) if not software_profile: return jsonify({ 'status': 'Unable to locate the software profile %d' % software_profile_id }) return run_conformance_report(db_session, software_profile, match_criteria, hostnames)
def api_run_conformance_report_that_match_host_software_profile(): db_session = DBSession() software_profile_id = request.form['software_profile_id'] match_criteria = request.form['match_criteria'] software_profile = get_software_profile_by_id(db_session, software_profile_id) if not software_profile: return jsonify({ 'status': 'Unable to locate the software profile %d' % software_profile_id }) hostnames = [] hosts = get_hosts_by_software_profile_id(db_session, software_profile.id) for host in hosts: hostnames.append(host.hostname) return run_conformance_report(db_session, software_profile, match_criteria, hostnames)
def api_get_host_dashboard_cookie(hostname): db_session = DBSession() host = get_host(db_session, hostname) rows = [] if host is not None: software_profile = get_software_profile_by_id(db_session, host.software_profile_id) system_option = SystemOption.get(db_session) row = {} connection_param = host.connection_param[0] row['hostname'] = host.hostname row['region'] = host.region.name if host.region is not None else UNKNOWN row['location'] = host.location row['roles'] = host.roles row['platform'] = host.platform row['software_platform'] = host.software_platform row['software_version'] = host.software_version row['host_or_ip'] = connection_param.host_or_ip row['username'] = connection_param.username row['connection'] = connection_param.connection_type row['port_number'] = connection_param.port_number row['created_by'] = host.created_by row['software_profile_name'] = '' if software_profile is None else software_profile.name if connection_param.jump_host is not None: row['jump_host'] = connection_param.jump_host.hostname # Last inventory successful time inventory_job = host.inventory_job[0] row['last_successful_inventory_elapsed_time'] = get_last_successful_inventory_elapsed_time( host) row['last_successful_inventory_time'] = inventory_job.last_successful_time row['status'] = inventory_job.status row['can_schedule'] = system_option.can_schedule rows.append(row) return jsonify(**{'data': rows})
def api_get_host_dashboard_cookie(hostname): db_session = DBSession() host = get_host(db_session, hostname) rows = [] if host is not None: software_profile = get_software_profile_by_id(db_session, host.software_profile_id) system_option = SystemOption.get(db_session) row = {} connection_param = host.connection_param[0] row['hostname'] = host.hostname row['region'] = host.region.name if host.region is not None else UNKNOWN row['location'] = host.location row['roles'] = host.roles row['platform'] = host.platform row['software_platform'] = host.software_platform row['software_version'] = host.software_version row['host_or_ip'] = connection_param.host_or_ip row['username'] = connection_param.username row['connection'] = connection_param.connection_type row['port_number'] = connection_param.port_number row['created_by'] = host.created_by row['software_profile_name'] = '' if software_profile is None else software_profile.name if connection_param.jump_host is not None: row['jump_host'] = connection_param.jump_host.hostname # Last inventory successful time inventory_job = host.inventory_job[0] row['last_successful_inventory_elapsed_time'] = get_last_successful_inventory_elapsed_time(host) row['last_successful_inventory_time'] = inventory_job.last_successful_time row['status'] = inventory_job.status row['can_schedule'] = system_option.can_schedule rows.append(row) return jsonify(**{'data': rows})
def api_import_hosts(): region_id = int(request.form['region_id']) jump_host_id = int(request.form['jump_host_id']) software_profile_id = int(request.form['software_profile_id']) data_list = request.form['data_list'] db_session = DBSession() if region_id == -1: return jsonify({'status': 'Region has not been specified.'}) if region_id > 0: region = get_region_by_id(db_session, region_id) if region is None: return jsonify({'status': 'Region is no longer exists in the database.'}) if jump_host_id > 0: jump_host = get_jump_host_by_id(db_session, jump_host_id) if jump_host is None: return jsonify({'status': 'Jump Host is no longer exists in the database.'}) if software_profile_id > 0: software_profile = get_software_profile_by_id(db_session, software_profile_id) if software_profile is None: return jsonify({'status': 'Software Profile is no longer exists in the database.'}) error = [] reader = csv.reader(data_list.splitlines(), delimiter=',') header_row = next(reader) # header_row: ['hostname', 'location', 'roles', 'ip', 'username', 'password', 'connection', 'port'] # Check mandatory data fields if HEADER_FIELD_HOSTNAME not in header_row: error.append('"hostname" is missing in the header.') if HEADER_FIELD_IP not in header_row: error.append('"ip" is missing in the header.') if HEADER_FIELD_CONNECTION not in header_row: error.append('"connection" is missing in the header.') for header_field in header_row: if header_field not in HEADER_FIELDS: error.append('"' + header_field + '" is not a correct header field.') if error: return jsonify({'status': '\n'.join(error)}) error = [] data_list = list(reader) region_dict = get_region_name_to_id_dict(db_session) # Check if each row has the same number of data fields as the header row = 2 for row_data in data_list: if len(row_data) != len(header_row): error.append('line {} has wrong number of data fields - {}.'.format(row, row_data)) else: hostname = get_acceptable_string(get_row_data(row_data, header_row, HEADER_FIELD_HOSTNAME)) if is_empty(hostname): error.append('line {} has invalid hostname - {}.'.format(row, row_data)) # Validate the connection type connection_type = get_row_data(row_data, header_row, HEADER_FIELD_CONNECTION) if is_empty(connection_type) or connection_type not in [ConnectionType.TELNET, ConnectionType.SSH]: error.append('line {} has a wrong connection type (should either be "telnet" or "ssh") - {}.'.format(row, row_data)) region_name = get_acceptable_string(get_row_data(row_data, header_row, HEADER_FIELD_REGION)) if region_name is not None: # No blank region is allowed if len(region_name) == 0: error.append('line {} has no region specified - {}.'.format(row, row_data)) else: if region_name not in region_dict.keys(): # Create the new region try: region = Region(name=region_name, created_by=current_user.username) db_session.add(region) db_session.commit() # Add to region dictionary for caching purpose. region_dict[region_name] = region.id except Exception as e: logger.exception('api_import_hosts() hit exception') error.append('Unable to create region {} - {}.'.format(region_name, e.message)) row += 1 if error: return jsonify({'status': '\n'.join(error)}) # Import the data row = 2 for row_data in data_list: try: created_by = current_user.username hostname = get_acceptable_string(get_row_data(row_data, header_row, HEADER_FIELD_HOSTNAME)) # Check if the host already exists in the database. host = get_host(db_session, hostname) region_name = get_acceptable_string(get_row_data(row_data, header_row, HEADER_FIELD_REGION)) if region_name is None: alternate_region_id = region_id else: alternate_region_id = region_dict[region_name] location = get_row_data(row_data, header_row, HEADER_FIELD_LOCATION) if host and location is None: location = host.location roles = get_row_data(row_data, header_row, HEADER_FIELD_ROLES) if host and roles is None: roles = host.roles host_or_ip = get_row_data(row_data, header_row, HEADER_FIELD_IP) if host and host_or_ip is None: host_or_ip = host.connection_param[0].host_or_ip connection_type = get_row_data(row_data, header_row, HEADER_FIELD_CONNECTION) if host and connection_type is None: connection_type = host.connection_param[0].connection_type username = get_row_data(row_data, header_row, HEADER_FIELD_USERNAME) if host and username is None: username = host.connection_param[0].username password = get_row_data(row_data, header_row, HEADER_FIELD_PASSWORD) if host and password is None: password = host.connection_param[0].password enable_password = get_row_data(row_data, header_row, HEADER_FIELD_ENABLE_PASSWORD) if host and enable_password is None: enable_password = host.connection_param[0].enable_password port_number = get_row_data(row_data, header_row, HEADER_FIELD_PORT) if host and port_number is None: port_number = host.connection_param[0].port_number # If no software profile is selected, retain the existing one instead of overwriting it. if host and (software_profile_id is None or software_profile_id <= 0): alternate_software_profile_id = host.software_profile_id else: alternate_software_profile_id = software_profile_id # If no jump host is selected, retain the existing one instead of overwriting it. if host and (jump_host_id is None or jump_host_id <= 0): alternate_jump_host_id = host.connection_param[0].jump_host_id else: alternate_jump_host_id = jump_host_id create_or_update_host(db_session, hostname, alternate_region_id, location, roles, alternate_software_profile_id, connection_type, host_or_ip, username, password, enable_password, port_number, alternate_jump_host_id, created_by, host) except Exception as e: return jsonify({'status': 'Line {} - {} - {}.'.format(row, e.message, row_data)}) row += 1 return jsonify({'status': 'OK'})