blob: 8f94b3e64dc24baf7054d62f3cd50ebd5767972f [file] [log] [blame]
Ales Komarek166cc672016-07-27 14:17:22 +02001# -*- coding: utf-8 -*-
2'''
3Management of reclass metadata
4==============================
5
6.. code-block:: yaml
7
8 node01:
9 reclass.node_present:
10 - name: hostaname.domain.com
11 - path: _generated
12 - cluster: default
13 - environment: prd
14 - classes:
15 - neco.service.neco
16 - neco2.class.neco.dal
17 - parameters:
18 neco: value1
19 neco2: value2
20
Adam Tengler8a1cf402017-05-16 10:59:35 +000021.. code-block:: yaml
22
23 node_meta_01:
24 reclass.cluster_meta_present:
25 - name: my_key
26 - value: my_value
Ales Komarek166cc672016-07-27 14:17:22 +020027'''
Adam Tengler8a1cf402017-05-16 10:59:35 +000028from __future__ import absolute_import
29
Adam Tengler805666d2017-05-15 16:01:13 +000030import json
Adam Tengler8a1cf402017-05-16 10:59:35 +000031import os
Adam Tengler805666d2017-05-15 16:01:13 +000032import six
Ales Komarek166cc672016-07-27 14:17:22 +020033
Adam Tengler8a1cf402017-05-16 10:59:35 +000034from reclass.config import find_and_read_configfile
35
Ales Komarek166cc672016-07-27 14:17:22 +020036
37def __virtual__():
38 '''
Adam Tengler805666d2017-05-15 16:01:13 +000039 Only load if the reclass module is in __salt__
Ales Komarek166cc672016-07-27 14:17:22 +020040 '''
Adam Tengler805666d2017-05-15 16:01:13 +000041 return 'reclass' if 'reclass.node_list' in __salt__ else False
42
43
Adam Tengler8a1cf402017-05-16 10:59:35 +000044def _get_classes_dir():
45 defaults = find_and_read_configfile()
46 return os.path.join(defaults.get('inventory_base_uri'), 'classes')
47
48
49def _get_cluster_dir():
50 classes_dir = _get_classes_dir()
51 return os.path.join(classes_dir, 'cluster')
52
53
Adam Tengler805666d2017-05-15 16:01:13 +000054def node_present(name, path=None, cluster="default", environment="prd", classes=None, parameters=None, **kwargs):
55 '''
56 Ensure that the reclass node exists
57
58 :param name: new node FQDN
59 :param path: custom path in nodes for new node
60 :param classes: classes given to the new node
61 :param parameters: parameters given to the new node
62 :param environment: node's environment
63 :param cluster: node's cluster
64
65 '''
66 ret = {'name': name,
67 'changes': {},
68 'result': True,
69 'comment': 'Node "{0}" already exists and it is in correct state'.format(name)}
70
71 # Check if node is already present
72 node = __salt__['reclass.node_get'](name, **kwargs)
73
74 if 'Error' in node:
75 # Create node
76 __salt__['reclass.node_create'](name, path, cluster, environment, classes, parameters, **kwargs)
77 ret['comment'] = 'Node "{0}" has been created'.format(name)
78 ret['changes']['Node'] = 'Created'
79 return ret
Ales Komarek166cc672016-07-27 14:17:22 +020080
Adam Tengler8a1cf402017-05-16 10:59:35 +000081
82def cluster_meta_present(name, value, file_name="overrides.yml", cluster="", **kwargs):
83 '''
84 Ensures that the cluster metadata entry exists
85
86 :param name: Metadata entry name
87 :param value: Metadata entry value
88 :param file_name: Name of metadata file, defaults to overrides.yml
89 :param cluster: Name of cluster directory to put the metadata file into, optional
90 '''
91 value = value or ""
92 cluster = cluster or ""
93 path = os.path.join(_get_cluster_dir(), cluster, file_name)
94 ret = {'name': name,
95 'changes': {},
96 'result': True,
97 'comment': 'Cluster metadata entry "{0}" already exists and is in correct state'.format(name)}
98 meta_check = __salt__['reclass.cluster_meta_get'](name, path, **kwargs)
99 if not meta_check:
100 __salt__['reclass.cluster_meta_set'](name, value, path, **kwargs)
101 ret['comment'] = 'Cluster meta entry {0} has been created'.format(name)
102 ret['changes']['Meta Entry'] = 'Cluster meta entry %s: "%s" has been created' % (name, value)
103 elif 'Error' in meta_check:
104 ret['comment'] = meta_check.get('Error')
105 ret['result'] = False
106 elif meta_check[name] != value:
107 __salt__['reclass.cluster_meta_set'](name, value, path, **kwargs)
108 ret['comment'] = 'Cluster metadata entry %s has been changed' % (name,)
109 ret['changes']['Old Meta Entry'] = '%s: "%s"' % (name, meta_check[name])
110 ret['changes']['New Meta Entry'] = '%s: "%s"' % (name, value)
111 return ret
112
113
114def cluster_meta_absent(name, file_name="overrides.yml", cluster="", **kwargs):
115 '''
116 Ensures that the cluster metadata entry does not exist
117
118 :param name: Metadata entry name
119 :param file_name: Name of metadata file, defaults to overrides.yml
120 :param cluster: Name of cluster directory to put the metadata file into, optional
121 '''
122 cluster = cluster or ""
123 path = os.path.join(_get_cluster_dir(), cluster, file_name)
124 ret = {'name': name,
125 'changes': {},
126 'result': True,
127 'comment': 'Cluster metadata entry "{0}" is already absent'.format(name)}
128 meta_check = __salt__['reclass.cluster_meta_get'](name, path, **kwargs)
129 if meta_check:
130 __salt__['reclass.cluster_meta_delete'](name, path, **kwargs)
131 ret['comment'] = 'Cluster metadata entry {0} has been deleted'.format(name)
132 ret['changes']['Meta Entry'] = 'Cluster metadata entry %s: "%s" has been deleted' % (name, meta_check[name])
133 elif 'Error' in meta_check:
134 ret['comment'] = meta_check.get('Error')
135 ret['result'] = False
136 return ret
137