Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nyx
Created February 26, 2014 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyx/8ed344254250c7b25c79 to your computer and use it in GitHub Desktop.
Save nyx/8ed344254250c7b25c79 to your computer and use it in GitHub Desktop.
allow salt-cloud to install specified master key
diff --git a/salt/cloud/__init__.py b/salt/cloud/__init__.py
index 5c0787d..ca5d938 100644
--- a/salt/cloud/__init__.py
+++ b/salt/cloud/__init__.py
@@ -1500,6 +1500,46 @@ class Map(Cloud):
'''
Execute the contents of the VM map
'''
+ def gen_master_keys(master_profile):
+ '''
+ Generates master private/public keypair.
+ '''
+ keysize = salt.config.get_cloud_config_value('keysize', master_profile, self.opts)
+ return salt.utils.cloud.gen_keys(keysize)
+
+ def load_master_keys(master_pem_path, master_pub_path):
+ '''
+ Loads master private/public keypair from specified files.
+ '''
+ def file_contents(path):
+ '''
+ Returns contents of file at given path or raises SaltCloudException if unable
+ '''
+ try:
+ fp = open(path, 'r')
+ except IOError as e:
+ msg = "Error reading file: %s: %s" % (path, str(e))
+ log.error(msg)
+ raise SaltCloudException(msg)
+ else:
+ with fp:
+ return fp.read()
+ master_pem = file_contents(master_pem_path)
+ master_pub = file_contents(master_pub_path)
+ return (master_pem, master_pub)
+
+ def master_keys(master_profile):
+ '''
+ Loads master public private keypair if paths specified, else generates them.
+ Returns tuple (<priv>, <pub>) where 'priv' and 'pub' contain key data
+ '''
+ master_pem_path = salt.config.get_cloud_config_value('master_pem_path', master_profile, self.opts)
+ master_pub_path = salt.config.get_cloud_config_value('master_pub_path', master_profile, self.opts)
+ if master_pem_path and master_pub_path:
+ return load_master_keys(master_pem_path, master_pub_path)
+ else:
+ return gen_master_keys(master_profile)
+
if self._has_loop(dmap):
msg = 'Uh-oh, that cloud map has a dependency loop!'
log.error(msg)
@@ -1553,9 +1593,7 @@ class Map(Cloud):
log.debug(
'Generating master keys for {0[name]!r}'.format(master_profile)
)
- priv, pub = salt.utils.cloud.gen_keys(
- salt.config.get_cloud_config_value('keysize', master_profile, self.opts)
- )
+ priv, pub = master_keys(master_profile)
master_profile['master_pub'] = pub
master_profile['master_pem'] = priv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment