add example for deploying vm with caddy
This commit is contained in:
17
examples/threefold/tfgrid3deployer/vm_gw_caddy/Caddyfile
Normal file
17
examples/threefold/tfgrid3deployer/vm_gw_caddy/Caddyfile
Normal file
@@ -0,0 +1,17 @@
|
||||
:80 { # Set this path to your site's directory.
|
||||
root \* /usr/share/caddy
|
||||
|
||||
# Enable the static file server.
|
||||
file_server
|
||||
|
||||
# Another common task is to set up a reverse proxy:
|
||||
# reverse_proxy localhost:8080
|
||||
|
||||
# Or serve a PHP site through php-fpm:
|
||||
# php_fastcgi localhost:9000
|
||||
|
||||
}
|
||||
|
||||
# Refer to the Caddy docs for more information:
|
||||
|
||||
# https://caddyserver.com/docs/caddyfile
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Exit immediately if a command exits with a non-zero status
|
||||
set -e
|
||||
|
||||
# Check if the script is run as root
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "Please run this script as root or using sudo."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Updating package lists..."
|
||||
apt update -y
|
||||
|
||||
echo "Installing prerequisites..."
|
||||
apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
|
||||
|
||||
echo "Adding Caddy repository..."
|
||||
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | apt-key add -
|
||||
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
|
||||
|
||||
echo "Updating package lists for Caddy..."
|
||||
apt update -y
|
||||
|
||||
echo "Installing Caddy..."
|
||||
apt install -y caddy
|
||||
|
||||
|
||||
echo "exec: caddy run --config /etc/caddy/Caddyfile" > /etc/zinit/caddy.yaml
|
||||
zinit monitor caddy
|
||||
73
examples/threefold/tfgrid3deployer/vm_gw_caddy/vm_gw_caddy.vsh
Executable file
73
examples/threefold/tfgrid3deployer/vm_gw_caddy/vm_gw_caddy.vsh
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env -S v -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals -cg run
|
||||
|
||||
import freeflowuniverse.herolib.threefold.gridproxy
|
||||
import freeflowuniverse.herolib.threefold.tfgrid3deployer
|
||||
import freeflowuniverse.herolib.installers.threefold.griddriver
|
||||
import os
|
||||
import time
|
||||
|
||||
mut installer := griddriver.get()!
|
||||
installer.install()!
|
||||
|
||||
v := tfgrid3deployer.get()!
|
||||
println('cred: ${v}')
|
||||
deployment_name := 'vm_gw_caddy'
|
||||
|
||||
mut deployment := tfgrid3deployer.new_deployment(deployment_name)!
|
||||
deployment.add_machine(
|
||||
name: 'vm_caddy'
|
||||
cpu: 1
|
||||
memory: 2
|
||||
planetary: true
|
||||
public_ip4: true
|
||||
size: 10 // 10 gig
|
||||
mycelium: tfgrid3deployer.Mycelium{}
|
||||
)
|
||||
deployment.deploy()!
|
||||
|
||||
vm1 := deployment.vm_get('vm_caddy')!
|
||||
println('vm1 info: ${vm1}')
|
||||
|
||||
vm1_public_ip4 := vm1.public_ip4.all_before('/')
|
||||
deployment.add_webname(name: 'mywebname', backend: 'http://${vm1_public_ip4}:80')
|
||||
deployment.deploy()!
|
||||
|
||||
// Retry logic to wait for the SSH server to be up
|
||||
max_retries := 10
|
||||
mut retries := 0
|
||||
mut is_ssh_up := false
|
||||
|
||||
for {
|
||||
if retries < max_retries {
|
||||
// Try to SSH into the machine
|
||||
ssh_check_cmd := 'ssh -o "StrictHostKeyChecking no" root@${vm1_public_ip4} -o ConnectTimeout=10 echo "SSH server is up"'
|
||||
ssh_check_res := os.execute(ssh_check_cmd)
|
||||
|
||||
if ssh_check_res.exit_code == 0 {
|
||||
is_ssh_up = true
|
||||
break
|
||||
}
|
||||
retries++
|
||||
println('SSH server not up, retrying in 5 seconds... (Attempt ${retries}/${max_retries})')
|
||||
time.sleep(time.second * 5)
|
||||
}
|
||||
}
|
||||
|
||||
if !is_ssh_up {
|
||||
panic('Failed to connect to the SSH server after ${max_retries} attempts.')
|
||||
}
|
||||
|
||||
cp_cmd := 'scp -o "UserKnownHostsFile=/dev/null" ${os.dir(@FILE)}/install_caddy.sh root@${vm1_public_ip4}:~ && mkdir -p /etc/caddy && scp -o "UserKnownHostsFile=/dev/null" ${os.dir(@FILE)}/Caddyfile root@${vm1_public_ip4}:/etc/caddy/'
|
||||
res1 := os.execute(cp_cmd)
|
||||
if res1.exit_code != 0 {
|
||||
panic('failed to copy files: ${res1.output}')
|
||||
}
|
||||
|
||||
cmd := 'ssh root@${vm1_public_ip4} -t "chmod +x ~/install_caddy.sh && ~/setup.sh" -o "UserKnownHostsFile=/dev/null"'
|
||||
res := os.execute(cmd)
|
||||
if res.exit_code != 0 {
|
||||
panic('failed to install and run caddy: ${res.output}')
|
||||
}
|
||||
|
||||
println('To access the machine, run the following command:')
|
||||
println('ssh root@${vm1_public_ip4}')
|
||||
Reference in New Issue
Block a user