diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 79433fbf..00000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "iexec-scheduler"] - path = iexec-scheduler - url = https://github.com/iExecBlockchainComputing/iexec-scheduler -[submodule "iexec-worker"] - path = iexec-worker - url = https://github.com/iExecBlockchainComputing/iexec-worker diff --git a/build.gradle b/build.gradle index b4395c88..8167b7ea 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,21 @@ +def UNIQUE_ID='hostname -f'.execute().text.trim() +def tokencommon='curl -XPOST --data name=${UNIQUE_ID} https://cc02acaa7954c9d63333b18e6d5decead7d457ac3cfddeeb:@packagecloud.io/install/repositories/iexec/common/tokens.text'.execute().text.trim() +def tokenscheduler='curl -XPOST --data name=${UNIQUE_ID} https://6f087db4a17b65a64d456e474a6b4b16b59a52d59690b70d:@packagecloud.io/install/repositories/iexec/scheduler/tokens.text'.execute().text.trim() +def tokenworker='curl -XPOST --data name=${UNIQUE_ID} https://77715138d032c32e0d60769e5ae002b18837aa67da59f966:@packagecloud.io/install/repositories/iexec/worker/tokens.text'.execute().text.trim() + apply plugin: 'java-library' repositories { mavenCentral() + maven { + url "https://packagecloud.io/priv/${tokencommon}/iexec/common/maven2" + } + maven { + url "https://packagecloud.io/priv/${tokenscheduler}/iexec/scheduler/maven2" + } + maven { + url "https://packagecloud.io/priv/${tokenworker}/iexec/worker/maven2" + } } dependencies { @@ -19,8 +33,9 @@ dependencies { compile "org.json:json:20160810" // ? https://mvnrepository.com/artifact/org.json/json compile "org.web3j:core:3.2.0" - compile project(':iexec-scheduler') - compile project(':iexec-worker') + + compile "com.iexec.scheduler:iexec-scheduler:0.1.0-SNAPSHOT" + compile "com.iexec.worker:iexec-worker:0.1.0-SNAPSHOT" api 'org.apache.commons:commons-math3:3.6.1' diff --git a/iexec-scheduler b/iexec-scheduler deleted file mode 160000 index b02de06e..00000000 --- a/iexec-scheduler +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b02de06e4f02da99fceb78186144b578153c9df9 diff --git a/iexec-worker b/iexec-worker deleted file mode 160000 index 7594644c..00000000 --- a/iexec-worker +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7594644c83064fc1bdf88a8f8fdf876e366219ba diff --git a/settings.gradle b/settings.gradle index f8206dcd..b900582d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -16,6 +16,3 @@ include 'services:webservice' */ rootProject.name = 'xtremweb' - -include 'iexec-scheduler' -include 'iexec-worker' diff --git a/src/main/java/xtremweb/common/HTTPLauncher.java b/src/main/java/xtremweb/common/HTTPLauncher.java index a8e15345..8f8d70a7 100644 --- a/src/main/java/xtremweb/common/HTTPLauncher.java +++ b/src/main/java/xtremweb/common/HTTPLauncher.java @@ -23,295 +23,297 @@ package xtremweb.common; -import java.io.DataInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; +import com.iexec.common.ethereum.CommonConfiguration; +import com.iexec.worker.ethereum.CommonConfigurationGetter; +import com.iexec.worker.ethereum.IexecWorkerLibrary; +import xtremweb.communications.CommClient; +import xtremweb.exec.Executor; +import xtremweb.worker.WorkerPocoWatcherImpl; + +import javax.net.ssl.SSLHandshakeException; +import java.io.*; import java.net.ConnectException; import java.net.MalformedURLException; import java.net.URL; -import javax.net.ssl.SSLHandshakeException; - -import com.iexec.worker.ethereum.IexecConfigurationService; -import xtremweb.communications.CommClient; -import xtremweb.exec.Executor; -import xtremweb.worker.WorkerWatcherImpl; - /** * This launches XtremWeb from its JAR file. This first retrieve URL from config * file, if any. If the config file contains no URL, this contructs a new URL * with the server found in the config file. - * + *

