Skip to content

Commit f998ddc

Browse files
author
Ulfat
authored
Merge pull request #327 from electrocucaracha/split-bootstrap-role
Separate functions from the Bootstrap Ansible Role
2 parents 2188a62 + 8f0bbf1 commit f998ddc

39 files changed

+1059
-172
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
name: Ansible Molecule test execution
17+
inputs:
18+
ansible-role:
19+
description: 'Ansible Role'
20+
required: true
21+
22+
runs:
23+
using: "composite"
24+
steps:
25+
- uses: actions/setup-python@v3
26+
with:
27+
python-version: '3.x'
28+
- name: install dependencies
29+
shell: bash
30+
run: pip install -r deployment/test-requirements.txt
31+
- name: Run molecule tests
32+
shell: bash
33+
run: |
34+
cd deployment/ansible/roles/${{ inputs.ansible-role }}/
35+
molecule --debug test

.github/workflows/on-demand_molecule.yml

+65-14
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,72 @@ on:
3030
- deployment/ansible/roles/**
3131

3232
jobs:
33-
check-molecule:
34-
name: Check Ansible Molecule role tests
35-
strategy:
36-
fail-fast: false
37-
matrix:
38-
role: [bootstrap, genesis]
33+
changes:
34+
runs-on: ubuntu-latest
35+
outputs:
36+
bootstrap: ${{ steps.filter.outputs.bootstrap }}
37+
genesis: ${{ steps.filter.outputs.genesis }}
38+
add-accounts: ${{ steps.filter.outputs.add-accounts }}
39+
configure: ${{ steps.filter.outputs.configure }}
40+
steps:
41+
- uses: actions/checkout@v3
42+
- uses: dorny/paths-filter@v2
43+
if: ${{ !env.ACT }}
44+
id: filter
45+
with:
46+
token: ${{ secrets.GITHUB_TOKEN }}
47+
filters: |
48+
reqs: &reqs
49+
- 'deployment/test-requirements.*'
50+
bootstrap:
51+
- *reqs
52+
- 'deployment/ansible/roles/bootstrap/**'
53+
genesis:
54+
- *reqs
55+
- 'deployment/ansible/roles/genesis/**'
56+
add-accounts:
57+
- *reqs
58+
- 'deployment/ansible/roles/add-accounts/**'
59+
configure:
60+
- *reqs
61+
- 'deployment/ansible/roles/configure/**'
62+
check-bootstrap:
63+
needs: changes
64+
if: needs.changes.outputs.bootstrap == 'true'
65+
name: Check Bootstrap Ansible role
66+
runs-on: ubuntu-latest
67+
steps:
68+
- uses: actions/checkout@v2
69+
- uses: ./.github/actions/setup-molecule
70+
with:
71+
ansible-role: bootstrap
72+
check-genesis:
73+
needs: changes
74+
if: needs.changes.outputs.genesis == 'true'
75+
name: Check Genesis Ansible role
76+
runs-on: ubuntu-latest
77+
steps:
78+
- uses: actions/checkout@v2
79+
- uses: ./.github/actions/setup-molecule
80+
with:
81+
ansible-role: genesis
82+
check-add-accounts:
83+
needs: changes
84+
if: needs.changes.outputs.add-accounts == 'true'
85+
name: Check Add accounts Ansible role
86+
runs-on: ubuntu-latest
87+
steps:
88+
- uses: actions/checkout@v2
89+
- uses: ./.github/actions/setup-molecule
90+
with:
91+
ansible-role: add-accounts
92+
check-configure:
93+
needs: changes
94+
if: needs.changes.outputs.configure == 'true'
95+
name: Check Configure Ansible role
3996
runs-on: ubuntu-latest
4097
steps:
4198
- uses: actions/checkout@v2
42-
- uses: actions/setup-python@v3
99+
- uses: ./.github/actions/setup-molecule
43100
with:
44-
python-version: '3.x'
45-
- name: install dependencies
46-
run: pip install -r deployment/test-requirements.txt
47-
- name: Run molecule tests
48-
run: |
49-
cd deployment/ansible/roles/${{ matrix.role }}/
50-
molecule --debug test
101+
ansible-role: configure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Add accounts
2+
3+
This role creates keys defined on `accounts` list variable and set up the
4+
command-line interface.
5+
6+
## Requirements
7+
8+
None
9+
10+
## Role Variables
11+
12+
```yaml
13+
accounts:
14+
- name: user1
15+
passphrase: password123
16+
roles:
17+
- NodeAdmin
18+
- Trustee
19+
```
20+
21+
A list of DCL accounts to be created on a specific target node.
22+
23+
## Dependencies
24+
25+
None
26+
27+
## Example Playbook
28+
29+
example inventory.yaml
30+
31+
```yaml
32+
all:
33+
vars:
34+
chain_id: dev-net
35+
hosts:
36+
node0:
37+
accounts:
38+
- name: jack
39+
passphrase: test1234
40+
roles:
41+
- NodeAdmin
42+
- Trustee
43+
node1:
44+
accounts:
45+
- name: alice
46+
passphrase: s3cr3t123
47+
roles:
48+
- NodeAdmin
49+
- Trustee
50+
node2:
51+
accounts:
52+
- name: bob
53+
passphrase: admin1234
54+
roles:
55+
- NodeAdmin
56+
- Trustee
57+
node3:
58+
accounts:
59+
- name: anna
60+
passphrase: test1234
61+
roles:
62+
- NodeAdmin
63+
```
64+
65+
in your playbook:
66+
67+
```yaml
68+
- name: bootstrap DCL nodes
69+
hosts: all
70+
become: true
71+
roles:
72+
- bootstrap
73+
- add-accounts
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# NOTE: Every host must define their own DCL accounts
17+
accounts: []
18+
19+
dcl_home: /var/lib/dcl/.dcl
20+
dcld:
21+
path: "{{ dcl_home }}/cosmovisor/genesis/bin/dcld"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
- name: converge
17+
hosts: all
18+
roles:
19+
- add-accounts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
driver:
17+
name: docker
18+
lint: |
19+
set -e
20+
yamllint -c ../../../../.yaml-lint.yml .
21+
platforms:
22+
- name: node0
23+
image: geerlingguy/docker-ubuntu2004-ansible:latest
24+
pre_build_image: true
25+
command: /sbin/init
26+
tmpfs:
27+
- /run
28+
- /tmp
29+
- /run/lock
30+
volumes:
31+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
32+
provisioner:
33+
name: ansible
34+
inventory:
35+
group_vars:
36+
all:
37+
chain_id: test-net
38+
host_vars:
39+
node0:
40+
accounts:
41+
- name: jack
42+
passphrase: test1234
43+
roles:
44+
- NodeAdmin
45+
- Trustee
46+
verifier:
47+
name: testinfra
48+
lint:
49+
name: flake8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
- name: prepare
17+
hosts: all
18+
roles:
19+
- bootstrap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2022 Samsung Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import os
17+
18+
import testinfra.utils.ansible_runner
19+
20+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
21+
os.environ["MOLECULE_INVENTORY_FILE"]
22+
).get_hosts("all")
23+
DCLD_HOME = "/var/lib/dcl/.dcl/"
24+
25+
26+
def test_accounts_creation(host):
27+
all_variables = host.ansible.get_variables()
28+
assert "accounts" in all_variables
29+
for account in all_variables["accounts"]:
30+
assert "passphrase" in account
31+
assert "name" in account
32+
cmd = host.run(
33+
f"echo {account['passphrase']}"
34+
f" | /var/lib/dcl/.dcl/cosmovisor/genesis/bin/dcld keys show {account['name']}"
35+
f" --home {DCLD_HOME} --output json"
36+
)
37+
assert cmd.succeeded
38+
assert len(cmd.stdout) > 0
39+
key_name = json.loads(cmd.stdout)
40+
for key in ["name", "type", "address", "pubkey"]:
41+
assert key in key_name
42+
assert key_name["name"] == account["name"]
43+
assert key_name["type"] == "local"
44+
assert host.file(f"{DCLD_HOME}{account['name']}.info").exists
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
# Copyright 2022 Samsung Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
- name: create directory for key name facts
17+
file:
18+
state: directory
19+
recurse: true
20+
path: /etc/ansible/facts.d
21+
22+
- name: prepare keys
23+
include_tasks: prepare-keys.yml
24+
vars:
25+
key_name: "{{ account.name }}"
26+
passphrase: "{{ account.passphrase }}"
27+
loop: "{{ accounts }}"
28+
loop_control:
29+
loop_var: account
30+
no_log: true

deployment/ansible/roles/bootstrap/tasks/prepare-keys.yml deployment/ansible/roles/add-accounts/tasks/prepare-keys.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@
3939

4040
- name: prepare-keys | persist key name local fact
4141
changed_when: false
42-
shell: "echo {{ passphrase }} | {{ dcld.path }} keys show {{ key_name }} --home {{ dcl_home }} | tee /etc/ansible/facts.d/{{ key_name}}.fact"
42+
shell: "echo {{ passphrase }} | {{ dcld.path }} keys show {{ key_name }} --home {{ dcl_home }} --output json | tee /etc/ansible/facts.d/{{ key_name}}.fact"
4343
no_log: true

0 commit comments

Comments
 (0)