Skip to content

Commit 57b825d

Browse files
authored
Merge pull request #4 from Automattic/add/buildkite
Add Buildkite
2 parents 0cfd690 + 0d48c00 commit 57b825d

File tree

5 files changed

+125
-6
lines changed

5 files changed

+125
-6
lines changed

.buildkite/pipeline.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Nodes with values to reuse in the pipeline.
2+
common_params:
3+
plugins: &common_plugins
4+
- &bash_cache automattic/bash-cache#v1.3.2: ~
5+
# Common environment values to use with the `env` key.
6+
env: &common_env
7+
IMAGE_ID: xcode-12.5.1
8+
9+
# This is the default pipeline – it will build and test the app
10+
steps:
11+
#################
12+
# Build and Test
13+
#################
14+
- label: "🧪 Build and Test"
15+
key: "test"
16+
command: |
17+
echo "--- :swift: Building + Testing"
18+
swift test
19+
env: *common_env
20+
plugins: *common_plugins
21+
22+
#################
23+
# Publish the Binary (if we're building a tag)
24+
#################
25+
- label: "⬆️ Publish Binary"
26+
key: "publish"
27+
command: .buildkite/publish.sh
28+
env: *common_env
29+
plugins: *common_plugins
30+
depends_on:
31+
- "test"
32+
if: build.tag != null
33+
agents:
34+
queue: "mac"

.buildkite/publish.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash -eu
2+
3+
# Install the `gh` binary if needed
4+
if ! command -v gh &> /dev/null; then
5+
brew install gh
6+
fi
7+
8+
swift build -c release
9+
BUILDDIR=.build/artifacts/release
10+
mkdir -p $BUILDDIR
11+
12+
cp .build/release/hostmgr $BUILDDIR/hostmgr
13+
cp Sources/hostmgr/resources/com.automattic.hostmgr.sync.plist $BUILDDIR/
14+
cp Sources/hostmgr/resources/com.automattic.hostmgr.git-mirror-sync.plist $BUILDDIR/
15+
cp Sources/hostmgr/resources/com.automattic.hostmgr.git-mirror-server.plist $BUILDDIR/
16+
17+
tar -czf hostmgr.tar.gz -C $BUILDDIR .
18+
mv hostmgr.tar.gz .build/artifacts/hostmgr.tar.gz
19+
20+
CHECKSUM=$(openssl sha256 .build/artifacts/hostmgr.tar.gz | awk '{print $2}')
21+
22+
echo "Build complete: .build/artifacts/hostmgr.tar.gz"
23+
echo " Checksum: $CHECKSUM"
24+
25+
gh auth status
26+
gh release create $BUILDKITE_TAG --title $BUILDKITE_TAG --notes "Checksum: $CHECKSUM" .build/artifacts/hostmgr.tar.gz

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/hostmgr/commands/vm/VMClone.swift

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,51 @@ struct VMCloneCommand: ParsableCommand {
3030
)
3131
var wait: Bool = false
3232

33+
@Flag(
34+
help: "Don't customize the VM's CPU and memory for the physical host – retain its existing settings"
35+
)
36+
var skipHostCustomization: Bool = false
37+
38+
@Option(
39+
help: "Hypervisor Type"
40+
)
41+
var hypervisorType: StoppedVM.HypervisorType = .apple
42+
43+
@Option(
44+
help: "Networking Type"
45+
)
46+
var networkingType: StoppedVM.NetworkType = .bridged
47+
3348
func run() throws {
3449

3550
let startDate = Date()
3651

3752
try cloneVM(vm: source, wait: wait || start, startDate: startDate)
3853

39-
if start {
40-
guard let newVM = try Parallels().lookupVM(named: destination)?.asStoppedVM() else {
41-
print("Error finding cloned VM")
42-
VMCloneCommand.exit()
43-
}
54+
guard let newVM = try Parallels().lookupVM(named: destination)?.asStoppedVM() else {
55+
print("Error finding cloned VM")
56+
VMCloneCommand.exit()
57+
}
58+
59+
if !skipHostCustomization {
60+
61+
let totalSystemMemory = Int(ProcessInfo().physicalMemory / (1024 * 1024)) // In MB
62+
let vmAvailableMemory = totalSystemMemory - 4096 // Always leave 4GB available to the VM host – the VM can have the rest
63+
64+
logger.debug("Total System Memory: \(totalSystemMemory) MB")
65+
logger.debug("Allocating \(vmAvailableMemory) MB to VM")
66+
67+
let cpuCoreCount = ProcessInfo().physicalProcessorCount
68+
logger.debug("Allocating \(cpuCoreCount) cores to VM")
4469

70+
try newVM.set(.cpuCount(cpuCoreCount))
71+
try newVM.set(.memorySize(vmAvailableMemory))
72+
}
73+
74+
try newVM.set(.hypervisorType(hypervisorType))
75+
try newVM.set(.networkType(networkingType))
76+
77+
if start {
4578
try startVM(vm: newVM, wait: wait, startDate: startDate)
4679
}
4780
}
@@ -76,3 +109,6 @@ struct VMCloneCommand: ParsableCommand {
76109
print(String(format: "VM cloned and booted in %.2f seconds", Date().timeIntervalSince(startDate)))
77110
}
78111
}
112+
113+
extension StoppedVM.NetworkType: ExpressibleByArgument {}
114+
extension StoppedVM.HypervisorType: ExpressibleByArgument {}

Sources/hostmgr/helpers/Foundation.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ extension String {
4343
return NSString(string: self).expandingTildeInPath
4444
}
4545
}
46+
47+
extension ProcessInfo {
48+
var physicalProcessorCount: Int {
49+
let output = Pipe()
50+
51+
do {
52+
let task = Process()
53+
task.launchPath = "/usr/sbin/sysctl"
54+
task.arguments = ["-n", "hw.physicalcpu"]
55+
task.standardOutput = output
56+
try task.run()
57+
58+
let cpuCountData = output.fileHandleForReading.readDataToEndOfFile()
59+
let cpuCountString = String(data: cpuCountData, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
60+
61+
return Int(cpuCountString) ?? self.processorCount
62+
}
63+
catch _ {
64+
// Fall back to returning the count including SMT cores
65+
return self.processorCount
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)