diff --git a/.gitignore b/.gitignore index 26204216..c1ae50dd 100755 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,4 @@ deployment/http-root/ajs/app/set_api_token_2.html *.log classes dist -deployment mysql_work_dir diff --git a/deployment/http-root/ajs/app/components/helpConstants.js b/deployment/http-root/ajs/app/components/helpConstants.js index 80d2650b..1dfcb818 100644 --- a/deployment/http-root/ajs/app/components/helpConstants.js +++ b/deployment/http-root/ajs/app/components/helpConstants.js @@ -48,10 +48,10 @@ "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME", "tagsdiscoveryScriptHelp" : "Type a shell script to discover tags for the project's nodes. The script runs on each individual Codeine peer.

" + "The script output should be written to a file in a path specified in the environment variable CODEINE_OUTPUT_FILE in the following json format:
[\"<tag-string>\", ...]

" + - "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS", + "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_HOST, CODEINE_PORT", "commandScriptHelp" : "Type a shell script to execute on nodes.

" + "Parameters will be provided to the script as Environment Variables.
" + - "In addition, the script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS", + "In addition, the script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS, CODEINE_HOST, CODEINE_PORT", "monitorsConfigureHelp" : "Monitors used to detect malfunctions and errors on the running nodes.

" + "Codeine runs the monitors periodically on all nodes, and notifies when a node fails.", "collectorsConfigureHelp" : "Collectors used to detect malfunctions and errors on the running nodes, and also to provide information about nodes status.

" + @@ -64,13 +64,13 @@ "credentialsHelp" : "The user that will be used to execute the configured script.

This feature does not work for most cases right now.", "notificationsEnabledHelp" : "If checked, emails will be sent to the configured users, when this collector starts to fail.", "monitorScriptHelp" : "Type a shell script to monitor the desired functionality of the node.

By convention, monitors are marked as failed if their exit status is non-zero.

" + - "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS.", + "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS, CODEINE_HOST, CODEINE_PORT.", "collectorScriptHelp" : "Type a shell script to collector the desired functionality of the node.

By convention, collectors are marked as failed if their exit status is non-zero.

" + - "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS.
"+ + "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS, CODEINE_HOST, CODEINE_PORT.
"+ "The collector output is expected in a file, that its name is in CODEINE_OUTPUT_FILE environment variable.", "versionScriptHelp" : "Type a shell script to specify the node's version.

" + "The script output should be written to a file in a path specified in the environment variable CODEINE_OUTPUT_FILE and should contain the version number (e.g.: 1.0.3).

" + - "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS.", + "The script can use the following environment variables in execution: CODEINE_PROJECT_NAME, CODEINE_NODE_NAME, CODEINE_NODE_ALIAS, CODEINE_NODE_TAGS, CODEINE_HOST, CODEINE_PORT.", "tabsConfigureHelp" : "Enables a separate view for a group of projects in the main page of Codeine.", "serverNameHelp" : "A name to be displayed when accessing UI or in mail messages", "webServerHostHelp" : "Full host name of the web server", diff --git a/deployment/http-root/ajs/app/components/project_configure/projectConfigureCtrl.js b/deployment/http-root/ajs/app/components/project_configure/projectConfigureCtrl.js index 7b65b4a3..e64d330a 100644 --- a/deployment/http-root/ajs/app/components/project_configure/projectConfigureCtrl.js +++ b/deployment/http-root/ajs/app/components/project_configure/projectConfigureCtrl.js @@ -17,11 +17,11 @@ } $scope.nodes = $scope.nodes.concat(org); $scope.model = { - commandAndMonitorAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS'], - versionAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS','CODEINE_OUTPUT_FILE'], - collectorAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS','CODEINE_OUTPUT_FILE'], + commandAndMonitorAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS','CODEINE_HOST', 'CODEINE_PORT'], + versionAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS','CODEINE_OUTPUT_FILE', 'CODEINE_HOST', 'CODEINE_PORT'], + collectorAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_NODE_TAGS','CODEINE_OUTPUT_FILE', 'CODEINE_HOST', 'CODEINE_PORT'], nodesDiscoveryAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_OUTPUT_FILE'], - tagsAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_OUTPUT_FILE'] + tagsAutoComplete : ['CODEINE_PROJECT_NAME','CODEINE_NODE_NAME','CODEINE_NODE_ALIAS','CODEINE_OUTPUT_FILE', 'CODEINE_HOST', 'CODEINE_PORT'] }; $scope.nodeQuantity = 30; diff --git a/src/common/codeine/model/Constants.java b/src/common/codeine/model/Constants.java index a7d6990d..66f0ac89 100755 --- a/src/common/codeine/model/Constants.java +++ b/src/common/codeine/model/Constants.java @@ -5,255 +5,268 @@ import java.io.File; -public class Constants -{ - - - public static final boolean SECURITY_ENABLED = true; - public static final boolean RUNNING_COLLECTORS_IN_PEER = true; - - public static final String HTTP_ROOT_CONTEXT = "/http-root"; - public static final String GUEST_USER = "Guest"; - - public static final String CODEINE_NODES_PROJECT_NAME = "Codeine_Internal_Nodes_Project"; - - public static final String PEER_LOG = "codeine_peer.log"; - public static final String DIRECTORY_LOG = "codeine_directory.log"; - public static final String SERVER_LOG = "codeine_server.log"; - public static final String PROJECT_CONF_FILE = "project.conf.json"; - public static final String INSTALLATION = "."; - public static final String MONITORS_DIR = "/monitors"; - public static final String CONF_DIR = "/conf"; - public static final String COMMANDS_DIR = "/plugins"; - public static final String COMMANDS_OUTPUT_DIR = "/plugins_output"; - public static final String ROOT_CONTEXT = "/"; - public static final String ANGULAR_CONTEXT = "/codeine"; - public static final String API_CONTEXT = "/api"; - public static final String API_WITH_TOKEN_CONTEXT = "/api-with-token"; - - - - public static final String MONITOR_OUTPUT_CONTEXT = "/monitor-output"; - public static final String COLLECTOR_OUTPUT_CONTEXT = "/collector-output"; - public static final String PROJECT_FILES_CONTEXT = "/projects-files"; - - public static final String PROJECT_NODES_CONTEXT = "/project-nodes"; - public static final String PROJECT_NODES_ALIASES_CONTEXT = "/project-nodes-aliases"; - public static final String PROJECT_NODES_CSV_CONTEXT = "/csv-nodes"; - public static final String PROJECT_STATUS_CONTEXT = "/project-status"; - public static final String NODE_STATUS_CONTEXT = "/node-status"; - public static final String MONITOR_STATUS_CONTEXT = "/monitor-status"; - public static final String COLLECTOR_STATUS_CONTEXT = "/collector-status"; - public static final String COMMAND_STATUS_CONTEXT = "/command-status"; - public static final String COMMAND_OUTPUT_CONTEXT = "/command-output"; - public static final String REPORTER_CONTEXT = "/reporter"; - public static final String METRICS_CONTEXT = "/metrics"; - public static final String UPGRADE_SERVER_CONTEXT = "/upgrade-server"; - public static final String COMMANDS_LOG_CONTEXT = "/commands-log"; - public static final String COMMANDS_EXECUTER_STATUS = "/commands-status"; - public static final String RESTART_ALL_PEERS_CONTEXT = "/restart-all"; - public static final String PROJECTS_LIST_CONTEXT = "/projects"; - public static final String PROJECT_PATH = "/project"; - public static final String NODE_PATH = "/node"; - public static final String COMMAND_NODE_CONTEXT = "/command-node"; - public static final String RELOAD_CONFIGURATION_CONTEXT = "/reload-configuration"; - public static final String REGISTER_PEER_IN_DIRECTORY_CONTEXT = "/register-in-directory"; - public static final String COMMAND_NODE_ALL_CONTEXT = "/command-node-all"; - public static final String COMMAND_NODES_CONTEXT = "/command-nodes"; - public static final String RESOURCESS_CONTEXT = "/resources"; - public static final String CONFIG_SUBMIT_CONTEXT = "/config-submit"; - public static final String PEER_STATUS_CONTEXT = "/peer/status"; - public static final String PEER_PROJECT_STATUS_CONTEXT = "/project/status"; - - public static final String MONITORS_STATISTICS_CONTEXT = "/monitors-statistics"; - - public static final String INFO_CONTEXT = "/info"; - public static final String USER_INFO_CONTEXT = "/user-info"; - public static final String MANAGE_STATISTICS_INFO_CONTEXT = "/manage-statistics"; - public static final String VERSION_INFO_CONTEXT = INFO_CONTEXT + "/version"; - public static final String VERSIONS_MAPPING_CONTEXT = "/versions-mapping"; - public static final String PEER_REPORT_CONTEXT = "/peer-report"; - public static final String RSYNC_SOURCE = "/version/rsync"; - public static final String COMMAND_NODE_IN_SERVER_CONTEXT = "/command-to-node"; - public static final String PEER_PORT_CONTEXT = "/peer/port"; - public static final String REGISTER_CONTEXT = "/register"; - public static final String PREPARE_FOR_SHUTDOWN_CONTEXT = "/prepare-for-shutdown"; - public static final String CANCEL_SHUTDOWN_CONTEXT = "/cancel-shutdown"; - public static final String GLOBAL_CONFIGURATION_CONTEXT = "/global-configuration"; - public static final String EXPERIMENTAL_CONFIGURATION_CONTEXT = "/experimental-configuration"; - public static final String PROJECT_CONFIGURATION_CONTEXT = "/project-configuration"; - public static final String PROJECT_COMMANDS_CONFIGURATION_CONTEXT = "/project-commands-configuration"; - public static final String PERMISSIONS_CONFIGURATION_CONTEXT = "/permissions"; - public static final String SESSION_INFO_CONTEXT = "/session-info"; - public static final String PUSH_PROJECTS_TO_DB_CONTEXT = "/push-projects-to-db"; - - public static final String REPLACE_NODE_NAME = "$node_name"; - public static final String DB_NAME = "codeineDB"; - public static final int DB_PORT = 27017; - - public static final boolean IS_MAIL_STARTEGY_MONGO = true; - - public static final String NO_VERSION = "No version"; - public static final String ALL_VERSION = "All versions"; - public static final String VERSION = "version"; - public static final String VERSION_COLLECTOR_NAME = "node_version_collector"; - public static final String TAGS_COLLECTOR_NAME = "node_tags_collector"; - - public static final String COMMAND_RESULT = "codeine-command-result="; - - public static final String JSON_COMMAND_FILE_NAME = "/command.json"; - - public static final String EXECUTION_ENV_CONFIGURATION_STEP = "CODEINE_CONFIGURATION_STEP"; - public static final String EXECUTION_ENV_OUTPUT_FILE = "CODEINE_OUTPUT_FILE"; - public static final String EXECUTION_ENV_PROJECT_NAME = "CODEINE_PROJECT_NAME"; - public static final String EXECUTION_ENV_USER_NAME = "CODEINE_USER_NAME"; - public static final String EXECUTION_ENV_PROJECT_STATUS = "CODEINE_PROJECT_STATUS"; - public static final String EXECUTION_ENV_NODE_NAME = "CODEINE_NODE_NAME"; - public static final String EXECUTION_ENV_NODE_ALIAS = "CODEINE_NODE_ALIAS"; - public static final String EXECUTION_ENV_NODE_TAGS = "CODEINE_NODE_TAGS"; - public static final String ENV_CODEINE_WORKAREA = "CODEINE_WORKAREA"; - public static final String ENV_CODEINE_HOST_WORKAREA = "CODEINE_HOST_WORKAREA"; - - public static String installDir = null; - - public static String getConfDir() { - return getWorkarea() + "/conf"; - } - public static String getConfPath() { - return getConfDir() + "/codeine.conf.json"; - } - public static String getViewConfPath() { - return getConfDir() + "/codeine.view.conf.json"; - } - public static String getExperimentalConfPath() { - return getConfDir() + "/codeine.experimental.conf.json"; - } - public static String getIdentityConfPath() { - return getConfDir() + "/identity.json"; - } - public static String getPermissionsConfPath() { - return getConfDir() + "/permissions.json"; - } - public static String getSpnegoPropertiesPath() { - return getConfDir() + "/spnego.properties"; - } - - public static String getLogDir(){ - return System.getProperty("log.dir", getResourcesDir()); - } - public static String getResourcesDir() { - return Constants.getInstallDir() + HTTP_ROOT_CONTEXT; - } - public static String getAngularDir() { - String appDir = System.getProperty("angularAppDir"); - if (StringUtils.isEmpty(appDir)) { - appDir = "dist"; - } - return Constants.getResourcesDir() + "/ajs/" + appDir; - } - public static String getAngularMainHtml() { - return Constants.getAngularDir() + "/index.html"; - } - - public static String getInstallDir(){ - if (installDir != null) - { - return installDir; - } - installDir = System.getProperty("installDir"); - if (installDir == null) - { - // if install dir not set, get relative to cwd: - String sJarPath = Constants.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - File jarFile = new File(sJarPath); - installDir = jarFile.getParentFile().getParent(); - } - System.out.println("Setting installDir to '" + installDir + "'"); //$NON-NLS-2$ - return installDir; - } - - public static final String COMMAND_FINISH_FILE = "/finished"; - - public static final String COMMAND_LOG_FILE = "/log"; - - public static final int DEFAULT_WEB_SERVER_PORT = 12347; - public static final int DEFAULT_PEER_PORT = 49671; - - public static final String DISCOVERY_PLUGIN = "discovery"; - - public static final String API_TOKEN = "api_token"; - - public static final String PROJECT_TAGS_CONTEXT = "/project-tags"; - - public static final String ANGULAR_WEB_URLS_PATH_SPEC = "/codeine/*"; - - public static final String ANGULAR_RESOURCES_CONTEXT_PATH = "/ajs"; - - public static final String PROJECTS_TABS_CONTEXT = "/projects-tabs"; - - - - public static class RequestHeaders { - public static final String NO_ZIP = "NO_ZIP"; - - } - public static class UrlParameters { - public static final String PROJECT_NAME = "project"; - public static final String VERSION_NAME = "version"; - public static final String NODE_NAME = "node-name"; - public static final String NODE = "node"; - public static final String MONITOR = "monitor"; - public static final String COLLECTOR = "collector"; - public static final String COMMAND_NAME = "command"; - public static final String PATH_NAME = "path"; - public static final String RESOURCE_NAME = "resource"; - public static final String LINK_NAME = "link"; - public static final String DATA_NAME = "data"; - public static final String REDIRECT = "redirect"; - public static final String COMMAND_ID = "command-id"; - public static final String DATA_ADDITIONAL_COMMAND_INFO_NAME = "more-command-info"; - public static final String SECTION = "section"; - public static final String VIEW_AS = "viewas"; - public static final String FILTER = "text-filter"; - public static final String FORCE = "force"; - public static final String ADDRESS = "server"; - public static final String INCLUDE_OUTPUT = "output"; - public static final String SYNC_REQUEST = "sync"; - } - - public static String getPersistentDir() { - return getWorkarea() + "/persistent." + InetUtils.getLocalHost().getHostName(); - } - public static String getWorkarea(){ - String workarea = System.getenv(ENV_CODEINE_WORKAREA); - if (null == workarea){ - return getInstallDir(); - } - return workarea; - } - public static String getHostWorkareaDir() { +public class Constants { + + + public static final boolean SECURITY_ENABLED = true; + public static final boolean RUNNING_COLLECTORS_IN_PEER = true; + + public static final String HTTP_ROOT_CONTEXT = "/http-root"; + public static final String GUEST_USER = "Guest"; + + public static final String CODEINE_NODES_PROJECT_NAME = "Codeine_Internal_Nodes_Project"; + + public static final String PEER_LOG = "codeine_peer.log"; + public static final String DIRECTORY_LOG = "codeine_directory.log"; + public static final String SERVER_LOG = "codeine_server.log"; + public static final String PROJECT_CONF_FILE = "project.conf.json"; + public static final String INSTALLATION = "."; + public static final String MONITORS_DIR = "/monitors"; + public static final String CONF_DIR = "/conf"; + public static final String COMMANDS_DIR = "/plugins"; + public static final String COMMANDS_OUTPUT_DIR = "/plugins_output"; + public static final String ROOT_CONTEXT = "/"; + public static final String ANGULAR_CONTEXT = "/codeine"; + public static final String API_CONTEXT = "/api"; + public static final String API_WITH_TOKEN_CONTEXT = "/api-with-token"; + + + public static final String MONITOR_OUTPUT_CONTEXT = "/monitor-output"; + public static final String COLLECTOR_OUTPUT_CONTEXT = "/collector-output"; + public static final String PROJECT_FILES_CONTEXT = "/projects-files"; + + public static final String PROJECT_NODES_CONTEXT = "/project-nodes"; + public static final String PROJECT_NODES_ALIASES_CONTEXT = "/project-nodes-aliases"; + public static final String PROJECT_NODES_CSV_CONTEXT = "/csv-nodes"; + public static final String PROJECT_STATUS_CONTEXT = "/project-status"; + public static final String NODE_STATUS_CONTEXT = "/node-status"; + public static final String MONITOR_STATUS_CONTEXT = "/monitor-status"; + public static final String COLLECTOR_STATUS_CONTEXT = "/collector-status"; + public static final String COMMAND_STATUS_CONTEXT = "/command-status"; + public static final String COMMAND_OUTPUT_CONTEXT = "/command-output"; + public static final String REPORTER_CONTEXT = "/reporter"; + public static final String METRICS_CONTEXT = "/metrics"; + public static final String UPGRADE_SERVER_CONTEXT = "/upgrade-server"; + public static final String COMMANDS_LOG_CONTEXT = "/commands-log"; + public static final String COMMANDS_EXECUTER_STATUS = "/commands-status"; + public static final String RESTART_ALL_PEERS_CONTEXT = "/restart-all"; + public static final String PROJECTS_LIST_CONTEXT = "/projects"; + public static final String PROJECT_PATH = "/project"; + public static final String NODE_PATH = "/node"; + public static final String COMMAND_NODE_CONTEXT = "/command-node"; + public static final String RELOAD_CONFIGURATION_CONTEXT = "/reload-configuration"; + public static final String REGISTER_PEER_IN_DIRECTORY_CONTEXT = "/register-in-directory"; + public static final String COMMAND_NODE_ALL_CONTEXT = "/command-node-all"; + public static final String COMMAND_NODES_CONTEXT = "/command-nodes"; + public static final String RESOURCESS_CONTEXT = "/resources"; + public static final String CONFIG_SUBMIT_CONTEXT = "/config-submit"; + public static final String PEER_STATUS_CONTEXT = "/peer/status"; + public static final String PEER_PROJECT_STATUS_CONTEXT = "/project/status"; + + public static final String MONITORS_STATISTICS_CONTEXT = "/monitors-statistics"; + + public static final String INFO_CONTEXT = "/info"; + public static final String USER_INFO_CONTEXT = "/user-info"; + public static final String MANAGE_STATISTICS_INFO_CONTEXT = "/manage-statistics"; + public static final String VERSION_INFO_CONTEXT = INFO_CONTEXT + "/version"; + public static final String VERSIONS_MAPPING_CONTEXT = "/versions-mapping"; + public static final String PEER_REPORT_CONTEXT = "/peer-report"; + public static final String RSYNC_SOURCE = "/version/rsync"; + public static final String COMMAND_NODE_IN_SERVER_CONTEXT = "/command-to-node"; + public static final String PEER_PORT_CONTEXT = "/peer/port"; + public static final String REGISTER_CONTEXT = "/register"; + public static final String PREPARE_FOR_SHUTDOWN_CONTEXT = "/prepare-for-shutdown"; + public static final String CANCEL_SHUTDOWN_CONTEXT = "/cancel-shutdown"; + public static final String GLOBAL_CONFIGURATION_CONTEXT = "/global-configuration"; + public static final String EXPERIMENTAL_CONFIGURATION_CONTEXT = "/experimental-configuration"; + public static final String PROJECT_CONFIGURATION_CONTEXT = "/project-configuration"; + public static final String PROJECT_COMMANDS_CONFIGURATION_CONTEXT = "/project-commands-configuration"; + public static final String PERMISSIONS_CONFIGURATION_CONTEXT = "/permissions"; + public static final String SESSION_INFO_CONTEXT = "/session-info"; + public static final String PUSH_PROJECTS_TO_DB_CONTEXT = "/push-projects-to-db"; + + public static final String REPLACE_NODE_NAME = "$node_name"; + public static final String DB_NAME = "codeineDB"; + public static final int DB_PORT = 27017; + + public static final boolean IS_MAIL_STARTEGY_MONGO = true; + + public static final String NO_VERSION = "No version"; + public static final String ALL_VERSION = "All versions"; + public static final String VERSION = "version"; + public static final String VERSION_COLLECTOR_NAME = "node_version_collector"; + public static final String TAGS_COLLECTOR_NAME = "node_tags_collector"; + + public static final String COMMAND_RESULT = "codeine-command-result="; + + public static final String JSON_COMMAND_FILE_NAME = "/command.json"; + + public static final String EXECUTION_ENV_CONFIGURATION_STEP = "CODEINE_CONFIGURATION_STEP"; + public static final String EXECUTION_ENV_OUTPUT_FILE = "CODEINE_OUTPUT_FILE"; + public static final String EXECUTION_ENV_PROJECT_NAME = "CODEINE_PROJECT_NAME"; + public static final String EXECUTION_ENV_USER_NAME = "CODEINE_USER_NAME"; + public static final String EXECUTION_ENV_PROJECT_STATUS = "CODEINE_PROJECT_STATUS"; + public static final String EXECUTION_ENV_NODE_NAME = "CODEINE_NODE_NAME"; + public static final String EXECUTION_ENV_NODE_ALIAS = "CODEINE_NODE_ALIAS"; + public static final String EXECUTION_ENV_NODE_TAGS = "CODEINE_NODE_TAGS"; + public static final String EXECUTION_ENV_CODEINE_SERVER = "CODEINE_HOST"; + public static final String EXECUTION_ENV_CODEINE_SERVER_PORT = "CODEINE_PORT"; + public static final String ENV_CODEINE_WORKAREA = "CODEINE_WORKAREA"; + public static final String ENV_CODEINE_HOST_WORKAREA = "CODEINE_HOST_WORKAREA"; + + public static String installDir = null; + + public static String getConfDir() { + return getWorkarea() + "/conf"; + } + + public static String getConfPath() { + return getConfDir() + "/codeine.conf.json"; + } + + public static String getViewConfPath() { + return getConfDir() + "/codeine.view.conf.json"; + } + + public static String getExperimentalConfPath() { + return getConfDir() + "/codeine.experimental.conf.json"; + } + + public static String getIdentityConfPath() { + return getConfDir() + "/identity.json"; + } + + public static String getPermissionsConfPath() { + return getConfDir() + "/permissions.json"; + } + + public static String getSpnegoPropertiesPath() { + return getConfDir() + "/spnego.properties"; + } + + public static String getLogDir() { + return System.getProperty("log.dir", getResourcesDir()); + } + + public static String getResourcesDir() { + return Constants.getInstallDir() + HTTP_ROOT_CONTEXT; + } + + public static String getAngularDir() { + String appDir = System.getProperty("angularAppDir"); + if (StringUtils.isEmpty(appDir)) { + appDir = "dist"; + } + return Constants.getResourcesDir() + "/ajs/" + appDir; + } + + public static String getAngularMainHtml() { + return Constants.getAngularDir() + "/index.html"; + } + + public static String getInstallDir() { + if (installDir != null) { + return installDir; + } + installDir = System.getProperty("installDir"); + if (installDir == null) { + // if install dir not set, get relative to cwd: + String sJarPath = Constants.class.getProtectionDomain().getCodeSource().getLocation() + .getPath(); + File jarFile = new File(sJarPath); + installDir = jarFile.getParentFile().getParent(); + } + System.out.println("Setting installDir to '" + installDir + "'"); //$NON-NLS-2$ + return installDir; + } + + public static final String COMMAND_FINISH_FILE = "/finished"; + + public static final String COMMAND_LOG_FILE = "/log"; + + public static final int DEFAULT_WEB_SERVER_PORT = 12347; + public static final int DEFAULT_PEER_PORT = 49671; + + public static final String DISCOVERY_PLUGIN = "discovery"; + + public static final String API_TOKEN = "api_token"; + + public static final String PROJECT_TAGS_CONTEXT = "/project-tags"; + + public static final String ANGULAR_WEB_URLS_PATH_SPEC = "/codeine/*"; + + public static final String ANGULAR_RESOURCES_CONTEXT_PATH = "/ajs"; + + public static final String PROJECTS_TABS_CONTEXT = "/projects-tabs"; + + + public static class RequestHeaders { + + public static final String NO_ZIP = "NO_ZIP"; + + } + + public static class UrlParameters { + + public static final String PROJECT_NAME = "project"; + public static final String VERSION_NAME = "version"; + public static final String NODE_NAME = "node-name"; + public static final String NODE = "node"; + public static final String MONITOR = "monitor"; + public static final String COLLECTOR = "collector"; + public static final String COMMAND_NAME = "command"; + public static final String PATH_NAME = "path"; + public static final String RESOURCE_NAME = "resource"; + public static final String LINK_NAME = "link"; + public static final String DATA_NAME = "data"; + public static final String REDIRECT = "redirect"; + public static final String COMMAND_ID = "command-id"; + public static final String DATA_ADDITIONAL_COMMAND_INFO_NAME = "more-command-info"; + public static final String SECTION = "section"; + public static final String VIEW_AS = "viewas"; + public static final String FILTER = "text-filter"; + public static final String FORCE = "force"; + public static final String ADDRESS = "server"; + public static final String INCLUDE_OUTPUT = "output"; + public static final String SYNC_REQUEST = "sync"; + } + + public static String getPersistentDir() { + return getWorkarea() + "/persistent." + InetUtils.getLocalHost().getHostName(); + } + + public static String getWorkarea() { + String workarea = System.getenv(ENV_CODEINE_WORKAREA); + if (null == workarea) { + return getInstallDir(); + } + return workarea; + } + + public static String getHostWorkareaDir() { // return Constants.getInstallDir() + "/../.."; - String workarea = System.getenv(ENV_CODEINE_HOST_WORKAREA); - if (null == workarea){ - return getPersistentDir();//assuming workarea is common somehow, so pid+port files will be separate for each host - } - return workarea; - } + String workarea = System.getenv(ENV_CODEINE_HOST_WORKAREA); + if (null == workarea) { + return getPersistentDir();//assuming workarea is common somehow, so pid+port files will be separate for each host + } + return workarea; + } + + public static String apiContext(String projectNodesContext) { + return API_CONTEXT + projectNodesContext; + } - public static String apiContext(String projectNodesContext) { - return API_CONTEXT + projectNodesContext; - } - public static String apiTokenContext(String projectNodesContext) { - return API_WITH_TOKEN_CONTEXT + projectNodesContext; - } + public static String apiTokenContext(String projectNodesContext) { + return API_WITH_TOKEN_CONTEXT + projectNodesContext; + } - public static final String OFFLINE_NODES = "Codeine daemon not running"; - public static final String NOT_REPORTING_NODES = "Codeine daemon not reporting node"; + public static final String OFFLINE_NODES = "Codeine daemon not running"; + public static final String NOT_REPORTING_NODES = "Codeine daemon not reporting node"; - public static final String CODEINE_API_TOKEN_DERIVER = "CodeineSecretKey"; + public static final String CODEINE_API_TOKEN_DERIVER = "CodeineSecretKey"; - public static String getFeatureFlagsDir() { - return getWorkarea() + "/features"; - } + public static String getFeatureFlagsDir() { + return getWorkarea() + "/features"; + } } diff --git a/src/peer/codeine/RunMonitors.java b/src/peer/codeine/RunMonitors.java index e04b9cfa..1231f139 100755 --- a/src/peer/codeine/RunMonitors.java +++ b/src/peer/codeine/RunMonitors.java @@ -1,5 +1,7 @@ package codeine; +import static com.google.common.collect.Maps.newHashMap; + import codeine.api.MonitorStatusInfo; import codeine.api.NodeInfo; import codeine.configuration.IConfigurationManager; @@ -7,6 +9,7 @@ import codeine.configuration.PathHelper; import codeine.credentials.CredHelper; import codeine.executer.Task; +import codeine.jsons.global.GlobalConfigurationJsonStore; import codeine.jsons.nodes.NodeDiscoveryStrategy; import codeine.jsons.peer_status.PeerStatus; import codeine.jsons.project.ProjectJson; @@ -29,8 +32,6 @@ import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; -import org.apache.log4j.Logger; - import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -39,50 +40,55 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; - -import static com.google.common.collect.Maps.newHashMap; +import org.apache.log4j.Logger; public class RunMonitors implements Task { - private IConfigurationManager configurationManager; - private String projectName; - private static final Logger log = Logger.getLogger(RunMonitors.class); - private static final int MAX_OUTPUT_SIZE = 1000000; - private Map lastRun = newHashMap(); - private PeerStatus projectStatusUpdater; - private final MailSender mailSender; - private final PathHelper pathHelper; - private NodeInfo node; - private NotificationDeliverToDatabase notificationDeliverToMongo; - private PeerStatusChangedUpdater mongoPeerStatusUpdater; - private SnoozeKeeper snoozeKeeper; - private ShellScript shellScript; - public RunMonitors(IConfigurationManager configurationManager, String project, PeerStatus projectStatusUpdater, MailSender mailSender, - PathHelper pathHelper, NodeInfo node, NotificationDeliverToDatabase notificationDeliverToMongo, - PeerStatusChangedUpdater mongoPeerStatusUpdater, SnoozeKeeper snoozeKeeper) { - this.configurationManager = configurationManager; - this.projectName = project; - this.projectStatusUpdater = projectStatusUpdater; - this.mailSender = mailSender; - this.pathHelper = pathHelper; - this.node = node; - this.notificationDeliverToMongo = notificationDeliverToMongo; - this.mongoPeerStatusUpdater = mongoPeerStatusUpdater; - this.snoozeKeeper = snoozeKeeper; - init(); - } + private GlobalConfigurationJsonStore globalConfigurationJsonStore; + private IConfigurationManager configurationManager; + private String projectName; + private static final Logger log = Logger.getLogger(RunMonitors.class); + private static final int MAX_OUTPUT_SIZE = 1000000; + private Map lastRun = newHashMap(); + private PeerStatus projectStatusUpdater; + private final MailSender mailSender; + private final PathHelper pathHelper; + private NodeInfo node; + private NotificationDeliverToDatabase notificationDeliverToMongo; + private PeerStatusChangedUpdater mongoPeerStatusUpdater; + private SnoozeKeeper snoozeKeeper; + private ShellScript shellScript; - private void init() { - String monitorOutputDirWithNode = pathHelper.getMonitorOutputDirWithNode(project().name(), node.name()); - FilesUtils.mkdirs(monitorOutputDirWithNode); - } + public RunMonitors(IConfigurationManager configurationManager, String project, + PeerStatus projectStatusUpdater, MailSender mailSender, + PathHelper pathHelper, NodeInfo node, + NotificationDeliverToDatabase notificationDeliverToMongo, + PeerStatusChangedUpdater mongoPeerStatusUpdater, SnoozeKeeper snoozeKeeper, GlobalConfigurationJsonStore globalConfigurationJsonStore) { + this.configurationManager = configurationManager; + this.projectName = project; + this.projectStatusUpdater = projectStatusUpdater; + this.mailSender = mailSender; + this.pathHelper = pathHelper; + this.node = node; + this.notificationDeliverToMongo = notificationDeliverToMongo; + this.mongoPeerStatusUpdater = mongoPeerStatusUpdater; + this.snoozeKeeper = snoozeKeeper; + this.globalConfigurationJsonStore = globalConfigurationJsonStore; + init(); + } - private ProjectJson project() { - return configurationManager.getProjectForName(projectName); - } + private void init() { + String monitorOutputDirWithNode = pathHelper + .getMonitorOutputDirWithNode(project().name(), node.name()); + FilesUtils.mkdirs(monitorOutputDirWithNode); + } - @Override - public void run() { + private ProjectJson project() { + return configurationManager.getProjectForName(projectName); + } + + @Override + public void run() { try { List monitors = Lists.newArrayList(project().monitors()); removeNonExistMonitors(); @@ -103,233 +109,269 @@ public void run() { } else if (project().node_discovery_startegy() == NodeDiscoveryStrategy.Configuration) { updateTagsByConfiguration(); } - } - catch (Throwable e) - { + } catch (Throwable e) { log.error("Error in RunMonitors", e); } - } + } - private void validateDiskSpaceForMonitorRun() { - File tempDir = new File(System.getProperty("java.io.tmpdir")); - if (tempDir.getUsableSpace() < 100){ - log.warn("not enough space to run monitors " + tempDir + " usableSpace: " + tempDir.getUsableSpace() + " , but it might work on the backup dir"); + private void validateDiskSpaceForMonitorRun() { + File tempDir = new File(System.getProperty("java.io.tmpdir")); + if (tempDir.getUsableSpace() < 100) { + log.warn("not enough space to run monitors " + tempDir + " usableSpace: " + tempDir + .getUsableSpace() + " , but it might work on the backup dir"); // snoozeKeeper.snoozeAll(); - } - } + } + } - private void updateTagsByConfiguration() { - projectStatusUpdater.updateTags(project(), node.name(), node.alias(), node.tags()); - } + private void updateTagsByConfiguration() { + projectStatusUpdater.updateTags(project(), node.name(), node.alias(), node.tags()); + } - private void removeNonExistMonitors() { - boolean removed = projectStatusUpdater.removeNonExistMonitors(project(), node.name(), node.alias()); - if (removed) { - updateStatusInMongo(); - } - } + private void removeNonExistMonitors() { + boolean removed = projectStatusUpdater + .removeNonExistMonitors(project(), node.name(), node.alias()); + if (removed) { + updateStatusInMongo(); + } + } - @SuppressWarnings("serial") - private void updateTagsByScript() { - ProjectJson project = project(); - if (project.node_discovery_startegy() != NodeDiscoveryStrategy.Script || StringUtils.isEmpty(project.tags_discovery_script())) { - log.info("tags discovery is not configured for project " + projectName); - return; - } - Map env = Maps.newHashMap(); - env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); - env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); - env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString(projectStatusUpdater.getTags(project.name(), node.name()), ";")); - env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); - env.putAll(project.environmentVariables()); - ShellScript script = new ShellScript( - "tags_" + projectName + "_" + node.name(), project.tags_discovery_script(), project.operating_system(), null, pathHelper.getProjectDir(projectName), env, null); - String tags = script.execute().outputFromFile(); - if (tags.isEmpty()){ - tags = "[]"; - } - List tagsList = new Gson().fromJson(tags, new TypeToken>(){}.getType()); - List prevTags = projectStatusUpdater.updateTags(project, node.name(), node.alias(), tagsList); - if (!tagsList.equals(prevTags)) { - updateStatusInMongo(); - } - } - - private void updateVersion() { - ProjectJson project = project(); - if (StringUtils.isEmpty(project.version_detection_script())) { - log.info("version is not configured for project " + projectName); - return; - } - Map env = Maps.newHashMap(); - env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); - env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); - env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString(projectStatusUpdater.getTags(project.name(), node.name()), ";")); - env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); - env.putAll(project.environmentVariables()); - ShellScript script = new ShellScript( - "version_" + projectName + "_" + node.name(), project.version_detection_script(), project.operating_system(), null, pathHelper.getProjectDir(projectName), env, null); - String version = script.execute().outputFromFile(); - if (version.isEmpty()){ - version = Constants.NO_VERSION; - } - String prevVersion = projectStatusUpdater.updateVersion(project, node.name(), node.alias(), version); - if (!version.equals(prevVersion)) { - updateStatusInMongo(); - } - } + @SuppressWarnings("serial") + private void updateTagsByScript() { + ProjectJson project = project(); + if (project.node_discovery_startegy() != NodeDiscoveryStrategy.Script || StringUtils + .isEmpty(project.tags_discovery_script())) { + log.info("tags discovery is not configured for project " + projectName); + return; + } + Map env = Maps.newHashMap(); + env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); + env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); + env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils + .collectionToString(projectStatusUpdater.getTags(project.name(), node.name()), ";")); + env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER, + globalConfigurationJsonStore.get().web_server_host()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT, + globalConfigurationJsonStore.get().web_server_port().toString()); + env.putAll(project.environmentVariables()); + ShellScript script = new ShellScript( + "tags_" + projectName + "_" + node.name(), project.tags_discovery_script(), + project.operating_system(), null, pathHelper.getProjectDir(projectName), env, null); + String tags = script.execute().outputFromFile(); + if (tags.isEmpty()) { + tags = "[]"; + } + List tagsList = new Gson().fromJson(tags, new TypeToken>() { + }.getType()); + List prevTags = projectStatusUpdater + .updateTags(project, node.name(), node.alias(), tagsList); + if (!tagsList.equals(prevTags)) { + updateStatusInMongo(); + } + } - private void runMonitorOnce(NodeMonitor monitor) { - Long lastRuntime = lastRun.get(monitor.name()); - if (lastRuntime == null || System.currentTimeMillis() - lastRuntime > minInterval(monitor)) { - try { - runMonitor(monitor); - } catch (Exception e) { - log.warn("got exception when executing monitor ", e); - } - lastRun.put(monitor.name(), System.currentTimeMillis()); - } else { - log.info("skipping monitor " + monitor); - } - } + private void updateVersion() { + ProjectJson project = project(); + if (StringUtils.isEmpty(project.version_detection_script())) { + log.info("version is not configured for project " + projectName); + return; + } + Map env = Maps.newHashMap(); + env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); + env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); + env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils + .collectionToString(projectStatusUpdater.getTags(project.name(), node.name()), ";")); + env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER, + globalConfigurationJsonStore.get().web_server_host()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT, + globalConfigurationJsonStore.get().web_server_port().toString()); + env.putAll(project.environmentVariables()); + ShellScript script = new ShellScript( + "version_" + projectName + "_" + node.name(), project.version_detection_script(), + project.operating_system(), null, pathHelper.getProjectDir(projectName), env, null); + String version = script.execute().outputFromFile(); + if (version.isEmpty()) { + version = Constants.NO_VERSION; + } + String prevVersion = projectStatusUpdater + .updateVersion(project, node.name(), node.alias(), version); + if (!version.equals(prevVersion)) { + updateStatusInMongo(); + } + } - private int minInterval(NodeMonitor c) { - if (null == c.minInterval()) { - return 20000; - } - return c.minInterval() * 60000; - } + private void runMonitorOnce(NodeMonitor monitor) { + Long lastRuntime = lastRun.get(monitor.name()); + if (lastRuntime == null || System.currentTimeMillis() - lastRuntime > minInterval( + monitor)) { + try { + runMonitor(monitor); + } catch (Exception e) { + log.warn("got exception when executing monitor ", e); + } + lastRun.put(monitor.name(), System.currentTimeMillis()); + } else { + log.info("skipping monitor " + monitor); + } + } - private void runMonitor(NodeMonitor monitor) { - Result res = null; - Stopwatch stopwatch = Stopwatch.createStarted(); - try { - boolean hasCredentials = hasCredentials(monitor); - List cmd = buildCmd(monitor, hasCredentials); - log.debug("will execute " + cmd); - log.debug("will execute encoded " + cmd); - Map map = Maps.newHashMap(); - map.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); - map.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); - map.put(Constants.EXECUTION_ENV_PROJECT_NAME, projectName); - map.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString(projectStatusUpdater.getTags(project().name(), node.name()), ";")); - map.putAll(project().environmentVariables()); - res = new ProcessExecuterBuilder(cmd, pathHelper.getProjectDir(project().name())).user(monitor.credentials()).env(map).build().execute(); - } catch (Exception e) { - res = new Result(ExitStatus.EXCEPTION, e.getMessage()); - log.debug("error in monitor", e); - } - stopwatch.stop(); - // long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS); - if (null == res.output()) { - res.output("No Output\n"); - } - writeResult(res, monitor, stopwatch); - String result = String.valueOf(res.success()); - MonitorStatusInfo monitorInfo = new MonitorStatusInfo(monitor.name(), result); - String previousResult = updateStatusInDatastore(monitorInfo); - log.info("monitor '" + monitor.name() + "' ended with result: " + res.success() + " , previous result " + previousResult + ", took: " + stopwatch); - if (shouldSendStatusToMongo(result, previousResult)) { - updateStatusInMongo(); - } - if (monitor.notification_enabled()) { - if (Constants.IS_MAIL_STARTEGY_MONGO) { - if (shouldSendNotificationToMongo(res, previousResult)) { - notificationDeliverToMongo.sendCollectorResult(monitor.name(), node, project(), res.output(), res.exit(), stopwatch.toString(), false, 1); - } - } else { - if (null == previousResult) { - previousResult = result; - } - mailSender.sendMailIfNeeded(Boolean.valueOf(result), Boolean.valueOf(previousResult), monitor, node, - res.output(), project()); - } - } else { - log.debug("notification not enabled for " + monitor); - } + private int minInterval(NodeMonitor c) { + if (null == c.minInterval()) { + return 20000; + } + return c.minInterval() * 60000; + } - } + private void runMonitor(NodeMonitor monitor) { + Result res = null; + Stopwatch stopwatch = Stopwatch.createStarted(); + try { + boolean hasCredentials = hasCredentials(monitor); + List cmd = buildCmd(monitor, hasCredentials); + log.debug("will execute " + cmd); + log.debug("will execute encoded " + cmd); + Map map = Maps.newHashMap(); + map.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); + map.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); + map.put(Constants.EXECUTION_ENV_PROJECT_NAME, projectName); + map.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils + .collectionToString(projectStatusUpdater.getTags(project().name(), node.name()), + ";")); + map.put(Constants.EXECUTION_ENV_CODEINE_SERVER, + globalConfigurationJsonStore.get().web_server_host()); + map.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT, + globalConfigurationJsonStore.get().web_server_port().toString()); + map.putAll(project().environmentVariables()); + res = new ProcessExecuterBuilder(cmd, pathHelper.getProjectDir(project().name())) + .user(monitor.credentials()).env(map).build().execute(); + } catch (Exception e) { + res = new Result(ExitStatus.EXCEPTION, e.getMessage()); + log.debug("error in monitor", e); + } + stopwatch.stop(); + // long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS); + if (null == res.output()) { + res.output("No Output\n"); + } + writeResult(res, monitor, stopwatch); + String result = String.valueOf(res.success()); + MonitorStatusInfo monitorInfo = new MonitorStatusInfo(monitor.name(), result); + String previousResult = updateStatusInDatastore(monitorInfo); + log.info("monitor '" + monitor.name() + "' ended with result: " + res.success() + + " , previous result " + previousResult + ", took: " + stopwatch); + if (shouldSendStatusToMongo(result, previousResult)) { + updateStatusInMongo(); + } + if (monitor.notification_enabled()) { + if (Constants.IS_MAIL_STARTEGY_MONGO) { + if (shouldSendNotificationToMongo(res, previousResult)) { + notificationDeliverToMongo + .sendCollectorResult(monitor.name(), node, project(), res.output(), + res.exit(), stopwatch.toString(), false, 1); + } + } else { + if (null == previousResult) { + previousResult = result; + } + mailSender + .sendMailIfNeeded(Boolean.valueOf(result), Boolean.valueOf(previousResult), + monitor, node, + res.output(), project()); + } + } else { + log.debug("notification not enabled for " + monitor); + } + + } - private void updateStatusInMongo() { - mongoPeerStatusUpdater.pushUpdate("Plain Old Monitor"); - } + private void updateStatusInMongo() { + mongoPeerStatusUpdater.pushUpdate("Plain Old Monitor"); + } - private boolean shouldSendNotificationToMongo(Result res, String previousResult) { - if (Constants.RUNNING_COLLECTORS_IN_PEER) { - return false; - } - if (snoozeKeeper.isSnooze(project().name(), node.name())) { - log.info("in snooze period"); - return false; - } - return null != previousResult && Boolean.valueOf(previousResult) && !res.success(); - } + private boolean shouldSendNotificationToMongo(Result res, String previousResult) { + if (Constants.RUNNING_COLLECTORS_IN_PEER) { + return false; + } + if (snoozeKeeper.isSnooze(project().name(), node.name())) { + log.info("in snooze period"); + return false; + } + return null != previousResult && Boolean.valueOf(previousResult) && !res.success(); + } - private boolean shouldSendStatusToMongo(String result, String previousResult) { - return !result.equals(previousResult); - } + private boolean shouldSendStatusToMongo(String result, String previousResult) { + return !result.equals(previousResult); + } - protected boolean hasCredentials(NodeMonitor collector) { - return collector.credentials() != null; - } + protected boolean hasCredentials(NodeMonitor collector) { + return collector.credentials() != null; + } - private String updateStatusInDatastore(MonitorStatusInfo monitor) { - return projectStatusUpdater.updateStatus(project(), monitor, node.name(), node.alias()); - } + private String updateStatusInDatastore(MonitorStatusInfo monitor) { + return projectStatusUpdater.updateStatus(project(), monitor, node.name(), node.alias()); + } - private List buildCmd(NodeMonitor c, boolean hasCredentials) { - String fileName = pathHelper.getMonitorsDir(project().name()) + File.separator + node.name() + "_" + c.name(); - if (c.script_content() != null) { - if (null != shellScript){ - log.warn("'shellScript' should be null but not", new RuntimeException()); - LogUtils.assertFailed(log, "'shellScript' should be null but not"); - } - fileName += node.name(); - shellScript = new ShellScript(fileName, c.script_content(), project().operating_system(), null, null, null, null); - fileName = shellScript.create(); - log.debug("file is " + fileName); - } - else if (FilesUtils.exists(fileName)){ //TODO remove after build 1100 - log.warn("monitor is in old format " + fileName); - } - else { - throw new RuntimeException("monitor is missing " + fileName); - } - List cmd = new ArrayList(); - if (project().operating_system() == OperatingSystem.Windows){ - cmd.add("cmd"); - cmd.add("/c"); - cmd.add("call"); - cmd.add(fileName); - } - else if (hasCredentials) { - cmd.add(PathHelper.getReadLogs()); - cmd.add(encode(c.credentials())); - cmd.add(encode("/bin/sh")); - cmd.add(encode("-xe")); - cmd.add(encode(fileName)); - } else { - cmd.add("/bin/sh"); - cmd.add("-xe"); - cmd.add(fileName); - } - return cmd; - } + private List buildCmd(NodeMonitor c, boolean hasCredentials) { + String fileName = + pathHelper.getMonitorsDir(project().name()) + File.separator + node.name() + "_" + c + .name(); + if (c.script_content() != null) { + if (null != shellScript) { + log.warn("'shellScript' should be null but not", new RuntimeException()); + LogUtils.assertFailed(log, "'shellScript' should be null but not"); + } + fileName += node.name(); + shellScript = new ShellScript(fileName, c.script_content(), + project().operating_system(), null, null, null, null); + fileName = shellScript.create(); + log.debug("file is " + fileName); + } else if (FilesUtils.exists(fileName)) { //TODO remove after build 1100 + log.warn("monitor is in old format " + fileName); + } else { + throw new RuntimeException("monitor is missing " + fileName); + } + List cmd = new ArrayList(); + if (project().operating_system() == OperatingSystem.Windows) { + cmd.add("cmd"); + cmd.add("/c"); + cmd.add("call"); + cmd.add(fileName); + } else if (hasCredentials) { + cmd.add(PathHelper.getReadLogs()); + cmd.add(encode(c.credentials())); + cmd.add(encode("/bin/sh")); + cmd.add(encode("-xe")); + cmd.add(encode(fileName)); + } else { + cmd.add("/bin/sh"); + cmd.add("-xe"); + cmd.add(fileName); + } + return cmd; + } - private String encode(final String value1) { - return CredHelper.encode(value1); - } + private String encode(final String value1) { + return CredHelper.encode(value1); + } - private void writeResult(Result res, NodeMonitor collector, Stopwatch stopwatch) { - String file = pathHelper.getMonitorOutputDirWithNode(project().name(), node.name()) + "/" + HttpUtils.specialEncode(collector.name()) - + ".txt"; - log.debug("Output for " + collector.name() + " will be written to: " + file); + private void writeResult(Result res, NodeMonitor collector, Stopwatch stopwatch) { + String file = + pathHelper.getMonitorOutputDirWithNode(project().name(), node.name()) + "/" + HttpUtils + .specialEncode(collector.name()) + + ".txt"; + log.debug("Output for " + collector.name() + " will be written to: " + file); // NodeWithMonitorsInfo nodeInfo = projectStatusUpdater.nodeInfo(project(), node.name(), node.alias()); - try (BufferedWriter out = new BufferedWriter(new FileWriter(file));) { - log.debug("writing the new format"); - String output = res.output() == null || res.output().length() <= MAX_OUTPUT_SIZE ? res.output() : "\nOutput too long...\n" + res.output().substring(res.output().length() - MAX_OUTPUT_SIZE); - MonitorExecutionResult monitorExecutionResult = new MonitorExecutionResult(collector.name(), res.exit(), output, stopwatch.elapsed(TimeUnit.MILLISECONDS), System.currentTimeMillis()); - out.write(new Gson().toJson(monitorExecutionResult)); + try (BufferedWriter out = new BufferedWriter(new FileWriter(file));) { + log.debug("writing the new format"); + String output = + res.output() == null || res.output().length() <= MAX_OUTPUT_SIZE ? res.output() + : "\nOutput too long...\n" + res.output() + .substring(res.output().length() - MAX_OUTPUT_SIZE); + MonitorExecutionResult monitorExecutionResult = new MonitorExecutionResult( + collector.name(), res.exit(), output, stopwatch.elapsed(TimeUnit.MILLISECONDS), + System.currentTimeMillis()); + out.write(new Gson().toJson(monitorExecutionResult)); // out.write("+------------------------------------------------------------------+\n"); // out.write("| monitor: " + collector.name() + "\n"); // if (hasCredentials(collector)) { @@ -344,14 +386,14 @@ private void writeResult(Result res, NodeMonitor collector, Stopwatch stopwatch) // out.write("| version: " + nodeInfo.version() + "\n"); // out.write("+------------------------------------------------------------------+\n"); // out.write(res.output); - } catch (IOException e) { - throw ExceptionUtils.asUnchecked(e); - } - } + } catch (IOException e) { + throw ExceptionUtils.asUnchecked(e); + } + } - @Override - public String toString() { - return "RunMonitors [project=" + project() + "]"; - } + @Override + public String toString() { + return "RunMonitors [project=" + project() + "]"; + } } diff --git a/src/peer/codeine/collectors/OneCollectorRunner.java b/src/peer/codeine/collectors/OneCollectorRunner.java index 047cec80..49366a86 100644 --- a/src/peer/codeine/collectors/OneCollectorRunner.java +++ b/src/peer/codeine/collectors/OneCollectorRunner.java @@ -8,6 +8,7 @@ import codeine.jsons.collectors.CollectorExecutionInfo; import codeine.jsons.collectors.CollectorExecutionInfoWithResult; import codeine.jsons.collectors.CollectorInfo; +import codeine.jsons.global.GlobalConfigurationJsonStore; import codeine.jsons.info.CodeineRuntimeInfo; import codeine.jsons.peer_status.PeerStatus; import codeine.jsons.project.ProjectJson; @@ -28,187 +29,216 @@ import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.inject.assistedinject.Assisted; -import org.apache.log4j.Logger; - -import javax.inject.Inject; import java.io.File; import java.util.Map; import java.util.concurrent.TimeUnit; +import javax.inject.Inject; +import org.apache.log4j.Logger; public class OneCollectorRunner implements IOneCollectorRunner { - private static final int MIN_INTERVAL_MILLI = 20000; - private static final Logger log = Logger.getLogger(OneCollectorRunner.class); - @Inject private PathHelper pathHelper; - @Inject private FeatureFlags featureFlags; - @Inject private PeerStatus peerStatus; - @Inject private Gson gson; - @Inject private PeerStatusChangedUpdater peerStatusChangedUpdater; - @Inject private SnoozeKeeper snoozeKeeper; - @Inject private NotificationDeliverToDatabase notificationDeliverToDatabase; - @Inject private CodeineRuntimeInfo codeineRuntimeInfo; - private CollectorInfo collectorInfo; - private ProjectJson project; - private NodeInfo node; - private Long lastRuntime; - private Result result; - private Result previousResult; - private Stopwatch stopwatch; - private LoadingCache notificationsCount = CacheBuilder.newBuilder() - .maximumSize(100) - .expireAfterWrite(24, TimeUnit.HOURS) - .build( - new CacheLoader() { - @Override - public Object load(Long key) { - return new Object(); - } - }); - - @Inject - public OneCollectorRunner(@Assisted CollectorInfo collector, @Assisted ProjectJson project, @Assisted NodeInfo node) { - this.collectorInfo = collector; - this.project = project; - this.node = node; - } - - @Override - public void execute() { - runOnceCheckMinInterval(); - } - - private void runOnceCheckMinInterval() { - if (featureFlags.isCollectorsDisabled()) { - log.info("collectors are disabled"); - return; - } - if (lastRuntime == null || System.currentTimeMillis() - lastRuntime > minInterval()) { - try { - runOnce(); - } catch (Exception e) { - log.warn("got exception when executing collector ", e); - } - lastRuntime = System.currentTimeMillis(); - } else { - log.info("skipping collector " + collectorInfo); - } - } - - private void runOnce() { - ShellScript shellScript = createShellScript(); - long startTime = System.currentTimeMillis(); - stopwatch = Stopwatch.createStarted(); - executeScriptAndDeleteIt(shellScript); - stopwatch.stop(); - if (null == result.output()) { - result.output("No Output\n"); - } - CollectorExecutionInfo info = new CollectorExecutionInfo(collectorInfo.name(), collectorInfo.type(), result.exit(), outputFromFile(), stopwatch.elapsed(TimeUnit.MILLISECONDS), startTime); - CollectorExecutionInfoWithResult resultWrapped = new CollectorExecutionInfoWithResult(info, result); - processResult(resultWrapped, stopwatch); - } - - public String outputFromFile() { - if (null == result || null == result.outputFromFile()) { - return ""; - } - return result.outputFromFile(); - } - - private void processResult(CollectorExecutionInfoWithResult resultWrapped, Stopwatch stopwatch) { - resultWrapped.result().limitOutputLength(); - writeResult(resultWrapped); - CollectorExecutionInfo lastValue = updateStatusInDataset(resultWrapped.info()); - log.info("collector '" + collectorInfo.name() + "' took:" + stopwatch + " result:" + resultWrapped.info().valueAndExitStatus() + (null != lastValue ? " previous:" + lastValue.valueAndExitStatus() : "")); - updateDatastoreIfNeeded(); - sendNotificationIfNeeded(); - } - - private ShellScript createShellScript() { - ShellScript shellScript = new ShellScript(getKey(), collectorInfo.script_content(), project.operating_system(), null, pathHelper.getProjectDir(project.name()), prepareEnv(), collectorInfo.cred()); - shellScript.create(); - return shellScript; - } - - private String getKey() { - return codeineRuntimeInfo.port() + "_" + pathHelper.getMonitorsDir(project.name()) + File.separator + node.name() + "_" + collectorInfo.name(); - } - - private void executeScriptAndDeleteIt(ShellScript shellScript) { - try { - result = shellScript.execute(); - } catch (Exception ex) { - result = new Result(ExitStatus.EXCEPTION, ex.getMessage()); - log.debug("error in collector", ex); - } finally { - shellScript.delete(); - } - } - - private Map prepareEnv() { - Map env = Maps.newHashMap(); - env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); - env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); - env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); - env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString(peerStatus.getTags(project.name(), node.name()), ";")); - env.putAll(project.environmentVariables()); - return env; - } + private static final int MIN_INTERVAL_MILLI = 20000; + private static final Logger log = Logger.getLogger(OneCollectorRunner.class); + @Inject + private PathHelper pathHelper; + @Inject + private FeatureFlags featureFlags; + @Inject + private PeerStatus peerStatus; + @Inject + private Gson gson; + @Inject + private PeerStatusChangedUpdater peerStatusChangedUpdater; + @Inject + private SnoozeKeeper snoozeKeeper; + @Inject + private NotificationDeliverToDatabase notificationDeliverToDatabase; + @Inject + private CodeineRuntimeInfo codeineRuntimeInfo; + @Inject + private GlobalConfigurationJsonStore globalConfigurationJsonStore; + private CollectorInfo collectorInfo; + private ProjectJson project; + private NodeInfo node; + private Long lastRuntime; + private Result result; + private Result previousResult; + private Stopwatch stopwatch; + private LoadingCache notificationsCount = CacheBuilder.newBuilder() + .maximumSize(100) + .expireAfterWrite(24, TimeUnit.HOURS) + .build( + new CacheLoader() { + @Override + public Object load(Long key) { + return new Object(); + } + }); + + @Inject + public OneCollectorRunner(@Assisted CollectorInfo collector, @Assisted ProjectJson project, + @Assisted NodeInfo node) { + this.collectorInfo = collector; + this.project = project; + this.node = node; + } + + @Override + public void execute() { + runOnceCheckMinInterval(); + } + + private void runOnceCheckMinInterval() { + if (featureFlags.isCollectorsDisabled()) { + log.info("collectors are disabled"); + return; + } + if (lastRuntime == null || System.currentTimeMillis() - lastRuntime > minInterval()) { + try { + runOnce(); + } catch (Exception e) { + log.warn("got exception when executing collector ", e); + } + lastRuntime = System.currentTimeMillis(); + } else { + log.info("skipping collector " + collectorInfo); + } + } + + private void runOnce() { + ShellScript shellScript = createShellScript(); + long startTime = System.currentTimeMillis(); + stopwatch = Stopwatch.createStarted(); + executeScriptAndDeleteIt(shellScript); + stopwatch.stop(); + if (null == result.output()) { + result.output("No Output\n"); + } + CollectorExecutionInfo info = new CollectorExecutionInfo(collectorInfo.name(), + collectorInfo.type(), result.exit(), outputFromFile(), + stopwatch.elapsed(TimeUnit.MILLISECONDS), startTime); + CollectorExecutionInfoWithResult resultWrapped = new CollectorExecutionInfoWithResult(info, + result); + processResult(resultWrapped, stopwatch); + } + + public String outputFromFile() { + if (null == result || null == result.outputFromFile()) { + return ""; + } + return result.outputFromFile(); + } + + private void processResult(CollectorExecutionInfoWithResult resultWrapped, + Stopwatch stopwatch) { + resultWrapped.result().limitOutputLength(); + writeResult(resultWrapped); + CollectorExecutionInfo lastValue = updateStatusInDataset(resultWrapped.info()); + log.info("collector '" + collectorInfo.name() + "' took:" + stopwatch + " result:" + + resultWrapped.info().valueAndExitStatus() + (null != lastValue ? " previous:" + + lastValue.valueAndExitStatus() : "")); + updateDatastoreIfNeeded(); + sendNotificationIfNeeded(); + } + + private ShellScript createShellScript() { + ShellScript shellScript = new ShellScript(getKey(), collectorInfo.script_content(), + project.operating_system(), null, pathHelper.getProjectDir(project.name()), + prepareEnv(), collectorInfo.cred()); + shellScript.create(); + return shellScript; + } + + private String getKey() { + return codeineRuntimeInfo.port() + "_" + pathHelper.getMonitorsDir(project.name()) + + File.separator + node.name() + "_" + collectorInfo.name(); + } + + private void executeScriptAndDeleteIt(ShellScript shellScript) { + try { + result = shellScript.execute(); + } catch (Exception ex) { + result = new Result(ExitStatus.EXCEPTION, ex.getMessage()); + log.debug("error in collector", ex); + } finally { + shellScript.delete(); + } + } + private Map prepareEnv() { + Map env = Maps.newHashMap(); + env.put(Constants.EXECUTION_ENV_NODE_NAME, node.name()); + env.put(Constants.EXECUTION_ENV_NODE_ALIAS, node.alias()); + env.put(Constants.EXECUTION_ENV_PROJECT_NAME, project.name()); + env.put(Constants.EXECUTION_ENV_NODE_TAGS, + StringUtils.collectionToString(peerStatus.getTags(project.name(), node.name()), ";")); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER, + globalConfigurationJsonStore.get().web_server_host()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT, + globalConfigurationJsonStore.get().web_server_port().toString()); + env.putAll(project.environmentVariables()); + return env; + } private void sendNotificationIfNeeded() { - if (new NotificationChecker().shouldSendNotification(snoozeKeeper, collectorInfo, project.name(), node.name(), notificationsCount, result, previousResult)) { + if (new NotificationChecker() + .shouldSendNotification(snoozeKeeper, collectorInfo, project.name(), node.name(), + notificationsCount, result, previousResult)) { notificationDeliverToDatabase.sendCollectorResult( - collectorInfo.name(), node, project, result.output(), result.exit(), stopwatch.toString(), - true, (int)notificationsCount.size()); + collectorInfo.name(), node, project, result.output(), result.exit(), + stopwatch.toString(), + true, (int) notificationsCount.size()); } previousResult = result; } + private boolean shouldUpdate() { + if (result == null) { + return false; + } + if (previousResult == null) { + return true; + } + boolean shouldUpdate; + shouldUpdate = !MiscUtils.equals(result.outputFromFile(), previousResult.outputFromFile()) + || !MiscUtils.equals(result.exit(), previousResult.exit()); + if (shouldUpdate) { + LogUtils.info(log, "collector should update", result.outputFromFile(), + previousResult.outputFromFile(), result.exit(), previousResult.exit()); + } + return shouldUpdate; + } + + private void updateDatastoreIfNeeded() { + if (shouldUpdate()) { + peerStatusChangedUpdater.pushUpdate("collector " + collectorInfo.name()); + } + } + + + private CollectorExecutionInfo updateStatusInDataset(CollectorExecutionInfo info) { + return peerStatus.updateStatus(project, info, node.name(), node.alias()); + } - private boolean shouldUpdate() { - if (result == null) { - return false; - } - if (previousResult == null) { - return true; - } - boolean shouldUpdate; - shouldUpdate = !MiscUtils.equals(result.outputFromFile(), previousResult.outputFromFile()) || !MiscUtils.equals(result.exit(), previousResult.exit()); - if (shouldUpdate) { - LogUtils.info(log, "collector should update", result.outputFromFile(), previousResult.outputFromFile(), result.exit(), previousResult.exit()); - } - return shouldUpdate; - } - - private void updateDatastoreIfNeeded() { - if (shouldUpdate()) { - peerStatusChangedUpdater.pushUpdate("collector " + collectorInfo.name()); - } - } - - - private CollectorExecutionInfo updateStatusInDataset(CollectorExecutionInfo info) { - return peerStatus.updateStatus(project, info, node.name(), node.alias()); - } - - private void writeResult(CollectorExecutionInfoWithResult result) { - String file = pathHelper.getCollectorOutputDirWithNode(project.name(), node.name()) + "/" + HttpUtils.specialEncode(collectorInfo.name()) - + ".txt"; - log.debug("Output for " + collectorInfo.name() + " will be written to: " + file); - TextFileUtils.setContents(file, gson.toJson(result)); - } - - private int minInterval() { - if (collectorInfo.min_interval() == null || collectorInfo.min_interval() <= 0) { - return MIN_INTERVAL_MILLI; - } - return collectorInfo.min_interval() * 60000; - } - - public void updateConf(CollectorInfo collectorInfo) { - this.collectorInfo = collectorInfo; - } + private void writeResult(CollectorExecutionInfoWithResult result) { + String file = + pathHelper.getCollectorOutputDirWithNode(project.name(), node.name()) + "/" + HttpUtils + .specialEncode(collectorInfo.name()) + + ".txt"; + log.debug("Output for " + collectorInfo.name() + " will be written to: " + file); + TextFileUtils.setContents(file, gson.toJson(result)); + } + + private int minInterval() { + if (collectorInfo.min_interval() == null || collectorInfo.min_interval() <= 0) { + return MIN_INTERVAL_MILLI; + } + return collectorInfo.min_interval() * 60000; + } + + public void updateConf(CollectorInfo collectorInfo) { + this.collectorInfo = collectorInfo; + } } diff --git a/src/peer/codeine/servlets/CommandNodeServlet.java b/src/peer/codeine/servlets/CommandNodeServlet.java index b4bc134d..3669a4ff 100755 --- a/src/peer/codeine/servlets/CommandNodeServlet.java +++ b/src/peer/codeine/servlets/CommandNodeServlet.java @@ -1,19 +1,5 @@ package codeine.servlets; -import java.io.File; -import java.io.PrintWriter; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.log4j.Logger; -import org.eclipse.jetty.http.HttpStatus; - import codeine.SnoozeKeeper; import codeine.configuration.IConfigurationManager; import codeine.configuration.PathHelper; @@ -23,195 +9,218 @@ import codeine.jsons.command.CommandInfoForSpecificNode; import codeine.jsons.command.CommandParameterInfo; import codeine.jsons.global.ExperimentalConfJsonStore; +import codeine.jsons.global.GlobalConfigurationJsonStore; import codeine.jsons.peer_status.PeerStatus; import codeine.jsons.project.ProjectJson; import codeine.model.Constants; import codeine.model.ExitStatus; import codeine.model.Result; import codeine.servlet.AbstractServlet; -import codeine.utils.FilesUtils; import codeine.utils.StringUtils; import codeine.utils.os.OperatingSystem; import codeine.utils.os_process.ProcessExecuter.ProcessExecuterBuilder; import codeine.utils.os_process.ShellScript; - import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import java.io.File; +import java.io.PrintWriter; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.log4j.Logger; +import org.eclipse.jetty.http.HttpStatus; + +public class CommandNodeServlet extends AbstractServlet { + + private static final Logger log = Logger.getLogger(CommandNodeServlet.class); + private static final long serialVersionUID = 1L; + @Inject + private PathHelper pathHelper; + @Inject + private IConfigurationManager configurationManager; + @Inject + private ExperimentalConfJsonStore experimentalConfJsonStore; + @Inject + private GlobalConfigurationJsonStore globalConfigurationJsonStore; + @Inject + private SnoozeKeeper snoozeKeeper; + @Inject + private PeerStatus projectStatusUpdater; + + + @Override + public void myPost(HttpServletRequest request, HttpServletResponse res) { + log.info("start handle command"); + if (Boolean.parseBoolean(getParameter(request, Constants.UrlParameters.FORCE)) + || experimentalConfJsonStore.get().allow_concurrent_commands_in_peer()) { + executeCommandNotSync(request, res); + } else { + executeCommandSync(request, res); + } + log.info("finished handle command"); + } + + /** + * this prevents multiple commands on the same peer, so preventing upgrade the peer during + * command for example + */ + private void executeCommandNotSync(HttpServletRequest request, HttpServletResponse res) { + executeInternal(request, res); + } + + private synchronized void executeCommandSync(HttpServletRequest request, + HttpServletResponse res) { + executeInternal(request, res); + } + + private void executeInternal(HttpServletRequest request, HttpServletResponse res) { + ShellScript cmdScript = null; + snoozeKeeper.snoozeAll(); + final PrintWriter writer = getWriter(res); + try { + String parameter = Constants.UrlParameters.DATA_NAME; + String data = getParameter(request, parameter); + CommandInfo commandInfo = gson().fromJson(data, CommandInfo.class); + String data2 = getParameter(request, + Constants.UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME); + CommandInfoForSpecificNode commandInfo2 = gson() + .fromJson(data2, CommandInfoForSpecificNode.class); + if (null != commandInfo2.key()) { + String decrypt = EncryptionUtils + .decrypt(Constants.CODEINE_API_TOKEN_DERIVER, commandInfo2.key()); + validateKey(decrypt); + } else { + log.warn("key is null", new RuntimeException()); + } + String dir = pathHelper.getCommandsDir(commandInfo.project_name()); + String script_content = commandInfo.script_content(); + String file = dir + File.separator + commandInfo.command_name(); + ProjectJson project = getProject(commandInfo.project_name()); + boolean windows_peer = project.operating_system() == OperatingSystem.Windows; + if (null != script_content) { + cmdScript = new ShellScript(file, script_content, project.operating_system(), + commandInfo2.tmp_dir(), null, null, null); + file = cmdScript.create(); + } else { + log.info("command not found " + file); + writer.println("command not found " + file); + res.setStatus(HttpStatus.NOT_FOUND_404); + return; + } + List cmd = Lists.newArrayList(); + List cmdForOutput = Lists.newArrayList(); + String cred = commandInfo.cred(); + log.info("executing command " + commandInfo.command_name()); + log.info("credentials: " + cred); + if (!StringUtils.isEmpty(cred) && !windows_peer) { + writer.println("credentials = " + cred); + cmd.add(PathHelper.getReadLogs()); + cmd.add(encodeIfNeeded(cred, cred)); + } + if (windows_peer) { + cmd.add(encodeIfNeeded("cmd", cred)); + cmd.add(encodeIfNeeded("/c", cred)); + cmd.add(encodeIfNeeded("call", cred)); + } else { + cmd.add(encodeIfNeeded("/bin/sh", cred)); + cmd.add(encodeIfNeeded("-xe", cred)); + } + cmd.add(encodeIfNeeded(file, cred)); + if (windows_peer) { + cmdForOutput.add("cmd"); + cmdForOutput.add("/c"); + cmdForOutput.add("call"); + } else { + cmdForOutput.add("/bin/sh"); + cmdForOutput.add("-xe"); + } + cmdForOutput.add(file); + writer.println("$ " + StringUtils.collectionToString(cmdForOutput)); + Function function = input -> { + writer.println(input); + writer.flush(); + return null; + }; + Map env = Maps.newHashMap(); + env.put(Constants.EXECUTION_ENV_PROJECT_NAME, commandInfo.project_name()); + env.put(Constants.EXECUTION_ENV_NODE_NAME, commandInfo2.node_name()); + env.put(Constants.EXECUTION_ENV_NODE_ALIAS, commandInfo2.node_alias()); + env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString( + projectStatusUpdater.getTags(commandInfo.project_name(), commandInfo2.node_name()), + ";")); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER, + globalConfigurationJsonStore.get().web_server_host()); + env.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT, + globalConfigurationJsonStore.get().web_server_port().toString()); + env.putAll(commandInfo2.environment_variables()); + env.putAll(getEnvParams(commandInfo)); + Result result = new ProcessExecuterBuilder(cmd, + pathHelper.getProjectDir(commandInfo.project_name())).cmdForOutput(cmdForOutput) + .timeoutInMinutes(commandInfo.timeoutInMinutes()).function(function).env(env) + .user(cred).build().execute(); + writer.println(Constants.COMMAND_RESULT + result.exit()); + writer.flush(); + log.info("command exit status is " + result.exit()); + } catch (Exception ex) { + try { + log.warn("failed on command execution", ex); + writer.println(Constants.COMMAND_RESULT + ExitStatus.EXCEPTION); + } catch (Exception e) { + log.warn("failed on command execution2", ex); + } + } finally { + if (null != cmdScript) { + cmdScript.delete(); + } + } + } + + private void validateKey(String decrypt) { + List l = Splitter.on("#").splitToList(decrypt); + if (l.size() != 2) { + log.warn("format error"); + return; + } + try { + UUID.fromString(l.get(0)); + } catch (Exception e) { + log.warn("format error bad parameter(0) " + l.get(0), e); + } + try { + long currentTimeMillis = System.currentTimeMillis(); + long serverTime = Long.valueOf(l.get(1)); + if (Math.abs(currentTimeMillis - serverTime) > TimeUnit.MINUTES.toMillis(1)) { + log.warn("time from server does not match"); + } + } catch (NumberFormatException e) { + log.warn("format error bad parameter(1) " + l.get(1), e); + } + } -public class CommandNodeServlet extends AbstractServlet -{ - private static final Logger log = Logger.getLogger(CommandNodeServlet.class); - private static final long serialVersionUID = 1L; - @Inject private PathHelper pathHelper; - @Inject private IConfigurationManager configurationManager; - @Inject private ExperimentalConfJsonStore experimentalConfJsonStore; - @Inject private SnoozeKeeper snoozeKeeper; - @Inject private PeerStatus projectStatusUpdater; - - - @Override - public void myPost(HttpServletRequest request, HttpServletResponse res) { - log.info("start handle command"); - if (Boolean.parseBoolean(getParameter(request, Constants.UrlParameters.FORCE)) || experimentalConfJsonStore.get().allow_concurrent_commands_in_peer()) { - executeCommandNotSync(request, res); - } - else { - executeCommandSync(request, res); - } - log.info("finished handle command"); - } - /** - * this prevents multiple commands on the same peer, so preventing upgrade the peer during command for example - */ - private void executeCommandNotSync(HttpServletRequest request, HttpServletResponse res) { - executeInternal(request, res); - } - private synchronized void executeCommandSync(HttpServletRequest request, HttpServletResponse res) { - executeInternal(request, res); - } - private void executeInternal(HttpServletRequest request, HttpServletResponse res) { - ShellScript cmdScript = null; - snoozeKeeper.snoozeAll(); - final PrintWriter writer = getWriter(res); - try { - String parameter = Constants.UrlParameters.DATA_NAME; - String data = getParameter(request, parameter); - CommandInfo commandInfo = gson().fromJson(data, CommandInfo.class); - String data2 = getParameter(request, Constants.UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME); - CommandInfoForSpecificNode commandInfo2 = gson().fromJson(data2, CommandInfoForSpecificNode.class); - if (null != commandInfo2.key()) { - String decrypt = EncryptionUtils.decrypt(Constants.CODEINE_API_TOKEN_DERIVER, commandInfo2.key()); - validateKey(decrypt); - } - else { - log.warn("key is null", new RuntimeException()); - } - // writer.println("INFO: Executing on node " + commandInfo2.node_alias()); + private Map getEnvParams(CommandInfo commandJson) { + Map $ = Maps.newHashMap(); + for (CommandParameterInfo p : commandJson.parameters()) { + $.put(p.name(), p.value()); + } + return $; + } - String dir = pathHelper.getCommandsDir(commandInfo.project_name()); - String script_content = commandInfo.script_content(); - String file = dir + File.separator + commandInfo.command_name(); - ProjectJson project = getProject(commandInfo.project_name()); - boolean windows_peer = project.operating_system() == OperatingSystem.Windows; - if (null != script_content){ - //new - cmdScript = new ShellScript(file, script_content, project.operating_system(), commandInfo2.tmp_dir(), null, null, null); - file = cmdScript.create(); - } - else if (FilesUtils.exists(file)) { //TODO remove after build 1100 - log.warn("command is in old file format: " + file); - } - else { - log.info("command not found " + file); - writer.println("command not found " + file); - res.setStatus(HttpStatus.NOT_FOUND_404); - return; - } - List cmd = Lists.newArrayList(); - List cmdForOutput = Lists.newArrayList(); - String cred = commandInfo.cred(); - log.info("executing command " + commandInfo.command_name()); - log.info("credentials: " + cred); - if (!StringUtils.isEmpty(cred) && !windows_peer){ - writer.println("credentials = " + cred); - cmd.add(PathHelper.getReadLogs()); - cmd.add(encodeIfNeeded(cred, cred)); - } - if (windows_peer) { - cmd.add(encodeIfNeeded("cmd", cred)); - cmd.add(encodeIfNeeded("/c", cred)); - cmd.add(encodeIfNeeded("call", cred)); - } - else { - cmd.add(encodeIfNeeded("/bin/sh", cred)); - cmd.add(encodeIfNeeded("-xe", cred)); - } - cmd.add(encodeIfNeeded(file, cred)); - if (windows_peer) { - cmdForOutput.add("cmd"); - cmdForOutput.add("/c"); - cmdForOutput.add("call"); - } - else { - cmdForOutput.add("/bin/sh"); - cmdForOutput.add("-xe"); - } - cmdForOutput.add(file); - writer.println("$ " + StringUtils.collectionToString(cmdForOutput)); - Function function = new Function(){ - @Override - public Void apply(String input){ - writer.println(input); - writer.flush(); - return null; - } - }; - Map env = Maps.newHashMap(); - env.put(Constants.EXECUTION_ENV_PROJECT_NAME, commandInfo.project_name()); - env.put(Constants.EXECUTION_ENV_NODE_NAME, commandInfo2.node_name()); - env.put(Constants.EXECUTION_ENV_NODE_ALIAS, commandInfo2.node_alias()); - env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils.collectionToString(projectStatusUpdater.getTags(commandInfo.project_name(), commandInfo2.node_name()), ";")); - env.putAll(commandInfo2.environment_variables()); - env.putAll(getEnvParams(commandInfo)); - Result result = new ProcessExecuterBuilder(cmd, pathHelper.getProjectDir(commandInfo.project_name())).cmdForOutput(cmdForOutput).timeoutInMinutes(commandInfo.timeoutInMinutes()).function(function).env(env).user(cred).build().execute(); - writer.println(Constants.COMMAND_RESULT + result.exit()); - writer.flush(); - log.info("command exit status is " + result.exit()); - } catch (Exception ex) { - try { - log.warn("failed on command execution", ex); - writer.println(Constants.COMMAND_RESULT + ExitStatus.EXCEPTION); - } catch (Exception e) { - log.warn("failed on command execution2", ex); - } - } - finally { - if (null != cmdScript){ - cmdScript.delete(); - } - } - } - private void validateKey(String decrypt) { - List l = Splitter.on("#").splitToList(decrypt); - if (l.size() != 2){ - log.warn("format error"); - return; - } - try { - UUID.fromString(l.get(0)); - } catch (Exception e) { - log.warn("format error bad parameter(0) " + l.get(0), e); - } - try { - long currentTimeMillis = System.currentTimeMillis(); - long serverTime = Long.valueOf(l.get(1)); - if (Math.abs(currentTimeMillis - serverTime) > TimeUnit.MINUTES.toMillis(1)) { - log.warn("time from server does not match"); - } - } catch (NumberFormatException e) { - log.warn("format error bad parameter(1) " + l.get(1), e); - } - } - private Map getEnvParams(CommandInfo commandJson) { - Map $ = Maps.newHashMap(); - for (CommandParameterInfo p : commandJson.parameters()) { - $.put(p.name(), p.value()); - } - return $; - } + private String encodeIfNeeded(String text, String cred) { + return null == cred ? text : CredHelper.encode(text); + } - private String encodeIfNeeded(String text, String cred) { - return null == cred ? text: CredHelper.encode(text); - } + private ProjectJson getProject(String projectName) { + return configurationManager.getProjectForName(projectName); + } - private ProjectJson getProject(String projectName) { - return configurationManager.getProjectForName(projectName); - } - - @Override - protected boolean checkPermissions(HttpServletRequest request) { - return true; - } + @Override + protected boolean checkPermissions(HttpServletRequest request) { + return true; + } } diff --git a/src/peer/codeine/servlets/command_backup/CommandNodeServletBackup.java b/src/peer/codeine/servlets/command_backup/CommandNodeServletBackup.java index 1206b05f..f3bc92d3 100644 --- a/src/peer/codeine/servlets/command_backup/CommandNodeServletBackup.java +++ b/src/peer/codeine/servlets/command_backup/CommandNodeServletBackup.java @@ -134,13 +134,10 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res } cmdForOutput.add(file); writer.println("$ " + StringUtils.collectionToString(cmdForOutput)); - Function function = new Function(){ - @Override - public Void apply(String input){ - writer.println(input); - writer.flush(); - return null; - } + Function function = input -> { + writer.println(input); + writer.flush(); + return null; }; Map env = getEnvParams(commandInfo); env.put(Constants.EXECUTION_ENV_PROJECT_NAME, commandInfo.project_name());