Beispiel #1
0
def edit_section(server_id):
	body = request.body.getvalue().decode('utf-8')
	section_name = request.headers.get('section-name')
	save = request.headers.get('action')
	token = request.headers.get('token')
	servers = check_permit_to_server(server_id)
	hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
	login, group_id = sql.get_username_groupid_from_api_token(token)

	if save == '':
		save = 'save'
	elif save == 'restart':
		save = ''
	elif save == 'reload':
		save = 'reload'

	for s in servers:
		ip = s[2]
		cfg = '/tmp/' + ip + '.cfg'

		out = funct.get_config(ip, cfg)
		start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name)

		returned_config = funct.rewrite_section(start_line, end_line, cfg, body)

		try:
			cfg_for_save = hap_configs_dir + ip + "-" + funct.get_data('config') + ".cfg"

			try:
				with open(cfg, "w") as conf:
					conf.write(returned_config)
				return_mess = 'section has been updated'
				os.system("/bin/cp %s %s" % (cfg, cfg_for_save))
				out = funct.master_slave_upload_and_restart(ip, cfg, save, login=login)
				funct.logging('localhost', " section " + section_name + " has been edited via API", login=login)
				funct.logging(ip, 'Section ' + section_name + ' has been edited via API', haproxywi=1, login=login,
							  keep_history=1, service='haproxy')

				if out:
					return_mess = out
			except IOError:
				return_mess = "cannot upload config"

			data = {server_id: return_mess}
		except Exception as e:
			data = {server_id: {"error": str(e)}}
			return dict(error=data)

		return dict(config=data)
Beispiel #2
0
def del_acl(server_id):
	body = request.body.getvalue().decode('utf-8')
	json_loads = json.loads(body)
	save = json_loads['action']
	section_name = json_loads['section-name']

	acl = generate_acl()
	servers = check_permit_to_server(server_id)
	status = ''

	for s in servers:
		cfg = '/tmp/' + s[2] + '.cfg'
		server_ip = s[2]
	try:
		out = funct.get_config(server_ip, cfg)
		start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name)
	except Exception as e:
		status = str(e)

	try:
		config_new_read = ''
		for line in config_read.split('\n'):
			if not line.startswith(acl):
				if line != '':
					config_new_read += line + '\n'
	except Exception as e:
		status = 'Cannot delete ACL: ' + str(e)

	try:
		config = funct.rewrite_section(start_line, end_line, cfg, config_new_read)
		try:
			with open(cfg, "w") as conf:
				conf.write(config)
		except IOError as e:
			status = "Cannot read import config file: " + str(e)
	except Exception as e:
		status = 'Cannot delete ACL: ' + str(e)

	try:
		out = funct.master_slave_upload_and_restart(server_ip, cfg, just_save=save)
		if out != '':
			status = out
		else:
			status = 'ACL has been deleted'
	except Exception as e:
		status = str(e)

	return dict(acl=status)
Beispiel #3
0
def add_acl(server_id):
	body = request.body.getvalue().decode('utf-8')
	json_loads = json.loads(body)
	save = json_loads['action']
	section_name = json_loads['section-name']

	acl = generate_acl(with_newline=1)
	servers = check_permit_to_server(server_id)
	status = ''

	for s in servers:
		cfg = '/tmp/' + s[2] + '.cfg'
		server_ip = s[2]

	try:
		out = funct.get_config(server_ip, cfg)
		start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name)
	except Exception as e:
		status = "Cannot read section: " + str(e)

	try:
		config_read += acl
		config = funct.rewrite_section(start_line, end_line, cfg, config_read)
		try:
			with open(cfg, "w") as conf:
				conf.write(config)
		except IOError as e:
			status = "Cannot read import config file: " + str(e)
	except Exception as e:
		status = str(e)

	try:
		out = funct.master_slave_upload_and_restart(server_ip, cfg, just_save=save)
		if out != '':
			status = out
		else:
			status = 'ACL has been added'
	except Exception as e:
		status = str(e)

	data = {'acl':status}
	return dict(data)
Beispiel #4
0
	try:
		funct.logging(serv, "config.py edited config")
	except Exception:
		pass
		
	config = form.getvalue('config')
	oldcfg = form.getvalue('oldconfig')
	save = form.getvalue('save')
	start_line = form.getvalue('start_line')
	end_line = form.getvalue('end_line')
	aftersave = 1

	if save == 'delete':
		config = ''

	config = funct.rewrite_section(start_line, end_line, oldcfg, config)
	
	try:
		with open(cfg, "w") as conf:
			conf.write(config)
	except IOError:
		error = "Can't read import config file"
	
	stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save)
		
	funct.diff_config(oldcfg, cfg)
	
	os.system("/bin/rm -f " + hap_configs_dir + "*.old")


template = template.render(h2=1, title="Working with HAProxy configs",