Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public class CreateSoftwareScheduleInput {
private Long repeat;

private List<String> machineIds;

@Valid
private PackageUpdateSettingsInput updateSettings;

@Valid
private SoftwareAutoUpdateInput autoUpdate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.openframe.api.dto.rmm.software;

import com.openframe.data.document.rmm.software.PackageUpdateMode;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Data;

@Data
public class PackageUpdateSettingsInput {

@NotNull
private PackageUpdateMode mode;

@Positive(message = "deadlineSeconds must be positive")
@Max(value = 2_592_000, message = "deadlineSeconds must not exceed 30 days")
private Long deadlineSeconds;

@NotNull
@Positive(message = "countdownSeconds must be positive")
@Max(value = 3_600, message = "countdownSeconds must not exceed 1 hour")
private Integer countdownSeconds;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.openframe.api.dto.rmm.software;

import com.openframe.data.document.device.DeviceStatus;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import lombok.Builder;
import lombok.Data;
Expand All @@ -14,6 +15,8 @@ public class SoftwareActionDeviceResponse {
private String hostname;
private String organizationId;
private String organizationName;
private DeviceStatus deviceStatus;
private Instant lastSeen;
private SoftwareActionStatus status;

private Integer exitCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public class SoftwareActionResponse {
private String executionId;

private String software;
private String version;
private SoftwareAction action;
private PackageManagerType engine;
private SoftwareActionStatus status;

private int totalMachineCount;
private int respondedMachineCount;
private int waitingMachineCount;

private Instant scheduledAt;
private Instant dispatchedAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.openframe.api.dto.rmm.software;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class SoftwareAutoUpdateInput {

@NotNull
private Boolean enabled;

@Valid
private PackageUpdateSettingsInput settings;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.openframe.api.dto.rmm.software;

import com.openframe.data.document.rmm.software.PackageUpdateSettings;
import com.openframe.data.document.rmm.software.SoftwareAction;
import com.openframe.data.document.rmm.software.SoftwareAutoUpdate;
import com.openframe.data.document.rmm.software.SoftwareBundleMode;
import com.openframe.data.document.rmm.software.SoftwareBundlePackage;
import com.openframe.data.document.rmm.software.SoftwareBundleStatus;
Expand All @@ -20,6 +22,8 @@ public class SoftwareBundleResponse {
private SoftwareBundleStatus status;
private List<String> machineIds;
private List<SoftwareBundlePackage> packages;
private PackageUpdateSettings updateSettings;
private SoftwareAutoUpdate autoUpdate;
private Instant startAt;
private String scheduleId;
private String createdBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.openframe.data.document.rmm.schedule.ScheduleTimeReference;
import com.openframe.data.document.rmm.schedule.SoftwareSchedulePackage;
import com.openframe.data.document.rmm.script.ScriptStatus;
import com.openframe.data.document.rmm.software.PackageUpdateSettings;
import com.openframe.data.document.rmm.software.SoftwareAction;
import com.openframe.data.document.rmm.software.SoftwareAutoUpdate;
import lombok.Builder;
import lombok.Data;

Expand All @@ -23,6 +25,8 @@ public class SoftwareScheduleResponse {
private String description;
private SoftwareAction action;
private List<SoftwareSchedulePackage> packages;
private PackageUpdateSettings updateSettings;
private SoftwareAutoUpdate autoUpdate;
private ScheduleDeviceSelectionMode selectionMode;
private ScheduleDeviceCriteria deviceCriteria;
private ScheduleScriptTrigger trigger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public class SubmitSoftwareBundleInput {

@Valid
private SoftwareBundleScheduleInput schedule;

@Valid
private PackageUpdateSettingsInput updateSettings;

@Valid
private SoftwareAutoUpdateInput autoUpdate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ public class UpdateSoftwareScheduleInput {
private Long repeat;

private List<String> machineIds;

@Valid
private PackageUpdateSettingsInput updateSettings;

@Valid
private SoftwareAutoUpdateInput autoUpdate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.openframe.api.service.rmm.software;

import com.openframe.api.dto.rmm.software.PackageUpdateSettingsInput;
import com.openframe.api.dto.rmm.software.SoftwareAutoUpdateInput;
import com.openframe.core.exception.BadRequestException;
import com.openframe.data.document.rmm.software.PackageUpdateMode;
import com.openframe.data.document.rmm.software.PackageUpdateSettings;
import com.openframe.data.document.rmm.software.SoftwareAction;
import com.openframe.data.document.rmm.software.SoftwareAutoUpdate;
import lombok.experimental.UtilityClass;

import java.time.Duration;

@UtilityClass
public class PackageUpdateSettingsMapper {

static final long DEFAULT_DEADLINE_SECONDS = Duration.ofDays(7).toSeconds();
static final int DEFAULT_COUNTDOWN_SECONDS = (int) Duration.ofMinutes(5).toSeconds();

public PackageUpdateSettings updateSettings(SoftwareAction action, PackageUpdateSettingsInput input) {
if (action != SoftwareAction.UPDATE) {
rejectOutsideUpdate(input, "updateSettings");
return null;
}
return input == null ? defaults() : toDomain(input);
}

public SoftwareAutoUpdate autoUpdate(SoftwareAction action, SoftwareAutoUpdateInput input) {
if (action != SoftwareAction.UPDATE) {
rejectOutsideUpdate(input, "autoUpdate");
return null;
}
if (input == null || !input.getEnabled()) {
return SoftwareAutoUpdate.builder().enabled(false).build();
}
PackageUpdateSettings settings = input.getSettings() == null ? null : toDomain(input.getSettings());
return SoftwareAutoUpdate.builder().enabled(true).settings(settings).build();
}

private PackageUpdateSettings defaults() {
return PackageUpdateSettings.builder()
.mode(PackageUpdateMode.WAIT)
.deadlineSeconds(DEFAULT_DEADLINE_SECONDS)
.countdownSeconds(DEFAULT_COUNTDOWN_SECONDS)
.build();
}

private PackageUpdateSettings toDomain(PackageUpdateSettingsInput input) {
PackageUpdateMode mode = input.getMode();
Long deadlineSeconds = mode == PackageUpdateMode.FORCE ? null : input.getDeadlineSeconds();
return PackageUpdateSettings.builder()
.mode(mode)
.deadlineSeconds(deadlineSeconds)
.countdownSeconds(input.getCountdownSeconds())
.build();
}

private void rejectOutsideUpdate(Object input, String field) {
if (input != null) {
throw new BadRequestException(field + " applies to UPDATE only: an app that is not installed yet cannot be running");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.openframe.api.dto.rmm.software.SoftwareActionDeviceFilterInput;
import com.openframe.api.dto.rmm.software.SoftwareActionDeviceResponse;
import com.openframe.data.document.device.Machine;
import com.openframe.data.document.organization.Organization;
import com.openframe.data.document.rmm.schedule.DeviceOnlineDispatchStatus;
import com.openframe.data.document.rmm.schedule.SoftwareScheduleMachineAssigned;
import com.openframe.data.document.rmm.script.ExecutionStatus;
import com.openframe.data.document.rmm.script.ScriptExecution;
import com.openframe.data.document.rmm.software.PackageUpdateDeviceState;
import com.openframe.data.document.rmm.software.SoftwareActionStatus;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.data.repository.organization.OrganizationRepository;
import com.openframe.data.repository.rmm.PackageUpdateDeviceStateRepository;
import com.openframe.data.repository.rmm.ScriptExecutionRepository;
import com.openframe.data.repository.rmm.SoftwareBundleOnlineDispatchRepository;
import com.openframe.data.repository.rmm.SoftwareScheduleMachineAssignedRepository;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class SoftwareActionDetailService {
private final SoftwareBundleOnlineDispatchRepository bundleOnlineDispatchRepository;
private final SoftwareScheduleOnlineDispatchRepository scheduleOnlineDispatchRepository;
private final SoftwareScheduleMachineAssignedRepository scheduleMachineAssignedRepository;
private final PackageUpdateDeviceStateRepository packageUpdateDeviceStateRepository;
private final MachineRepository machineRepository;
private final OrganizationRepository organizationRepository;
private final TenantIdProvider tenantIdProvider;
Expand All @@ -63,9 +65,11 @@ public List<SoftwareActionDeviceResponse> devices(String executionId, String bun

// 3. Overlay execution leaves — the real outcome wins over any pending state.
if (hasText(executionId)) {
for (ScriptExecution leaf : scriptExecutionRepository.findByTenantIdAndExecutionId(tenantId, executionId)) {
List<ScriptExecution> leaves = scriptExecutionRepository.findByTenantIdAndExecutionId(tenantId, executionId);
for (ScriptExecution leaf : leaves) {
byMachine.put(leaf.getMachineId(), fromLeaf(leaf));
}
overlayWaiting(tenantId, leaves, byMachine);
}

enrichWithCustomer(tenantId, byMachine);
Expand Down Expand Up @@ -98,6 +102,28 @@ private void addPending(Map<String, SoftwareActionDeviceResponse> byMachine, Str
}
}

// 4. A running package update reads WAITING from its device state, so rows recorded before the leaf carried
// the waiting flag show it too.
private void overlayWaiting(String tenantId, List<ScriptExecution> leaves,
Map<String, SoftwareActionDeviceResponse> byMachine) {
Map<String, String> requestByMachine = new HashMap<>();
for (ScriptExecution leaf : leaves) {
if (hasText(leaf.getPackageUpdateRequestId()) && isRunning(leaf.getStatus())) {
requestByMachine.put(leaf.getMachineId(), leaf.getPackageUpdateRequestId());
}
}
if (requestByMachine.isEmpty()) {
return;
}
for (PackageUpdateDeviceState state : packageUpdateDeviceStateRepository
.findByTenantIdAndRequestIdIn(tenantId, new HashSet<>(requestByMachine.values()))) {
if (state.getStatus() == SoftwareActionStatus.WAITING
&& state.getRequestId().equals(requestByMachine.get(state.getMachineId()))) {
byMachine.get(state.getMachineId()).setStatus(SoftwareActionStatus.WAITING);
}
}
}

// Batch-load hostname and customer in two queries, not per row.
private void enrichWithCustomer(String tenantId, Map<String, SoftwareActionDeviceResponse> byMachine) {
if (byMachine.isEmpty()) {
Expand Down Expand Up @@ -125,6 +151,8 @@ private void enrichWithCustomer(String tenantId, Map<String, SoftwareActionDevic
row.setHostname(m.getHostname());
row.setOrganizationId(m.getOrganizationId());
row.setOrganizationName(organizationNames.get(m.getOrganizationId()));
row.setDeviceStatus(m.getStatus());
row.setLastSeen(m.getLastSeen());
}
});
}
Expand Down Expand Up @@ -178,6 +206,10 @@ private static SoftwareActionDeviceResponse pending(String machineId, SoftwareAc
.build();
}

private static boolean isRunning(ExecutionStatus status) {
return status == null || status == ExecutionStatus.QUEUED || status == ExecutionStatus.RUNNING;
}

private static SoftwareActionStatus deviceStatus(ExecutionStatus status) {
if (status == null) {
return SoftwareActionStatus.IN_PROGRESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ private static SoftwareActionResponse toResponse(SoftwareActionSummary s) {
.id(SoftwareActionId.of(s.getExecutionId(), s.getBundleId(), s.getScheduleId()).encode())
.executionId(s.getExecutionId())
.software(s.getPackageName())
.version(s.getVersion())
.action(s.getAction())
.engine(s.getPackageManager())
.status(s.getStatus())
.totalMachineCount(s.getTotalMachineCount())
.respondedMachineCount(s.getRespondedMachineCount())
.waitingMachineCount(s.getWaitingMachineCount())
.dispatchedAt(s.getDispatchedAt())
.initiatedBy(s.getInitiatedBy())
.bundleId(s.getBundleId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.openframe.data.document.rmm.schedule.ScheduleOfflineBehavior;
import com.openframe.data.document.rmm.schedule.ScheduleTimeReference;
import com.openframe.data.document.rmm.script.OsType;
import com.openframe.data.document.rmm.software.PackageUpdateSettings;
import com.openframe.data.document.rmm.software.SoftwareAutoUpdate;
import com.openframe.data.document.rmm.software.SoftwareBundle;
import com.openframe.data.document.rmm.software.SoftwareBundleMode;
import com.openframe.data.document.rmm.software.SoftwareBundleOnlineDispatch;
Expand Down Expand Up @@ -134,15 +136,19 @@ public SoftwareBundleResponse submit(SubmitSoftwareBundleInput input, String act
packageManagerAvailability.requireSoftwareManageable(machineIds, managers);
Map<String, OsType> deviceOsTypes = machinePlatformResolver.osTypesByMachineId(machineIds);
rejectDevicesWithoutCompatiblePackage(machineIds, packages, deviceOsTypes);
PackageUpdateSettings updateSettings = PackageUpdateSettingsMapper.updateSettings(input.getAction(), input.getUpdateSettings());
SoftwareAutoUpdate autoUpdate = PackageUpdateSettingsMapper.autoUpdate(input.getAction(), input.getAutoUpdate());

entity.setAction(input.getAction());
entity.setPackages(packages);
entity.setUpdateSettings(updateSettings);
entity.setAutoUpdate(autoUpdate);

Instant now = Instant.now();
if (input.getSchedule() != null) {
entity.setMode(SoftwareBundleMode.SCHEDULED);
entity.setStartAt(input.getSchedule().getStartAt());
submitScheduled(entity, input.getSchedule(), actor);
submitScheduled(entity, input, actor);
} else {
entity.setMode(SoftwareBundleMode.NOW);
armOnlineDispatch(entity, now);
Expand Down Expand Up @@ -250,7 +256,8 @@ private static OsType osTypeOf(PackageManagerType packageManager) {
return packageManager == PackageManagerType.BREW ? OsType.MAC_OS : OsType.WINDOWS;
}

private void submitScheduled(SoftwareBundle bundle, SoftwareBundleScheduleInput sched, String actor) {
private void submitScheduled(SoftwareBundle bundle, SubmitSoftwareBundleInput submit, String actor) {
SoftwareBundleScheduleInput sched = submit.getSchedule();
CreateSoftwareScheduleInput input = new CreateSoftwareScheduleInput();
input.setName(sched.getName() != null ? sched.getName() : scheduleName(bundle));
input.setDescription(sched.getDescription());
Expand All @@ -263,6 +270,8 @@ private void submitScheduled(SoftwareBundle bundle, SoftwareBundleScheduleInput
input.setStartAt(sched.getStartAt());
input.setRepeat(sched.getRepeat());
input.setMachineIds(bundle.getMachineIds());
input.setUpdateSettings(submit.getUpdateSettings());
input.setAutoUpdate(submit.getAutoUpdate());

SoftwareScheduleResponse schedule = softwareScheduleService.create(input, actor);
bundle.setScheduleId(schedule.getId());
Expand Down Expand Up @@ -361,6 +370,8 @@ private static SoftwareBundleResponse toResponse(SoftwareBundle b) {
.status(b.getStatus())
.machineIds(b.getMachineIds() == null ? List.of() : b.getMachineIds())
.packages(b.getPackages() == null ? List.of() : b.getPackages())
.updateSettings(b.getUpdateSettings())
.autoUpdate(b.getAutoUpdate())
.startAt(b.getStartAt())
.scheduleId(b.getScheduleId())
.createdBy(b.getCreatedBy())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public SoftwareScheduleResponse create(CreateSoftwareScheduleInput input, String
.description(input.getDescription())
.action(input.getAction())
.packages(packages)
.updateSettings(PackageUpdateSettingsMapper.updateSettings(input.getAction(), input.getUpdateSettings()))
.autoUpdate(PackageUpdateSettingsMapper.autoUpdate(input.getAction(), input.getAutoUpdate()))
.trigger(ScheduleScriptTrigger.DATE_TIME)
.timeReference(timeReference)
.offlineBehavior(input.getOfflineBehavior())
Expand Down Expand Up @@ -100,6 +102,8 @@ public SoftwareScheduleResponse update(UpdateSoftwareScheduleInput input, String
entity.setDescription(input.getDescription());
entity.setAction(input.getAction());
entity.setPackages(packages);
entity.setUpdateSettings(PackageUpdateSettingsMapper.updateSettings(input.getAction(), input.getUpdateSettings()));
entity.setAutoUpdate(PackageUpdateSettingsMapper.autoUpdate(input.getAction(), input.getAutoUpdate()));
entity.setTimeReference(timeReference);
entity.setOfflineBehavior(input.getOfflineBehavior());
entity.setReconnectWindowSeconds(input.getReconnectWindowSeconds());
Expand Down Expand Up @@ -286,6 +290,7 @@ private SoftwareScheduleResponse toResponse(SoftwareSchedule s) {
return SoftwareScheduleResponse.builder()
.id(s.getId()).name(s.getName()).description(s.getDescription())
.action(s.getAction()).packages(s.getPackages())
.updateSettings(s.getUpdateSettings()).autoUpdate(s.getAutoUpdate())
.selectionMode(s.getSelectionMode()).deviceCriteria(s.getDeviceCriteria())
.trigger(s.getTrigger()).timeReference(s.getTimeReference())
.offlineBehavior(s.getOfflineBehavior()).reconnectWindowSeconds(s.getReconnectWindowSeconds())
Expand Down
Loading
Loading