Skip to content

Commit

Permalink
Fix missing hostname (#185)
Browse files Browse the repository at this point in the history
* Fix empty name fields if used without an inventory

Lack of populated destination's name prevented the expansion of SPOT_HOST_NAME var.

* add test for suppressed dup info in logger prefix with matched addr and name
  • Loading branch information
umputun committed Mar 24, 2024
1 parent 07eb87a commit b944985
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bin/
.bin/
*.gpg
private-key.asc
spot-private.yml
spot-private*.yml
spot-inventory-private.yml
spot-inventory-private2.yml
site/public/
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func TestTargetHosts(t *testing.T) {
},
{
"target as single host with port", "host4.example.com:2222", nil,
[]Destination{{Host: "host4.example.com", Port: 2222, User: "defaultuser"}},
[]Destination{{Host: "host4.example.com", Name: "host4.example.com", Port: 2222, User: "defaultuser"}},
false,
},
{
Expand All @@ -438,17 +438,17 @@ func TestTargetHosts(t *testing.T) {
},
{
"target as single host address with user", "user2@host5.example.com", nil,
[]Destination{{Host: "host5.example.com", Port: 22, User: "user2"}},
[]Destination{{Host: "host5.example.com", Name: "host5.example.com", Port: 22, User: "user2"}},
false,
},
{
"target as single host address, port and user", "user2@host5.example.com:2345", nil,
[]Destination{{Host: "host5.example.com", Port: 2345, User: "user2"}},
[]Destination{{Host: "host5.example.com", Name: "host5.example.com", Port: 2345, User: "user2"}},
false,
},
{"invalid host:port format", "host5.example.com:invalid", nil, nil, true},
{"random host without a port", "host5.example.com", nil,
[]Destination{{Host: "host5.example.com", Port: 22, User: "defaultuser"}},
[]Destination{{Host: "host5.example.com", Name: "host5.example.com", Port: 22, User: "defaultuser"}},
false,
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func (tg *targetExtractor) destinationsFromInventory(name string) ([]Destination
return nil, fmt.Errorf("can't parse port %s: %w", elems[1], err)
}
log.Printf("[DEBUG] target %q used as host:port %s:%d", name, elems[0], port)
return []Destination{{Host: elems[0], Port: port, User: user}}, nil
return []Destination{{Host: elems[0], Name: elems[0], Port: port, User: user}}, nil
}

// we have no idea what this is, use it as host:22
log.Printf("[DEBUG] target %q used as host:22 %s", name, name)
return []Destination{{Host: name, Port: 22, User: user}}, nil
return []Destination{{Host: name, Name: name, Port: 22, User: user}}, nil
}
8 changes: 4 additions & 4 deletions pkg/config/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,28 +225,28 @@ func TestHostAddressParsing(t *testing.T) {
name: "address only, default port",
input: "192.168.1.1",
user: "user",
expected: Destination{Host: "192.168.1.1", Port: 22, User: "user"},
expected: Destination{Host: "192.168.1.1", Name: "192.168.1.1", Port: 22, User: "user"},
err: false,
},
{
name: "user and address only, default port",
input: "john@192.168.1.1",
user: "user",
expected: Destination{Host: "192.168.1.1", Port: 22, User: "john"},
expected: Destination{Host: "192.168.1.1", Name: "192.168.1.1", Port: 22, User: "john"},
err: false,
},
{
name: "port specified",
input: "192.168.1.1:2222",
user: "user",
expected: Destination{Host: "192.168.1.1", Port: 2222, User: "user"},
expected: Destination{Host: "192.168.1.1", Name: "192.168.1.1", Port: 2222, User: "user"},
err: false,
},
{
name: "user and port specified",
input: "john@192.168.1.1:2222",
user: "user",
expected: Destination{Host: "192.168.1.1", Port: 2222, User: "john"},
expected: Destination{Host: "192.168.1.1", Name: "192.168.1.1", Port: 2222, User: "john"},
err: false,
},
{
Expand Down
5 changes: 5 additions & 0 deletions pkg/executor/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ type colorizedWriter struct {

// WithHost creates a new StdoutColorWriter with the given hostAddr name.
func (s *colorizedWriter) WithHost(hostAddr, hostName string) LogWriter {
if strings.HasPrefix(hostAddr, hostName+":") {
// in case if we don't have hostName it was set to hostAddr without port
// we want to prevent log prefix duplication, i.e. [dev1.umputun.dev dev1.umputun.dev:22]
hostName = ""
}
return &colorizedWriter{wr: s.wr, hostAddr: hostAddr, hostName: hostName,
prefix: s.prefix, secrets: s.secrets, monochrome: s.monochrome}
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/executor/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestColorizedWriter(t *testing.T) {
expectedLines []string
}{
{
name: "WithPrefix no host name",
name: "INFO no host name",
prefix: "INFO",
hostAddr: "localhost",
input: "This is a test message\nThis is another test message",
Expand All @@ -147,7 +147,7 @@ func TestColorizedWriter(t *testing.T) {
},
},
{
name: "WithPrefix with host name",
name: "INFO with host name",
prefix: "INFO",
hostAddr: "localhost",
hostName: "my-host",
Expand All @@ -158,7 +158,7 @@ func TestColorizedWriter(t *testing.T) {
},
},
{
name: "WithPrefix with host name and secrets",
name: "host name and secrets",
prefix: "INFO",
hostAddr: "localhost",
hostName: "my-host",
Expand All @@ -170,7 +170,7 @@ func TestColorizedWriter(t *testing.T) {
},
},
{
name: "WithoutPrefix no host name",
name: "no host name, no prefix",
prefix: "",
hostAddr: "localhost",
input: "This is a test message\nThis is another test message",
Expand All @@ -180,7 +180,7 @@ func TestColorizedWriter(t *testing.T) {
},
},
{
name: "WithoutPrefix, set host name",
name: "set host name using WithHost",
prefix: "",
hostAddr: "localhost",
input: "This is a test message\nThis is another test message",
Expand All @@ -192,7 +192,19 @@ func TestColorizedWriter(t *testing.T) {
},
},
{
name: "WithoutPrefix with host name",
name: "set host name and matching host addr using WithHost",
prefix: "",
hostAddr: "localhost",
input: "This is a test message\nThis is another test message",
withHostName: "127.0.0.1",
withHostAddr: "127.0.0.1:80",
expectedLines: []string{
"[127.0.0.1:80] This is a test message",
"[127.0.0.1:80] This is another test message",
},
},
{
name: "with host name, no prefix",
prefix: "",
hostAddr: "localhost",
hostName: "my-host",
Expand Down

0 comments on commit b944985

Please sign in to comment.