generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from FMotalleb/rehaul
Rehaul
- Loading branch information
Showing
21 changed files
with
582 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package abstraction | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/FMotalleb/crontab-go/config" | ||
) | ||
|
||
type CmdConnection interface { | ||
Prepare(context.Context, *config.Task) error | ||
Connect() error | ||
Execute() ([]byte, error) | ||
Disconnect() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,9 @@ | |
jobs: | ||
- name: Test Job | ||
tasks: | ||
- command: ip a | ||
env: | ||
SHELL: C:\WINDOWS\System32\OpenSSH\ssh.exe | ||
SHELL_ARGS: "-i;C:/Users/Motalleb/.xpipe/storage/data/id_rsa_old;[email protected];-p;9011" | ||
- command: ip a -pt | ||
retries: 5 | ||
retry-delay: 5s | ||
schedulers: | ||
- on-init: true | ||
- interval: 10m10s |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cfgcompiler | ||
|
||
import ( | ||
"github.com/robfig/cron/v3" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
"github.com/FMotalleb/crontab-go/core/schedule" | ||
) | ||
|
||
func CompileScheduler(sh *config.JobScheduler, cr *cron.Cron, logger *logrus.Entry) abstraction.Scheduler { | ||
switch { | ||
case sh.Cron != "": | ||
scheduler := schedule.NewCron( | ||
sh.Cron, | ||
cr, | ||
logger, | ||
) | ||
return &scheduler | ||
case sh.Interval != 0: | ||
scheduler := schedule.NewInterval( | ||
sh.Interval, | ||
logger, | ||
) | ||
return &scheduler | ||
|
||
case sh.OnInit: | ||
scheduler := schedule.Init{} | ||
return &scheduler | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cfgcompiler | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
"github.com/FMotalleb/crontab-go/core/task" | ||
) | ||
|
||
func CompileTask(t *config.Task, logger *logrus.Entry) abstraction.Executable { | ||
var exe abstraction.Executable | ||
switch { | ||
case t.Command != "": | ||
exe = task.NewCommand(t, logger) | ||
case t.Get != "": | ||
exe = task.NewGet(t, logger) | ||
case t.Post != "": | ||
exe = task.NewPost(t, logger) | ||
default: | ||
logger.Fatalln("cannot handle given task config", t) | ||
} | ||
|
||
onDone := []abstraction.Executable{} | ||
for _, d := range t.OnDone { | ||
onDone = append(onDone, CompileTask(&d, logger)) | ||
} | ||
exe.SetDoneHooks(onDone) | ||
onFail := []abstraction.Executable{} | ||
for _, d := range t.OnFail { | ||
onFail = append(onFail, CompileTask(&d, logger)) | ||
} | ||
exe.SetFailHooks(onFail) | ||
|
||
return exe | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package connection | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
) | ||
|
||
func CompileConnection(conn *config.TaskConnection, logger *logrus.Entry) abstraction.CmdConnection { | ||
if conn.Local { | ||
return NewLocalCMDConn(logger) | ||
} | ||
log.Fatalln("cannot compile given taskConnection", conn) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package connection | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/cmd" | ||
"github.com/FMotalleb/crontab-go/config" | ||
credential "github.com/FMotalleb/crontab-go/core/os_credential" | ||
) | ||
|
||
type Local struct { | ||
log *logrus.Entry | ||
cmd *exec.Cmd | ||
} | ||
|
||
func NewLocalCMDConn(log *logrus.Entry) abstraction.CmdConnection { | ||
return &Local{ | ||
log: log.WithField( | ||
"connection", "local", | ||
), | ||
} | ||
} | ||
|
||
// Prepare implements abstraction.CmdConnection. | ||
func (l *Local) Prepare(ctx context.Context, task *config.Task) error { | ||
shell := cmd.CFG.Shell | ||
shellArgs := cmd.CFG.ShellArgs | ||
env := os.Environ() | ||
for key, val := range task.Env { | ||
env = append(env, fmt.Sprintf("%s=%s", key, val)) | ||
switch strings.ToLower(key) { | ||
case "shell": | ||
l.log.Info("you've used `SHELL` env variable in command environments, overriding the global shell with:", val) | ||
shell = val | ||
case "shell_args": | ||
l.log.Info("you've used `SHELL_ARGS` env variable in command environments, overriding the global shell_args with: ", val) | ||
shellArgs = strings.Split(val, ";") | ||
} | ||
} | ||
workingDir := task.WorkingDirectory | ||
if workingDir == "" { | ||
var e error | ||
workingDir, e = os.Getwd() | ||
if e != nil { | ||
return fmt.Errorf("cannot get current working directory: %s", e) | ||
} | ||
} | ||
l.cmd = exec.CommandContext( | ||
ctx, | ||
shell, | ||
append(shellArgs, task.Command)..., | ||
) | ||
l.log = l.log.WithFields( | ||
logrus.Fields{ | ||
"working_directory": workingDir, | ||
"shell": shell, | ||
"shell_args": shellArgs, | ||
}, | ||
) | ||
credential.SetUser(l.log, l.cmd, task.UserName, task.GroupName) | ||
l.cmd.Env = env | ||
l.cmd.Dir = workingDir | ||
|
||
return nil | ||
} | ||
|
||
// Connect implements abstraction.CmdConnection. | ||
func (l *Local) Connect() error { | ||
return nil | ||
} | ||
|
||
// Disconnect implements abstraction.CmdConnection. | ||
func (l *Local) Disconnect() error { | ||
return nil | ||
} | ||
|
||
// Execute implements abstraction.CmdConnection. | ||
func (l *Local) Execute() ([]byte, error) { | ||
var res bytes.Buffer | ||
l.cmd.Stdout = &res | ||
l.cmd.Stderr = &res | ||
if err := l.cmd.Start(); err != nil { | ||
l.log.Warn("failed to start the command ", err) | ||
return []byte{}, err | ||
} else if err := l.cmd.Wait(); err != nil { | ||
l.log.Warnf("command failed with answer: %s", strings.TrimSpace(res.String())) | ||
l.log.Warn("failed to execute the command", err) | ||
return res.Bytes(), err | ||
} else { | ||
return res.Bytes(), nil | ||
} | ||
} |
Oops, something went wrong.