From 00475c4831376d355e82d107e902bcea163f9928 Mon Sep 17 00:00:00 2001
From: root <root@myblin.my.domain>
Date: Tue, 14 Jan 2025 04:48:13 -0500
Subject: [PATCH 1/5] add systemd unit

---
 .gitignore                     |  6 +++---
 Makefile                       | 23 ++++++++++++++++++-----
 main.go                        |  3 +--
 rc.d/cbsd-mq-router            |  4 ++--
 systemd/README.md              |  3 +++
 systemd/cbsd-mq-router.service | 22 ++++++++++++++++++++++
 6 files changed, 49 insertions(+), 12 deletions(-)
 create mode 100644 systemd/README.md
 create mode 100644 systemd/cbsd-mq-router.service

diff --git a/.gitignore b/.gitignore
index 687ab0d..c7f860c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
 .idea/
-./src
-./cbsd-mq-router
-./log
+src
+cbsd-mq-router
+log
diff --git a/Makefile b/Makefile
index 8b4bb8e..3d6ddd7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,23 @@
+UNAME_S := $(shell uname -s)
+
 all: 
 	@./build.sh
+
 clean:
-	rm -f bs_router
+	rm -f cbsd-mq-router
+	rm -rf src
+
 install: all
-	install bs_router /usr/local/sbin
-	install rc.d/bs_router /usr/local/etc/rc.d/bs_router
-uninstall: 
-	rm -f /usr/local/sbin/bs_router /usr/local/etc/rc.d/bs_router
+	install cbsd-mq-router /usr/local/sbin
+ifeq ($(UNAME_S),Linux)
+	install systemd/cbsd-mq-router.service /lib/systemd/system/cbsd-mq-router.service
+else
+	install rc.d/cbsd-mq-router /usr/local/etc/rc.d/cbsd-mq-router
+endif
 
+uninstall:
+ifeq ($(UNAME_S),Linux)
+	rm -f /usr/local/sbin/cbsd-mq-router /lib/systemd/system/cbsd-mq-router.service
+else
+	rm -f /usr/local/sbin/cbsd-mq-router /usr/local/etc/rc.d/cbsd-mq-router
+endif
diff --git a/main.go b/main.go
index a58c72d..e318381 100644
--- a/main.go
+++ b/main.go
@@ -1,5 +1,4 @@
-// CBSD Project 2013-2021
-// CBSD Team <cbsd+subscribe@lists.tilda.center>
+// CBSD Project 2013-2025
 // beanstalkd driven CBSD sample
 package main
 
diff --git a/rc.d/cbsd-mq-router b/rc.d/cbsd-mq-router
index 15906d7..0fd3dbb 100755
--- a/rc.d/cbsd-mq-router
+++ b/rc.d/cbsd-mq-router
@@ -1,8 +1,8 @@
 #!/bin/sh
 
 # PROVIDE: cbsd_mq_router
-# REQUIRE: NETWORK
-# BEFORE:  DAEMON
+# REQUIRE: FILESYSTEMS LOGIN NETWORK beanstalkd cbsdd
+# KEYWORD: shutdown
 
 . /etc/rc.subr
 
