Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gobble/Rig refactor #25

Draft
wants to merge 28 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
689234b
feat: initial custom logger support
kaplan-michael Apr 23, 2024
e2c0ebe
feat: initial support for rig
kaplan-michael Apr 24, 2024
ab9c156
refactor: refactor: task execution with rig
kaplan-michael Apr 28, 2024
b33bb00
add: rig example
kaplan-michael Apr 28, 2024
ca4b6fc
refactor: echo task & ping command
kaplan-michael Apr 28, 2024
0283456
refactor: task: authorized_key use new structure
kaplan-michael Apr 29, 2024
002957d
refactor: task: chmod use new structure
kaplan-michael Apr 29, 2024
ed278b3
refactor: task: replace_string_in_local_file use new structure
kaplan-michael Apr 29, 2024
8778760
refactor: task: print use new structure
kaplan-michael Apr 29, 2024
c033a3d
refactor: task: template use new structure
kaplan-michael Apr 29, 2024
48e8619
add: task: pkg_manager
kaplan-michael Apr 29, 2024
7c17b9d
refactor: ping: move ping into a new task structure
kaplan-michael Apr 29, 2024
fa3afee
refactor: host: task dispatch & fs init
kaplan-michael Apr 29, 2024
641145a
refactor: task: cp use rig funcs & new structure
kaplan-michael Apr 29, 2024
558cce8
refactor: task: template adapt to cp task
kaplan-michael Apr 29, 2024
e56280e
bump deps & use rig fork
kaplan-michael Apr 29, 2024
dd8b61c
support recursive copying
kaplan-michael May 4, 2024
6136c2e
fix: merge task & host vars in tasks
kaplan-michael May 4, 2024
a46d46e
refactor: split play filtering
kaplan-michael May 5, 2024
febdbaa
feat: threadsafe styled printing
kaplan-michael May 5, 2024
fb7351d
fix: support password with go ssh
kaplan-michael Jun 25, 2024
dc52954
feat: implement basic sudo password support
kaplan-michael Jun 25, 2024
724e72d
fix: better printing and depreciation warnings
kaplan-michael Jun 25, 2024
f74ea6a
chore: cleanup old unused code
kaplan-michael Jun 25, 2024
7e7cca7
fix: proper support for sudo in tasks
kaplan-michael Jul 1, 2024
d0cc23f
fix: deprecated printing
kaplan-michael Jul 1, 2024
37a9adc
fix: linter warnings
kaplan-michael Jul 3, 2024
18edad1
feat: vendor dependencies
kaplan-michael Jul 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
linters-settings:
revive:
confidence: 0.5
gocyclo:
min-complexity: 15

#https://golangci-lint.run/usage/linters/
linters:
disable-all: true
enable:
- govet # Vet examines Go source code and reports suspicious constructs
- errcheck # Errcheck checks for unchecked errors
- staticcheck # Staticcheck is a state of the art linter
- unused # Checks Go code for unused constants, variables, functions and types
- gofmt # Gofmt checks whether code was gofmt-ed
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
- revive # Golint is a linter for Go source code
- ineffassign # Detects when assignments to existing variables are not used
- misspell # Finds commonly misspelled English words in comments
- unconvert # Remove unnecessary type conversions

run:
timeout: 5m
modules-download-mode: readonly

119 changes: 32 additions & 87 deletions cmd/ping/ping.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,63 @@
package ping

