Skip to content

Commit 7ddb2ba

Browse files
authored
codeine-251 pass command id to peer so peer will add command link to env of command (#252)
1 parent ede7bcf commit 7ddb2ba

11 files changed

+466
-420
lines changed

src/common/codeine/configuration/Links.java

+75-63
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,80 @@
99

1010
public class Links {
1111

12-
private @Inject GlobalConfigurationJsonStore globalConfiguration;
13-
14-
public String directoryPeerStatus() {
15-
return "http://" + globalConfiguration.get().directory_host() + ":" + globalConfiguration.get().directory_port()
16-
+ Constants.PEER_STATUS_CONTEXT;
17-
}
18-
19-
public String getLogLink(String hostport) {
20-
return "http://" + hostport + Constants.RESOURCESS_CONTEXT;
21-
}
22-
23-
public String getPeerLink(String hostport) {
24-
return "http://" + hostport;
25-
}
26-
27-
public String getPeerCommandLink(String hostport, String project, String command, String userArgs) {
28-
String args = null == userArgs ? "" : "&version=" + HttpUtils.encodeURL(userArgs);
29-
return getPeerLink(hostport) + Constants.COMMAND_NODE_CONTEXT + "?project=" + HttpUtils.encodeURL(project) + "&command=" + HttpUtils.encodeURL(command) + args;
30-
}
31-
32-
public String getProjectLink(String name) {
33-
return Constants.PROJECT_STATUS_CONTEXT + "?project="+HttpUtils.encodeURL(name);
34-
}
35-
36-
public String getPeerMonitorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
37-
String nodeContextPath = getNodeMonitorOutputContextPath(projectName);
38-
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils.specialEncode(collectorName) + ".txt";
39-
}
40-
public String getPeerCollectorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
41-
String nodeContextPath = getNodeCollectorOutputContextPath(projectName);
42-
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils.specialEncode(collectorName) + ".txt";
43-
}
44-
45-
public String getWebServerLink() {
46-
return "http://" + globalConfiguration.get().web_server_host() + ":" + globalConfiguration.get().web_server_port();
47-
}
48-
49-
public String getNodeMonitorOutputContextPath(String projectName) {
50-
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName) + Constants.MONITOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
51-
}
52-
public String getNodeCollectorOutputContextPath(String projectName) {
53-
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName) + Constants.COLLECTOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
54-
}
55-
public String getNodeMonitorOutputContextPathAllProjects() {
56-
return Constants.PROJECT_PATH;
57-
}
58-
59-
public String getWebServerLandingPage() {
60-
return getWebServerLink() + Constants.PROJECTS_LIST_CONTEXT;
61-
}
62-
63-
public String getWebServerProjectAlerts(ProjectJson project) {
64-
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project.name()) + "/status";
65-
}
66-
67-
public String getWebServerMonitorStatus(String project_name, String node_name, String collector_name) {
68-
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) +
69-
"/node/" + HttpUtils.encodeURL(node_name) + "/monitor/" + HttpUtils.encodeURL(collector_name) + "/status";
70-
}
71-
public String getWebServerCollectorStatus(String project_name, String node_name, String collector_name) {
72-
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) +
73-
"/node/" + HttpUtils.encodeURL(node_name) + "/collector/" + HttpUtils.encodeURL(collector_name) + "/status";
74-
}
12+
private @Inject
13+
GlobalConfigurationJsonStore globalConfiguration;
14+
15+
public String directoryPeerStatus() {
16+
return "http://" + globalConfiguration.get().directory_host() + ":" + globalConfiguration.get().directory_port()
17+
+ Constants.PEER_STATUS_CONTEXT;
18+
}
19+
20+
public String getLogLink(String hostport) {
21+
return "http://" + hostport + Constants.RESOURCESS_CONTEXT;
22+
}
23+
24+
public String getPeerLink(String hostport) {
25+
return "http://" + hostport;
26+
}
27+
28+
public String getPeerCommandLink(String hostport, String project, String command, String userArgs) {
29+
String args = null == userArgs ? "" : "&version=" + HttpUtils.encodeURL(userArgs);
30+
return getPeerLink(hostport) + Constants.COMMAND_NODE_CONTEXT + "?project=" + HttpUtils.encodeURL(project)
31+
+ "&command=" + HttpUtils.encodeURL(command) + args;
32+
}
33+
34+
public String getProjectLink(String name) {
35+
return Constants.PROJECT_STATUS_CONTEXT + "?project=" + HttpUtils.encodeURL(name);
36+
}
37+
38+
public String getPeerMonitorResultLink(String hostport, String projectName, String collectorName, String nodeName) {
39+
String nodeContextPath = getNodeMonitorOutputContextPath(projectName);
40+
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils
41+
.specialEncode(collectorName) + ".txt";
42+
}
43+
44+
public String getPeerCollectorResultLink(String hostport, String projectName, String collectorName,
45+
String nodeName) {
46+
String nodeContextPath = getNodeCollectorOutputContextPath(projectName);
47+
return getPeerLink(hostport) + nodeContextPath + "/" + HttpUtils.specialEncode(nodeName) + "/" + HttpUtils
48+
.specialEncode(collectorName) + ".txt";
49+
}
50+
51+
public String getWebServerLink() {
52+
return "http://" + globalConfiguration.get().web_server_host() + ":" + globalConfiguration.get()
53+
.web_server_port();
54+
}
55+
56+
public String getNodeMonitorOutputContextPath(String projectName) {
57+
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName)
58+
+ Constants.MONITOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
59+
}
60+
61+
public String getNodeCollectorOutputContextPath(String projectName) {
62+
return getNodeMonitorOutputContextPathAllProjects() + "/" + HttpUtils.encodeURL(projectName)
63+
+ Constants.COLLECTOR_OUTPUT_CONTEXT + Constants.NODE_PATH;
64+
}
65+
66+
public String getNodeMonitorOutputContextPathAllProjects() {
67+
return Constants.PROJECT_PATH;
68+
}
69+
70+
public String getWebServerLandingPage() {
71+
return getWebServerLink() + Constants.PROJECTS_LIST_CONTEXT;
72+
}
73+
74+
public String getWebServerProjectAlerts(ProjectJson project) {
75+
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project.name()) + "/status";
76+
}
77+
78+
public String getWebServerCollectorStatus(String project_name, String node_name, String collector_name) {
79+
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) + "/node/" + HttpUtils
80+
.encodeURL(node_name) + "/collector/" + HttpUtils.encodeURL(collector_name) + "/status";
81+
}
82+
83+
public String getWebServerCommandStatus(String project_name, String command_name, long command_id) {
84+
return getWebServerLink() + "/codeine/project/" + HttpUtils.encodeURL(project_name) + "/command/" + HttpUtils
85+
.encodeURL(command_name) + "/" + command_id + "/status";
86+
}
7587

