Skip to content

Commit

Permalink
separate ansible_host from host address (closes #35) (#143)
Browse files Browse the repository at this point in the history
Now it is possible to directly specify `ansible_host` parameter
  • Loading branch information
ftelnov authored Oct 26, 2023
1 parent 1c050a5 commit 483bf12
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 3 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,40 @@ With this flag, once the instances are distributed over the target hosts accordi
---
#### Configuration `ansible-host`
The `ansible-host` variable needs to be set in the final inventory configuration at the hosts. An example of a desirable one:
```yaml
storages:
vars:
ansible_host: 192.168.1.1
hosts:
storage-1-1: ~
```
By default, `ansible_host` is populated from the `address` parameter of the cluster host configuration,
for example for the above result the source might look like this:
```yaml
hosts:
- name: storages
config:
address: 192.168.1.1
```
You can also set this parameter manually via the `ansible_host` host configuration parameter. It has a higher priority:
```yaml
hosts:
- name: storages
config:
address: 192.168.1.1
ansible_host: 192.168.1.2
```
---
### Reverse parsing config
Since `Genin` is a relatively new tool, and `picodata` is far from full
Expand Down
34 changes: 34 additions & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,40 @@ hosts:

---

#### Параметр `ansible-host`

В конечной конфигурации инвентаря у хостов нужно проставить переменную `ansible-host`. Пример желаемого:

```yaml
storages:
vars:
ansible_host: 192.168.1.1
hosts:
storage-1-1: ~
```

По умолчанию `ansible_host` заполняется из параметра `address` конфигурации хоста кластера,
например для вышеуказанного результата источник может выглядеть так:

```yaml
hosts:
- name: storages
config:
address: 192.168.1.1
```

Можно также задать этот параметр вручную через параметр конфигурации хоста `ansible_host`. Он имеет больший приоритет:

```yaml
hosts:
- name: storages
config:
address: 192.168.1.1
ansible_host: 192.168.1.2
```

---

### Обратный парсинг

Так как `Genin` это относительно свежий инструмент, и в `picodata` далеко не все
Expand Down
5 changes: 4 additions & 1 deletion src/task/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl<'a> TryFrom<&'a Inventory> for Cluster {
name: name.clone(),
config: HostV2Config::from(ansible_host.clone())
.with_additional_config(additional_config.clone())
.with_ansible_host(ansible_host.clone())
.with_ports(
inventory
.all
Expand Down Expand Up @@ -354,7 +355,9 @@ impl<'a> TryFrom<&'a Inventory> for Cluster {
failure_domains: Default::default(),
roles: Vec::new(),
cartridge_extra_env: instance.vars.clone(),
config: InstanceV2Config::from_inventory_host(&instance),
config: InstanceV2Config::from_inventory_host(
&instance,
),
vars: instance.vars.clone(),
view: View::default(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ HostV2 {
address: Ip(
192.168.123.11,
),
ansible_host: None,
distance: None,
additional_config: {},
},
Expand All @@ -38,6 +39,7 @@ HostV2 {
address: Ip(
192.168.123.11,
),
ansible_host: None,
distance: None,
additional_config: {},
},
Expand Down Expand Up @@ -461,6 +463,7 @@ HostV2 {
address: Ip(
192.168.123.11,
),
ansible_host: None,
distance: None,
additional_config: {},
},
Expand Down
23 changes: 23 additions & 0 deletions src/task/cluster/hst/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl From<Host> for HostV2 {
http_port: host.ports.http_as_option(),
binary_port: host.ports.binary_as_option(),
address: Address::from(host.ip),
ansible_host: Default::default(),
distance: Some(host.distance).and_then(|distance| {
if distance.eq(&0) {
None
Expand Down Expand Up @@ -213,6 +214,7 @@ impl HostV2 {
http_port: ports.http_as_option(),
binary_port: ports.binary_as_option(),
address: Address::from(ip),
ansible_host: Default::default(),
distance: None,
additional_config: IndexMap::new(),
},
Expand All @@ -237,6 +239,7 @@ impl HostV2 {
http_port: ports.http_as_option(),
binary_port: ports.binary_as_option(),
address: Address::from(ip),
ansible_host: Default::default(),
distance: None,
additional_config: IndexMap::new(),
},
Expand Down Expand Up @@ -837,6 +840,8 @@ pub struct HostV2Config {
pub binary_port: Option<u16>,
#[serde(default, skip_serializing_if = "Address::is_none")]
pub address: Address,
#[serde(default, skip_serializing_if = "Address::is_none")]
pub ansible_host: Address,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub distance: Option<usize>,
#[serde(default, skip_serializing_if = "IndexMap::is_empty")]
Expand Down Expand Up @@ -892,6 +897,7 @@ impl<'a> From<&'a InvHostConfig> for HostV2Config {
http_port: Some(*http_port),
binary_port: Some(advertise_uri.port),
address: advertise_uri.address.clone(),
ansible_host: Default::default(),
distance: None,
additional_config: additional_config.clone(),
},
Expand All @@ -906,6 +912,7 @@ impl<'a> From<&'a InvHostConfig> for HostV2Config {
.address
})
.unwrap(),
ansible_host: Default::default(),
distance: None,
additional_config: additional_config.clone(),
},
Expand Down Expand Up @@ -953,15 +960,31 @@ impl HostV2Config {
}
}

pub fn with_ansible_host(self, ansible_host: Address) -> Self {
Self {
ansible_host,
..self
}
}

pub fn address(&self) -> Address {
self.address.clone()
}

/// Wraps ansible_host behavior - if it isn't supplied, address would be used as a replacement.
pub fn ansible_host(&self) -> Address {
if !self.ansible_host.is_none() {
return self.ansible_host.clone();
}
self.address.clone()
}

pub fn merge(self, other: HostV2Config) -> Self {
Self {
http_port: self.http_port.or(other.http_port),
binary_port: self.binary_port.or(other.binary_port),
address: self.address.or(other.address),
ansible_host: self.ansible_host.or(other.ansible_host),
distance: self.distance.or(other.distance),
additional_config: merge_index_maps(self.additional_config, other.additional_config),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ hosts:
http_port: 8081
binary_port: 3031
address: 192.168.16.11
ansible_host: 192.168.16.11
- name: server-2
config:
http_port: 8081
binary_port: 3031
address: 192.168.16.12
ansible_host: 192.168.16.12
failover:
mode: stateful
state_provider: stateboard
Expand Down
58 changes: 58 additions & 0 deletions src/task/cluster/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,61 @@ topology:

insta::assert_display_snapshot!(uncolorize(result));
}

#[test]
fn specify_ansible_host() {
let cluster = r#"---
topology:
- name: router
replicasets_count: 2
roles:
- router
- failover-coordinator
hosts:
- name: datacenter-1
config:
http_port: 8081
binary_port: 3031
hosts:
- name: server-1
config:
address: 192.168.16.11
- name: server-2
config:
ansible_host: 192.168.16.14
address: 192.168.16.12
vars:
ansible_user: ansible
ansible_password: ansible
cartridge_app_name: myapp
cartridge_cluster_cookie: myapp-cookie
cartridge_package_path: /tmp/myapp.rpm
cartridge_bootstrap_vshard: true
"#;

let mut cluster: Cluster = serde_yaml::from_str(cluster).unwrap();
cluster.hosts.spread();

let inventory = Inventory::try_from(&cluster).unwrap();
let ansible_host = inventory.all.children.values().last().unwrap();
let ansible_host = match ansible_host {
Child::Replicaset { .. } => panic!("unexpected"),
Child::Host { vars, .. } => &vars.ansible_host,
};
assert_eq!(ansible_host, &Address::from("192.168.16.14"));

let with_default_ansible_host = inventory
.all
.children
.values()
.filter(|child| {
let ansible_host = match child {
Child::Replicaset { .. } => return false,
Child::Host { vars, .. } => &vars.ansible_host,
};
ansible_host == &Address::from("192.168.16.11")
})
.count();

assert_eq!(with_default_ansible_host, 1);
}
4 changes: 2 additions & 2 deletions src/task/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<'a> TryFrom<&'a Cluster> for Inventory {
.entry(host.name.clone())
.or_insert(Child::Host {
vars: HostVars {
ansible_host: host.config.address(),
ansible_host: host.config.ansible_host(),
additional_config: host.config.additional_config.clone(),
},
hosts: host
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<'a> TryFrom<&'a Option<Cluster>> for Inventory {
.entry(host.name.clone())
.or_insert(Child::Host {
vars: HostVars {
ansible_host: host.config.address(),
ansible_host: host.config.ansible_host(),
additional_config: host.config.additional_config.clone(),
},
hosts: host
Expand Down

0 comments on commit 483bf12

Please sign in to comment.