mirror of
https://github.com/cirruslabs/tart.git
synced 2026-10-21 09:42:13 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6321d547cf | ||
|
|
f241e21614 | ||
|
|
0eca923604 | ||
|
|
87f29cc11f | ||
|
|
90d1393137 | ||
|
|
625d431d10 | ||
|
|
4648e1aea1 | ||
|
|
4b62b73015 | ||
|
|
ae7018c31f | ||
|
|
e54c89da0c | ||
|
|
9a0ec3e6b0 | ||
|
|
400f85a493 | ||
|
|
0105280b5d | ||
|
|
c063c5bdc2 | ||
|
|
8a2b0151e0 | ||
|
|
2eea51d6ec | ||
|
|
1890909d28 | ||
|
|
0cad2e454c | ||
|
|
17dcf942c1 | ||
|
|
6296df7c0c | ||
|
|
c54b140750 | ||
|
|
048a5506df | ||
|
|
553b36349b |
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
export VERSION="${CIRRUS_TAG:-0}"
|
||||||
|
|
||||||
|
mkdir -p .ci/pkg/
|
||||||
|
cp .build/arm64-apple-macosx/debug/tart .ci/pkg/
|
||||||
|
pkgbuild --root .ci/pkg --version $VERSION --install-location /usr/local/bin/ --identifier com.github.cirruslabs.tart --sign "Developer ID Installer: Fedor Korotkov (9M2P8L4D89)" "./dist/Tart-$VERSION.pkg"
|
||||||
|
xcrun notarytool submit "./dist/Tart-$VERSION.pkg" --keychain-profile "notarytool" --wait
|
||||||
|
xcrun stapler staple "./dist/Tart-$VERSION.pkg"
|
||||||
+15
-1
@@ -22,10 +22,24 @@ task:
|
|||||||
macos_instance:
|
macos_instance:
|
||||||
image: ghcr.io/cirruslabs/macos-ventura-xcode:latest
|
image: ghcr.io/cirruslabs/macos-ventura-xcode:latest
|
||||||
env:
|
env:
|
||||||
|
MACOS_CERTIFICATE: ENCRYPTED[8a6930a8c1286e7e536ea41b7647ea40e99174ad15e9cfcc753754fea55a619b355415629dff515b54a8921643e314e5]
|
||||||
|
AC_PASSWORD: ENCRYPTED[4a761023e7e06fe2eb350c8b6e8e7ca961af193cb9ba47605f25f1d353abc3142606f412e405be48fd897a78787ea8c2]
|
||||||
GITHUB_TOKEN: ENCRYPTED[!98ace8259c6024da912c14d5a3c5c6aac186890a8d4819fad78f3e0c41a4e0cd3a2537dd6e91493952fb056fa434be7c!]
|
GITHUB_TOKEN: ENCRYPTED[!98ace8259c6024da912c14d5a3c5c6aac186890a8d4819fad78f3e0c41a4e0cd3a2537dd6e91493952fb056fa434be7c!]
|
||||||
GORELEASER_KEY: ENCRYPTED[!9b80b6ef684ceaf40edd4c7af93014ee156c8aba7e6e5795f41c482729887b5c31f36b651491d790f1f668670888d9fd!]
|
GORELEASER_KEY: ENCRYPTED[!9b80b6ef684ceaf40edd4c7af93014ee156c8aba7e6e5795f41c482729887b5c31f36b651491d790f1f668670888d9fd!]
|
||||||
install_script: brew install go goreleaser/tap/goreleaser-pro
|
setup_script:
|
||||||
|
- cd $HOME
|
||||||
|
- echo $MACOS_CERTIFICATE | base64 --decode > certificate.p12
|
||||||
|
- security create-keychain -p password101 build.keychain
|
||||||
|
- security default-keychain -s build.keychain
|
||||||
|
- security unlock-keychain -p password101 build.keychain
|
||||||
|
- security import certificate.p12 -k build.keychain -P password101 -T /usr/bin/codesign -T /usr/bin/pkgbuild
|
||||||
|
- security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k password101 build.keychain
|
||||||
|
- xcrun notarytool store-credentials "notarytool" --apple-id "hello@cirruslabs.org" --team-id "9M2P8L4D89" --password $AC_PASSWORD
|
||||||
|
install_script:
|
||||||
|
- brew install go goreleaser/tap/goreleaser-pro
|
||||||
|
- brew install mitchellh/gon/gon
|
||||||
info_script:
|
info_script:
|
||||||
|
- security find-identity -v
|
||||||
- xcodebuild -version
|
- xcodebuild -version
|
||||||
- swift -version
|
- swift -version
|
||||||
release_script: goreleaser
|
release_script: goreleaser
|
||||||
|
|||||||
+7
-2
@@ -7,13 +7,16 @@ builds:
|
|||||||
goarch:
|
goarch:
|
||||||
- arm64
|
- arm64
|
||||||
prebuilt:
|
prebuilt:
|
||||||
path: .build/{{ .Arch }}-apple-macosx/debug/tart
|
path: .build/arm64-apple-macosx/debug/tart
|
||||||
|
hooks:
|
||||||
|
post:
|
||||||
|
- gon gon.hcl
|
||||||
|
- .ci/create-pkg.sh
|
||||||
|
|
||||||
before:
|
before:
|
||||||
hooks:
|
hooks:
|
||||||
- .ci/set-version.sh
|
- .ci/set-version.sh
|
||||||
- swift build -c debug --product tart
|
- swift build -c debug --product tart
|
||||||
- codesign --sign - --entitlements Resources/tart.entitlements --force .build/arm64-apple-macosx/debug/tart
|
|
||||||
|
|
||||||
archives:
|
archives:
|
||||||
- id: binary
|
- id: binary
|
||||||
@@ -24,6 +27,8 @@ archives:
|
|||||||
|
|
||||||
release:
|
release:
|
||||||
prerelease: auto
|
prerelease: auto
|
||||||
|
extra_files:
|
||||||
|
- glob: ./dist/Tart-{{ .Tag }}.pkg
|
||||||
|
|
||||||
brews:
|
brews:
|
||||||
- name: tart
|
- name: tart
|
||||||
|
|||||||
@@ -8,6 +8,22 @@ Built by CI engineers for your automation needs. Here are some highlights of Tar
|
|||||||
* Use Tart Packer Plugin to automate VM creation.
|
* Use Tart Packer Plugin to automate VM creation.
|
||||||
* Built-in CI integration.
|
* Built-in CI integration.
|
||||||
|
|
||||||
|
*Tart* is already adopted by several automation services:
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://cirrus-ci.org/guide/macOS/" target=_blank>
|
||||||
|
<img src="https://github.com/cirruslabs/tart/raw/main/Resources/Users/CirrusCI.png" height="65"/>
|
||||||
|
</a>
|
||||||
|
<a href="https://codemagic.io/" target=_blank>
|
||||||
|
<img src="https://github.com/cirruslabs/tart/raw/main/Resources/Users/Codemagic.png" height="65"/>
|
||||||
|
</a>
|
||||||
|
<a href="https://testingbot.com/" target=_blank>
|
||||||
|
<img src="https://github.com/cirruslabs/tart/raw/main/Resources/Users/TestingBot.png" height="65"/>
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
Try running a Tart VM on your Apple Silicon device running macOS Monterey or later (will download a 25 GB image):
|
Try running a Tart VM on your Apple Silicon device running macOS Monterey or later (will download a 25 GB image):
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
@@ -191,10 +207,20 @@ tart push my-local-vm-name acme.io/remoteorg/name:latest acme.io/remoteorg/name:
|
|||||||
|
|
||||||
#### Pulling a Remote Image
|
#### Pulling a Remote Image
|
||||||
|
|
||||||
|
You can either pull an image:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
tart pull acme.io/remoteorg/name:latest my-local-vm-name
|
tart pull acme.io/remoteorg/name:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
|
...or instantiate a VM from a remote image:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
tart clone acme.io/remoteorg/name:latest my-local-vm-name
|
||||||
|
```
|
||||||
|
|
||||||
|
This invocation calls the `tart pull` implicitly (if the image is not being present) before doing the actual cloning.
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -242,3 +268,21 @@ tart pull acme.io/remoteorg/name:latest my-local-vm-name
|
|||||||
Tart is limited by functionality of Apple's `Virtualization.Framework`. At the moment `Virtualization.Framework`
|
Tart is limited by functionality of Apple's `Virtualization.Framework`. At the moment `Virtualization.Framework`
|
||||||
doesn't support nested virtualization.
|
doesn't support nested virtualization.
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Changing the default NAT subnet</summary>
|
||||||
|
|
||||||
|
To change the default network to `192.168.77.1`:
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.vmnet.plist Shared_Net_Address -string 192.168.77.1
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that even through a network would normally be specified as `192.168.77.0`, the [vmnet framework](https://developer.apple.com/documentation/vmnet) seems to treat this as a starting address too and refuses to pick up such network-like values.
|
||||||
|
|
||||||
|
The default subnet mask `255.255.255.0` should suffice for most use-cases, however, you can also change it to `255.255.0.0`, for example:
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.vmnet.plist Shared_Net_Mask -string 255.255.0.0
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:dbcf6f65f26c0e17b2fe55a9712fc783315cd64905a0febce758b31d7e4e923b
|
||||||
|
size 6787
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:4239996f30145992936cfc8faa0232fa8904aedf4de61e30ef4c4fc86cad587f
|
||||||
|
size 14588
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:227192fcd215c9cc05b1ebcd2616bd1f8c95be184726f64d91fc4507a88c66e1
|
||||||
|
size 18393
|
||||||
@@ -32,12 +32,22 @@ struct Clone: AsyncParsableCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let sourceVM = try VMStorageHelper.open(sourceName)
|
let sourceVM = try VMStorageHelper.open(sourceName)
|
||||||
let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress())
|
|
||||||
|
|
||||||
let tmpVMDir = try VMDirectory.temporary()
|
let tmpVMDir = try VMDirectory.temporary()
|
||||||
|
|
||||||
|
// Lock the temporary VM directory to prevent it's garbage collection
|
||||||
|
let tmpVMDirLock = try FileLock(lockURL: tmpVMDir.baseURL)
|
||||||
|
try tmpVMDirLock.lock()
|
||||||
|
|
||||||
try await withTaskCancellationHandler(operation: {
|
try await withTaskCancellationHandler(operation: {
|
||||||
|
let lock = try FileLock(lockURL: Config().tartHomeDir)
|
||||||
|
try lock.lock()
|
||||||
|
|
||||||
|
let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress())
|
||||||
try sourceVM.clone(to: tmpVMDir, generateMAC: generateMAC)
|
try sourceVM.clone(to: tmpVMDir, generateMAC: generateMAC)
|
||||||
try localStorage.move(newName, from: tmpVMDir)
|
try localStorage.move(newName, from: tmpVMDir)
|
||||||
|
|
||||||
|
try lock.unlock()
|
||||||
}, onCancel: {
|
}, onCancel: {
|
||||||
try? FileManager.default.removeItem(at: tmpVMDir.baseURL)
|
try? FileManager.default.removeItem(at: tmpVMDir.baseURL)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ struct Create: AsyncParsableCommand {
|
|||||||
func run() async throws {
|
func run() async throws {
|
||||||
do {
|
do {
|
||||||
let tmpVMDir = try VMDirectory.temporary()
|
let tmpVMDir = try VMDirectory.temporary()
|
||||||
|
|
||||||
|
// Lock the temporary VM directory to prevent it's garbage collection
|
||||||
|
let tmpVMDirLock = try FileLock(lockURL: tmpVMDir.baseURL)
|
||||||
|
try tmpVMDirLock.lock()
|
||||||
|
|
||||||
try await withTaskCancellationHandler(operation: {
|
try await withTaskCancellationHandler(operation: {
|
||||||
if let fromIPSW = fromIPSW {
|
if let fromIPSW = fromIPSW {
|
||||||
if fromIPSW == "latest" {
|
if fromIPSW == "latest" {
|
||||||
@@ -40,7 +45,7 @@ struct Create: AsyncParsableCommand {
|
|||||||
if #available(macOS 13, *) {
|
if #available(macOS 13, *) {
|
||||||
_ = try await VM.linux(vmDir: tmpVMDir, diskSizeGB: diskSize)
|
_ = try await VM.linux(vmDir: tmpVMDir, diskSizeGB: diskSize)
|
||||||
} else {
|
} else {
|
||||||
throw UnsupportedOSError()
|
throw UnsupportedOSError("Linux VMs", "are")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ struct Login: AsyncParsableCommand {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
let registry = try Registry(host: host, namespace: "", insecure: insecure,
|
let registry = try Registry(host: host, namespace: "", insecure: insecure,
|
||||||
credentialsProvider: credentialsProvider)
|
credentialsProviders: [credentialsProvider])
|
||||||
try await registry.ping()
|
try await registry.ping()
|
||||||
} catch {
|
} catch {
|
||||||
print("invalid credentials: \(error)")
|
print("invalid credentials: \(error)")
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ struct Prune: AsyncParsableCommand {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheReclaimedBytes -= try prunable.sizeBytes()
|
cacheReclaimedBytes += try prunable.sizeBytes()
|
||||||
try prunable.delete()
|
try prunable.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,13 +37,23 @@ struct Run: AsyncParsableCommand {
|
|||||||
@Flag var withSoftnet: Bool = false
|
@Flag var withSoftnet: Bool = false
|
||||||
|
|
||||||
@Option(help: ArgumentHelp("""
|
@Option(help: ArgumentHelp("""
|
||||||
Additional disk attachments with an optional read-only specifier\n(e.g. --disk=\"disk.bin\" --disk=\"disk.bin:ro\")
|
Additional disk attachments with an optional read-only specifier\n(e.g. --disk=\"disk.bin\" --disk=\"ubuntu.iso:ro\")
|
||||||
""", discussion: """
|
""", discussion: """
|
||||||
Learn how to create a disk image using Disk Utility here:
|
Learn how to create a disk image using Disk Utility here:
|
||||||
https://support.apple.com/en-gb/guide/disk-utility/dskutl11888/mac
|
https://support.apple.com/en-gb/guide/disk-utility/dskutl11888/mac
|
||||||
"""))
|
""", valueName: "path[:ro]"))
|
||||||
var disk: [String] = []
|
var disk: [String] = []
|
||||||
|
|
||||||
|
@Option(help: ArgumentHelp("""
|
||||||
|
Additional directory shares with an optional read-only specifier\n(e.g. --dir=\"build:~/src/build\" --dir=\"sources:~/src/sources:ro\")
|
||||||
|
""", discussion: """
|
||||||
|
Requires host to be macOS 13.0 (Ventura) or newer.
|
||||||
|
All shared directories are automatically mounted to "/Volumes/My Shared Files" directory on macOS,
|
||||||
|
while on Linux you have to do it manually: "mount -t virtiofs com.apple.virtio-fs.automount /mount/point".
|
||||||
|
For macOS guests, they must be running macOS 13.0 (Ventura) or newer.
|
||||||
|
""", valueName: "name:path[:ro]"))
|
||||||
|
var dir: [String] = []
|
||||||
|
|
||||||
func validate() throws {
|
func validate() throws {
|
||||||
if vnc && vncExperimental {
|
if vnc && vncExperimental {
|
||||||
throw ValidationError("--vnc and --vnc-experimental are mutually exclusive")
|
throw ValidationError("--vnc and --vnc-experimental are mutually exclusive")
|
||||||
@@ -56,7 +66,8 @@ struct Run: AsyncParsableCommand {
|
|||||||
vm = try VM(
|
vm = try VM(
|
||||||
vmDir: vmDir,
|
vmDir: vmDir,
|
||||||
withSoftnet: withSoftnet,
|
withSoftnet: withSoftnet,
|
||||||
additionalDiskAttachments: additionalDiskAttachments()
|
additionalDiskAttachments: additionalDiskAttachments(),
|
||||||
|
directoryShares: directoryShares()
|
||||||
)
|
)
|
||||||
|
|
||||||
let vncImpl: VNC? = try {
|
let vncImpl: VNC? = try {
|
||||||
@@ -117,8 +128,9 @@ struct Run: AsyncParsableCommand {
|
|||||||
func additionalDiskAttachments() throws -> [VZDiskImageStorageDeviceAttachment] {
|
func additionalDiskAttachments() throws -> [VZDiskImageStorageDeviceAttachment] {
|
||||||
var result: [VZDiskImageStorageDeviceAttachment] = []
|
var result: [VZDiskImageStorageDeviceAttachment] = []
|
||||||
let readOnlySuffix = ":ro"
|
let readOnlySuffix = ":ro"
|
||||||
|
let expandedDiskPaths = disk.map { NSString(string:$0).expandingTildeInPath }
|
||||||
|
|
||||||
for rawDisk in disk {
|
for rawDisk in expandedDiskPaths {
|
||||||
if rawDisk.hasSuffix(readOnlySuffix) {
|
if rawDisk.hasSuffix(readOnlySuffix) {
|
||||||
result.append(try VZDiskImageStorageDeviceAttachment(
|
result.append(try VZDiskImageStorageDeviceAttachment(
|
||||||
url: URL(fileURLWithPath: String(rawDisk.prefix(rawDisk.count - readOnlySuffix.count))),
|
url: URL(fileURLWithPath: String(rawDisk.prefix(rawDisk.count - readOnlySuffix.count))),
|
||||||
@@ -135,6 +147,34 @@ struct Run: AsyncParsableCommand {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func directoryShares() throws -> [DirectoryShare] {
|
||||||
|
var result: [DirectoryShare] = []
|
||||||
|
|
||||||
|
for rawDir in dir {
|
||||||
|
let splits = rawDir.split(maxSplits: 2) { $0 == ":" }
|
||||||
|
|
||||||
|
if splits.count < 2 {
|
||||||
|
throw ValidationError("invalid --dir syntax: should at least include name and path, colon-separated")
|
||||||
|
}
|
||||||
|
|
||||||
|
var readOnly: Bool = false
|
||||||
|
|
||||||
|
if splits.count == 3 {
|
||||||
|
if splits[2] == "ro" {
|
||||||
|
readOnly = true
|
||||||
|
} else {
|
||||||
|
throw ValidationError("invalid --dir syntax: optional read-only specifier can only be \"ro\"")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (name, path) = (String(splits[0]), String(splits[1]))
|
||||||
|
|
||||||
|
result.append(DirectoryShare(name: name, path: URL(fileURLWithPath: NSString(string: path).expandingTildeInPath), readOnly: readOnly))
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
private func runUI() {
|
private func runUI() {
|
||||||
let nsApp = NSApplication.shared
|
let nsApp = NSApplication.shared
|
||||||
nsApp.setActivationPolicy(.regular)
|
nsApp.setActivationPolicy(.regular)
|
||||||
@@ -148,6 +188,8 @@ struct Run: AsyncParsableCommand {
|
|||||||
Group {
|
Group {
|
||||||
VMView(vm: vm!).onAppear {
|
VMView(vm: vm!).onAppear {
|
||||||
NSWindow.allowsAutomaticWindowTabbing = false
|
NSWindow.allowsAutomaticWindowTabbing = false
|
||||||
|
}.onDisappear {
|
||||||
|
NSApplication.shared.terminate(self)
|
||||||
}
|
}
|
||||||
}.frame(width: CGFloat(vm!.config.display.width), height: CGFloat(vm!.config.display.height))
|
}.frame(width: CGFloat(vm!.config.display.width), height: CGFloat(vm!.config.display.height))
|
||||||
}.commands {
|
}.commands {
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ import Foundation
|
|||||||
struct Config {
|
struct Config {
|
||||||
let tartHomeDir: URL
|
let tartHomeDir: URL
|
||||||
let tartCacheDir: URL
|
let tartCacheDir: URL
|
||||||
|
let tartTmpDir: URL
|
||||||
|
|
||||||
init() {
|
init() throws {
|
||||||
var tartHomeDir: URL
|
var tartHomeDir: URL
|
||||||
|
|
||||||
if let customTartHome = ProcessInfo.processInfo.environment["TART_HOME"] {
|
if let customTartHome = ProcessInfo.processInfo.environment["TART_HOME"] {
|
||||||
@@ -14,9 +15,27 @@ struct Config {
|
|||||||
.homeDirectoryForCurrentUser
|
.homeDirectoryForCurrentUser
|
||||||
.appendingPathComponent(".tart", isDirectory: true)
|
.appendingPathComponent(".tart", isDirectory: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tartHomeDir = tartHomeDir
|
self.tartHomeDir = tartHomeDir
|
||||||
|
|
||||||
tartCacheDir = tartHomeDir.appendingPathComponent("cache", isDirectory: true)
|
tartCacheDir = tartHomeDir.appendingPathComponent("cache", isDirectory: true)
|
||||||
|
try FileManager.default.createDirectory(at: tartCacheDir, withIntermediateDirectories: true)
|
||||||
|
|
||||||
|
tartTmpDir = tartHomeDir.appendingPathComponent("tmp", isDirectory: true)
|
||||||
|
try FileManager.default.createDirectory(at: tartTmpDir, withIntermediateDirectories: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func gc() throws {
|
||||||
|
for entry in try FileManager.default.contentsOfDirectory(at: tartTmpDir,
|
||||||
|
includingPropertiesForKeys: [], options: []) {
|
||||||
|
let lock = try FileLock(lockURL: entry)
|
||||||
|
if try !lock.trylock() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
try FileManager.default.removeItem(at: entry)
|
||||||
|
|
||||||
|
try lock.unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func jsonEncoder() -> JSONEncoder {
|
static func jsonEncoder() -> JSONEncoder {
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
class DockerConfigCredentialsProvider: CredentialsProvider {
|
||||||
|
func retrieve(host: String) throws -> (String, String)? {
|
||||||
|
let dockerConfigURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".docker").appendingPathComponent("config.json")
|
||||||
|
if !FileManager.default.fileExists(atPath: dockerConfigURL.path) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let config = try JSONDecoder().decode(DockerConfig.self, from: Data(contentsOf: dockerConfigURL))
|
||||||
|
|
||||||
|
if let credentialsFromAuth = config.auths?[host]?.decodeCredentials() {
|
||||||
|
return credentialsFromAuth
|
||||||
|
}
|
||||||
|
if let helperProgram = config.credHelpers?[host] {
|
||||||
|
return try executeHelper(binaryName: "docker-credential-\(helperProgram)", host: host)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func executeHelper(binaryName: String, host: String) throws -> (String, String)? {
|
||||||
|
guard let executableURL = resolveBinaryPath(binaryName) else {
|
||||||
|
throw CredentialsProviderError.Failed(message: "\(binaryName) not found in PATH")
|
||||||
|
}
|
||||||
|
|
||||||
|
let process = Process.init()
|
||||||
|
process.executableURL = executableURL
|
||||||
|
process.arguments = ["get"]
|
||||||
|
|
||||||
|
let outPipe = Pipe()
|
||||||
|
let inPipe = Pipe()
|
||||||
|
|
||||||
|
process.standardOutput = outPipe
|
||||||
|
process.standardError = outPipe
|
||||||
|
process.standardInput = inPipe
|
||||||
|
|
||||||
|
process.launch()
|
||||||
|
|
||||||
|
inPipe.fileHandleForWriting.write("\(host)\n".data(using: .utf8)!)
|
||||||
|
inPipe.fileHandleForWriting.closeFile()
|
||||||
|
|
||||||
|
process.waitUntilExit()
|
||||||
|
|
||||||
|
if !(process.terminationReason == .exit && process.terminationStatus == 0) {
|
||||||
|
throw CredentialsProviderError.Failed(message: "Docker helper failed!")
|
||||||
|
}
|
||||||
|
|
||||||
|
let getOutput = try JSONDecoder().decode(
|
||||||
|
DockerGetOutput.self, from: outPipe.fileHandleForReading.readDataToEndOfFile()
|
||||||
|
)
|
||||||
|
return (getOutput.Username, getOutput.Secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func store(host: String, user: String, password: String) throws {
|
||||||
|
throw CredentialsProviderError.Failed(message: "Docker helpers don't support storing!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DockerConfig: Codable {
|
||||||
|
var auths: Dictionary<String, DockerAuthConfig>? = Dictionary()
|
||||||
|
var credHelpers: Dictionary<String, String>? = Dictionary()
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DockerAuthConfig: Codable {
|
||||||
|
var auth: String? = nil
|
||||||
|
|
||||||
|
func decodeCredentials() -> (String, String)? {
|
||||||
|
// auth is a base64("username:password")
|
||||||
|
guard let authBase64 = auth else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard let data = Data(base64Encoded: authBase64) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard let components = String(data: data, encoding: .utf8)?.components(separatedBy: ":") else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if components.count != 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return (components[0], components[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DockerGetOutput: Codable {
|
||||||
|
var Username: String
|
||||||
|
var Secret: String
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import Foundation
|
||||||
|
import System
|
||||||
|
|
||||||
|
enum FileLockError: Error, Equatable {
|
||||||
|
case Failed(_ message: String)
|
||||||
|
case AlreadyLocked
|
||||||
|
}
|
||||||
|
|
||||||
|
class FileLock {
|
||||||
|
let url: URL
|
||||||
|
let fd: Int32
|
||||||
|
|
||||||
|
init(lockURL: URL) throws {
|
||||||
|
url = lockURL
|
||||||
|
fd = open(lockURL.path, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
close(fd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func trylock() throws -> Bool {
|
||||||
|
try flockWrapper(LOCK_EX | LOCK_NB)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lock() throws {
|
||||||
|
_ = try flockWrapper(LOCK_EX)
|
||||||
|
}
|
||||||
|
|
||||||
|
func unlock() throws {
|
||||||
|
_ = try flockWrapper(LOCK_UN)
|
||||||
|
}
|
||||||
|
|
||||||
|
func flockWrapper(_ operation: Int32) throws -> Bool {
|
||||||
|
let ret = flock(fd, operation)
|
||||||
|
if ret != 0 {
|
||||||
|
let details = Errno(rawValue: CInt(errno))
|
||||||
|
|
||||||
|
if (operation & LOCK_NB) != 0 && details == .wouldBlock {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
throw FileLockError.Failed("failed to lock \(url): \(details)")
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ class IPSWCache: PrunableStorage {
|
|||||||
let baseURL: URL
|
let baseURL: URL
|
||||||
|
|
||||||
init() throws {
|
init() throws {
|
||||||
baseURL = Config().tartCacheDir.appendingPathComponent("IPSWs", isDirectory: true)
|
baseURL = try Config().tartCacheDir.appendingPathComponent("IPSWs", isDirectory: true)
|
||||||
try FileManager.default.createDirectory(at: baseURL, withIntermediateDirectories: true)
|
try FileManager.default.createDirectory(at: baseURL, withIntermediateDirectories: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -88,29 +88,29 @@ class Registry {
|
|||||||
|
|
||||||
let baseURL: URL
|
let baseURL: URL
|
||||||
let namespace: String
|
let namespace: String
|
||||||
let credentialsProvider: CredentialsProvider
|
let credentialsProviders: [CredentialsProvider]
|
||||||
|
|
||||||
var currentAuthToken: Authentication? = nil
|
var currentAuthToken: Authentication? = nil
|
||||||
|
|
||||||
init(urlComponents: URLComponents,
|
init(urlComponents: URLComponents,
|
||||||
namespace: String,
|
namespace: String,
|
||||||
credentialsProvider: CredentialsProvider = KeychainCredentialsProvider()
|
credentialsProviders: [CredentialsProvider] = [DockerConfigCredentialsProvider(), KeychainCredentialsProvider()]
|
||||||
) throws {
|
) throws {
|
||||||
baseURL = urlComponents.url!
|
baseURL = urlComponents.url!
|
||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
self.credentialsProvider = credentialsProvider
|
self.credentialsProviders = credentialsProviders
|
||||||
}
|
}
|
||||||
|
|
||||||
convenience init(
|
convenience init(
|
||||||
host: String,
|
host: String,
|
||||||
namespace: String,
|
namespace: String,
|
||||||
insecure: Bool = false,
|
insecure: Bool = false,
|
||||||
credentialsProvider: CredentialsProvider = KeychainCredentialsProvider()
|
credentialsProviders: [CredentialsProvider] = [DockerConfigCredentialsProvider(), KeychainCredentialsProvider()]
|
||||||
) throws {
|
) throws {
|
||||||
let proto = insecure ? "http" : "https"
|
let proto = insecure ? "http" : "https"
|
||||||
let baseURLComponents = URLComponents(string: proto + "://" + host + "/v2/")!
|
let baseURLComponents = URLComponents(string: proto + "://" + host + "/v2/")!
|
||||||
|
|
||||||
try self.init(urlComponents: baseURLComponents, namespace: namespace, credentialsProvider: credentialsProvider)
|
try self.init(urlComponents: baseURLComponents, namespace: namespace, credentialsProviders: credentialsProviders)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ping() async throws {
|
func ping() async throws {
|
||||||
@@ -303,7 +303,7 @@ class Registry {
|
|||||||
let wwwAuthenticate = try WWWAuthenticate(rawHeaderValue: wwwAuthenticateRaw)
|
let wwwAuthenticate = try WWWAuthenticate(rawHeaderValue: wwwAuthenticateRaw)
|
||||||
|
|
||||||
if wwwAuthenticate.scheme == "Basic" {
|
if wwwAuthenticate.scheme == "Basic" {
|
||||||
if let (user, password) = try credentialsProvider.retrieve(host: baseURL.host!) {
|
if let (user, password) = try lookupCredentials(host: baseURL.host!) {
|
||||||
currentAuthToken = BasicAuthentication(user: user, password: password)
|
currentAuthToken = BasicAuthentication(user: user, password: password)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ class Registry {
|
|||||||
|
|
||||||
var headers: Dictionary<String, String> = Dictionary()
|
var headers: Dictionary<String, String> = Dictionary()
|
||||||
|
|
||||||
if let (user, password) = try credentialsProvider.retrieve(host: baseURL.host!) {
|
if let (user, password) = try lookupCredentials(host: baseURL.host!) {
|
||||||
let encodedCredentials = "\(user):\(password)".data(using: .utf8)?.base64EncodedString()
|
let encodedCredentials = "\(user):\(password)".data(using: .utf8)?.base64EncodedString()
|
||||||
headers["Authorization"] = "Basic \(encodedCredentials!)"
|
headers["Authorization"] = "Basic \(encodedCredentials!)"
|
||||||
}
|
}
|
||||||
@@ -356,6 +356,15 @@ class Registry {
|
|||||||
currentAuthToken = try TokenResponse.parse(fromData: bodyData)
|
currentAuthToken = try TokenResponse.parse(fromData: bodyData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func lookupCredentials(host: String) throws -> (String, String)? {
|
||||||
|
for provider in credentialsProviders {
|
||||||
|
if let (user, password) = try provider.retrieve(host: host) {
|
||||||
|
return (user, password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
private func authAwareRequest(request: HTTPClientRequest) async throws -> HTTPClientResponse {
|
private func authAwareRequest(request: HTTPClientRequest) async throws -> HTTPClientResponse {
|
||||||
var request = request
|
var request = request
|
||||||
|
|
||||||
|
|||||||
@@ -84,4 +84,14 @@ struct Darwin: Platform {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pointingDevices() -> [VZPointingDeviceConfiguration] {
|
||||||
|
if #available(macOS 13, *) {
|
||||||
|
// Trackpad is only supported starting with macOS Ventura
|
||||||
|
// macOS Monterey will continue using a USB device == .darwin
|
||||||
|
return [VZMacTrackpadConfiguration(), VZUSBScreenCoordinatePointingDeviceConfiguration()]
|
||||||
|
} else {
|
||||||
|
return [VZUSBScreenCoordinatePointingDeviceConfiguration()]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,4 +30,8 @@ struct Linux: Platform {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pointingDevices() -> [VZPointingDeviceConfiguration] {
|
||||||
|
[VZUSBScreenCoordinatePointingDeviceConfiguration()]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ protocol Platform: Codable {
|
|||||||
func bootLoader(nvramURL: URL) throws -> VZBootLoader
|
func bootLoader(nvramURL: URL) throws -> VZBootLoader
|
||||||
func platform(nvramURL: URL) -> VZPlatformConfiguration
|
func platform(nvramURL: URL) -> VZPlatformConfiguration
|
||||||
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration
|
func graphicsDevice(vmConfig: VMConfig) -> VZGraphicsDeviceConfiguration
|
||||||
|
func pointingDevices() -> [VZPointingDeviceConfiguration]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ struct Root: AsyncParsableCommand {
|
|||||||
do {
|
do {
|
||||||
var command = try parseAsRoot()
|
var command = try parseAsRoot()
|
||||||
|
|
||||||
|
// Run garbage-collection before each command (shouldn't take too long)
|
||||||
|
try Config().gc()
|
||||||
|
|
||||||
if var asyncCommand = command as? AsyncParsableCommand {
|
if var asyncCommand = command as? AsyncParsableCommand {
|
||||||
try await asyncCommand.run()
|
try await asyncCommand.run()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class Softnet {
|
|||||||
init(vmMACAddress: String) throws {
|
init(vmMACAddress: String) throws {
|
||||||
let binaryName = "softnet"
|
let binaryName = "softnet"
|
||||||
|
|
||||||
guard let executableURL = Self.resolveBinaryPath(binaryName) else {
|
guard let executableURL = resolveBinaryPath(binaryName) else {
|
||||||
throw SoftnetError.InitializationFailed(why: "\(binaryName) not found in PATH")
|
throw SoftnetError.InitializationFailed(why: "\(binaryName) not found in PATH")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,23 +43,6 @@ class Softnet {
|
|||||||
process.waitUntilExit()
|
process.waitUntilExit()
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func resolveBinaryPath(_ name: String) -> URL? {
|
|
||||||
guard let path = ProcessInfo.processInfo.environment["PATH"] else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for pathComponent in path.split(separator: ":") {
|
|
||||||
let url = URL(fileURLWithPath: String(pathComponent))
|
|
||||||
.appendingPathComponent(name, isDirectory: false)
|
|
||||||
|
|
||||||
if FileManager.default.fileExists(atPath: url.path) {
|
|
||||||
return url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
private func setSocketBuffers(_ fd: Int32, _ sizeBytes: Int) throws {
|
private func setSocketBuffers(_ fd: Int32, _ sizeBytes: Int) throws {
|
||||||
var option_value = sizeBytes
|
var option_value = sizeBytes
|
||||||
let option_len = socklen_t(MemoryLayout<Int>.size)
|
let option_len = socklen_t(MemoryLayout<Int>.size)
|
||||||
|
|||||||
@@ -5,3 +5,20 @@ extension Collection {
|
|||||||
indices.contains(index) ? self[index] : nil
|
indices.contains(index) ? self[index] : nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveBinaryPath(_ name: String) -> URL? {
|
||||||
|
guard let path = ProcessInfo.processInfo.environment["PATH"] else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for pathComponent in path.split(separator: ":") {
|
||||||
|
let url = URL(fileURLWithPath: String(pathComponent))
|
||||||
|
.appendingPathComponent(name, isDirectory: false)
|
||||||
|
|
||||||
|
if FileManager.default.fileExists(atPath: url.path) {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
+34
-6
@@ -11,7 +11,11 @@ struct DownloadFailed: Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct UnsupportedOSError: Error, CustomStringConvertible {
|
struct UnsupportedOSError: Error, CustomStringConvertible {
|
||||||
private(set) var description: String = "error: Linux VMs are only supported on macOS 13.0 (Ventura) or newer"
|
let description: String
|
||||||
|
|
||||||
|
init(_ what: String, _ plural: String) {
|
||||||
|
description = "error: \(what) \(plural) only supported on hosts running macOS 13.0 (Ventura) or newer"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UnsupportedArchitectureError: Error {
|
struct UnsupportedArchitectureError: Error {
|
||||||
@@ -34,7 +38,8 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
|
|
||||||
init(vmDir: VMDirectory,
|
init(vmDir: VMDirectory,
|
||||||
withSoftnet: Bool = false,
|
withSoftnet: Bool = false,
|
||||||
additionalDiskAttachments: [VZDiskImageStorageDeviceAttachment] = []
|
additionalDiskAttachments: [VZDiskImageStorageDeviceAttachment] = [],
|
||||||
|
directoryShares: [DirectoryShare] = []
|
||||||
) throws {
|
) throws {
|
||||||
name = vmDir.name
|
name = vmDir.name
|
||||||
config = try VMConfig.init(fromURL: vmDir.configURL)
|
config = try VMConfig.init(fromURL: vmDir.configURL)
|
||||||
@@ -50,7 +55,8 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
|
|
||||||
let configuration = try Self.craftConfiguration(diskURL: vmDir.diskURL,
|
let configuration = try Self.craftConfiguration(diskURL: vmDir.diskURL,
|
||||||
nvramURL: vmDir.nvramURL, vmConfig: config,
|
nvramURL: vmDir.nvramURL, vmConfig: config,
|
||||||
softnet: softnet, additionalDiskAttachments: additionalDiskAttachments)
|
softnet: softnet, additionalDiskAttachments: additionalDiskAttachments,
|
||||||
|
directoryShares: directoryShares)
|
||||||
virtualMachine = VZVirtualMachine(configuration: configuration)
|
virtualMachine = VZVirtualMachine(configuration: configuration)
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
@@ -149,7 +155,8 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
|
|
||||||
let configuration = try Self.craftConfiguration(diskURL: vmDir.diskURL, nvramURL: vmDir.nvramURL,
|
let configuration = try Self.craftConfiguration(diskURL: vmDir.diskURL, nvramURL: vmDir.nvramURL,
|
||||||
vmConfig: config, softnet: softnet,
|
vmConfig: config, softnet: softnet,
|
||||||
additionalDiskAttachments: additionalDiskAttachments)
|
additionalDiskAttachments: additionalDiskAttachments,
|
||||||
|
directoryShares: [])
|
||||||
virtualMachine = VZVirtualMachine(configuration: configuration)
|
virtualMachine = VZVirtualMachine(configuration: configuration)
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
@@ -228,7 +235,8 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
nvramURL: URL,
|
nvramURL: URL,
|
||||||
vmConfig: VMConfig,
|
vmConfig: VMConfig,
|
||||||
softnet: Softnet? = nil,
|
softnet: Softnet? = nil,
|
||||||
additionalDiskAttachments: [VZDiskImageStorageDeviceAttachment]
|
additionalDiskAttachments: [VZDiskImageStorageDeviceAttachment],
|
||||||
|
directoryShares: [DirectoryShare]
|
||||||
) throws -> VZVirtualMachineConfiguration {
|
) throws -> VZVirtualMachineConfiguration {
|
||||||
let configuration = VZVirtualMachineConfiguration()
|
let configuration = VZVirtualMachineConfiguration()
|
||||||
|
|
||||||
@@ -256,7 +264,7 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
|
|
||||||
// Keyboard and mouse
|
// Keyboard and mouse
|
||||||
configuration.keyboards = [VZUSBKeyboardConfiguration()]
|
configuration.keyboards = [VZUSBKeyboardConfiguration()]
|
||||||
configuration.pointingDevices = [VZUSBScreenCoordinatePointingDeviceConfiguration()]
|
configuration.pointingDevices = vmConfig.platform.pointingDevices()
|
||||||
|
|
||||||
// Networking
|
// Networking
|
||||||
let vio = VZVirtioNetworkDeviceConfiguration()
|
let vio = VZVirtioNetworkDeviceConfiguration()
|
||||||
@@ -278,6 +286,20 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
// Entropy
|
// Entropy
|
||||||
configuration.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
|
configuration.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
|
||||||
|
|
||||||
|
// Directory share
|
||||||
|
if #available(macOS 13, *) {
|
||||||
|
var directories: [String : VZSharedDirectory] = Dictionary()
|
||||||
|
directoryShares.forEach { directories[$0.name] = VZSharedDirectory(url: $0.path, readOnly: $0.readOnly) }
|
||||||
|
|
||||||
|
let automountTag = VZVirtioFileSystemDeviceConfiguration.macOSGuestAutomountTag
|
||||||
|
let sharingDevice = VZVirtioFileSystemDeviceConfiguration(tag: automountTag)
|
||||||
|
sharingDevice.share = VZMultipleDirectoryShare(directories: directories)
|
||||||
|
|
||||||
|
configuration.directorySharingDevices = [sharingDevice]
|
||||||
|
} else if !directoryShares.isEmpty {
|
||||||
|
throw UnsupportedOSError("directory sharing", "is")
|
||||||
|
}
|
||||||
|
|
||||||
try configuration.validate()
|
try configuration.validate()
|
||||||
|
|
||||||
return configuration
|
return configuration
|
||||||
@@ -298,3 +320,9 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
|
|||||||
sema.signal()
|
sema.signal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DirectoryShare {
|
||||||
|
let name: String
|
||||||
|
let path: URL
|
||||||
|
let readOnly: Bool
|
||||||
|
}
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ struct VMConfig: Codable {
|
|||||||
if #available(macOS 13, *) {
|
if #available(macOS 13, *) {
|
||||||
platform = try Linux(from: decoder)
|
platform = try Linux(from: decoder)
|
||||||
} else {
|
} else {
|
||||||
throw UnsupportedOSError()
|
throw UnsupportedOSError("Linux VMs", "are")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cpuCountMin = try container.decode(Int.self, forKey: .cpuCountMin)
|
cpuCountMin = try container.decode(Int.self, forKey: .cpuCountMin)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ struct VMDirectory: Prunable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static func temporary() throws -> VMDirectory {
|
static func temporary() throws -> VMDirectory {
|
||||||
let tmpDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
let tmpDir = try Config().tartTmpDir.appendingPathComponent(UUID().uuidString)
|
||||||
try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: false)
|
try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: false)
|
||||||
|
|
||||||
return VMDirectory(baseURL: tmpDir)
|
return VMDirectory(baseURL: tmpDir)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class VMStorageLocal {
|
class VMStorageLocal {
|
||||||
let baseURL: URL = Config().tartHomeDir.appendingPathComponent("vms", isDirectory: true)
|
let baseURL: URL = try! Config().tartHomeDir.appendingPathComponent("vms", isDirectory: true)
|
||||||
|
|
||||||
private func vmURL(_ name: String) -> URL {
|
private func vmURL(_ name: String) -> URL {
|
||||||
baseURL.appendingPathComponent(name, isDirectory: true)
|
baseURL.appendingPathComponent(name, isDirectory: true)
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class VMStorageOCI: PrunableStorage {
|
class VMStorageOCI: PrunableStorage {
|
||||||
let baseURL = Config().tartCacheDir.appendingPathComponent("OCIs", isDirectory: true)
|
let baseURL = try! Config().tartCacheDir.appendingPathComponent("OCIs", isDirectory: true)
|
||||||
|
|
||||||
private func vmURL(_ name: RemoteName) -> URL {
|
private func vmURL(_ name: RemoteName) -> URL {
|
||||||
baseURL.appendingRemoteName(name)
|
baseURL.appendingRemoteName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func hostDirectoryURL(_ name: RemoteName) -> URL {
|
||||||
|
baseURL.appendingHost(name)
|
||||||
|
}
|
||||||
|
|
||||||
func exists(_ name: RemoteName) -> Bool {
|
func exists(_ name: RemoteName) -> Bool {
|
||||||
VMDirectory(baseURL: vmURL(name)).initialized
|
VMDirectory(baseURL: vmURL(name)).initialized
|
||||||
}
|
}
|
||||||
@@ -125,17 +129,41 @@ class VMStorageOCI: PrunableStorage {
|
|||||||
let digestName = RemoteName(host: name.host, namespace: name.namespace,
|
let digestName = RemoteName(host: name.host, namespace: name.namespace,
|
||||||
reference: Reference(digest: Digest.hash(manifestData)))
|
reference: Reference(digest: Digest.hash(manifestData)))
|
||||||
|
|
||||||
|
// Ensure that host directory for given RemoteName exists in OCI storage
|
||||||
|
let hostDirectoryURL = hostDirectoryURL(digestName)
|
||||||
|
try FileManager.default.createDirectory(at: hostDirectoryURL, withIntermediateDirectories: true)
|
||||||
|
|
||||||
|
// Acquire a lock on it to prevent concurrent pulls for a single host
|
||||||
|
let lock = try FileLock(lockURL: hostDirectoryURL)
|
||||||
|
|
||||||
|
let sucessfullyLocked = try lock.trylock()
|
||||||
|
if !sucessfullyLocked {
|
||||||
|
print("waiting for lock...")
|
||||||
|
try lock.lock()
|
||||||
|
}
|
||||||
|
defer { try! lock.unlock() }
|
||||||
|
|
||||||
|
if Task.isCancelled {
|
||||||
|
throw CancellationError()
|
||||||
|
}
|
||||||
|
|
||||||
if !exists(digestName) {
|
if !exists(digestName) {
|
||||||
let tmpVMDir = try VMDirectory.temporary()
|
let tmpVMDir = try VMDirectory.temporary()
|
||||||
|
|
||||||
|
// Lock the temporary VM directory to prevent it's garbage collection
|
||||||
|
let tmpVMDirLock = try FileLock(lockURL: tmpVMDir.baseURL)
|
||||||
|
try tmpVMDirLock.lock()
|
||||||
|
|
||||||
// Try to reclaim some cache space if we know the VM size in advance
|
// Try to reclaim some cache space if we know the VM size in advance
|
||||||
if let uncompressedDiskSize = manifest.uncompressedDiskSize() {
|
if let uncompressedDiskSize = manifest.uncompressedDiskSize() {
|
||||||
let requiredCapacityBytes = UInt64(uncompressedDiskSize + 128 * 1024 * 1024)
|
let requiredCapacityBytes = UInt64(uncompressedDiskSize + 128 * 1024 * 1024)
|
||||||
|
|
||||||
let attrs = try tmpVMDir.baseURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
|
let attrs = try Config().tartCacheDir.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityKey])
|
||||||
let availableCapacityBytes = UInt64(attrs.volumeAvailableCapacityForImportantUsage!)
|
let availableCapacityBytes = max(UInt64(attrs.volumeAvailableCapacityForImportantUsage!), UInt64(attrs.volumeAvailableCapacity!))
|
||||||
|
|
||||||
if availableCapacityBytes < requiredCapacityBytes {
|
// There is a suspicious that occasionally capacity is returned as zero which can't be true.
|
||||||
|
// Let's validate to avoid unnecessary pruning.
|
||||||
|
if 0 < availableCapacityBytes && availableCapacityBytes < requiredCapacityBytes {
|
||||||
try Prune.pruneReclaim(reclaimBytes: requiredCapacityBytes - availableCapacityBytes)
|
try Prune.pruneReclaim(reclaimBytes: requiredCapacityBytes - availableCapacityBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,4 +209,8 @@ extension URL {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendingHost(_ name: RemoteName) -> URL {
|
||||||
|
self.appendingPathComponent(name.host, isDirectory: true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import tart
|
||||||
|
|
||||||
|
final class FileLockTests: XCTestCase {
|
||||||
|
func testSimple() throws {
|
||||||
|
// Create a temporary file that will be used as a lock
|
||||||
|
let url = temporaryFile()
|
||||||
|
|
||||||
|
// Make sure this file can be locked and unlocked
|
||||||
|
let lock = try FileLock(lockURL: url)
|
||||||
|
try lock.lock()
|
||||||
|
try lock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDoubleLockResultsInError() throws {
|
||||||
|
// Create a temporary file that will be used as a lock
|
||||||
|
let url = temporaryFile()
|
||||||
|
|
||||||
|
// Create two locks on a same file and ensure one of them fails
|
||||||
|
let firstLock = try FileLock(lockURL: url)
|
||||||
|
try firstLock.lock()
|
||||||
|
|
||||||
|
let secondLock = try! FileLock(lockURL: url)
|
||||||
|
XCTAssertFalse(try secondLock.trylock())
|
||||||
|
}
|
||||||
|
|
||||||
|
private func temporaryFile() -> URL {
|
||||||
|
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
|
||||||
|
|
||||||
|
FileManager.default.createFile(atPath: url.path, contents: nil)
|
||||||
|
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
source = [".build/arm64-apple-macosx/debug/tart"]
|
||||||
|
bundle_id = "com.github.cirruslabs.tart"
|
||||||
|
|
||||||
|
apple_id {
|
||||||
|
username = "hello@cirruslabs.org"
|
||||||
|
password = "@env:AC_PASSWORD"
|
||||||
|
}
|
||||||
|
|
||||||
|
sign {
|
||||||
|
application_identity = "Developer ID Application: Fedor Korotkov"
|
||||||
|
entitlements_file = "Resources/tart.entitlements"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user