7688
}

src/common/codeine/jsons/command/CommandInfoForSpecificNode.java

+48-40
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,52 @@
66

77
public class CommandInfoForSpecificNode {
88

9-
private String node_name;
10-
private String node_alias;
11-
private String tmp_dir;
12-
private String key;
13-
private Map<String, String> environment_variables = Maps.newLinkedHashMap();
14-
15-
public CommandInfoForSpecificNode(String node_name, String node_alias, String tmp_dir, String key, Map<String, String> environment_variables) {
16-
super();
17-
this.node_name = node_name;
18-
this.node_alias = node_alias;
19-
this.tmp_dir = tmp_dir;
20-
this.key = key;
21-
this.environment_variables = environment_variables;
22-
}
23-
public String node_alias() {
24-
return node_alias;
25-
}
26-
public String node_name() {
27-
return node_name;
28-
}
29-
public String tmp_dir() {
30-
return tmp_dir;
31-
}
32-
public String key() {
33-
return key;
34-
}
35-
public Map<String, String> environment_variables() {
36-
return environment_variables;
37-
}
38-
39-
@Override
40-
public String toString() {
41-
return "CommandInfoForSpecificNode{" +
42-
"node_name='" + node_name + '\'' +
43-
", node_alias='" + node_alias + '\'' +
44-
", tmp_dir='" + tmp_dir + '\'' +
45-
", key='" + key + '\'' +
46-
", environment_variables=" + environment_variables +
47-
'}';
48-
}
9+
private String node_name;
10+
private String node_alias;
11+
private String tmp_dir;
12+
private String key;
13+
private Map<String, String> environment_variables = Maps.newLinkedHashMap();
14+
private Long commandId;
15+
16+
public CommandInfoForSpecificNode(String node_name, String node_alias, String tmp_dir, String key,
17+
Map<String, String> environment_variables, long commandId) {
18+
super();
19+
this.node_name = node_name;
20+
this.node_alias = node_alias;
21+
this.tmp_dir = tmp_dir;
22+
this.key = key;
23+
this.environment_variables = environment_variables;
24+
this.commandId = commandId;
25+
}
26+
27+
public String node_alias() {
28+
return node_alias;
29+
}
30+
31+
public String node_name() {
32+
return node_name;
33+
}
34+
35+
public String tmp_dir() {
36+
return tmp_dir;
37+
}
38+
39+
public String key() {
40+
return key;
41+
}
42+
43+
public Map<String, String> environment_variables() {
44+
return environment_variables;
45+
}
46+
47+
public Long command_id() {
48+
return commandId;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "CommandInfoForSpecificNode{" + "node_name='" + node_name + '\'' + ", node_alias='" + node_alias + '\''
54+
+ ", tmp_dir='" + tmp_dir + '\'' + ", key='" + key + '\'' + ", environment_variables="
55+
+ environment_variables + ", commandId=" + commandId + '}';
56+
}
4957
}