* URL must be like "http://aServer/aFile.jar. Default URL is * "http://aServer/XWHEP/download/xtremweb.jar" */ public final class HTTPLauncher { - private static final int SLEEPDELAY = 2500; - private XWConfigurator config; - - /** - * This retrieves the default communication client and initializes it - * - * @return the default comm client - */ - private CommClient commClient() throws ConnectException { - - CommClient commClient = null; - try { - commClient = config.defaultCommClient(); - } catch (final Exception e) { - throw new ConnectException(e.toString()); - } - return commClient; - } - - /** - * Creates a new HTTPLauncher instance. - * - * @param a - * a String[] value : command line arguments which - * specificies the config file name - * @throws IOException - */ - private HTTPLauncher(final String[] a) throws IOException { - - IexecConfigurationService.initialize("../conf/iexec-worker.yml"); - WorkerWatcherImpl workerWatcher = new WorkerWatcherImpl(); - - - final String[] argv = a.clone(); - CommandLineParser args = null; - try { - args = new CommandLineParser(argv); - } catch (final Exception e) { - XWTools.fatal("xtremweb.upgrade.HTTPLauncher " + e); - } - - try { - config = (XWConfigurator) args.getOption(CommandLineOptions.CONFIG); - } catch (final Exception e) { - XWTools.fatal("can retreive config file"); - } - - final Logger logger = new Logger(); - - URL url = null; - try { - url = config.launcherURL(); - if ((url.getPath() == null) || (url.getPath().length() == 0)) { - url = new URL(url.toString() + "/XWHEP/download/" + XWTools.JARFILENAME); - } - } catch (final MalformedURLException e) { - logger.warn("Invalid launcher URL : " + url); - url = null; - } - - Version serverVersion = null; - File jarFile = null; - final File rootDir = config.getConfigFile().getParentFile().getParentFile(); - File libDir = new File(rootDir, "lib"); - if (!libDir.exists()) { - libDir = config.getTmpDir(); - } - File binDir = new File(rootDir, "bin"); - if (!binDir.exists()) { - binDir = config.getTmpDir(); - } - - final Version version = Version.currentVersion; - while (true) { - logger.debug("launcherURL = " + url); - logger.debug("Current version : " + version.toString()); - - boolean upgrade = false; - - try { - serverVersion = commClient().version(); - logger.debug("Server version : " + serverVersion); - - if (serverVersion != null) { - - final File newJarFile = new File(libDir, XWTools.JARFILENAME + "-" + serverVersion); - - if (!newJarFile.exists() - && !serverVersion.toString().equals(version.toString())) { - logger.info("Server version : " + serverVersion); - logger.info("********** ********** **********"); - logger.info("We must upgrade"); - logger.info("********** ********** **********"); - upgrade = true; - jarFile = null; - jarFile = newJarFile; - } - - if (newJarFile.exists()) { - jarFile = newJarFile; - } - } - } catch (final SSLHandshakeException e) { - logger.fatal("SSL error (maybe we have received a new keystore: relaunch is then necessary) : " + e); - } catch (final Exception e) { - upgrade = false; - logger.exception(e); - } - - if ((upgrade) && (url != null)) { - logger.info("Downloading xwhep JAR file"); - try (final StreamIO io = new StreamIO(null, new DataInputStream(url.openStream()), false)) { - logger.debug("" + jarFile + ".exists() = " + jarFile.exists()); - - io.readFileContent(jarFile); - } catch (final FileNotFoundException e) { - logger.fatal("Can't download " + XWTools.JARFILENAME + " : " + e); - } catch (final Exception e) { - logger.exception(e); - logger.warn("Can't download " + XWTools.JARFILENAME + "; using default : " + e.toString()); - } - } else { - logger.info("Not downloading xwhep JAR file"); - } - - Executor exec = null; - String tmpPath = null; - String jarFilePath = null; - String keystorePath = null; - String configPath = null; - String xwcp = null; - String javacp = System.getProperty("java.class.path"); - final StringBuilder javaCmd = new StringBuilder("java "); - - try { - logger.debug("00 libDir = " + libDir.getAbsolutePath()); - if (config.getProperty(XWPropertyDefs.XWCP) != null) { - libDir = new File(config.getProperty(XWPropertyDefs.XWCP)); - if (libDir.isFile()) { - libDir = libDir.getParentFile(); - } - } - logger.config("libDir = " + libDir.getAbsolutePath()); - - if ((jarFile == null) || !jarFile.exists()) { - jarFile = new File(libDir, XWTools.JARFILENAME); - } - - logger.config("jarFile = " + jarFile.getAbsolutePath()); - Thread.sleep(SLEEPDELAY); - - tmpPath = config.getTmpDir().getAbsolutePath(); - jarFilePath = jarFile.getAbsolutePath(); - keystorePath = config.getProperty(XWPropertyDefs.SSLKEYSTORE); - configPath = config.getProperty(XWPropertyDefs.CONFIGFILE); - xwcp = config.getProperty(XWPropertyDefs.XWCP); - if (tmpPath.endsWith("/") || tmpPath.endsWith("\\")) { - tmpPath = tmpPath.substring(0, tmpPath.length() - 1); - } - if (jarFilePath.endsWith("/") || jarFilePath.endsWith("\\")) { - jarFilePath = jarFilePath.substring(0, jarFilePath.length() - 1); - } - if (keystorePath.endsWith("/") || keystorePath.endsWith("\\")) { - keystorePath = keystorePath.substring(0, keystorePath.length() - 1); - } - if (configPath.endsWith("/") || configPath.endsWith("\\")) { - configPath = configPath.substring(0, configPath.length() - 1); - } - if (xwcp.endsWith("/") || xwcp.endsWith("\\")) { - xwcp = xwcp.substring(0, xwcp.length() - 1); - } - - if (OSEnum.getOs().isWin32()) { - tmpPath = "\"" + tmpPath + "\""; - jarFilePath = "\"" + jarFilePath + "\""; - keystorePath = "\"" + keystorePath + "\""; - configPath = "\"" + configPath + "\""; - xwcp = "\"" + xwcp + "\""; - } - - final String hwmem = System.getProperty(XWPropertyDefs.HWMEM.toString()) == "" ? "" - : " -D" + XWPropertyDefs.HWMEM + "=" + System.getProperty(XWPropertyDefs.HWMEM.toString()); - final String log4jconf = System.getProperty(XWPropertyDefs.LOG4JCONFIGFILE.propertyName()) == null ? - "" : " -D" + XWPropertyDefs.LOG4JCONFIGFILE.propertyName() + "=" + System.getProperty(XWPropertyDefs.LOG4JCONFIGFILE.propertyName()); - final String loglevel= System.getProperty(XWPropertyDefs.LOGGERLEVEL.propertyName()) == null ? - "" : " -D" + XWPropertyDefs.LOGGERLEVEL.propertyName() + "=" + System.getProperty(XWPropertyDefs.LOGGERLEVEL.propertyName()); - - final StringBuilder javaOpts = new StringBuilder(" -D" + XWPropertyDefs.CACHEDIR.propertyName() + "=" + tmpPath - + log4jconf + loglevel - + " -D" + XWPropertyDefs.JAVALIBPATH.propertyName() + "=" + tmpPath + hwmem - + " -D" + XWPropertyDefs.XWCP.propertyName() + "=" + xwcp - + " -D" + XWPropertyDefs.JAVAKEYSTORE.propertyName() + "=" + keystorePath - + " -cp " + jarFilePath + File.pathSeparator + (javacp != null ? javacp : "") - + " xtremweb.worker.Worker " - + " --xwconfig " + configPath); - - if (OSEnum.getOs().isWin32()) { - javaCmd.append(" -Xrs "); - } - - final String serveurOpt = " -server "; - final String cmd = javaCmd.toString() + serveurOpt + javaOpts.toString(); - - logger.config("Executing " + cmd); - final FileInputStream in = null; - exec = new Executor(cmd, binDir.getAbsolutePath(), in, System.out, System.err, - Long.parseLong(config.getProperty(XWPropertyDefs.TIMEOUT))); - int rc = exec.startAndWait(); - XWReturnCode returnCode = XWReturnCode.fromInt(rc); - logger.config("returnCode = " + returnCode + " (" + rc + ")"); - - if (returnCode == XWReturnCode.RESTART) { - continue; - } - - if (returnCode != XWReturnCode.SUCCESS) { - - final String cmd1 = javaCmd.toString() + javaOpts.toString(); - - logger.config("Trying to launch the worker without \"" + serveurOpt + "\" java option : " + cmd1); - exec = new Executor(cmd1, binDir.getAbsolutePath(), in, System.out, System.err, - Long.parseLong(config.getProperty(XWPropertyDefs.TIMEOUT))); - rc = exec.startAndWait(); - returnCode = XWReturnCode.fromInt(rc); - } - - if (returnCode == XWReturnCode.RESTART) { - continue; - } - - if (returnCode != XWReturnCode.SUCCESS) { - XWTools.fatal("We can't launch the worker : return code = " + returnCode + " (" + rc + ")" - + "\n(maybe URL launcher is not set properly or does not point to server version...)" - + "\n(maybe config file is corrupted...)"); - } - } catch (final Exception e) { - logger.exception(e); - logger.error(e.toString()); - } finally { - if (exec != null) { - try { - logger.info("Stopping process"); - exec.stop(); - } catch (final Exception e) { - } - } - exec = null; - tmpPath = null; - jarFilePath = null; - keystorePath = null; - configPath = null; - xwcp = null; - javacp = null; - } - } - } - - public static void main(final String[] argv) { - try { - new HTTPLauncher(argv); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + private static final int SLEEPDELAY = 2500; + private XWConfigurator config; + + /** + * Creates a new HTTPLauncher instance. + * + * @param a a String[] value : command line arguments which + * specificies the config file name + * @throws IOException + */ + private HTTPLauncher(final String[] a) throws IOException { + /* + IexecWorkerLibrary.initialize("../conf/iexec-worker.yml", new CommonConfigurationGetter() { + @Override + public CommonConfiguration getCommonConfiguration(String schedulerApiUrl) { + final String url = schedulerApiUrl + "/commonconfiguration"; + return new RestTemplate().getForObject(url, CommonConfiguration.class); + } + }); + WorkerPocoWatcherImpl workerPocoWatcher = new WorkerPocoWatcherImpl(); + */ + + final String[] argv = a.clone(); + CommandLineParser args = null; + try { + args = new CommandLineParser(argv); + } catch (final Exception e) { + XWTools.fatal("xtremweb.upgrade.HTTPLauncher " + e); + } + + try { + config = (XWConfigurator) args.getOption(CommandLineOptions.CONFIG); + } catch (final Exception e) { + XWTools.fatal("can retreive config file"); + } + + final Logger logger = new Logger(); + + URL url = null; + try { + url = config.launcherURL(); + if ((url.getPath() == null) || (url.getPath().length() == 0)) { + url = new URL(url.toString() + "/XWHEP/download/" + XWTools.JARFILENAME); + } + } catch (final MalformedURLException e) { + logger.warn("Invalid launcher URL : " + url); + url = null; + } + + Version serverVersion = null; + File jarFile = null; + final File rootDir = config.getConfigFile().getParentFile().getParentFile(); + File libDir = new File(rootDir, "lib"); + if (!libDir.exists()) { + libDir = config.getTmpDir(); + } + File binDir = new File(rootDir, "bin"); + if (!binDir.exists()) { + binDir = config.getTmpDir(); + } + + final Version version = Version.currentVersion; + while (true) { + logger.debug("launcherURL = " + url); + logger.debug("Current version : " + version.toString()); + + boolean upgrade = false; + + try { + serverVersion = commClient().version(); + logger.debug("Server version : " + serverVersion); + + if (serverVersion != null) { + + final File newJarFile = new File(libDir, XWTools.JARFILENAME + "-" + serverVersion); + + if (!newJarFile.exists() + && !serverVersion.toString().equals(version.toString())) { + logger.info("Server version : " + serverVersion); + logger.info("********** ********** **********"); + logger.info("We must upgrade"); + logger.info("********** ********** **********"); + upgrade = true; + jarFile = null; + jarFile = newJarFile; + } + + if (newJarFile.exists()) { + jarFile = newJarFile; + } + } + } catch (final SSLHandshakeException e) { + logger.fatal("SSL error (maybe we have received a new keystore: relaunch is then necessary) : " + e); + } catch (final Exception e) { + upgrade = false; + logger.exception(e); + } + + if ((upgrade) && (url != null)) { + logger.info("Downloading xwhep JAR file"); + try (final StreamIO io = new StreamIO(null, new DataInputStream(url.openStream()), false)) { + logger.debug("" + jarFile + ".exists() = " + jarFile.exists()); + + io.readFileContent(jarFile); + } catch (final FileNotFoundException e) { + logger.fatal("Can't download " + XWTools.JARFILENAME + " : " + e); + } catch (final Exception e) { + logger.exception(e); + logger.warn("Can't download " + XWTools.JARFILENAME + "; using default : " + e.toString()); + } + } else { + logger.info("Not downloading xwhep JAR file"); + } + + Executor exec = null; + String tmpPath = null; + String jarFilePath = null; + String keystorePath = null; + String configPath = null; + String xwcp = null; + String javacp = System.getProperty("java.class.path"); + final StringBuilder javaCmd = new StringBuilder("java "); + + try { + logger.debug("00 libDir = " + libDir.getAbsolutePath()); + if (config.getProperty(XWPropertyDefs.XWCP) != null) { + libDir = new File(config.getProperty(XWPropertyDefs.XWCP)); + if (libDir.isFile()) { + libDir = libDir.getParentFile(); + } + } + logger.config("libDir = " + libDir.getAbsolutePath()); + + if ((jarFile == null) || !jarFile.exists()) { + jarFile = new File(libDir, XWTools.JARFILENAME); + } + + logger.config("jarFile = " + jarFile.getAbsolutePath()); + Thread.sleep(SLEEPDELAY); + + tmpPath = config.getTmpDir().getAbsolutePath(); + jarFilePath = jarFile.getAbsolutePath(); + keystorePath = config.getProperty(XWPropertyDefs.SSLKEYSTORE); + configPath = config.getProperty(XWPropertyDefs.CONFIGFILE); + xwcp = config.getProperty(XWPropertyDefs.XWCP); + if (tmpPath.endsWith("/") || tmpPath.endsWith("\\")) { + tmpPath = tmpPath.substring(0, tmpPath.length() - 1); + } + if (jarFilePath.endsWith("/") || jarFilePath.endsWith("\\")) { + jarFilePath = jarFilePath.substring(0, jarFilePath.length() - 1); + } + if (keystorePath.endsWith("/") || keystorePath.endsWith("\\")) { + keystorePath = keystorePath.substring(0, keystorePath.length() - 1); + } + if (configPath.endsWith("/") || configPath.endsWith("\\")) { + configPath = configPath.substring(0, configPath.length() - 1); + } + if (xwcp.endsWith("/") || xwcp.endsWith("\\")) { + xwcp = xwcp.substring(0, xwcp.length() - 1); + } + + if (OSEnum.getOs().isWin32()) { + tmpPath = "\"" + tmpPath + "\""; + jarFilePath = "\"" + jarFilePath + "\""; + keystorePath = "\"" + keystorePath + "\""; + configPath = "\"" + configPath + "\""; + xwcp = "\"" + xwcp + "\""; + } + + final String hwmem = System.getProperty(XWPropertyDefs.HWMEM.toString()) == "" ? "" + : " -D" + XWPropertyDefs.HWMEM + "=" + System.getProperty(XWPropertyDefs.HWMEM.toString()); + final String log4jconf = System.getProperty(XWPropertyDefs.LOG4JCONFIGFILE.propertyName()) == null ? + "" : " -D" + XWPropertyDefs.LOG4JCONFIGFILE.propertyName() + "=" + System.getProperty(XWPropertyDefs.LOG4JCONFIGFILE.propertyName()); + final String loglevel = System.getProperty(XWPropertyDefs.LOGGERLEVEL.propertyName()) == null ? + "" : " -D" + XWPropertyDefs.LOGGERLEVEL.propertyName() + "=" + System.getProperty(XWPropertyDefs.LOGGERLEVEL.propertyName()); + + final StringBuilder javaOpts = new StringBuilder(" -D" + XWPropertyDefs.CACHEDIR.propertyName() + "=" + tmpPath + + log4jconf + loglevel + + " -D" + XWPropertyDefs.JAVALIBPATH.propertyName() + "=" + tmpPath + hwmem + + " -D" + XWPropertyDefs.XWCP.propertyName() + "=" + xwcp + + " -D" + XWPropertyDefs.JAVAKEYSTORE.propertyName() + "=" + keystorePath + + " -cp " + jarFilePath + File.pathSeparator + (javacp != null ? javacp : "") + + " xtremweb.worker.Worker " + + " --xwconfig " + configPath); + + if (OSEnum.getOs().isWin32()) { + javaCmd.append(" -Xrs "); + } + + final String serveurOpt = " -server "; + final String cmd = javaCmd.toString() + serveurOpt + javaOpts.toString(); + + logger.config("Executing " + cmd); + final FileInputStream in = null; + exec = new Executor(cmd, binDir.getAbsolutePath(), in, System.out, System.err, + Long.parseLong(config.getProperty(XWPropertyDefs.TIMEOUT))); + int rc = exec.startAndWait(); + XWReturnCode returnCode = XWReturnCode.fromInt(rc); + logger.config("returnCode = " + returnCode + " (" + rc + ")"); + + if (returnCode == XWReturnCode.RESTART) { + continue; + } + + if (returnCode != XWReturnCode.SUCCESS) { + + final String cmd1 = javaCmd.toString() + javaOpts.toString(); + + logger.config("Trying to launch the worker without \"" + serveurOpt + "\" java option : " + cmd1); + exec = new Executor(cmd1, binDir.getAbsolutePath(), in, System.out, System.err, + Long.parseLong(config.getProperty(XWPropertyDefs.TIMEOUT))); + rc = exec.startAndWait(); + returnCode = XWReturnCode.fromInt(rc); + } + + if (returnCode == XWReturnCode.RESTART) { + continue; + } + + if (returnCode != XWReturnCode.SUCCESS) { + XWTools.fatal("We can't launch the worker : return code = " + returnCode + " (" + rc + ")" + + "\n(maybe URL launcher is not set properly or does not point to server version...)" + + "\n(maybe config file is corrupted...)"); + } + } catch (final Exception e) { + logger.exception(e); + logger.error(e.toString()); + } finally { + if (exec != null) { + try { + logger.info("Stopping process"); + exec.stop(); + } catch (final Exception e) { + } + } + exec = null; + tmpPath = null; + jarFilePath = null; + keystorePath = null; + configPath = null; + xwcp = null; + javacp = null; + } + } + } + + public static void main(final String[] argv) { + try { + new HTTPLauncher(argv); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * This retrieves the default communication client and initializes it + * + * @return the default comm client + */ + private CommClient commClient() throws ConnectException { + + CommClient commClient = null; + try { + commClient = config.defaultCommClient(); + } catch (final Exception e) { + throw new ConnectException(e.toString()); + } + return commClient; + } } diff --git a/src/main/java/xtremweb/dispatcher/Dispatcher.java b/src/main/java/xtremweb/dispatcher/Dispatcher.java index 9db61105..510065ff 100644 --- a/src/main/java/xtremweb/dispatcher/Dispatcher.java +++ b/src/main/java/xtremweb/dispatcher/Dispatcher.java @@ -23,35 +23,24 @@ package xtremweb.dispatcher; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.Timer; - -import com.iexec.scheduler.ethereum.IexecConfigurationService; -import com.iexec.scheduler.iexechub.IexecHubService; -import com.iexec.scheduler.iexechub.IexecHubWatcher; -import com.iexec.scheduler.workerpool.WorkerPoolService; +import com.iexec.scheduler.ethereum.IexecSchedulerLibrary; import org.eclipse.jetty.server.session.SessionHandler; - -import xtremweb.common.CommandLineOptions; -import xtremweb.common.CommandLineParser; -import xtremweb.common.Logger; -import xtremweb.common.Version; -import xtremweb.common.XWConfigurator; -import xtremweb.common.XWPropertyDefs; -import xtremweb.common.XWReturnCode; -import xtremweb.common.XWRole; -import xtremweb.common.XWTools; +import xtremweb.common.*; import xtremweb.communications.AccessLogger; import xtremweb.communications.HTTPServer; import xtremweb.communications.TCPServer; import xtremweb.security.PEMPublicKeyValidator; import xtremweb.security.X509ProxyValidator; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.Timer; + /** * Dispatcher Implementation * The Dispatcher is responsible for delivering task to servers + * * @author V. Neri, G. Fedak */ @@ -61,265 +50,259 @@ public class Dispatcher { - private final Logger logger; - - /** - * The TaskSet and TaskManager - */ - private static TaskSet tset; - - /** - * Database Interface - */ - private static DBInterface db; - - /** Scheduling policy */ - private static Scheduler scheduler; - - /** - * This aims to validate an X509 certificate against certificate paths - * - * @since 7.0.0 - */ - private static X509ProxyValidator proxyValidator; - /** - * This aims to challenge client connections - * - * @since 7.5.0 - */ - private static PEMPublicKeyValidator certValidator; - - /** Timer */ - private static Timer timer; - - /** - * This stores and tests the command line parameters - */ - private CommandLineParser args; - /** - * Config as read from config file - */ - private static XWConfigurator config; - - /** - * This is the default constructor - */ - public Dispatcher(final String a[]) { - final String[] argv = a.clone(); - logger = new Logger(this); - try { - args = new CommandLineParser(argv); - } catch (final Exception e) { - logger.fatal("Command line error " + e); - } - - XWRole.setDispatcher(); - - try { - setConfig((XWConfigurator) args.getOption(CommandLineOptions.CONFIG)); - } catch (final Exception e) { - logger.fatal("can retreive config file"); - } - } - - /** - * Main function - */ - public static void main(final String args[]) { - - try { - new Dispatcher(args).go(); - } catch (final Exception e) { - System.err.println(e.toString()); - } - } - - /** - * Main function - */ - public void go() throws Exception { - - IexecConfigurationService.initialize("../conf/iexec-scheduler.yml"); - IexecHubWatcher iexecHubWatcher = new IexecHubWatcherImpl(); - - timer = new Timer(); - - try { - db = new DBInterface(config); - } catch(Exception e) { - e.printStackTrace(); - } - - tset = new HashTaskSet(); - try { - setScheduler( - (Scheduler) (Class.forName(config.getProperty(XWPropertyDefs.SCHEDULERCLASS)).newInstance())); - } catch (final Exception e) { - logger.exception(e); - logger.fatal(e.toString()); - } - - try { - setProxyValidator(new X509ProxyValidator()); - } catch (final Exception e) { - setProxyValidator(null); - logger.exception("Can't create ProxyValidator", e); - } - - try { - getProxyValidator().setCACertificateEntries(config.getKeyStore()); - } catch (final Exception e) { - logger.exception("Can't add CA certificates to keystore", e); - } - - try { - certValidator = new PEMPublicKeyValidator(); - } catch (final Exception e) { - certValidator = null; - logger.exception("Can't create PEMPublicKeyValidator", e); - } - - tset.start(); - try { - while (!tset.isReady()) { - Thread.sleep(1000); - logger.info("still waiting task set..."); - } - } catch (final Exception e) { - logger.error("exception while waiting task set..."); - } - - try { - - @SuppressWarnings("unused") - final AccessLogger accessLogger = new AccessLogger(config.getFile(XWPropertyDefs.HOMEDIR), - XWTools.getLocalHostName()); - - final TCPServer tcpServer = new TCPServer(); - tcpServer.initComm(config, new TCPHandler(config)); - tcpServer.start(); - - logger.warn("UDP server not started"); - - if (config.http()) { - final HTTPServer httpServer = new HTTPServer(); - // httpServer.initComm(config, new - // HTTPHandler(config)); - - // Handler needs HTTP session to handle openid call returns - final SessionHandler main_sessionHandler = new SessionHandler(); - main_sessionHandler.setHandler(new HTTPHandler(config)); - httpServer.initComm(config, main_sessionHandler); - httpServer.addHandler(HTTPHandler.PATH, main_sessionHandler); - - // OpenId Handler needs HTTP session - // final SessionHandler opid_sessionHandler = new - // SessionHandler(); - // final HTTPOpenIdHandler oh = new HTTPOpenIdHandler(); - // HTTPOpenIdHandler.setCACertificateEntries(config.getKeyStore()); - // opid_sessionHandler.setHandler(oh); - // httpServer.addHandler(HTTPOpenIdHandler.handlerPath, - // opid_sessionHandler); - - // OAuth Handler needs HTTP session - final SessionHandler oauth_sessionHandler = new SessionHandler(); - final HTTPOAuthHandler oah = new HTTPOAuthHandler(); - oauth_sessionHandler.setHandler(oah); - httpServer.addHandler(HTTPOAuthHandler.handlerPath, oauth_sessionHandler); - - if((config.getProperty(XWPropertyDefs.JWTETHSECRET) != null) && - (config.getProperty(XWPropertyDefs.JWTETHISSUER) != null)) { - // JWT Handler needs HTTP session - final SessionHandler jwt_sessionHandler = new SessionHandler(); - final HTTPJWTEthereumAuthHandler jwth = new HTTPJWTEthereumAuthHandler(); - jwt_sessionHandler.setHandler(jwth); - httpServer.addHandler(HTTPJWTEthereumAuthHandler.handlerPath, jwt_sessionHandler); - } - // StatsHandler does not need HTTP session - httpServer.addHandler(HTTPStatsHandler.PATH, new HTTPStatsHandler()); - httpServer.start(); - } - } catch (final Exception e) { - logger.exception(e); - logger.fatal("Dispatcher main(): " + e); - } - - final Collection services = XWTools.split(config.getProperty(XWPropertyDefs.SERVICES)); - if (services != null) { - for (final Iterator iter = services.iterator(); iter.hasNext();) { - try { - final String s = iter.next(); - logger.debug(s); - db.insertService(s); - } catch (final Exception e) { - logger.warn("Unable to start service : " + e); - } - db.updateAppsPool(); - } - } - - logger.info("XWHEP Dispatcher(" + Version.currentVersion + ") started [" + new Date() + "]"); - logger.info("DB vendor = " + config.getProperty(XWPropertyDefs.DBVENDOR)); - logger.info("mileStone = " + config.getProperty(XWPropertyDefs.MILESTONES)); - logger.info("Time out = " + config.getProperty(XWPropertyDefs.TIMEOUT)); - logger.info("Disk opt'd = " + config.getProperty(XWPropertyDefs.OPTIMIZEDISK)); - logger.info("Net opt'd = " + config.getProperty(XWPropertyDefs.OPTIMIZENETWORK)); - logger.info("NIO = " + config.getProperty(XWPropertyDefs.JAVANIO)); - config.dump(System.out, "XWHEP Dispatcher started "); - } - - public static void shutdown() { - db.unlockWorks(XWTools.getLocalHostName()); - System.exit(XWReturnCode.SUCCESS.ordinal()); - } - - @Override - public void finalize() { - db.unlockWorks(XWTools.getLocalHostName()); - } - - /** - * @return the config - */ - public static XWConfigurator getConfig() { - return config; - } - - /** - * @param config - * the config to set - */ - public static void setConfig(final XWConfigurator config) { - Dispatcher.config = config; - } - - /** - * @return the scheduler - */ - public static Scheduler getScheduler() { - return scheduler; - } - - /** - * @param scheduler - * the scheduler to set - */ - public static void setScheduler(final Scheduler scheduler) { - Dispatcher.scheduler = scheduler; - } - - /** - * @return the proxyValidator - */ - public static X509ProxyValidator getProxyValidator() { - return proxyValidator; - } - - /** - * @param proxyValidator - * the proxyValidator to set - */ - public static void setProxyValidator(final X509ProxyValidator proxyValidator) { - Dispatcher.proxyValidator = proxyValidator; - } + /** + * The TaskSet and TaskManager + */ + private static TaskSet tset; + /** + * Database Interface + */ + private static DBInterface db; + /** + * Scheduling policy + */ + private static Scheduler scheduler; + /** + * This aims to validate an X509 certificate against certificate paths + * + * @since 7.0.0 + */ + private static X509ProxyValidator proxyValidator; + /** + * This aims to challenge client connections + * + * @since 7.5.0 + */ + private static PEMPublicKeyValidator certValidator; + /** + * Timer + */ + private static Timer timer; + /** + * Config as read from config file + */ + private static XWConfigurator config; + private final Logger logger; + /** + * This stores and tests the command line parameters + */ + private CommandLineParser args; + + /** + * This is the default constructor + */ + public Dispatcher(final String a[]) { + final String[] argv = a.clone(); + logger = new Logger(this); + try { + args = new CommandLineParser(argv); + } catch (final Exception e) { + logger.fatal("Command line error " + e); + } + + XWRole.setDispatcher(); + + try { + setConfig((XWConfigurator) args.getOption(CommandLineOptions.CONFIG)); + } catch (final Exception e) { + logger.fatal("can retreive config file"); + } + } + + /** + * Main function + */ + public static void main(final String args[]) { + + try { + new Dispatcher(args).go(); + } catch (final Exception e) { + System.err.println(e.toString()); + } + } + + public static void shutdown() { + db.unlockWorks(XWTools.getLocalHostName()); + System.exit(XWReturnCode.SUCCESS.ordinal()); + } + + /** + * @return the config + */ + public static XWConfigurator getConfig() { + return config; + } + + /** + * @param config the config to set + */ + public static void setConfig(final XWConfigurator config) { + Dispatcher.config = config; + } + + /** + * @return the scheduler + */ + public static Scheduler getScheduler() { + return scheduler; + } + + /** + * @param scheduler the scheduler to set + */ + public static void setScheduler(final Scheduler scheduler) { + Dispatcher.scheduler = scheduler; + } + + /** + * @return the proxyValidator + */ + public static X509ProxyValidator getProxyValidator() { + return proxyValidator; + } + + /** + * @param proxyValidator the proxyValidator to set + */ + public static void setProxyValidator(final X509ProxyValidator proxyValidator) { + Dispatcher.proxyValidator = proxyValidator; + } + + /** + * Main function + */ + public void go() throws Exception { + + IexecSchedulerLibrary.initialize("../conf/iexec-scheduler.yml"); + SchedulerPocoWatcherImpl schedulerPocoWatcher = new SchedulerPocoWatcherImpl(); + + timer = new Timer(); + + try { + db = new DBInterface(config); + } catch (Exception e) { + e.printStackTrace(); + } + + tset = new HashTaskSet(); + try { + setScheduler( + (Scheduler) (Class.forName(config.getProperty(XWPropertyDefs.SCHEDULERCLASS)).newInstance())); + } catch (final Exception e) { + logger.exception(e); + logger.fatal(e.toString()); + } + + try { + setProxyValidator(new X509ProxyValidator()); + } catch (final Exception e) { + setProxyValidator(null); + logger.exception("Can't create ProxyValidator", e); + } + + try { + getProxyValidator().setCACertificateEntries(config.getKeyStore()); + } catch (final Exception e) { + logger.exception("Can't add CA certificates to keystore", e); + } + + try { + certValidator = new PEMPublicKeyValidator(); + } catch (final Exception e) { + certValidator = null; + logger.exception("Can't create PEMPublicKeyValidator", e); + } + + tset.start(); + try { + while (!tset.isReady()) { + Thread.sleep(1000); + logger.info("still waiting task set..."); + } + } catch (final Exception e) { + logger.error("exception while waiting task set..."); + } + + try { + + @SuppressWarnings("unused") final AccessLogger accessLogger = new AccessLogger(config.getFile(XWPropertyDefs.HOMEDIR), + XWTools.getLocalHostName()); + + final TCPServer tcpServer = new TCPServer(); + tcpServer.initComm(config, new TCPHandler(config)); + tcpServer.start(); + + logger.warn("UDP server not started"); + + if (config.http()) { + final HTTPServer httpServer = new HTTPServer(); + // httpServer.initComm(config, new + // HTTPHandler(config)); + + // Handler needs HTTP session to handle openid call returns + final SessionHandler main_sessionHandler = new SessionHandler(); + main_sessionHandler.setHandler(new HTTPHandler(config)); + httpServer.initComm(config, main_sessionHandler); + httpServer.addHandler(HTTPHandler.PATH, main_sessionHandler); + + // OpenId Handler needs HTTP session + // final SessionHandler opid_sessionHandler = new + // SessionHandler(); + // final HTTPOpenIdHandler oh = new HTTPOpenIdHandler(); + // HTTPOpenIdHandler.setCACertificateEntries(config.getKeyStore()); + // opid_sessionHandler.setHandler(oh); + // httpServer.addHandler(HTTPOpenIdHandler.handlerPath, + // opid_sessionHandler); + + // OAuth Handler needs HTTP session + final SessionHandler oauth_sessionHandler = new SessionHandler(); + final HTTPOAuthHandler oah = new HTTPOAuthHandler(); + oauth_sessionHandler.setHandler(oah); + httpServer.addHandler(HTTPOAuthHandler.handlerPath, oauth_sessionHandler); + + if ((config.getProperty(XWPropertyDefs.JWTETHSECRET) != null) && + (config.getProperty(XWPropertyDefs.JWTETHISSUER) != null)) { + // JWT Handler needs HTTP session + final SessionHandler jwt_sessionHandler = new SessionHandler(); + final HTTPJWTEthereumAuthHandler jwth = new HTTPJWTEthereumAuthHandler(); + jwt_sessionHandler.setHandler(jwth); + httpServer.addHandler(HTTPJWTEthereumAuthHandler.handlerPath, jwt_sessionHandler); + } + // StatsHandler does not need HTTP session + httpServer.addHandler(HTTPStatsHandler.PATH, new HTTPStatsHandler()); + httpServer.start(); + } + } catch (final Exception e) { + logger.exception(e); + logger.fatal("Dispatcher main(): " + e); + } + + final Collection services = XWTools.split(config.getProperty(XWPropertyDefs.SERVICES)); + if (services != null) { + for (final Iterator iter = services.iterator(); iter.hasNext(); ) { + try { + final String s = iter.next(); + logger.debug(s); + db.insertService(s); + } catch (final Exception e) { + logger.warn("Unable to start service : " + e); + } + db.updateAppsPool(); + } + } + + logger.info("XWHEP Dispatcher(" + Version.currentVersion + ") started [" + new Date() + "]"); + logger.info("DB vendor = " + config.getProperty(XWPropertyDefs.DBVENDOR)); + logger.info("mileStone = " + config.getProperty(XWPropertyDefs.MILESTONES)); + logger.info("Time out = " + config.getProperty(XWPropertyDefs.TIMEOUT)); + logger.info("Disk opt'd = " + config.getProperty(XWPropertyDefs.OPTIMIZEDISK)); + logger.info("Net opt'd = " + config.getProperty(XWPropertyDefs.OPTIMIZENETWORK)); + logger.info("NIO = " + config.getProperty(XWPropertyDefs.JAVANIO)); + config.dump(System.out, "XWHEP Dispatcher started "); + } + + @Override + public void finalize() { + db.unlockWorks(XWTools.getLocalHostName()); + } } diff --git a/src/main/java/xtremweb/dispatcher/IexecHubWatcherImpl.java b/src/main/java/xtremweb/dispatcher/IexecHubWatcherImpl.java deleted file mode 100644 index 179a0ecb..00000000 --- a/src/main/java/xtremweb/dispatcher/IexecHubWatcherImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package xtremweb.dispatcher; - -import com.iexec.scheduler.actuator.ActuatorService; -import com.iexec.scheduler.database.ContributionService; -import com.iexec.scheduler.iexechub.IexecHubService; -import com.iexec.scheduler.iexechub.IexecHubWatcher; - -public class IexecHubWatcherImpl implements IexecHubWatcher { - - private final static IexecHubService iexecHubService = IexecHubService.getInstance(); - private final static ContributionService contributionService = ContributionService.getInstance(); - private final static ActuatorService actuatorService = ActuatorService.getInstance(); - - public IexecHubWatcherImpl() { - iexecHubService.register(this); - } - - @Override - public void onSubscription(String worker) { - - } - - @Override - public void onWorkOrderActivated(String workOrderId) { - - } -} diff --git a/src/main/java/xtremweb/dispatcher/SchedulerPocoWatcherImpl.java b/src/main/java/xtremweb/dispatcher/SchedulerPocoWatcherImpl.java new file mode 100644 index 00000000..0f11f7eb --- /dev/null +++ b/src/main/java/xtremweb/dispatcher/SchedulerPocoWatcherImpl.java @@ -0,0 +1,46 @@ +package xtremweb.dispatcher; + +import com.iexec.common.contracts.generated.WorkerPool; +import com.iexec.common.ethereum.Utils; +import com.iexec.scheduler.actuator.ActuatorService; +import com.iexec.scheduler.database.ContributionService; +import com.iexec.scheduler.iexechub.IexecHubService; +import com.iexec.scheduler.iexechub.IexecHubWatcher; +import com.iexec.scheduler.workerpool.WorkerPoolService; +import com.iexec.scheduler.workerpool.WorkerPoolWatcher; + +import java.math.BigInteger; +import java.util.Arrays; + +public class SchedulerPocoWatcherImpl implements IexecHubWatcher, WorkerPoolWatcher { + + private final static IexecHubService iexecHubService = IexecHubService.getInstance(); + private final static WorkerPoolService workerPoolService = WorkerPoolService.getInstance(); + private final static ContributionService contributionService = ContributionService.getInstance(); + private final static ActuatorService actuatorService = ActuatorService.getInstance(); + + public SchedulerPocoWatcherImpl() { + iexecHubService.registerIexecHubWatcher(this); + workerPoolService.registerWorkerPoolWatcher(this); + } + + @Override + public void onSubscription(String worker) { + //actuatorService.createMarketOrder(BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(100), BigInteger.ONE); //on N worker alive + } + + @Override + public void onWorkOrderActivated(String workOrderId) { + //actuatorService.allowWorkersToContribute(workOrderId, Arrays.asList("0x70a1bebd73aef241154ea353d6c8c52d420d4f5b"), "O"); + } + + @Override + public void onContributeEvent(WorkerPool.ContributeEventResponse contributeEventResponse) { + //actuatorService.revealConsensus(contributeEventResponse.woid, Utils.hashResult("iExec the wanderer")); + } + + @Override + public void onReveal(WorkerPool.RevealEventResponse revealEventResponse) { + //actuatorService.finalizeWork(revealEventResponse.woid,"aStdout", "aStderr", "anUri"); + } +} diff --git a/src/main/java/xtremweb/worker/WorkerPocoWatcherImpl.java b/src/main/java/xtremweb/worker/WorkerPocoWatcherImpl.java new file mode 100644 index 00000000..f3c74823 --- /dev/null +++ b/src/main/java/xtremweb/worker/WorkerPocoWatcherImpl.java @@ -0,0 +1,38 @@ +package xtremweb.worker; + + +import com.iexec.worker.actuator.ActuatorService; +import com.iexec.worker.iexechub.IexecHubService; +import com.iexec.worker.iexechub.IexecHubWatcher; +import com.iexec.worker.workerpool.WorkerPoolService; +import com.iexec.worker.workerpool.WorkerPoolWatcher; + +import java.math.BigInteger; + +public class WorkerPocoWatcherImpl implements WorkerPoolWatcher, IexecHubWatcher { + + private final static IexecHubService iexecHubService = IexecHubService.getInstance(); + private final static WorkerPoolService workerPoolService = WorkerPoolService.getInstance(); + private final static ActuatorService actuatorService = ActuatorService.getInstance(); + + public WorkerPocoWatcherImpl() { + iexecHubService.registerIexecHubWatcher(this); + workerPoolService.registerWorkerPoolWatcher(this); + actuatorService.subscribeToPool(); + } + + @Override + public void onAllowWorkerToContribute(String workOrderId) { + //actuatorService.contribute(workOrderId, "iExec the wanderer", BigInteger.ZERO, "0", "O"); + } + + @Override + public void onRevealConsensus(String workOrderId) { + //actuatorService.reveal(workOrderId, "iExec the wanderer"); + } + + @Override + public void onWorkOrderCompleted(String workOrderId) { + //got some RLC + } +} diff --git a/src/main/java/xtremweb/worker/WorkerWatcherImpl.java b/src/main/java/xtremweb/worker/WorkerWatcherImpl.java deleted file mode 100644 index 852dddf4..00000000 --- a/src/main/java/xtremweb/worker/WorkerWatcherImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package xtremweb.worker; - - -import com.iexec.worker.actuator.ActuatorService; -import com.iexec.worker.workerpool.WorkerPoolService; -import com.iexec.worker.workerpool.WorkerPoolWatcher; - -public class WorkerWatcherImpl implements WorkerPoolWatcher { - - private final static WorkerPoolService workerPoolService = WorkerPoolService.getInstance(); - private final static ActuatorService actuatorService = ActuatorService.getInstance(); - - public WorkerWatcherImpl() { - workerPoolService.registerWorkerPoolWatcher(this); - actuatorService.subscribeToPool(); - } - - - @Override - public void onRevealConsensus(String workOrderId) { - - } - - @Override - public void onCallForContribution(String workOrderId) { - - } -} diff --git a/src/main/resources/conf/iexec-scheduler.yml b/src/main/resources/conf/iexec-scheduler.yml index 29929a00..37e179c6 100644 --- a/src/main/resources/conf/iexec-scheduler.yml +++ b/src/main/resources/conf/iexec-scheduler.yml @@ -1,35 +1,20 @@ -web3jConfig: - clientAddress: http://localhost:8545 #default - adminClient: false #default - startBlock: 0 -contractConfig: - rlcAddress: - iexecHubAddress: - walletConfig: - address: "0x8bd535d49b095ef648cd85ea827867d358872809" - folder: "../wallet" + folder: "./src/main/resources/wallet" filename: "UTC--2018-02-14T08-32-12.500000000Z--8bd535d49b095ef648cd85ea827867d358872809.json" password: "whatever" - -workerPoolConfig: - address: #leave blank will create a new workerpool (else plug scheduler to existing one) - name: "myWorkerPool" #unchangeable after creation - subscriptionLockStakePolicy: 0 #unchangeable after creation - subscriptionMinimumStakePolicy: 10 - subscriptionMinimumScorePolicy: 0 - stakeRatioPolicy: 30 - schedulerRewardRatioPolicy: 1 - resultRetentionPolicy: 7 - mode: 1 - list: - - "0x000000000000000000000000000000000000bad1" - - "0x000000000000000000000000000000000000bad2" - - - - - - - - +commonConfiguration: + nodeConfig: + clientAddress: http://localhost:8545 #default + adminClient: false #default + startBlock: 0 + contractConfig: + rlcAddress: 0xCHANGE_IT + iexecHubAddress: 0xCHANGE_IT + workerPoolConfig: + address: #leave blank will create a new workerpool (else plug scheduler to existing one) + name: "myWorkerPool" #unchangeable after creation + subscriptionLockStakePolicy: 0 #unchangeable after creation + subscriptionMinimumStakePolicy: 10 + subscriptionMinimumScorePolicy: 0 + stakeRatioPolicy: 30 + schedulerRewardRatioPolicy: 1 \ No newline at end of file diff --git a/src/main/resources/conf/iexec-worker.yml b/src/main/resources/conf/iexec-worker.yml index 9ec6bd45..adf9b8e8 100644 --- a/src/main/resources/conf/iexec-worker.yml +++ b/src/main/resources/conf/iexec-worker.yml @@ -1,16 +1,5 @@ -web3jConfig: - clientAddress: http://localhost:8545 #default - adminClient: false #default - startBlock: 0 -contractConfig: - rlcAddress: - iexecHubAddress: - workerPoolAddress: - subscriptionMinimumStakePolicy: 10 - stakeRatioPolicy: 30 - walletConfig: - address: "0x70a1bebd73aef241154ea353d6c8c52d420d4f5b" - folder: "../wallet" + folder: "./src/main/resources/wallet" filename: "UTC--2018-02-14T11-15-48.411000000Z--70a1bebd73aef241154ea353d6c8c52d420d4f5b.json" password: "whatever" +schedulerUrl: http://localhost:8080/ \ No newline at end of file