Initial commit
[ta/infra-ansible.git] / roles / allocate_cpu_cores / tasks / main.yml
diff --git a/roles/allocate_cpu_cores/tasks/main.yml b/roles/allocate_cpu_cores/tasks/main.yml
new file mode 100644 (file)
index 0000000..cca66d8
--- /dev/null
@@ -0,0 +1,122 @@
+---
+
+# Copyright 2019 Nokia
+
+# 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.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+- name: Prepare.
+  set_fact:
+    ncir_cpu_remain: []
+    ncir_cpu_sets: {}
+
+- name: Get CPU topology.
+  get_cpu_topology:
+    var: ncir_cpu_topology
+- debug:
+    var: ncir_cpu_topology
+
+- name: Make default performance profile.
+  set_fact:
+    perf_profile: {
+      platform_cpus: "{{ ncir_cpu_topology | cpu_topology_defaults(hosts[hostname]['service_profiles'], ncir_default_platform_cpus, virtual_env, hosts | length < 2) }}"
+    }
+- debug:
+    var: perf_profile
+
+- name: Get performance profile.
+  set_fact:
+    perf_profile: "{{ perf_profile | combine(performance_profiles[hosts[hostname]['performance_profiles'][0]]) }}"
+  when: "'performance_profiles' in hosts[hostname]"
+- debug:
+    var: perf_profile
+
+- name: Make CPU sets.
+  include_tasks: make_cpu_sets.yaml
+  loop_control:
+    loop_var: cpu_set
+  with_items:
+      - { 'name': 'ovs_dpdk', 'opt': "{{ cpu_set_mapping['ovs_dpdk']['opt'] }}", 'where': 'tail' }
+      - { 'name': 'platform', 'opt': "{{ cpu_set_mapping['platform']['opt'] }}", 'where': 'head' }
+  when: "cpu_set.opt in perf_profile"
+
+- name: Initialize VM CPU share.
+  set_fact:
+    vm_share_percent: 100
+
+- name: Set collocated VM CPU set.
+  set_fact:
+    vm_share_percent: "{{ 100 - (perf_profile['caas_cpu_pool_share'] | default(100) | int) }}"
+  when:
+    - "'compute' in hosts[hostname]['service_profiles']"
+    - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
+
+- name: Make VM CPU sets.
+  include_tasks: make_cpu_sets.yaml
+  loop_control:
+    loop_var: cpu_set
+  with_items:
+    - { 'name': 'vm', 'request': "{{ ncir_cpu_topology | get_cpu_count_by_percent(vm_share_percent) }}", 'where': 'tail' }
+  when:
+    - "'compute' in hosts[hostname]['service_profiles']"
+
+- name: Calculate CaaS CPU counts
+  set_fact:
+    perf_profile: "{{ perf_profile | combine({ item.key: ncir_cpu_topology | get_cpu_count_by_percent(item.value) }) }}"
+  with_dict: "{{ perf_profile['caas_cpu_pools'] | default({}) }}"
+  when: hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
+
+- name: Make CaaS CPU sets.
+  include_tasks: make_cpu_sets.yaml
+  loop_control:
+    loop_var: cpu_set
+  with_items:
+    - { 'name': 'caas_exclusive', 'opt': "{{ cpu_set_mapping['caas_exclusive']['opt'] }}", 'where': 'tail' }
+    - { 'name': 'caas_shared',    'opt': "{{ cpu_set_mapping['caas_shared']['opt'] }}",    'where': 'tail' }
+  when:
+    - "cpu_set.opt in perf_profile"
+    - hosts[hostname]['service_profiles'] | intersect(caas_service_profiles)
+
+- name: Make set of remaining CPUs.
+  set_fact:
+    ncir_cpu_remain: "{{ ncir_cpu_remain | union(item) }}"
+  with_items: "{{ ncir_cpu_topology.values() }}"
+- debug:
+    var: ncir_cpu_remain
+
+- name: Allocate remaining CPUs to platform.
+  set_fact:
+    ncir_cpu_sets: "{{ ncir_cpu_sets | combine({ 'platform': ncir_cpu_sets['platform'] | default([]) | union(ncir_cpu_remain) }) }}"
+    ncir_cpu_remain: []
+  when: "ncir_cpu_remain | length > 0"
+
+- debug:
+    var: ncir_cpu_sets
+
+- name: Make sure facts directory exists.
+  file:
+    path: "{{ allocation_fact_file | dirname }}"
+    state: directory
+    mode: 0755
+
+- name: Save CPU allocation.
+  template:
+    src: ncir_cpu_allocation.fact.j2
+    dest: "{{ allocation_fact_file }}"
+  register: cpu_allocation
+
+- name: Refresh facts.
+  setup:
+  when:
+    - cpu_allocation.changed
+    - not skip_facts_reload | default(False)