Compare commits
No commits in common. "a2a89011fe89e96a5f13523bd9337c18bb2908ca" and "02321870b5dc327768bbc58d40330232d01b7be4" have entirely different histories.
a2a89011fe
...
02321870b5
@ -1,9 +0,0 @@
|
||||
## 0.2.4 - 2023-02-18
|
||||
### Added
|
||||
* Notifications now display errors and the output of the failed command.
|
||||
* CI configs for GitHub and Woodpecker
|
||||
* Added `version` subcommand
|
||||
### Changed
|
||||
* Console logging can be disabled by setting `console-disabled` in the `logging` object
|
||||
## Fixed
|
||||
* If Host was not defined for an incomplete `hosts` object, any commands would fail as they could not look up the values in the SSH config files.
|
@ -1,6 +0,0 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
|
||||
and is generated by [Changie](https://github.com/miniscruff/changie).
|
@ -1,26 +0,0 @@
|
||||
changesDir: .changes
|
||||
unreleasedDir: unreleased
|
||||
headerPath: header.tpl.md
|
||||
changelogPath: CHANGELOG.md
|
||||
versionExt: md
|
||||
versionFormat: '## {{.Version}} - {{.Time.Format "2006-01-02"}}'
|
||||
kindFormat: '### {{.Kind}}'
|
||||
changeFormat: '* {{.Body}}'
|
||||
kinds:
|
||||
- label: Added
|
||||
auto: minor
|
||||
- label: Changed
|
||||
auto: major
|
||||
- label: Deprecated
|
||||
auto: minor
|
||||
- label: Removed
|
||||
auto: major
|
||||
- label: Fixed
|
||||
auto: patch
|
||||
- label: Security
|
||||
auto: patch
|
||||
newlines:
|
||||
afterChangelogHeader: 1
|
||||
beforeChangelogVersion: 1
|
||||
endOfVersion: 1
|
||||
envPrefix: CHANGIE_
|
37
.github/workflows/release.yml
vendored
37
.github/workflows/release.yml
vendored
@ -1,37 +0,0 @@
|
||||
name: goreleaser
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ] # your default branch if different
|
||||
paths: [ CHANGELOG.md ] # your changelog file if different
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
# issues: write
|
||||
|
||||
jobs:
|
||||
goreleaser:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- run: git fetch --force --tags
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '==1.19.5'
|
||||
cache: true
|
||||
# More assembly might be required: Docker logins, GPG, etc. It all depends
|
||||
# on your needs.
|
||||
- uses: goreleaser/goreleaser-action@v4
|
||||
with:
|
||||
# either 'goreleaser' (default) or 'goreleaser-pro':
|
||||
distribution: goreleaser
|
||||
version: latest
|
||||
args: release --release-notes=".changes/$(go run backy.go version).md" -f .goreleaser/github.yml --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GORELEASER_TOKEN }}
|
||||
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
|
||||
# distribution:
|
||||
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
|
@ -1,3 +1,5 @@
|
||||
# This is an example .goreleaser.yml file with some sensible defaults.
|
||||
# Make sure to check the documentation at https://goreleaser.com
|
||||
before:
|
||||
hooks:
|
||||
# You may remove this if you don't use go modules.
|
||||
@ -17,7 +19,7 @@ archives:
|
||||
- format: tar.gz
|
||||
# this name template makes the OS and Arch compatible with the results of uname.
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_{{ .Version }}_
|
||||
{{ .ProjectName }}
|
||||
{{- title .Os }}_
|
||||
{{- if eq .Arch "amd64" }}x86_64
|
||||
{{- else if eq .Arch "386" }}i386
|
||||
@ -32,7 +34,11 @@ checksum:
|
||||
snapshot:
|
||||
name_template: "{{ incpatch .Version }}-next"
|
||||
changelog:
|
||||
skip: false
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- '^docs:'
|
||||
- '^test:'
|
||||
|
||||
gitea_urls:
|
||||
api: https://git.andrewnw.xyz/api/v1
|
@ -1,41 +0,0 @@
|
||||
# This is an example .goreleaser.yml file with some sensible defaults.
|
||||
# Make sure to check the documentation at https://goreleaser.com
|
||||
before:
|
||||
hooks:
|
||||
# You may remove this if you don't use go modules.
|
||||
- go mod tidy
|
||||
builds:
|
||||
- env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- freebsd
|
||||
- linux
|
||||
goarch:
|
||||
- "386"
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
archives:
|
||||
- format: tar.gz
|
||||
# this name template makes the OS and Arch compatible with the results of uname.
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_{{ .Version }}_
|
||||
{{- title .Os }}_
|
||||
{{- if eq .Arch "amd64" }}x86_64
|
||||
{{- else if eq .Arch "386" }}i386
|
||||
{{- else }}{{ .Arch }}{{ end }}
|
||||
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||
# use zip for windows archives
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
checksum:
|
||||
name_template: 'checksums.txt'
|
||||
snapshot:
|
||||
name_template: "{{ incpatch .Version }}-next"
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- '^docs:'
|
||||
- '^test:'
|
@ -1,46 +0,0 @@
|
||||
before:
|
||||
hooks:
|
||||
# You may remove this if you don't use go modules.
|
||||
- go mod tidy
|
||||
builds:
|
||||
- env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- freebsd
|
||||
- linux
|
||||
goarch:
|
||||
- "386"
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
archives:
|
||||
- format: binary
|
||||
# this name template makes the OS and Arch compatible with the results of uname.
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_{{ .Version }}_
|
||||
{{- title .Os }}_
|
||||
{{- if eq .Arch "amd64" }}x86_64
|
||||
{{- else if eq .Arch "386" }}i386
|
||||
{{- else }}{{ .Arch }}{{ end }}
|
||||
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||
# use zip for windows archives
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
checksum:
|
||||
name_template: 'checksums.txt'
|
||||
snapshot:
|
||||
name_template: "{{ incpatch .Version }}-next"
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- '^docs:'
|
||||
- '^test:'
|
||||
|
||||
gitea_urls:
|
||||
api: https://git.vern.cc/api/v1
|
||||
download: https://git.vern.cc
|
||||
# The lines beneath this are called `modelines`. See `:help modeline`
|
||||
# Feel free to remove those if you don't want/use them.
|
||||
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
@ -1,10 +0,0 @@
|
||||
pipeline:
|
||||
release:
|
||||
image: goreleaser/goreleaser
|
||||
commands:
|
||||
- goreleaser release -f .goreleaser/vern.yml --release-notes=".changes/$(go run backy.go version).md"
|
||||
secrets: [ gitea_token ]
|
||||
when:
|
||||
event: tag
|
||||
|
||||
branches: master
|
17
CHANGELOG.md
17
CHANGELOG.md
@ -1,17 +0,0 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
|
||||
and is generated by [Changie](https://github.com/miniscruff/changie).
|
||||
|
||||
|
||||
## 0.2.4 - 2023-02-18
|
||||
### Added
|
||||
* Notifications now display errors and the output of the failed command.
|
||||
* CI configs for GitHub and Woodpecker
|
||||
* Added `version` subcommand
|
||||
### Changed
|
||||
* Console logging can be disabled by setting `console-disabled` in the `logging` object
|
||||
## Fixed
|
||||
* If Host was not defined for an incomplete `hosts` object, any commands would fail as they could not look up the values in the SSH config files.
|
7
Makefile
7
Makefile
@ -1,8 +1,5 @@
|
||||
build:
|
||||
go build
|
||||
|
||||
install:
|
||||
go install .
|
||||
|
||||
goreleaser-snapshot:
|
||||
goreleaser -f .goreleaser/gitea.yml release --snapshot --clean
|
||||
gorealeaser-build:
|
||||
goreleaser release --snapshot --rm-dist
|
@ -12,9 +12,10 @@ import (
|
||||
|
||||
var (
|
||||
backupCmd = &cobra.Command{
|
||||
Use: "backup [--lists=list1,list2]",
|
||||
Use: "backup [--lists==list1,list2]",
|
||||
Short: "Runs commands defined in config file.",
|
||||
Long: "Backup executes commands defined in config file.\nUse the --lists flag to execute the specified commands. If not specified, all lists will be executed.",
|
||||
Long: `Backup executes commands defined in config file.
|
||||
Use the --lists flag to execute the specified commands.`,
|
||||
Run: Backup,
|
||||
}
|
||||
)
|
||||
|
@ -8,9 +8,9 @@ import (
|
||||
|
||||
var (
|
||||
cronCmd = &cobra.Command{
|
||||
Use: "cron [flags]",
|
||||
Short: "Runs command lists defined in config file.",
|
||||
Long: `Cron starts a scheduler that executes command lists at the time defined in config file.`,
|
||||
Use: "cron command ...",
|
||||
Short: "Runs commands defined in config file.",
|
||||
Long: `Cron executes commands at the time defined in config file.`,
|
||||
Run: cron,
|
||||
}
|
||||
)
|
||||
|
@ -29,5 +29,6 @@ func execute(cmd *cobra.Command, args []string) {
|
||||
opts := backy.NewOpts(cfgFile, backy.AddCommands(args))
|
||||
opts.InitConfig()
|
||||
// opts.InitMongo()
|
||||
backy.ReadConfig(opts).ExecuteCmds(opts)
|
||||
backy.ReadConfig(opts).ExecuteCmds()
|
||||
|
||||
}
|
||||
|
@ -36,5 +36,5 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level")
|
||||
|
||||
rootCmd.AddCommand(backupCmd, execCmd, cronCmd, versionCmd)
|
||||
rootCmd.AddCommand(backupCmd, execCmd, cronCmd)
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const versionStr = "0.2.4"
|
||||
|
||||
var (
|
||||
versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Prints the version and exits.",
|
||||
Run: version,
|
||||
}
|
||||
)
|
||||
|
||||
func version(cmd *cobra.Command, args []string) {
|
||||
|
||||
fmt.Printf("v%s\n", versionStr)
|
||||
|
||||
os.Exit(0)
|
||||
}
|
@ -29,10 +29,9 @@ var Sprintf = fmt.Sprintf
|
||||
// The environment of local commands will be the machine's environment plus any extra
|
||||
// variables specified in the Env file or Environment.
|
||||
// Dir can also be specified for local commands.
|
||||
func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]string, error) {
|
||||
func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) error {
|
||||
|
||||
var (
|
||||
outputArr []string
|
||||
ArgsStr string
|
||||
cmdOutBuf bytes.Buffer
|
||||
cmdOutWriters io.Writer
|
||||
@ -42,23 +41,24 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]s
|
||||
env: command.Environment,
|
||||
}
|
||||
)
|
||||
envVars.env = append(envVars.env, os.Environ()...)
|
||||
|
||||
for _, v := range command.Args {
|
||||
ArgsStr += fmt.Sprintf(" %s", v)
|
||||
}
|
||||
|
||||
if command.Host != nil {
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on host %s", command.Cmd, ArgsStr, *command.Host)).Send()
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command: %s %s on host %s", command.Cmd, ArgsStr, *command.Host)).Send()
|
||||
|
||||
err := command.RemoteHost.ConnectToSSHHost(log, hosts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
defer command.RemoteHost.SshClient.Close()
|
||||
commandSession, err := command.RemoteHost.SshClient.NewSession()
|
||||
if err != nil {
|
||||
log.Err(fmt.Errorf("new ssh session: %w", err)).Send()
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
defer commandSession.Close()
|
||||
|
||||
@ -81,15 +81,12 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]s
|
||||
outMap := make(map[string]interface{})
|
||||
outMap["cmd"] = cmd
|
||||
outMap["output"] = outScanner.Text()
|
||||
if str, ok := outMap["output"].(string); ok {
|
||||
outputArr = append(outputArr, str)
|
||||
}
|
||||
log.Info().Fields(outMap).Send()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err)).Send()
|
||||
return outputArr, err
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cmdExists := command.checkCmdExists()
|
||||
@ -99,7 +96,7 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]s
|
||||
|
||||
var err error
|
||||
if command.Shell != "" {
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on local machine in %s", command.Cmd, ArgsStr, command.Shell)).Send()
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command: %s %s on local machine in %s", command.Cmd, ArgsStr, command.Shell)).Send()
|
||||
ArgsStr = fmt.Sprintf("%s %s", command.Cmd, ArgsStr)
|
||||
localCMD := exec.Command(command.Shell, "-c", ArgsStr)
|
||||
if command.Dir != nil {
|
||||
@ -121,29 +118,23 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]s
|
||||
outMap := make(map[string]interface{})
|
||||
outMap["cmd"] = command.Cmd
|
||||
outMap["output"] = outScanner.Text()
|
||||
if str, ok := outMap["output"].(string); ok {
|
||||
outputArr = append(outputArr, str)
|
||||
}
|
||||
log.Info().Fields(outMap).Send()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(fmt.Errorf("error when running cmd %s: %w", command.Cmd, err)).Send()
|
||||
return outputArr, err
|
||||
log.Error().Err(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err)).Send()
|
||||
return err
|
||||
}
|
||||
return outputArr, nil
|
||||
return nil
|
||||
}
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command %s %s on local machine", command.Cmd, ArgsStr)).Send()
|
||||
log.Info().Str("Command", fmt.Sprintf("Running command: %s %s on local machine", command.Cmd, ArgsStr)).Send()
|
||||
|
||||
localCMD := exec.Command(command.Cmd, command.Args...)
|
||||
if command.Dir != nil {
|
||||
localCMD.Dir = *command.Dir
|
||||
}
|
||||
// fmt.Printf("%v\n", envVars.env)
|
||||
|
||||
injectEnvIntoLocalCMD(envVars, localCMD, log)
|
||||
cmdOutWriters = io.MultiWriter(&cmdOutBuf)
|
||||
// fmt.Printf("%v\n", localCMD.Environ())
|
||||
|
||||
if IsCmdStdOutEnabled() {
|
||||
cmdOutWriters = io.MultiWriter(os.Stdout, &cmdOutBuf)
|
||||
@ -156,21 +147,17 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) ([]s
|
||||
outMap := make(map[string]interface{})
|
||||
outMap["cmd"] = command.Cmd
|
||||
outMap["output"] = outScanner.Text()
|
||||
if str, ok := outMap["output"].(string); ok {
|
||||
outputArr = append(outputArr, str)
|
||||
}
|
||||
log.Info().Fields(outMap).Send()
|
||||
}
|
||||
if err != nil {
|
||||
log.Error().Err(fmt.Errorf("error when running cmd %s: %w", command.Cmd, err)).Send()
|
||||
return outputArr, err
|
||||
log.Error().Err(fmt.Errorf("error when running cmd: %s: %w", command.Cmd, err)).Send()
|
||||
return err
|
||||
}
|
||||
}
|
||||
return outputArr, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *BackyConfigFile, results chan<- string) {
|
||||
|
||||
func cmdListWorker(id int, jobs <-chan *CmdList, config *BackyConfigFile, results chan<- string) {
|
||||
for list := range jobs {
|
||||
var currentCmd string
|
||||
fieldsMap := make(map[string]interface{})
|
||||
@ -186,7 +173,7 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *BackyCo
|
||||
cmdLogger := config.Logger.With().
|
||||
Str("backy-cmd", cmd).
|
||||
Logger()
|
||||
outputArr, runOutErr := cmdToRun.RunCmd(&cmdLogger, config.Hosts)
|
||||
runOutErr := cmdToRun.RunCmd(&cmdLogger, config.Hosts)
|
||||
count++
|
||||
if runOutErr != nil {
|
||||
var errMsg bytes.Buffer
|
||||
@ -196,8 +183,8 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *BackyCo
|
||||
errStruct["Command"] = currentCmd
|
||||
errStruct["Err"] = runOutErr
|
||||
errStruct["CmdsRan"] = cmdsRan
|
||||
errStruct["Output"] = outputArr
|
||||
tmpErr := msgTemps.err.Execute(&errMsg, errStruct)
|
||||
t := template.Must(template.New("error.txt").ParseFS(templates, "templates/error.txt"))
|
||||
tmpErr := t.Execute(&errMsg, errStruct)
|
||||
if tmpErr != nil {
|
||||
config.Logger.Err(tmpErr).Send()
|
||||
}
|
||||
@ -217,7 +204,8 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *BackyCo
|
||||
successStruct := make(map[string]interface{})
|
||||
successStruct["listName"] = list.Name
|
||||
successStruct["CmdsRan"] = cmdsRan
|
||||
tmpErr := msgTemps.success.Execute(&successMsg, successStruct)
|
||||
t := template.Must(template.New("success.txt").ParseFS(templates, "templates/success.txt"))
|
||||
tmpErr := t.Execute(&successMsg, successStruct)
|
||||
if tmpErr != nil {
|
||||
config.Logger.Err(tmpErr).Send()
|
||||
break
|
||||
@ -240,10 +228,6 @@ func cmdListWorker(msgTemps *msgTemplates, jobs <-chan *CmdList, config *BackyCo
|
||||
|
||||
// RunBackyConfig runs a command list from the BackyConfigFile.
|
||||
func (config *BackyConfigFile) RunBackyConfig(cron string) {
|
||||
mTemps := &msgTemplates{
|
||||
err: template.Must(template.New("error.txt").ParseFS(templates, "templates/error.txt")),
|
||||
success: template.Must(template.New("success.txt").ParseFS(templates, "templates/success.txt")),
|
||||
}
|
||||
configListsLen := len(config.CmdConfigLists)
|
||||
listChan := make(chan *CmdList, configListsLen)
|
||||
results := make(chan string)
|
||||
@ -251,7 +235,8 @@ func (config *BackyConfigFile) RunBackyConfig(cron string) {
|
||||
// This starts up 3 workers, initially blocked
|
||||
// because there are no jobs yet.
|
||||
for w := 1; w <= configListsLen; w++ {
|
||||
go cmdListWorker(mTemps, listChan, config, results)
|
||||
go cmdListWorker(w, listChan, config, results)
|
||||
|
||||
}
|
||||
|
||||
// Here we send 5 `jobs` and then `close` that
|
||||
@ -277,16 +262,11 @@ func (config *BackyConfigFile) RunBackyConfig(cron string) {
|
||||
|
||||
}
|
||||
|
||||
func (config *BackyConfigFile) ExecuteCmds(opts *BackyConfigOpts) {
|
||||
for _, cmd := range opts.executeCmds {
|
||||
cmdToRun := config.Cmds[cmd]
|
||||
cmdLogger := config.Logger.With().
|
||||
Str("backy-cmd", cmd).
|
||||
Logger()
|
||||
_, runErr := cmdToRun.RunCmd(&cmdLogger, config.Hosts)
|
||||
func (config *BackyConfigFile) ExecuteCmds() {
|
||||
for _, cmd := range config.Cmds {
|
||||
runErr := cmd.RunCmd(&config.Logger, config.Hosts)
|
||||
if runErr != nil {
|
||||
config.Logger.Err(runErr).Send()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -71,11 +71,12 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
|
||||
os.Setenv("BACKY_LOGLEVEL", Sprintf("%v", globalLvl))
|
||||
}
|
||||
|
||||
consoleLoggingDisabled := backyViper.GetBool(getLoggingKeyFromConfig("console-disabled"))
|
||||
consoleLoggingEnabled := backyViper.GetBool(getLoggingKeyFromConfig("console"))
|
||||
|
||||
os.Setenv("BACKY_CONSOLE_LOGGING", "enabled")
|
||||
// Other qualifiers can go here as well
|
||||
if consoleLoggingDisabled {
|
||||
if consoleLoggingEnabled {
|
||||
os.Setenv("BACKY_CONSOLE_LOGGING", "enabled")
|
||||
} else {
|
||||
os.Setenv("BACKY_CONSOLE_LOGGING", "")
|
||||
}
|
||||
|
||||
@ -118,10 +119,7 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
|
||||
if unmarshalErr != nil {
|
||||
panic(fmt.Errorf("error unmarshalling hosts struct: %w", unmarshalErr))
|
||||
}
|
||||
for hostConfigName, host := range backyConfigFile.Hosts {
|
||||
if host.Host == "" {
|
||||
host.Host = hostConfigName
|
||||
}
|
||||
for _, host := range backyConfigFile.Hosts {
|
||||
if host.ProxyJump != "" {
|
||||
proxyHosts := strings.Split(host.ProxyJump, ",")
|
||||
if len(proxyHosts) > 1 {
|
||||
@ -155,7 +153,6 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmdListCfg := backyViper.Sub("cmd-configs")
|
||||
unmarshalErr = cmdListCfg.Unmarshal(&backyConfigFile.CmdConfigLists)
|
||||
if unmarshalErr != nil {
|
||||
|
@ -20,7 +20,7 @@ import (
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
var PrivateKeyExtraInfoErr = errors.New("Private key may be encrypted. \nIf encrypted, make sure the password is specified correctly in the correct section: \n privatekeypassword: password (not recommended) \n privatekeypassword: env:PR_KEY_PASS \n privatekeypassword: file:/path/to/password-file. \n ")
|
||||
var ErrPrivateKeyFileFailedToOpen = errors.New("Failed to open private key file. If encrypted, make sure the password is specified.")
|
||||
var TS = strings.TrimSpace
|
||||
|
||||
// ConnectToSSHHost connects to a host by looking up the config values in the directory ~/.ssh/config
|
||||
@ -38,6 +38,16 @@ func (remoteConfig *Host) ConnectToSSHHost(log *zerolog.Logger, hosts map[string
|
||||
if TS(remoteConfig.ConfigFilePath) == "" {
|
||||
remoteConfig.useDefaultConfig = true
|
||||
}
|
||||
|
||||
if remoteConfig.ProxyHost != nil {
|
||||
for _, proxyHost := range remoteConfig.ProxyHost {
|
||||
log.Info().Msgf("Proxy Host %s", proxyHost.Host)
|
||||
err := proxyHost.GetProxyJumpConfig(hosts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
khPath, khPathErr := GetKnownHosts(remoteConfig.KnownHostsFile)
|
||||
|
||||
if khPathErr != nil {
|
||||
@ -67,20 +77,6 @@ func (remoteConfig *Host) ConnectToSSHHost(log *zerolog.Logger, hosts map[string
|
||||
if decodeErr != nil {
|
||||
return decodeErr
|
||||
}
|
||||
|
||||
err := remoteConfig.GetProxyJumpFromConfig(hosts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if remoteConfig.ProxyHost != nil {
|
||||
for _, proxyHost := range remoteConfig.ProxyHost {
|
||||
log.Info().Msgf("Proxy Host %s", proxyHost.Host)
|
||||
err := proxyHost.GetProxyJumpConfig(hosts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
remoteConfig.ClientConfig.Timeout = time.Second * 30
|
||||
remoteConfig.GetPrivateKeyFileFromConfig()
|
||||
remoteConfig.GetPort()
|
||||
@ -90,7 +86,7 @@ func (remoteConfig *Host) ConnectToSSHHost(log *zerolog.Logger, hosts map[string
|
||||
if remoteConfig.HostName == "" {
|
||||
return errors.New("No hostname found or specified")
|
||||
}
|
||||
err = remoteConfig.GetAuthMethods()
|
||||
err := remoteConfig.GetAuthMethods()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -150,13 +146,13 @@ func (remoteHost *Host) GetAuthMethods() error {
|
||||
if remoteHost.PrivateKeyPassword == "" {
|
||||
signer, err = ssh.ParsePrivateKey(privateKey)
|
||||
if err != nil {
|
||||
return errors.Errorf("Failed to open private key file %s: %v \n\n %v", remoteHost.PrivateKeyPath, err, PrivateKeyExtraInfoErr)
|
||||
return ErrPrivateKeyFileFailedToOpen
|
||||
}
|
||||
remoteHost.ClientConfig.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
||||
} else {
|
||||
signer, err = ssh.ParsePrivateKeyWithPassphrase(privateKey, []byte(remoteHost.PrivateKeyPassword))
|
||||
if err != nil {
|
||||
return errors.Errorf("Failed to open private key file %s: %v \n\n %v", remoteHost.PrivateKeyPath, err, PrivateKeyExtraInfoErr)
|
||||
return err
|
||||
}
|
||||
remoteHost.ClientConfig.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
||||
}
|
||||
@ -197,7 +193,7 @@ func (remoteHost *Host) GetPrivateKeyFileFromConfig() {
|
||||
// GetPort checks if the port from the config file is 0
|
||||
// If it is the port is searched in the SSH config file(s)
|
||||
func (remoteHost *Host) GetPort() {
|
||||
port := fmt.Sprintf("%d", remoteHost.Port)
|
||||
port := fmt.Sprintf("%v", remoteHost.Port)
|
||||
// port specifed?
|
||||
if port == "0" {
|
||||
port, _ = remoteHost.SSHConfigFile.SshConfigFile.Get(remoteHost.Host, "Port")
|
||||
@ -208,16 +204,16 @@ func (remoteHost *Host) GetPort() {
|
||||
}
|
||||
}
|
||||
}
|
||||
portNum, _ := strconv.ParseUint(port, 10, 16)
|
||||
portNum, _ := strconv.ParseUint(port, 10, 32)
|
||||
remoteHost.Port = uint16(portNum)
|
||||
}
|
||||
|
||||
func (remoteHost *Host) CombineHostNameWithPort() {
|
||||
port := fmt.Sprintf(":%d", remoteHost.Port)
|
||||
if strings.HasSuffix(remoteHost.HostName, port) {
|
||||
port := fmt.Sprintf(":%v", remoteHost.Port)
|
||||
if strings.Contains(remoteHost.HostName, port) {
|
||||
return
|
||||
}
|
||||
remoteHost.HostName = fmt.Sprintf("%s:%d", remoteHost.HostName, remoteHost.Port)
|
||||
remoteHost.HostName = fmt.Sprintf("%s:%v", remoteHost.HostName, remoteHost.Port)
|
||||
}
|
||||
|
||||
func (remoteHost *Host) GetHostName() {
|
||||
@ -274,7 +270,7 @@ func GetPrivateKeyPassword(key string) (string, error) {
|
||||
privKeyPassFilePath, _ = resolveDir(privKeyPassFilePath)
|
||||
keyFile, keyFileErr := os.Open(privKeyPassFilePath)
|
||||
if keyFileErr != nil {
|
||||
return "", errors.Errorf("Private key password file %s failed to open. \n Make sure it is accessible and correct.", privKeyPassFilePath)
|
||||
return "", ErrPrivateKeyFileFailedToOpen
|
||||
}
|
||||
passwordScanner := bufio.NewScanner(keyFile)
|
||||
for passwordScanner.Scan() {
|
||||
@ -336,12 +332,10 @@ func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error {
|
||||
if proxyHostFound {
|
||||
remoteConfig.ProxyHost = append(remoteConfig.ProxyHost, proxyHost)
|
||||
} else {
|
||||
if proxyJump != "" {
|
||||
newProxy := &Host{Host: proxyJump}
|
||||
remoteConfig.ProxyHost = append(remoteConfig.ProxyHost, newProxy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -350,6 +344,7 @@ func (remoteConfig *Host) GetProxyJumpConfig(hosts map[string]*Host) error {
|
||||
remoteConfig.useDefaultConfig = true
|
||||
}
|
||||
|
||||
// log.Info().Msgf("Proxy Host %s", remoteConfig.ProxyHost[0].Host)
|
||||
khPath, khPathErr := GetKnownHosts(remoteConfig.KnownHostsFile)
|
||||
|
||||
if khPathErr != nil {
|
||||
|
@ -1,12 +1,8 @@
|
||||
Command list {{.listName }} failed on running {{.Command}}.
|
||||
|
||||
{{ if .Err }} The error was {{ .Err }}{{ end }}
|
||||
The error was {{ .Err }}
|
||||
|
||||
{{ if .Output }} The output was {{- range .Output}} {{.}} {{end}} {{end}}
|
||||
|
||||
{{ if .CmdsRan }}
|
||||
The following commands ran:
|
||||
{{- range .CmdsRan}}
|
||||
- {{. -}}
|
||||
{{end}}
|
||||
{{ end }}
|
@ -1,8 +1,10 @@
|
||||
// types.go
|
||||
// Copyright (C) Andrew Woodlee 2023
|
||||
// License: Apache-2.0
|
||||
package backy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/kevinburke/ssh_config"
|
||||
"github.com/nikoksr/notify"
|
||||
@ -13,14 +15,12 @@ import (
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type (
|
||||
CmdConfigSchema struct {
|
||||
type CmdConfigSchema struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
CmdList []string `bson:"command-list,omitempty"`
|
||||
Name string `bson:"name,omitempty"`
|
||||
}
|
||||
|
||||
CmdSchema struct {
|
||||
type CmdSchema struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"`
|
||||
Cmd string `bson:"cmd,omitempty"`
|
||||
Args []string `bson:"args,omitempty"`
|
||||
@ -28,14 +28,14 @@ type (
|
||||
Dir string `bson:"dir,omitempty"`
|
||||
}
|
||||
|
||||
Schemas struct {
|
||||
type Schemas struct {
|
||||
CmdConfigSchema
|
||||
CmdSchema
|
||||
}
|
||||
|
||||
// Host defines a host to which to connect.
|
||||
// If not provided, the values will be looked up in the default ssh config files
|
||||
Host struct {
|
||||
type Host struct {
|
||||
ConfigFilePath string `yaml:"configfilepath,omitempty"`
|
||||
Host string `yaml:"host,omitempty"`
|
||||
HostName string `yaml:"hostname,omitempty"`
|
||||
@ -48,19 +48,22 @@ type (
|
||||
Password string `yaml:"password,omitempty"`
|
||||
PrivateKeyPath string `yaml:"privatekeypath,omitempty"`
|
||||
PrivateKeyPassword string `yaml:"privatekeypassword,omitempty"`
|
||||
UseConfigFiles bool `yaml:"use_config_files,omitempty"`
|
||||
useDefaultConfig bool
|
||||
User string `yaml:"user,omitempty"`
|
||||
// ProxyHost holds the configuration for a ProxyJump host
|
||||
ProxyHost []*Host
|
||||
// CertPath string `yaml:"cert_path,omitempty"`
|
||||
}
|
||||
|
||||
sshConfigFile struct {
|
||||
type sshConfigFile struct {
|
||||
SshConfigFile *ssh_config.Config
|
||||
DefaultUserSettings *ssh_config.UserSettings
|
||||
}
|
||||
|
||||
Command struct {
|
||||
type Command struct {
|
||||
// Remote bool `yaml:"remote,omitempty"`
|
||||
|
||||
Output BackyCommandOutput `yaml:"-"`
|
||||
|
||||
// command to run
|
||||
Cmd string `yaml:"cmd"`
|
||||
@ -77,7 +80,7 @@ type (
|
||||
RemoteHost *Host `yaml:"-"`
|
||||
|
||||
// Args is an array that holds the arguments to cmd
|
||||
Args []string `yaml:"args,omitempty"`
|
||||
Args []string `yaml:"Args,omitempty"`
|
||||
|
||||
/*
|
||||
Dir specifies a directory in which to run the command.
|
||||
@ -92,9 +95,9 @@ type (
|
||||
Environment []string `yaml:"environment,omitempty"`
|
||||
}
|
||||
|
||||
BackyOptionFunc func(*BackyConfigOpts)
|
||||
type BackyOptionFunc func(*BackyConfigOpts)
|
||||
|
||||
CmdList struct {
|
||||
type CmdList struct {
|
||||
Name string `yaml:"name,omitempty"`
|
||||
Cron string `yaml:"cron,omitempty"`
|
||||
Order []string `yaml:"order,omitempty"`
|
||||
@ -104,7 +107,7 @@ type (
|
||||
// NotifyConfig map[string]*notify.Notify
|
||||
}
|
||||
|
||||
BackyConfigFile struct {
|
||||
type BackyConfigFile struct {
|
||||
|
||||
// Cmds holds the commands for a list.
|
||||
// Key is the name of the command,
|
||||
@ -124,7 +127,7 @@ type (
|
||||
Logger zerolog.Logger
|
||||
}
|
||||
|
||||
BackyConfigOpts struct {
|
||||
type BackyConfigOpts struct {
|
||||
// Global log level
|
||||
BackyLogLvl *string
|
||||
// Holds config file
|
||||
@ -148,23 +151,22 @@ type (
|
||||
viper *viper.Viper
|
||||
}
|
||||
|
||||
NotificationsConfig struct {
|
||||
type NotificationsConfig struct {
|
||||
Config *viper.Viper
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
CmdOutput struct {
|
||||
type CmdOutput struct {
|
||||
Err error
|
||||
Output bytes.Buffer
|
||||
}
|
||||
|
||||
environmentVars struct {
|
||||
type BackyCommandOutput interface {
|
||||
Error() error
|
||||
GetOutput() CmdOutput
|
||||
}
|
||||
|
||||
type environmentVars struct {
|
||||
file string
|
||||
env []string
|
||||
}
|
||||
|
||||
msgTemplates struct {
|
||||
success *template.Template
|
||||
err *template.Template
|
||||
}
|
||||
)
|
||||
|
@ -44,15 +44,16 @@ func injectEnvIntoSSH(envVarsToInject environmentVars, process *ssh.Session, log
|
||||
}
|
||||
|
||||
errEnvFile:
|
||||
// fmt.Printf("%v", envVarsToInject.env)
|
||||
if len(envVarsToInject.env) > 0 {
|
||||
for _, envVal := range envVarsToInject.env {
|
||||
// don't append env Vars for Backy
|
||||
if strings.Contains(envVal, "=") {
|
||||
if strings.Contains(envVal, "=") && !strings.HasPrefix(envVal, "BACKY_") {
|
||||
envVarArr := strings.Split(envVal, "=")
|
||||
process.Setenv(envVarArr[0], envVarArr[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func injectEnvIntoLocalCMD(envVarsToInject environmentVars, process *exec.Cmd, log *zerolog.Logger) {
|
||||
if envVarsToInject.file != "" {
|
||||
@ -74,13 +75,14 @@ func injectEnvIntoLocalCMD(envVarsToInject environmentVars, process *exec.Cmd, l
|
||||
|
||||
}
|
||||
errEnvFile:
|
||||
|
||||
if len(envVarsToInject.env) > 0 {
|
||||
for _, envVal := range envVarsToInject.env {
|
||||
if strings.Contains(envVal, "=") {
|
||||
process.Env = append(process.Env, envVal)
|
||||
}
|
||||
}
|
||||
process.Env = append(process.Env, os.Environ()...)
|
||||
}
|
||||
envVarsToInject.env = append(envVarsToInject.env, os.Environ()...)
|
||||
}
|
||||
|
||||
func (cmd *Command) checkCmdExists() bool {
|
||||
@ -102,8 +104,9 @@ func CheckConfigValues(config *viper.Viper) {
|
||||
for _, key := range requiredKeys {
|
||||
isKeySet := config.IsSet(key)
|
||||
if !isKeySet {
|
||||
logging.ExitWithMSG(Sprintf("Config key %s is not defined in %s. Please make sure this value is set and has the appropriate keys set.", key, config.ConfigFileUsed()), 1, nil)
|
||||
logging.ExitWithMSG(Sprintf("Config key %s is not defined in %s", key, config.ConfigFileUsed()), 1, nil)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,6 +117,8 @@ func testFile(c string) error {
|
||||
if errors.Is(fileOpenErr, os.ErrNotExist) {
|
||||
return fileOpenErr
|
||||
}
|
||||
|
||||
fmt.Printf("%s\t\t%v", c, fileOpenErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
8
release
Executable file → Normal file
8
release
Executable file → Normal file
@ -1,6 +1,4 @@
|
||||
#!/bin/bash
|
||||
export GORELEASER_CURRENT_TAG="$(go run backy.go version)"
|
||||
git tag "$(go run backy.go version)"
|
||||
git push all
|
||||
git push all --tags
|
||||
goreleaser release -f .goreleaser/gitea.yml --clean --release-notes=".changes/$(go run backy.go version).md"
|
||||
git tag "$(svu next)"
|
||||
git push --tags
|
||||
goreleaser --rm-dist
|
Loading…
Reference in New Issue
Block a user