Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Refactor Ansible role #2219

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions deploy/ansible/roles/iroha-docker/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##### Description
# Description
[This role](https://github.com/hyperledger/iroha/tree/master/deploy/ansible/roles/iroha-docker) deploys multiple replicas of Iroha containers (one Iroha peer per container) on remote hosts. Each Iroha peer can communicate with others in two ways:
- using public IP addresses or hostnames set in inventory list OR
- using private IP addresses of the Docker overlay network
Expand All @@ -7,11 +7,11 @@ The first one is easier to implement since it does not require preliminary confi

This option is enabled by default.

The second one can be used when there exists an overlay network between the hosts. In short, overlay network allows for Docker containers to communicate using a single subnet. Such that each container would have a unique IP address in that subnet. Learn more in official Docker documentation (https://docs.docker.com/network/overlay). We recommend to use Calico for setting up Docker overlay network since it can be used as a network plugin (https://docs.projectcalico.org/v1.5/getting-started/docker/tutorials/basic).
The second one can be used when there exists an overlay network between the hosts. In short, overlay network allows for Docker containers to communicate using a single subnet. Such that each container would have a unique IP address in that subnet. Learn more in official Docker documentation (https://docs.docker.com/network/overlay). Overlay network can be created if your instance is part of Swarm cluster. Another method does not involve creating a Swarm cluster but requires a distributed key-value storage. [There is a guide](https://docker-k8s-lab.readthedocs.io/en/latest/docker/docker-etcd.html) on how to create such overlay network.

The second way is also suitable for local-only deployments.
The second way is also suitable for local-only deployments and does not require any key-value storage.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "Another method does not involve..." be a third method with a separate paragraph? Otherwise it seems a bit misleading.


##### Requirements
# Requirements
Tested on Ubuntu 16.04, 18.04
- Local:
- python3, python3-dev
Expand All @@ -26,7 +26,7 @@ The second way is also suitable for local-only deployments.
**Note:**
> `docker.io` package from Ubuntu repos will not work. Either use Ansible role or install Docker following official instructions for your OS flavor.

##### Quick Start
# Quick Start
1. Install Ansible
```
pip3 install ansible
Expand All @@ -49,11 +49,11 @@ It will also install Docker along with required python modules. If you want to s
**Note:**
> This command escalates privileges on a remote host during the run. It is required to be able to spin up Docker containers. We recommend to run the playbook using a passwordless remote sudo user.

##### Initial configuration
# Initial configuration

See `defaults/main.yml` file to get more details about available configuration options.

##### Examples
# Examples
**Example 1**
<!-- TODO: Cover more example cases -->
Deploying 6 Iroha peers on two remote hosts communicating using public IP addresses. With 2 and 4 replicas on each host respectively.
Expand All @@ -77,12 +77,12 @@ Deploying 6 Iroha peers on two remote hosts communicating using public IP addres

**192.168.122.109.yml**
```
replicas: 2
iroha_replicas: 2
```

**192.168.122.30.yml**
```
replicas: 4
iroha_replicas: 4
```

4. Run the playbook
Expand Down
45 changes: 34 additions & 11 deletions deploy/ansible/roles/iroha-docker/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Minimum total nodes is 6 (5f+1) in order for consensus to work properly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still 3f+1 at the moment.

#
# Default: 6
replicas: 6
iroha_replicas: 6

# Whether to use custom hostname for EACH container.
# If set to `true`, Iroha peers will communicate using these hostnames. Hostnames should be set using
Expand All @@ -11,7 +11,7 @@ replicas: 6
# variable. Container and service names in Docker Compose files will be auto-generated.
#
# Default: false
custom_hostnames: false
iroha_custom_hostnames: false

# Affects how Iroha peers are communicated. If set to `true`, Docker overlay network with that
# name will be used. It must be created beforehand. The recommended way is to use Calico plugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Calico mentions are removed from iroha-docker/README.md, should it be removed from this file as well?

Expand All @@ -21,29 +21,51 @@ custom_hostnames: false
# deployments for testing purposes.
#
# Default: false
overlay_network: false
iroha_overlay_network: false

## Deployment configs
container_basename: iroha
iroha_container_basename: iroha
# Path on a local machine for generated configs prior moving them on the remote
config_local_dir: /tmp/iroha-ansible-conf
iroha_config_local_dir: /tmp/iroha-ansible-conf
# Path on a remote machine for generated configs
deploy_dir: /opt/iroha-deploy
iroha_deploy_dir: /opt/iroha-deploy
# Overlay network name
# If using overlay network plugin (like Calico) the network must be created prior running this role
# The network will be created automatically if deploying locally or with `overlay_network`
# The network will be created automatically if deploying locally or with `iroha_overlay_network`
# set to `false`
iroha_network_name: iroha-net

# The role is incompatible with Iroha versions below RC2 since keys format has changed.
# Apply the patch (files/old-keys-format.patch) if you need support for previous Iroha versions.
iroha_docker_tag: 1.0.0_rc2
postgres_docker_tag: '9.5'
iroha_docker_tag: 1.0.0_rc5
iroha_postgres_docker_tag: '9.5'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we bump version to postures 10?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The minimal supported version of Postgres is 9.5, AFAIK. It is used in CI to verify compatibility of each build. By using v9.5 we are confident that no breaking changes exist in later versions (though, they probably also play nice with Iroha. But we cannot say for sure).


## The block of variables that toggles certain tasks of the role
# Toggle tasks in `init-vars.yml` file. Will initialize variables for container names,
# Iroha hostnames, etc. No configs will be changed.
# Default: true
iroha_init_vars: true
# Toggle tasks in `config-gen.yml` file. Will generate new keys for Iroha with a new Genesis
# Block
# Default: true
iroha_generate_init_configs: true
# Toggle tasks in `deploy.yml` file. Enable if you just need to update Iroha configs and Docker
# Compose file
# Default: true
iroha_update_runtime_configs: true

# Toggles usage of a custom Genesis Block. Useful if you have custom roles, users, permissions,
# etc. The role will fill your custom Genesis Block with Iroha peers while preserving everything
# else.
# Default: false
iroha_custom_genesis_block: false
# Path to the custom Genesis Block. Used only when `iroha_custom_genesis_block` set to `true`
iroha_custom_genesis_block_path: ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a single variable iroha_custom_genesis_block_path, which is commented out by default, and use it with is defined instead of a separate bool variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for me, it is not immediately obvious that setting just iroha_custom_genesis_block_path variable would change execution path. Also, it is already tested in that 2-variables configuration.


## Iroha config
# This value will be used as a base peer port for Iroha container. E.g. setting `replicas` option to
# This value will be used as a base peer port for Iroha container. E.g. setting `iroha_replicas` option to
# 6 will generate Docker Compose file with port numbers starting from 10001 counting up to 10006.
# If `custom_hostnames` set to `true` AND contains port numbers this option is ignored
# If `iroha_custom_hostnames` set to `true` AND contains port numbers this option is ignored
iroha_peer_port: 10001
# As above but for base Torii port
iroha_torii_port: 50051
Expand All @@ -66,3 +88,4 @@ iroha_blockstore_path: /tmp/block_store
#iroha_mst_expiration_time: 1440
#iroha_max_rounds_delay: 3000
#iroha_stale_stream_max_rounds: 2

8 changes: 8 additions & 0 deletions deploy/ansible/roles/iroha-docker/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# handlers file for Iroha
---
- name: restart Iroha
docker_service:
project_src: "{{ iroha_deploy_dir }}"
pull: yes
state: present
recreate: always
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline and check other files.

80 changes: 31 additions & 49 deletions deploy/ansible/roles/iroha-docker/tasks/config-gen.yml
Original file line number Diff line number Diff line change
@@ -1,81 +1,63 @@
- block:
- set_fact:
iroha_nodes: []
iroha_all_nodes: []

- name: generate hostnames (no overlay)
set_fact:
iroha_nodes: "{{ iroha_nodes }} + [ '{{ inventory_hostname }}:{{ iroha_peer_port | int + item | int }}' ]"
loop: "{{ range(0, replicas |int) | list }}"
when: not custom_hostnames and not overlay_network

- shell: "cat /dev/urandom | tr -cd [:xdigit:] | tr '[:upper:]' '[:lower:]' | head -c {{ 8 * replicas | int}}"
register: random_hex
when: not custom_hostnames and overlay_network

- name: generate hostnames (overlay)
set_fact:
iroha_nodes: "{{ iroha_nodes }} + [ '{{ container_basename }}-{{ item }}-{{ random_hex.stdout[item|int*8:(item|int+1)*8-1] }}:{{ iroha_peer_port }}' ]"
loop: "{{ range(0, replicas) | list }}"
when: not custom_hostnames and overlay_network

- name: generate hostnames (custom_hostnames)
set_fact:
iroha_nodes: "{{ iroha_nodes }} + [ '{% if item.split(':') | length < 2 %}{{ item }}:{{ iroha_peer_port | int + idx }}{% else %}{{ item }}{% endif %}' ]"
when: custom_hostnames and hostnames is defined
loop: "{{ hostnames }}"
loop_control:
index_var: idx

- set_fact:
iroha_peers_map: "{{ iroha_peers_map | default([]) }} + [ {{ {'hostname': item, 'human_hostname': 'c_' + item | regex_replace('\\.', '_') | regex_replace(':', '_'), 'peer_port': item.split(':')[1] } }} ]"
loop: "{{ iroha_nodes }}"

- debug:
var: iroha_peers_map

- set_fact:
iroha_nodes: "{{ iroha_peers_map }}"

- set_fact:
iroha_all_nodes: "{{ iroha_all_nodes }} + {{ hostvars[item]['iroha_nodes'] }}"
loop: "{{ ansible_play_hosts }}"
- name: copy custom genesis block
copy:
src: "{{ iroha_custom_genesis_block_path }}"
dest: "{{ role_path }}/files/genesis.block"
run_once: yes
when: iroha_custom_genesis_block
failed_when: iroha_custom_genesis_block_path is not defined

- name: generate Iroha configs
command: "./config_gen.sh -p{{ iroha_all_nodes | join(',', attribute='hostname') }} -o{{ config_local_dir }} -f"
command: "./config_gen.sh -p{{ iroha_all_nodes | join(',', attribute='hostname') }} -o{{ iroha_config_local_dir }} -f"
run_once: yes
args:
chdir: "{{ role_path }}/files"

- name: make config dirs
file:
path: "{{ config_local_dir }}/{{ inventory_hostname }}/conf/{{ item.human_hostname }}"
path: "{{ iroha_config_local_dir }}/{{ inventory_hostname }}/conf/{{ item.human_hostname }}"
state: directory
loop: "{{ iroha_nodes }}"

- name: move keys
copy:
src: "{{ config_local_dir }}/{{ item[0].hostname }}{{ item[1] }}"
dest: "{{ config_local_dir }}/{{ inventory_hostname }}/conf/{{ item[0].human_hostname }}"
src: "{{ iroha_config_local_dir }}/{{ item[0].hostname }}{{ item[1] }}"
dest: "{{ iroha_config_local_dir }}/{{ inventory_hostname }}/conf/{{ item[0].human_hostname }}"
loop: "{{ iroha_nodes | product(['.priv', '.pub']) | list }}"

- name: move genesis.block
copy:
src: "{{ config_local_dir }}/genesis.block"
dest: "{{ config_local_dir }}/{{ inventory_hostname }}/conf/{{ item.human_hostname }}"
src: "{{ iroha_config_local_dir }}/genesis.block"
dest: "{{ iroha_config_local_dir }}/{{ inventory_hostname }}/conf/{{ item.human_hostname }}"
loop: "{{ iroha_nodes }}"

- name: cleanup
file:
path: "{{ config_local_dir }}/{{ item[0].hostname }}{{ item[1] }}"
path: "{{ iroha_config_local_dir }}/{{ item[0].hostname }}{{ item[1] }}"
state: absent
loop: "{{ iroha_nodes | product(['.priv', '.pub']) | list }}"

- name: cleanup
file:
path: "{{ config_local_dir }}/genesis.block"
path: "{{ iroha_config_local_dir }}/genesis.block"
state: absent
run_once: yes
become: no
delegate_to: localhost
when: iroha_generate_init_configs
tags: ["iroha-config-gen"]

- name: create deploy dir
file:
state: directory
path: "{{ iroha_deploy_dir }}/conf"
tags: ["iroha-config-gen"]

- name: copy config files
synchronize:
src: "{{ iroha_config_local_dir }}/{{ inventory_hostname }}/conf/"
dest: "{{ iroha_deploy_dir }}/conf/"
recursive: yes
checksum: yes
when: iroha_generate_init_configs
tags: ["iroha-config-gen"]
108 changes: 37 additions & 71 deletions deploy/ansible/roles/iroha-docker/tasks/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,71 +1,37 @@
- name: create deploy dir
file:
state: directory
path: "{{ deploy_dir }}/conf"

- stat:
path: "{{ deploy_dir }}/docker-compose.yml"
register: docker_compose_file

- name: stop Iroha
docker_service:
project_src: "{{ deploy_dir }}"
project_name: ansible_iroha
remove_volumes: yes
state: absent
when: docker_compose_file.stat.exists

- name: remove Iroha network (overlay)
docker_network:
name: "{{ iroha_network_name }}"
driver: overlay
state: absent
run_once: yes
when: docker_compose_file.stat.exists and overlay_network

- name: remove Iroha network
docker_network:
name: "{{ iroha_network_name }}"
state: absent
when: docker_compose_file.stat.exists and not overlay_network

- name: generate Docker compose
template:
src: docker-compose.yml.j2
dest: "{{ deploy_dir }}/docker-compose.yml"

- name: copy config files
synchronize:
src: "{{ config_local_dir }}/{{ inventory_hostname }}/conf/"
dest: "{{ deploy_dir }}/conf/"
recursive: yes
delete: yes

- name: generate config files
template:
src: config.docker.j2
dest: "{{ deploy_dir }}/conf/{{ item.human_hostname }}/config.docker"
loop: "{{ iroha_nodes }}"

- name: create Docker network
docker_network:
name: "{{ iroha_network_name }}"
driver: overlay
run_once: yes
when: overlay_network

- name: create Docker network
docker_network:
name: "{{ iroha_network_name }}"
when: not overlay_network

- name: pull Iroha
command: docker-compose pull
args:
chdir: "{{ deploy_dir }}"
changed_when: False

- name: run Iroha
docker_service:
project_src: "{{ deploy_dir }}"
project_name: ansible_iroha
- block:
- name: create deploy dir
file:
state: directory
path: "{{ iroha_deploy_dir }}/conf"

# Attachable overlay network cannot be created with a `docker_network` module
- name: create Docker network
shell: "docker network create -d overlay --attachable {{ iroha_network_name }} || true"
when: iroha_overlay_network
run_once: yes

- name: create Docker network
docker_network:
name: "{{ iroha_network_name }}"
when: not iroha_overlay_network

- name: generate Docker Compose
template:
src: docker-compose.yml.j2
dest: "{{ iroha_deploy_dir }}/docker-compose.yml"
notify: restart Iroha
tags: ["iroha-deploy"]

- name: generate config files
template:
src: config.docker.j2
dest: "{{ iroha_deploy_dir }}/conf/{{ item.human_hostname }}/config.docker"
notify: restart Iroha
loop: "{{ iroha_nodes }}"
tags: ["iroha-deploy"]

- name: run Iroha
docker_service:
project_src: "{{ iroha_deploy_dir }}"
tags: ["iroha-deploy"]
when: iroha_update_runtime_configs
Loading