Skip to content

Commit

Permalink
don't allow set of targets named "all"
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 9, 2023
1 parent 7e2e99b commit eb724ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/config/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,13 @@ func (p *PlayBook) checkConfig() error {
}
}

// check what target set is not called "all"
for k := range p.Targets {
if strings.EqualFold(k, allHostsGrp) {
return fmt.Errorf("target %q is reserved for all hosts", allHostsGrp)
}
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/config/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ func TestPlaybook_New(t *testing.T) {
assert.Equal(t, "docker", tsk.Commands[4].Name)
assert.Equal(t, map[string]string{"SEC1": "VAL1", "SEC2": "VAL2"}, tsk.Commands[4].Secrets)
})

t.Run("playbook prohibited all target", func(t *testing.T) {
_, err := New("testdata/playbook-with-all-group.yml", nil, nil)
require.ErrorContains(t, err, "config testdata/playbook-with-all-group.yml is invalid: target \"all\" is reserved for all hosts")
})
}

func TestPlayBook_Task(t *testing.T) {
Expand Down
45 changes: 45 additions & 0 deletions pkg/config/testdata/playbook-with-all-group.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
user: umputun
inventory: "testdata/hosts-without-groups.yml"

targets:
remark42:
hosts: [{name: "h1", host: "h1.example.com"}, {host: "h2.example.com"}]
staging:
groups: ["gr1"]
all:
groups: ["gr1", "gr2"]

tasks:
- name: deploy-remark42
commands:
- 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
3 changes: 1 addition & 2 deletions pkg/config/testdata/playbook-with-inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ targets:
remark42:
hosts: [{name: "h1", host: "h1.example.com"}, {host: "h2.example.com"}]
staging:
groups: ["all"]

groups: ["gr1", "gr2", "all"]

tasks:
- name: deploy-remark42
Expand Down

0 comments on commit eb724ec

Please sign in to comment.