From 4a41482c25b9d84f18b1aaf24280ee0e2befbc93 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 13 Mar 2013 17:42:26 +0000 Subject: [PATCH] Various fixes. Start implementing help CLI (not working) git-svn-id: https://svn.argeo.org/slc/trunk@6128 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../META-INF/spring/jcr.xml | 1 + .../slc/core/execution/DefaultAgent.java | 67 ++++++---- .../slc/core/execution/DefaultAgentCli.java | 114 ++++++++++++++++-- .../main/java/org/argeo/slc/cli/SlcMain.java | 26 +++- .../slc/execution/SlcAgentDescriptor.java | 68 ----------- 5 files changed, 174 insertions(+), 102 deletions(-) delete mode 100644 runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java diff --git a/modules/org.argeo.slc.agent.jcr/META-INF/spring/jcr.xml b/modules/org.argeo.slc.agent.jcr/META-INF/spring/jcr.xml index 5dd41b799..5750fc957 100644 --- a/modules/org.argeo.slc.agent.jcr/META-INF/spring/jcr.xml +++ b/modules/org.argeo.slc.agent.jcr/META-INF/spring/jcr.xml @@ -5,6 +5,7 @@ + diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java index 9bfde6edc..41c66622d 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java @@ -16,10 +16,8 @@ package org.argeo.slc.core.execution; import java.io.UnsupportedEncodingException; -import java.net.InetAddress; import java.net.URI; import java.net.URLDecoder; -import java.net.UnknownHostException; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -28,44 +26,45 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.argeo.slc.BasicNameVersion; import org.argeo.slc.SlcException; import org.argeo.slc.execution.ExecutionModuleDescriptor; import org.argeo.slc.execution.ExecutionModulesManager; import org.argeo.slc.execution.ExecutionProcess; import org.argeo.slc.execution.SlcAgent; -import org.argeo.slc.execution.SlcAgentDescriptor; /** Implements the base methods of an SLC agent. */ public class DefaultAgent implements SlcAgent { - private final static Log log = LogFactory.getLog(DefaultAgent.class); + // private final static Log log = LogFactory.getLog(DefaultAgent.class); /** UTF-8 charset for encoding. */ private final static String UTF8 = "UTF-8"; - private SlcAgentDescriptor agentDescriptor; + private String agentUuid = null; + // private SlcAgentDescriptor agentDescriptor; private ExecutionModulesManager modulesManager; private ThreadGroup processesThreadGroup; private Map runningProcesses = Collections .synchronizedMap(new HashMap()); + private String defaultModulePrefix = null; + /* * LIFECYCLE */ /** Initialization */ public void init() { - agentDescriptor = new SlcAgentDescriptor(); - agentDescriptor.setUuid(initAgentUuid()); - try { - agentDescriptor.setHost(InetAddress.getLocalHost().getHostName()); - } catch (UnknownHostException e) { - log.error("Cannot resolve localhost host name: " + e.getMessage()); - agentDescriptor.setHost("localhost"); - } + agentUuid = initAgentUuid(); + // agentDescriptor = new SlcAgentDescriptor(); + // agentDescriptor.setUuid(initAgentUuid()); + // try { + // agentDescriptor.setHost(InetAddress.getLocalHost().getHostName()); + // } catch (UnknownHostException e) { + // log.error("Cannot resolve localhost host name: " + e.getMessage()); + // agentDescriptor.setHost("localhost"); + // } processesThreadGroup = new ThreadGroup("SLC Processes of Agent #" - + agentDescriptor.getUuid()); + + agentUuid); // modulesManager.registerProcessNotifier(this, // new HashMap()); @@ -129,7 +128,9 @@ public class DefaultAgent implements SlcAgent { String[] path = uri.getPath().split("/"); if (path.length < 3) throw new SlcException("Badly formatted URI: " + uri); - String module = path[1]; + String moduleName = path[1]; + // TODO process version + String moduleVersion = null; StringBuilder flow = new StringBuilder(); for (int i = 2; i < path.length; i++) flow.append('/').append(path[i]); @@ -138,9 +139,9 @@ public class DefaultAgent implements SlcAgent { if (uri.getQuery() != null) values = getQueryMap(uri.getQuery()); - modulesManager.start(new BasicNameVersion(module, null)); + // Get execution module descriptor ExecutionModuleDescriptor emd = getExecutionModuleDescriptor( - module, null); + moduleName, moduleVersion); process.getRealizedFlows().add( emd.asRealizedFlow(flow.toString(), values)); } @@ -181,8 +182,24 @@ public class DefaultAgent implements SlcAgent { } public ExecutionModuleDescriptor getExecutionModuleDescriptor( - String moduleName, String version) { - return modulesManager.getExecutionModuleDescriptor(moduleName, version); + String moduleName, String moduleVersion) { + // Get execution module descriptor + ExecutionModuleDescriptor emd; + try { + modulesManager.start(new BasicNameVersion(moduleName, moduleVersion)); + emd = modulesManager.getExecutionModuleDescriptor(moduleName, + moduleVersion); + } catch (SlcException e) { + if (defaultModulePrefix != null) { + moduleName = defaultModulePrefix + "." + moduleName; + modulesManager.start(new BasicNameVersion(moduleName, + moduleVersion)); + emd = modulesManager.getExecutionModuleDescriptor(moduleName, + moduleVersion); + } else + throw e; + } + return emd; } public List listExecutionModuleDescriptors() { @@ -235,16 +252,16 @@ public class DefaultAgent implements SlcAgent { this.modulesManager = modulesManager; } - protected SlcAgentDescriptor getAgentDescriptor() { - return agentDescriptor; + public void setDefaultModulePrefix(String defaultModulePrefix) { + this.defaultModulePrefix = defaultModulePrefix; } public String getAgentUuid() { - return agentDescriptor.getUuid(); + return agentUuid; } @Override public String toString() { - return agentDescriptor.toString(); + return "Agent #" + getAgentUuid(); } } diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgentCli.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgentCli.java index 823207e87..c902ad3a3 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgentCli.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgentCli.java @@ -7,15 +7,27 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.security.OsAuthenticationToken; import org.argeo.slc.SlcException; +import org.argeo.slc.execution.ExecutionFlowDescriptor; +import org.argeo.slc.execution.ExecutionModuleDescriptor; +import org.argeo.slc.execution.ExecutionSpec; +import org.argeo.slc.execution.ExecutionSpecAttribute; import org.argeo.slc.execution.SlcAgent; import org.argeo.slc.execution.SlcAgentCli; import org.springframework.security.Authentication; import org.springframework.security.AuthenticationManager; import org.springframework.security.context.SecurityContextHolder; +/** + * Authenticates thread and executes synchronously a command line execution. + * Reference implementation of args to URIs algorithm. + */ public class DefaultAgentCli implements SlcAgentCli { + private final static Log log = LogFactory.getLog(DefaultAgentCli.class); + private final static String UTF8 = "UTF-8"; private SlcAgent agent; private AuthenticationManager authenticationManager; @@ -23,14 +35,25 @@ public class DefaultAgentCli implements SlcAgentCli { private Long timeout = 24 * 60 * 60 * 1000l; public String process(String[] args) { - OsAuthenticationToken oat = new OsAuthenticationToken(); - Authentication authentication = authenticationManager.authenticate(oat); - SecurityContextHolder.getContext().setAuthentication(authentication); + if (SecurityContextHolder.getContext().getAuthentication() == null) { + OsAuthenticationToken oat = new OsAuthenticationToken(); + Authentication authentication = authenticationManager + .authenticate(oat); + SecurityContextHolder.getContext() + .setAuthentication(authentication); + } - List uris = asURIs(args); - String processUuid = agent.process(uris); - agent.waitFor(processUuid, timeout); - return processUuid; + if (args.length > 0 && args[0].equals("help")) { + StringBuilder buf = new StringBuilder(); + help(args, buf); + log.info(buf); + return buf.toString(); + } else { + List uris = asURIs(args); + String processUuid = agent.process(uris); + agent.waitFor(processUuid, timeout); + return processUuid; + } } public static List asURIs(String[] args) { @@ -103,6 +126,83 @@ public class DefaultAgentCli implements SlcAgentCli { } } + protected void help(String[] rawArgs, StringBuilder buf) { + String[] args = Arrays.copyOfRange(rawArgs, 1, rawArgs.length); + List uris = asURIs(args); + uris: for (URI uri : uris) { + String[] path = uri.getPath().split("/"); + if (path.length < 2) { + for (ExecutionModuleDescriptor emd : agent + .listExecutionModuleDescriptors()) { + buf.append( + "# Execution Module " + emd.getName() + " v" + + emd.getVersion()).append('\n'); + if (emd.getDescription() != null + && !emd.getDescription().trim().equals("")) + buf.append(emd.getDescription()).append('\n'); + } + continue uris; + } + + String moduleName = path[1]; + // TODO process version + String moduleVersion = null; + + ExecutionModuleDescriptor emd = agent.getExecutionModuleDescriptor( + moduleName, moduleVersion); + + if (path.length >= 2) { + StringBuilder flow = new StringBuilder(); + for (int i = 2; i < path.length; i++) + flow.append('/').append(path[i]); + String flowPath = flow.toString(); + ExecutionFlowDescriptor flowDescriptor = null; + for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) { + if (efd.getName().equals(flowPath)) { + flowDescriptor = efd; + break; + } + } + if (flowDescriptor == null) + throw new SlcException("Flow " + uri + " not found"); + + buf.append( + "# Execution Module " + emd.getName() + " v" + + emd.getVersion()).append('\n'); + buf.append(" Flow ").append(flowDescriptor.getName()); + if (flowDescriptor.getDescription() != null + && !flowDescriptor.getDescription().trim().equals("")) + buf.append(" ").append(flowDescriptor.getDescription()); + buf.append('\n'); + ExecutionSpec spec = flowDescriptor.getExecutionSpec(); + for (String attrKey : spec.getAttributes().keySet()) { + ExecutionSpecAttribute esa = spec.getAttributes().get( + attrKey); + buf.append(" --").append(attrKey); + // TODO check values in query part + if (esa.getValue() != null) + buf.append(" ").append(esa.getValue()); + buf.append('\n'); + } + } else { + // module only + buf.append( + "# Execution Module " + emd.getName() + " v" + + emd.getVersion()).append('\n'); + if (emd.getDescription() != null + && !emd.getDescription().trim().equals("")) + buf.append(emd.getDescription()).append('\n'); + for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) { + buf.append(" ").append(efd.getName()); + if (efd.getDescription() != null + && !efd.getDescription().trim().equals("")) + buf.append(" ").append(efd.getDescription()); + } + buf.append('\n'); + } + } + } + public void setAgent(SlcAgent agent) { this.agent = agent; } diff --git a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java index 606748279..3890feefc 100644 --- a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java +++ b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java @@ -17,6 +17,7 @@ package org.argeo.slc.cli; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.security.AccessController; @@ -34,6 +35,7 @@ import javax.security.auth.login.LoginContext; import org.argeo.osgi.boot.OsgiBoot; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; import org.osgi.framework.ServiceReference; import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; @@ -46,6 +48,8 @@ public class SlcMain implements PrivilegedAction { public final static String os; public final static String slcDirName = ".slc"; + final static File homeDir = new File(System.getProperty("user.home")); + static { String osName = System.getProperty("os.name"); if (osName.startsWith("Win")) @@ -83,6 +87,7 @@ public class SlcMain implements PrivilegedAction { public String run() { long begin = System.currentTimeMillis(); + Framework framework = null; try { info("## Date : " + new Date()); info("## Data : " + dataDir.getCanonicalPath()); @@ -103,7 +108,7 @@ public class SlcMain implements PrivilegedAction { // Spring configs currently require System properties System.getProperties().putAll(configuration); - Framework framework = frameworkFactory.newFramework(configuration); + framework = frameworkFactory.newFramework(configuration); framework.start(); BundleContext bundleContext = framework.getBundleContext(); @@ -121,7 +126,7 @@ public class SlcMain implements PrivilegedAction { .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BUNDLES))); else osgiBoot.installUrls(osgiBoot.getBundlesUrls(System - .getProperty("user.home") + "/.slc/modules/**")); + .getProperty("user.home") + "/.slc/modules/;in=**")); // Start runtime osgiBoot.startBundles(bundlesToStart); @@ -159,7 +164,16 @@ public class SlcMain implements PrivilegedAction { return ret.toString(); } catch (Exception e) { + // Shutdown OSGi runtime + if (framework != null) + try { + framework.stop(); + framework.waitForStop(15 * 1000); + } catch (Exception silent) { + } throw new RuntimeException("Cannot run SLC command line", e); + } finally { + } } @@ -182,6 +196,7 @@ public class SlcMain implements PrivilegedAction { if (slcDir == null) { slcDir = new File(executionDir, slcDirName); slcDir.mkdirs(); + info("## Creating an SLC node at " + slcDir + " ..."); } } @@ -235,6 +250,13 @@ public class SlcMain implements PrivilegedAction { File parentDir = currentDir.getParentFile(); if (parentDir == null) return null; + try { + // ~/.slc reserved for agent + if (parentDir.getCanonicalPath().equals(homeDir.getCanonicalPath())) + return null; + } catch (IOException e) { + throw new RuntimeException("Cannot check home directory", e); + } return findSlcDir(parentDir); } diff --git a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java deleted file mode 100644 index 2ab20df1c..000000000 --- a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/SlcAgentDescriptor.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.execution; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - - -public class SlcAgentDescriptor implements Cloneable, Serializable { - private static final long serialVersionUID = 1L; - private String uuid; - private String host; - private List moduleDescriptors = new ArrayList(); - - public SlcAgentDescriptor() { - - } - - public SlcAgentDescriptor(SlcAgentDescriptor template) { - uuid = template.uuid; - host = template.host; - moduleDescriptors.addAll(template.moduleDescriptors); - } - - public String getUuid() { - return uuid; - } - - public void setUuid(String uuid) { - this.uuid = uuid; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public List getModuleDescriptors() { - return moduleDescriptors; - } - - public void setModuleDescriptors( - List modulesDescriptors) { - this.moduleDescriptors = modulesDescriptors; - } - - @Override - public String toString() { - return host + " #" + uuid; - } -} -- 2.39.2