Skip to content

Commit

Permalink
feat: add launchd support for macOS
Browse files Browse the repository at this point in the history
See #18
  • Loading branch information
phm07 committed Jul 3, 2024
1 parent 3e82d9b commit e0b3fc1
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -ue -o pipefail

run_launchd() {
FILEPATH="$HOME/Library/LaunchAgents/com.hetzner.cloud.k8sdev.registry-port-forward.plist"
launchctl unload "$FILEPATH" 2> /dev/null || true
rm -f "$FILEPATH"
}

if which launchctl > /dev/null; then
run_launchd
fi
6 changes: 6 additions & 0 deletions example/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ up: .terraform env.auto.tfvars

down: .terraform env.auto.tfvars
tofu destroy -auto-approve
bash files/cleanup.sh

port-forward:
source files/env.sh
Expand Down
6 changes: 6 additions & 0 deletions main-setup.tf
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ resource "local_file" "registry_port_forward" {
file_permission = "0755"
}

resource "local_file" "cleanup" {
source = "${path.module}/cleanup.sh"
filename = "${path.root}/files/cleanup.sh"
file_permission = "0755"
}

resource "local_file" "env" {
content = <<-EOT
#!/usr/bin/env bash
Expand Down
50 changes: 48 additions & 2 deletions registry-port-forward.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ue -o pipefail

run() {
run_systemd() {
unit="k8s-registry-port-forward.service"
description="Port Forward for Container Registry of k8s dev environment"

Expand All @@ -17,4 +17,50 @@ run() {
kubectl port-forward -n kube-system svc/docker-registry 30666:5000
}

run
run_launchd() {
FILEPATH="$HOME/Library/LaunchAgents/com.hetzner.cloud.k8sdev.registry-port-forward.plist"

cat > "$FILEPATH" <<EOF
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.hetzner.cloud.k8sdev.portforward.plist</string>
<key>Program</key>
<string>$(which kubectl)</string>
<key>ProgramArguments</key>
<array>
<string>kubectl</string>
<string>port-forward</string>
<string>-n</string>
<string>kube-system</string>
<string>svc/docker-registry</string>
<string>30666:5000</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>KUBECONFIG</key>
<string>$KUBECONFIG</string>
</dict>
<key>WorkingDirectory</key>
<string>$PWD</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
EOF

launchctl load "$FILEPATH"
}

if which systemctl > /dev/null; then
run_systemd
elif which launchctl > /dev/null; then
run_launchd
else
echo "No supported init system found"
exit 1
fi

0 comments on commit e0b3fc1

Please sign in to comment.