Bläddra i källkod

Merge pull request #509 from sakshiarora13/mappingfile_validations

Mapping file validations
Lucas A. Wilson 3 år sedan
förälder
incheckning
445d043e9a

+ 37 - 0
control_plane/roles/control_plane_common/tasks/count_component_roles.yml

@@ -0,0 +1,37 @@
+# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# limitations under the License.
+---
+
+- name: Count of manager nodes defined
+  set_fact:
+    count_of_manager: "{{ count_of_manager| int + 1 }}"
+  when: item == group_name_manager
+  tags: install
+
+- name: Count of compute nodes defined
+  set_fact:
+    count_of_compute: "{{ count_of_compute| int + 1 }}"
+  when: item == group_name_compute
+  tags: install
+
+- name: Count of login nodes defined
+  set_fact:
+    count_of_login: "{{ count_of_login| int + 1 }}"
+  when: item == group_name_login
+  tags: install
+
+- name: Count of NFS nodes defined
+  set_fact:
+    count_of_nfs_node: "{{ count_of_nfs_node| int + 1 }}"
+  when: item == group_name_nfs
+  tags: install

+ 9 - 1
control_plane/roles/control_plane_common/tasks/main.yml

@@ -38,8 +38,16 @@
   import_tasks: fetch_sm_inputs.yml
   import_tasks: fetch_sm_inputs.yml
   when: ib_switch_support
   when: ib_switch_support
 
 
+- name: Host mapping file validation
+  import_tasks: validate_host_mapping_file.yml
+  when: host_mapping_file_path |length >0
+
+- name: Device mapping file validation
+  import_tasks: validate_device_mapping_file.yml
+  when: mngmnt_mapping_file_path |length >0
+
 - name: Encrypt idrac_tools_vars.yml
 - name: Encrypt idrac_tools_vars.yml
   import_tasks: encrypt_idrac_tools_vars.yml
   import_tasks: encrypt_idrac_tools_vars.yml
 
 
 - name: NFS Server setup for offline repo and awx
 - name: NFS Server setup for offline repo and awx
-  import_tasks: nfs_server_setup.yml
+  import_tasks: nfs_server_setup.yml

+ 82 - 0
control_plane/roles/control_plane_common/tasks/validate_device_mapping_file.yml

@@ -0,0 +1,82 @@
+# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# limitations under the License.
+---
+- name: Check that device mapping file exists at mentioned path
+  stat:
+    path: "{{ mngmnt_mapping_file_path }}"
+  register: stat_result
+  tags: install
+
+- name: Fail if config file doesn't exist
+  fail:
+    msg: "{{ fail_msg_mapping_file + mngmnt_mapping_file_path }}"
+  when: not stat_result.stat.exists
+  tags: install
+
+- name: Read device mapping file from CSV file and return a dictionary
+  read_csv:
+    path: "{{ mngmnt_mapping_file_path }}"
+    key: "{{ mapping_file_key }}"
+  register: device_mapping_file
+  delegate_to: localhost
+  tags: install
+
+- name: Check if header is present in mapping file
+  shell:  set -o pipefail && awk 'NR==1 { print $1}' "{{ mngmnt_mapping_file_path }}"
+  register: mngmnt_header
+  changed_when: false
+  tags: install
+
+- name: Fail if header not in correct format
+  fail:
+    msg: "{{ fail_device_mapping_file_header }}"
+  when: mngmnt_header.stdout !=  device_mapping_header_format
+  tags: install
+
+- name: Check if mapping file is comma seperated
+  shell: awk -F\, '{print NF-1}' "{{ mngmnt_mapping_file_path }}"
+  register: mngmnt_comma_seperated
+  changed_when: false
+  tags: install
+
+- name: Fail if not comma seperated or if all fields are not given
+  fail:
+    msg: "{{ fail_mapping_file_field_seperation }}"
+  when: not(item =="1")
+  with_items: "{{ mngmnt_comma_seperated.stdout_lines }}"
+  tags: install
+
+- name: Initialize count variables
+  set_fact:
+    list_of_ips: []
+    count_total_items: "{{ device_mapping_file.dict |length }}"
+  tags: install
+
+- name: Create list of IPs in mapping file
+  set_fact:
+    list_of_ips: "{{ [ item.value.IP ] + list_of_ips }}"
+  loop: "{{ device_mapping_file.dict | dict2items }}"
+  loop_control:
+    label: "{{ item.value.MAC }}"
+  tags: install
+
+- name: Find count of unique IPs
+  set_fact:
+    count_of_unique_ip : "{{ list_of_ips| unique| length }}"
+  tags: install
+
+- name: Validation to check if unique IPs are provided for each node
+  fail:
+    msg: "{{ fail_mapping_file_duplicate_ip + mngmnt_mapping_file_path }}"
+  when: not(count_of_unique_ip|int == count_total_items|int)
+  tags: install

