This commit is contained in:
2023-12-01 23:02:33 +01:00
commit 7634b181de
48 changed files with 8610 additions and 0 deletions

120
cmd/add.go Normal file
View File

@@ -0,0 +1,120 @@
package cmd
import (
"context"
"errors"
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
var (
addUser = ""
addGroup = ""
addRootPath = ""
addBinaryPath = ""
addServiceName = ""
addBinaryTargetPath = ""
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "add a project for watching",
Long: `add a project for watching
Example:
A project residing in /var/www/dalu/project
with a binary project at /var/www/dalu/project/project
and a systemd service named dalu-project.service
with the binary being owned by dalu:dalu
gomanager add \
--root-path="/var/www/dalu/project" \
--binary-path="project" \
--service-name="dalu-project.service" \
--user="dalu" \
--group="dalu"
let's assume we'd like to resulting binary to be moved to /usr/local/bin/project after building
we add
--binary-target-path="/usr/local/bin/project"
or with short notation:
gomanager add \
-r="/var/www/dalu/project" \
-b="project" \
-s="dalu-project.service" \
-u="dalu" \
-g="dalu"
with moving the compiled binary
-t="/usr/local/bin/project"
`,
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
if addUser == "" {
return errors.New("user is empty")
}
if addGroup == "" {
return errors.New("group is empty")
}
if addRootPath == "" {
return errors.New("root-path is empty")
}
if addBinaryPath == "" {
return errors.New("binary-path is empty")
}
if addServiceName == "" {
return errors.New("service-name is empty")
}
p := client.Project.
Create().
SetUser(addUser).
SetGroup(addGroup).
SetRootPath(addRootPath).
SetBinaryPath(addBinaryPath).
SetServiceName(addServiceName)
if addBinaryTargetPath != "" {
p = p.SetMoveToTarget(true).SetBinaryTargetPath(addBinaryTargetPath)
}
project, e := p.Save(context.Background())
if e != nil {
return e
}
fmt.Printf("Saved project with ID: %d\n", project.ID)
return nil
},
}
func init() {
rootCmd.AddCommand(addCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
addCmd.Flags().StringVarP(&addRootPath, "root-path", "r", "", "/var/www/code.icod.de/goroot")
addCmd.Flags().StringVarP(&addServiceName, "service-name", "s", "", "mysystemd.service")
addCmd.Flags().StringVarP(&addBinaryPath, "binary-path", "b", "", "gomanager or cmd/run/gomanager")
addCmd.Flags().StringVarP(&addBinaryTargetPath, "binary-target-path", "t", "", "../binaries/gomanager-binary")
addCmd.Flags().StringVarP(&addUser, "user", "u", "nginx", "www-data")
addCmd.Flags().StringVarP(&addGroup, "group", "g", "nginx", "www-data")
}

35
cmd/clean.go Normal file
View File

@@ -0,0 +1,35 @@
package cmd
import (
"os"
"runtime"
"github.com/spf13/cobra"
)
// cleanCmd represents the clean command
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Cleans the gomanager database",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
if e := os.Remove(sqliteFilename); e != nil {
return e
}
return os.Remove("/var/lib/gomanager")
},
}
func init() {
rootCmd.AddCommand(cleanCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cleanCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cleanCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

42
cmd/cron.go Normal file
View File

@@ -0,0 +1,42 @@
package cmd
import (
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"code.icod.de/dalu/gomanager/runner"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
// cronCmd represents the cron command
var cronCmd = &cobra.Command{
Use: "cron",
Short: "this command is meant to be ran via cron",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
r := runner.NewRunner(client)
return r.Run()
},
}
func init() {
rootCmd.AddCommand(cronCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cronCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// cronCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

81
cmd/list.go Normal file
View File

@@ -0,0 +1,81 @@
package cmd
import (
"context"
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
var (
colTitleIndex = "ID"
colTitleRootPath = "Root Path"
colTitleBinaryPath = "Binary"
colTitleServiceName = "Service Name"
colTitleUser = "User"
colTitleGroup = "Group"
colTitleBinaryTargetPath = "Target"
rowHeader = table.Row{
colTitleIndex,
colTitleRootPath,
colTitleBinaryPath,
colTitleServiceName,
colTitleUser,
colTitleGroup,
colTitleBinaryTargetPath,
}
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists all watched projects",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
ms, e := client.Project.Query().All(context.Background())
if e != nil {
return e
}
tw := table.NewWriter()
tw.AppendHeader(rowHeader)
for _, m := range ms {
tw.AppendRow(table.Row{
m.ID,
m.RootPath,
m.BinaryPath,
m.ServiceName,
m.User,
m.Group,
m.BinaryTargetPath,
})
}
tw.SetIndexColumn(1)
tw.SetTitle("Watched Projects")
fmt.Println(tw.Render())
return nil
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

59
cmd/logs.go Normal file
View File

@@ -0,0 +1,59 @@
package cmd
import (
"context"
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"code.icod.de/dalu/gomanager/ent/logentry"
"code.icod.de/dalu/gomanager/ent/project"
"entgo.io/ent/dialect/sql"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
var (
showLogID = -1
)
// logsCmd represents the logs command
var logsCmd = &cobra.Command{
Use: "logs",
Short: "Show logs of a project",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
ms, e := client.Project.Query().Where(project.ID(showLogID)).
QueryLogentries().
Order(logentry.ByDate(sql.OrderDesc())).
All(context.Background())
if e != nil {
return e
}
for _, m := range ms {
fmt.Println(m.Date, m.Content)
}
return nil
},
}
func init() {
rootCmd.AddCommand(logsCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// logsCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// logsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
logsCmd.Flags().IntVar(&showLogID, "id", -1, "1")
}

70
cmd/remove.go Normal file
View File

@@ -0,0 +1,70 @@
package cmd
import (
"context"
"errors"
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
var (
removeID = -1
)
// removeCmd represents the remove command
var removeCmd = &cobra.Command{
Use: "remove",
Short: "Remove a job with an ID",
Long: `Remove a job with an ID
Use gomanager list to find the project's (or job's) ID.
Example:
sudo ./gomanager list
+---------------------------------------------------------------------------------------------------------------------+
| Watched Projects |
+----+---------------------------------------------------+-------------+---------------------+-------+-------+--------+
| ID | ROOT PATH | BINARY | SERVICE NAME | USER | GROUP | TARGET |
+----+---------------------------------------------------+-------------+---------------------+-------+-------+--------+
| 1 | /home/darko/go/src/code.icod.de/dalu/simpleforum/ | simpleforum | simpleforum.service | nginx | nginx | |
| 2 | /home/darko/go/src/code.icod.de/dalu/affx/ | affx | affx.service | nginx | nginx | |
+----+---------------------------------------------------+-------------+---------------------+-------+-------+--------+
Then use
gomanager remove --id=1
to remove the simpleforum project
'
`,
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
if removeID == -1 {
return errors.New("the ID is not set. Use --id=<number> to remove a job with a <number> ID")
}
return client.Project.DeleteOneID(removeID).Exec(context.Background())
},
}
func init() {
rootCmd.AddCommand(removeCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// removeCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// removeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
removeCmd.Flags().IntVar(&removeID, "id", -1, "1")
}

39
cmd/root.go Normal file
View File

@@ -0,0 +1,39 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
)
const sqliteFilename = "/var/lib/gomanager/gomanager.dat"
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gomanager",
Short: "gomanager: code.icod.de/dalu/gomanager",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gomanager.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

55
cmd/setup.go Normal file
View File

@@ -0,0 +1,55 @@
package cmd
import (
"context"
"errors"
"fmt"
"os"
"code.icod.de/dalu/gomanager/ent"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "sets up the program",
RunE: func(cmd *cobra.Command, args []string) error {
if os.Getuid() != 0 {
return errors.New("this program must be ran as root")
}
_, e := os.Stat("/var/lib/gomanager")
if errors.Is(e, os.ErrNotExist) {
err := os.MkdirAll("/var/lib/gomanager", 0777)
if err != nil {
return err
}
}
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
return fmt.Errorf("failed creating schema resources: %v", err)
}
return nil
},
}
func init() {
rootCmd.AddCommand(setupCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setupCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// setupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

51
cmd/start.go Normal file
View File

@@ -0,0 +1,51 @@
package cmd
import (
"code.icod.de/dalu/gomanager/runner"
"fmt"
"runtime"
"time"
"code.icod.de/dalu/gomanager/ent"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "starts the autonomous watcher mode, which checks projects every 24h",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
r := runner.NewRunner(client)
t := time.NewTicker(time.Hour * 24)
for {
select {
case <-t.C:
if e := r.Run(); e != nil {
return e
}
}
}
},
}
func init() {
rootCmd.AddCommand(startCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}