Skip to content

Commit

Permalink
codeine-251 pass command id to peer so peer will add command link to …
Browse files Browse the repository at this point in the history
…env of command (#252)
  • Loading branch information
ezraroi authored Mar 26, 2019
1 parent ede7bcf commit 7ddb2ba
Show file tree
Hide file tree
Showing 11 changed files with 466 additions and 420 deletions.
138 changes: 75 additions & 63 deletions src/common/codeine/configuration/Links.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,80 @@

public class Links {

private @Inject GlobalConfigurationJsonStore globalConfiguration;

public String directoryPeerStatus() {
return "http://" + globalConfiguration.get().directory_host() + ":" + globalConfiguration.get().directory_port()
+ Constants.PEER_STATUS_CONTEXT;
}

public String getLogLink(String hostport) {
return "http://" + hostport + Constants.RESOURCESS_CONTEXT;
}

public String getPeerLink(String hostport) {
return "http://" + hostport;
}

public String getPeerCommandLink(String hostport, String project, String command, String userArgs) {
String args = null == userArgs ? "" : "&version=" + HttpUtils.encodeURL(userArgs);
return getPeerLink(hostport) + Constants.COMMAND_NODE_CONTEXT + "?project=" + HttpUtils.encodeURL(project) + "&command=" + HttpUtils.encodeURL(command) + args;
}

public String getProjectLink(String name) {
return Constants.PROJECT_STATUS_CONTEXT + "?project="+HttpUtils.encodeURL(name);
}

public String getPeerMonitorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
String nodeContextPath = getNodeMonitorOutputContextPath(projectName);
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils.specialEncode(collectorName) + ".txt";
}
public String getPeerCollectorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
String nodeContextPath = getNodeCollectorOutputContextPath(projectName);
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils.specialEncode(collectorName) + ".txt";
}

public String getWebServerLink() {
return "http://" + globalConfiguration.get().web_server_host() + ":" + globalConfiguration.get().web_server_port();
}

public String getNodeMonitorOutputContextPath(String projectName) {
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName) + Constants.MONITOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
}
public String getNodeCollectorOutputContextPath(String projectName) {
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName) + Constants.COLLECTOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
}
public String getNodeMonitorOutputContextPathAllProjects() {
return Constants.PROJECT_PATH;
}

public String getWebServerLandingPage() {
return getWebServerLink() + Constants.PROJECTS_LIST_CONTEXT;
}

public String getWebServerProjectAlerts(ProjectJson project) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project.name()) + "/status";
}

public String getWebServerMonitorStatus(String project_name, String node_name, String collector_name) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) +
"/node/" + HttpUtils.encodeURL(node_name) + "/monitor/" + HttpUtils.encodeURL(collector_name) + "/status";
}
public String getWebServerCollectorStatus(String project_name, String node_name, String collector_name) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) +
"/node/" + HttpUtils.encodeURL(node_name) + "/collector/" + HttpUtils.encodeURL(collector_name) + "/status";
}
private @Inject
GlobalConfigurationJsonStore globalConfiguration;

public String directoryPeerStatus() {
return "http://" + globalConfiguration.get().directory_host() + ":" + globalConfiguration.get().directory_port()
+ Constants.PEER_STATUS_CONTEXT;
}

public String getLogLink(String hostport) {
return "http://" + hostport + Constants.RESOURCESS_CONTEXT;
}

public String getPeerLink(String hostport) {
return "http://" + hostport;
}

public String getPeerCommandLink(String hostport, String project, String command, String userArgs) {
String args = null == userArgs ? "" : "&version=" + HttpUtils.encodeURL(userArgs);
return getPeerLink(hostport) + Constants.COMMAND_NODE_CONTEXT + "?project=" + HttpUtils.encodeURL(project)
+ "&command=" + HttpUtils.encodeURL(command) + args;
}

public String getProjectLink(String name) {
return Constants.PROJECT_STATUS_CONTEXT + "?project=" + HttpUtils.encodeURL(name);
}

public String getPeerMonitorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
String nodeContextPath = getNodeMonitorOutputContextPath(projectName);
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils
.specialEncode(collectorName) + ".txt";
}

public String getPeerCollectorResultLink(String hostport, String projectName, String collectorName,
String nodeName) {
String nodeContextPath = getNodeCollectorOutputContextPath(projectName);
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils
.specialEncode(collectorName) + ".txt";
}

public String getWebServerLink() {
return "http://" + globalConfiguration.get().web_server_host() + ":" + globalConfiguration.get()
.web_server_port();
}

public String getNodeMonitorOutputContextPath(String projectName) {
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName)
+ Constants.MONITOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
}

public String getNodeCollectorOutputContextPath(String projectName) {
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName)
+ Constants.COLLECTOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
}

public String getNodeMonitorOutputContextPathAllProjects() {
return Constants.PROJECT_PATH;
}

public String getWebServerLandingPage() {
return getWebServerLink() + Constants.PROJECTS_LIST_CONTEXT;
}

public String getWebServerProjectAlerts(ProjectJson project) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project.name()) + "/status";
}

public String getWebServerCollectorStatus(String project_name, String node_name, String collector_name) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) + "/node/" + HttpUtils
.encodeURL(node_name) + "/collector/" + HttpUtils.encodeURL(collector_name) + "/status";
}

public String getWebServerCommandStatus(String project_name, String command_name, long command_id) {
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) + "/command/" + HttpUtils
.encodeURL(command_name) + "/" + command_id + "/status";
}

}
88 changes: 48 additions & 40 deletions src/common/codeine/jsons/command/CommandInfoForSpecificNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,52 @@