+ 179 - 0
control_plane/roles/control_plane_common/tasks/validate_host_mapping_file.yml

@@ -0,0 +1,179 @@
+# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# limitations under the License.
+---
+- name: Check that host mapping file exists at mentioned path
+  stat:
+    path: "{{ host_mapping_file_path }}"
+  register: stat_result
+  tags: install
+
+- name: Fail if config file doesn't exist
+  fail:
+    msg: "{{ fail_msg_mapping_file + host_mapping_file_path }}"
+  when: not stat_result.stat.exists
+  tags: install
+
+- name: Read host mapping file from CSV file and return a dictionary
+  read_csv:
+    path: "{{ host_mapping_file_path }}"
+    key: "{{ mapping_file_key }}"
+  register: mapping_file
+  delegate_to: localhost
+  tags: install
+
+- name: Initialize variable for role support in mapping file
+  set_fact:
+    component_role_support: false
+  tags: install
+
+- name: Check if header is present in mapping file
+  shell:  set -o pipefail && awk 'NR==1 { print $1}' "{{ host_mapping_file_path }}"
+  register: mngmnt_header
+  changed_when: false
+  tags: install
+
+- name: Fail if header not in correct format
+  assert:
+    that: (mngmnt_header.stdout ==  host_mapping_header_format) or (mngmnt_header.stdout == host_mapping_header_with_role_format)
+    fail_msg: "{{ fail_mapping_file_header }}"
+  tags: install
+
+- name: Check if mapping file is comma seperated
+  shell: awk -F\, '{print NF-1}' "{{ host_mapping_file_path }}"
+  register: mngmnt_comma_seperated
+  changed_when: false
+  tags: install
+
+- name: Set variable if component roles given in mapping file
+  set_fact:
+    component_role_support: true
+  when: mngmnt_header.stdout == host_mapping_header_with_role_format
+  tags: install
+
+- name: Fail if not comma seperated or if all fields are not given for MAC,Hostname,IP,Component_role
+  fail:
+    msg: "{{ fail_mapping_file_field_seperation }}"
+  when: not(item =="3") and not (item == "-1") and component_role_support
+  with_items: "{{ mngmnt_comma_seperated.stdout_lines }}"
+  tags: install
+
+- name: Fail if not comma seperated or if all fields are not given for MAC,Hostname,IP
+  fail:
+    msg: "{{ fail_mapping_file_field_seperation }}"
+  when: not(item =="2") and not (item == "-1") and not(component_role_support)
+  with_items: "{{ mngmnt_comma_seperated.stdout_lines }}"
+  tags: install
+
+- name: Initialize count variables
+  set_fact:
+    list_of_ips: []
+    list_of_roles: []
+    list_of_hostnames: []
+    count_of_manager: 0
+    count_of_compute: 0
+    count_of_nfs_node: 0
+    count_of_login: 0
+    count_total_items: "{{ mapping_file.dict |length }}"
+  tags: install
+
+- name: Create list of IPs and component roles and hostnames defined in mapping file
+  set_fact:
+    list_of_ips: "{{ [ item.value.IP ] + list_of_ips }}"
+    list_of_hostnames: "{{ [ item.value.Hostname ] + list_of_hostnames }}"
+  loop: "{{ mapping_file.dict | dict2items }}"
+  loop_control:
+    label: "{{ item.value.MAC }}"
+  tags: install
+
+- name: Create list of component roles defined in mapping file
+  set_fact:
+    list_of_roles: "{{ [ item.value.Component_role ] + list_of_roles }}"
+  loop: "{{ mapping_file.dict | dict2items }}"
+  loop_control:
+    label: "{{ item.value.MAC }}"
+  when: component_role_support
+  tags: install
+
+- name: Assert hostnames
+  assert:
+    that:
+      - '"_" not in item'
+      - '"." not in item'
+      - '" " not in item'
+    quiet: yes
+    fail_msg: "{{ fail_mapping_file_hostname_chars + item }}"
+  with_items: "{{ list_of_hostnames }}"
+  tags: install
+
+- name: Find count of unique IPs
+  set_fact:
+    count_of_unique_ip : "{{ list_of_ips| unique| length }}"
+  tags: install
+
+- name: Validation to check if unique IPs are provided for each node
+  fail:
+    msg: "{{ fail_mapping_file_duplicate_ip + host_mapping_file_path }}"
+  when: not(count_of_unique_ip|int == count_total_items|int)
+  tags: install
+
+- name: Find count of unique hostnames
+  set_fact:
+    count_of_unique_hostnames : "{{ list_of_hostnames | unique | length }}"
+  tags: install
+
+- name: Validation to check if unique hostnames are provided for each node
+  fail:
+    msg: "{{ fail_mapping_file_duplicate_hostname }}"
+  when: not(count_of_unique_hostnames|int == count_total_items| int)
+  tags: install
+
+- name: Find count of each component role defined in mapping file
+  include_tasks: count_component_roles.yml
+  loop: "{{ list_of_roles }}"
+  when: component_role_support
+  tags: install
+
+- block:
+  - name: Validation to check if component roles for each node is defined
+    fail:
+      msg: "{{ fail_mapping_file_roles_error }}"
+    when: not( count_total_items|int == (count_of_manager|int + count_of_compute|int + count_of_login|int + count_of_nfs_node|int))
+
+  - name: Validation to check number of manager nodes defined
+    fail:
+      msg: "{{ fail_mapping_file_manager_role }}"
+    when: not (count_of_manager | int  == 1)
+
+  - name: Validation to check number of compute nodes defined
+    fail:
+      msg: "{{ fail_mapping_file_compute_role }}"
+    when: count_of_compute|int  < 1
+
+  - name: Validation to check number of login nodes defined
+    fail:
+      msg: "{{ fail_mapping_file_login_role }}"
+    when: not ( count_of_login|int == 1)
+
+  - name: Validation to check number of nfs nodes defined
+    fail:
+      msg: "{{ fail_mapping_file_nfs_role }}"
+    when: powervault_support and not (count_of_nfs_node|int == 1)
+  tags: install
+
+  rescue:
+  - name: Count of roles defined
+    fail:
+      msg: "{{ count_of_roles_defined }}"
+    tags: install
+
+  when: component_role_support

