notifications: add http config
All checks were successful
ci/woodpecker/push/go-lint Pipeline was successful
All checks were successful
ci/woodpecker/push/go-lint Pipeline was successful
This commit is contained in:
parent
4d705d78fb
commit
6bef0c3e5b
@ -217,7 +217,13 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
||||
command.UserHome = strings.TrimSpace(string(userHome))
|
||||
userSshDir := fmt.Sprintf("%s/.ssh", command.UserHome)
|
||||
|
||||
os.MkdirAll(userSshDir, 0700)
|
||||
if _, err := os.Stat(userSshDir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(userSshDir, 0700)
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating directory %s %v", userSshDir, err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(fmt.Sprintf("%s/authorized_keys", userSshDir)); os.IsNotExist(err) {
|
||||
_, err := os.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
||||
if err != nil {
|
||||
@ -457,7 +463,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).Str("hookType", "error").
|
||||
Logger()
|
||||
errCmd.RunCmd(cmdLogger, opts)
|
||||
_, _ = errCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
|
||||
case "success":
|
||||
@ -467,7 +473,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).Str("hookType", "success").
|
||||
Logger()
|
||||
successCmd.RunCmd(cmdLogger, opts)
|
||||
_, _ = successCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
case "final":
|
||||
for _, v := range cmd.Hooks.Final {
|
||||
@ -476,7 +482,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
||||
cmdLogger := opts.Logger.With().
|
||||
Str("backy-cmd", v).Str("hookType", "final").
|
||||
Logger()
|
||||
finalCmd.RunCmd(cmdLogger, opts)
|
||||
_, _ = finalCmd.RunCmd(cmdLogger, opts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -220,6 +220,7 @@ func setLoggingOptions(k *koanf.Koanf, opts *ConfigOpts) {
|
||||
logFile = k.String(getLoggingKeyFromConfig("file"))
|
||||
opts.LogFilePath = logFile
|
||||
}
|
||||
opts.LogFilePath = logFile
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
if isLoggingVerbose {
|
||||
|
@ -5,10 +5,12 @@ package backy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
stdHttp "net/http"
|
||||
"strings"
|
||||
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/http"
|
||||
"github.com/nikoksr/notify/service/mail"
|
||||
"github.com/nikoksr/notify/service/matrix"
|
||||
"maunium.net/go/mautrix/id"
|
||||
@ -30,6 +32,11 @@ type MailConfig struct {
|
||||
Password string `yaml:"password"`
|
||||
}
|
||||
|
||||
type HttpConfig struct {
|
||||
Url string `yaml:"url"`
|
||||
Headers map[string][]string `yaml:"headers"`
|
||||
}
|
||||
|
||||
// SetupNotify sets up notify instances for each command list.
|
||||
func (opts *ConfigOpts) SetupNotify() {
|
||||
|
||||
@ -74,6 +81,14 @@ func (opts *ConfigOpts) SetupNotify() {
|
||||
continue
|
||||
}
|
||||
services = append(services, mtrxConf)
|
||||
case "http":
|
||||
conf, ok := opts.NotificationConf.HttpConfig[confId]
|
||||
if !ok {
|
||||
opts.Logger.Info().Err(fmt.Errorf("error: ID %s not found in http object", confId)).Str("list", confName).Send()
|
||||
continue
|
||||
}
|
||||
httpConf := setupHttp(conf)
|
||||
services = append(services, httpConf)
|
||||
|
||||
default:
|
||||
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
|
||||
@ -100,3 +115,20 @@ func setupMail(config MailConfig) *mail.Mail {
|
||||
mailClient.BodyFormat(mail.PlainText)
|
||||
return mailClient
|
||||
}
|
||||
|
||||
func setupHttp(httpConf HttpConfig) *http.Service {
|
||||
|
||||
httpService := http.New()
|
||||
// httpService.AddReceiversURLs(httpConf.Url)
|
||||
httpService.AddReceivers(&http.Webhook{
|
||||
URL: httpConf.Url,
|
||||
Header: httpConf.Headers,
|
||||
ContentType: "text/plain",
|
||||
Method: stdHttp.MethodPost,
|
||||
BuildPayload: func(subject, message string) (payload any) {
|
||||
return "[text/plain]: " + subject + " - " + message
|
||||
},
|
||||
})
|
||||
|
||||
return httpService
|
||||
}
|
||||
|
@ -523,12 +523,19 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating file /tmp/%s: %v", uuidFile.String(), passFileErr)
|
||||
}
|
||||
|
||||
passFile.Write([]byte(userNamePass))
|
||||
_, err = passFile.Write([]byte(userNamePass))
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error writing to file /tmp/%s: %v", uuidFile.String(), err)
|
||||
}
|
||||
|
||||
ArgsStr = fmt.Sprintf("cat %s | chpasswd", passFilePath)
|
||||
defer passFile.Close()
|
||||
|
||||
defer client.Remove(passFilePath)
|
||||
rmFileFunc := func() {
|
||||
_ = client.Remove(passFilePath)
|
||||
}
|
||||
|
||||
defer rmFileFunc()
|
||||
// commandSession.Stdin = command.stdin
|
||||
}
|
||||
if err := commandSession.Run(ArgsStr); err != nil {
|
||||
@ -561,7 +568,10 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating sftp client: %v", err)
|
||||
}
|
||||
|
||||
client.MkdirAll(userSshDir)
|
||||
err = client.MkdirAll(userSshDir)
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating directory %s: %v", userSshDir, err)
|
||||
}
|
||||
_, err = client.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
||||
if err != nil {
|
||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)
|
||||
|
@ -239,6 +239,7 @@ type (
|
||||
Notifications struct {
|
||||
MailConfig map[string]MailConfig `yaml:"mail,omitempty"`
|
||||
MatrixConfig map[string]MatrixStruct `yaml:"matrix,omitempty"`
|
||||
HttpConfig map[string]HttpConfig `yaml:"http,omitempty"`
|
||||
}
|
||||
|
||||
CmdOutput struct {
|
||||
|
@ -110,7 +110,11 @@ func injectEnvIntoSSH(envVarsToInject environmentVars, process *ssh.Session, opt
|
||||
goto errEnvFile
|
||||
}
|
||||
for key, val := range envMap {
|
||||
process.Setenv(key, GetVaultKey(val, opts, log))
|
||||
err = process.Setenv(key, GetVaultKey(val, opts, log))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,7 +125,11 @@ errEnvFile:
|
||||
if strings.Contains(envVal, "=") {
|
||||
envVarArr := strings.Split(envVal, "=")
|
||||
|
||||
process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
|
||||
err := process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ func (c *Cache) Set(source, hash string, data []byte, dataType string) (CacheDat
|
||||
path := filepath.Join(c.dir, fmt.Sprintf("%s-%s", fileName, sourceHash))
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
os.MkdirAll(c.dir, 0700)
|
||||
_ = os.MkdirAll(c.dir, 0700)
|
||||
}
|
||||
|
||||
err := os.WriteFile(path, data, 0644)
|
||||
@ -171,7 +171,7 @@ func (cf *CachedFetcher) Hash(data []byte) string {
|
||||
func LoadMetadataFromFile(filePath string) ([]*CacheData, error) {
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
// Create the file if it does not exist
|
||||
os.MkdirAll(path.Dir(filePath), 0700)
|
||||
_ = os.MkdirAll(path.Dir(filePath), 0700)
|
||||
emptyData := []byte("[]")
|
||||
err := os.WriteFile(filePath, emptyData, 0644)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user