Skip to content

Instantly share code, notes, and snippets.

@Harwood
Created October 13, 2023 15:35
Show Gist options
  • Save Harwood/af8723950708b9b28ffd70a23317cdc3 to your computer and use it in GitHub Desktop.
Save Harwood/af8723950708b9b28ffd70a23317cdc3 to your computer and use it in GitHub Desktop.
import Foundation
enum DockerComposeCommand {
case down
case up
}
enum TailscaleServeType {
case https
case tlsTerminatedTcp
}
let tailscalePortType: [Int:TailscaleServeType] = [
443:.tlsTerminatedTcp, // Homepage
5000:.https, // Kavita
9000:.https, // Komga
10000:.https, // Audiobookshelf
32400:.tlsTerminatedTcp, // Plex
]
let tailscalePorts: [Int:Int] = [
443:3000, // Homepage
5000:5000, // Kavita
9000:8080, // Komga
10000:10000, // Audiobookshelf
32400:32400, // Plex
]
func dockerCompose(command: DockerComposeCommand,
composeFile: String = "./docker-compose.yml") {
let dockerCompose = Process()
dockerCompose.executableURL = URL(fileURLWithPath: "/usr/local/bin/docker-compose")
switch command {
case .down:
dockerCompose.arguments = [
"down",
]
case .up:
fallthrough
default:
dockerCompose.arguments = [
"up",
"--detach",
// "--scale",
// "plex=0",
]
}
do {
try dockerCompose.run()
dockerCompose.waitUntilExit()
} catch {
print("Docker Compose failed \(error)")
}
}
func main(_ arguments: [String],
tailscalePorts: [Int:Int]) {
let command = arguments.count > 1
? arguments[1]
: "start"
switch command {
case "stop":
stop(tailscalePorts: tailscalePorts)
case "start":
fallthrough
default:
start(tailscalePorts: tailscalePorts)
}
}
func start(tailscalePorts: [Int:Int]) {
dockerCompose(command: .up)
tailscaleServe(ports: tailscalePorts)
}
func stop(tailscalePorts: [Int:Int]) {
tailscaleServe(ports: tailscalePorts, off: true)
dockerCompose(command: .down)
}
func tailscaleServe(ports: [Int:Int], off: Bool = false) {
ports.forEach {
do {
let tailscale = Process()
tailscale.executableURL = URL(fileURLWithPath: "/opt/homebrew/bin/tailscale")
tailscale.arguments = [
"--socket",
"/var/run/tailscaled.socket",
"serve",
// "status",
]
switch tailscalePortType[$0.key] {
case .https:
tailscale.arguments?.append("https:\($0.key)")
tailscale.arguments?.append("/")
tailscale.arguments?.append("http://127.0.0.1:\($0.value)")
case .tlsTerminatedTcp:
fallthrough
default:
tailscale.arguments?.append("tls-terminated-tcp:\($0.key)")
tailscale.arguments?.append("tcp://127.0.0.1:\($0.value)")
}
if off {
tailscale.arguments?.append("off")
}
// print(tailscale)
try tailscale.run()
tailscale.waitUntilExit()
} catch {
print("Tailscale serve \( off ? "off" : "on" )failed: Port \($0)")
}
}
}
main(CommandLine.arguments,
tailscalePorts: tailscalePorts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment