Merge pull request #12 from damjanek/master
Fix broken dhcpsnippets & set up properly salt-master ip in curtain script
diff --git a/_modules/maas.py b/_modules/maas.py
index c2ba659..4b127d8 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -108,6 +108,7 @@
'updated': [],
}
for name, config_data in config.iteritems():
+ self._update = False
try:
data = self.fill_data(name, config_data, **extra)
if name in all_elements:
@@ -434,63 +435,64 @@
return new
-#class SSHPrefs(MaasObject):
-# def __init__(self):
-# super(SSHPrefs, self).__init__()
-# self._all_elements_url = u'api/2.0/machines/'
-# self._create_url = u'api/2.0/account/prefs/sshkeys/'
-# self._config_path = 'region.sshprefs'
-# self._element_key = 'hostname'
-# self._update_key = 'system_id'
-#
-# def fill_data(self, value):
-# data = {
-# 'key': value,
-# }
-# return data
-#
-# def process(self):
-# config = __salt__['config.get']('maas')
-# for part in self._config_path.split('.'):
-# config = config.get(part, {})
-# extra = {}
-# for name, url_call in self._extra_data_urls.iteritems():
-# key = 'id'
-# if isinstance(url_call, tuple):
-# url_call, key = url_call[:]
-# extra[name] = {v['name']: v[key] for v in
-# json.loads(self._maas.get(url_call).read())}
-# if self._all_elements_url:
-# all_elements = {}
-# elements = self._maas.get(self._all_elements_url).read()
-# res_json = json.loads(elements)
-# for element in res_json:
-# if isinstance(element, (str, unicode)):
-# all_elements[element] = {}
-# else:
-# all_elements[element[self._element_key]] = element
-# else:
-# all_elements = {}
-# ret = {
-# 'success': [],
-# 'errors': {},
-# 'updated': [],
-# }
-# for config_data in config:
-# try:
-# data = self.fill_data(config_data, **extra)
-# self.send(data)
-# ret['success'].append(name)
-# except urllib2.HTTPError as e:
-# etxt = e.read()
-# LOG.exception('Failed for object %s reason %s', name, etxt)
-# ret['errors'][name] = str(etxt)
-# except Exception as e:
-# LOG.exception('Failed for object %s reason %s', name, e)
-# ret['errors'][name] = str(e)
-# if ret['errors']:
-# raise Exception(ret)
-# return ret
+class SSHPrefs(MaasObject):
+ def __init__(self):
+ super(SSHPrefs, self).__init__()
+ self._all_elements_url = None
+ self._create_url = u'api/2.0/account/prefs/sshkeys/'
+ self._config_path = 'region.sshprefs'
+ self._element_key = 'hostname'
+ self._update_key = 'system_id'
+
+ def fill_data(self, value):
+ data = {
+ 'key': value,
+ }
+ return data
+
+ def process(self):
+ config = __salt__['config.get']('maas')
+ for part in self._config_path.split('.'):
+ config = config.get(part, {})
+ extra = {}
+ for name, url_call in self._extra_data_urls.iteritems():
+ key = 'id'
+ if isinstance(url_call, tuple):
+ url_call, key = url_call[:]
+ extra[name] = {v['name']: v[key] for v in
+ json.loads(self._maas.get(url_call).read())}
+ if self._all_elements_url:
+ all_elements = {}
+ elements = self._maas.get(self._all_elements_url).read()
+ res_json = json.loads(elements)
+ for element in res_json:
+ if isinstance(element, (str, unicode)):
+ all_elements[element] = {}
+ else:
+ all_elements[element[self._element_key]] = element
+ else:
+ all_elements = {}
+ ret = {
+ 'success': [],
+ 'errors': {},
+ 'updated': [],
+ }
+ for config_data in config:
+ name = config_data[:10]
+ try:
+ data = self.fill_data(config_data, **extra)
+ self.send(data)
+ ret['success'].append(name)
+ except urllib2.HTTPError as e:
+ etxt = e.read()
+ LOG.exception('Failed for object %s reason %s', name, etxt)
+ ret['errors'][name] = str(etxt)
+ except Exception as e:
+ LOG.exception('Failed for object %s reason %s', name, e)
+ ret['errors'][name] = str(e)
+ if ret['errors']:
+ raise Exception(ret)
+ return ret
class Domain(MaasObject):
def __init__(self):
@@ -585,3 +587,6 @@
def process_domain():
return Domain().process()
+
+def process_sshprefs():
+ return SSHPrefs().process()
diff --git a/maas/files/curtin_userdata_amd64_generic_trusty b/maas/files/curtin_userdata_amd64_generic_trusty
index 07a3273..ecfbc41 100644
--- a/maas/files/curtin_userdata_amd64_generic_trusty
+++ b/maas/files/curtin_userdata_amd64_generic_trusty
@@ -24,7 +24,7 @@
{% endraw %}
salt_02_hostname_set: ["curtin", "in-target", "--", "echo", "{% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}"]
salt_03_hostname_get: ["curtin", "in-target", "--", "sh", "-c", "echo 'id: {% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}' >> /etc/salt/minion"]
- salt_04_master: ["curtin", "in-target", "--", "sh", "-c", "echo 'master: {{grains['ip4_interfaces']['enp0s3'][0]}}' >> /etc/salt/minion"]
+ salt_04_master: ["curtin", "in-target", "--", "sh", "-c", "echo 'master: {{ salt_master_ip }} >> /etc/salt/minion"]
{% raw %}
{{if third_party_drivers and driver}}
driver_00_key_get: curtin in-target -- sh -c "/bin/echo -en '{{key_string}}' > /tmp/maas-{{driver['package']}}.gpg"
diff --git a/maas/files/curtin_userdata_amd64_generic_xenial b/maas/files/curtin_userdata_amd64_generic_xenial
index 8bec9c4..2c4b294 100644
--- a/maas/files/curtin_userdata_amd64_generic_xenial
+++ b/maas/files/curtin_userdata_amd64_generic_xenial
@@ -24,7 +24,7 @@
{% endraw %}
salt_02_hostname_set: ["curtin", "in-target", "--", "echo", "{% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}"]
salt_03_hostname_get: ["curtin", "in-target", "--", "sh", "-c", "echo 'id: {% raw %}{{node.hostname}}{% endraw %}.{{pillar.linux.system.domain}}' >> /etc/salt/minion"]
- salt_04_master: ["curtin", "in-target", "--", "sh", "-c", "echo 'master: {{grains['ip4_interfaces']['enp0s3'][0]}}' >> /etc/salt/minion"]
+ salt_04_master: ["curtin", "in-target", "--", "sh", "-c", "echo 'master: {{ salt_master_ip }} >> /etc/salt/minion"]
{% raw %}
{{if third_party_drivers and driver}}
driver_00_key_get: curtin in-target -- sh -c "/bin/echo -en '{{key_string}}' > /tmp/maas-{{driver['package']}}.gpg"
diff --git a/maas/region.sls b/maas/region.sls
index f2a9e4f..7b8043c 100644
--- a/maas/region.sls
+++ b/maas/region.sls
@@ -53,6 +53,8 @@
- user: root
- group: root
- mode: 644
+ - context:
+ salt_master_ip: {{ region.salt_master_ip }}
- require:
- pkg: maas_region_packages
@@ -63,6 +65,8 @@
- user: root
- group: root
- mode: 644
+ - context:
+ salt_master_ip: {{ region.salt_master_ip }}
- require:
- pkg: maas_region_packages
@@ -149,4 +153,10 @@
- require:
- module: maas_config
+maas_sshprefs:
+ module.run:
+ - name: maas.process_sshprefs
+ - require:
+ - module: maas_config
+
{%- endif %}