Skip to content

Commit

Permalink
Implement provisioning network interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed Sep 25, 2022
1 parent 34db87f commit f97c441
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
4 changes: 2 additions & 2 deletions kiss/api/src/ansible/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl AnsibleClient {
Some((Self::LABEL_BOX_NAME.into(), box_name.clone())),
Some((
Self::LABEL_BOX_ACCESS_ADDRESS.into(),
job.spec.access.address.to_string(),
job.spec.access.address_primary.to_string(),
)),
Some((
Self::LABEL_BOX_MACHINE_UUID.into(),
Expand Down Expand Up @@ -144,7 +144,7 @@ impl AnsibleClient {
},
EnvVar {
name: "ansible_ssh_host".into(),
value: Some(job.spec.access.address.to_string()),
value: Some(job.spec.access.management_address().to_string()),
..Default::default()
},
EnvVar {
Expand Down
9 changes: 8 additions & 1 deletion kiss/api/src/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ impl BoxState {
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BoxAccessSpec {
pub address: IpAddr,
pub address_primary: IpAddr,
pub address_secondary: Option<IpAddr>,
}

impl BoxAccessSpec {
pub fn management_address(&self) -> IpAddr {
self.address_secondary.unwrap_or(self.address_primary)
}
}

#[derive(
Expand Down
4 changes: 4 additions & 0 deletions templates/kiss/tasks/commission/network-playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- hosts: all
tasks:
- include: network.yaml
70 changes: 65 additions & 5 deletions templates/kiss/tasks/commission/network.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
---
- name: Get Network Interfaces
shell: ls /sys/class/net | grep -e '^en*'
register: netdev
- name: Collect network inteface infomations
loop: "{{ ansible_interfaces }}"
# filter the physical devices that has connected to the specific network as ethernet
when: >
item in ansible_facts
and ansible_facts[item].type == 'ether'
and ansible_facts[item].active
and ansible_facts[item].ipv4.netmask == '255.240.0.0'
and ansible_facts[item].ipv4.network == '10.32.0.0'
set_fact:
interfaces: >
{{ interfaces|default([]) + [{
'name': item,
'address_ipv4': ansible_facts[item].ipv4.address,
'module': ansible_facts[item].module,
'pciid': ansible_facts[item].pciid,
'speed': ansible_facts[item].speed,
'speed_neg': -ansible_facts[item].speed,
}] }}
- name: Get Network Interfaces
- name: Sort by speed, module, and PCI ID
set_fact:
interfaces: "{{ interfaces | sort(attribute='speed_neg,module,pciid') }}"

- name: Select the fastest interface as Primary
when: interfaces | length > 0
set_fact:
interface_primary: "{{ interfaces[0] }}"
interface_primary_address_ipv4: "{{ interfaces[0]['address_ipv4'] }}"

- name: Select the secondary fastest interface as Secondary
when: interfaces | length > 1
set_fact:
interface_secondary: "{{ interfaces[1] }}"
interface_secondary_address_ipv4: "{{ interfaces[1]['address_ipv4'] }}"

- name: Ping from primary interface to the gateway
when: interface_primary_address_ipv4 is defined
shell: >
ping -4 -c 4
-I {{ interface_primary_address_ipv4 }}
{{ ansible_default_ipv4.gateway }}
register: result
until: result.rc == 0
delay: 5
retries: 5

- name: Ping from secondary interface to the gateway
when: interface_secondary_address_ipv4 is defined
shell: >
ping -4 -c 4
-I {{ interface_secondary_address_ipv4 }}
{{ ansible_default_ipv4.gateway }}
register: result
until: result.rc == 0
delay: 5
retries: 5

- name: Show about the primary inteface
when: interface_primary is defined
debug:
var: interface_primary

- name: Show about the secondary inteface
when: interface_secondary is defined
debug:
msg: "{{ netdev.stdout_lines }}"
var: interface_secondary
3 changes: 2 additions & 1 deletion templates/kiss/tasks/commission/submit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
body_format: json
body:
access:
address: "{{ ansible_ssh_host }}"
address_primary: "{{ interface_primary_address_ipv4 | default(ansible_ssh_host) }}"
address_secondary: "{{ interface_secondary_address_ipv4 | default(None) }}"
machine:
uuid: "{{ ansible_host_uuid }}"
power:
Expand Down

0 comments on commit f97c441

Please sign in to comment.