gomanager/runner/runner.go

150 lines
3.4 KiB
Go
Raw Normal View History

2023-12-01 23:02:33 +01:00
package runner
import (
"code.icod.de/dalu/gomanager/ent"
"context"
"fmt"
2023-12-01 23:37:36 +01:00
"io"
2023-12-01 23:02:33 +01:00
"os"
"os/exec"
"strings"
)
type Runner struct {
client *ent.Client
}
func NewRunner(client *ent.Client) *Runner {
r := new(Runner)
r.client = client
return r
}
// Run checks all projects sequentially
func (r *Runner) Run() error {
ms, e := r.client.Project.Query().All(context.Background())
if e != nil {
2024-01-08 16:54:54 +01:00
return fmt.Errorf("error querying projects: %s", e)
2023-12-01 23:02:33 +01:00
}
if len(ms) == 0 {
return nil
}
v, e := getCurrentGoVersion()
if e != nil {
2024-01-08 16:54:54 +01:00
return fmt.Errorf("error getting current go version: %s", e)
2023-12-01 23:02:33 +01:00
}
for _, m := range ms {
2024-01-08 17:13:48 +01:00
fmt.Println(fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath))
fv, e := getVersionOfFile(fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath))
2023-12-01 23:02:33 +01:00
if e != nil {
2024-01-08 17:00:26 +01:00
return fmt.Errorf("error getting version of file: %s", e)
2023-12-01 23:02:33 +01:00
}
if v != fv {
l, e := goBuildBinary(m.RootPath)
if e != nil {
2024-01-08 16:45:09 +01:00
return fmt.Errorf("error in goBuildBinary: %s", e)
2023-12-01 23:02:33 +01:00
}
_, e = r.client.Logentry.
Create().
SetContent(l).
Save(context.Background())
if e != nil {
2024-01-08 16:45:09 +01:00
return fmt.Errorf("error creating log entry: %s", e)
2023-12-01 23:02:33 +01:00
}
2023-12-01 23:37:36 +01:00
if m.MoveToTarget {
if fl, e := copyFile(
fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath),
fmt.Sprintf("%s/%s", m.RootPath, m.BinaryTargetPath),
); e != nil {
return fmt.Errorf("error copying file to target: %s", e)
} else {
_, e = r.client.Logentry.
Create().
SetContent(fl).
Save(context.Background())
if e != nil {
2024-01-08 16:45:09 +01:00
return fmt.Errorf("error saving log entry: %s", e)
2023-12-01 23:37:36 +01:00
}
}
} else {
if e := chownBinary(m.User, m.Group, fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath)); e != nil {
2024-01-08 16:45:09 +01:00
return fmt.Errorf("error chowning binary: %s", e)
2023-12-01 23:37:36 +01:00
}
}
2023-12-01 23:02:33 +01:00
}
}
return nil
}
// getCurrentGoVersion returns the version of the installed go package or a non-nil error
func getCurrentGoVersion() (string, error) {
cmd := exec.Command("go", "version")
var out strings.Builder
cmd.Stdout = &out
if e := cmd.Run(); e != nil {
return "", e
}
split := strings.Split(out.String(), " ")
version := split[2]
return version, nil
}
2024-01-08 17:11:57 +01:00
// getVersionOfFile returns the version of a go binary or a non-nil error
2023-12-01 23:02:33 +01:00
func getVersionOfFile(file string) (string, error) {
cmd := exec.Command("go", "version", file)
var out strings.Builder
cmd.Stdout = &out
if e := cmd.Run(); e != nil {
return "", e
}
split := strings.Split(out.String(), " ")
version := split[1]
return version, nil
}
// goBuildBinary does a go build in the root directory, then "cd"s back to the previous working directory.
func goBuildBinary(root string) (string, error) {
cwd, e := os.Getwd()
if e != nil {
return "", e
}
if e := os.Chdir(root); e != nil {
return "", e
}
cmd := exec.Command("go", "build")
var out strings.Builder
cmd.Stdout = &out
if e := cmd.Run(); e != nil {
return "", e
}
if e := os.Chdir(cwd); e != nil {
return "", e
}
return out.String(), nil
}
func chownBinary(user, group, file string) error {
cmd := exec.Command("chown", fmt.Sprintf("%s:%s", user, group), file)
return cmd.Run()
}
2023-12-01 23:37:36 +01:00
func copyFile(src, dst string) (string, error) {
srcF, e := os.Open(src)
if e != nil {
return "", e
}
defer srcF.Close()
dstF, e := os.Open(dst)
if e != nil {
return "", e
}
defer dstF.Close()
if size, e := io.Copy(dstF, srcF); e != nil {
return "", e
} else {
return fmt.Sprintf("copied %d bytes from %s to %s", size, src, dst), nil
}
}