Module: check_mk
Branch: master
Commit: cf845b2e317f9b2344255fa59194c7d449d0d0d8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cf845b2e317f9b…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Jul 2 10:45:53 2018 +0200
Refactored user profile replication to WatoWebApiMode
Change-Id: Ia0323fe9f8bc66c4037b9c029ab1994860d98380
---
web/htdocs/js/wato.js | 42 +++++++++++++++++++++++++++++++-----------
web/htdocs/wato.py | 46 ++++++++++++++++++++++++++++++++++++++++++----
web/htdocs/watolib.py | 37 -------------------------------------
web/plugins/pages/wato.py | 3 +--
4 files changed, 74 insertions(+), 54 deletions(-)
diff --git a/web/htdocs/js/wato.js b/web/htdocs/js/wato.js
index 4b95ef5..29d7283 100644
--- a/web/htdocs/js/wato.js
+++ b/web/htdocs/js/wato.js
@@ -886,8 +886,28 @@ function finish_activation()
var profile_replication_progress = new Array();
function wato_do_profile_replication(siteid, est, progress_text) {
- get_url("wato_ajax_profile_repl.py?site=" + siteid,
- wato_profile_replication_result, siteid);
+ call_ajax("wato_ajax_profile_repl.py", {
+ response_handler : function (handler_data, response_json) {
+ var response = JSON.parse(response_json);
+ var success = response.result_code === 0;
+ var msg = response.result;
+
+ set_profile_replication_result(handler_data["site_id"], success,
msg);
+ },
+ error_handler : function (handler_data, status_code, error_msg) {
+ set_profile_replication_result(handler_data["site_id"], False,
+ "Failed to perform profile replication [" + status_code +
"]: " + error_msg);
+ },
+ method : "POST",
+ post_data : "request=" + encodeURIComponent(JSON.stringify({
+ "site": siteid
+ })),
+ handler_data : {
+ "site_id": siteid
+ },
+ add_ajax_id : false
+ });
+
profile_replication_progress[siteid] = 20; // 10 of 10 10ths
setTimeout("profile_replication_step('"+siteid+"',
"+est+", '"+progress_text+"');", est/20);
}
@@ -916,15 +936,15 @@ function profile_replication_step(siteid, est, progress_text) {
}
}
-// g_num_replsites is set by the page code in wato.py to the number async jobs started
-// in total
-function wato_profile_replication_result(siteid, code) {
- profile_replication_progress[siteid] = 0;
- var oDiv = document.getElementById("site-" + siteid);
- if (code[0] == "0")
- profile_replication_set_status(siteid, 'repl_success', code.substr(2));
- else
- profile_replication_set_status(siteid, 'repl_failed', code.substr(2));
+function set_profile_replication_result(site_id, success, msg) {
+ profile_replication_progress[site_id] = 0;
+
+ var icon_name = success ? 'repl_success' : 'repl_failed';
+
+ profile_replication_set_status(site_id, icon_name, msg);
+
+ // g_num_replsites is set by the page code in wato.py to the number async jobs
started
+ // in total
g_num_replsites--;
if (0 == g_num_replsites) {
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 5eb0d43..dfc07f6 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -15017,10 +15017,10 @@ def user_profile_async_replication_page():
def user_profile_async_replication_dialog(sites):
repstatus = watolib.load_replication_status()
- html.message(_('In order to activate your changes available on all remote sites,
your user profile needs '
- 'to be replicated to the remote sites. This is done on this page
now. Each site '
- 'is being represented by a single image which is first shown gray
and then fills '
- 'to green during synchronisation.'))
+ html.p(_('In order to activate your changes available on all remote sites, your
user profile needs '
+ 'to be replicated to the remote sites. This is done on this page now.
Each site '
+ 'is being represented by a single image which is first shown gray and
then fills '
+ 'to green during synchronisation.'))
html.h3(_('Replication States'))
html.open_div(id_="profile_repl")
@@ -15278,6 +15278,44 @@ def page_user_profile(change_pw=False):
html.footer()
+class ModeAjaxProfileReplication(WatoWebApiMode):
+ """AJAX handler for asynchronous replication of user profiles (changed
passwords)"""
+
+ def page(self):
+ request = self.webapi_request()
+
+ site_id = request.get("site")
+ if not site_id:
+ raise MKUserError(None, "The site_id is missing")
+
+ if site_id not in config.sitenames():
+ raise MKUserError(None, _("The requested site does not exist"))
+
+ status = sites.state(site_id, {}).get("state", "unknown")
+ if status == "dead":
+ raise MKGeneralException(_('The site is marked as dead. Not trying to
replicate.'))
+
+ site = config.site(site_id)
+ result = self._synchronize_profile(site_id, site, config.user.id)
+
+ if result != True:
+ watolib.add_profile_replication_change(site_id, result)
+ raise MKGeneralException(result)
+
+ return _("Replication completed successfully.")
+
+
+ def _synchronize_profile(self, site_id, site, user_id):
+ users = userdb.load_users(lock = False)
+ if not user_id in users:
+ raise MKUserError(None, _('The requested user does not exist'))
+
+ start = time.time()
+ result = watolib.push_user_profile_to_site(site, user_id, users[user_id])
+ duration = time.time() - start
+ watolib.ActivateChanges().update_activation_time(site_id,
watolib.ACTIVATION_TIME_PROFILE_SYNC, duration)
+ return result
+
#.
# .--Agent-Output--------------------------------------------------------.
# | _ _ ___ _ _ |
diff --git a/web/htdocs/watolib.py b/web/htdocs/watolib.py
index 879179f..8ad01a4 100644
--- a/web/htdocs/watolib.py
+++ b/web/htdocs/watolib.py
@@ -4525,43 +4525,6 @@ def push_user_profile_to_site(site, user_id, profile):
return response
-def synchronize_profile(site_id, site, user_id):
- users = userdb.load_users(lock = False)
- if not user_id in users:
- raise MKUserError(None, _('The requested user does not exist'))
-
- start = time.time()
- result = push_user_profile_to_site(site, user_id, users[user_id])
- duration = time.time() - start
- ActivateChanges().update_activation_time(site_id, ACTIVATION_TIME_PROFILE_SYNC,
duration)
- return result
-
-
-# AJAX handler for asynchronous replication of user profiles (changed passwords)
-def ajax_profile_repl():
- site_id = html.var("site")
-
- status = sites.state(site_id, {}).get("state", "unknown")
- if status == "dead":
- result = _('The site is marked as dead. Not trying to replicate.')
-
- else:
- site = config.site(site_id)
- try:
- result = synchronize_profile(site_id, site, config.user.id)
- except Exception, e:
- logger.exception()
- result = "%s" % e
-
- if result == True:
- answer = "0 %s" % _("Replication completed successfully.")
- else:
- answer = "1 %s" % (_("Error: %s") % result)
- add_profile_replication_change(site_id, result)
-
- html.write(answer)
-
-
# Add pending entry to make sync possible later for admins
def add_profile_replication_change(site_id, result):
add_change("edit-users", _('Profile changed (sync failed: %s)') %
result,
diff --git a/web/plugins/pages/wato.py b/web/plugins/pages/wato.py
index 3ea86c0..e13873c 100644
--- a/web/plugins/pages/wato.py
+++ b/web/plugins/pages/wato.py
@@ -25,7 +25,6 @@
# Boston, MA 02110-1301 USA.
import wato
-import watolib
register_handlers({
"wato" : wato.page_handler,
@@ -35,7 +34,7 @@ register_handlers({
"user_profile" : wato.page_user_profile,
"user_change_pw" : lambda:
wato.page_user_profile(change_pw=True),
- "wato_ajax_profile_repl" : watolib.ajax_profile_repl,
+ "wato_ajax_profile_repl" : lambda:
wato.ModeAjaxProfileReplication().handle_page(),
"automation_login" : lambda: wato.ModeAutomationLogin().page(),
"noauth:automation" : lambda: wato.ModeAutomation().page(),