Schedule script run

This commit is contained in:
Magnus Åhall 2025-08-07 23:26:15 +02:00
parent fc992b8bdc
commit 6d05152ab2
6 changed files with 162 additions and 7 deletions

109
script.go
View file

@ -6,6 +6,9 @@ import (
"github.com/jmoiron/sqlx"
// Standard
"crypto/md5"
"database/sql"
"encoding/hex"
"encoding/json"
"strings"
"time"
@ -26,6 +29,17 @@ type Hook struct {
SSH string
}
func GetScript(scriptID int) (script Script, err error) { // {{{
row := db.QueryRowx(`SELECT * FROM script WHERE id=$1`, scriptID)
err = row.StructScan(&script)
if err != nil {
err = werr.Wrap(err)
return
}
return
} // }}}
func GetScripts() (scripts []Script, err error) { // {{{
scripts = []Script{}
@ -146,11 +160,40 @@ func SearchScripts(search string) (scripts []Script, err error) { // {{{
return
} // }}}
func HookScript(nodeID, scriptID int) (err error) {// {{{
func HookScript(nodeID, scriptID int) (err error) { // {{{
_, err = db.Exec(`INSERT INTO hook(node_id, script_id, ssh) VALUES($1, $2, '<host>')`, nodeID, scriptID)
return
}// }}}
} // }}}
func GetHook(hookID int) (hook Hook, err error) { // {{{
row := db.QueryRow(`
SELECT
to_json(res)
FROM (
SELECT
h.id,
h.ssh,
(SELECT to_json(node) FROM node WHERE id = h.node_id) AS node,
(SELECT to_json(script) FROM script WHERE id = h.script_id) AS script
FROM hook h
WHERE
h.id = $1
) res
`,
hookID,
)
var data []byte
if err = row.Scan(&data); err != nil {
err = werr.Wrap(err)
}
err = json.Unmarshal(data, &hook)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}
func UpdateHook(hook Hook) (err error) { // {{{
_, err = db.Exec(`UPDATE hook SET ssh=$2 WHERE id=$1`, hook.ID, strings.TrimSpace(hook.SSH))
if err != nil {
@ -167,3 +210,65 @@ func DeleteHook(hookID int) (err error) { // {{{
}
return
} // }}}
func ScheduleHook(hookID int) (err error) { // {{{
/* Script source is needed to preserve the full execution
against changing of the script at a later date.
To not waste db disk, the md5sum of the script is
calculated and the changed version of the script is just
stored once.
*/
hook, err := GetHook(hookID)
if err != nil {
err = werr.Wrap(err)
return
}
scriptLogID, err := ScriptPreservedID(hook.Script.Source)
if err != nil {
err = werr.Wrap(err)
return
}
// The node tree data is retrieved and sent as input to the script.
var topNode *Node
topNode, err = GetNodeTree(hook.Node.ID, 0, true)
if err != nil {
err = werr.Wrap(err)
return
}
nodeData, err := json.Marshal(topNode)
if err != nil {
err = werr.Wrap(err)
return
}
_, err = db.Exec(`INSERT INTO execution(script_log_id, data, ssh) VALUES($1, $2, $3)`, scriptLogID, nodeData, hook.SSH)
if err != nil {
err = werr.Wrap(err)
return
}
return
} // }}}
func ScriptPreservedID(source string) (id int, err error) { // {{{
sum := md5.Sum([]byte(source))
md5sum := hex.EncodeToString(sum[:])
row := db.QueryRow(`SELECT id FROM script_log WHERE md5sum=$1`, md5sum)
if err = row.Scan(&id); err == nil {
return
}
if err != sql.ErrNoRows {
err = werr.Wrap(err)
return
}
row = db.QueryRow(`INSERT INTO script_log(md5sum, source) VALUES($1, $2) RETURNING id`, md5sum, source)
err = row.Scan(&id)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}