Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 3 | Module for handling reclass metadata models. |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 4 | |
| 5 | ''' |
| 6 | |
| 7 | from __future__ import absolute_import |
| 8 | |
| 9 | import logging |
| 10 | import os |
| 11 | import sys |
Ales Komarek | a961df4 | 2016-11-21 21:50:24 +0100 | [diff] [blame] | 12 | import six |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 13 | import yaml |
Ales Komarek | a961df4 | 2016-11-21 21:50:24 +0100 | [diff] [blame] | 14 | import json |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 15 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 16 | from reclass import get_storage, output |
| 17 | from reclass.core import Core |
| 18 | from reclass.config import find_and_read_configfile |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 19 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 20 | LOG = logging.getLogger(__name__) |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 21 | |
Ales Komarek | a961df4 | 2016-11-21 21:50:24 +0100 | [diff] [blame] | 22 | |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 23 | def __virtual__(): |
| 24 | ''' |
| 25 | Only load this module if reclass |
| 26 | is installed on this minion. |
| 27 | ''' |
| 28 | return 'reclass' |
| 29 | |
| 30 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 31 | def _get_nodes_dir(): |
| 32 | defaults = find_and_read_configfile() |
| 33 | return os.path.join(defaults.get('inventory_base_uri'), 'nodes') |
| 34 | |
| 35 | |
| 36 | def _get_classes_dir(): |
| 37 | defaults = find_and_read_configfile() |
| 38 | return os.path.join(defaults.get('inventory_base_uri'), 'classes') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 39 | |
| 40 | |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 41 | def _get_node_meta(name, cluster="default", environment="prd", classes=None, parameters=None): |
| 42 | host_name = name.split('.')[0] |
| 43 | domain_name = '.'.join(name.split('.')[1:]) |
| 44 | |
| 45 | if classes == None: |
| 46 | meta_classes = [] |
| 47 | else: |
| 48 | if isinstance(classes, six.string_types): |
| 49 | meta_classes = json.loads(classes) |
| 50 | else: |
| 51 | meta_classes = classes |
| 52 | |
| 53 | if parameters == None: |
| 54 | meta_parameters = {} |
| 55 | else: |
| 56 | if isinstance(parameters, six.string_types): |
| 57 | meta_parameters = json.loads(parameters) |
| 58 | else: |
| 59 | # generate dict from OrderedDict |
| 60 | meta_parameters = {k: v for (k, v) in parameters.items()} |
| 61 | |
| 62 | node_meta = { |
| 63 | 'classes': meta_classes, |
| 64 | 'parameters': { |
| 65 | '_param': meta_parameters, |
| 66 | 'linux': { |
| 67 | 'system': { |
| 68 | 'name': host_name, |
| 69 | 'domain': domain_name, |
| 70 | 'cluster': cluster, |
| 71 | 'environment': environment, |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return node_meta |
| 78 | |
| 79 | |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 80 | def node_create(name, path=None, cluster="default", environment="prd", classes=None, parameters=None, **kwargs): |
| 81 | ''' |
| 82 | Create a reclass node |
| 83 | |
| 84 | :param name: new node FQDN |
| 85 | :param path: custom path in nodes for new node |
| 86 | :param classes: classes given to the new node |
| 87 | :param parameters: parameters given to the new node |
| 88 | :param environment: node's environment |
| 89 | :param cluster: node's cluster |
| 90 | |
| 91 | CLI Examples: |
| 92 | |
| 93 | .. code-block:: bash |
| 94 | |
| 95 | salt '*' reclass.node_create server.domain.com classes=[system.neco1, system.neco2] |
| 96 | salt '*' reclass.node_create namespace/test enabled=False |
| 97 | |
| 98 | ''' |
| 99 | ret = {} |
| 100 | |
| 101 | node = node_get(name=name) |
| 102 | |
| 103 | if node and not "Error" in node: |
| 104 | LOG.debug("node {0} exists".format(name)) |
| 105 | ret[name] = node |
| 106 | return ret |
| 107 | |
| 108 | host_name = name.split('.')[0] |
| 109 | domain_name = '.'.join(name.split('.')[1:]) |
| 110 | |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 111 | node_meta = _get_node_meta(name, cluster, environment, classes, parameters) |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 112 | LOG.debug(node_meta) |
| 113 | |
| 114 | if path == None: |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 115 | file_path = os.path.join(_get_nodes_dir(), name + '.yml') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 116 | else: |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 117 | file_path = os.path.join(_get_nodes_dir(), path, name + '.yml') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 118 | |
| 119 | with open(file_path, 'w') as node_file: |
Ales Komarek | a961df4 | 2016-11-21 21:50:24 +0100 | [diff] [blame] | 120 | node_file.write(yaml.safe_dump(node_meta, default_flow_style=False)) |
Ales Komarek | 71f94b0 | 2016-07-27 14:48:57 +0200 | [diff] [blame] | 121 | |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 122 | return node_get(name) |
| 123 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 124 | |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 125 | def node_delete(name, **kwargs): |
| 126 | ''' |
| 127 | Delete a reclass node |
| 128 | |
| 129 | :params node: Node name |
| 130 | |
| 131 | CLI Examples: |
| 132 | |
| 133 | .. code-block:: bash |
| 134 | |
| 135 | salt '*' reclass.node_delete demo01.domain.com |
| 136 | salt '*' reclass.node_delete name=demo01.domain.com |
| 137 | ''' |
| 138 | |
| 139 | node = node_get(name=name) |
| 140 | |
| 141 | if 'Error' in node: |
| 142 | return {'Error': 'Unable to retreive node'} |
| 143 | |
| 144 | if node[name]['path'] == '': |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 145 | file_path = os.path.join(_get_nodes_dir(), name + '.yml') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 146 | else: |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 147 | file_path = os.path.join(_get_nodes_dir(), node[name]['path'], name + '.yml') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 148 | |
| 149 | os.remove(file_path) |
| 150 | |
| 151 | ret = 'Node {0} deleted'.format(name) |
| 152 | |
| 153 | return ret |
| 154 | |
| 155 | |
| 156 | def node_get(name, path=None, **kwargs): |
| 157 | ''' |
| 158 | Return a specific node |
| 159 | |
| 160 | CLI Examples: |
| 161 | |
| 162 | .. code-block:: bash |
| 163 | |
| 164 | salt '*' reclass.node_get host01.domain.com |
| 165 | salt '*' reclass.node_get name=host02.domain.com |
| 166 | ''' |
| 167 | ret = {} |
| 168 | nodes = node_list(**kwargs) |
| 169 | |
| 170 | if not name in nodes: |
| 171 | return {'Error': 'Error in retrieving node'} |
| 172 | ret[name] = nodes[name] |
| 173 | return ret |
| 174 | |
| 175 | |
| 176 | def node_list(**connection_args): |
| 177 | ''' |
| 178 | Return a list of available nodes |
| 179 | |
| 180 | CLI Example: |
| 181 | |
| 182 | .. code-block:: bash |
| 183 | |
| 184 | salt '*' reclass.node_list |
| 185 | ''' |
| 186 | ret = {} |
| 187 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 188 | for root, sub_folders, files in os.walk(_get_nodes_dir()): |
Adam Tengler | 805666d | 2017-05-15 16:01:13 +0000 | [diff] [blame^] | 189 | for fl in files: |
| 190 | file_path = os.path.join(root, fl) |
| 191 | with open(file_path, 'r') as file_handle: |
| 192 | file_read = yaml.load(file_handle.read()) |
| 193 | file_data = file_read or {} |
| 194 | classes = file_data.get('classes', []) |
| 195 | parameters = file_data.get('parameters', {}).get('_param', []) |
| 196 | name = fl.replace('.yml', '') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 197 | host_name = name.split('.')[0] |
| 198 | domain_name = '.'.join(name.split('.')[1:]) |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 199 | path = root.replace(_get_nodes_dir()+'/', '') |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 200 | ret[name] = { |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 201 | 'name': host_name, |
| 202 | 'domain': domain_name, |
| 203 | 'cluster': 'default', |
| 204 | 'environment': 'prd', |
| 205 | 'path': path, |
| 206 | 'classes': classes, |
| 207 | 'parameters': parameters |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return ret |
| 211 | |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 212 | |
Ales Komarek | 166cc67 | 2016-07-27 14:17:22 +0200 | [diff] [blame] | 213 | def node_update(name, classes=None, parameters=None, **connection_args): |
| 214 | ''' |
| 215 | Update a node metadata information, classes and parameters. |
| 216 | |
| 217 | CLI Examples: |
| 218 | |
| 219 | .. code-block:: bash |
| 220 | |
| 221 | salt '*' reclass.node_update name=nodename classes="[clas1, class2]" |
| 222 | ''' |
| 223 | node = node_get(name=name) |
| 224 | if not node.has_key('Error'): |
Ales Komarek | 71f94b0 | 2016-07-27 14:48:57 +0200 | [diff] [blame] | 225 | node = node[name.split("/")[1]] |
| 226 | else: |
| 227 | return {'Error': 'Error in retrieving node'} |
Ales Komarek | a4a9f57 | 2016-12-03 20:15:50 +0100 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def inventory(**connection_args): |
| 231 | ''' |
| 232 | Get all nodes in inventory and their associated services/roles classification. |
| 233 | |
| 234 | CLI Examples: |
| 235 | |
| 236 | .. code-block:: bash |
| 237 | |
| 238 | salt '*' reclass.inventory |
| 239 | ''' |
| 240 | defaults = find_and_read_configfile() |
| 241 | storage = get_storage(defaults['storage_type'], _get_nodes_dir(), _get_classes_dir()) |
| 242 | reclass = Core(storage, None) |
| 243 | nodes = reclass.inventory()["nodes"] |
| 244 | output = {} |
| 245 | |
| 246 | for node in nodes: |
| 247 | service_classification = [] |
| 248 | role_classification = [] |
| 249 | for service in nodes[node]['parameters']: |
| 250 | if service not in ['_param', 'private_keys', 'public_keys', 'known_hosts']: |
| 251 | service_classification.append(service) |
| 252 | for role in nodes[node]['parameters'][service]: |
| 253 | if role not in ['_support', '_orchestrate', 'common']: |
| 254 | role_classification.append('%s.%s' % (service, role)) |
| 255 | output[node] = { |
| 256 | 'roles': role_classification, |
| 257 | 'services': service_classification, |
| 258 | } |
| 259 | return output |