HoneyComb

HoneyComb is a web interface for managing FreeBSD jails and Bhyve VMs.
Log | Files | Refs | README | LICENSE

main.go (2640B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"html/template"
      6 	"net/http"
      7 	"os/exec"
      8 )
      9 
     10 type naming func(num int) string
     11 
     12 func get_name(n int) (jail string) {
     13 	j_names := [2]string{"ca", "test-jail"}
     14 	jail = j_names[n]
     15 	return
     16 }
     17 
     18 func helloHandler(w http.ResponseWriter, r *http.Request) {
     19 	tmpl := template.Must(template.ParseFiles("static/hello.html"))
     20 	tmpl.Execute(w, map[string]string{"Name": "HTMX + Go"})
     21 }
     22 
     23 func j_status(w http.ResponseWriter, r *http.Request) {
     24 	tmpl := template.Must(template.ParseFiles("static/cmd.html"))
     25 	cmd := exec.Command("jls");
     26 	output, err := cmd.Output()
     27 	if err != nil {
     28 		fmt.Println("Error:", err)
     29 		return
     30 	}
     31 	out := string(output)
     32 	tmpl.Execute(w, map[string]string{"Output": out})
     33 }
     34 
     35 func j_start(w http.ResponseWriter, r *http.Request, j_name string) {
     36 	tmpl := template.Must(template.ParseFiles("static/cmd.html"))
     37 	cmd := exec.Command("doas", "service", "jail", "onestart", j_name);
     38 	output, err := cmd.Output()
     39 	if err != nil {
     40 		fmt.Println("Error:", err)
     41 		return
     42 	}
     43 	out := string(output)
     44 	tmpl.Execute(w, map[string]string{"Output": out})
     45 }
     46 
     47 func j_stop(w http.ResponseWriter, r *http.Request, j_name string) {
     48 	tmpl := template.Must(template.ParseFiles("static/cmd.html"))
     49 	cmd := exec.Command("doas", "service", "jail", "onestop", j_name);
     50 	output, err := cmd.Output()
     51 	if err != nil {
     52 		fmt.Println("Error:", err)
     53 		return
     54 	}
     55 	out := string(output)
     56 	tmpl.Execute(w, map[string]string{"Output": out})
     57 }
     58 
     59 /* Example function to be able to pass arguments through 	*/
     60 /* The web server handler									*/
     61 func handler(w http.ResponseWriter, r *http.Request, random string) {
     62 	tmpl := template.Must(template.ParseFiles("static/hello.html"))
     63 	tmpl.Execute(w, map[string]string{"Name": random})
     64 }
     65 
     66 /* This function handles rendering the buttons for the 	*
     67 *  various jails on the system, will be extented for	*
     68 *  VMs as well.											*/
     69 func render_ui(w http.ResponseWriter, r *http.Request, j_name string) {
     70 	tmpl := template.Must(template.ParseFiles("static/buttons.html"))
     71 	tmpl.Execute(w, map[string]string{"j_name": j_name})
     72 }
     73 
     74 func main() {
     75 
     76 	test := "Tyler"
     77 	jail_name := "ca"
     78 
     79 	http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
     80 		handler(w, r, test)
     81 	})
     82 
     83 	/* Start Jails */
     84 	http.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
     85 		j_start(w, r, jail_name)
     86 	})
     87 
     88 	/* Stop Jails */
     89 	http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
     90 		j_stop(w, r, jail_name)
     91 	})
     92 
     93 	http.Handle("/", http.FileServer(http.Dir("./static")))
     94 	http.HandleFunc("/status", j_status)
     95 	http.HandleFunc("/hello", helloHandler)
     96 	http.ListenAndServe(":8080", nil)
     97 }