Skip to content

Commit dfeaff7

Browse files
committed
Only publish git mirrors once
1 parent ff790aa commit dfeaff7

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

Sources/hostmgr/commands/cache/GitMirrorFetchCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ struct GitMirrorFetchCommand: AsyncParsableCommand {
3131
if try !gitMirror.archiveExistsLocally {
3232
Console.info("Fetching the Git Mirror for \(gitMirror.url)")
3333

34-
guard let server = try await servers.first(havingFileAtPath: gitMirror.remoteFilename) else {
34+
guard let server = try await servers.first(havingFileNamed: gitMirror.remoteFilename) else {
3535
Console.exit("No Git Mirror found for \(gitMirror.slug)", style: .error)
3636
}
3737

3838
let progress = Console.startProgress("Downloading Git Mirror", type: .download)
3939
try await server.downloadFile(
40-
at: gitMirror.remoteFilename,
40+
named: gitMirror.remoteFilename,
4141
to: gitMirror.archivePath,
4242
progress: progress.update
4343
)

Sources/hostmgr/commands/cache/GitMirrorPublishCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct GitMirrorPublishCommand: AsyncParsableCommand {
1010
)
1111

1212
@Option(
13-
help: "The URL to the Git repository that will be fetched"
13+
help: "The URL to the Git repository that will be cached"
1414
)
1515
var gitMirror: GitMirror?
1616

@@ -30,7 +30,7 @@ struct GitMirrorPublishCommand: AsyncParsableCommand {
3030
Console.exit("There is no local Git Mirror at \(gitMirror.localPath)", style: .error)
3131
}
3232

33-
guard try await !server.hasFile(at: gitMirror.remoteFilename) else {
33+
guard try await !server.hasFile(named: gitMirror.remoteFilename) else {
3434
Console.exit("Remote mirror already exists – exiting", style: .error)
3535
}
3636

Sources/hostmgr/commands/sync/SyncAuthorizedKeysCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ struct SyncAuthorizedKeysCommand: AsyncParsableCommand, FollowsCommandPolicies {
3737

3838
Console.heading("Syncing Authorized Keys")
3939

40-
guard try await server.hasFile(at: Constants.s3Key) else {
40+
guard try await server.hasFile(named: Constants.s3Key) else {
4141
Console.error("Unable to locate authorized_keys file – exiting")
4242
throw ExitCode(rawValue: 1)
4343
}
4444

4545
let progressBar = Console.startProgress("Downloading `authorized_keys`", type: .download)
46-
try await server.downloadFile(at: Constants.s3Key, to: destination, progress: progressBar.update)
46+
try await server.downloadFile(named: Constants.s3Key, to: destination, progress: progressBar.update)
4747

4848
// Fix the permissions on the file, if needed
4949
Console.info("Setting file permissions on \(destination)")

Sources/libhostmgr/Platforms/RemoteVMLibrary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct RemoteVMLibrary {
4747
}
4848

4949
public func hasImage(named name: String) async throws -> Bool {
50-
try await listImages(sortedBy: .name).contains(where: { $0.name == name })
50+
try await S3Server.vmImages.hasFile(named: name)
5151
}
5252

5353
public func lookupImage(named name: String) async throws -> RemoteVMImage {
@@ -73,14 +73,14 @@ public struct RemoteVMLibrary {
7373
throw HostmgrError.notEnoughLocalDiskSpaceToDownloadFile(image.fileName, image.size, availableStorageSpace)
7474
}
7575

76-
guard let server = try await servers.first(havingFileAtPath: image.fileName) else {
76+
guard let server = try await servers.first(havingFileNamed: image.fileName) else {
7777
throw HostmgrError.unableToFindRemoteImage(name)
7878
}
7979

8080
let destination = Paths.vmImageStorageDirectory.appendingPathComponent(image.fileName)
8181

8282
try await server.downloadFile(
83-
at: image.fileName,
83+
named: image.fileName,
8484
to: destination,
8585
progress: progressCallback
8686
)

Sources/libhostmgr/Servers/CacheServer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public struct CacheServer: ReadableRemoteFileProvider {
7070
self.basePath = path
7171
}
7272

73-
public func hasFile(at path: String) async throws -> Bool {
74-
guard let url = baseURL?.appendingPathComponent(path) else {
73+
public func hasFile(named name: String) async throws -> Bool {
74+
guard let url = baseURL?.appendingPathComponent(name) else {
7575
return false
7676
}
7777

@@ -85,11 +85,11 @@ public struct CacheServer: ReadableRemoteFileProvider {
8585
}
8686

8787
public func downloadFile(
88-
at path: String,
88+
named name: String,
8989
to destination: URL,
9090
progress: @escaping ProgressCallback)
9191
async throws {
92-
guard let url = baseURL?.appendingPathComponent(path) else {
92+
guard let url = baseURL?.appendingPathComponent(name) else {
9393
preconditionFailure("Don't try to download a file without checking if it exists first")
9494
}
9595

Sources/libhostmgr/Servers/RemoteFileProvider.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import Foundation
22

33
public protocol RemoteFileProvider {
4-
func hasFile(at path: String) async throws -> Bool
4+
func hasFile(named name: String) async throws -> Bool
55
}
66

77
public protocol ReadableRemoteFileProvider: RemoteFileProvider {
8-
func downloadFile(at path: String, to destination: URL, progress: @escaping ProgressCallback) async throws
8+
func downloadFile(named name: String, to destination: URL, progress: @escaping ProgressCallback) async throws
99
}
1010

1111
protocol WritableRemoteFileProvider: RemoteFileProvider {
1212
func uploadFile(at source: URL, to destination: String, progress: @escaping ProgressCallback) async throws
1313
}
1414

1515
public extension [ReadableRemoteFileProvider] {
16-
func first(havingFileAtPath path: String) async throws -> ReadableRemoteFileProvider? {
16+
func first(havingFileNamed name: String) async throws -> ReadableRemoteFileProvider? {
1717
for server in self {
1818
do {
19-
if try await server.hasFile(at: path) {
19+
if try await server.hasFile(named: name) {
2020
return server
2121
}
2222
} catch {

Sources/libhostmgr/Servers/S3Server.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct S3Server: RemoteFileProvider {
1818

1919
public static let vmImages: S3Server = S3Server(
2020
bucketName: Configuration.shared.vmImagesBucket,
21-
prefix: "images",
21+
prefix: "images/",
2222
endpoint: .accelerated
2323
)
2424

@@ -32,8 +32,14 @@ public struct S3Server: RemoteFileProvider {
3232
try await s3Client.list(bucket: bucketName, prefix: prefix ?? "/").objects.map(self.convert)
3333
}
3434

35-
public func hasFile(at path: String) async throws -> Bool {
36-
try await listFiles().contains { $0.path.hasPrefix(path) }
35+
public func hasFile(named name: String) async throws -> Bool {
36+
do {
37+
let object = try await s3Client.head(bucket: bucketName, key: key(fromName: name))
38+
return object != nil
39+
} catch let err {
40+
debugPrint(err.localizedDescription)
41+
return false
42+
}
3743
}
3844

3945
var s3Client: S3Client {
@@ -45,12 +51,16 @@ public struct S3Server: RemoteFileProvider {
4551
func convert(_ s3Object: S3Object) -> RemoteFile {
4652
RemoteFile(size: s3Object.size, path: s3Object.key, lastModifiedAt: s3Object.lastModifiedAt)
4753
}
54+
55+
func key(fromName name: String) -> String {
56+
"\(self.prefix ?? "/")\(name)"
57+
}
4858
}
4959

5060
extension S3Server: ReadableRemoteFileProvider {
51-
public func downloadFile(at path: String, to destination: URL, progress: @escaping ProgressCallback) async throws {
61+
public func downloadFile(named name: String, to destination: URL, progress: @escaping ProgressCallback) async throws {
5262
let tempUrl = try await s3Client.download(
53-
objectWithKey: path,
63+
objectWithKey: key(fromName: name),
5464
inBucket: self.bucketName,
5565
progressCallback: progress
5666
)

0 commit comments

Comments
 (0)