diff --git a/systemd/README.md b/systemd/README.md
new file mode 100644
index 0000000..2f60c45
--- /dev/null
+++ b/systemd/README.md
@@ -0,0 +1,3 @@
+This is systemd-unit file sample for InitWare ( https://github.com/InitWare/InitWare ) or SystemD
+
+Drop cbsd-mq-router.service as /lib/systemd/system/cbsd-mq-router.service
diff --git a/systemd/cbsd-mq-router.service b/systemd/cbsd-mq-router.service
new file mode 100644
index 0000000..516b3eb
--- /dev/null
+++ b/systemd/cbsd-mq-router.service
@@ -0,0 +1,22 @@
+[Unit]
+Description=CBSD MQ router
+After=network.target network-online.target remote-fs.target nss-lookup.target openvswitch-switch.service
+Wants=network-online.target
+Documentation=https://github.com/cbsd/cbsd-mq-router, man:cbsd(8)
+ConditionPathExists=/etc/cbsd-mq-router.json
+
+[Service]
+Environment=NOINTER=1
+Type=simple
+ExecStart=/usr/local/sbin/cbsd-mq-router -config /etc/cbsd-mq-router.json
+PIDFile=/run/cbsd-mq-router.pid
+Restart=always
+RestartSec=10
+User=nobody
+Group=nogroup
+PrivateTmp=true
+TimeoutStopSec=5
+KillMode=mixed
+
+[Install]
+WantedBy=multi-user.target

From 36c9cc1995404fce0b3be6a64a69ca2bdeb88706 Mon Sep 17 00:00:00 2001
From: root <root@myblin.my.domain>
Date: Thu, 30 Jan 2025 12:20:09 -0500
Subject: [PATCH 2/5] use file instead of /dev/stdout

---
 Makefile | 3 +++
 log.go   | 2 +-
 main.go  | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 3d6ddd7..c4d062d 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,9 @@ install: all
 	install cbsd-mq-router /usr/local/sbin
 ifeq ($(UNAME_S),Linux)
 	install systemd/cbsd-mq-router.service /lib/systemd/system/cbsd-mq-router.service
+	@test -d /var/log/cbsdmq || mkdir -m 0755 /var/log/cbsdmq
+	@chown nobody:nogroup /var/log/cbsdmq
+	@test -r /etc/cbsd-mq-router.json || sed 's:/dev/stdout:/var/log/cbsdmq/stdout.log:g' etc/cbsd-mq-router.json > /etc/cbsd-mq-router.json
 else
 	install rc.d/cbsd-mq-router /usr/local/etc/rc.d/cbsd-mq-router
 endif
diff --git a/log.go b/log.go
index 1512a71..137d9aa 100644
--- a/log.go
+++ b/log.go
@@ -32,7 +32,7 @@ func log_init(logfile string) {
 	f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
 
 	if err != nil {
-		fmt.Printf("config load error: %s", err)
+		fmt.Printf("log_init: config load error: %s", err)
 		os.Exit(1)
 	}
 
diff --git a/main.go b/main.go
index e318381..0bbadce 100644
--- a/main.go
+++ b/main.go
@@ -34,7 +34,7 @@ func main() {
 	config, err := LoadConfiguration(*configFile)
 
 	if err != nil {
-		fmt.Println("config load error")
+		fmt.Println("main: config load error")
 		os.Exit(1)
 	}
 

From c2cf20f5ade327e6106223c745bdf987f6d50dbe Mon Sep 17 00:00:00 2001
From: root <root@myblin.my.domain>
Date: Fri, 31 Jan 2025 10:45:05 -0500
Subject: [PATCH 3/5] unset PATH vars

---
 build.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/build.sh b/build.sh
index fbcebee..1ba8238 100755
--- a/build.sh
+++ b/build.sh
@@ -1,5 +1,4 @@
 #!/bin/sh
-export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
 pgm="${0##*/}"				# Program basename
 progdir="${0%/*}"			# Program directory
 workdir=$( realpath ${progdir} )	# realpath dir
@@ -8,6 +7,7 @@ cd ${workdir}
 # Check go install
 if [ -z "$( which go )" ]; then
 	echo "error: Go is not installed. Please install go: pkg install -y lang/go"
+	echo "PATH: ${PATH}"
 	exit 1
 fi
 

From 9fc8bc7f6633ea59eb48778ee23838ab05596016 Mon Sep 17 00:00:00 2001
From: root <root@myblin.my.domain>
Date: Fri, 31 Jan 2025 10:59:34 -0500
Subject: [PATCH 4/5] systemd unit: fix user

---
 Makefile                       | 4 ++--
 systemd/cbsd-mq-router.service | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index c4d062d..38ab8b0 100644
--- a/Makefile
+++ b/Makefile
@@ -12,8 +12,8 @@ install: all
 ifeq ($(UNAME_S),Linux)
 	install systemd/cbsd-mq-router.service /lib/systemd/system/cbsd-mq-router.service
 	@test -d /var/log/cbsdmq || mkdir -m 0755 /var/log/cbsdmq
-	@chown nobody:nogroup /var/log/cbsdmq
-	@test -r /etc/cbsd-mq-router.json || sed 's:/dev/stdout:/var/log/cbsdmq/stdout.log:g' etc/cbsd-mq-router.json > /etc/cbsd-mq-router.json
+	@test -d /var/log/cbsd_mq_router || mkdir -m 0755 /var/log/cbsd_mq_router
+	@test -r /etc/cbsd-mq-router.json || sed 's:/dev/stdout:/var/log/cbsd_mq_router/cbsd_mq_router.log:g' etc/cbsd-mq-router.json > /etc/cbsd-mq-router.json
 else
 	install rc.d/cbsd-mq-router /usr/local/etc/rc.d/cbsd-mq-router
 endif
diff --git a/systemd/cbsd-mq-router.service b/systemd/cbsd-mq-router.service
index 516b3eb..5c5c59c 100644
--- a/systemd/cbsd-mq-router.service
+++ b/systemd/cbsd-mq-router.service
@@ -12,8 +12,8 @@ ExecStart=/usr/local/sbin/cbsd-mq-router -config /etc/cbsd-mq-router.json
 PIDFile=/run/cbsd-mq-router.pid
 Restart=always
 RestartSec=10
-User=nobody
-Group=nogroup
+User=root
+Group=root
 PrivateTmp=true
 TimeoutStopSec=5
 KillMode=mixed

From 5a93e43597f97a7827fec79610bf9552187ee916 Mon Sep 17 00:00:00 2001
From: root <root@debee.my.domain>
Date: Sun, 2 Feb 2025 13:45:12 -0500
Subject: [PATCH 5/5] bget -> vmget: support for qemu/xen/other

---
 .gitignore           |  1 +
 README.md            | 11 +++++++++++
 bget.go              | 26 --------------------------
 build.sh             |  2 +-
 comment_processor.go | 30 +++++++++++++++---------------
 go.mod               | 13 +++++++++++++
 go.sum               |  8 ++++++++
 main.go              |  1 -
 vmget.go             | 36 ++++++++++++++++++++++++++++++++++++
 9 files changed, 85 insertions(+), 43 deletions(-)
 delete mode 100644 bget.go
 create mode 100644 go.mod
 create mode 100644 go.sum
 create mode 100644 vmget.go

diff --git a/.gitignore b/.gitignore
index c7f860c..2f34221 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
 src
 cbsd-mq-router
 log
+pkg
diff --git a/README.md b/README.md
index fc5279c..84f7975 100644
--- a/README.md
+++ b/README.md
@@ -23,3 +23,14 @@ go build
 pkg update -f
 ```
 
+## update mods
+
+```
+rm -f go.mod go.sum
+```
+
+```
+go mod init cbsd-mq-router
+go mod tidy
+```
+
diff --git a/bget.go b/bget.go
deleted file mode 100644
index f6bb804..0000000
--- a/bget.go
+++ /dev/null
@@ -1,26 +0,0 @@
-package main
-
-import (
-	"fmt"
-	"os/exec"
-	"strings"
-)
-
-// get bhyve properties via cbsd bget command
-func bget(jname string, properties string) string {
-	var result string
-	// todo: rewrite to SQLite3
-	cmdStr := fmt.Sprintf("/usr/local/bin/cbsd bget jname=%s mode=quiet %s", jname, properties)
-	cmdArgs := strings.Fields(cmdStr)
-	cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
-	out, err := cmd.CombinedOutput()
-	if err != nil {
-		Infof("bget cmd.Run() failed (cbsd bget jname=%s mode=quiet %s) with %s\n", jname, properties, err)
-		return ""
-	}
-
-	result=(string(out))
-	fmt.Printf("bget str: [%s]\n", result)
-
-	return result
-}
diff --git a/build.sh b/build.sh
index 1ba8238..b7dc723 100755
--- a/build.sh
+++ b/build.sh
@@ -20,7 +20,7 @@ fi
 
 export GOPATH="${workdir}"
 export GOBIN="${workdir}"
-export GO111MODULE=off
+#export GO111MODULE=off
 
 set -e
 go get
diff --git a/comment_processor.go b/comment_processor.go
index a7c7501..ff76153 100644
--- a/comment_processor.go
+++ b/comment_processor.go
@@ -11,8 +11,9 @@ import (
 	"reflect"
 )
 
-func createKeyValuePairs(m map[string]interface{}) (string,string) {
+func createKeyValuePairs(m map[string]interface{}) (string,string,string) {
 	jname := new(bytes.Buffer)
+	emulator := new(bytes.Buffer)
 	b := new(bytes.Buffer)
 	var err error
 	for key, value := range m {
@@ -21,6 +22,7 @@ func createKeyValuePairs(m map[string]interface{}) (string,string) {
 				_, err = fmt.Fprintf(b, " %s=\"%s\"", key, value)
 				switch key {
 					case "jname": fmt.Fprintf(jname,"%s",value)
+					case "emulator": fmt.Fprintf(emulator,"%s",value)
 //					default: fmt.Printf("PAIRS: %s, %s\n",key,value)
 				}
 			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
@@ -29,23 +31,21 @@ func createKeyValuePairs(m map[string]interface{}) (string,string) {
 				_, err = fmt.Fprintf(b, " %s=\"%f\"", key, value)
 			default:
 				_, err = fmt.Fprintf(b, " %s=\"%s\"", key, value)
-				//loogging!
+				//logging!
 //				_, err = fmt.Fprintf(b, " %s=\"%s\"", key, v.Kind(), value)
 //				fmt.Printf("unhandled kind %s", v.Kind())
 		}
 
-//		_, err := fmt.Fprintf(b, " %s=\"%s\"", key, value)
-
 		if err != nil {
-			return "", ""
+			return "", "", ""
 		}
+	}
 
-//		switch key {
-//			case "jname": fmt.Fprintf(jname, "%s", value)
-//		}
-
+	if len(emulator.String()) < 2 {
+		fmt.Fprintf(emulator,"%s","bhyve")
 	}
-	return b.String(), jname.String()
+
+	return b.String(), jname.String(), emulator.String()
 }
 
 
@@ -78,14 +78,12 @@ func DoProcess(comment *Comment, logdir string) (error, *CbsdTask) {
 
 	fmt.Printf("JobID %d\n", comment.JobID)
 
-	cbsdArgs, jname := createKeyValuePairs(comment.CommandArgs)
-
+	cbsdArgs, jname, emulator := createKeyValuePairs(comment.CommandArgs)
 	cmdstr := fmt.Sprintf("/usr/local/bin/cbsd %s %s", comment.Command, cbsdArgs)
 	_, err = fmt.Fprintf(commentFile, "%s\n", cmdstr)
 	if err != nil {
 		return err, nil
 	}
-
 	cmd := exec.Command("/bin/sh", filePath)
 	cmd.Stdin = os.Stdin
 	cmd.Stderr = os.Stderr
@@ -133,7 +131,9 @@ func DoProcess(comment *Comment, logdir string) (error, *CbsdTask) {
 	cbsdTask.Progress = 100
 
 	if len(jname)>0 {
-		vm_guid = bget(jname,"vm_zfs_guid")
+
+		vm_guid = vmget(jname,emulator,"vm_zfs_guid")
+
 		if len(vm_guid) > 0 {
 			Infof("GUID found [%s]\n", vm_guid)
 		} else {
@@ -169,7 +169,7 @@ func DoProcess(comment *Comment, logdir string) (error, *CbsdTask) {
 	switch comment.Command {
 		case "bstart":
 			var vnc string
-			vnc = bget(jname,"vnc")
+			vnc = vmget(jname,emulator,"vnc")
 
 			if len(vnc) > 0 {
 				Infof("get VNC [%s]\n", vnc)
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..68535c6
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,13 @@
+module cbsd-mq-router
+
+go 1.23.5
+
+require (
+	github.com/beanstalkd/go-beanstalk v0.2.0
+	golang.org/x/crypto v0.32.0
+)
+
+require (
+	golang.org/x/sys v0.29.0 // indirect
+	golang.org/x/term v0.28.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..a10e63f
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,8 @@
+github.com/beanstalkd/go-beanstalk v0.2.0 h1:6UOJugnu47uNB2jJO/lxyDgeD1Yds7owYi1USELqexA=
+github.com/beanstalkd/go-beanstalk v0.2.0/go.mod h1:/G8YTyChOtpOArwLTQPY1CHB+i212+av35bkPXXj56Y=
+golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
+golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
+golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
+golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
+golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
diff --git a/main.go b/main.go
index 0bbadce..072bb93 100644
--- a/main.go
+++ b/main.go
@@ -60,7 +60,6 @@ func main() {
 	Infof("Broker engine: %s\n", *broker)
 	Infof("Logfile: %s\n", config.Logfile)
 	Infof("MQ logdir: %s\n", config.BeanstalkConfig.LogDir)
-
 	os.Setenv("cbsd_workdir", *cbsdEnv)
 	os.Setenv("workdir", *cbsdEnv)
 	os.Setenv("NOINTER", "1")
diff --git a/vmget.go b/vmget.go
new file mode 100644
index 0000000..6a9a516
--- /dev/null
+++ b/vmget.go
@@ -0,0 +1,36 @@
+package main
+
+import (
+	"fmt"
+	"os/exec"
+	"strings"
+)
+
+// get VM properties via cbsd Xget command
+func vmget(jname string, emulator string, properties string) string {
+	var result string
+	var getPrefix string
+	// todo: rewrite to SQLite3
+
+	switch emulator {
+		case "jail": getPrefix="jget"
+		case "qemu": getPrefix="qget"
+		case "virtualbox": getPrefix="vget"
+		case "xen": getPrefix="xget"
+		default: getPrefix="bget"
+	}
+
+	cmdStr := fmt.Sprintf("/usr/local/bin/cbsd %s jname=%s mode=quiet %s", getPrefix, jname, properties)
+	cmdArgs := strings.Fields(cmdStr)
+	cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
+	out, err := cmd.CombinedOutput()
+	if err != nil {
+		Infof("vmget cmd.Run() failed (cbsd %s jname=%s mode=quiet %s) with %s\n", getPrefix, jname, properties, err)
+		return ""
+	}
+
+	result=(string(out))
+	fmt.Printf("vmget str: [%s]\n", result)
+
+	return result
+}