public class CommandInfoForSpecificNode {

private String node_name;
private String node_alias;
private String tmp_dir;
private String key;
private Map<String, String> environment_variables = Maps.newLinkedHashMap();

public CommandInfoForSpecificNode(String node_name, String node_alias, String tmp_dir, String key, Map<String, String> environment_variables) {
super();
this.node_name = node_name;
this.node_alias = node_alias;
this.tmp_dir = tmp_dir;
this.key = key;
this.environment_variables = environment_variables;
}
public String node_alias() {
return node_alias;
}
public String node_name() {
return node_name;
}
public String tmp_dir() {
return tmp_dir;
}
public String key() {
return key;
}
public Map<String, String> environment_variables() {
return environment_variables;
}

@Override
public String toString() {
return "CommandInfoForSpecificNode{" +
"node_name='" + node_name + '\'' +
", node_alias='" + node_alias + '\'' +
", tmp_dir='" + tmp_dir + '\'' +
", key='" + key + '\'' +
", environment_variables=" + environment_variables +
'}';
}
private String node_name;
private String node_alias;
private String tmp_dir;
private String key;
private Map<String, String> environment_variables = Maps.newLinkedHashMap();
private Long commandId;

public CommandInfoForSpecificNode(String node_name, String node_alias, String tmp_dir, String key,
Map<String, String> environment_variables, long commandId) {
super();
this.node_name = node_name;
this.node_alias = node_alias;
this.tmp_dir = tmp_dir;
this.key = key;
this.environment_variables = environment_variables;
this.commandId = commandId;
}

public String node_alias() {
return node_alias;
}

public String node_name() {
return node_name;
}

public String tmp_dir() {
return tmp_dir;
}

public String key() {
return key;
}

public Map<String, String> environment_variables() {
return environment_variables;
}

public Long command_id() {
return commandId;
}

@Override
public String toString() {
return "CommandInfoForSpecificNode{" + "node_name='" + node_name + '\'' + ", node_alias='" + node_alias + '\''
+ ", tmp_dir='" + tmp_dir + '\'' + ", key='" + key + '\'' + ", environment_variables="
+ environment_variables + ", commandId=" + commandId + '}';
}
}
2 changes: 2 additions & 0 deletions src/common/codeine/model/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ public class Constants {
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_COMMAND_NAME = "CODEINE_COMMAND_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_COMMAND_STATUS_LINK = "CODEINE_COMMAND_STATUS_LINK";
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";
Expand Down
3 changes: 1 addition & 2 deletions src/peer/codeine/CodeinePeerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import codeine.jsons.peer_status.PeerStatus;
import codeine.nodes.NodesManagerPeer;
import codeine.nodes.NodesRunner;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.assistedinject.FactoryModuleBuilder;
Expand Down Expand Up @@ -48,4 +47,4 @@ protected void configure() {
install(new FactoryModuleBuilder().build(TagsCollectorRunnerFactory.class));
}

}
}
45 changes: 24 additions & 21 deletions src/peer/codeine/servlets/CommandNodeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import codeine.SnoozeKeeper;
import codeine.configuration.IConfigurationManager;
import codeine.configuration.Links;
import codeine.configuration.PathHelper;
import codeine.credentials.CredHelper;
import codeine.jsons.auth.EncryptionUtils;
Expand Down Expand Up @@ -54,13 +55,15 @@ public class CommandNodeServlet extends AbstractServlet {
private SnoozeKeeper snoozeKeeper;
@Inject
private PeerStatus projectStatusUpdater;
@Inject
private Links links;


@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()) {
if (Boolean.parseBoolean(getParameter(request, Constants.UrlParameters.FORCE)) || experimentalConfJsonStore
.get().allow_concurrent_commands_in_peer()) {
executeCommandNotSync(request, res);
} else {
executeCommandSync(request, res);
Expand All @@ -69,15 +72,13 @@ public void myPost(HttpServletRequest request, HttpServletResponse res) {
}

/**
* this prevents multiple commands on the same peer, so preventing upgrade the peer during
* command for example
* 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) {
private synchronized void executeCommandSync(HttpServletRequest request, HttpServletResponse res) {
executeInternal(request, res);
}

Expand All @@ -89,13 +90,10 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
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);
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());
String decrypt = EncryptionUtils.decrypt(Constants.CODEINE_API_TOKEN_DERIVER, commandInfo2.key());
validateKey(decrypt);
} else {
log.warn("key is null", new RuntimeException());
Expand All @@ -106,8 +104,8 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
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);
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);
Expand Down Expand Up @@ -151,19 +149,24 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
};
Map<String, String> env = Maps.newHashMap();
env.put(Constants.EXECUTION_ENV_PROJECT_NAME, commandInfo.project_name());
if (commandInfo2.command_id() != null) {
env.put(Constants.EXECUTION_ENV_COMMAND_STATUS_LINK, links
.getWebServerCommandStatus(commandInfo.project_name(), commandInfo.name(),
commandInfo2.command_id()));
}
env.put(Constants.EXECUTION_ENV_COMMAND_NAME, commandInfo.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(getTags(commandInfo.project_name(), commandInfo2.node_name()),";"));
env.put(Constants.EXECUTION_ENV_CODEINE_SERVER,
globalConfigurationJsonStore.get().web_server_host());
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)
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();
Expand Down
Loading

0 comments on commit 7ddb2ba

Please sign in to comment.