blob: 58093980ef89304a8c832c563e2de0f21f8a94a9 [file] [log] [blame]
Dennis Dmitrievf4826bc2017-09-28 19:03:54 +03001{#-
21. Check if 'local_metadata' matches to something in 'global_metadata'.
3 If yes, fetch and process the data.
4 If no, initialize necessary data.
5 If partially intersects (keepalived roles for different clusters): add a fail-state warning to the YAML to avoid using the wrong config
62. Set necessary 'params' using the 'local_metadata'
73. Store the 'local_metadata' to the 'global_metadata' if required.
84. Increment/decrement the 'global_metadata' objects if necessary (next IP address, next vrouter ID, master->slave, etc)
9
10global_metadata keep states across the nodes
11local_metadata keep states for the current node only
12
13Example of local_metadata and global_metadata runtime content:
14
15 local_metadata:
16 keepalived_vip_priority:
17 - openstack_control
18 - openstack_database
19 - openstack_message_queue
20 rabbitmq_cluster_role:
21 - openstack_message_queue
22
23 global_metadata:
24 keepalived_vip_priority: # Separate counters
25 openstack_control|openstack_database|openstack_message_queue: 254
26 cicd_control|infra_kvm: 254
27 keepalived_vip_virtual_router_id: # Common counter
28 __latest: 11
29 openstack_control|openstack_database|openstack_message_queue: 10
30 cicd_control|infra_kvm: 11
31 mysql_cluster_role:
32 openstack_database: master
33#}
34
35{%- macro stateful_roles_check(counter_name) %}
36{#- ####################################### -#}
37
38 {#- 1. Check that there is no intersections between different groups of roles for the <counter_name> #}
39 {%- for names, counter in global_metadata.get(counter_name, {}).items() %}
40 {%- set global_roles = names.split('|') %}
41 {%- for local_counter_role_name in local_metadata.get(counter_name, []) %}
42 {%- if local_counter_role_name in global_roles %}
43 {%- set adding_names = local_metadata.get(counter_name, [])|sort|join('|') %}
44 {%- if names != adding_names %}
45 {#- Found unexpected combination of roles, cause the template rendering exception #}
46 {%- include("======> NODE ROLES MAPPING ERROR! Please check the roles for the node '" + inventory_node_name + "' , metaparam '" + counter_name + "':\n======> Existing roles: " + names + "\n======> Adding roles: " + adding_names) %}
47 {%- endif %}
48 {%- endif %}
49 {%- endfor %}
50 {%- endfor %}
51{%- endmacro %}
52
53{%- macro stateful_counter(counter_name, counter_start, counter_end, counter_step, uniq_per_node=True) %}
54{#- ############################################################################# -#}
55{%- if counter_name in local_metadata %}
56 {{- stateful_roles_check(counter_name) }}
57
58 {%- if counter_name not in global_metadata %}
59 {%- set _ = global_metadata.update({counter_name: {}}) %}
60 {%- endif %}
61 {%- set counter_roles_name = local_metadata[counter_name]|sort|join('|') %}
62
63 {%- if uniq_per_node == True %}
64
65 {%- if counter_roles_name not in global_metadata[counter_name] %}
66 {#- Set default value for <counter_roles_name> = <counter_start> #}
67 {%- set _ = global_metadata[counter_name].update({counter_roles_name: counter_start}) %}
68 {%- else %}
69 {#- Increment or decrement value <counter_roles_name> #}
70 {%- set _ = global_metadata[counter_name].update({counter_roles_name: global_metadata[counter_name][counter_roles_name] + counter_step}) %}
71 {%- if global_metadata[counter_name][counter_roles_name] == counter_end %}
72 {# Cause a jinja render exception and make visible the message with correct counter_name #}
73 {%- include("======> VALUE_ERROR: " + counter_name + "=" + counter_end + " is out of bounds!" ) %}
74 {%- endif %}
75 {%- endif %}
76
77 {%- else %}
78
79 {%- if '__latest' not in global_metadata[counter_name] %}
80 {#- Set the value for __latest = <counter_start> #}
81 {%- set _ = global_metadata[counter_name].update({'__latest': counter_start}) %}
82 {%- endif %}
83 {%- if counter_roles_name not in global_metadata[counter_name] %}
84 {%- set _ = global_metadata[counter_name].update({'__latest': global_metadata[counter_name]['__latest'] + counter_step}) %}
85 {%- if global_metadata[counter_name]['__latest'] == counter_end %}
86 {# Cause a jinja render exception and make visible the message with correct counter_name #}
87 {%- include("======> VALUE_ERROR: " + counter_name + "=" + counter_end + " is out of bounds!" ) %}
88 {%- endif %}
89 {%- set _ = global_metadata[counter_name].update({counter_roles_name: global_metadata[counter_name]['__latest']}) %}
90 {%- endif %}
91
92 {%- endif %}
93 {%- set _ = params.update({counter_name: global_metadata[counter_name][counter_roles_name]}) %}
94{%- endif %}
95{%- endmacro %}
96
97{%- macro stateful_masterslave(masterslave_name, master_name='master', slave_name='slave') %}
98{#- ##################################################################################### -#}
99{%- if masterslave_name in local_metadata %}
100 {{- stateful_roles_check(masterslave_name) }}
101
102 {%- if masterslave_name not in global_metadata %}
103 {%- set _ = global_metadata.update({masterslave_name: {}}) %}
104 {%- endif %}
105 {%- set masterslave_roles_name = local_metadata[masterslave_name]|sort|join('|') %}
106
107 {%- if masterslave_roles_name not in global_metadata[masterslave_name] %}
108 {#- Set first value <masterslave_roles_name> = <master_name> #}
109 {%- set _ = global_metadata[masterslave_name].update({masterslave_roles_name: master_name}) %}
110 {%- else %}
111 {#- Set value <masterslave_roles_name> = <slave_name> #}
112 {%- set _ = global_metadata[masterslave_name].update({masterslave_roles_name: slave_name}) %}
113 {%- endif %}
114 {%- set _ = params.update({masterslave_name: global_metadata[masterslave_name][masterslave_roles_name]}) %}
115{%- endif %}
116{%- endmacro %}
117
118{{- stateful_counter('cicd_database_id', counter_start=1, counter_end=255, counter_step=1) }}
119{{- stateful_counter('opencontrail_database_id', counter_start=1, counter_end=255, counter_step=1) }}
120{{- stateful_counter('keepalived_vip_priority', counter_start=254, counter_end=1, counter_step=-1) }}
121{{- stateful_counter('keepalived_vip_virtual_router_id', counter_start=159, counter_end=250, counter_step=1, uniq_per_node=False) }}
122{{- stateful_masterslave('rabbitmq_cluster_role') }}
123{{- stateful_masterslave('mysql_cluster_role') }}
124{{- stateful_masterslave('redis_cluster_role') }}