Skip to content

Commit

Permalink
don't treat targets set in simple pb as names if no inventory set
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 9, 2023
1 parent 1e3825a commit 7e2e99b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pkg/config/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func New(fname string, overrides *Overrides, secProvider SecretsProvider) (res *
return nil, fmt.Errorf("can't read config %s: %w", fname, err)
}

if err = unmarshalPlaybookFile(fname, data, res); err != nil {
if err = unmarshalPlaybookFile(fname, data, overrides, res); err != nil {
return nil, fmt.Errorf("can't unmarshal config: %w", err)
}

Expand Down Expand Up @@ -173,7 +173,7 @@ func New(fname string, overrides *Overrides, secProvider SecretsProvider) (res *
// It will try to guess format by file extension or use yaml as toml.
// First it will try to unmarshal to a complete PlayBook struct, if it fails,
// it will try to unmarshal to a SimplePlayBook struct and convert it to a complete PlayBook struct.
func unmarshalPlaybookFile(fname string, data []byte, res *PlayBook) (err error) {
func unmarshalPlaybookFile(fname string, data []byte, overrides *Overrides, res *PlayBook) (err error) {

unmarshal := func(data []byte, v interface{}) error {
// try to unmarshal yml first and then toml
Expand Down Expand Up @@ -217,16 +217,24 @@ func unmarshalPlaybookFile(fname string, data []byte, res *PlayBook) (err error)
res.Tasks = []Task{{Commands: simple.Task}} // simple playbook has just a list of commands as the task
res.Tasks[0].Name = "default" // we have only one task, set it as default

hasInventory := simple.Inventory != "" || (overrides != nil && overrides.Inventory != "")
target := Target{}
for _, t := range simple.Targets {
if strings.Contains(t, ":") {
ip, port := splitIPAddress(t)
target.Hosts = append(target.Hosts, Destination{Host: ip, Port: port}) // set as hosts in case of ip:port
log.Printf("[DEBUG] set target host %s:%d", ip, port)
} else {
target.Names = append(target.Names, t) // set as names in case of just name
}

if hasInventory && !strings.Contains(t, ":") {
target.Names = append(target.Names, t) // set as names in case of just name and inventory is set
log.Printf("[DEBUG] set target name %s", t)
}

if !hasInventory && !strings.Contains(t, ":") { // set as host with :22 in case of just name and no inventory
target.Hosts = append(target.Hosts, Destination{Host: t, Port: 22}) // set as hosts in case of ip:port
log.Printf("[DEBUG] set target host %s:22", t)
}
}
res.Targets = map[string]Target{"default": target}
return nil
Expand Down
15 changes: 14 additions & 1 deletion pkg/config/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestPlaybook_New(t *testing.T) {
require.ErrorContains(t, err, `duplicate task name "deploy"`)
})

t.Run("simple playbook", func(t *testing.T) {
t.Run("simple playbook with inventory", func(t *testing.T) {
c, err := New("testdata/simple-playbook.yml", nil, nil)
require.NoError(t, err)
assert.Equal(t, 1, len(c.Tasks), "1 task")
Expand All @@ -120,6 +120,19 @@ func TestPlaybook_New(t *testing.T) {
assert.Equal(t, []Destination{{Host: "127.0.0.1", Port: 2222}}, c.Targets["default"].Hosts)
})

t.Run("simple playbook without inventory", func(t *testing.T) {
c, err := New("testdata/simple-playbook-no-inventory.yml", nil, nil)
require.NoError(t, err)
assert.Equal(t, 1, len(c.Tasks), "1 task")
assert.Equal(t, "default", c.Tasks[0].Name, "task name")
assert.Equal(t, 5, len(c.Tasks[0].Commands), "5 commands")

assert.Equal(t, 1, len(c.Targets))
assert.Equal(t, 0, len(c.Targets["default"].Names))
assert.Equal(t, []Destination{{Host: "name1", Port: 22}, {Host: "192.168.1.1", Port: 22},
{Host: "127.0.0.1", Port: 2222}}, c.Targets["default"].Hosts)
})

t.Run("playbook with secrets", func(t *testing.T) {
secProvider := &mocks.SecretProvider{
GetFunc: func(key string) (string, error) {
Expand Down
37 changes: 37 additions & 0 deletions pkg/config/testdata/simple-playbook-no-inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
user: app
ssh_key: ~/.ssh/id_rsa

targets: ["name1", "192.168.1.1", "127.0.0.1:2222"]

task:
- name: wait
script: sleep 5

- name: copy configuration
copy: {"src": "/local/remark42.yml", "dst": "/srv/remark42.yml", "mkdir": true}

- name: some local command
options: {local: true}
script: |
ls -la /srv
du -hcs /srv
- name: git
before: "echo before git"
after: "echo after git"
onerror: "echo onerror git"
script: |
git clone https://example.com/remark42.git /srv || true # clone if doesn't exists, but don't fail if exists
cd /srv
git pull
- name: docker
options: {no_auto: true}
script: |
docker pull umputun/remark42:latest
docker stop remark42 || true
docker rm remark42 || true
docker run -d --name remark42 -p 8080:8080 umputun/remark42:latest
env:
FOO: bar
BAR: qux

0 comments on commit 7e2e99b

Please sign in to comment.