feat: add remote reboot support
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 26.1.7
|
||||||
|
- Added remote reboot support for hosts running KeyHelp API 2.14 or newer.
|
||||||
|
- Added a dedicated `APIv2_14` client and mapped 2.14+ hosts to it instead of treating them as API 2.13.
|
||||||
|
- Fixed the reboot request to call `/api/v2/server/reboot` with the required JSON confirmation payload.
|
||||||
|
- Changed the reboot confirmation and result UI to non-blocking sheets/banner feedback so failures no longer trap the app in modal dialogs.
|
||||||
|
- Improved API error messages by surfacing the server response body instead of only generic HTTP status codes.
|
||||||
|
- Reduced expected reboot noise by suppressing ping checks for a short grace period after a reboot request.
|
||||||
|
|
||||||
## 26.1.6
|
## 26.1.6
|
||||||
- Publish Gitea releases as stable by default instead of pre-releases.
|
- Publish Gitea releases as stable by default instead of pre-releases.
|
||||||
- Update the Homebrew tap automatically after each successful release by rewriting the cask version and DMG checksum, then pushing the tap repo.
|
- Update the Homebrew tap automatically after each successful release by rewriting the cask version and DMG checksum, then pushing the tap repo.
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import Foundation
|
|||||||
enum APIVersion: String, CaseIterable {
|
enum APIVersion: String, CaseIterable {
|
||||||
case v2_12 = "2.12"
|
case v2_12 = "2.12"
|
||||||
case v2_13 = "2.13"
|
case v2_13 = "2.13"
|
||||||
|
case v2_14 = "2.14"
|
||||||
|
|
||||||
static func from(versionString: String) -> APIVersion? {
|
static func from(versionString: String) -> APIVersion? {
|
||||||
if let version = APIVersion(rawValue: versionString) {
|
if let version = APIVersion(rawValue: versionString) {
|
||||||
@@ -24,7 +25,8 @@ enum APIVersion: String, CaseIterable {
|
|||||||
|
|
||||||
switch (major, minor) {
|
switch (major, minor) {
|
||||||
case (2, 12): return .v2_12
|
case (2, 12): return .v2_12
|
||||||
case (2, 13...): return .v2_13
|
case (2, 13): return .v2_13
|
||||||
|
case (2, 14...): return .v2_14
|
||||||
default: return nil
|
default: return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,6 +38,7 @@ protocol AnyServerAPI {
|
|||||||
func fetchMemoryData() async throws -> Any
|
func fetchMemoryData() async throws -> Any
|
||||||
func fetchUtilizationData() async throws -> Any
|
func fetchUtilizationData() async throws -> Any
|
||||||
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
|
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
|
||||||
|
func restartServer(apiKey: String) async throws
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct AnyServerAPIWrapper<T: ServerAPIProtocol>: AnyServerAPI {
|
private struct AnyServerAPIWrapper<T: ServerAPIProtocol>: AnyServerAPI {
|
||||||
@@ -64,6 +67,10 @@ private struct AnyServerAPIWrapper<T: ServerAPIProtocol>: AnyServerAPI {
|
|||||||
func fetchServerSummary(apiKey: String) async throws -> ServerInfo {
|
func fetchServerSummary(apiKey: String) async throws -> ServerInfo {
|
||||||
return try await wrapped.fetchServerSummary(apiKey: apiKey)
|
return try await wrapped.fetchServerSummary(apiKey: apiKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func restartServer(apiKey: String) async throws {
|
||||||
|
try await wrapped.restartServer(apiKey: apiKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class APIFactory {
|
class APIFactory {
|
||||||
@@ -73,6 +80,8 @@ class APIFactory {
|
|||||||
return AnyServerAPIWrapper(APIv2_12(baseURL: baseURL))
|
return AnyServerAPIWrapper(APIv2_12(baseURL: baseURL))
|
||||||
case .v2_13:
|
case .v2_13:
|
||||||
return AnyServerAPIWrapper(APIv2_13(baseURL: baseURL))
|
return AnyServerAPIWrapper(APIv2_13(baseURL: baseURL))
|
||||||
|
case .v2_14:
|
||||||
|
return AnyServerAPIWrapper(APIv2_14(baseURL: baseURL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +113,7 @@ class APIFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return AnyServerAPIWrapper(APIv2_13(baseURL: baseURL))
|
return AnyServerAPIWrapper(APIv2_14(baseURL: baseURL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ protocol ServerAPIProtocol {
|
|||||||
func fetchMemory() async throws -> MemoryType
|
func fetchMemory() async throws -> MemoryType
|
||||||
func fetchUtilization() async throws -> UtilizationType
|
func fetchUtilization() async throws -> UtilizationType
|
||||||
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
|
func fetchServerSummary(apiKey: String) async throws -> ServerInfo
|
||||||
|
func restartServer(apiKey: String) async throws
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SystemInfo: Codable {
|
struct SystemInfo: Codable {
|
||||||
@@ -36,6 +37,15 @@ class BaseAPIClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func performRequest<T: Codable>(_ request: URLRequest, responseType: T.Type) async throws -> T {
|
func performRequest<T: Codable>(_ request: URLRequest, responseType: T.Type) async throws -> T {
|
||||||
|
let (data, _) = try await performDataRequest(request)
|
||||||
|
return try JSONDecoder().decode(T.self, from: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func performRequestWithoutBody(_ request: URLRequest) async throws {
|
||||||
|
_ = try await performDataRequest(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func performDataRequest(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {
|
||||||
let (data, response) = try await session.data(for: request)
|
let (data, response) = try await session.data(for: request)
|
||||||
|
|
||||||
guard let httpResponse = response as? HTTPURLResponse else {
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
@@ -43,25 +53,61 @@ class BaseAPIClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard 200...299 ~= httpResponse.statusCode else {
|
guard 200...299 ~= httpResponse.statusCode else {
|
||||||
throw APIError.httpError(httpResponse.statusCode)
|
throw APIError.httpError(
|
||||||
|
httpResponse.statusCode,
|
||||||
|
BaseAPIClient.extractErrorMessage(from: data)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return try JSONDecoder().decode(T.self, from: data)
|
return (data, httpResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func extractErrorMessage(from data: Data) -> String? {
|
||||||
|
guard !data.isEmpty else { return nil }
|
||||||
|
|
||||||
|
if let envelope = try? JSONDecoder().decode(APIErrorEnvelope.self, from: data) {
|
||||||
|
let parts = [envelope.code, envelope.message]
|
||||||
|
.compactMap { $0?.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
|
||||||
|
if !parts.isEmpty {
|
||||||
|
return parts.joined(separator: " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let text = String(data: data, encoding: .utf8)?
|
||||||
|
.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||||
|
!text.isEmpty {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum APIError: Error, LocalizedError {
|
enum APIError: Error, LocalizedError {
|
||||||
case invalidURL
|
case invalidURL
|
||||||
case invalidResponse
|
case invalidResponse
|
||||||
case httpError(Int)
|
case httpError(Int, String?)
|
||||||
case decodingError(Error)
|
case decodingError(Error)
|
||||||
|
case unsupportedFeature(String)
|
||||||
|
|
||||||
var errorDescription: String? {
|
var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
case .invalidURL: return "Invalid URL"
|
case .invalidURL: return "Invalid URL"
|
||||||
case .invalidResponse: return "Invalid response"
|
case .invalidResponse: return "Invalid response"
|
||||||
case .httpError(let code): return "HTTP Error: \(code)"
|
case .httpError(let code, let message):
|
||||||
|
if let message, !message.isEmpty {
|
||||||
|
return "HTTP Error: \(code)\n\(message)"
|
||||||
|
}
|
||||||
|
return "HTTP Error: \(code)"
|
||||||
case .decodingError(let error): return "Decoding error: \(error.localizedDescription)"
|
case .decodingError(let error): return "Decoding error: \(error.localizedDescription)"
|
||||||
|
case .unsupportedFeature(let feature): return "\(feature) is not supported by this host"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct APIErrorEnvelope: Decodable {
|
||||||
|
let code: String?
|
||||||
|
let message: String?
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,19 @@ import UserNotifications
|
|||||||
|
|
||||||
enum PingService {
|
enum PingService {
|
||||||
private static var previousPingStates: [String: Bool] = [:]
|
private static var previousPingStates: [String: Bool] = [:]
|
||||||
|
private static var suppressedUntil: [String: Date] = [:]
|
||||||
|
|
||||||
|
static func suppressChecks(for hostname: String, duration: TimeInterval) {
|
||||||
|
suppressedUntil[hostname] = Date().addingTimeInterval(duration)
|
||||||
|
}
|
||||||
|
|
||||||
static func ping(hostname: String, apiKey: String, notificationsEnabled: Bool = true) async -> Bool {
|
static func ping(hostname: String, apiKey: String, notificationsEnabled: Bool = true) async -> Bool {
|
||||||
|
if let suppressedUntil = suppressedUntil[hostname], suppressedUntil > Date() {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
suppressedUntil.removeValue(forKey: hostname)
|
||||||
|
}
|
||||||
|
|
||||||
guard let url = URL(string: "https://\(hostname)/api/v2/ping") else {
|
guard let url = URL(string: "https://\(hostname)/api/v2/ping") else {
|
||||||
print("❌ [PingService] Invalid URL for \(hostname)")
|
print("❌ [PingService] Invalid URL for \(hostname)")
|
||||||
return false
|
return false
|
||||||
@@ -18,9 +29,6 @@ enum PingService {
|
|||||||
do {
|
do {
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
|
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
|
||||||
if let responseString = String(data: data, encoding: .utf8) {
|
|
||||||
print("❌ [PingService] HTTP \(httpResponse.statusCode): \(responseString)")
|
|
||||||
}
|
|
||||||
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -33,7 +41,6 @@ enum PingService {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
print("❌ [PingService] Error pinging \(hostname): \(error)")
|
|
||||||
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
handlePingFailure(for: hostname, notificationsEnabled: notificationsEnabled)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,6 +185,10 @@ struct ServerInfo: Codable, Hashable, Equatable {
|
|||||||
].filter { !$0.isEmpty }
|
].filter { !$0.isEmpty }
|
||||||
return components.isEmpty ? nil : components.joined(separator: " • ")
|
return components.isEmpty ? nil : components.joined(separator: " • ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var supportsRestartCommand: Bool {
|
||||||
|
ServerInfo.version(apiVersion, isAtLeast: "2.14")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Helpers & Sample Data
|
// MARK: - Helpers & Sample Data
|
||||||
@@ -226,6 +230,27 @@ extension ServerInfo {
|
|||||||
return normalized
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func version(_ value: String, isAtLeast minimum: String) -> Bool {
|
||||||
|
let lhs = value
|
||||||
|
.split(separator: ".")
|
||||||
|
.compactMap { Int($0) }
|
||||||
|
let rhs = minimum
|
||||||
|
.split(separator: ".")
|
||||||
|
.compactMap { Int($0) }
|
||||||
|
|
||||||
|
let count = max(lhs.count, rhs.count)
|
||||||
|
for index in 0..<count {
|
||||||
|
let left = index < lhs.count ? lhs[index] : 0
|
||||||
|
let right = index < rhs.count ? rhs[index] : 0
|
||||||
|
|
||||||
|
if left != right {
|
||||||
|
return left > right
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
static let placeholder = ServerInfo(
|
static let placeholder = ServerInfo(
|
||||||
hostname: "preview.example.com",
|
hostname: "preview.example.com",
|
||||||
ipAddresses: ["192.168.1.1", "fe80::1"],
|
ipAddresses: ["192.168.1.1", "fe80::1"],
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ class APIv2_12: BaseAPIClient, ServerAPIProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard httpResponse.statusCode == 200 else {
|
guard httpResponse.statusCode == 200 else {
|
||||||
throw APIError.httpError(httpResponse.statusCode)
|
throw APIError.httpError(httpResponse.statusCode, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
@@ -189,6 +189,10 @@ class APIv2_12: BaseAPIClient, ServerAPIProtocol {
|
|||||||
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
|
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
|
||||||
return envelope.toDomain()
|
return envelope.toDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func restartServer(apiKey: String) async throws {
|
||||||
|
throw APIError.unsupportedFeature("Server reboot")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Server Summary Mapping
|
// MARK: - Server Summary Mapping
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ class APIv2_13: BaseAPIClient, ServerAPIProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard httpResponse.statusCode == 200 else {
|
guard httpResponse.statusCode == 200 else {
|
||||||
throw APIError.httpError(httpResponse.statusCode)
|
throw APIError.httpError(httpResponse.statusCode, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
@@ -189,6 +189,10 @@ class APIv2_13: BaseAPIClient, ServerAPIProtocol {
|
|||||||
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
|
let envelope = try decoder.decode(ServerSummaryEnvelope.self, from: data)
|
||||||
return envelope.toDomain()
|
return envelope.toDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func restartServer(apiKey: String) async throws {
|
||||||
|
throw APIError.unsupportedFeature("Server reboot")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Server Summary Mapping
|
// MARK: - Server Summary Mapping
|
||||||
|
|||||||
30
Sources/Model/API/Versions/APIv2_14.swift
Normal file
30
Sources/Model/API/Versions/APIv2_14.swift
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
//
|
||||||
|
// APIv2_14.swift
|
||||||
|
// iKeyMon
|
||||||
|
//
|
||||||
|
// Created by tracer on 19.04.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class APIv2_14: APIv2_13 {
|
||||||
|
private enum Endpoint: String {
|
||||||
|
case serverReboot = "/api/v2/server/reboot"
|
||||||
|
|
||||||
|
func url(baseURL: URL) -> URL {
|
||||||
|
baseURL.appendingPathComponent(rawValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override func restartServer(apiKey: String) async throws {
|
||||||
|
var request = URLRequest(url: Endpoint.serverReboot.url(baseURL: baseURL))
|
||||||
|
request.httpMethod = "POST"
|
||||||
|
request.setValue(apiKey, forHTTPHeaderField: "X-API-KEY")
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Accept")
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
|
request.timeoutInterval = 30
|
||||||
|
request.httpBody = #"{"confirm":true}"#.data(using: .utf8)
|
||||||
|
|
||||||
|
try await performRequestWithoutBody(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ struct MainView: View {
|
|||||||
@State private var refreshTimer: Timer.TimerPublisher?
|
@State private var refreshTimer: Timer.TimerPublisher?
|
||||||
@State private var refreshSubscription: AnyCancellable?
|
@State private var refreshSubscription: AnyCancellable?
|
||||||
@State private var pingTimer: Timer?
|
@State private var pingTimer: Timer?
|
||||||
|
@State private var restartingServerID: UUID?
|
||||||
@State private var lastRefreshInterval: Int?
|
@State private var lastRefreshInterval: Int?
|
||||||
@State private var previousServiceStates: [String: String] = [:]
|
@State private var previousServiceStates: [String: String] = [:]
|
||||||
private let serverOrderKey = MainView.serverOrderKeyStatic
|
private let serverOrderKey = MainView.serverOrderKeyStatic
|
||||||
@@ -79,7 +80,15 @@ struct MainView: View {
|
|||||||
} detail: {
|
} detail: {
|
||||||
if let selectedServerID,
|
if let selectedServerID,
|
||||||
let index = servers.firstIndex(where: { selectedServerID == $0.id }) {
|
let index = servers.firstIndex(where: { selectedServerID == $0.id }) {
|
||||||
ServerDetailView(server: $servers[index], isFetching: isFetchingInfo)
|
let serverID = servers[index].id
|
||||||
|
ServerDetailView(
|
||||||
|
server: $servers[index],
|
||||||
|
isFetching: isFetchingInfo,
|
||||||
|
canRestart: servers[index].info?.supportsRestartCommand == true,
|
||||||
|
isRestarting: restartingServerID == serverID
|
||||||
|
) {
|
||||||
|
await restartServer(for: serverID)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ContentUnavailableView("No Server Selected", systemImage: "server.rack")
|
ContentUnavailableView("No Server Selected", systemImage: "server.rack")
|
||||||
}
|
}
|
||||||
@@ -235,9 +244,6 @@ struct MainView: View {
|
|||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
servers[index].pingable = pingable
|
servers[index].pingable = pingable
|
||||||
}
|
}
|
||||||
if !pingable {
|
|
||||||
print("📶 [MainView] Ping \(server.hostname): offline")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -332,6 +338,87 @@ struct MainView: View {
|
|||||||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
||||||
UNUserNotificationCenter.current().add(request)
|
UNUserNotificationCenter.current().add(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func restartServer(for id: UUID) async -> ServerActionFeedback {
|
||||||
|
guard let server = servers.first(where: { $0.id == id }) else {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Failed",
|
||||||
|
message: "The selected server could not be found."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard server.info?.supportsRestartCommand == true else {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Unavailable",
|
||||||
|
message: "\(server.hostname) does not support remote reboot via the API."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let apiKey = KeychainHelper.loadApiKey(for: server.hostname)?.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||||
|
!apiKey.isEmpty else {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Failed",
|
||||||
|
message: "No API key is configured for \(server.hostname)."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let baseURL = URL(string: "https://\(server.hostname)") else {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Failed",
|
||||||
|
message: "The server URL for \(server.hostname) is invalid."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
restartingServerID = id
|
||||||
|
defer { restartingServerID = nil }
|
||||||
|
|
||||||
|
do {
|
||||||
|
let api: AnyServerAPI
|
||||||
|
if let versionString = server.info?.apiVersion,
|
||||||
|
let versionedAPI = APIFactory.createAPI(baseURL: baseURL, versionString: versionString) {
|
||||||
|
api = versionedAPI
|
||||||
|
} else {
|
||||||
|
api = try await APIFactory.detectAndCreateAPI(baseURL: baseURL, apiKey: apiKey)
|
||||||
|
}
|
||||||
|
try await api.restartServer(apiKey: apiKey)
|
||||||
|
PingService.suppressChecks(for: server.hostname, duration: 90)
|
||||||
|
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Requested",
|
||||||
|
message: "The reboot command was sent to \(server.hostname). The host may become unavailable briefly while it restarts."
|
||||||
|
)
|
||||||
|
} catch let error as URLError where Self.isExpectedRestartDisconnect(error) {
|
||||||
|
PingService.suppressChecks(for: server.hostname, duration: 90)
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Requested",
|
||||||
|
message: "The reboot command appears to have been accepted by \(server.hostname). The connection dropped while the host was going away, which is expected during a reboot."
|
||||||
|
)
|
||||||
|
} catch APIError.httpError(404, let message) {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Unavailable",
|
||||||
|
message: message ?? "\(server.hostname) returned 404 for /api/v2/server/reboot."
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
return ServerActionFeedback(
|
||||||
|
title: "Reboot Failed",
|
||||||
|
message: error.localizedDescription
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func isExpectedRestartDisconnect(_ error: URLError) -> Bool {
|
||||||
|
switch error.code {
|
||||||
|
case .timedOut,
|
||||||
|
.cannotConnectToHost,
|
||||||
|
.networkConnectionLost,
|
||||||
|
.notConnectedToInternet,
|
||||||
|
.cannotFindHost,
|
||||||
|
.dnsLookupFailed:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|||||||
@@ -7,9 +7,18 @@
|
|||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
struct ServerActionFeedback: Identifiable {
|
||||||
|
let id = UUID()
|
||||||
|
let title: String
|
||||||
|
let message: String
|
||||||
|
}
|
||||||
|
|
||||||
struct ServerDetailView: View {
|
struct ServerDetailView: View {
|
||||||
@Binding var server: Server
|
@Binding var server: Server
|
||||||
var isFetching: Bool
|
var isFetching: Bool
|
||||||
|
var canRestart: Bool = false
|
||||||
|
var isRestarting: Bool = false
|
||||||
|
var onRestart: (() async -> ServerActionFeedback)? = nil
|
||||||
@AppStorage("showIntervalIndicator") private var showIntervalIndicator: Bool = true
|
@AppStorage("showIntervalIndicator") private var showIntervalIndicator: Bool = true
|
||||||
@AppStorage("refreshInterval") private var refreshInterval: Int = 60
|
@AppStorage("refreshInterval") private var refreshInterval: Int = 60
|
||||||
|
|
||||||
@@ -18,6 +27,8 @@ struct ServerDetailView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@State private var progress: Double = 0
|
@State private var progress: Double = 0
|
||||||
|
@State private var showRestartSheet = false
|
||||||
|
@State private var restartFeedback: ServerActionFeedback?
|
||||||
let timer = Timer.publish(every: 1.0 / 60.0, on: .main, in: .common).autoconnect()
|
let timer = Timer.publish(every: 1.0 / 60.0, on: .main, in: .common).autoconnect()
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@@ -33,7 +44,13 @@ struct ServerDetailView: View {
|
|||||||
VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
Spacer().frame(height: 6)
|
Spacer().frame(height: 6)
|
||||||
TabView {
|
TabView {
|
||||||
GeneralView(server: resolvedBinding)
|
GeneralView(
|
||||||
|
server: resolvedBinding,
|
||||||
|
canRestart: canRestart,
|
||||||
|
isRestarting: isRestarting
|
||||||
|
) {
|
||||||
|
showRestartSheet = true
|
||||||
|
}
|
||||||
.tabItem {
|
.tabItem {
|
||||||
Text("General").unredacted()
|
Text("General").unredacted()
|
||||||
}
|
}
|
||||||
@@ -57,6 +74,17 @@ struct ServerDetailView: View {
|
|||||||
}
|
}
|
||||||
.padding(0)
|
.padding(0)
|
||||||
}
|
}
|
||||||
|
.overlay(alignment: .bottomTrailing) {
|
||||||
|
if let feedback = restartFeedback {
|
||||||
|
RestartFeedbackBanner(
|
||||||
|
feedback: feedback,
|
||||||
|
onDismiss: {
|
||||||
|
restartFeedback = nil
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.padding()
|
||||||
|
}
|
||||||
|
}
|
||||||
.onReceive(timer) { _ in
|
.onReceive(timer) { _ in
|
||||||
guard showIntervalIndicator else { return }
|
guard showIntervalIndicator else { return }
|
||||||
withAnimation(.linear(duration: 1.0 / 60.0)) {
|
withAnimation(.linear(duration: 1.0 / 60.0)) {
|
||||||
@@ -64,6 +92,25 @@ struct ServerDetailView: View {
|
|||||||
if progress >= 1 { progress = 0 }
|
if progress >= 1 { progress = 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.sheet(isPresented: $showRestartSheet) {
|
||||||
|
RestartConfirmationSheet(
|
||||||
|
hostname: server.hostname,
|
||||||
|
isRestarting: isRestarting,
|
||||||
|
onCancel: {
|
||||||
|
showRestartSheet = false
|
||||||
|
},
|
||||||
|
onConfirm: {
|
||||||
|
guard let onRestart else { return }
|
||||||
|
showRestartSheet = false
|
||||||
|
Task {
|
||||||
|
let feedback = await onRestart()
|
||||||
|
await MainActor.run {
|
||||||
|
restartFeedback = feedback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var resolvedBinding: Binding<Server> {
|
private var resolvedBinding: Binding<Server> {
|
||||||
@@ -81,7 +128,8 @@ struct ServerDetailView: View {
|
|||||||
#Preview {
|
#Preview {
|
||||||
ServerDetailView(
|
ServerDetailView(
|
||||||
server: .constant(Server(id: UUID(), hostname: "preview.example.com", info: ServerInfo.placeholder)),
|
server: .constant(Server(id: UUID(), hostname: "preview.example.com", info: ServerInfo.placeholder)),
|
||||||
isFetching: false
|
isFetching: false,
|
||||||
|
canRestart: true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,3 +145,67 @@ private struct LoadingBadge: View {
|
|||||||
.background(.ultraThinMaterial, in: Capsule())
|
.background(.ultraThinMaterial, in: Capsule())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct RestartConfirmationSheet: View {
|
||||||
|
let hostname: String
|
||||||
|
let isRestarting: Bool
|
||||||
|
let onCancel: () -> Void
|
||||||
|
let onConfirm: () -> Void
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 16) {
|
||||||
|
Text("Reboot this server?")
|
||||||
|
.font(.title3.weight(.semibold))
|
||||||
|
|
||||||
|
Text("This will send a reboot command to \(hostname).")
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
|
||||||
|
HStack {
|
||||||
|
Spacer()
|
||||||
|
|
||||||
|
Button("Cancel") {
|
||||||
|
onCancel()
|
||||||
|
}
|
||||||
|
.keyboardShortcut(.cancelAction)
|
||||||
|
.disabled(isRestarting)
|
||||||
|
|
||||||
|
Button("Reboot", role: .destructive) {
|
||||||
|
onConfirm()
|
||||||
|
}
|
||||||
|
.keyboardShortcut(.defaultAction)
|
||||||
|
.disabled(isRestarting)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(24)
|
||||||
|
.frame(width: 420)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct RestartFeedbackBanner: View {
|
||||||
|
let feedback: ServerActionFeedback
|
||||||
|
let onDismiss: () -> Void
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 12) {
|
||||||
|
Text(feedback.title)
|
||||||
|
.font(.headline)
|
||||||
|
|
||||||
|
Text(feedback.message)
|
||||||
|
.font(.subheadline)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
|
||||||
|
HStack {
|
||||||
|
Spacer()
|
||||||
|
|
||||||
|
Button("OK") {
|
||||||
|
onDismiss()
|
||||||
|
}
|
||||||
|
.keyboardShortcut(.defaultAction)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.frame(maxWidth: 360, alignment: .leading)
|
||||||
|
.padding(24)
|
||||||
|
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 16, style: .continuous))
|
||||||
|
.shadow(radius: 12)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import SwiftUI
|
|||||||
|
|
||||||
struct GeneralView: View {
|
struct GeneralView: View {
|
||||||
@Binding var server: Server
|
@Binding var server: Server
|
||||||
|
var canRestart: Bool = false
|
||||||
|
var isRestarting: Bool = false
|
||||||
|
var onRestart: (() -> Void)? = nil
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeometryReader { geometry in
|
GeometryReader { geometry in
|
||||||
@@ -117,6 +120,33 @@ struct GeneralView: View {
|
|||||||
monospaced: true
|
monospaced: true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if canRestart, let onRestart {
|
||||||
|
TableRowView(showDivider: false) {
|
||||||
|
Text("Actions")
|
||||||
|
} value: {
|
||||||
|
VStack(alignment: .leading, spacing: 8) {
|
||||||
|
Button(role: .destructive) {
|
||||||
|
onRestart()
|
||||||
|
} label: {
|
||||||
|
if isRestarting {
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
ProgressView()
|
||||||
|
.controlSize(.small)
|
||||||
|
Text("Rebooting…")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Label("Reboot Server", systemImage: "arrow.clockwise.circle")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.disabled(isRestarting)
|
||||||
|
|
||||||
|
Text("Sends a reboot command to the selected host.")
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
.frame(minHeight: geometry.size.height, alignment: .top)
|
.frame(minHeight: geometry.size.height, alignment: .top)
|
||||||
@@ -132,7 +162,7 @@ struct GeneralView: View {
|
|||||||
@State var previewServer = Server(hostname: "example.com", info: .placeholder)
|
@State var previewServer = Server(hostname: "example.com", info: .placeholder)
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeneralView(server: $previewServer)
|
GeneralView(server: $previewServer, canRestart: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user