small pixel drawing of a pufferfish zoa

the shittiest deploy code this world has ever seen
Jes Olson j3s@c3f.net
Fri, 30 Sep 2022 18:12:28 -0500
commit

899769bafa23518214fef64a05d8f0c11211a381

parent

7a8ed144df4454996f2b4dcaf0169718b63a3e86

4 files changed, 94 insertions(+), 21 deletions(-)

jump to
A build

@@ -0,0 +1,8 @@

+#!/bin/sh + +CGO_ENABLED=0 go build +sudo cp zoa /usr/local/sbin/ + +if [ "$(hostname)" = "nostromo" ]; then + scp zoa j3s.sh:/var/www/trash.j3s.sh/zoa +fi
D deploy

@@ -1,4 +0,0 @@

-#!/bin/sh - -CGO_ENABLED=0 go build -scp zoa j3s.sh:/var/www/trash.j3s.sh/zoa
M main.gomain.go

@@ -4,6 +4,7 @@ import (

"fmt" "log" "os" + "os/exec" "path/filepath" "j3s.sh/zoa/git"

@@ -24,11 +25,51 @@ if len(os.Args) > 2 {

branch = os.Args[2] } - path := os.Args[1] - if path == "-h" || path == "--help" || path == "help" { + command := os.Args[1] + if command == "-h" || command == "--help" || command == "help" { printUsage() } - gitMode := git.GitMode(path) + + if command == "deploy" { + if len(os.Args) < 3 { + fmt.Println("please supply one or more servers to deploy to") + os.Exit(1) + } + hosts := os.Args[2:] + + zoaPath, err := exec.LookPath("zoa") + if err != nil { + log.Fatal(err) + } + + err = shell.SSH("command -v zoa", hosts) + if err != nil { + // Install Zoa + err = shell.SCP(zoaPath, hosts) + if err != nil { + log.Fatal(err) + } + err = shell.SSH("chmod +x zoa", hosts) + if err != nil { + log.Fatal(err) + } + err = shell.SSH("mv zoa /usr/local/sbin/", hosts) + if err != nil { + subErr := shell.SSH("sudo mv zoa /usr/local/sbin/", hosts) + if subErr != nil { + log.Fatal(err) + } + } + } + + err = shell.SSH("sudo zoa https://git.sr.ht/~j3s/testy", hosts) + if err != nil { + log.Fatal(err) + } + os.Exit(0) + } + + gitMode := git.GitMode(command) if gitMode { utils.SetZoaRoot("/var/lib/zoa/repo")

@@ -38,36 +79,34 @@ log.Fatal(err)

} // check the path & branch, make sure it's correct // then clone - git.Clone(path, branch) + git.Clone(command, branch) } else { - utils.SetZoaRoot(path) + utils.SetZoaRoot(command) } // TODO: this writer is responsible for the random stdout // maybe save the stdout for debug mode somehow main := filepath.Join(utils.ZoaRoot, "main") - utils.BluePrintln("(✿◠‿◠) zoa") shell.RunScript(main) } func printUsage() { // TODO: -v -h - fmt.Println(`usage: zoa [git repo | fs path] [optional git branch] + fmt.Println(`usage: zoa [deploy | run] [remote host | path] + +sorry, this shit is a work in progress details: - zoa [git repo] [optional branch] - zoa will clone the given repo+branch to /var/lib/zoa/repo and execute main. - if the given repo/branch is already present, zoa will git fetch, then - execute main. + zoa run [path] will execute the zoa script in the path - zoa [filesystem path] - zoa will cd to the given path & execute the main script there - (mostly used for development) + zoa deploy [remote host] will ssh to the given server and: + - install zoa (if not installed) + - copy the local zoa repo to the host + - execute zoa examples: - zoa https://github.com/biox/zoa - zoa https://github.com/biox/zoa dev - zoa .`) + zoa run ./my-dir/main + zoa deploy j3s.sh git.j3s.sh`) os.Exit(1) }
M shell/shell.goshell/shell.go

@@ -5,6 +5,7 @@ "context"

"fmt" "log" "os" + "os/exec" "path/filepath" "strings"

@@ -102,6 +103,7 @@ return nil

} func RunScript(s string) { + utils.BluePrintln("(✿◠‿◠) zoa") script, err := parseFile(s) if err != nil { fmt.Printf("error in %s: %s\n", s, err)

@@ -157,3 +159,31 @@ return false, err

} return true, nil } + +func SSH(command string, hostlist []string) error { + _, err := exec.LookPath("ssh") + if err != nil { + log.Fatal(err) + } + for _, server := range hostlist { + cmd := exec.Command("ssh", server, command) + cmd.Stdout = os.Stdout + err := cmd.Run() + return err + } + return nil +} + +func SCP(file string, hostlist []string) error { + _, err := exec.LookPath("scp") + if err != nil { + log.Fatal(err) + } + for _, server := range hostlist { + cmd := exec.Command("scp", file, server+":") + cmd.Stdout = os.Stdout + err := cmd.Run() + return err + } + return nil +}