View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.drivers.resManagers;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.gwe.app.agent.AgentApp;
25  import org.gwe.drivers.netAccess.ConnectorException;
26  import org.gwe.drivers.netAccess.ShellCommand;
27  import org.gwe.persistence.model.AllocationInfo;
28  import org.gwe.persistence.model.DaemonConfigDesc;
29  import org.gwe.utils.IOUtils;
30  
31  /**
32   * @author Marco Ruiz
33   * @since Aug 8, 2007
34   */
35  public abstract class ResourceManagerDriver {
36  
37  	private static Log log = LogFactory.getLog(ResourceManagerDriver.class);
38  
39  	public abstract boolean isSupportedJobManagerAvailable();
40  
41  	protected String id;
42  	
43  	protected String agentScriptPreffix;
44  	protected DaemonConfigDesc configuration;
45  	
46  	public String getId() {
47  	    return id;
48      }
49  
50  	public void setId(String id) {
51      	this.id = id;
52      }
53  
54  	public void setConfiguration(DaemonConfigDesc config) {
55  		this.configuration = config;
56  		String installPath = configuration.getHeadResource().getInstallation().getInstallPath();
57  		String baseScript = IOUtils.concatenatePaths(installPath, "/bin/gwed-base-script.sh");
58  		agentScriptPreffix = "#!/bin/bash\n " + baseScript + " " + AgentApp.class.getName() + " " + installPath + " ";
59  	}
60  	
61  	public String getAllocWorkspace(AllocationInfo alloc) {
62  		return configuration.getAllocWorkspace(alloc.getId());
63  	}
64  
65      public final void allocateComputeResource(AllocationInfo alloc) throws ResourceAllocationException {
66  		String workspace = getAllocWorkspace(alloc);
67  		new File(workspace).mkdir();
68  		log.info("Creating allocation " + alloc.getId() + " with workspace " + workspace);
69  		String systemPid = queueAllocationRequest(alloc);
70          log.info("Allocation " + alloc.getId() + " created with process id = " + systemPid);
71          alloc.setSystemPid(systemPid);
72      }
73      
74  	protected String[] runQueueCommand(String cmd, AllocationInfo alloc) throws ResourceAllocationException {
75  		log.trace("Command: " + cmd);
76  		try {
77  			String results = new ShellCommand(cmd, getAllocWorkspace(alloc), null).runLocally();
78  	        for (String line : results.split("\n")) { log.trace("R: " + line); }
79  			return results.split("\n");
80  		} catch (ConnectorException e) {
81  			throw new ResourceAllocationException("Could not run queue command '" + cmd + "'", e);
82  		}
83  	}
84      
85      // Must return the PID
86      public abstract String queueAllocationRequest(AllocationInfo alloc) throws ResourceAllocationException;
87      
88  	protected String writeAllocationFile(String fileName, String content, AllocationInfo alloc) throws ResourceAllocationException {
89  		try {
90  			String fullFileName = IOUtils.concatenatePaths(getAllocWorkspace(alloc), fileName);
91  			IOUtils.createLocalExecutableFile(fullFileName, content);
92  		    log.info("Wrote file '" + fileName + "' for allocation " + alloc.getId());
93  	        return fileName;
94          } catch (IOException e) {
95  			String msg = "Could not write file " + fileName + " needed to launch agent for allocation " + alloc.getId();
96  			log.warn(msg, e);
97  			throw new ResourceAllocationException(msg, e);
98          }
99      }
100     
101 	public abstract AllocationPhase killAllocation(AllocationInfo alloc) throws ResourceAllocationException;
102 	public abstract AllocationPhase checkAllocation(AllocationInfo alloc) throws ResourceAllocationException;
103 }