import (
"bufio"
"fmt"
"io"
"log"
"os"

"github.com/mohae/deepcopy"
"github.com/sikalabs/gobble/pkg/host"
"github.com/sikalabs/gobble/pkg/logger"
"github.com/sikalabs/gobble/pkg/run"
ping "github.com/sikalabs/gobble/pkg/task/lib/ping"

"github.com/sikalabs/gobble/cmd/root"
"github.com/sikalabs/gobble/pkg/config"
"github.com/sikalabs/gobble/pkg/libtask"
"github.com/sikalabs/gobble/pkg/task/lib/echo"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var FlagConfigFilePath string

// TODO Reimplement ping as a task

var Cmd = &cobra.Command{
Use: "ping",
Short: "Ping all hosts from gobblefile",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
shellReturnCode := 0
conf, err := readConfigFile(FlagConfigFilePath)
conf, err := config.ReadConfigFile(FlagConfigFilePath)
if err != nil {
log.Fatalln(err)
logger.Log.Fatal(err)
}

if conf.Meta.SchemaVersion != 3 {
log.Fatalln(fmt.Errorf("unsupported schema version: %d", conf.Meta.SchemaVersion))
if conf.Meta.SchemaVersion != 4 {
logger.Log.Fatalf("unsupported schema version: %d", conf.Meta.SchemaVersion)
}

conf.AllHosts = conf.Hosts

for hostAliasName, hostAliases := range conf.HostsAliases {
for _, hostAlias := range hostAliases {
conf.AllHosts[hostAliasName] = append(conf.AllHosts[hostAliasName], conf.Hosts[hostAlias]...)
}
//Initialize host connections
targets, err := host.InitializeHosts(conf.RigHosts, conf.HostsAliases)
if err != nil {
logger.Log.Fatal(err)
}

allHosts := map[string]config.ConfigHost{}

for _, hosts := range conf.AllHosts {
for _, host := range hosts {
allHosts[host.SSHTarget] = host
}
task := ping.Task{
BaseTask: libtask.BaseTask{
Name: "ping all hosts",
},
}
ti := libtask.TaskInput{
Config: conf,
Sudo: false,
Vars: conf.Global.Vars,
Dry: false,
}

for _, host := range allHosts {
ti := libtask.TaskInput{
SSHTarget: host.SSHTarget,
SSHPassword: host.SSHPassword,
SSHOptions: host.SSHOptions,
SudoPassword: host.SudoPassword,
Config: conf,
NoStrictHostKeyChecking: conf.Global.NoStrictHostKeyChecking,
Sudo: false,
Vars: mergeMaps(conf.Global.Vars, host.Vars),
Dry: false,
Quiet: false,
}
taskParams := echo.TaskEcho{
Message: "ping",
}

fmt.Printf("Ping %s using SSH ...", host.SSHTarget)
out := echo.Run(ti, taskParams)
isOK := out.Error == nil
if isOK {
fmt.Printf(" OK\n")
} else {
fmt.Printf(" ERR\n")
shellReturnCode = 1
}
out := run.DispatchTask(&task, ti, targets)
isOK := out.Error == nil
if isOK {
logger.Log.Printf("Hosts are OK")
} else {
logger.Log.Errorf("Hosts are not reachable")
shellReturnCode = 1
}
os.Exit(shellReturnCode)
},
Expand All @@ -90,41 +73,3 @@ func init() {
"Path to config file, \"-\" for STDIN",
)
}

func readConfigFile(configFilePath string) (config.Config, error) {
var buf []byte
var err error
c := config.Config{}

if configFilePath == "-" {
// Read from stdin
buf, err = io.ReadAll(bufio.NewReader(os.Stdin))
if err != nil {
return c, err
}
} else {
// Read from file
buf, err = os.ReadFile(configFilePath)
if err != nil {
return c, err
}
}

_ = yaml.Unmarshal(buf, &c)
if err != nil {
return c, err
}

return c, nil
}

func mergeMaps(m1, m2 map[string]interface{}) map[string]interface{} {
if m1 == nil {
m1 = make(map[string]interface{})
}
deepCopyM1 := deepcopy.Copy(m1).(map[string]interface{})
for k, v := range m2 {
deepCopyM1[k] = v
}
return deepCopyM1
}
19 changes: 18 additions & 1 deletion cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@

package root

import (
"github.com/sikalabs/gobble/pkg/logger"
"github.com/sikalabs/gobble/pkg/printer"
"github.com/sikalabs/gobble/version"
"github.com/spf13/cobra"
)

var FlagVerbosity int
var Cmd = &cobra.Command{
Use: "gobble",
Short: "gobble, " + version.Version,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Initialize the logger with the verbosity flag
logger.InitCharmLogger(FlagVerbosity)
printer.InitPrinter(FlagVerbosity)
},
}

func init() {
// Add a persistent flag for verbosity to the root command
Cmd.PersistentFlags().IntVarP(&FlagVerbosity,
"verbosity",
"v",
1,
"Set the logging verbosity (1=error, 2=warn, 3=info, 4=debug)")
}
5 changes: 2 additions & 3 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package version

import (
"log"

"github.com/sikalabs/gobble/cmd/root"
"github.com/sikalabs/gobble/pkg/logger"
"github.com/sikalabs/gobble/pkg/run"
"github.com/spf13/cobra"
)
Expand All @@ -22,7 +21,7 @@ var Cmd = &cobra.Command{
Run: func(c *cobra.Command, args []string) {
err := run.RunFromFile(FlagConfigFilePath, FlagDryRun, FlagQuietOutput, FlagOnlyTags, FlagSkipTags)
if err != nil {
log.Fatalln(err)
logger.Log.Fatal(err)
}
},
}
Expand Down
3 changes: 0 additions & 3 deletions examples/config/fail/gobblefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ global:
hosts:
all:
- ssh_target: localhost
vars:
ip: localhost
msg: Host Gobble!
plays:
- name: Fail on purpose
hosts: [all]
Expand Down
36 changes: 36 additions & 0 deletions examples/config/rig/gobblefile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
meta:
schema_version: 4
global:
no_strict_host_key_checking: true
vars:
prefix: '[gobble]'
msg: Global Gobble!
rig_hosts:
remote_ssh:
- ssh:
address: localhost
user: mkaplan
keyPath: ~/.ssh/id_ed25519
remote_openssh:
- openssh:
address: localhost
user: mkaplan
keyPath: ~/.ssh/id_ed25519
local_local:
- local: true

hosts_aliases:
all:
- remote_ssh
- remote_openssh
- local_local

plays:
- name: touch file
hosts: [all]
sudo: false
tags: [setup]
tasks:
- name: touch file
command:
cmd: echo "Simulated stdout message" >&1 && echo "Simulated stderr message" >&2 && exit 1
51 changes: 46 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
module github.com/sikalabs/gobble

go 1.19
go 1.22.0

toolchain go1.22.2

require (
github.com/charmbracelet/lipgloss v0.11.0
github.com/charmbracelet/log v0.4.0
github.com/k0sproject/rig/v2 v2.0.0-alpha.2
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/spf13/cobra v1.2.1
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30
gopkg.in/yaml.v2 v2.4.0
github.com/muesli/termenv v0.15.2
github.com/spf13/cobra v1.8.1
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
gopkg.in/yaml.v3 v3.0.1
)

replace github.com/k0sproject/rig/v2 v2.0.0-alpha.2 => github.com/sikalabs/rig/v2 v2.0.0-20240531164001-ebe4d69390ff

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bodgit/ntlmssp v0.0.0-20240506230425-31973bb52d9b // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/goidentity/v6 v6.0.1 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
github.com/masterzen/winrm v0.0.0-20240702205601-3fad6e106085 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
Loading