use of com.aws.greengrass.logging.api.LogEventBuilder in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalService method run.
@SuppressWarnings("PMD.CloseResource")
protected Pair<RunStatus, Exec> run(Topic t, String cmd, IntConsumer background, List<Exec> trackingList, boolean requiresPrivilege) throws InterruptedException {
if (runWith == null) {
Optional<RunWith> opt = computeRunWithConfiguration();
if (!opt.isPresent()) {
logger.atError().log("Could not determine user/group to run with. Ensure that {} is set for {}", DeviceConfiguration.RUN_WITH_TOPIC, deviceConfiguration.getNucleusComponentName());
return new Pair<>(RunStatus.Errored, null);
}
runWith = opt.get();
LogEventBuilder logEvent = logger.atDebug().kv("user", runWith.getUser());
if (runWith.getGroup() != null) {
logEvent.kv("group", runWith.getGroup());
}
if (runWith.getShell() != null) {
logEvent.kv("shell", runWith.getShell());
}
logEvent.log("Saving user information for service execution");
if (!updateComponentPathOwner()) {
logger.atError().log("Service artifacts may not be accessible to user");
}
}
final ShellRunner shellRunner = context.get(ShellRunner.class);
Exec exec;
try {
exec = shellRunner.setup(t.getFullName(), cmd, this);
} catch (IOException e) {
logger.atError().log("Error setting up to run {}", t.getFullName(), e);
return new Pair<>(RunStatus.Errored, null);
}
if (exec == null) {
return new Pair<>(RunStatus.NothingDone, null);
}
exec = addUser(exec, requiresPrivilege);
exec = addShell(exec);
addEnv(exec, t.parent);
logger.atDebug().setEventType("generic-service-run").log();
// Track all running processes that we fork
if (exec.isRunning()) {
trackingList.add(exec);
}
RunStatus ret = shellRunner.successful(exec, t.getFullName(), background, this) ? RunStatus.OK : RunStatus.Errored;
return new Pair<>(ret, exec);
}
use of com.aws.greengrass.logging.api.LogEventBuilder in project aws-greengrass-nucleus by aws-greengrass.
the class UnixPlatform method id.
/**
* Run the `id` program which returns user and group information about a particular user.
*
* @param id the identifier (numeric or name). If empty, then the current user is looked up.
* @param option whether to load group or user information.
* @param loadName whether a name should or id should be returned.
* @return the output of id (either an integer string or name of the user/group) or empty if an error occurs.
*/
private static Optional<String> id(String id, IdOption option, boolean loadName) {
boolean loadSelf = Utils.isEmpty(id);
String[] cmd = new String[2 + (loadName ? 1 : 0) + (loadSelf ? 0 : 1)];
int i = 0;
cmd[i] = "id";
switch(option) {
case Group:
cmd[++i] = "-g";
break;
case User:
cmd[++i] = "-u";
break;
default:
logger.atDebug().setEventType("id-lookup").addKeyValue("option", option).log("invalid option provided for id");
return Optional.empty();
}
if (loadName) {
cmd[++i] = "-n";
}
if (!loadSelf) {
cmd[++i] = id;
}
logger.atTrace().setEventType("id-lookup").kv("command", String.join(" ", cmd)).log();
StringBuilder out = new StringBuilder();
StringBuilder err = new StringBuilder();
Throwable cause = null;
try (Exec exec = getInstance().createNewProcessRunner()) {
Optional<Integer> exit = exec.withExec(cmd).withShell().withOut(out::append).withErr(err::append).exec();
if (exit.isPresent() && exit.get() == 0) {
return Optional.of(out.toString().trim());
}
} catch (InterruptedException e) {
Arrays.stream(e.getSuppressed()).forEach((t) -> {
logger.atError().setCause(e).log("interrupted");
});
cause = e;
} catch (IOException e) {
cause = e;
}
LogEventBuilder logEvent = logger.atError().setEventType("id-lookup");
if (option == IdOption.Group) {
logEvent.kv("group", id);
} else if (option == IdOption.User && !loadSelf) {
logEvent.kv("user", id);
}
logEvent.kv(STDOUT, out).kv(STDERR, err).setCause(cause).log("Error while looking up id" + (loadSelf ? " for current user" : ""));
return Optional.empty();
}
Aggregations