Skip to content

Commit

Permalink
frontend: add ability to force release servers
Browse files Browse the repository at this point in the history
Sometimes people leave or the login changes and it is important to allow
admins to be able to clear out all ACLs in those situations.

Signed-off-by: Nishanth Aravamudan <nacc@linux.vnet.ibm.com>
  • Loading branch information
Nishanth Aravamudan committed Sep 4, 2015
1 parent f54ef40 commit 0bc0c0b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 19 deletions.
26 changes: 24 additions & 2 deletions frontend/afe/reservations.py
Expand Up @@ -21,11 +21,10 @@

__author__ = "Julius Gawlas <julius.gawlas@hp.com>"


from autotest.frontend.afe import models


__all__ = ['create', 'release']
__all__ = ['create', 'release', 'force_release']


#
Expand Down Expand Up @@ -101,3 +100,26 @@ def release(hosts_to_release, username=None):
user_acl = acls[0]
user_acl.hosts.remove(*hosts)
user_acl.on_host_membership_change()


def force_release(hosts_to_release, username=None):
"""
Force release a collection of hosts from user
This will remove all ACLs from the hosts
:param hosts_to_release: strings or idents for hosts to release
:type hosts_to_release: list
:param username: login of the user reserving hosts
:type username: str
"""
hosts = models.Host.smart_get_bulk(hosts_to_release)
if not hosts:
raise Exception("At least one host must be specified")
user = get_user(username)
if not user.is_superuser():
raise Exception("Must be super user to force release")
acls = models.AclGroup.objects.all()
for user_acl in acls:
user_acl.hosts.remove(*hosts)
user_acl.on_host_membership_change()
14 changes: 14 additions & 0 deletions frontend/afe/rpc_interface.py
Expand Up @@ -507,6 +507,20 @@ def release_hosts(host_filter_data, username=None):
username=username)


def force_release_hosts(host_filter_data, username=None):
"""
Force release some hosts (remove all ACLs).
:param host_filter_data: Filters out which hosts to release.
:param username: login of the user releasing hosts, which needs have elevated privileges
:type username: str
:return: None.
"""
hosts = models.Host.query_objects(host_filter_data)
reservations.force_release(hosts_to_release=[h.hostname for h in hosts],
username=username)


# tests