src/common/codeine/model/Constants.java

+2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ public class Constants {
104104
public static final String EXECUTION_ENV_CONFIGURATION_STEP = "CODEINE_CONFIGURATION_STEP";
105105
public static final String EXECUTION_ENV_OUTPUT_FILE = "CODEINE_OUTPUT_FILE";
106106
public static final String EXECUTION_ENV_PROJECT_NAME = "CODEINE_PROJECT_NAME";
107+
public static final String EXECUTION_ENV_COMMAND_NAME = "CODEINE_COMMAND_NAME";
107108
public static final String EXECUTION_ENV_USER_NAME = "CODEINE_USER_NAME";
108109
public static final String EXECUTION_ENV_PROJECT_STATUS = "CODEINE_PROJECT_STATUS";
109110
public static final String EXECUTION_ENV_NODE_NAME = "CODEINE_NODE_NAME";
110111
public static final String EXECUTION_ENV_NODE_ALIAS = "CODEINE_NODE_ALIAS";
111112
public static final String EXECUTION_ENV_NODE_TAGS = "CODEINE_NODE_TAGS";
113+
public static final String EXECUTION_ENV_COMMAND_STATUS_LINK = "CODEINE_COMMAND_STATUS_LINK";
112114
public static final String EXECUTION_ENV_CODEINE_SERVER = "CODEINE_HOST";
113115
public static final String EXECUTION_ENV_CODEINE_SERVER_PORT = "CODEINE_PORT";
114116
public static final String ENV_CODEINE_WORKAREA = "CODEINE_WORKAREA";

src/peer/codeine/CodeinePeerModule.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import codeine.jsons.peer_status.PeerStatus;
2121
import codeine.nodes.NodesManagerPeer;
2222
import codeine.nodes.NodesRunner;
23-
2423
import com.google.inject.AbstractModule;
2524
import com.google.inject.Scopes;
2625
import com.google.inject.assistedinject.FactoryModuleBuilder;
@@ -48,4 +47,4 @@ protected void configure() {
4847
install(new FactoryModuleBuilder().build(TagsCollectorRunnerFactory.class));
4948
}
5049

51-
}
50+
}

src/peer/codeine/servlets/CommandNodeServlet.java

