Pavel Loginov 3 years ago
parent c135cb0876
commit 4560152621

@ -57,6 +57,7 @@ def index():
'haproxy/<id,hostname,ip>/log':'show HAProxy log by id or hostname or ip. May to have config next headers: rows(format INT) default: 10 grep, waf(if needs WAF log) default: 0, start_hour(format: 24) default: 00, start_minute, end_hour(format: 24) default: 24, end_minute. METHOD: GET',
'haproxy/<id,hostname,ip>/section':'show certain section, headers: section-name. METHOD: GET',
'haproxy/<id,hostname,ip>/section/add':'add section to the HAProxy config by id or hostname or ip. Has to have config header with section and action header for action after upload. Section header must consist type: listen, frontend, etc. Action header accepts next value: save, test, reload and restart. Can be empty for just save. METHOD: POST',
'haproxy/<id,hostname,ip>/section/edit':'edit section in the HAProxy config by id or hostname or ip. Has to have config header with section, action header for action after upload and body of a new section configuration. Section header must consist type: listen, frontend, etc. Action header accepts next value: save, test, reload and restart. Can be empty for just save. METHOD: POST',
'haproxy/<id,hostname,ip>/acl':'add acl to certain section. Must be JSON body: "section-name", "if", "then", "if_value", "then_value" and "action" for action after upload. Action accepts next value: "save", "test", "reload" and "restart". METHOD: POST',
'haproxy/<id,hostname,ip>/acl':'delete acl to certain section. Must be JSON body: "section-name", "if", "then", "if_value", "then_value" and "action" for action after upload. Action accepts next value: "save", "test", "reload" and "restart". METHOD: DELETE'
}
@ -185,6 +186,14 @@ def callback(haproxy_id):
return api_funct.add_to_config(haproxy_id)
@route('/haproxy/<haproxy_id>/section/edit', method=['POST'])
@route('/haproxy/<haproxy_id:int>/section/edit', method=['POST'])
def callback(haproxy_id):
if not check_login(required_service=1):
return dict(error=_error_auth)
return api_funct.edit_section(haproxy_id)
@route('/haproxy/<haproxy_id>/acl', method=['POST'])
def add_acl(haproxy_id):
if not check_login(required_service=1):

@ -239,6 +239,56 @@ def get_section(server_id):
return dict(section=data)
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)
def upload_config(server_id):
data = {}
@ -367,6 +417,7 @@ def show_log(server_id):
return dict(log=data)
def add_acl(server_id):
body = request.body.getvalue().decode('utf-8')
json_loads = json.loads(body)

Loading…
Cancel
Save