+ 30 - 0
control_plane/roles/control_plane_common/vars/main.yml

@@ -140,3 +140,33 @@ nfs_services:
   - mountd
   - mountd
   - rpc-bind
   - rpc-bind
   - nfs
   - nfs
+
+# Usage: validate_host_mapping_file.yml
+fail_msg_mapping_file: "Mapping file doesn't exist at given path: "
+mapping_file_key: "MAC"
+fail_mapping_file_header: "Header of csv file is not in correct format.
+                          It should be of the format: MAC,Hostname,IP,Component_role or MAC,Hostname,IP"
+host_mapping_header_format: "MAC,Hostname,IP"
+host_mapping_header_with_role_format: "MAC,Hostname,IP,Component_role"
+fail_mapping_file_field_seperation: "Failed: Mapping file should be comma separated and all fields must be filled."
+fail_mapping_file_duplicate_ip: "Failed: Duplicate ip exists. Please verify following mapping file again: "
+fail_mapping_file_duplicate_hostname: "Failed: Duplicate hostname exists. Please verify host mapping file again."
+fail_mapping_file_hostname_chars: "Hostname should not contain _ or . or space as it will cause error with slurm and K8s. Found in: "
+fail_mapping_file_roles_error: "Failed. Define correct Component Roles for each node.
+                                Component roles can only take values: {{ group_name_manager }}, {{group_name_compute}},
+                                 {{ group_name_login }}, {{ group_name_nfs }}"
+fail_mapping_file_manager_role: "Exactly 1 manager node must be defined"
+fail_mapping_file_compute_role: "Atleast 1 compute node must be defined"
+fail_mapping_file_login_role: "Exactly 1 login node must be defined"
+fail_mapping_file_nfs_role: "Exactly 1 nfs node must be defined"
+count_of_roles_defined: "Component Roles defined: Manager Node: {{ count_of_manager }},
+                        Compute Nodes: {{ count_of_compute }}, Login Node: {{ count_of_login }},
+                        Nfs Node: {{ count_of_nfs_node }}, Total Nodes: {{ count_total_items }} "
+group_name_manager: "manager"
+group_name_compute: "compute"
+group_name_login: "login_node"
+group_name_nfs: "nfs_node"
+
+# Usage: validate_device_mapping_file.yml
+fail_device_mapping_file_header: "Failed: Header (MAC,IP) should be present in the mapping file."
+device_mapping_header_format: "MAC,IP"

+ 3 - 0
examples/host_mapping_file_one_touch.csv

@@ -0,0 +1,3 @@
+MAC,Hostname,IP,Component_role
+xx:yy:zz:aa:bb,server,1.2.3.4,manager
+aa:bb:cc:dd:ee,server2,10.10.11.12,nfs_node

+ 3 - 0
examples/host_mapping_file_os_provisioning.csv

@@ -0,0 +1,3 @@
+MAC,Hostname,IP
+xx:yy:zz:aa:bb,server,1.2.3.4
+aa:bb:cc:dd:ee,server2,10.10.11.12

+ 2 - 0
examples/mapping_device_file.csv

@@ -0,0 +1,2 @@
+MAC,IP
+xx:yy:zz:aa:bb,1.2.3.4

+ 0 - 2
examples/mapping_file.csv

@@ -1,2 +0,0 @@
-MAC,Hostname,IP
-xx:yy:zz:aa:bb,server,1.2.3.4