Update the template_version alias for all the templates to pike.
[apex-tripleo-heat-templates.git] / firstboot / os-net-config-mappings.yaml
1 heat_template_version: pike
2
3 description: >
4   Configure os-net-config mappings for specific nodes
5   Your environment file needs to look like:
6     parameter_defaults:
7       NetConfigDataLookup:
8         node1:
9           nic1: "00:c8:7c:e6:f0:2e"
10         node2:
11           nic1: "00:18:7d:99:0c:b6"
12         node3:
13           dmiString: 'system-uuid'
14           id: 'A8C85861-1B16-4803-8689-AFC62984F8F6'
15           nic1: em3
16         # Dell PowerEdge
17         nodegroup1:
18           dmiString: "system-product-name"
19           id: "PowerEdge R630"
20           nic1: em3
21           nic2: em1
22           nic3: em2
23         # Cisco UCS B200-M4"
24         nodegroup2:
25           dmiString: "system-product-name"
26           id: "UCSB-B200-M4"
27           nic1: enp7s0
28           nic2: enp6s0
29
30   This will result in the first node* entry where either:
31        a) a mac matches a local device
32     or b) a DMI String matches the specified id
33   being written as a mapping file for os-net-config in
34   /etc/os-net-config/mapping.yaml
35
36 parameters:
37   # Note this requires a liberty heat or newer in the undercloud due to
38   # the 2015-10-15 (which is required to enable str_replace serializing
39   # the json parameter to json, another approch with a string parameter
40   # will be required for older heat versions)
41   NetConfigDataLookup:
42     type: json
43     default: {}
44     description: per-node configuration map
45
46 resources:
47   userdata:
48     type: OS::Heat::MultipartMime
49     properties:
50       parts:
51       - config: {get_resource: OsNetConfigMappings}
52
53   OsNetConfigMappings:
54     type: OS::Heat::SoftwareConfig
55     properties:
56       group: ungrouped
57       config:
58         str_replace:
59           template: |
60             #!/bin/sh
61             eth_addr=$(cat /sys/class/net/*/address | tr '\n' ',')
62             mkdir -p /etc/os-net-config
63
64             # Create an os-net-config mapping file, note this defaults to
65             # /etc/os-net-config/mapping.yaml, so we use that name despite
66             # rendering the result as json
67             echo '$node_lookup' | python -c "
68             import json
69             import sys
70             import copy
71             from subprocess import PIPE, Popen
72             import yaml
73
74             def write_mapping_file(interface_mapping):
75               with open('/etc/os-net-config/mapping.yaml', 'w') as f:
76                 yaml.safe_dump(interface_mapping, f,  default_flow_style=False)
77
78             input = sys.stdin.readline() or '{}'
79             data = json.loads(input)
80             for node in data:
81               interface_mapping = {'interface_mapping':
82                                       copy.deepcopy(data[node])}
83               if 'dmiString' in interface_mapping['interface_mapping']:
84                 del interface_mapping['interface_mapping']['dmiString']
85               if 'id' in interface_mapping['interface_mapping']:
86                 del interface_mapping['interface_mapping']['id']
87               # Match on mac addresses first
88               if any(x in '$eth_addr'.split(',') for x in data[node].values()):
89                 write_mapping_file(interface_mapping)
90                 break
91               # If data contain dmiString and id keys, try to match node(group)
92               if 'dmiString' in data[node] and 'id' in data[node]:
93                 ps = Popen([ 'dmidecode',
94                              '--string', data[node].get('dmiString') ],
95                              stdout=PIPE)
96                 out, err = ps.communicate()
97                 if data[node].get('id') == out.rstrip():
98                   write_mapping_file(interface_mapping)
99                   break
100             "
101           params:
102             $node_lookup: {get_param: NetConfigDataLookup}
103
104 outputs:
105   OS::stack_id:
106     value: {get_resource: userdata}