def current(self): """Scroll to the current position in the buffer. The current poistion is determined by the first tag found, otherwise it just scrolls to the mark position. """ # Try to find a mark ('executing' or 'error') and scroll to it start, end = self.buf.get_bounds() res = self.buf.forward_iter_to_source_mark(start, None) if res: self.scroll_to_lineno(start.get_line()) return # If we can't find a mark then look for tags # It might be better to scroll to the mark than these tags for tag in ('executing', 'queued', 'error', 'done'): try: start, end = common.get_region(self.buf, tag) self.scroll_to_lineno(start.get_line()) return except common.TagError: continue #common.view.popup_error("Sorry, cannot find any region of interest.") # Scroll to mark, if any res = self.tw.scroll_mark_onscreen(self.mark)
def api_get_regions(request): """ GET: http://localhost:5000/api/v1/regions http://localhost:5000/api/v1/regions?name=Region_1 """ validate_url_parameters(request, [KEY_REGION_NAME]) rows = [] db_session = DBSession region_name = request.args.get(KEY_REGION_NAME) if region_name: region = get_region(db_session, region_name) if region is None: raise ValueError("Region '{}' does not exist in the database.".format(region_name)) regions = [region] else: regions = get_region_list(db_session) for region in regions: if region is not None: row = dict() row[KEY_REGION_NAME] = region.name row[KEY_SERVER_REPOSITORIES] = [s.hostname for s in region.servers] rows.append(row) return jsonify(**{RESPONSE_ENVELOPE: {'region_list': rows}})
def api_get_regions(request): """ GET: http://localhost:5000/api/v1/regions http://localhost:5000/api/v1/regions?name=Region_1 """ validate_url_parameters(request, [KEY_REGION_NAME]) rows = [] db_session = DBSession region_name = request.args.get(KEY_REGION_NAME) if region_name: region = get_region(db_session, region_name) if region is None: raise ValueError( "Region '{}' does not exist in the database.".format( region_name)) regions = [region] else: regions = get_region_list(db_session) for region in regions: if region is not None: row = dict() row[KEY_REGION_NAME] = region.name row[KEY_SERVER_REPOSITORIES] = [s.hostname for s in region.servers] rows.append(row) return jsonify(**{RESPONSE_ENVELOPE: {'region_list': rows}})
def append_error(self, page, tagname, textstr): tagname = str(tagname) txtbuf = page.buf start, end = common.get_region(txtbuf, tagname) txtbuf.insert_with_tags_by_name(end, textstr, tagname) self.change_text(page, tagname, 'error')
def region_create(): if not can_create(current_user): abort(401) form = RegionForm(request.form) if request.method == 'POST' and form.validate(): db_session = DBSession() region = get_region(db_session, form.region_name.data) if region is not None: return render_template('region/edit.html', form=form, duplicate_error=True) # Compose a list of server hostnames server_names = [ get_server_by_id(db_session, id).hostname for id in request.form.getlist('selected-servers') ] try: create_or_update_region(db_session=db_session, region_name=form.region_name.data, server_repositories=",".join(server_names), created_by=current_user.username) except Exception as e: db_session.rollback() logger.exception("region_create() encountered an exception: " + e.message) return redirect(url_for('home')) return render_template('region/edit.html', form=form)
def api_create_regions(request): """ POST: http://localhost:5000/api/v1/regions BODY: [{ "name": "Region_1" "server_repository": ["Repository1", "Repository2"] },{ "name": "Region_2" }] """ rows = [] db_session = DBSession() error_found = False json_list = convert_json_request_to_list(request) for data in json_list: row = dict() try: validate_required_keys_in_dict(data, [KEY_REGION_NAME]) region_name = get_acceptable_string(data[KEY_REGION_NAME]) row[KEY_REGION_NAME] = region_name if region_name is None or len(region_name) == 0: raise ValueError("'{}' is an invalid region name.".format(data[KEY_REGION_NAME])) validate_acceptable_keys_in_dict(data, [KEY_REGION_NAME, KEY_SERVER_REPOSITORIES]) # If the server_repositories is not in the json, it will return None server_repositories = convert_value_to_list(data, KEY_SERVER_REPOSITORIES) region = get_region(db_session, region_name) if region is not None and server_repositories is None: server_repositories = get_region_server_name_list(region) create_or_update_region(db_session=db_session, region_name=region_name, server_repositories=None if server_repositories is None else ','.join(server_repositories), created_by=g.api_user.username, region=region) row[RESPONSE_STATUS] = APIStatus.SUCCESS except Exception as e: row[RESPONSE_STATUS] = APIStatus.FAILED row[RESPONSE_STATUS_MESSAGE] = e.message error_found = True rows.append(row) return jsonify(**{RESPONSE_ENVELOPE: {'region_list': rows}}), (HTTP_OK if not error_found else HTTP_MULTI_STATUS_ERROR)
def insert_line(self, page, tagname, newtag, level, textstr): tagname = str(tagname) txtbuf = page.buf start, end = common.get_region(txtbuf, tagname) end2 = end.copy() end2.forward_to_line_end() if end.get_line() != end2.get_line(): end2 = end.copy() txtbuf.create_tag(newtag, foreground="black") prefix = '\n' + (' ' * level) + ' ' txtbuf.insert_with_tags_by_name(end2, prefix, 'code') txtbuf.insert_with_tags_by_name(end2, textstr, newtag) txtbuf.insert_with_tags_by_name(end2, ' ', 'code')
def replace_text(self, page, tagname, textstr, start_offset=0): #print "replacing '%s' on %s" % (textstr, tagname) tagname = str(tagname) txtbuf = page.buf #print "getting region" start, end = common.get_region(txtbuf, tagname) start.forward_chars(start_offset) text = txtbuf.get_text(start, end) #print "deleting %s" % (text) txtbuf.delete(start, end) #print "inserting %s" % (textstr) txtbuf.insert_with_tags_by_name(start, textstr, tagname) # Scroll the view to this area page.tw.scroll_to_iter(start, 0.1)
def change_text(self, page, tagname, key): tagname = str(tagname) tag = page.tagtbl.lookup(tagname) if not tag: raise TagError("Tag not found: '%s'" % tagname) bnch = common.monitor_tags[key] for key, val in bnch.items(): tag.set_property(key, val) #page.tw.tag_raise(ast_num) # Scroll the view to this area start, end = common.get_region(page.buf, tagname) page.tw.scroll_to_iter(start, 0.1)
def update_time(self, page, tagname, vals, time_s): if not self.show_times: return tagname = str(tagname) txtbuf = page.buf start, end = common.get_region(txtbuf, tagname) if vals.has_key('time_added'): length = vals['time_added'] end = start.copy() end.forward_chars(length) txtbuf.delete(start, end) vals['time_added'] = len(time_s) txtbuf.insert_with_tags_by_name(start, time_s, tagname)
def api_import_hosts(): importable_header = [ HEADER_FIELD_HOSTNAME, HEADER_FIELD_REGION, HEADER_FIELD_ROLES, HEADER_FIELD_IP, HEADER_FIELD_USERNAME, HEADER_FIELD_PASSWORD, HEADER_FIELD_CONNECTION, HEADER_FIELD_PORT ] region_id = request.form['region'] data_list = request.form['data_list'] db_session = DBSession() selected_region = get_region_by_id(db_session, region_id) if selected_region is None: return jsonify( {'status': 'Region is no longer exists in the database.'}) # Check mandatory data fields error = [] reader = csv.reader(data_list.splitlines(), delimiter=',') header_row = next(reader) 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 importable_header: error.append('"' + header_field + '" is not a correct header field.') if error: return jsonify({'status': ','.join(error)}) # Check if each row has the same number of data fields as the header error = [] data_list = list(reader) row = 2 COLUMN_CONNECTION = get_column_number(header_row, HEADER_FIELD_CONNECTION) COLUMN_REGION = get_column_number(header_row, HEADER_FIELD_REGION) for row_data in data_list: if len(row_data) > 0: if len(row_data) != len(header_row): error.append('line %d has wrong number of data fields.' % row) else: if COLUMN_CONNECTION >= 0: # Validate the connection type data_field = row_data[COLUMN_CONNECTION] if data_field != ConnectionType.TELNET and data_field != ConnectionType.SSH: error.append( 'line %d has a wrong connection type (should either be "telnet" or "ssh").' % row) if COLUMN_REGION >= 0: # Create a region if necessary data_field = get_acceptable_string(row_data[COLUMN_REGION]) region = get_region(db_session, data_field) if region is None and data_field: try: db_session.add( Region(name=data_field, created_by=current_user.username)) db_session.commit() except Exception: db_session.rollback() error.append('Unable to create region %s.' % data_field) row += 1 if error: return jsonify({'status': ','.join(error)}) # Import the data error = [] im_regions = {} for data in data_list: if len(data) == 0: continue db_host = None im_host = Host() im_host.region_id = selected_region.id im_host.created_by = current_user.username im_host.inventory_job.append(InventoryJob()) im_host.context.append(HostContext()) im_host.connection_param.append(ConnectionParam()) im_host.connection_param[0].username = '' im_host.connection_param[0].password = '' im_host.connection_param[0].port_number = '' for column in range(len(header_row)): header_field = header_row[column] data_field = data[column].strip() if header_field == HEADER_FIELD_HOSTNAME: hostname = get_acceptable_string(data_field) db_host = get_host(db_session, hostname) im_host.hostname = hostname elif header_field == HEADER_FIELD_REGION: region_name = get_acceptable_string(data_field) if region_name in im_regions: im_host.region_id = im_regions[region_name] else: region = get_region(db_session, region_name) if region is not None: im_host.region_id = region.id # Saved for later lookup im_regions[region_name] = region.id elif header_field == HEADER_FIELD_ROLES: im_host.roles = remove_extra_spaces(data_field) elif header_field == HEADER_FIELD_IP: im_host.connection_param[0].host_or_ip = remove_extra_spaces( data_field) elif header_field == HEADER_FIELD_USERNAME: username = get_acceptable_string(data_field) im_host.connection_param[0].username = username elif header_field == HEADER_FIELD_PASSWORD: im_host.connection_param[0].password = data_field elif header_field == HEADER_FIELD_CONNECTION: im_host.connection_param[0].connection_type = data_field elif header_field == HEADER_FIELD_PORT: im_host.connection_param[0].port_number = remove_extra_spaces( data_field) # Import host already exists in the database, just update it if db_host is not None: db_host.created_by = im_host.created_by db_host.region_id = im_host.region_id if HEADER_FIELD_ROLES in header_row: db_host.roles = im_host.roles if HEADER_FIELD_IP in header_row: db_host.connection_param[ 0].host_or_ip = im_host.connection_param[0].host_or_ip if HEADER_FIELD_USERNAME in header_row: db_host.connection_param[ 0].username = im_host.connection_param[0].username if HEADER_FIELD_PASSWORD in header_row: db_host.connection_param[ 0].password = im_host.connection_param[0].password if HEADER_FIELD_CONNECTION in header_row: db_host.connection_param[ 0].connection_type = im_host.connection_param[ 0].connection_type if HEADER_FIELD_PORT in header_row: db_host.connection_param[ 0].port_number = im_host.connection_param[0].port_number else: # Add the import host db_session.add(im_host) if error: return jsonify({'status': error}) else: db_session.commit() return jsonify({'status': 'OK'})
def api_get_hosts(request): """ GET: http://localhost:5000/api/v1/hosts http://localhost:5000/api/v1/hosts?hostname=Host_1 http://localhost:5000/api/v1/hosts?region=SJ Labs http://localhost:5000/api/v1/hosts?region=SJ Labs&page=2 http://localhost:5000/api/v1/hosts?region=SJ%20Labs&family=ASR9K """ validate_url_parameters(request, [KEY_HOSTNAME, KEY_REGION, KEY_FAMILY, 'page']) page = 1 clauses = [] db_session = DBSession hostname = request.args.get(KEY_HOSTNAME) if hostname: host = get_host(db_session, hostname) if host is None: raise ValueError( "Host '{}' does not exist in the database.".format(hostname)) hosts = [host] else: try: page = int( request.args.get('page')) if request.args.get('page') else 1 if page <= 0: page = 1 except Exception: return failed_response('page must be an numeric value') region_name = request.args.get(KEY_REGION) if region_name: region = get_region(db_session, region_name) if region: clauses.append(Host.region_id == region.id) else: return failed_response( "Region '{}' does not exist in the database.".format( region_name)) family = request.args.get(KEY_FAMILY) if family: clauses.append(Host.family == family) hosts = get_hosts_by_page(db_session, clauses, page) region_dict = get_region_id_to_name_dict(db_session) jump_host_dict = get_jump_host_id_to_name_dict(db_session) software_profile_dict = get_software_profile_id_to_name_dict(db_session) rows = [] for host in hosts: row = dict() row[KEY_HOSTNAME] = host.hostname row[KEY_REGION] = check_none(region_dict.get(host.region_id)) row[KEY_ROLES] = [] if is_empty(host.roles) else host.roles.split(',') connection_param = check_none(host.connection_param[0]) row[KEY_FAMILY] = check_none(host.family) row[KEY_CHASSIS] = check_none(host.platform) row[KEY_SOFTWARE_PLATFORM] = check_none(host.software_platform) row[KEY_SOFTWARE_VERSION] = check_none(host.software_version) row[KEY_OS_TYPE] = check_none(host.os_type) row[KEY_LOCATION] = check_none(host.location) row[KEY_SOFTWARE_PROFILE] = check_none( software_profile_dict.get(host.software_profile_id)) if connection_param: row[KEY_TS_OR_IP] = [] if is_empty( connection_param.host_or_ip ) else connection_param.host_or_ip.split(',') row[KEY_CONNECTION_TYPE] = check_none( connection_param.connection_type) row[KEY_USERNAME] = check_none(connection_param.username) row[KEY_PORT_NUMBER] = [] if is_empty( connection_param.port_number ) else connection_param.port_number.split(',') row[KEY_JUMP_HOST] = check_none( jump_host_dict.get(connection_param.jump_host_id)) rows.append(row) total_pages = get_total_pages(db_session, Host, clauses) return jsonify( **{ RESPONSE_ENVELOPE: { 'host_list': rows }, 'current_page': page, 'total_pages': total_pages })
def api_import_hosts(): importable_header = [HEADER_FIELD_HOSTNAME, HEADER_FIELD_REGION, HEADER_FIELD_ROLES, HEADER_FIELD_IP, HEADER_FIELD_USERNAME, HEADER_FIELD_PASSWORD, HEADER_FIELD_CONNECTION, HEADER_FIELD_PORT] region_id = request.form['region'] data_list = request.form['data_list'] db_session = DBSession() selected_region = get_region_by_id(db_session, region_id) if selected_region is None: return jsonify({'status': 'Region is no longer exists in the database.'}) # Check mandatory data fields error = [] reader = csv.reader(data_list.splitlines(), delimiter=',') header_row = next(reader) 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 importable_header: error.append('"' + header_field + '" is not a correct header field.') if error: return jsonify({'status': ','.join(error)}) # Check if each row has the same number of data fields as the header error = [] data_list = list(reader) row = 2 COLUMN_CONNECTION = get_column_number(header_row, HEADER_FIELD_CONNECTION) COLUMN_REGION = get_column_number(header_row, HEADER_FIELD_REGION) for row_data in data_list: if len(row_data) > 0: if len(row_data) != len(header_row): error.append('line %d has wrong number of data fields.' % row) else: if COLUMN_CONNECTION >= 0: # Validate the connection type data_field = row_data[COLUMN_CONNECTION] if data_field != ConnectionType.TELNET and data_field != ConnectionType.SSH: error.append('line %d has a wrong connection type (should either be "telnet" or "ssh").' % row) if COLUMN_REGION >= 0: # Create a region if necessary data_field = get_acceptable_string(row_data[COLUMN_REGION]) region = get_region(db_session, data_field) if region is None and data_field: try: db_session.add(Region(name=data_field, created_by=current_user.username)) db_session.commit() except Exception: db_session.rollback() error.append('Unable to create region %s.' % data_field) row += 1 if error: return jsonify({'status': ','.join(error)}) # Import the data error = [] im_regions = {} for data in data_list: if len(data) == 0: continue db_host = None im_host = Host() im_host.region_id = selected_region.id im_host.created_by = current_user.username im_host.inventory_job.append(InventoryJob()) im_host.context.append(HostContext()) im_host.connection_param.append(ConnectionParam()) im_host.connection_param[0].username = '' im_host.connection_param[0].password = '' im_host.connection_param[0].port_number = '' for column in range(len(header_row)): header_field = header_row[column] data_field = data[column].strip() if header_field == HEADER_FIELD_HOSTNAME: hostname = get_acceptable_string(data_field) db_host = get_host(db_session, hostname) im_host.hostname = hostname elif header_field == HEADER_FIELD_REGION: region_name = get_acceptable_string(data_field) if region_name in im_regions: im_host.region_id = im_regions[region_name] else: region = get_region(db_session, region_name) if region is not None: im_host.region_id = region.id # Saved for later lookup im_regions[region_name] = region.id elif header_field == HEADER_FIELD_ROLES: im_host.roles = remove_extra_spaces(data_field) elif header_field == HEADER_FIELD_IP: im_host.connection_param[0].host_or_ip = remove_extra_spaces(data_field) elif header_field == HEADER_FIELD_USERNAME: username = get_acceptable_string(data_field) im_host.connection_param[0].username = username elif header_field == HEADER_FIELD_PASSWORD: im_host.connection_param[0].password = data_field elif header_field == HEADER_FIELD_CONNECTION: im_host.connection_param[0].connection_type = data_field elif header_field == HEADER_FIELD_PORT: im_host.connection_param[0].port_number = remove_extra_spaces(data_field) # Import host already exists in the database, just update it if db_host is not None: db_host.created_by = im_host.created_by db_host.region_id = im_host.region_id if HEADER_FIELD_ROLES in header_row: db_host.roles = im_host.roles if HEADER_FIELD_IP in header_row: db_host.connection_param[0].host_or_ip = im_host.connection_param[0].host_or_ip if HEADER_FIELD_USERNAME in header_row: db_host.connection_param[0].username = im_host.connection_param[0].username if HEADER_FIELD_PASSWORD in header_row: db_host.connection_param[0].password = im_host.connection_param[0].password if HEADER_FIELD_CONNECTION in header_row: db_host.connection_param[0].connection_type = im_host.connection_param[0].connection_type if HEADER_FIELD_PORT in header_row: db_host.connection_param[0].port_number = im_host.connection_param[0].port_number else: # Add the import host db_session.add(im_host) if error: return jsonify({'status': error}) else: db_session.commit() return jsonify({'status': 'OK'})
def api_create_regions(request): """ POST: http://localhost:5000/api/v1/regions BODY: [{ "name": "Region_1" "server_repository": ["Repository1", "Repository2"] },{ "name": "Region_2" }] """ rows = [] db_session = DBSession() error_found = False json_list = convert_json_request_to_list(request) for data in json_list: row = dict() try: validate_required_keys_in_dict(data, [KEY_REGION_NAME]) region_name = get_acceptable_string(data[KEY_REGION_NAME]) row[KEY_REGION_NAME] = region_name if region_name is None or len(region_name) == 0: raise ValueError("'{}' is an invalid region name.".format( data[KEY_REGION_NAME])) validate_acceptable_keys_in_dict( data, [KEY_REGION_NAME, KEY_SERVER_REPOSITORIES]) # If the server_repositories is not in the json, it will return None server_repositories = convert_value_to_list( data, KEY_SERVER_REPOSITORIES) region = get_region(db_session, region_name) if region is not None and server_repositories is None: server_repositories = get_region_server_name_list(region) create_or_update_region( db_session=db_session, region_name=region_name, server_repositories=None if server_repositories is None else ','.join(server_repositories), created_by=g.api_user.username, region=region) row[RESPONSE_STATUS] = APIStatus.SUCCESS except Exception as e: row[RESPONSE_STATUS] = APIStatus.FAILED row[RESPONSE_STATUS_MESSAGE] = e.message error_found = True rows.append(row) return jsonify(**{RESPONSE_ENVELOPE: { 'region_list': rows }}), (HTTP_OK if not error_found else HTTP_MULTI_STATUS_ERROR)
def api_get_nonlocal_servers_by_region_name(region_name): db_session = DBSession() region = get_region(db_session, region_name) return get_nonlocal_servers(db_session, region)