+24-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import codeine.SnoozeKeeper;
44
import codeine.configuration.IConfigurationManager;
5+
import codeine.configuration.Links;
56
import codeine.configuration.PathHelper;
67
import codeine.credentials.CredHelper;
78
import codeine.jsons.auth.EncryptionUtils;
@@ -54,13 +55,15 @@ public class CommandNodeServlet extends AbstractServlet {
5455
private SnoozeKeeper snoozeKeeper;
5556
@Inject
5657
private PeerStatus projectStatusUpdater;
58+
@Inject
59+
private Links links;
5760

5861

5962
@Override
6063
public void myPost(HttpServletRequest request, HttpServletResponse res) {
6164
log.info("start handle command");
62-
if (Boolean.parseBoolean(getParameter(request, Constants.UrlParameters.FORCE))
63-
|| experimentalConfJsonStore.get().allow_concurrent_commands_in_peer()) {
65+
if (Boolean.parseBoolean(getParameter(request, Constants.UrlParameters.FORCE)) || experimentalConfJsonStore
66+
.get().allow_concurrent_commands_in_peer()) {
6467
executeCommandNotSync(request, res);
6568
} else {
6669
executeCommandSync(request, res);
@@ -69,15 +72,13 @@ public void myPost(HttpServletRequest request, HttpServletResponse res) {
6972
}
7073

7174
/**
72-
* this prevents multiple commands on the same peer, so preventing upgrade the peer during
73-
* command for example
75+
* this prevents multiple commands on the same peer, so preventing upgrade the peer during command for example
7476
*/
7577
private void executeCommandNotSync(HttpServletRequest request, HttpServletResponse res) {
7678
executeInternal(request, res);
7779
}
7880

79-
private synchronized void executeCommandSync(HttpServletRequest request,
80-
HttpServletResponse res) {
81+
private synchronized void executeCommandSync(HttpServletRequest request, HttpServletResponse res) {
8182
executeInternal(request, res);
8283
}
8384

@@ -89,13 +90,10 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
8990
String parameter = Constants.UrlParameters.DATA_NAME;
9091
String data = getParameter(request, parameter);
9192
CommandInfo commandInfo = gson().fromJson(data, CommandInfo.class);
92-
String data2 = getParameter(request,
93-
Constants.UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME);
94-
CommandInfoForSpecificNode commandInfo2 = gson()
95-
.fromJson(data2, CommandInfoForSpecificNode.class);
93+
String data2 = getParameter(request, Constants.UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME);
94+
CommandInfoForSpecificNode commandInfo2 = gson().fromJson(data2, CommandInfoForSpecificNode.class);
9695
if (null != commandInfo2.key()) {
97-
String decrypt = EncryptionUtils
98-
.decrypt(Constants.CODEINE_API_TOKEN_DERIVER, commandInfo2.key());
96+
String decrypt = EncryptionUtils.decrypt(Constants.CODEINE_API_TOKEN_DERIVER, commandInfo2.key());
9997
validateKey(decrypt);
10098
} else {
10199
log.warn("key is null", new RuntimeException());
@@ -106,8 +104,8 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
106104
ProjectJson project = getProject(commandInfo.project_name());
107105
boolean windows_peer = project.operating_system() == OperatingSystem.Windows;
108106
if (null != script_content) {
109-
cmdScript = new ShellScript(file, script_content, project.operating_system(),
110-
commandInfo2.tmp_dir(), null, null, null);
107+
cmdScript = new ShellScript(file, script_content, project.operating_system(), commandInfo2.tmp_dir(),
108+
null, null, null);
111109
file = cmdScript.create();
112110
} else {
113111
log.info("command not found " + file);
@@ -151,19 +149,24 @@ private void executeInternal(HttpServletRequest request, HttpServletResponse res
151149
};
152150
Map<String, String> env = Maps.newHashMap();
153151
env.put(Constants.EXECUTION_ENV_PROJECT_NAME, commandInfo.project_name());
152+
if (commandInfo2.command_id() != null) {
153+
env.put(Constants.EXECUTION_ENV_COMMAND_STATUS_LINK, links
154+
.getWebServerCommandStatus(commandInfo.project_name(), commandInfo.name(),
155+
commandInfo2.command_id()));
156+
}
157+
env.put(Constants.EXECUTION_ENV_COMMAND_NAME, commandInfo.name());
154158
env.put(Constants.EXECUTION_ENV_NODE_NAME, commandInfo2.node_name());
155159
env.put(Constants.EXECUTION_ENV_NODE_ALIAS, commandInfo2.node_alias());
156-
env.put(Constants.EXECUTION_ENV_NODE_TAGS,
157-
StringUtils.collectionToString(getTags(commandInfo.project_name(), commandInfo2.node_name()),";"));
158-
env.put(Constants.EXECUTION_ENV_CODEINE_SERVER,
159-
globalConfigurationJsonStore.get().web_server_host());
160+
env.put(Constants.EXECUTION_ENV_NODE_TAGS, StringUtils
161+
.collectionToString(projectStatusUpdater.getTags(commandInfo.project_name(), commandInfo2.node_name()),
162+
";"));
163+
env.put(Constants.EXECUTION_ENV_CODEINE_SERVER, globalConfigurationJsonStore.get().web_server_host());
160164
env.put(Constants.EXECUTION_ENV_CODEINE_SERVER_PORT,
161165
globalConfigurationJsonStore.get().web_server_port().toString());
162166
env.putAll(commandInfo2.environment_variables());
163167
env.putAll(getEnvParams(commandInfo));
164-
Result result = new ProcessExecuterBuilder(cmd,
165-
pathHelper.getProjectDir(commandInfo.project_name())).cmdForOutput(cmdForOutput)
166-
.timeoutInMinutes(commandInfo.timeoutInMinutes()).function(function).env(env)
168+
Result result = new ProcessExecuterBuilder(cmd, pathHelper.getProjectDir(commandInfo.project_name()))
169+
.cmdForOutput(cmdForOutput).timeoutInMinutes(commandInfo.timeoutInMinutes()).function(function).env(env)
167170
.user(cred).build().execute();
168171
writer.println(Constants.COMMAND_RESULT + result.exit());
169172
writer.flush();

0 commit comments

Comments
 (0)