Permalink
Browse files

Added ability to list by group id and group name.

Added group id and group name to list output
  • Loading branch information...
1 parent eaa592e commit 6c23dfb7027debe290ca531cbe3f0680c0aace1c @rrusnak1 rrusnak1 committed with oldpatricka Jul 6, 2011
@@ -33,6 +33,7 @@
public String getVMsByDN(String userDN) throws RemoteException;
public String getVMsByUser(String user) throws RemoteException;
public String getAllVMsByHost(String hostname) throws RemoteException;
- public String getAllVMsByGroup(String groupId) throws RemoteException;
+ public String[] getAllVMsByGroupId(String groupId) throws RemoteException;
+ public String[] getAllVMsByGroupName(String groupName) throws RemoteException;
public String shutdown(int type, String typeID, String seconds) throws RemoteException;
}
@@ -747,8 +747,7 @@
</bean>
<bean id="nimbus-remoting-admintools"
- class="org.globus.workspace.remoting.admin.defaults.DefaultRemoteAdminToolsMgmt"
- init-method="initialize">
+ class="org.globus.workspace.remoting.admin.defaults.DefaultRemoteAdminToolsMgmt">
<property name="manager"
ref="nimbus-rm.manager" />
@@ -758,6 +757,8 @@
ref="other.AuthzDataSource" />
<property name="workspaceHome"
ref="nimbus-rm.home.instance" />
+ <property name="authzCallout"
+ ref="nimbus-rm.service.binding.AuthorizationCallout" />
</bean>
<!-- ===================================================================
@@ -254,4 +254,12 @@ private static String gRead(String fileName) throws IOException {
}
return sb.toString();
}
+
+ public boolean hasDN(String dn) {
+ for(int i = 0; i < DNs.length; i++) {
+ if(dn.equals(DNs[i]))
+ return true;
+ }
+ return false;
+ }
}
@@ -385,6 +385,14 @@ public String getGroupName(String caller) {
return null;
}
+ public int getGroupIDFromCaller(String caller) {
+ for(int i = 0; i < this.groups.length; i++) {
+ if(groups[i].hasDN(caller))
+ return ++i;
+ }
+ return 0;
+ }
+
// -------------------------------------------------------------------------
// FOR CLOUD AUTOCONFIG
@@ -22,8 +22,9 @@
private String id;
private String node;
- private String groupId;
private String callerIdentity;
+ private String groupId;
+ private String groupName;
private String state;
private String startTime;
private String endTime;
@@ -33,12 +34,13 @@
//Used by Gson
public VMTranslation() {}
- public VMTranslation(String id, String node, String groupId, String callerIdentity, String state,
- String startTime, String endTime, String memory, String cpuCount) {
+ public VMTranslation(String id, String node, String callerIdentity, String groupId, String groupName,
+ String state, String startTime, String endTime, String memory, String cpuCount) {
this.id = id;
this.node = node;
- this.groupId = groupId;
this.callerIdentity = callerIdentity;
+ this.groupId = groupId;
+ this.groupName = groupName;
this.state = state;
this.startTime = startTime;
this.endTime = endTime;
@@ -55,6 +57,9 @@ public String getNode() {
public String getGroupId() {
return groupId;
}
+ public String getGroupName() {
+ return groupName;
+ }
public String getCallerIdentity() {
return callerIdentity;
}
@@ -167,6 +167,11 @@ public Options getOptions() {
public final Option GROUP_ID_OPT =
OptionBuilder.withLongOpt(GROUP_ID_LONG).hasOptionalArg().create(GROUP_ID);
+ public static final String GROUP_NAME = "gn";
+ public static final String GROUP_NAME_LONG= "gname";
+ public final Option GROUP_NAME_OPT =
+ OptionBuilder.withLongOpt(GROUP_NAME_LONG).hasOptionalArg().create(GROUP_NAME);
+
public static final String ID = "i";
public static final String ID_LONG = "id";
public final Option ID_OPT =
@@ -192,7 +197,8 @@ public Options getOptions() {
REPORT_OPT, JSON_OPT, OUTPUT_OPT, ADD_NODES_OPT, LIST_NODES_OPT,
REMOVE_NODES_OPT, UPDATE_NODES_OPT, NETWORKS_OPT, MEMORY_OPT, POOL_OPT,
ACTIVE_OPT, INACTIVE_OPT, LIST_VMS_OPT, SHUTDOWN_VMS_OPT, USER_OPT, ID_OPT,
- SECONDS_OPT, ALL_VMS_OPT, HOST_LIST_OPT, HOST_SHUTDOWN_OPT, DN_OPT, GROUP_ID_OPT
+ SECONDS_OPT, ALL_VMS_OPT, HOST_LIST_OPT, HOST_SHUTDOWN_OPT, DN_OPT, GROUP_ID_OPT,
+ GROUP_NAME_OPT
};
}
@@ -38,6 +38,7 @@
private static final String FIELD_ID = "id";
private static final String FIELD_NODE = "node";
private static final String FIELD_GROUP_ID = "group_id";
+ private static final String FIELD_GROUP_NAME = "group_name";
private static final String FIELD_CREATOR = "creator";
private static final String FIELD_STATE = "state";
private static final String FIELD_START = "start time";
@@ -46,14 +47,15 @@
private static final String FIELD_CPU_COUNT = "cpu count";
final static String[] ADMIN_FIELDS = new String[] {
- FIELD_ID, FIELD_NODE, FIELD_GROUP_ID, FIELD_CREATOR, FIELD_STATE, FIELD_START,
+ FIELD_ID, FIELD_NODE, FIELD_GROUP_ID, FIELD_GROUP_NAME, FIELD_CREATOR, FIELD_STATE, FIELD_START,
FIELD_END, FIELD_MEMORY, FIELD_CPU_COUNT};
private ToolAction action;
private RemoteAdminToolsManagement remoteAdminToolsManagement;
private String user;
private String userDN;
private String groupId;
+ private String groupName;
private String vmID;
private String hostname;
private String seconds;
@@ -190,6 +192,14 @@ private void loadArgs(String[] args) throws ParameterProblem {
this.groupId = gid;
numOpts++;
}
+ if(line.hasOption(Opts.GROUP_NAME)) {
+ final String gname = line.getOptionValue(Opts.GROUP_NAME);
+ if(gname == null || gname.trim().length() == 0) {
+ throw new ParameterProblem("Group name value is empty");
+ }
+ this.groupName = gname;
+ numOpts++;
+ }
if(line.hasOption(Opts.HOST_LIST)) {
final String hostname = line.getOptionValue(Opts.HOST_LIST);
if(hostname == null || hostname.trim().length() == 0) {
@@ -315,12 +325,32 @@ else if(this.userDN != null) {
vms = gson.fromJson(vmsJson, VMTranslation[].class);
}
else if(this.groupId != null) {
- final String vmsJson = this.remoteAdminToolsManagement.getAllVMsByGroup(groupId);
+ final String[] vmsJson = this.remoteAdminToolsManagement.getAllVMsByGroupId(groupId);
if(vmsJson == null) {
System.err.println("No vms with group id " + groupId + " found");
return;
}
- vms = gson.fromJson(vmsJson, VMTranslation[].class);
+ for(int i = 0; i < vmsJson.length; i++) {
+ if(vmsJson[i] != null) {
+ vms = gson.fromJson(vmsJson[i], VMTranslation[].class);
+ reporter.report(vmsToMaps(vms), this.outStream);
+ }
+ }
+ return;
+ }
+ else if(this.groupName != null) {
+ final String[] vmsJson = this.remoteAdminToolsManagement.getAllVMsByGroupName(groupName);
+ if(vmsJson == null) {
+ System.err.println("No vms with group name " + groupName + " found");
+ return;
+ }
+ for(int i = 0; i < vmsJson.length; i++) {
+ if(vmsJson[i] != null) {
+ vms = gson.fromJson(vmsJson[i], VMTranslation[].class);
+ reporter.report(vmsToMaps(vms), this.outStream);
+ }
+ }
+ return;
}
else if(this.hostname != null) {
final String vmsJson = this.remoteAdminToolsManagement.getAllVMsByHost(hostname);
@@ -341,7 +371,7 @@ else if(this.hostname != null) {
reporter.report(vmsToMaps(vms), this.outStream);
}
catch (RemoteException e) {
- System.err.println(e.getMessage());
+ super.handleRemoteException(e);
}
catch (IOException e) {
throw new ExecutionProblem("Problem writing output: " + e.getMessage(), e);
@@ -388,10 +418,11 @@ else if(hostname != null) {
}
private static Map<String, String> vmToMap(VMTranslation vmt) {
- final HashMap<String, String> map = new HashMap(7);
+ final HashMap<String, String> map = new HashMap(8);
map.put(FIELD_ID, vmt.getId());
map.put(FIELD_NODE, vmt.getNode());
map.put(FIELD_GROUP_ID, vmt.getGroupId());
+ map.put(FIELD_GROUP_NAME, vmt.getGroupName());
map.put(FIELD_CREATOR, vmt.getCallerIdentity());
map.put(FIELD_STATE, vmt.getState());
map.put(FIELD_START, vmt.getStartTime());
@@ -19,6 +19,7 @@ Actions
--user (-u) -Option to list VMs by user display name
--dn (-d) -Option to list VMs by user DN name
--gid (-g) -Option to list VMs by group id
+ --gname (-gn) -Option to list VMs by group name
--host (-hl) -Option to list VMs running under particular host
@@ -19,8 +19,11 @@
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.globus.workspace.groupauthz.Group;
+import org.globus.workspace.groupauthz.GroupAuthz;
import org.globus.workspace.remoting.admin.VMTranslation;
import org.globus.workspace.service.WorkspaceHome;
+import org.globus.workspace.service.binding.authorization.CreationAuthorizationCallout;
import org.globus.workspace.service.binding.vm.VirtualMachine;
import org.nimbus.authz.AuthzDBException;
import org.nimbus.authz.UserAlias;
@@ -54,6 +57,7 @@
protected ReprFactory reprFactory;
protected DataSource authzDataSource;
protected WorkspaceHome workspaceHome;
+ protected CreationAuthorizationCallout authzCallout;
private AuthzDBAdapter authz;
private final Gson gson;
@@ -91,17 +95,32 @@ public String getAllVMsByHost(String hostname) throws RemoteException {
return gson.toJson(vmts);
}
- public String getAllVMsByGroup(String groupId) throws RemoteException {
- VM[] vms = getVMByGroup(groupId);
+ public String[] getAllVMsByGroupId(String groupId) throws RemoteException {
+ int id;
+ try {
+ id = Integer.parseInt(groupId);
+ }
+ catch(NumberFormatException e) {
+ return null;
+ }
- if(vms == null)
+ GroupAuthz groupAuthz = (GroupAuthz) authzCallout;
+ Group[] groups = groupAuthz.getGroups();
+ if(id <= 0 || id > groups.length)
return null;
- final List<VMTranslation> vmts = new ArrayList<VMTranslation>(vms.length);
- for(int i = 0; i < vms.length; i++) {
- vmts.add(translateVM(vms[i]));
+ Group group = groups[--id];
+ return getAllVMsByGroup(group);
+ }
+
+ public String[] getAllVMsByGroupName(String groupName) throws RemoteException {
+ GroupAuthz groupAuthz = (GroupAuthz) authzCallout;
+ Group[] groups = groupAuthz.getGroups();
+ for(int i = 0; i < groups.length; i++) {
+ if(groups[i] != null && groups[i].getName().equals(groupName))
+ return getAllVMsByGroup(groups[i]);
}
- return gson.toJson(vmts);
+ return null;
}
public String getVMsByDN(String userDN) throws RemoteException {
@@ -297,44 +316,46 @@ private String getVMsByUserId(String userId) throws RemoteException {
}
}
- /*
- * Looks through all running vms and compares group ids
- */
- private VM[] getVMByGroup(String groupId) throws RemoteException {
- try {
- VM[] vms;
- VM[] all = manager.getGlobalAll();
- int cnt = 0;
+ private String[] getAllVMsByGroup(Group group) throws RemoteException {
- vms = new VM[all.length];
- for(int i = 0; i < all.length; i++) {
- String group = all[i].getGroupID();
- if(group != null && group.equals(groupId))
- vms[cnt++] = all[i];
- }
-
- if(cnt == 0) {
- errorMsg = "No running VMs with group id " + groupId + " found";
+ if(group != null) {
+ String[] dns = group.getIdentities();
+ if(dns.length == 0)
return null;
+
+ authz = new AuthzDBAdapter(authzDataSource);
+ String[] vms = new String[dns.length];
+ int vmcnt = 0;
+ for(int i = 0; i < dns.length; i++) {
+ try {
+ String userId = authz.getCanonicalUserIdFromDn(dns[i]);
+ String vm = getVMsByUserId(userId);
+ if(vm != null)
+ vms[vmcnt++] = vm;
+ }
+ catch(AuthzDBException e) {}
}
- else
- return vms;
- }
- catch (ManageException e) {
- throw new RemoteException(e.getMessage());
+ if(vms[0] == null)
+ return null;
+
+ return vms;
}
+ return null;
}
/*
* Translates vm info into string format for passing over rmi with Gson
*/
private VMTranslation translateVM(VM vm) throws RemoteException {
try {
+ GroupAuthz groupAuthz = (GroupAuthz) authzCallout;
+
String id = vm.getID();
VirtualMachine vmlong = workspaceHome.find(id).getVM();
String node = vmlong.getNode();
- String groupId = vm.getGroupID();
String creatorId = vm.getCreator().getIdentity();
+ String groupId = Integer.toString(groupAuthz.getGroupIDFromCaller(creatorId));
+ String groupName = groupAuthz.getGroupName(creatorId);
String state = vm.getState().getState();
String startTime = vm.getSchedule().getStartTime().getTime().toString();
String endTime = vm.getSchedule().getDestructionTime().getTime().toString();
@@ -343,7 +364,7 @@ private VMTranslation translateVM(VM vm) throws RemoteException {
String memory = Integer.toString(ra.getMemory());
String cpuCount = Integer.toString(ra.getIndCpuCount());
- VMTranslation vmt = new VMTranslation(id, node, groupId, creatorId, state, startTime, endTime,
+ VMTranslation vmt = new VMTranslation(id, node, creatorId, groupId, groupName, state, startTime, endTime,
memory, cpuCount);
return vmt;
}
@@ -370,4 +391,8 @@ public void setAuthzDataSource(DataSource authzDataSource) {
public void setWorkspaceHome(WorkspaceHome workspaceHome) {
this.workspaceHome = workspaceHome;
}
+
+ public void setAuthzCallout(CreationAuthorizationCallout authzCallout) {
+ this.authzCallout = authzCallout;
+ }
}

0 comments on commit 6c23dfb

Please sign in to comment.