def add_test(name, test_type, path, author=None, dependencies=None,
Expand Down
27 changes: 22 additions & 5 deletions frontend/client/src/autotest/afe/AfeUtils.java
Expand Up @@ -37,7 +37,8 @@ public class AfeUtils {

public static final ClassFactory factory = new SiteClassFactory();

private static StaticDataRepository staticData = StaticDataRepository.getRepository();
private static final StaticDataRepository staticData = StaticDataRepository.getRepository();
private static final JSONObject user = staticData.getData("current_user").isObject();

public static String formatStatusCounts(JSONObject counts, String joinWith) {
StringBuilder result = new StringBuilder();
Expand Down Expand Up @@ -387,7 +388,7 @@ public static void removeSecondsFromDateField(JSONObject row,
row.put(targetFieldName, new JSONString(date));
}

public static void handleHostsReservations(JSONArray hostIds, final boolean reserve,
public static void handleHostsReservations(JSONArray hostIds, final boolean reserve, final boolean force,
final String message,
final SimpleCallback callback) {
JSONObject hostFilterData = new JSONObject();
Expand All @@ -397,10 +398,14 @@ public static void handleHostsReservations(JSONArray hostIds, final boolean rese
params.put("host_filter_data", hostFilterData);

String functionName;
if (reserve)
if (reserve) {
functionName = "reserve_hosts";
else
functionName = "release_hosts";
} else {
if (force)
functionName = "force_release_hosts";
else
functionName = "release_hosts";
}
JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();

rpcProxy.rpcCall(functionName, params, new JsonRpcCallback() {
Expand All @@ -414,4 +419,16 @@ public void onSuccess(JSONValue result) {
}


public static boolean hostIsEveryoneAccessible(JSONObject host)
{
String hostAcl = Utils.jsonToString(host.get(HostDataSource.HOST_ACLS));
return (hostAcl.indexOf(HostDataSource.EVERYONE_ACL) != -1);
}

public static boolean hostIsAclAccessible(JSONObject host)
{
String hostAcl = Utils.jsonToString(host.get(HostDataSource.HOST_ACLS));
String login = Utils.jsonToString(user.get("login"));
return (hostAcl.indexOf(login) != -1);
}
}
48 changes: 40 additions & 8 deletions frontend/client/src/autotest/afe/HostDetailView.java
Expand Up @@ -2,6 +2,7 @@

import autotest.afe.create.CreateJobViewPresenter.JobCreateListener;
import autotest.common.SimpleCallback;
import autotest.common.StaticDataRepository;
import autotest.common.Utils;
import autotest.common.table.DataSource;
import autotest.common.table.DataSource.DataCallback;
Expand Down Expand Up @@ -31,13 +32,18 @@
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.Window;

import java.util.Date;
import java.util.List;
import java.util.Set;

public class HostDetailView extends DetailView
implements DataCallback, TableActionsListener, SelectableRowFilter {
private static final StaticDataRepository staticData = StaticDataRepository.getRepository();
private static final JSONObject user = staticData.getData("current_user").isObject();
private static final String[][] HOST_JOBS_COLUMNS = {
{DataTable.WIDGET_COLUMN, ""}, {"type", "Type"}, {"job__id", "Job ID"},
{"job_owner", "Job Owner"}, {"job_name", "Job Name"}, {"profile", "Profile"},
Expand Down Expand Up @@ -142,6 +148,7 @@ protected void preprocessRow(JSONObject row) {
private Button reinstallButton = new Button("Reinstall");
private Button reserveButton = new Button("Reserve");
private Button releaseButton = new Button("Release");
private Button forceReleaseButton = new Button("Force Release");
private CheckBox showSpecialTasks = new CheckBox();

public HostDetailView(HostDetailListener hostDetailListener,
Expand Down Expand Up @@ -228,6 +235,11 @@ public void handlePage(List<JSONObject> data) {
showText(lockedText, "view_host_locked");
showField(currentHostObject, "protection", "view_host_protection");
showField(currentHostObject, "current_profile", "view_host_current_profile");

reserveButton.setVisible(AfeUtils.hostIsEveryoneAccessible(currentHostObject));
releaseButton.setVisible(AfeUtils.hostIsAclAccessible(currentHostObject));
forceReleaseButton.setVisible((int)user.get("access_level").isNumber().doubleValue() >= 1);

String pageTitle = "Host " + hostname;
updateLockButton();
displayObjectData(pageTitle);
Expand Down Expand Up @@ -279,7 +291,7 @@ public void onClick(ClickEvent event) {
});
addWidget(lockButton, "view_host_lock_button");

HorizontalPanel buttons = new HorizontalPanel();
HorizontalPanel host_buttons = new HorizontalPanel();

reverifyButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Expand All @@ -293,7 +305,7 @@ public void doCallback(Object source) {
}, "Host " + hostname);
}
});
buttons.add(reverifyButton);
host_buttons.add(reverifyButton);

reinstallButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Expand All @@ -302,35 +314,55 @@ public void onClick(ClickEvent event) {
AfeUtils.scheduleReinstall(set, hostname, jobCreateListener);
}
});
buttons.add(reinstallButton);
host_buttons.add(reinstallButton);

HorizontalPanel reservation_buttons = new HorizontalPanel();

reserveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
JSONArray hostIds = new JSONArray();
hostIds.set(0, currentHostObject.get("id"));
AfeUtils.handleHostsReservations(hostIds, true, "Host reserved", new SimpleCallback() {
AfeUtils.handleHostsReservations(hostIds, true, false, "Host reserved", new SimpleCallback() {
public void doCallback(Object source) {
refresh();
}
});
}
});
buttons.add(reserveButton);
reserveButton.setVisible(false);
reservation_buttons.add(reserveButton);

releaseButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
JSONArray hostIds = new JSONArray();
hostIds.set(0, currentHostObject.get("id"));
AfeUtils.handleHostsReservations(hostIds, false, "Host released", new SimpleCallback() {
AfeUtils.handleHostsReservations(hostIds, false, false, "Host released", new SimpleCallback() {
public void doCallback(Object source) {
refresh();
}
});
}
});

releaseButton.setVisible(false);
reservation_buttons.add(releaseButton);

forceReleaseButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
JSONArray hostIds = new JSONArray();
hostIds.set(0, currentHostObject.get("id"));
AfeUtils.handleHostsReservations(hostIds, false, true, "Host force released", new SimpleCallback() {
public void doCallback(Object source) {
refresh();
}
});
}
});
buttons.add(releaseButton);
forceReleaseButton.setVisible(false);
reservation_buttons.add(forceReleaseButton);

addWidget(buttons, "view_host_buttons");
addWidget(host_buttons, "view_host_buttons");
addWidget(reservation_buttons, "view_host_reservation_buttons");
}

public void onError(JSONObject errorObject) {
Expand Down
13 changes: 9 additions & 4 deletions frontend/client/src/autotest/afe/HostListView.java
Expand Up @@ -106,13 +106,13 @@ private void reinstallSelectedHosts() {
AfeUtils.scheduleReinstall(selectedSet, "Hosts", jobCreateListener);
}

private void handleHostsReservations(boolean reserve) {
private void handleHostsReservations(boolean reserve, boolean force) {
JSONArray hostIds = getSelectedHostIds();
if (hostIds == null) {
return;
}

AfeUtils.handleHostsReservations(hostIds, reserve, "DONE", new SimpleCallback() {
AfeUtils.handleHostsReservations(hostIds, reserve, force, "DONE", new SimpleCallback() {
public void doCallback(Object source) {
refresh();
}
Expand Down Expand Up @@ -166,12 +166,17 @@ public void execute() {
});
menu.addItem("Reserve hosts", new Command() {
public void execute() {
handleHostsReservations(true);
handleHostsReservations(true, false);
}
});
menu.addItem("Release hosts", new Command() {
public void execute() {
handleHostsReservations(false);
handleHostsReservations(false, false);
}
});
menu.addItem("Force release hosts", new Command() {
public void execute() {
handleHostsReservations(false, true);
}
});

Expand Down

0 comments on commit 0bc0c0b

Please sign in to comment.