Skip to content

Commit

Permalink
Allow duplicate hosts (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
alajmo authored Jul 11, 2022
1 parent b9798d0 commit 47cdac6
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 92 deletions.
56 changes: 3 additions & 53 deletions core/dao/import_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"github.com/alajmo/sake/core"
Expand Down Expand Up @@ -252,14 +251,11 @@ func (c *ConfigYAML) parseConfig() (Config, error) {
config.KnownHostsFile = *cr.KnownHostsFile
}

// Check duplicate hosts
hostErr := checkDuplicateHosts(config.Servers)

// Check duplicate imports
importErr := checkDuplicateImports(cr.Imports)

// Concat errors
errString := concatErrors(hostErr, importErr, cr, &importCycles, &taskCycles)
errString := concatErrors(importErr, cr, &importCycles, &taskCycles)

if errString != "" {
return config, &core.ConfigErr{Msg: errString}
Expand All @@ -268,8 +264,8 @@ func (c *ConfigYAML) parseConfig() (Config, error) {
return config, nil
}

func concatErrors(hostErr string, importErr string, cr ConfigResources, importCycles *[]NodeLink, taskCycles *[]TaskLink) string {
errString := fmt.Sprintf("%s%s", hostErr, importErr)
func concatErrors(importErr string, cr ConfigResources, importCycles *[]NodeLink, taskCycles *[]TaskLink) string {
errString := importErr

if len(*importCycles) > 0 {
err := &FoundCyclicDependency{Cycles: *importCycles}
Expand Down Expand Up @@ -612,52 +608,6 @@ func dfsImport(n *Node, m map[string]*Node, cycles *[]NodeLink, cr *ConfigResour
n.Visited = true
}

type FoundDuplicateHosts struct {
host string
servers []string
}

func (c *FoundDuplicateHosts) Error() string {
var msg string

var errPrefix = text.FgRed.Sprintf("error")
var ptrPrefix = text.FgBlue.Sprintf("-->")
msg = fmt.Sprintf("%s: %s`%s`%s\n %s", errPrefix, "found duplicate host ", c.host, " for the following servers", ptrPrefix)
msg += fmt.Sprintf(" %s\n", c.servers[0])
for i, s := range c.servers[1:] {
if i < len(c.servers[1:])-1 {
msg += fmt.Sprintf(" %s\n", s)
} else {
msg += fmt.Sprintf(" %s", s)
}
}

return msg
}

func checkDuplicateHosts(servers []Server) string {
hostServer := make(map[string][]string)
for _, s := range servers {
hostServer[s.Host] = append(hostServer[s.Host], s.Name)
}

keys := make([]string, 0, len(hostServer))
for k := range hostServer {
keys = append(keys, k)
}
sort.Strings(keys)

var configErr string
for _, k := range keys {
if len(hostServer[k]) > 1 {
err := &FoundDuplicateHosts{host: k, servers: hostServer[k]}
configErr = fmt.Sprintf("%s%s\n\n", configErr, err.Error())
}
}

return configErr
}

type FoundDuplicateImports struct {
imports []string
}
Expand Down
11 changes: 0 additions & 11 deletions core/dao/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,6 @@ func (c Config) FilterServers(
return finalServers, nil
}

func (c Config) GetServerHosts() []string {
hosts := []string{}
for _, server := range c.Servers {
if server.Host != "" {
hosts = append(hosts, server.Host)
}
}

return hosts
}

func (c Config) GetServer(name string) (*Server, error) {
for _, server := range c.Servers {
if name == server.Name {
Expand Down
2 changes: 1 addition & 1 deletion core/run/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Client interface {
Stderr() io.Reader
Stdout() io.Reader
Signal(os.Signal) error
GetHost() string
GetName() string
}

type ErrConnect struct {
Expand Down
7 changes: 4 additions & 3 deletions core/run/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (run *Run) RunTask(
continue
}

_, reachable := run.RemoteClients[server.Host]
_, reachable := run.RemoteClients[server.Name]
if reachable {
reachableServers = append(reachableServers, server)
}
Expand Down Expand Up @@ -201,6 +201,7 @@ func (run *Run) SetClients(
defer wg.Done()

local := &LocalhostClient{
Name: server.Name,
User: server.User,
Host: server.Host,
}
Expand Down Expand Up @@ -270,9 +271,9 @@ func (run *Run) SetClients(
for client := range clientCh {
switch client.(type) {
case *SSHClient:
remoteClients[client.GetHost()] = client
remoteClients[client.GetName()] = client
case *LocalhostClient:
localCLients[client.GetHost()] = client
localCLients[client.GetName()] = client
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/run/localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

// Client is a wrapper over the SSH connection/sessions.
type LocalhostClient struct {
Name string
User string
Host string

Expand Down Expand Up @@ -112,6 +113,6 @@ func (c *LocalhostClient) Signal(sig os.Signal) error {
return c.cmd.Process.Signal(sig)
}

func (c *LocalhostClient) GetHost() string {
return c.Host
func (c *LocalhostClient) GetName() string {
return c.Name
}
4 changes: 2 additions & 2 deletions core/run/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ func (c *SSHClient) Signal(sig os.Signal) error {
}
}

func (c *SSHClient) GetHost() string {
return c.Host
func (c *SSHClient) GetName() string {
return c.Name
}

// VerifyHost validates that the host is found in known_hosts file
Expand Down
4 changes: 2 additions & 2 deletions core/run/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func (run *Run) TableWork(rIndex int, dryRun bool, data dao.TableOutput, dataMut
combinedEnvs := dao.MergeEnvs(cmd.Envs, server.Envs)
var client Client
if cmd.Local || server.Local {
client = run.LocalClients[server.Host]
client = run.LocalClients[server.Name]
} else {
client = run.RemoteClients[server.Host]
client = run.RemoteClients[server.Name]
}

shell := dao.SelectFirstNonEmpty(cmd.Shell, server.Shell, config.Shell)
Expand Down
6 changes: 3 additions & 3 deletions core/run/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (run *Run) TextWork(rIndex int, prefixMaxLen int, dryRun bool) error {
config := run.Config
task := run.Task
server := run.Servers[rIndex]
prefix := getPrefixer(run.LocalClients[server.Host], rIndex, prefixMaxLen, task.Theme.Text, task.Spec.Parallel)
prefix := getPrefixer(run.LocalClients[server.Name], rIndex, prefixMaxLen, task.Theme.Text, task.Spec.Parallel)

numTasks := len(task.Tasks)

Expand All @@ -72,9 +72,9 @@ func (run *Run) TextWork(rIndex int, prefixMaxLen int, dryRun bool) error {
var client Client
combinedEnvs := dao.MergeEnvs(cmd.Envs, server.Envs)
if cmd.Local || server.Local {
client = run.LocalClients[server.Host]
client = run.LocalClients[server.Name]
} else {
client = run.RemoteClients[server.Host]
client = run.RemoteClients[server.Name]
}

shell := dao.SelectFirstNonEmpty(cmd.Shell, server.Shell, config.Shell)
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Fix local WorkDir when it's not set
- Allow duplicate hosts

## 0.10.1

Expand Down
38 changes: 23 additions & 15 deletions test/playground/sake.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ servers:
tags: [remote, pi]
work_dir: /home/samir

pihole:
pihole-1:
desc: runs pihole
host: 172.24.2.3
tags: [remote, pi]
tags: [remote, pi, pihole]
user: test
identity_file: ../keys/id_ed25519_pem_no

pihole-2:
desc: runs pihole
host: 172.24.2.3
tags: [remote, pi, pihole]
user: test
identity_file: ../keys/id_ed25519_pem_no

ip6:
desc: server-9
host: 2001:3984:3989::10
tags: [remote,reachable]
tags: [remote, reachable]
user: test
password: test

Expand All @@ -38,11 +47,11 @@ specs:
output: text

info:
output: table
parallel: true
ignore_errors: true
ignore_unreachable: true
any_errors_fatal: false
output: table
parallel: true
ignore_errors: true
ignore_unreachable: true
any_errors_fatal: false

env:
VERSION: v0.1.0
Expand Down Expand Up @@ -162,7 +171,7 @@ tasks:
script:
desc: run local script on remote server
env:
file: ""
file: ''
local: true
cmd: |
file=$(basename $file)
Expand All @@ -185,7 +194,7 @@ tasks:
desc: upload file or directory
spec: info
env:
src: ""
src: ''
dest: /tmp
local: true
cmd: rsync --recursive --verbose --archive --update $src $SAKE_SERVER_HOST:$dest
Expand All @@ -194,21 +203,20 @@ tasks:
desc: download file or directory
spec: info
env:
src: ""
dest: ""
src: ''
dest: ''
local: true
cmd: rsync --recursive --verbose --archive --update $SAKE_SERVER_HOST:$src $dest

docker-exec:
desc: attach to docker container
env:
NAME: ""
NAME: ''
tty: true
cmd: ssh -t $SAKE_SERVER_USER@$SAKE_SERVER_HOST "docker exec -it $NAME bash"

attach-mealie:
desc: attach to mealie
env:
NAME: "mealie"
NAME: 'mealie'
task: docker-exec

0 comments on commit 47cdac6

Please sign in to comment.