mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): user data logging (#1474)
* fix(ec2): added user data output streaming to cloud watch * fix(ec2): deleted dead code after merging * fix(ec2): adjust comments position * fix(ec2): fixed userdata logging and added mockito tests
This commit is contained in:
1 parent
2b26149ef5
commit
bb6ee4f69d
3 files changed
+76
-19
No files matched your search
@@ -104,6 +104,10 @@ public class ContainerLogStreamer {
|
||||
return LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/" + suffix;
|
||||
}
|
||||
|
||||
public void streamToCloudWatchLogs(String logGroup, String logStream, String region, String line) {
|
||||
forwardToCloudWatchLogs(logGroup, logStream, region, line);
|
||||
}
|
||||
|
||||
private void forwardToCloudWatchLogs(String logGroup, String logStream, String region, String line) {
|
||||
try {
|
||||
Map<String, Object> event = new HashMap<>();
|
||||
|
||||
@@ -417,21 +417,31 @@ public class Ec2ContainerManager {
|
||||
|
||||
private void executeUserData(String containerId, String instanceId, String userData, String region) {
|
||||
try {
|
||||
String logGroup = "/aws/ec2/" + instanceId;
|
||||
String logStream = logStreamer.generateLogStreamName("user-data");
|
||||
|
||||
List<String> shellScripts = userDataShellScripts(userData);
|
||||
if (shellScripts.isEmpty()) {
|
||||
LOG.infov("UserData for EC2 instance {0} did not contain executable shellscript parts", instanceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute the script and stream output to CloudWatch
|
||||
for (int i = 0; i < shellScripts.size(); i++) {
|
||||
executeUserDataShellScript(containerId, instanceId, shellScripts.get(i), i + 1, shellScripts.size());
|
||||
executeUserDataShellScript(
|
||||
containerId, instanceId, shellScripts.get(i), i + 1, shellScripts.size(),
|
||||
logGroup, logStream, region
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("UserData execution failed for EC2 instance {0}: {1}", instanceId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void executeUserDataShellScript(String containerId, String instanceId, String scriptContent, int partNumber, int partCount) throws Exception {
|
||||
private void executeUserDataShellScript(
|
||||
String containerId, String instanceId, String scriptContent, int partNumber, int partCount,
|
||||
String logGroup, String logStream, String region
|
||||
) throws Exception {
|
||||
byte[] script = scriptContent.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] tar = buildSingleFileTar("user-data.sh", script, 0755);
|
||||
dockerClient.copyArchiveToContainerCmd(containerId)
|
||||
@@ -453,8 +463,12 @@ public class Ec2ContainerManager {
|
||||
dockerClient.execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
if (frame.getPayload() != null) {
|
||||
try { output.write(frame.getPayload()); } catch (IOException ignored) {}
|
||||
byte[] payload = frame.getPayload();
|
||||
if (payload == null) return;
|
||||
try { output.write(payload); } catch (IOException ignored) {}
|
||||
String line = new String(payload, StandardCharsets.UTF_8).stripTrailing();
|
||||
if (!line.isEmpty()) {
|
||||
logStreamer.streamToCloudWatchLogs(logGroup, logStream, region, line);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user