From 1b3f6a00d6d680fa1f418814d2106f62a13617d0 Mon Sep 17 00:00:00 2001
From: Recolic <git@me.recolic.net>
Date: Mon, 15 Jan 2024 17:23:02 -0800
Subject: [PATCH] .WIP

---
 README.md                                     |   2 +-
 {src => archived}/daemon-test.sh              |   0
 .../http-root}/base64zip.js                   |   0
 {http-root => archived/http-root}/index.html  |   0
 {http-root => archived/http-root}/pako.js     |   0
 .../http-root}/res/bulma.min.css              |   0
 .../http-root}/res/clipboard.min.js           |   0
 .../http-root}/res/clippy.svg                 |   0
 .../http-root}/res/jquery.min.js              |   0
 .../http-root}/res/sha256.min.js              |   0
 .../http-root}/res/sheet/css/main.css         |   0
 .../http-root}/res/sheet/css/util.css         |   0
 .../sheet/fonts/OpenSans/OpenSans-Regular.ttf | Bin
 .../css/font-awesome.min.css                  |   0
 .../http-root}/res/sheet/js/main.js           |   0
 .../res/sheet/vendor/animate/animate.css      |   0
 .../vendor/bootstrap/css/bootstrap.min.css    |   0
 .../vendor/bootstrap/js/bootstrap.min.js      |   0
 .../res/sheet/vendor/bootstrap/js/popper.js   |   0
 .../sheet/vendor/jquery/jquery-3.2.1.min.js   |   0
 .../perfect-scrollbar/perfect-scrollbar.css   |   0
 .../res/sheet/vendor/select2/select2.min.css  |   0
 .../res/sheet/vendor/select2/select2.min.js   |   0
 {http-root => archived/http-root}/strftime.js |   0
 {http-root => archived/http-root}/template.js |   0
 requirements.log => archived/requirements.log |   0
 src/cron-callback.sh                          |  80 ++++++++++++++++++
 src/init.settings                             |   4 +
 src/runtime.settings                          |   4 +
 src/vm.list                                   |   5 --
 30 files changed, 89 insertions(+), 6 deletions(-)
 rename {src => archived}/daemon-test.sh (100%)
 rename {http-root => archived/http-root}/base64zip.js (100%)
 rename {http-root => archived/http-root}/index.html (100%)
 rename {http-root => archived/http-root}/pako.js (100%)
 rename {http-root => archived/http-root}/res/bulma.min.css (100%)
 rename {http-root => archived/http-root}/res/clipboard.min.js (100%)
 rename {http-root => archived/http-root}/res/clippy.svg (100%)
 rename {http-root => archived/http-root}/res/jquery.min.js (100%)
 rename {http-root => archived/http-root}/res/sha256.min.js (100%)
 rename {http-root => archived/http-root}/res/sheet/css/main.css (100%)
 rename {http-root => archived/http-root}/res/sheet/css/util.css (100%)
 rename {http-root => archived/http-root}/res/sheet/fonts/OpenSans/OpenSans-Regular.ttf (100%)
 rename {http-root => archived/http-root}/res/sheet/fonts/font-awesome-4.7.0/css/font-awesome.min.css (100%)
 rename {http-root => archived/http-root}/res/sheet/js/main.js (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/animate/animate.css (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/bootstrap/css/bootstrap.min.css (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/bootstrap/js/bootstrap.min.js (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/bootstrap/js/popper.js (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/jquery/jquery-3.2.1.min.js (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/perfect-scrollbar/perfect-scrollbar.css (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/select2/select2.min.css (100%)
 rename {http-root => archived/http-root}/res/sheet/vendor/select2/select2.min.js (100%)
 rename {http-root => archived/http-root}/strftime.js (100%)
 rename {http-root => archived/http-root}/template.js (100%)
 rename requirements.log => archived/requirements.log (100%)
 create mode 100644 src/cron-callback.sh
 create mode 100644 src/init.settings
 create mode 100644 src/runtime.settings
 delete mode 100644 src/vm.list

diff --git a/README.md b/README.md
index eda0c4a..2701981 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # simple-vps-provider
 
-Turn your home server to DigitalOcean / Linode / Vultr with a simple script.
+Turn your server/desktop to DigitalOcean / Linode / Vultr with a super-simple script.
 
 No complex configuration, no fancy dependency, no management cost. This project is designed for single user home server. It allows you to easily get a server for testing and dev.
 
diff --git a/src/daemon-test.sh b/archived/daemon-test.sh
similarity index 100%
rename from src/daemon-test.sh
rename to archived/daemon-test.sh
diff --git a/http-root/base64zip.js b/archived/http-root/base64zip.js
similarity index 100%
rename from http-root/base64zip.js
rename to archived/http-root/base64zip.js
diff --git a/http-root/index.html b/archived/http-root/index.html
similarity index 100%
rename from http-root/index.html
rename to archived/http-root/index.html
diff --git a/http-root/pako.js b/archived/http-root/pako.js
similarity index 100%
rename from http-root/pako.js
rename to archived/http-root/pako.js
diff --git a/http-root/res/bulma.min.css b/archived/http-root/res/bulma.min.css
similarity index 100%
rename from http-root/res/bulma.min.css
rename to archived/http-root/res/bulma.min.css
diff --git a/http-root/res/clipboard.min.js b/archived/http-root/res/clipboard.min.js
similarity index 100%
rename from http-root/res/clipboard.min.js
rename to archived/http-root/res/clipboard.min.js
diff --git a/http-root/res/clippy.svg b/archived/http-root/res/clippy.svg
similarity index 100%
rename from http-root/res/clippy.svg
rename to archived/http-root/res/clippy.svg
diff --git a/http-root/res/jquery.min.js b/archived/http-root/res/jquery.min.js
similarity index 100%
rename from http-root/res/jquery.min.js
rename to archived/http-root/res/jquery.min.js
diff --git a/http-root/res/sha256.min.js b/archived/http-root/res/sha256.min.js
similarity index 100%
rename from http-root/res/sha256.min.js
rename to archived/http-root/res/sha256.min.js
diff --git a/http-root/res/sheet/css/main.css b/archived/http-root/res/sheet/css/main.css
similarity index 100%
rename from http-root/res/sheet/css/main.css
rename to archived/http-root/res/sheet/css/main.css
diff --git a/http-root/res/sheet/css/util.css b/archived/http-root/res/sheet/css/util.css
similarity index 100%
rename from http-root/res/sheet/css/util.css
rename to archived/http-root/res/sheet/css/util.css
diff --git a/http-root/res/sheet/fonts/OpenSans/OpenSans-Regular.ttf b/archived/http-root/res/sheet/fonts/OpenSans/OpenSans-Regular.ttf
similarity index 100%
rename from http-root/res/sheet/fonts/OpenSans/OpenSans-Regular.ttf
rename to archived/http-root/res/sheet/fonts/OpenSans/OpenSans-Regular.ttf
diff --git a/http-root/res/sheet/fonts/font-awesome-4.7.0/css/font-awesome.min.css b/archived/http-root/res/sheet/fonts/font-awesome-4.7.0/css/font-awesome.min.css
similarity index 100%
rename from http-root/res/sheet/fonts/font-awesome-4.7.0/css/font-awesome.min.css
rename to archived/http-root/res/sheet/fonts/font-awesome-4.7.0/css/font-awesome.min.css
diff --git a/http-root/res/sheet/js/main.js b/archived/http-root/res/sheet/js/main.js
similarity index 100%
rename from http-root/res/sheet/js/main.js
rename to archived/http-root/res/sheet/js/main.js
diff --git a/http-root/res/sheet/vendor/animate/animate.css b/archived/http-root/res/sheet/vendor/animate/animate.css
similarity index 100%
rename from http-root/res/sheet/vendor/animate/animate.css
rename to archived/http-root/res/sheet/vendor/animate/animate.css
diff --git a/http-root/res/sheet/vendor/bootstrap/css/bootstrap.min.css b/archived/http-root/res/sheet/vendor/bootstrap/css/bootstrap.min.css
similarity index 100%
rename from http-root/res/sheet/vendor/bootstrap/css/bootstrap.min.css
rename to archived/http-root/res/sheet/vendor/bootstrap/css/bootstrap.min.css
diff --git a/http-root/res/sheet/vendor/bootstrap/js/bootstrap.min.js b/archived/http-root/res/sheet/vendor/bootstrap/js/bootstrap.min.js
similarity index 100%
rename from http-root/res/sheet/vendor/bootstrap/js/bootstrap.min.js
rename to archived/http-root/res/sheet/vendor/bootstrap/js/bootstrap.min.js
diff --git a/http-root/res/sheet/vendor/bootstrap/js/popper.js b/archived/http-root/res/sheet/vendor/bootstrap/js/popper.js
similarity index 100%
rename from http-root/res/sheet/vendor/bootstrap/js/popper.js
rename to archived/http-root/res/sheet/vendor/bootstrap/js/popper.js
diff --git a/http-root/res/sheet/vendor/jquery/jquery-3.2.1.min.js b/archived/http-root/res/sheet/vendor/jquery/jquery-3.2.1.min.js
similarity index 100%
rename from http-root/res/sheet/vendor/jquery/jquery-3.2.1.min.js
rename to archived/http-root/res/sheet/vendor/jquery/jquery-3.2.1.min.js
diff --git a/http-root/res/sheet/vendor/perfect-scrollbar/perfect-scrollbar.css b/archived/http-root/res/sheet/vendor/perfect-scrollbar/perfect-scrollbar.css
similarity index 100%
rename from http-root/res/sheet/vendor/perfect-scrollbar/perfect-scrollbar.css
rename to archived/http-root/res/sheet/vendor/perfect-scrollbar/perfect-scrollbar.css
diff --git a/http-root/res/sheet/vendor/select2/select2.min.css b/archived/http-root/res/sheet/vendor/select2/select2.min.css
similarity index 100%
rename from http-root/res/sheet/vendor/select2/select2.min.css
rename to archived/http-root/res/sheet/vendor/select2/select2.min.css
diff --git a/http-root/res/sheet/vendor/select2/select2.min.js b/archived/http-root/res/sheet/vendor/select2/select2.min.js
similarity index 100%
rename from http-root/res/sheet/vendor/select2/select2.min.js
rename to archived/http-root/res/sheet/vendor/select2/select2.min.js
diff --git a/http-root/strftime.js b/archived/http-root/strftime.js
similarity index 100%
rename from http-root/strftime.js
rename to archived/http-root/strftime.js
diff --git a/http-root/template.js b/archived/http-root/template.js
similarity index 100%
rename from http-root/template.js
rename to archived/http-root/template.js
diff --git a/requirements.log b/archived/requirements.log
similarity index 100%
rename from requirements.log
rename to archived/requirements.log
diff --git a/src/cron-callback.sh b/src/cron-callback.sh
new file mode 100644
index 0000000..dd5e441
--- /dev/null
+++ b/src/cron-callback.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+# pacman -S cdrkit qemu-base
+
+workdir=./data
+mkdir -p "$workdir"
+cd "$workdir"
+mkdir -p base vm tmp
+
+function generate_metadata () {
+    local name=$1
+    echo "local-hostname: $name"
+}
+function generate_userdata () {
+    local username=$1
+    local password=$2
+    local name=$3
+    # TODO: allow public key?
+    echo "#cloud-config
+system_info:
+  default_user:
+    name: $username
+    home: /home/$username
+
+password: $password
+chpasswd: { expire: False }
+hostname: $name
+
+# configure sshd to allow users logging in using password 
+# rather than just keys
+ssh_pwauth: True
+"
+}
+
+function download_cloud_img_if_not_exist () {
+    local cloudimg="$1"
+    [[ "$1" != "focal-server-cloudimg-amd64.img" ]] && echo "ERROR: cloudimg not supported"
+    [[ -f "base/$cloudimg" ]] || aria2c -o "base/$cloudimg" https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img || ! echo "Failed to download ubuntu cloudimg" || return $?
+}
+
+function create_vm_if_not_exist () {
+    local name=$1
+    local cores=$2
+    local ram=$3
+    local disk=$4
+    local cloudimg=$5
+    local username=$6
+    local password=$7
+    local vnc=$8
+    local ports="$9"
+
+    if [[ -f "vm/$name/disk.img" ]]; then
+        # already exists
+        :
+    else
+        download_cloud_img_if_not_exist "$cloudimg" || return $?
+        rm -rf "vm/$name" ; mkdir -p "vm/$name"
+        # create it
+        generate_metadata "$name" > "vm/$name/meta-data" || return $?
+        generate_userdata "$username" "$password" "$name" > "vm/$name/user-data" || return $?
+        ( cd "vm/$name" ; genisoimage  -output initimg.iso -volid cidata -joliet -rock user-data meta-data ) || return $?
+        qemu-img create -f qcow2 -F qcow2 -o backing_file="../../base/focal-server-cloudimg-amd64.img" "vm/$name/disk.img" || return $?
+        qemu-img resize "vm/$name/disk.img" "$disk" || return $?
+    fi
+}
+
+function start_vm_if_not_running () {
+    local name=$1
+    local options="$2"
+
+    # start it
+    qemu-system-x86_64 -drive file="vm/$name/disk.img",if=virtio -cdrom "vm/$name/initimg.iso" -cpu host --enable-kvm -bios /usr/share/edk2-ovmf/x64/OVMF.fd -net nic,model=rtl8139 &
+    pid=$!
+    echo PID=$pid
+    # TODO
+}
+
+[[ $2 = "" ]] && echo "Temp script to create VM. Usage: $0 MY_GOOD_VM11 :11" && exit 1
+create_vm_from "$1" 2 4G 50G __hardcoded__ r 1 "$2" __hardcoded__ || exit $?
+echo "DEBUG: sshpass -p 1 ssh -p 30472 r@localhost"
+
diff --git a/src/init.settings b/src/init.settings
new file mode 100644
index 0000000..be9b8fb
--- /dev/null
+++ b/src/init.settings
@@ -0,0 +1,4 @@
+# name;cloudimg;disk;username;password
+instance1;focal-server-cloudimg-amd64.img;60G;r;1
+gitlab-ci;focal-server-cloudimg-amd64.img;60G;r;1
+httptest ;focal-server-cloudimg-amd64.img;60G;r;1
diff --git a/src/runtime.settings b/src/runtime.settings
new file mode 100644
index 0000000..fca2de2
--- /dev/null
+++ b/src/runtime.settings
@@ -0,0 +1,4 @@
+# name;options
+instance1;focal-server-cloudimg-amd64.img;60G;-m 2G -smp 2 -vnc :11 -net user,hostfwd=tcp::30472-:22
+gitlab-ci;focal-server-cloudimg-amd64.img;40G;-m 4G -smp 4 -vnc :12 -net user,hostfwd=tcp::30473-:22
+httptest ;focal-server-cloudimg-amd64.img;20G;-m 1G -smp 1 -vnc :13 -net user,hostfwd=tcp::30474-:22
diff --git a/src/vm.list b/src/vm.list
deleted file mode 100644
index a570254..0000000
--- a/src/vm.list
+++ /dev/null
@@ -1,5 +0,0 @@
-# hostname,cores,mem,disk,base-img,username,password,ports
-
-instance1,4,8G,60G,focal-server-cloudimg-amd64.img,r,1,22
-gitlab-ci,2,2G,40G,focal-server-cloudimg-amd64.img,r,1,22
-httptest,1,1G,20G,focal-server-cloudimg-amd64.img,r,1,22 80 443
-- 
GitLab