Skip to content

Commit 10d4aae

Browse files
committed
Fix issues with first-time DHCP lease
1 parent 64bcf28 commit 10d4aae

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Cilicon/VMManager.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ class VMManager: NSObject, ObservableObject {
8282
}.value
8383
}
8484

85+
private func fetchIP(macAddress: String) async throws -> String {
86+
var leaseTries = 0
87+
88+
while true {
89+
let ipResult = Result {
90+
try LeaseParser.leaseForMacAddress(mac: macAddress).ipAddress
91+
}
92+
switch ipResult {
93+
case let .success(ip):
94+
return ip
95+
case let .failure(err):
96+
if leaseTries >= 5 {
97+
throw err
98+
}
99+
try await Task.sleep(for: .seconds(5))
100+
leaseTries += 1
101+
}
102+
}
103+
}
104+
85105
@MainActor
86106
private func setupAndRunVirtualMachine() async throws {
87107
try await cloneBundle()
@@ -93,7 +113,7 @@ class VMManager: NSObject, ObservableObject {
93113
SSHLogger.shared.log(string: "---------- Starting Up ----------\n")
94114
try await virtualMachine.start()
95115
vmState = .running(virtualMachine)
96-
self.ip = try LeaseParser.leaseForMacAddress(mac: masterBundle.configuration.macAddress.string).ipAddress
116+
self.ip = try await fetchIP(macAddress: clonedBundle.configuration.macAddress.string)
97117

98118
let client = try await SSHClient.connect(
99119
host: ip,

0 commit comments

Comments
 (0)