repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
saltstack/salt | salt/utils/cloud.py | rename_key | def rename_key(pki_dir, id_, new_id):
'''
Rename a key, when an instance has also been renamed
'''
oldkey = os.path.join(pki_dir, 'minions', id_)
newkey = os.path.join(pki_dir, 'minions', new_id)
if os.path.isfile(oldkey):
os.rename(oldkey, newkey) | python | def rename_key(pki_dir, id_, new_id):
'''
Rename a key, when an instance has also been renamed
'''
oldkey = os.path.join(pki_dir, 'minions', id_)
newkey = os.path.join(pki_dir, 'minions', new_id)
if os.path.isfile(oldkey):
os.rename(oldkey, newkey) | [
"def",
"rename_key",
"(",
"pki_dir",
",",
"id_",
",",
"new_id",
")",
":",
"oldkey",
"=",
"os",
".",
"path",
".",
"join",
"(",
"pki_dir",
",",
"'minions'",
",",
"id_",
")",
"newkey",
"=",
"os",
".",
"path",
".",
"join",
"(",
"pki_dir",
",",
"'minion... | Rename a key, when an instance has also been renamed | [
"Rename",
"a",
"key",
"when",
"an",
"instance",
"has",
"also",
"been",
"renamed"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L291-L298 | train |
saltstack/salt | salt/utils/cloud.py | minion_config | def minion_config(opts, vm_):
'''
Return a minion's configuration for the provided options and VM
'''
# Don't start with a copy of the default minion opts; they're not always
# what we need. Some default options are Null, let's set a reasonable default
minion = {
'master': 'salt',
'log_level': 'info',
'hash_type': 'sha256',
}
# Now, let's update it to our needs
minion['id'] = vm_['name']
master_finger = salt.config.get_cloud_config_value('master_finger', vm_, opts)
if master_finger is not None:
minion['master_finger'] = master_finger
minion.update(
# Get ANY defined minion settings, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
salt.config.get_cloud_config_value(
'minion', vm_, opts, default={}, search_global=True
)
)
make_master = salt.config.get_cloud_config_value('make_master', vm_, opts)
if 'master' not in minion and make_master is not True:
raise SaltCloudConfigError(
'A master setting was not defined in the minion\'s configuration.'
)
# Get ANY defined grains settings, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
minion.setdefault('grains', {}).update(
salt.config.get_cloud_config_value(
'grains', vm_, opts, default={}, search_global=True
)
)
return minion | python | def minion_config(opts, vm_):
'''
Return a minion's configuration for the provided options and VM
'''
# Don't start with a copy of the default minion opts; they're not always
# what we need. Some default options are Null, let's set a reasonable default
minion = {
'master': 'salt',
'log_level': 'info',
'hash_type': 'sha256',
}
# Now, let's update it to our needs
minion['id'] = vm_['name']
master_finger = salt.config.get_cloud_config_value('master_finger', vm_, opts)
if master_finger is not None:
minion['master_finger'] = master_finger
minion.update(
# Get ANY defined minion settings, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
salt.config.get_cloud_config_value(
'minion', vm_, opts, default={}, search_global=True
)
)
make_master = salt.config.get_cloud_config_value('make_master', vm_, opts)
if 'master' not in minion and make_master is not True:
raise SaltCloudConfigError(
'A master setting was not defined in the minion\'s configuration.'
)
# Get ANY defined grains settings, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
minion.setdefault('grains', {}).update(
salt.config.get_cloud_config_value(
'grains', vm_, opts, default={}, search_global=True
)
)
return minion | [
"def",
"minion_config",
"(",
"opts",
",",
"vm_",
")",
":",
"# Don't start with a copy of the default minion opts; they're not always",
"# what we need. Some default options are Null, let's set a reasonable default",
"minion",
"=",
"{",
"'master'",
":",
"'salt'",
",",
"'log_level'",... | Return a minion's configuration for the provided options and VM | [
"Return",
"a",
"minion",
"s",
"configuration",
"for",
"the",
"provided",
"options",
"and",
"VM"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L301-L344 | train |
saltstack/salt | salt/utils/cloud.py | master_config | def master_config(opts, vm_):
'''
Return a master's configuration for the provided options and VM
'''
# Let's get a copy of the salt master default options
master = copy.deepcopy(salt.config.DEFAULT_MASTER_OPTS)
# Some default options are Null, let's set a reasonable default
master.update(
log_level='info',
log_level_logfile='info',
hash_type='sha256'
)
# Get ANY defined master setting, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
master.update(
salt.config.get_cloud_config_value(
'master', vm_, opts, default={}, search_global=True
)
)
return master | python | def master_config(opts, vm_):
'''
Return a master's configuration for the provided options and VM
'''
# Let's get a copy of the salt master default options
master = copy.deepcopy(salt.config.DEFAULT_MASTER_OPTS)
# Some default options are Null, let's set a reasonable default
master.update(
log_level='info',
log_level_logfile='info',
hash_type='sha256'
)
# Get ANY defined master setting, merging data, in the following order
# 1. VM config
# 2. Profile config
# 3. Global configuration
master.update(
salt.config.get_cloud_config_value(
'master', vm_, opts, default={}, search_global=True
)
)
return master | [
"def",
"master_config",
"(",
"opts",
",",
"vm_",
")",
":",
"# Let's get a copy of the salt master default options",
"master",
"=",
"copy",
".",
"deepcopy",
"(",
"salt",
".",
"config",
".",
"DEFAULT_MASTER_OPTS",
")",
"# Some default options are Null, let's set a reasonable ... | Return a master's configuration for the provided options and VM | [
"Return",
"a",
"master",
"s",
"configuration",
"for",
"the",
"provided",
"options",
"and",
"VM"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L347-L369 | train |
saltstack/salt | salt/utils/cloud.py | salt_config_to_yaml | def salt_config_to_yaml(configuration, line_break='\n'):
'''
Return a salt configuration dictionary, master or minion, as a yaml dump
'''
return salt.utils.yaml.safe_dump(
configuration,
line_break=line_break,
default_flow_style=False) | python | def salt_config_to_yaml(configuration, line_break='\n'):
'''
Return a salt configuration dictionary, master or minion, as a yaml dump
'''
return salt.utils.yaml.safe_dump(
configuration,
line_break=line_break,
default_flow_style=False) | [
"def",
"salt_config_to_yaml",
"(",
"configuration",
",",
"line_break",
"=",
"'\\n'",
")",
":",
"return",
"salt",
".",
"utils",
".",
"yaml",
".",
"safe_dump",
"(",
"configuration",
",",
"line_break",
"=",
"line_break",
",",
"default_flow_style",
"=",
"False",
"... | Return a salt configuration dictionary, master or minion, as a yaml dump | [
"Return",
"a",
"salt",
"configuration",
"dictionary",
"master",
"or",
"minion",
"as",
"a",
"yaml",
"dump"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L372-L379 | train |
saltstack/salt | salt/utils/cloud.py | bootstrap | def bootstrap(vm_, opts=None):
'''
This is the primary entry point for logging into any system (POSIX or
Windows) to install Salt. It will make the decision on its own as to which
deploy function to call.
'''
if opts is None:
opts = __opts__
deploy_config = salt.config.get_cloud_config_value(
'deploy',
vm_, opts, default=False)
inline_script_config = salt.config.get_cloud_config_value(
'inline_script',
vm_, opts, default=None)
if deploy_config is False and inline_script_config is None:
return {
'Error': {
'No Deploy': '\'deploy\' is not enabled. Not deploying.'
}
}
if vm_.get('driver') == 'saltify':
saltify_driver = True
else:
saltify_driver = False
key_filename = salt.config.get_cloud_config_value(
'key_filename', vm_, opts, search_global=False,
default=salt.config.get_cloud_config_value(
'ssh_keyfile', vm_, opts, search_global=False, default=None
)
)
if key_filename is not None and not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined ssh_keyfile \'{0}\' does not exist'.format(
key_filename
)
)
has_ssh_agent = False
if (opts.get('ssh_agent', False) and
'SSH_AUTH_SOCK' in os.environ and
stat.S_ISSOCK(os.stat(os.environ['SSH_AUTH_SOCK']).st_mode)):
has_ssh_agent = True
if (key_filename is None and
salt.config.get_cloud_config_value(
'password', vm_, opts, default=None
) is None and
salt.config.get_cloud_config_value(
'win_password', vm_, opts, default=None
) is None and
has_ssh_agent is False):
raise SaltCloudSystemExit(
'Cannot deploy Salt in a VM if the \'key_filename\' setting '
'is not set and there is no password set for the VM. '
'Check the provider docs for \'change_password\' option if it '
'is supported by your provider.'
)
ret = {}
minion_conf = minion_config(opts, vm_)
deploy_script_code = os_script(
salt.config.get_cloud_config_value(
'os', vm_, opts, default='bootstrap-salt'
),
vm_, opts, minion_conf
)
ssh_username = salt.config.get_cloud_config_value(
'ssh_username', vm_, opts, default='root'
)
if 'file_transport' not in opts:
opts['file_transport'] = vm_.get('file_transport', 'sftp')
# If we haven't generated any keys yet, do so now.
if 'pub_key' not in vm_ and 'priv_key' not in vm_:
log.debug('Generating keys for \'%s\'', vm_['name'])
vm_['priv_key'], vm_['pub_key'] = gen_keys(
salt.config.get_cloud_config_value(
'keysize',
vm_,
opts
)
)
key_id = vm_.get('name')
if 'append_domain' in vm_:
key_id = '.'.join([key_id, vm_['append_domain']])
accept_key(
opts['pki_dir'], vm_['pub_key'], key_id
)
if 'os' not in vm_:
vm_['os'] = salt.config.get_cloud_config_value(
'script',
vm_,
opts
)
# NOTE: deploy_kwargs is also used to pass inline_script variable content
# to run_inline_script function
host = salt.config.get_cloud_config_value('ssh_host', vm_, opts)
deploy_kwargs = {
'opts': opts,
'host': host,
'port': salt.config.get_cloud_config_value(
'ssh_port', vm_, opts, default=22
),
'salt_host': vm_.get('salt_host', host),
'username': ssh_username,
'script': deploy_script_code,
'inline_script': inline_script_config,
'name': vm_['name'],
'has_ssh_agent': has_ssh_agent,
'tmp_dir': salt.config.get_cloud_config_value(
'tmp_dir', vm_, opts, default='/tmp/.saltcloud'
),
'vm_': vm_,
'start_action': opts['start_action'],
'parallel': opts['parallel'],
'sock_dir': opts['sock_dir'],
'conf_file': opts['conf_file'],
'minion_pem': vm_['priv_key'],
'minion_pub': vm_['pub_key'],
'master_sign_pub_file': salt.config.get_cloud_config_value(
'master_sign_pub_file', vm_, opts, default=None),
'keep_tmp': opts['keep_tmp'],
'sudo': salt.config.get_cloud_config_value(
'sudo', vm_, opts, default=(ssh_username != 'root')
),
'sudo_password': salt.config.get_cloud_config_value(
'sudo_password', vm_, opts, default=None
),
'tty': salt.config.get_cloud_config_value(
'tty', vm_, opts, default=True
),
'password': salt.config.get_cloud_config_value(
'password', vm_, opts, search_global=False
),
'key_filename': key_filename,
'script_args': salt.config.get_cloud_config_value(
'script_args', vm_, opts
),
'script_env': salt.config.get_cloud_config_value(
'script_env', vm_, opts
),
'minion_conf': minion_conf,
'force_minion_config': salt.config.get_cloud_config_value(
'force_minion_config', vm_, opts, default=False
),
'preseed_minion_keys': vm_.get('preseed_minion_keys', None),
'display_ssh_output': salt.config.get_cloud_config_value(
'display_ssh_output', vm_, opts, default=True
),
'known_hosts_file': salt.config.get_cloud_config_value(
'known_hosts_file', vm_, opts, default='/dev/null'
),
'file_map': salt.config.get_cloud_config_value(
'file_map', vm_, opts, default=None
),
'maxtries': salt.config.get_cloud_config_value(
'wait_for_passwd_maxtries', vm_, opts, default=15
),
'preflight_cmds': salt.config.get_cloud_config_value(
'preflight_cmds', vm_, opts, default=[]
),
'cloud_grains': {'driver': vm_['driver'],
'provider': vm_['provider'],
'profile': vm_['profile']
}
}
inline_script_kwargs = deploy_kwargs.copy() # make a copy at this point
# forward any info about possible ssh gateway to deploy script
# as some providers need also a 'gateway' configuration
if 'gateway' in vm_:
deploy_kwargs.update({'gateway': vm_['gateway']})
# Deploy salt-master files, if necessary
if salt.config.get_cloud_config_value('make_master', vm_, opts) is True:
deploy_kwargs['make_master'] = True
deploy_kwargs['master_pub'] = vm_['master_pub']
deploy_kwargs['master_pem'] = vm_['master_pem']
master_conf = master_config(opts, vm_)
deploy_kwargs['master_conf'] = master_conf
if master_conf.get('syndic_master', None):
deploy_kwargs['make_syndic'] = True
deploy_kwargs['make_minion'] = salt.config.get_cloud_config_value(
'make_minion', vm_, opts, default=True
)
if saltify_driver:
deploy_kwargs['wait_for_passwd_maxtries'] = 0 # No need to wait/retry with Saltify
win_installer = salt.config.get_cloud_config_value(
'win_installer', vm_, opts
)
if win_installer:
deploy_kwargs['port'] = salt.config.get_cloud_config_value(
'smb_port', vm_, opts, default=445
)
deploy_kwargs['win_installer'] = win_installer
minion = minion_config(opts, vm_)
deploy_kwargs['master'] = minion['master']
deploy_kwargs['username'] = salt.config.get_cloud_config_value(
'win_username', vm_, opts, default='Administrator'
)
win_pass = salt.config.get_cloud_config_value(
'win_password', vm_, opts, default=''
)
if win_pass:
deploy_kwargs['password'] = win_pass
deploy_kwargs['use_winrm'] = salt.config.get_cloud_config_value(
'use_winrm', vm_, opts, default=False
)
deploy_kwargs['winrm_port'] = salt.config.get_cloud_config_value(
'winrm_port', vm_, opts, default=5986
)
deploy_kwargs['winrm_use_ssl'] = salt.config.get_cloud_config_value(
'winrm_use_ssl', vm_, opts, default=True
)
deploy_kwargs['winrm_verify_ssl'] = salt.config.get_cloud_config_value(
'winrm_verify_ssl', vm_, opts, default=True
)
if saltify_driver:
deploy_kwargs['port_timeout'] = 1 # No need to wait/retry with Saltify
# Store what was used to the deploy the VM
event_kwargs = copy.deepcopy(deploy_kwargs)
del event_kwargs['opts']
del event_kwargs['minion_pem']
del event_kwargs['minion_pub']
del event_kwargs['sudo_password']
if 'password' in event_kwargs:
del event_kwargs['password']
ret['deploy_kwargs'] = event_kwargs
fire_event(
'event',
'executing deploy script',
'salt/cloud/{0}/deploying'.format(vm_['name']),
args={'kwargs': salt.utils.data.simple_types_filter(event_kwargs)},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
if inline_script_config and deploy_config is False:
inline_script_deployed = run_inline_script(**inline_script_kwargs)
if inline_script_deployed is not False:
log.info('Inline script(s) ha(s|ve) run on %s', vm_['name'])
ret['deployed'] = False
return ret
else:
if win_installer:
deployed = deploy_windows(**deploy_kwargs)
else:
deployed = deploy_script(**deploy_kwargs)
if inline_script_config:
inline_script_deployed = run_inline_script(**inline_script_kwargs)
if inline_script_deployed is not False:
log.info('Inline script(s) ha(s|ve) run on %s', vm_['name'])
if deployed is not False:
ret['deployed'] = True
if deployed is not True:
ret.update(deployed)
log.info('Salt installed on %s', vm_['name'])
return ret
log.error('Failed to start Salt on host %s', vm_['name'])
return {
'Error': {
'Not Deployed': 'Failed to start Salt on host {0}'.format(
vm_['name']
)
}
} | python | def bootstrap(vm_, opts=None):
'''
This is the primary entry point for logging into any system (POSIX or
Windows) to install Salt. It will make the decision on its own as to which
deploy function to call.
'''
if opts is None:
opts = __opts__
deploy_config = salt.config.get_cloud_config_value(
'deploy',
vm_, opts, default=False)
inline_script_config = salt.config.get_cloud_config_value(
'inline_script',
vm_, opts, default=None)
if deploy_config is False and inline_script_config is None:
return {
'Error': {
'No Deploy': '\'deploy\' is not enabled. Not deploying.'
}
}
if vm_.get('driver') == 'saltify':
saltify_driver = True
else:
saltify_driver = False
key_filename = salt.config.get_cloud_config_value(
'key_filename', vm_, opts, search_global=False,
default=salt.config.get_cloud_config_value(
'ssh_keyfile', vm_, opts, search_global=False, default=None
)
)
if key_filename is not None and not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined ssh_keyfile \'{0}\' does not exist'.format(
key_filename
)
)
has_ssh_agent = False
if (opts.get('ssh_agent', False) and
'SSH_AUTH_SOCK' in os.environ and
stat.S_ISSOCK(os.stat(os.environ['SSH_AUTH_SOCK']).st_mode)):
has_ssh_agent = True
if (key_filename is None and
salt.config.get_cloud_config_value(
'password', vm_, opts, default=None
) is None and
salt.config.get_cloud_config_value(
'win_password', vm_, opts, default=None
) is None and
has_ssh_agent is False):
raise SaltCloudSystemExit(
'Cannot deploy Salt in a VM if the \'key_filename\' setting '
'is not set and there is no password set for the VM. '
'Check the provider docs for \'change_password\' option if it '
'is supported by your provider.'
)
ret = {}
minion_conf = minion_config(opts, vm_)
deploy_script_code = os_script(
salt.config.get_cloud_config_value(
'os', vm_, opts, default='bootstrap-salt'
),
vm_, opts, minion_conf
)
ssh_username = salt.config.get_cloud_config_value(
'ssh_username', vm_, opts, default='root'
)
if 'file_transport' not in opts:
opts['file_transport'] = vm_.get('file_transport', 'sftp')
# If we haven't generated any keys yet, do so now.
if 'pub_key' not in vm_ and 'priv_key' not in vm_:
log.debug('Generating keys for \'%s\'', vm_['name'])
vm_['priv_key'], vm_['pub_key'] = gen_keys(
salt.config.get_cloud_config_value(
'keysize',
vm_,
opts
)
)
key_id = vm_.get('name')
if 'append_domain' in vm_:
key_id = '.'.join([key_id, vm_['append_domain']])
accept_key(
opts['pki_dir'], vm_['pub_key'], key_id
)
if 'os' not in vm_:
vm_['os'] = salt.config.get_cloud_config_value(
'script',
vm_,
opts
)
# NOTE: deploy_kwargs is also used to pass inline_script variable content
# to run_inline_script function
host = salt.config.get_cloud_config_value('ssh_host', vm_, opts)
deploy_kwargs = {
'opts': opts,
'host': host,
'port': salt.config.get_cloud_config_value(
'ssh_port', vm_, opts, default=22
),
'salt_host': vm_.get('salt_host', host),
'username': ssh_username,
'script': deploy_script_code,
'inline_script': inline_script_config,
'name': vm_['name'],
'has_ssh_agent': has_ssh_agent,
'tmp_dir': salt.config.get_cloud_config_value(
'tmp_dir', vm_, opts, default='/tmp/.saltcloud'
),
'vm_': vm_,
'start_action': opts['start_action'],
'parallel': opts['parallel'],
'sock_dir': opts['sock_dir'],
'conf_file': opts['conf_file'],
'minion_pem': vm_['priv_key'],
'minion_pub': vm_['pub_key'],
'master_sign_pub_file': salt.config.get_cloud_config_value(
'master_sign_pub_file', vm_, opts, default=None),
'keep_tmp': opts['keep_tmp'],
'sudo': salt.config.get_cloud_config_value(
'sudo', vm_, opts, default=(ssh_username != 'root')
),
'sudo_password': salt.config.get_cloud_config_value(
'sudo_password', vm_, opts, default=None
),
'tty': salt.config.get_cloud_config_value(
'tty', vm_, opts, default=True
),
'password': salt.config.get_cloud_config_value(
'password', vm_, opts, search_global=False
),
'key_filename': key_filename,
'script_args': salt.config.get_cloud_config_value(
'script_args', vm_, opts
),
'script_env': salt.config.get_cloud_config_value(
'script_env', vm_, opts
),
'minion_conf': minion_conf,
'force_minion_config': salt.config.get_cloud_config_value(
'force_minion_config', vm_, opts, default=False
),
'preseed_minion_keys': vm_.get('preseed_minion_keys', None),
'display_ssh_output': salt.config.get_cloud_config_value(
'display_ssh_output', vm_, opts, default=True
),
'known_hosts_file': salt.config.get_cloud_config_value(
'known_hosts_file', vm_, opts, default='/dev/null'
),
'file_map': salt.config.get_cloud_config_value(
'file_map', vm_, opts, default=None
),
'maxtries': salt.config.get_cloud_config_value(
'wait_for_passwd_maxtries', vm_, opts, default=15
),
'preflight_cmds': salt.config.get_cloud_config_value(
'preflight_cmds', vm_, opts, default=[]
),
'cloud_grains': {'driver': vm_['driver'],
'provider': vm_['provider'],
'profile': vm_['profile']
}
}
inline_script_kwargs = deploy_kwargs.copy() # make a copy at this point
# forward any info about possible ssh gateway to deploy script
# as some providers need also a 'gateway' configuration
if 'gateway' in vm_:
deploy_kwargs.update({'gateway': vm_['gateway']})
# Deploy salt-master files, if necessary
if salt.config.get_cloud_config_value('make_master', vm_, opts) is True:
deploy_kwargs['make_master'] = True
deploy_kwargs['master_pub'] = vm_['master_pub']
deploy_kwargs['master_pem'] = vm_['master_pem']
master_conf = master_config(opts, vm_)
deploy_kwargs['master_conf'] = master_conf
if master_conf.get('syndic_master', None):
deploy_kwargs['make_syndic'] = True
deploy_kwargs['make_minion'] = salt.config.get_cloud_config_value(
'make_minion', vm_, opts, default=True
)
if saltify_driver:
deploy_kwargs['wait_for_passwd_maxtries'] = 0 # No need to wait/retry with Saltify
win_installer = salt.config.get_cloud_config_value(
'win_installer', vm_, opts
)
if win_installer:
deploy_kwargs['port'] = salt.config.get_cloud_config_value(
'smb_port', vm_, opts, default=445
)
deploy_kwargs['win_installer'] = win_installer
minion = minion_config(opts, vm_)
deploy_kwargs['master'] = minion['master']
deploy_kwargs['username'] = salt.config.get_cloud_config_value(
'win_username', vm_, opts, default='Administrator'
)
win_pass = salt.config.get_cloud_config_value(
'win_password', vm_, opts, default=''
)
if win_pass:
deploy_kwargs['password'] = win_pass
deploy_kwargs['use_winrm'] = salt.config.get_cloud_config_value(
'use_winrm', vm_, opts, default=False
)
deploy_kwargs['winrm_port'] = salt.config.get_cloud_config_value(
'winrm_port', vm_, opts, default=5986
)
deploy_kwargs['winrm_use_ssl'] = salt.config.get_cloud_config_value(
'winrm_use_ssl', vm_, opts, default=True
)
deploy_kwargs['winrm_verify_ssl'] = salt.config.get_cloud_config_value(
'winrm_verify_ssl', vm_, opts, default=True
)
if saltify_driver:
deploy_kwargs['port_timeout'] = 1 # No need to wait/retry with Saltify
# Store what was used to the deploy the VM
event_kwargs = copy.deepcopy(deploy_kwargs)
del event_kwargs['opts']
del event_kwargs['minion_pem']
del event_kwargs['minion_pub']
del event_kwargs['sudo_password']
if 'password' in event_kwargs:
del event_kwargs['password']
ret['deploy_kwargs'] = event_kwargs
fire_event(
'event',
'executing deploy script',
'salt/cloud/{0}/deploying'.format(vm_['name']),
args={'kwargs': salt.utils.data.simple_types_filter(event_kwargs)},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
if inline_script_config and deploy_config is False:
inline_script_deployed = run_inline_script(**inline_script_kwargs)
if inline_script_deployed is not False:
log.info('Inline script(s) ha(s|ve) run on %s', vm_['name'])
ret['deployed'] = False
return ret
else:
if win_installer:
deployed = deploy_windows(**deploy_kwargs)
else:
deployed = deploy_script(**deploy_kwargs)
if inline_script_config:
inline_script_deployed = run_inline_script(**inline_script_kwargs)
if inline_script_deployed is not False:
log.info('Inline script(s) ha(s|ve) run on %s', vm_['name'])
if deployed is not False:
ret['deployed'] = True
if deployed is not True:
ret.update(deployed)
log.info('Salt installed on %s', vm_['name'])
return ret
log.error('Failed to start Salt on host %s', vm_['name'])
return {
'Error': {
'Not Deployed': 'Failed to start Salt on host {0}'.format(
vm_['name']
)
}
} | [
"def",
"bootstrap",
"(",
"vm_",
",",
"opts",
"=",
"None",
")",
":",
"if",
"opts",
"is",
"None",
":",
"opts",
"=",
"__opts__",
"deploy_config",
"=",
"salt",
".",
"config",
".",
"get_cloud_config_value",
"(",
"'deploy'",
",",
"vm_",
",",
"opts",
",",
"de... | This is the primary entry point for logging into any system (POSIX or
Windows) to install Salt. It will make the decision on its own as to which
deploy function to call. | [
"This",
"is",
"the",
"primary",
"entry",
"point",
"for",
"logging",
"into",
"any",
"system",
"(",
"POSIX",
"or",
"Windows",
")",
"to",
"install",
"Salt",
".",
"It",
"will",
"make",
"the",
"decision",
"on",
"its",
"own",
"as",
"to",
"which",
"deploy",
"... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L382-L668 | train |
saltstack/salt | salt/utils/cloud.py | ssh_usernames | def ssh_usernames(vm_, opts, default_users=None):
'''
Return the ssh_usernames. Defaults to a built-in list of users for trying.
'''
if default_users is None:
default_users = ['root']
usernames = salt.config.get_cloud_config_value(
'ssh_username', vm_, opts
)
if not isinstance(usernames, list):
usernames = [usernames]
# get rid of None's or empty names
usernames = [x for x in usernames if x]
# Keep a copy of the usernames the user might have provided
initial = usernames[:]
# Add common usernames to the list to be tested
for name in default_users:
if name not in usernames:
usernames.append(name)
# Add the user provided usernames to the end of the list since enough time
# might need to pass before the remote service is available for logins and
# the proper username might have passed its iteration.
# This has detected in a CentOS 5.7 EC2 image
usernames.extend(initial)
return usernames | python | def ssh_usernames(vm_, opts, default_users=None):
'''
Return the ssh_usernames. Defaults to a built-in list of users for trying.
'''
if default_users is None:
default_users = ['root']
usernames = salt.config.get_cloud_config_value(
'ssh_username', vm_, opts
)
if not isinstance(usernames, list):
usernames = [usernames]
# get rid of None's or empty names
usernames = [x for x in usernames if x]
# Keep a copy of the usernames the user might have provided
initial = usernames[:]
# Add common usernames to the list to be tested
for name in default_users:
if name not in usernames:
usernames.append(name)
# Add the user provided usernames to the end of the list since enough time
# might need to pass before the remote service is available for logins and
# the proper username might have passed its iteration.
# This has detected in a CentOS 5.7 EC2 image
usernames.extend(initial)
return usernames | [
"def",
"ssh_usernames",
"(",
"vm_",
",",
"opts",
",",
"default_users",
"=",
"None",
")",
":",
"if",
"default_users",
"is",
"None",
":",
"default_users",
"=",
"[",
"'root'",
"]",
"usernames",
"=",
"salt",
".",
"config",
".",
"get_cloud_config_value",
"(",
"... | Return the ssh_usernames. Defaults to a built-in list of users for trying. | [
"Return",
"the",
"ssh_usernames",
".",
"Defaults",
"to",
"a",
"built",
"-",
"in",
"list",
"of",
"users",
"for",
"trying",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L671-L699 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_fun | def wait_for_fun(fun, timeout=900, **kwargs):
'''
Wait until a function finishes, or times out
'''
start = time.time()
log.debug('Attempting function %s', fun)
trycount = 0
while True:
trycount += 1
try:
response = fun(**kwargs)
if not isinstance(response, bool):
return response
except Exception as exc:
log.debug('Caught exception in wait_for_fun: %s', exc)
time.sleep(1)
log.debug('Retrying function %s on (try %s)', fun, trycount)
if time.time() - start > timeout:
log.error('Function timed out: %s', timeout)
return False | python | def wait_for_fun(fun, timeout=900, **kwargs):
'''
Wait until a function finishes, or times out
'''
start = time.time()
log.debug('Attempting function %s', fun)
trycount = 0
while True:
trycount += 1
try:
response = fun(**kwargs)
if not isinstance(response, bool):
return response
except Exception as exc:
log.debug('Caught exception in wait_for_fun: %s', exc)
time.sleep(1)
log.debug('Retrying function %s on (try %s)', fun, trycount)
if time.time() - start > timeout:
log.error('Function timed out: %s', timeout)
return False | [
"def",
"wait_for_fun",
"(",
"fun",
",",
"timeout",
"=",
"900",
",",
"*",
"*",
"kwargs",
")",
":",
"start",
"=",
"time",
".",
"time",
"(",
")",
"log",
".",
"debug",
"(",
"'Attempting function %s'",
",",
"fun",
")",
"trycount",
"=",
"0",
"while",
"True... | Wait until a function finishes, or times out | [
"Wait",
"until",
"a",
"function",
"finishes",
"or",
"times",
"out"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L702-L721 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_port | def wait_for_port(host, port=22, timeout=900, gateway=None):
'''
Wait until a connection to the specified port can be made on a specified
host. This is usually port 22 (for SSH), but in the case of Windows
installations, it might be port 445 (for psexec). It may also be an
alternate port for SSH, depending on the base image.
'''
start = time.time()
# Assign test ports because if a gateway is defined
# we first want to test the gateway before the host.
test_ssh_host = host
test_ssh_port = port
if gateway:
ssh_gateway = gateway['ssh_gateway']
ssh_gateway_port = 22
if ':' in ssh_gateway:
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
if 'ssh_gateway_port' in gateway:
ssh_gateway_port = gateway['ssh_gateway_port']
test_ssh_host = ssh_gateway
test_ssh_port = ssh_gateway_port
log.debug(
'Attempting connection to host %s on port %s '
'via gateway %s on port %s',
host, port, ssh_gateway, ssh_gateway_port
)
else:
log.debug('Attempting connection to host %s on port %s', host, port)
trycount = 0
while True:
trycount += 1
try:
if socket.inet_pton(socket.AF_INET6, host):
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.settimeout(5)
sock.connect((test_ssh_host, int(test_ssh_port)))
# Stop any remaining reads/writes on the socket
sock.shutdown(socket.SHUT_RDWR)
# Close it!
sock.close()
break
except socket.error as exc:
log.debug('Caught exception in wait_for_port: %s', exc)
time.sleep(1)
if time.time() - start > timeout:
log.error('Port connection timed out: %s', timeout)
return False
log.debug(
'Retrying connection to %s %s on port %s (try %s)',
'gateway' if gateway else 'host', test_ssh_host, test_ssh_port, trycount
)
if not gateway:
return True
# Let the user know that his gateway is good!
log.debug('Gateway %s on port %s is reachable.', test_ssh_host, test_ssh_port)
# Now we need to test the host via the gateway.
# We will use netcat on the gateway to test the port
ssh_args = []
ssh_args.extend([
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking=no',
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile=/dev/null',
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
])
# There should never be both a password and an ssh key passed in, so
if 'ssh_gateway_key' in gateway:
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(gateway['ssh_gateway_key'])
])
# Netcat command testing remote port
command = 'nc -z -w5 -q0 {0} {1}'.format(host, port)
# SSH command
pcmd = 'ssh {0} {1}@{2} -p {3} {4}'.format(
' '.join(ssh_args), gateway['ssh_gateway_user'], ssh_gateway,
ssh_gateway_port, pipes.quote('date')
)
cmd = 'ssh {0} {1}@{2} -p {3} {4}'.format(
' '.join(ssh_args), gateway['ssh_gateway_user'], ssh_gateway,
ssh_gateway_port, pipes.quote(command)
)
log.debug('SSH command: \'%s\'', cmd)
kwargs = {'display_ssh_output': False,
'password': gateway.get('ssh_gateway_password', None)}
trycount = 0
usable_gateway = False
gateway_retries = 5
while True:
trycount += 1
# test gateway usage
if not usable_gateway:
pstatus = _exec_ssh_cmd(pcmd, allow_failure=True, **kwargs)
if pstatus == 0:
usable_gateway = True
else:
gateway_retries -= 1
log.error(
'Gateway usage seems to be broken, '
'password error ? Tries left: %s', gateway_retries)
if not gateway_retries:
raise SaltCloudExecutionFailure(
'SSH gateway is reachable but we can not login')
# then try to reach out the target
if usable_gateway:
status = _exec_ssh_cmd(cmd, allow_failure=True, **kwargs)
# Get the exit code of the SSH command.
# If 0 then the port is open.
if status == 0:
return True
time.sleep(1)
if time.time() - start > timeout:
log.error('Port connection timed out: %s', timeout)
return False
log.debug(
'Retrying connection to host %s on port %s '
'via gateway %s on port %s. (try %s)',
host, port, ssh_gateway, ssh_gateway_port, trycount
) | python | def wait_for_port(host, port=22, timeout=900, gateway=None):
'''
Wait until a connection to the specified port can be made on a specified
host. This is usually port 22 (for SSH), but in the case of Windows
installations, it might be port 445 (for psexec). It may also be an
alternate port for SSH, depending on the base image.
'''
start = time.time()
# Assign test ports because if a gateway is defined
# we first want to test the gateway before the host.
test_ssh_host = host
test_ssh_port = port
if gateway:
ssh_gateway = gateway['ssh_gateway']
ssh_gateway_port = 22
if ':' in ssh_gateway:
ssh_gateway, ssh_gateway_port = ssh_gateway.split(':')
if 'ssh_gateway_port' in gateway:
ssh_gateway_port = gateway['ssh_gateway_port']
test_ssh_host = ssh_gateway
test_ssh_port = ssh_gateway_port
log.debug(
'Attempting connection to host %s on port %s '
'via gateway %s on port %s',
host, port, ssh_gateway, ssh_gateway_port
)
else:
log.debug('Attempting connection to host %s on port %s', host, port)
trycount = 0
while True:
trycount += 1
try:
if socket.inet_pton(socket.AF_INET6, host):
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.settimeout(5)
sock.connect((test_ssh_host, int(test_ssh_port)))
# Stop any remaining reads/writes on the socket
sock.shutdown(socket.SHUT_RDWR)
# Close it!
sock.close()
break
except socket.error as exc:
log.debug('Caught exception in wait_for_port: %s', exc)
time.sleep(1)
if time.time() - start > timeout:
log.error('Port connection timed out: %s', timeout)
return False
log.debug(
'Retrying connection to %s %s on port %s (try %s)',
'gateway' if gateway else 'host', test_ssh_host, test_ssh_port, trycount
)
if not gateway:
return True
# Let the user know that his gateway is good!
log.debug('Gateway %s on port %s is reachable.', test_ssh_host, test_ssh_port)
# Now we need to test the host via the gateway.
# We will use netcat on the gateway to test the port
ssh_args = []
ssh_args.extend([
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking=no',
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile=/dev/null',
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
])
# There should never be both a password and an ssh key passed in, so
if 'ssh_gateway_key' in gateway:
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(gateway['ssh_gateway_key'])
])
# Netcat command testing remote port
command = 'nc -z -w5 -q0 {0} {1}'.format(host, port)
# SSH command
pcmd = 'ssh {0} {1}@{2} -p {3} {4}'.format(
' '.join(ssh_args), gateway['ssh_gateway_user'], ssh_gateway,
ssh_gateway_port, pipes.quote('date')
)
cmd = 'ssh {0} {1}@{2} -p {3} {4}'.format(
' '.join(ssh_args), gateway['ssh_gateway_user'], ssh_gateway,
ssh_gateway_port, pipes.quote(command)
)
log.debug('SSH command: \'%s\'', cmd)
kwargs = {'display_ssh_output': False,
'password': gateway.get('ssh_gateway_password', None)}
trycount = 0
usable_gateway = False
gateway_retries = 5
while True:
trycount += 1
# test gateway usage
if not usable_gateway:
pstatus = _exec_ssh_cmd(pcmd, allow_failure=True, **kwargs)
if pstatus == 0:
usable_gateway = True
else:
gateway_retries -= 1
log.error(
'Gateway usage seems to be broken, '
'password error ? Tries left: %s', gateway_retries)
if not gateway_retries:
raise SaltCloudExecutionFailure(
'SSH gateway is reachable but we can not login')
# then try to reach out the target
if usable_gateway:
status = _exec_ssh_cmd(cmd, allow_failure=True, **kwargs)
# Get the exit code of the SSH command.
# If 0 then the port is open.
if status == 0:
return True
time.sleep(1)
if time.time() - start > timeout:
log.error('Port connection timed out: %s', timeout)
return False
log.debug(
'Retrying connection to host %s on port %s '
'via gateway %s on port %s. (try %s)',
host, port, ssh_gateway, ssh_gateway_port, trycount
) | [
"def",
"wait_for_port",
"(",
"host",
",",
"port",
"=",
"22",
",",
"timeout",
"=",
"900",
",",
"gateway",
"=",
"None",
")",
":",
"start",
"=",
"time",
".",
"time",
"(",
")",
"# Assign test ports because if a gateway is defined",
"# we first want to test the gateway... | Wait until a connection to the specified port can be made on a specified
host. This is usually port 22 (for SSH), but in the case of Windows
installations, it might be port 445 (for psexec). It may also be an
alternate port for SSH, depending on the base image. | [
"Wait",
"until",
"a",
"connection",
"to",
"the",
"specified",
"port",
"can",
"be",
"made",
"on",
"a",
"specified",
"host",
".",
"This",
"is",
"usually",
"port",
"22",
"(",
"for",
"SSH",
")",
"but",
"in",
"the",
"case",
"of",
"Windows",
"installations",
... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L724-L860 | train |
saltstack/salt | salt/utils/cloud.py | run_winexe_command | def run_winexe_command(cmd, args, host, username, password, port=445):
'''
Run a command remotly via the winexe executable
'''
creds = "-U '{0}%{1}' //{2}".format(
username,
password,
host
)
logging_creds = "-U '{0}%XXX-REDACTED-XXX' //{1}".format(
username,
host
)
cmd = 'winexe {0} {1} {2}'.format(creds, cmd, args)
logging_cmd = 'winexe {0} {1} {2}'.format(logging_creds, cmd, args)
return win_cmd(cmd, logging_command=logging_cmd) | python | def run_winexe_command(cmd, args, host, username, password, port=445):
'''
Run a command remotly via the winexe executable
'''
creds = "-U '{0}%{1}' //{2}".format(
username,
password,
host
)
logging_creds = "-U '{0}%XXX-REDACTED-XXX' //{1}".format(
username,
host
)
cmd = 'winexe {0} {1} {2}'.format(creds, cmd, args)
logging_cmd = 'winexe {0} {1} {2}'.format(logging_creds, cmd, args)
return win_cmd(cmd, logging_command=logging_cmd) | [
"def",
"run_winexe_command",
"(",
"cmd",
",",
"args",
",",
"host",
",",
"username",
",",
"password",
",",
"port",
"=",
"445",
")",
":",
"creds",
"=",
"\"-U '{0}%{1}' //{2}\"",
".",
"format",
"(",
"username",
",",
"password",
",",
"host",
")",
"logging_cred... | Run a command remotly via the winexe executable | [
"Run",
"a",
"command",
"remotly",
"via",
"the",
"winexe",
"executable"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L938-L953 | train |
saltstack/salt | salt/utils/cloud.py | run_psexec_command | def run_psexec_command(cmd, args, host, username, password, port=445):
'''
Run a command remotly using the psexec protocol
'''
if has_winexe() and not HAS_PSEXEC:
ret_code = run_winexe_command(cmd, args, host, username, password, port)
return None, None, ret_code
service_name = 'PS-Exec-{0}'.format(uuid.uuid4())
stdout, stderr, ret_code = '', '', None
client = Client(host, username, password, port=port, encrypt=False, service_name=service_name)
client.connect()
try:
client.create_service()
stdout, stderr, ret_code = client.run_executable(cmd, args)
finally:
client.remove_service()
client.disconnect()
return stdout, stderr, ret_code | python | def run_psexec_command(cmd, args, host, username, password, port=445):
'''
Run a command remotly using the psexec protocol
'''
if has_winexe() and not HAS_PSEXEC:
ret_code = run_winexe_command(cmd, args, host, username, password, port)
return None, None, ret_code
service_name = 'PS-Exec-{0}'.format(uuid.uuid4())
stdout, stderr, ret_code = '', '', None
client = Client(host, username, password, port=port, encrypt=False, service_name=service_name)
client.connect()
try:
client.create_service()
stdout, stderr, ret_code = client.run_executable(cmd, args)
finally:
client.remove_service()
client.disconnect()
return stdout, stderr, ret_code | [
"def",
"run_psexec_command",
"(",
"cmd",
",",
"args",
",",
"host",
",",
"username",
",",
"password",
",",
"port",
"=",
"445",
")",
":",
"if",
"has_winexe",
"(",
")",
"and",
"not",
"HAS_PSEXEC",
":",
"ret_code",
"=",
"run_winexe_command",
"(",
"cmd",
",",... | Run a command remotly using the psexec protocol | [
"Run",
"a",
"command",
"remotly",
"using",
"the",
"psexec",
"protocol"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L956-L973 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_winexe | def wait_for_winexe(host, port, username, password, timeout=900):
'''
Wait until winexe connection can be established.
'''
start = time.time()
log.debug(
'Attempting winexe connection to host %s on port %s',
host, port
)
try_count = 0
while True:
try_count += 1
try:
# Shell out to winexe to check %TEMP%
ret_code = run_winexe_command(
"sc", "query winexesvc", host, username, password, port
)
if ret_code == 0:
log.debug('winexe connected...')
return True
log.debug('Return code was %s', ret_code)
except socket.error as exc:
log.debug('Caught exception in wait_for_winexesvc: %s', exc)
if time.time() - start > timeout:
return False
time.sleep(1) | python | def wait_for_winexe(host, port, username, password, timeout=900):
'''
Wait until winexe connection can be established.
'''
start = time.time()
log.debug(
'Attempting winexe connection to host %s on port %s',
host, port
)
try_count = 0
while True:
try_count += 1
try:
# Shell out to winexe to check %TEMP%
ret_code = run_winexe_command(
"sc", "query winexesvc", host, username, password, port
)
if ret_code == 0:
log.debug('winexe connected...')
return True
log.debug('Return code was %s', ret_code)
except socket.error as exc:
log.debug('Caught exception in wait_for_winexesvc: %s', exc)
if time.time() - start > timeout:
return False
time.sleep(1) | [
"def",
"wait_for_winexe",
"(",
"host",
",",
"port",
",",
"username",
",",
"password",
",",
"timeout",
"=",
"900",
")",
":",
"start",
"=",
"time",
".",
"time",
"(",
")",
"log",
".",
"debug",
"(",
"'Attempting winexe connection to host %s on port %s'",
",",
"h... | Wait until winexe connection can be established. | [
"Wait",
"until",
"winexe",
"connection",
"can",
"be",
"established",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L976-L1002 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_psexecsvc | def wait_for_psexecsvc(host, port, username, password, timeout=900):
'''
Wait until psexec connection can be established.
'''
if has_winexe() and not HAS_PSEXEC:
return wait_for_winexe(host, port, username, password, timeout)
start = time.time()
try_count = 0
while True:
try_count += 1
ret_code = 1
try:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c hostname', host, username, password, port=port
)
except Exception as exc:
log.exception("Unable to execute command")
if ret_code == 0:
log.debug('psexec connected...')
return True
if time.time() - start > timeout:
return False
log.debug(
'Retrying psexec connection to host %s on port %s (try %s)',
host, port, try_count
)
time.sleep(1) | python | def wait_for_psexecsvc(host, port, username, password, timeout=900):
'''
Wait until psexec connection can be established.
'''
if has_winexe() and not HAS_PSEXEC:
return wait_for_winexe(host, port, username, password, timeout)
start = time.time()
try_count = 0
while True:
try_count += 1
ret_code = 1
try:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c hostname', host, username, password, port=port
)
except Exception as exc:
log.exception("Unable to execute command")
if ret_code == 0:
log.debug('psexec connected...')
return True
if time.time() - start > timeout:
return False
log.debug(
'Retrying psexec connection to host %s on port %s (try %s)',
host, port, try_count
)
time.sleep(1) | [
"def",
"wait_for_psexecsvc",
"(",
"host",
",",
"port",
",",
"username",
",",
"password",
",",
"timeout",
"=",
"900",
")",
":",
"if",
"has_winexe",
"(",
")",
"and",
"not",
"HAS_PSEXEC",
":",
"return",
"wait_for_winexe",
"(",
"host",
",",
"port",
",",
"use... | Wait until psexec connection can be established. | [
"Wait",
"until",
"psexec",
"connection",
"can",
"be",
"established",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1005-L1031 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_winrm | def wait_for_winrm(host, port, username, password, timeout=900, use_ssl=True, verify=True):
'''
Wait until WinRM connection can be established.
'''
# Ensure the winrm service is listening before attempting to connect
wait_for_port(host=host, port=port, timeout=timeout)
start = time.time()
log.debug(
'Attempting WinRM connection to host %s on port %s',
host, port
)
transport = 'ssl'
if not use_ssl:
transport = 'ntlm'
trycount = 0
while True:
trycount += 1
try:
winrm_kwargs = {'target': host,
'auth': (username, password),
'transport': transport}
if not verify:
log.debug("SSL validation for WinRM disabled.")
winrm_kwargs['server_cert_validation'] = 'ignore'
s = winrm.Session(**winrm_kwargs)
if hasattr(s.protocol, 'set_timeout'):
s.protocol.set_timeout(15)
log.trace('WinRM endpoint url: %s', s.url)
r = s.run_cmd('sc query winrm')
if r.status_code == 0:
log.debug('WinRM session connected...')
return s
log.debug('Return code was %s', r.status_code)
except WinRMTransportError as exc:
log.debug('Caught exception in wait_for_winrm: %s', exc)
except InvalidCredentialsError as exc:
log.error((
'Caught Invalid Credentials error in wait_for_winrm. '
'You may have an incorrect username/password, '
'the new minion\'s WinRM configuration is not correct, '
'the customization spec has not finished, '
'or we are waiting for an account rename policy to take effect. '
'Connection attempts will continue to be made until the WinRM timeout '
'has been exceeded.'
))
except ReadTimeout as exc:
log.error('Caught Read Timeout while waiting for winrm.')
except ConnectionError as exc:
log.error((
'Caught Connection Error while waiting for winrm. '
'Connection attempts will continue to be made until the WinRM timeout '
'has been exceeded.'
))
if time.time() - start > timeout:
log.error('WinRM connection timed out: %s', timeout)
return None
log.debug(
'Retrying WinRM connection to host %s on port %s (try %s)',
host, port, trycount
)
time.sleep(1) | python | def wait_for_winrm(host, port, username, password, timeout=900, use_ssl=True, verify=True):
'''
Wait until WinRM connection can be established.
'''
# Ensure the winrm service is listening before attempting to connect
wait_for_port(host=host, port=port, timeout=timeout)
start = time.time()
log.debug(
'Attempting WinRM connection to host %s on port %s',
host, port
)
transport = 'ssl'
if not use_ssl:
transport = 'ntlm'
trycount = 0
while True:
trycount += 1
try:
winrm_kwargs = {'target': host,
'auth': (username, password),
'transport': transport}
if not verify:
log.debug("SSL validation for WinRM disabled.")
winrm_kwargs['server_cert_validation'] = 'ignore'
s = winrm.Session(**winrm_kwargs)
if hasattr(s.protocol, 'set_timeout'):
s.protocol.set_timeout(15)
log.trace('WinRM endpoint url: %s', s.url)
r = s.run_cmd('sc query winrm')
if r.status_code == 0:
log.debug('WinRM session connected...')
return s
log.debug('Return code was %s', r.status_code)
except WinRMTransportError as exc:
log.debug('Caught exception in wait_for_winrm: %s', exc)
except InvalidCredentialsError as exc:
log.error((
'Caught Invalid Credentials error in wait_for_winrm. '
'You may have an incorrect username/password, '
'the new minion\'s WinRM configuration is not correct, '
'the customization spec has not finished, '
'or we are waiting for an account rename policy to take effect. '
'Connection attempts will continue to be made until the WinRM timeout '
'has been exceeded.'
))
except ReadTimeout as exc:
log.error('Caught Read Timeout while waiting for winrm.')
except ConnectionError as exc:
log.error((
'Caught Connection Error while waiting for winrm. '
'Connection attempts will continue to be made until the WinRM timeout '
'has been exceeded.'
))
if time.time() - start > timeout:
log.error('WinRM connection timed out: %s', timeout)
return None
log.debug(
'Retrying WinRM connection to host %s on port %s (try %s)',
host, port, trycount
)
time.sleep(1) | [
"def",
"wait_for_winrm",
"(",
"host",
",",
"port",
",",
"username",
",",
"password",
",",
"timeout",
"=",
"900",
",",
"use_ssl",
"=",
"True",
",",
"verify",
"=",
"True",
")",
":",
"# Ensure the winrm service is listening before attempting to connect",
"wait_for_port... | Wait until WinRM connection can be established. | [
"Wait",
"until",
"WinRM",
"connection",
"can",
"be",
"established",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1034-L1096 | train |
saltstack/salt | salt/utils/cloud.py | validate_windows_cred_winexe | def validate_windows_cred_winexe(host,
username='Administrator',
password=None,
retries=10,
retry_delay=1):
'''
Check if the windows credentials are valid
'''
cmd = "winexe -U '{0}%{1}' //{2} \"hostname\"".format(
username,
password,
host
)
logging_cmd = "winexe -U '{0}%XXX-REDACTED-XXX' //{1} \"hostname\"".format(
username,
host
)
for i in range(retries):
ret_code = win_cmd(
cmd,
logging_command=logging_cmd
)
return ret_code == 0 | python | def validate_windows_cred_winexe(host,
username='Administrator',
password=None,
retries=10,
retry_delay=1):
'''
Check if the windows credentials are valid
'''
cmd = "winexe -U '{0}%{1}' //{2} \"hostname\"".format(
username,
password,
host
)
logging_cmd = "winexe -U '{0}%XXX-REDACTED-XXX' //{1} \"hostname\"".format(
username,
host
)
for i in range(retries):
ret_code = win_cmd(
cmd,
logging_command=logging_cmd
)
return ret_code == 0 | [
"def",
"validate_windows_cred_winexe",
"(",
"host",
",",
"username",
"=",
"'Administrator'",
",",
"password",
"=",
"None",
",",
"retries",
"=",
"10",
",",
"retry_delay",
"=",
"1",
")",
":",
"cmd",
"=",
"\"winexe -U '{0}%{1}' //{2} \\\"hostname\\\"\"",
".",
"format... | Check if the windows credentials are valid | [
"Check",
"if",
"the",
"windows",
"credentials",
"are",
"valid"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1099-L1121 | train |
saltstack/salt | salt/utils/cloud.py | validate_windows_cred | def validate_windows_cred(host,
username='Administrator',
password=None,
retries=10,
retry_delay=1):
'''
Check if the windows credentials are valid
'''
for i in range(retries):
ret_code = 1
try:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c hostname', host, username, password, port=445
)
except Exception as exc:
log.exception("Exceoption while executing psexec")
if ret_code == 0:
break
time.sleep(retry_delay)
return ret_code == 0 | python | def validate_windows_cred(host,
username='Administrator',
password=None,
retries=10,
retry_delay=1):
'''
Check if the windows credentials are valid
'''
for i in range(retries):
ret_code = 1
try:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c hostname', host, username, password, port=445
)
except Exception as exc:
log.exception("Exceoption while executing psexec")
if ret_code == 0:
break
time.sleep(retry_delay)
return ret_code == 0 | [
"def",
"validate_windows_cred",
"(",
"host",
",",
"username",
"=",
"'Administrator'",
",",
"password",
"=",
"None",
",",
"retries",
"=",
"10",
",",
"retry_delay",
"=",
"1",
")",
":",
"for",
"i",
"in",
"range",
"(",
"retries",
")",
":",
"ret_code",
"=",
... | Check if the windows credentials are valid | [
"Check",
"if",
"the",
"windows",
"credentials",
"are",
"valid"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1124-L1143 | train |
saltstack/salt | salt/utils/cloud.py | wait_for_passwd | def wait_for_passwd(host, port=22, ssh_timeout=15, username='root',
password=None, key_filename=None, maxtries=15,
trysleep=1, display_ssh_output=True, gateway=None,
known_hosts_file='/dev/null', hard_timeout=None):
'''
Wait until ssh connection can be accessed via password or ssh key
'''
trycount = 0
while trycount < maxtries:
connectfail = False
try:
kwargs = {'hostname': host,
'port': port,
'username': username,
'password_retries': maxtries,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'known_hosts_file': known_hosts_file,
'ssh_timeout': ssh_timeout,
'hard_timeout': hard_timeout}
kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
if not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined key_filename \'{0}\' does not exist'.format(
key_filename
)
)
kwargs['key_filename'] = key_filename
log.debug('Using %s as the key_filename', key_filename)
elif password:
kwargs['password'] = password
log.debug('Using password authentication')
trycount += 1
log.debug(
'Attempting to authenticate as %s (try %s of %s)',
username, trycount, maxtries
)
status = root_cmd('date', tty=False, sudo=False, **kwargs)
if status != 0:
connectfail = True
if trycount < maxtries:
time.sleep(trysleep)
continue
log.error('Authentication failed: status code %s', status)
return False
if connectfail is False:
return True
return False
except SaltCloudPasswordError:
raise
except Exception:
if trycount >= maxtries:
return False
time.sleep(trysleep) | python | def wait_for_passwd(host, port=22, ssh_timeout=15, username='root',
password=None, key_filename=None, maxtries=15,
trysleep=1, display_ssh_output=True, gateway=None,
known_hosts_file='/dev/null', hard_timeout=None):
'''
Wait until ssh connection can be accessed via password or ssh key
'''
trycount = 0
while trycount < maxtries:
connectfail = False
try:
kwargs = {'hostname': host,
'port': port,
'username': username,
'password_retries': maxtries,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'known_hosts_file': known_hosts_file,
'ssh_timeout': ssh_timeout,
'hard_timeout': hard_timeout}
kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
if not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined key_filename \'{0}\' does not exist'.format(
key_filename
)
)
kwargs['key_filename'] = key_filename
log.debug('Using %s as the key_filename', key_filename)
elif password:
kwargs['password'] = password
log.debug('Using password authentication')
trycount += 1
log.debug(
'Attempting to authenticate as %s (try %s of %s)',
username, trycount, maxtries
)
status = root_cmd('date', tty=False, sudo=False, **kwargs)
if status != 0:
connectfail = True
if trycount < maxtries:
time.sleep(trysleep)
continue
log.error('Authentication failed: status code %s', status)
return False
if connectfail is False:
return True
return False
except SaltCloudPasswordError:
raise
except Exception:
if trycount >= maxtries:
return False
time.sleep(trysleep) | [
"def",
"wait_for_passwd",
"(",
"host",
",",
"port",
"=",
"22",
",",
"ssh_timeout",
"=",
"15",
",",
"username",
"=",
"'root'",
",",
"password",
"=",
"None",
",",
"key_filename",
"=",
"None",
",",
"maxtries",
"=",
"15",
",",
"trysleep",
"=",
"1",
",",
... | Wait until ssh connection can be accessed via password or ssh key | [
"Wait",
"until",
"ssh",
"connection",
"can",
"be",
"accessed",
"via",
"password",
"or",
"ssh",
"key"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1146-L1204 | train |
saltstack/salt | salt/utils/cloud.py | deploy_windows | def deploy_windows(host,
port=445,
timeout=900,
username='Administrator',
password=None,
name=None,
sock_dir=None,
conf_file=None,
start_action=None,
parallel=False,
minion_pub=None,
minion_pem=None,
minion_conf=None,
keep_tmp=False,
script_args=None,
script_env=None,
port_timeout=15,
preseed_minion_keys=None,
win_installer=None,
master=None,
tmp_dir='C:\\salttmp',
opts=None,
master_sign_pub_file=None,
use_winrm=False,
winrm_port=5986,
winrm_use_ssl=True,
winrm_verify_ssl=True,
**kwargs):
'''
Copy the install files to a remote Windows box, and execute them
'''
if not isinstance(opts, dict):
opts = {}
if use_winrm and not HAS_WINRM:
log.error('WinRM requested but module winrm could not be imported')
return False
if not use_winrm and has_winexe() and not HAS_PSEXEC:
salt.utils.versions.warn_until(
'Sodium',
'Support for winexe has been deprecated and will be removed in '
'Sodium, please install pypsexec instead.'
)
starttime = time.mktime(time.localtime())
log.debug('Deploying %s at %s (Windows)', host, starttime)
log.trace('HAS_WINRM: %s, use_winrm: %s', HAS_WINRM, use_winrm)
port_available = wait_for_port(host=host, port=port, timeout=port_timeout * 60)
if not port_available:
return False
service_available = False
winrm_session = None
if HAS_WINRM and use_winrm:
winrm_session = wait_for_winrm(host=host, port=winrm_port,
username=username, password=password,
timeout=port_timeout * 60, use_ssl=winrm_use_ssl,
verify=winrm_verify_ssl)
if winrm_session is not None:
service_available = True
else:
service_available = wait_for_psexecsvc(host=host, port=port,
username=username, password=password,
timeout=port_timeout * 60)
if port_available and service_available:
log.debug('SMB port %s on %s is available', port, host)
log.debug('Logging into %s:%s as %s', host, port, username)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
smb_conn = salt.utils.smb.get_conn(host, username, password)
if smb_conn is False:
log.error('Please install smbprotocol to enable SMB functionality')
return False
salt.utils.smb.mkdirs('salttemp', conn=smb_conn)
salt.utils.smb.mkdirs('salt/conf/pki/minion', conn=smb_conn)
if minion_pub:
salt.utils.smb.put_str(minion_pub, 'salt\\conf\\pki\\minion\\minion.pub', conn=smb_conn)
if minion_pem:
salt.utils.smb.put_str(minion_pem, 'salt\\conf\\pki\\minion\\minion.pem', conn=smb_conn)
if master_sign_pub_file:
# Read master-sign.pub file
log.debug("Copying master_sign.pub file from %s to minion", master_sign_pub_file)
try:
salt.utils.smb.put_file(
master_sign_pub_file,
'salt\\conf\\pki\\minion\\master_sign.pub',
'C$',
conn=smb_conn,
)
except Exception as e:
log.debug("Exception copying master_sign.pub file %s to minion", master_sign_pub_file)
# Copy over win_installer
# win_installer refers to a file such as:
# /root/Salt-Minion-0.17.0-win32-Setup.exe
# ..which exists on the same machine as salt-cloud
comps = win_installer.split('/')
local_path = '/'.join(comps[:-1])
installer = comps[-1]
salt.utils.smb.put_file(
win_installer,
'salttemp\\{0}'.format(installer),
'C$',
conn=smb_conn,
)
if use_winrm:
winrm_cmd(winrm_session, 'c:\\salttemp\\{0}'.format(installer), ['/S', '/master={0}'.format(master),
'/minion-name={0}'.format(name)]
)
else:
cmd = 'c:\\salttemp\\{0}'.format(installer)
args = "/S /master={0} /minion-name={1}".format(master, name)
stdout, stderr, ret_code = run_psexec_command(
cmd, args, host, username, password
)
if ret_code != 0:
raise Exception('Fail installer {0}'.format(ret_code))
# Copy over minion_conf
if minion_conf:
if not isinstance(minion_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_windows` now only accepts '
'dictionaries for its `minion_conf` parameter. '
'Loading YAML...'
)
minion_grains = minion_conf.pop('grains', {})
if minion_grains:
salt.utils.smb.put_str(
salt_config_to_yaml(minion_grains, line_break='\r\n'),
'salt\\conf\\grains',
conn=smb_conn
)
# Add special windows minion configuration
# that must be in the minion config file
windows_minion_conf = {
'ipc_mode': 'tcp',
'root_dir': 'c:\\salt',
'pki_dir': '/conf/pki/minion',
'multiprocessing': False,
}
minion_conf = dict(minion_conf, **windows_minion_conf)
salt.utils.smb.put_str(
salt_config_to_yaml(minion_conf, line_break='\r\n'),
'salt\\conf\\minion',
conn=smb_conn
)
# Delete C:\salttmp\ and installer file
# Unless keep_tmp is True
if not keep_tmp:
if use_winrm:
winrm_cmd(winrm_session, 'rmdir', ['/Q', '/S', 'C:\\salttemp\\'])
else:
salt.utils.smb.delete_file('salttemp\\{0}'.format(installer), 'C$', conn=smb_conn)
salt.utils.smb.delete_directory('salttemp', 'C$', conn=smb_conn)
# Shell out to psexec to ensure salt-minion service started
if use_winrm:
winrm_cmd(winrm_session, 'sc', ['stop', 'salt-minion'])
time.sleep(5)
winrm_cmd(winrm_session, 'sc', ['start', 'salt-minion'])
else:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c sc stop salt-minion', host, username, password
)
if ret_code != 0:
return False
time.sleep(5)
log.debug('Run psexec: sc start salt-minion')
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c sc start salt-minion', host, username, password
)
if ret_code != 0:
return False
# Fire deploy action
fire_event(
'event',
'{0} has been deployed at {1}'.format(name, host),
'salt/cloud/{0}/deploy_windows'.format(name),
args={'name': name},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
return True
return False | python | def deploy_windows(host,
port=445,
timeout=900,
username='Administrator',
password=None,
name=None,
sock_dir=None,
conf_file=None,
start_action=None,
parallel=False,
minion_pub=None,
minion_pem=None,
minion_conf=None,
keep_tmp=False,
script_args=None,
script_env=None,
port_timeout=15,
preseed_minion_keys=None,
win_installer=None,
master=None,
tmp_dir='C:\\salttmp',
opts=None,
master_sign_pub_file=None,
use_winrm=False,
winrm_port=5986,
winrm_use_ssl=True,
winrm_verify_ssl=True,
**kwargs):
'''
Copy the install files to a remote Windows box, and execute them
'''
if not isinstance(opts, dict):
opts = {}
if use_winrm and not HAS_WINRM:
log.error('WinRM requested but module winrm could not be imported')
return False
if not use_winrm and has_winexe() and not HAS_PSEXEC:
salt.utils.versions.warn_until(
'Sodium',
'Support for winexe has been deprecated and will be removed in '
'Sodium, please install pypsexec instead.'
)
starttime = time.mktime(time.localtime())
log.debug('Deploying %s at %s (Windows)', host, starttime)
log.trace('HAS_WINRM: %s, use_winrm: %s', HAS_WINRM, use_winrm)
port_available = wait_for_port(host=host, port=port, timeout=port_timeout * 60)
if not port_available:
return False
service_available = False
winrm_session = None
if HAS_WINRM and use_winrm:
winrm_session = wait_for_winrm(host=host, port=winrm_port,
username=username, password=password,
timeout=port_timeout * 60, use_ssl=winrm_use_ssl,
verify=winrm_verify_ssl)
if winrm_session is not None:
service_available = True
else:
service_available = wait_for_psexecsvc(host=host, port=port,
username=username, password=password,
timeout=port_timeout * 60)
if port_available and service_available:
log.debug('SMB port %s on %s is available', port, host)
log.debug('Logging into %s:%s as %s', host, port, username)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
smb_conn = salt.utils.smb.get_conn(host, username, password)
if smb_conn is False:
log.error('Please install smbprotocol to enable SMB functionality')
return False
salt.utils.smb.mkdirs('salttemp', conn=smb_conn)
salt.utils.smb.mkdirs('salt/conf/pki/minion', conn=smb_conn)
if minion_pub:
salt.utils.smb.put_str(minion_pub, 'salt\\conf\\pki\\minion\\minion.pub', conn=smb_conn)
if minion_pem:
salt.utils.smb.put_str(minion_pem, 'salt\\conf\\pki\\minion\\minion.pem', conn=smb_conn)
if master_sign_pub_file:
# Read master-sign.pub file
log.debug("Copying master_sign.pub file from %s to minion", master_sign_pub_file)
try:
salt.utils.smb.put_file(
master_sign_pub_file,
'salt\\conf\\pki\\minion\\master_sign.pub',
'C$',
conn=smb_conn,
)
except Exception as e:
log.debug("Exception copying master_sign.pub file %s to minion", master_sign_pub_file)
# Copy over win_installer
# win_installer refers to a file such as:
# /root/Salt-Minion-0.17.0-win32-Setup.exe
# ..which exists on the same machine as salt-cloud
comps = win_installer.split('/')
local_path = '/'.join(comps[:-1])
installer = comps[-1]
salt.utils.smb.put_file(
win_installer,
'salttemp\\{0}'.format(installer),
'C$',
conn=smb_conn,
)
if use_winrm:
winrm_cmd(winrm_session, 'c:\\salttemp\\{0}'.format(installer), ['/S', '/master={0}'.format(master),
'/minion-name={0}'.format(name)]
)
else:
cmd = 'c:\\salttemp\\{0}'.format(installer)
args = "/S /master={0} /minion-name={1}".format(master, name)
stdout, stderr, ret_code = run_psexec_command(
cmd, args, host, username, password
)
if ret_code != 0:
raise Exception('Fail installer {0}'.format(ret_code))
# Copy over minion_conf
if minion_conf:
if not isinstance(minion_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_windows` now only accepts '
'dictionaries for its `minion_conf` parameter. '
'Loading YAML...'
)
minion_grains = minion_conf.pop('grains', {})
if minion_grains:
salt.utils.smb.put_str(
salt_config_to_yaml(minion_grains, line_break='\r\n'),
'salt\\conf\\grains',
conn=smb_conn
)
# Add special windows minion configuration
# that must be in the minion config file
windows_minion_conf = {
'ipc_mode': 'tcp',
'root_dir': 'c:\\salt',
'pki_dir': '/conf/pki/minion',
'multiprocessing': False,
}
minion_conf = dict(minion_conf, **windows_minion_conf)
salt.utils.smb.put_str(
salt_config_to_yaml(minion_conf, line_break='\r\n'),
'salt\\conf\\minion',
conn=smb_conn
)
# Delete C:\salttmp\ and installer file
# Unless keep_tmp is True
if not keep_tmp:
if use_winrm:
winrm_cmd(winrm_session, 'rmdir', ['/Q', '/S', 'C:\\salttemp\\'])
else:
salt.utils.smb.delete_file('salttemp\\{0}'.format(installer), 'C$', conn=smb_conn)
salt.utils.smb.delete_directory('salttemp', 'C$', conn=smb_conn)
# Shell out to psexec to ensure salt-minion service started
if use_winrm:
winrm_cmd(winrm_session, 'sc', ['stop', 'salt-minion'])
time.sleep(5)
winrm_cmd(winrm_session, 'sc', ['start', 'salt-minion'])
else:
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c sc stop salt-minion', host, username, password
)
if ret_code != 0:
return False
time.sleep(5)
log.debug('Run psexec: sc start salt-minion')
stdout, stderr, ret_code = run_psexec_command(
'cmd.exe', '/c sc start salt-minion', host, username, password
)
if ret_code != 0:
return False
# Fire deploy action
fire_event(
'event',
'{0} has been deployed at {1}'.format(name, host),
'salt/cloud/{0}/deploy_windows'.format(name),
args={'name': name},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
return True
return False | [
"def",
"deploy_windows",
"(",
"host",
",",
"port",
"=",
"445",
",",
"timeout",
"=",
"900",
",",
"username",
"=",
"'Administrator'",
",",
"password",
"=",
"None",
",",
"name",
"=",
"None",
",",
"sock_dir",
"=",
"None",
",",
"conf_file",
"=",
"None",
","... | Copy the install files to a remote Windows box, and execute them | [
"Copy",
"the",
"install",
"files",
"to",
"a",
"remote",
"Windows",
"box",
"and",
"execute",
"them"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1207-L1408 | train |
saltstack/salt | salt/utils/cloud.py | deploy_script | def deploy_script(host,
port=22,
timeout=900,
username='root',
password=None,
key_filename=None,
script=None,
name=None,
sock_dir=None,
provider=None,
conf_file=None,
start_action=None,
make_master=False,
master_pub=None,
master_pem=None,
master_conf=None,
minion_pub=None,
minion_pem=None,
minion_conf=None,
keep_tmp=False,
script_args=None,
script_env=None,
ssh_timeout=15,
maxtries=15,
make_syndic=False,
make_minion=True,
display_ssh_output=True,
preseed_minion_keys=None,
parallel=False,
sudo_password=None,
sudo=False,
tty=None,
vm_=None,
opts=None,
tmp_dir='/tmp/.saltcloud',
file_map=None,
master_sign_pub_file=None,
cloud_grains=None,
force_minion_config=False,
**kwargs):
'''
Copy a deploy script to a remote server, execute it, and remove it
'''
if not isinstance(opts, dict):
opts = {}
vm_ = vm_ or {} # if None, default to empty dict
cloud_grains = cloud_grains or {}
tmp_dir = '{0}-{1}'.format(tmp_dir.rstrip('/'), uuid.uuid4())
deploy_command = salt.config.get_cloud_config_value(
'deploy_command', vm_, opts,
default=os.path.join(tmp_dir, 'deploy.sh'))
if key_filename is not None and not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined key_filename \'{0}\' does not exist'.format(
key_filename
)
)
gateway = None
if 'gateway' in kwargs:
gateway = kwargs['gateway']
starttime = time.localtime()
log.debug(
'Deploying %s at %s',
host, time.strftime('%Y-%m-%d %H:%M:%S', starttime)
)
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
hard_timeout = opts.get('hard_timeout', None)
if wait_for_port(host=host, port=port, gateway=gateway):
log.debug('SSH port %s on %s is available', port, host)
if wait_for_passwd(host, port=port, username=username,
password=password, key_filename=key_filename,
ssh_timeout=ssh_timeout,
display_ssh_output=display_ssh_output,
gateway=gateway, known_hosts_file=known_hosts_file,
maxtries=maxtries, hard_timeout=hard_timeout):
log.debug('Logging into %s:%s as %s', host, port, username)
ssh_kwargs = {
'hostname': host,
'port': port,
'username': username,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'sudo_password': sudo_password,
'sftp': opts.get('use_sftp', False)
}
ssh_kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
log.debug('Using %s as the key_filename', key_filename)
ssh_kwargs['key_filename'] = key_filename
elif password and kwargs.get('has_ssh_agent', False) is False:
ssh_kwargs['password'] = password
if root_cmd('test -e \'{0}\''.format(tmp_dir), tty, sudo,
allow_failure=True, **ssh_kwargs):
ret = root_cmd(('sh -c "( mkdir -p -m 700 \'{0}\' )"').format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t create temporary '
'directory in {0} !'.format(tmp_dir)
)
if sudo:
comps = tmp_dir.lstrip('/').rstrip('/').split('/')
if comps:
if len(comps) > 1 or comps[0] != 'tmp':
ret = root_cmd(
'chown {0} "{1}"'.format(username, tmp_dir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Cant set {0} ownership on {1}'.format(
username, tmp_dir))
if not isinstance(file_map, dict):
file_map = {}
# Copy an arbitrary group of files to the target system
remote_dirs = []
file_map_success = []
file_map_fail = []
for map_item in file_map:
local_file = map_item
remote_file = file_map[map_item]
if not os.path.exists(map_item):
log.error(
'The local file "%s" does not exist, and will not be '
'copied to "%s" on the target system',
local_file, remote_file
)
file_map_fail.append({local_file: remote_file})
continue
if os.path.isdir(local_file):
dir_name = os.path.basename(local_file)
remote_dir = os.path.join(os.path.dirname(remote_file),
dir_name)
else:
remote_dir = os.path.dirname(remote_file)
if remote_dir not in remote_dirs:
root_cmd('mkdir -p \'{0}\''.format(remote_dir), tty, sudo, **ssh_kwargs)
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown {0} \'{1}\''.format(
ssh_kwargs['username'], remote_dir
),
tty, sudo, **ssh_kwargs
)
remote_dirs.append(remote_dir)
ssh_file(
opts, remote_file, kwargs=ssh_kwargs, local_file=local_file
)
file_map_success.append({local_file: remote_file})
# Minion configuration
if minion_pem:
ssh_file(opts, '{0}/minion.pem'.format(tmp_dir), minion_pem, ssh_kwargs)
ret = root_cmd('chmod 600 \'{0}/minion.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}/minion.pem'.format(tmp_dir))
if minion_pub:
ssh_file(opts, '{0}/minion.pub'.format(tmp_dir), minion_pub, ssh_kwargs)
if master_sign_pub_file:
ssh_file(opts, '{0}/master_sign.pub'.format(tmp_dir), kwargs=ssh_kwargs, local_file=master_sign_pub_file)
if minion_conf:
if not isinstance(minion_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_script now only accepts '
'dictionaries for it\'s `minion_conf` parameter. '
'Loading YAML...'
)
minion_grains = minion_conf.pop('grains', {})
if minion_grains:
ssh_file(
opts,
'{0}/grains'.format(tmp_dir),
salt_config_to_yaml(minion_grains),
ssh_kwargs
)
if cloud_grains and opts.get('enable_cloud_grains', True):
minion_conf['grains'] = {'salt-cloud': cloud_grains}
ssh_file(
opts,
'{0}/minion'.format(tmp_dir),
salt_config_to_yaml(minion_conf),
ssh_kwargs
)
# Master configuration
if master_pem:
ssh_file(opts, '{0}/master.pem'.format(tmp_dir), master_pem, ssh_kwargs)
ret = root_cmd('chmod 600 \'{0}/master.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Cant set perms on {0}/master.pem'.format(tmp_dir))
if master_pub:
ssh_file(opts, '{0}/master.pub'.format(tmp_dir), master_pub, ssh_kwargs)
if master_conf:
if not isinstance(master_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_script now only accepts '
'dictionaries for it\'s `master_conf` parameter. '
'Loading from YAML ...'
)
ssh_file(
opts,
'{0}/master'.format(tmp_dir),
salt_config_to_yaml(master_conf),
ssh_kwargs
)
# XXX: We need to make these paths configurable
preseed_minion_keys_tempdir = '{0}/preseed-minion-keys'.format(
tmp_dir)
if preseed_minion_keys is not None:
# Create remote temp dir
ret = root_cmd(
'mkdir \'{0}\''.format(preseed_minion_keys_tempdir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Cant create {0}'.format(preseed_minion_keys_tempdir))
ret = root_cmd(
'chmod 700 \'{0}\''.format(preseed_minion_keys_tempdir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}'.format(
preseed_minion_keys_tempdir))
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown {0} \'{1}\''.format(
ssh_kwargs['username'], preseed_minion_keys_tempdir
),
tty, sudo, **ssh_kwargs
)
# Copy pre-seed minion keys
for minion_id, minion_key in six.iteritems(preseed_minion_keys):
rpath = os.path.join(
preseed_minion_keys_tempdir, minion_id
)
ssh_file(opts, rpath, minion_key, ssh_kwargs)
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown -R root \'{0}\''.format(
preseed_minion_keys_tempdir
),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Can\'t set ownership for {0}'.format(
preseed_minion_keys_tempdir))
# Run any pre-flight commands before running deploy scripts
preflight_cmds = kwargs.get('preflight_cmds', [])
for command in preflight_cmds:
cmd_ret = root_cmd(command, tty, sudo, **ssh_kwargs)
if cmd_ret:
raise SaltCloudSystemExit(
'Pre-flight command failed: \'{0}\''.format(command)
)
# The actual deploy script
if script:
# got strange escaping issues with sudoer, going onto a
# subshell fixes that
ssh_file(opts, '{0}/deploy.sh'.format(tmp_dir), script, ssh_kwargs)
ret = root_cmd(
('sh -c "( chmod +x \'{0}/deploy.sh\' )";'
'exit $?').format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}/deploy.sh'.format(tmp_dir))
time_used = time.mktime(time.localtime()) - time.mktime(starttime)
newtimeout = timeout - time_used
queue = None
process = None
# Consider this code experimental. It causes Salt Cloud to wait
# for the minion to check in, and then fire a startup event.
# Disabled if parallel because it doesn't work!
if start_action and not parallel:
queue = multiprocessing.Queue()
process = multiprocessing.Process(
target=check_auth, kwargs=dict(
name=name, sock_dir=sock_dir,
timeout=newtimeout, queue=queue
)
)
log.debug('Starting new process to wait for salt-minion')
process.start()
# Run the deploy script
if script:
if 'bootstrap-salt' in script:
deploy_command += ' -c \'{0}\''.format(tmp_dir)
if force_minion_config:
deploy_command += ' -F'
if make_syndic is True:
deploy_command += ' -S'
if make_master is True:
deploy_command += ' -M'
if make_minion is False:
deploy_command += ' -N'
if keep_tmp is True:
deploy_command += ' -K'
if preseed_minion_keys is not None:
deploy_command += ' -k \'{0}\''.format(
preseed_minion_keys_tempdir
)
if script_args:
deploy_command += ' {0}'.format(script_args)
if script_env:
if not isinstance(script_env, dict):
raise SaltCloudSystemExit(
'The \'script_env\' configuration setting NEEDS '
'to be a dictionary not a {0}'.format(
type(script_env)
)
)
environ_script_contents = ['#!/bin/sh']
for key, value in six.iteritems(script_env):
environ_script_contents.append(
'setenv {0} \'{1}\' >/dev/null 2>&1 || '
'export {0}=\'{1}\''.format(key, value)
)
environ_script_contents.append(deploy_command)
# Upload our environ setter wrapper
ssh_file(
opts,
'{0}/environ-deploy-wrapper.sh'.format(tmp_dir),
'\n'.join(environ_script_contents),
ssh_kwargs
)
root_cmd(
'chmod +x \'{0}/environ-deploy-wrapper.sh\''.format(tmp_dir),
tty, sudo, **ssh_kwargs
)
# The deploy command is now our wrapper
deploy_command = '\'{0}/environ-deploy-wrapper.sh\''.format(
tmp_dir,
)
if root_cmd(deploy_command, tty, sudo, **ssh_kwargs) != 0:
raise SaltCloudSystemExit(
'Executing the command \'{0}\' failed'.format(
deploy_command
)
)
log.debug('Executed command \'%s\'', deploy_command)
# Remove the deploy script
if not keep_tmp:
root_cmd('rm -f \'{0}/deploy.sh\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/deploy.sh', tmp_dir)
if script_env:
root_cmd(
'rm -f \'{0}/environ-deploy-wrapper.sh\''.format(
tmp_dir
),
tty, sudo, **ssh_kwargs
)
log.debug('Removed %s/environ-deploy-wrapper.sh', tmp_dir)
if keep_tmp:
log.debug('Not removing deployment files from %s/', tmp_dir)
else:
# Remove minion configuration
if minion_pub:
root_cmd('rm -f \'{0}/minion.pub\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion.pub', tmp_dir)
if minion_pem:
root_cmd('rm -f \'{0}/minion.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion.pem', tmp_dir)
if minion_conf:
root_cmd('rm -f \'{0}/grains\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/grains', tmp_dir)
root_cmd('rm -f \'{0}/minion\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion', tmp_dir)
if master_sign_pub_file:
root_cmd('rm -f {0}/master_sign.pub'.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master_sign.pub', tmp_dir)
# Remove master configuration
if master_pub:
root_cmd('rm -f \'{0}/master.pub\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master.pub', tmp_dir)
if master_pem:
root_cmd('rm -f \'{0}/master.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master.pem', tmp_dir)
if master_conf:
root_cmd('rm -f \'{0}/master\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master', tmp_dir)
# Remove pre-seed keys directory
if preseed_minion_keys is not None:
root_cmd(
'rm -rf \'{0}\''.format(
preseed_minion_keys_tempdir
), tty, sudo, **ssh_kwargs
)
log.debug('Removed %s', preseed_minion_keys_tempdir)
if start_action and not parallel:
queuereturn = queue.get()
process.join()
if queuereturn and start_action:
# client = salt.client.LocalClient(conf_file)
# output = client.cmd_iter(
# host, 'state.highstate', timeout=timeout
# )
# for line in output:
# print(line)
log.info('Executing %s on the salt-minion', start_action)
root_cmd(
'salt-call {0}'.format(start_action),
tty, sudo, **ssh_kwargs
)
log.info(
'Finished executing %s on the salt-minion',
start_action
)
# Fire deploy action
fire_event(
'event',
'{0} has been deployed at {1}'.format(name, host),
'salt/cloud/{0}/deploy_script'.format(name),
args={
'name': name,
'host': host
},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
if file_map_fail or file_map_success:
return {
'File Upload Success': file_map_success,
'File Upload Failure': file_map_fail,
}
return True
return False | python | def deploy_script(host,
port=22,
timeout=900,
username='root',
password=None,
key_filename=None,
script=None,
name=None,
sock_dir=None,
provider=None,
conf_file=None,
start_action=None,
make_master=False,
master_pub=None,
master_pem=None,
master_conf=None,
minion_pub=None,
minion_pem=None,
minion_conf=None,
keep_tmp=False,
script_args=None,
script_env=None,
ssh_timeout=15,
maxtries=15,
make_syndic=False,
make_minion=True,
display_ssh_output=True,
preseed_minion_keys=None,
parallel=False,
sudo_password=None,
sudo=False,
tty=None,
vm_=None,
opts=None,
tmp_dir='/tmp/.saltcloud',
file_map=None,
master_sign_pub_file=None,
cloud_grains=None,
force_minion_config=False,
**kwargs):
'''
Copy a deploy script to a remote server, execute it, and remove it
'''
if not isinstance(opts, dict):
opts = {}
vm_ = vm_ or {} # if None, default to empty dict
cloud_grains = cloud_grains or {}
tmp_dir = '{0}-{1}'.format(tmp_dir.rstrip('/'), uuid.uuid4())
deploy_command = salt.config.get_cloud_config_value(
'deploy_command', vm_, opts,
default=os.path.join(tmp_dir, 'deploy.sh'))
if key_filename is not None and not os.path.isfile(key_filename):
raise SaltCloudConfigError(
'The defined key_filename \'{0}\' does not exist'.format(
key_filename
)
)
gateway = None
if 'gateway' in kwargs:
gateway = kwargs['gateway']
starttime = time.localtime()
log.debug(
'Deploying %s at %s',
host, time.strftime('%Y-%m-%d %H:%M:%S', starttime)
)
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
hard_timeout = opts.get('hard_timeout', None)
if wait_for_port(host=host, port=port, gateway=gateway):
log.debug('SSH port %s on %s is available', port, host)
if wait_for_passwd(host, port=port, username=username,
password=password, key_filename=key_filename,
ssh_timeout=ssh_timeout,
display_ssh_output=display_ssh_output,
gateway=gateway, known_hosts_file=known_hosts_file,
maxtries=maxtries, hard_timeout=hard_timeout):
log.debug('Logging into %s:%s as %s', host, port, username)
ssh_kwargs = {
'hostname': host,
'port': port,
'username': username,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'sudo_password': sudo_password,
'sftp': opts.get('use_sftp', False)
}
ssh_kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
log.debug('Using %s as the key_filename', key_filename)
ssh_kwargs['key_filename'] = key_filename
elif password and kwargs.get('has_ssh_agent', False) is False:
ssh_kwargs['password'] = password
if root_cmd('test -e \'{0}\''.format(tmp_dir), tty, sudo,
allow_failure=True, **ssh_kwargs):
ret = root_cmd(('sh -c "( mkdir -p -m 700 \'{0}\' )"').format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t create temporary '
'directory in {0} !'.format(tmp_dir)
)
if sudo:
comps = tmp_dir.lstrip('/').rstrip('/').split('/')
if comps:
if len(comps) > 1 or comps[0] != 'tmp':
ret = root_cmd(
'chown {0} "{1}"'.format(username, tmp_dir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Cant set {0} ownership on {1}'.format(
username, tmp_dir))
if not isinstance(file_map, dict):
file_map = {}
# Copy an arbitrary group of files to the target system
remote_dirs = []
file_map_success = []
file_map_fail = []
for map_item in file_map:
local_file = map_item
remote_file = file_map[map_item]
if not os.path.exists(map_item):
log.error(
'The local file "%s" does not exist, and will not be '
'copied to "%s" on the target system',
local_file, remote_file
)
file_map_fail.append({local_file: remote_file})
continue
if os.path.isdir(local_file):
dir_name = os.path.basename(local_file)
remote_dir = os.path.join(os.path.dirname(remote_file),
dir_name)
else:
remote_dir = os.path.dirname(remote_file)
if remote_dir not in remote_dirs:
root_cmd('mkdir -p \'{0}\''.format(remote_dir), tty, sudo, **ssh_kwargs)
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown {0} \'{1}\''.format(
ssh_kwargs['username'], remote_dir
),
tty, sudo, **ssh_kwargs
)
remote_dirs.append(remote_dir)
ssh_file(
opts, remote_file, kwargs=ssh_kwargs, local_file=local_file
)
file_map_success.append({local_file: remote_file})
# Minion configuration
if minion_pem:
ssh_file(opts, '{0}/minion.pem'.format(tmp_dir), minion_pem, ssh_kwargs)
ret = root_cmd('chmod 600 \'{0}/minion.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}/minion.pem'.format(tmp_dir))
if minion_pub:
ssh_file(opts, '{0}/minion.pub'.format(tmp_dir), minion_pub, ssh_kwargs)
if master_sign_pub_file:
ssh_file(opts, '{0}/master_sign.pub'.format(tmp_dir), kwargs=ssh_kwargs, local_file=master_sign_pub_file)
if minion_conf:
if not isinstance(minion_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_script now only accepts '
'dictionaries for it\'s `minion_conf` parameter. '
'Loading YAML...'
)
minion_grains = minion_conf.pop('grains', {})
if minion_grains:
ssh_file(
opts,
'{0}/grains'.format(tmp_dir),
salt_config_to_yaml(minion_grains),
ssh_kwargs
)
if cloud_grains and opts.get('enable_cloud_grains', True):
minion_conf['grains'] = {'salt-cloud': cloud_grains}
ssh_file(
opts,
'{0}/minion'.format(tmp_dir),
salt_config_to_yaml(minion_conf),
ssh_kwargs
)
# Master configuration
if master_pem:
ssh_file(opts, '{0}/master.pem'.format(tmp_dir), master_pem, ssh_kwargs)
ret = root_cmd('chmod 600 \'{0}/master.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Cant set perms on {0}/master.pem'.format(tmp_dir))
if master_pub:
ssh_file(opts, '{0}/master.pub'.format(tmp_dir), master_pub, ssh_kwargs)
if master_conf:
if not isinstance(master_conf, dict):
# Let's not just fail regarding this change, specially
# since we can handle it
raise DeprecationWarning(
'`salt.utils.cloud.deploy_script now only accepts '
'dictionaries for it\'s `master_conf` parameter. '
'Loading from YAML ...'
)
ssh_file(
opts,
'{0}/master'.format(tmp_dir),
salt_config_to_yaml(master_conf),
ssh_kwargs
)
# XXX: We need to make these paths configurable
preseed_minion_keys_tempdir = '{0}/preseed-minion-keys'.format(
tmp_dir)
if preseed_minion_keys is not None:
# Create remote temp dir
ret = root_cmd(
'mkdir \'{0}\''.format(preseed_minion_keys_tempdir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Cant create {0}'.format(preseed_minion_keys_tempdir))
ret = root_cmd(
'chmod 700 \'{0}\''.format(preseed_minion_keys_tempdir),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}'.format(
preseed_minion_keys_tempdir))
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown {0} \'{1}\''.format(
ssh_kwargs['username'], preseed_minion_keys_tempdir
),
tty, sudo, **ssh_kwargs
)
# Copy pre-seed minion keys
for minion_id, minion_key in six.iteritems(preseed_minion_keys):
rpath = os.path.join(
preseed_minion_keys_tempdir, minion_id
)
ssh_file(opts, rpath, minion_key, ssh_kwargs)
if ssh_kwargs['username'] != 'root':
root_cmd(
'chown -R root \'{0}\''.format(
preseed_minion_keys_tempdir
),
tty, sudo, **ssh_kwargs
)
if ret:
raise SaltCloudSystemExit(
'Can\'t set ownership for {0}'.format(
preseed_minion_keys_tempdir))
# Run any pre-flight commands before running deploy scripts
preflight_cmds = kwargs.get('preflight_cmds', [])
for command in preflight_cmds:
cmd_ret = root_cmd(command, tty, sudo, **ssh_kwargs)
if cmd_ret:
raise SaltCloudSystemExit(
'Pre-flight command failed: \'{0}\''.format(command)
)
# The actual deploy script
if script:
# got strange escaping issues with sudoer, going onto a
# subshell fixes that
ssh_file(opts, '{0}/deploy.sh'.format(tmp_dir), script, ssh_kwargs)
ret = root_cmd(
('sh -c "( chmod +x \'{0}/deploy.sh\' )";'
'exit $?').format(tmp_dir),
tty, sudo, **ssh_kwargs)
if ret:
raise SaltCloudSystemExit(
'Can\'t set perms on {0}/deploy.sh'.format(tmp_dir))
time_used = time.mktime(time.localtime()) - time.mktime(starttime)
newtimeout = timeout - time_used
queue = None
process = None
# Consider this code experimental. It causes Salt Cloud to wait
# for the minion to check in, and then fire a startup event.
# Disabled if parallel because it doesn't work!
if start_action and not parallel:
queue = multiprocessing.Queue()
process = multiprocessing.Process(
target=check_auth, kwargs=dict(
name=name, sock_dir=sock_dir,
timeout=newtimeout, queue=queue
)
)
log.debug('Starting new process to wait for salt-minion')
process.start()
# Run the deploy script
if script:
if 'bootstrap-salt' in script:
deploy_command += ' -c \'{0}\''.format(tmp_dir)
if force_minion_config:
deploy_command += ' -F'
if make_syndic is True:
deploy_command += ' -S'
if make_master is True:
deploy_command += ' -M'
if make_minion is False:
deploy_command += ' -N'
if keep_tmp is True:
deploy_command += ' -K'
if preseed_minion_keys is not None:
deploy_command += ' -k \'{0}\''.format(
preseed_minion_keys_tempdir
)
if script_args:
deploy_command += ' {0}'.format(script_args)
if script_env:
if not isinstance(script_env, dict):
raise SaltCloudSystemExit(
'The \'script_env\' configuration setting NEEDS '
'to be a dictionary not a {0}'.format(
type(script_env)
)
)
environ_script_contents = ['#!/bin/sh']
for key, value in six.iteritems(script_env):
environ_script_contents.append(
'setenv {0} \'{1}\' >/dev/null 2>&1 || '
'export {0}=\'{1}\''.format(key, value)
)
environ_script_contents.append(deploy_command)
# Upload our environ setter wrapper
ssh_file(
opts,
'{0}/environ-deploy-wrapper.sh'.format(tmp_dir),
'\n'.join(environ_script_contents),
ssh_kwargs
)
root_cmd(
'chmod +x \'{0}/environ-deploy-wrapper.sh\''.format(tmp_dir),
tty, sudo, **ssh_kwargs
)
# The deploy command is now our wrapper
deploy_command = '\'{0}/environ-deploy-wrapper.sh\''.format(
tmp_dir,
)
if root_cmd(deploy_command, tty, sudo, **ssh_kwargs) != 0:
raise SaltCloudSystemExit(
'Executing the command \'{0}\' failed'.format(
deploy_command
)
)
log.debug('Executed command \'%s\'', deploy_command)
# Remove the deploy script
if not keep_tmp:
root_cmd('rm -f \'{0}/deploy.sh\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/deploy.sh', tmp_dir)
if script_env:
root_cmd(
'rm -f \'{0}/environ-deploy-wrapper.sh\''.format(
tmp_dir
),
tty, sudo, **ssh_kwargs
)
log.debug('Removed %s/environ-deploy-wrapper.sh', tmp_dir)
if keep_tmp:
log.debug('Not removing deployment files from %s/', tmp_dir)
else:
# Remove minion configuration
if minion_pub:
root_cmd('rm -f \'{0}/minion.pub\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion.pub', tmp_dir)
if minion_pem:
root_cmd('rm -f \'{0}/minion.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion.pem', tmp_dir)
if minion_conf:
root_cmd('rm -f \'{0}/grains\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/grains', tmp_dir)
root_cmd('rm -f \'{0}/minion\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/minion', tmp_dir)
if master_sign_pub_file:
root_cmd('rm -f {0}/master_sign.pub'.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master_sign.pub', tmp_dir)
# Remove master configuration
if master_pub:
root_cmd('rm -f \'{0}/master.pub\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master.pub', tmp_dir)
if master_pem:
root_cmd('rm -f \'{0}/master.pem\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master.pem', tmp_dir)
if master_conf:
root_cmd('rm -f \'{0}/master\''.format(tmp_dir),
tty, sudo, **ssh_kwargs)
log.debug('Removed %s/master', tmp_dir)
# Remove pre-seed keys directory
if preseed_minion_keys is not None:
root_cmd(
'rm -rf \'{0}\''.format(
preseed_minion_keys_tempdir
), tty, sudo, **ssh_kwargs
)
log.debug('Removed %s', preseed_minion_keys_tempdir)
if start_action and not parallel:
queuereturn = queue.get()
process.join()
if queuereturn and start_action:
# client = salt.client.LocalClient(conf_file)
# output = client.cmd_iter(
# host, 'state.highstate', timeout=timeout
# )
# for line in output:
# print(line)
log.info('Executing %s on the salt-minion', start_action)
root_cmd(
'salt-call {0}'.format(start_action),
tty, sudo, **ssh_kwargs
)
log.info(
'Finished executing %s on the salt-minion',
start_action
)
# Fire deploy action
fire_event(
'event',
'{0} has been deployed at {1}'.format(name, host),
'salt/cloud/{0}/deploy_script'.format(name),
args={
'name': name,
'host': host
},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
if file_map_fail or file_map_success:
return {
'File Upload Success': file_map_success,
'File Upload Failure': file_map_fail,
}
return True
return False | [
"def",
"deploy_script",
"(",
"host",
",",
"port",
"=",
"22",
",",
"timeout",
"=",
"900",
",",
"username",
"=",
"'root'",
",",
"password",
"=",
"None",
",",
"key_filename",
"=",
"None",
",",
"script",
"=",
"None",
",",
"name",
"=",
"None",
",",
"sock_... | Copy a deploy script to a remote server, execute it, and remove it | [
"Copy",
"a",
"deploy",
"script",
"to",
"a",
"remote",
"server",
"execute",
"it",
"and",
"remove",
"it"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1411-L1887 | train |
saltstack/salt | salt/utils/cloud.py | run_inline_script | def run_inline_script(host,
name=None,
port=22,
timeout=900,
username='root',
key_filename=None,
inline_script=None,
ssh_timeout=15,
display_ssh_output=True,
parallel=False,
sudo_password=None,
sudo=False,
password=None,
tty=None,
opts=None,
tmp_dir='/tmp/.saltcloud-inline_script',
**kwargs):
'''
Run the inline script commands, one by one
:**kwargs: catch all other things we may get but don't actually need/use
'''
gateway = None
if 'gateway' in kwargs:
gateway = kwargs['gateway']
starttime = time.mktime(time.localtime())
log.debug('Deploying %s at %s', host, starttime)
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
if wait_for_port(host=host, port=port, gateway=gateway):
log.debug('SSH port %s on %s is available', port, host)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
if wait_for_passwd(host, port=port, username=username,
password=password, key_filename=key_filename,
ssh_timeout=ssh_timeout,
display_ssh_output=display_ssh_output,
gateway=gateway, known_hosts_file=known_hosts_file):
log.debug('Logging into %s:%s as %s', host, port, username)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ssh_kwargs = {
'hostname': host,
'port': port,
'username': username,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'sudo_password': sudo_password,
'sftp': opts.get('use_sftp', False)
}
ssh_kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
log.debug('Using %s as the key_filename', key_filename)
ssh_kwargs['key_filename'] = key_filename
elif password and 'has_ssh_agent' in kwargs and kwargs['has_ssh_agent'] is False:
ssh_kwargs['password'] = password
# TODO: write some tests ???
# TODO: check edge cases (e.g. ssh gateways, salt deploy disabled, etc.)
if root_cmd('test -e \\"{0}\\"'.format(tmp_dir), tty, sudo,
allow_failure=True, **ssh_kwargs) and inline_script:
log.debug('Found inline script to execute.')
for cmd_line in inline_script:
log.info('Executing inline command: %s', cmd_line)
ret = root_cmd('sh -c "( {0} )"'.format(cmd_line),
tty, sudo, allow_failure=True, **ssh_kwargs)
if ret:
log.info('[%s] Output: %s', cmd_line, ret)
# TODO: ensure we send the correct return value
return True | python | def run_inline_script(host,
name=None,
port=22,
timeout=900,
username='root',
key_filename=None,
inline_script=None,
ssh_timeout=15,
display_ssh_output=True,
parallel=False,
sudo_password=None,
sudo=False,
password=None,
tty=None,
opts=None,
tmp_dir='/tmp/.saltcloud-inline_script',
**kwargs):
'''
Run the inline script commands, one by one
:**kwargs: catch all other things we may get but don't actually need/use
'''
gateway = None
if 'gateway' in kwargs:
gateway = kwargs['gateway']
starttime = time.mktime(time.localtime())
log.debug('Deploying %s at %s', host, starttime)
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
if wait_for_port(host=host, port=port, gateway=gateway):
log.debug('SSH port %s on %s is available', port, host)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
if wait_for_passwd(host, port=port, username=username,
password=password, key_filename=key_filename,
ssh_timeout=ssh_timeout,
display_ssh_output=display_ssh_output,
gateway=gateway, known_hosts_file=known_hosts_file):
log.debug('Logging into %s:%s as %s', host, port, username)
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ssh_kwargs = {
'hostname': host,
'port': port,
'username': username,
'timeout': ssh_timeout,
'display_ssh_output': display_ssh_output,
'sudo_password': sudo_password,
'sftp': opts.get('use_sftp', False)
}
ssh_kwargs.update(__ssh_gateway_config_dict(gateway))
if key_filename:
log.debug('Using %s as the key_filename', key_filename)
ssh_kwargs['key_filename'] = key_filename
elif password and 'has_ssh_agent' in kwargs and kwargs['has_ssh_agent'] is False:
ssh_kwargs['password'] = password
# TODO: write some tests ???
# TODO: check edge cases (e.g. ssh gateways, salt deploy disabled, etc.)
if root_cmd('test -e \\"{0}\\"'.format(tmp_dir), tty, sudo,
allow_failure=True, **ssh_kwargs) and inline_script:
log.debug('Found inline script to execute.')
for cmd_line in inline_script:
log.info('Executing inline command: %s', cmd_line)
ret = root_cmd('sh -c "( {0} )"'.format(cmd_line),
tty, sudo, allow_failure=True, **ssh_kwargs)
if ret:
log.info('[%s] Output: %s', cmd_line, ret)
# TODO: ensure we send the correct return value
return True | [
"def",
"run_inline_script",
"(",
"host",
",",
"name",
"=",
"None",
",",
"port",
"=",
"22",
",",
"timeout",
"=",
"900",
",",
"username",
"=",
"'root'",
",",
"key_filename",
"=",
"None",
",",
"inline_script",
"=",
"None",
",",
"ssh_timeout",
"=",
"15",
"... | Run the inline script commands, one by one
:**kwargs: catch all other things we may get but don't actually need/use | [
"Run",
"the",
"inline",
"script",
"commands",
"one",
"by",
"one",
":",
"**",
"kwargs",
":",
"catch",
"all",
"other",
"things",
"we",
"may",
"get",
"but",
"don",
"t",
"actually",
"need",
"/",
"use"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1890-L1961 | train |
saltstack/salt | salt/utils/cloud.py | filter_event | def filter_event(tag, data, defaults):
'''
Accept a tag, a dict and a list of default keys to return from the dict, and
check them against the cloud configuration for that tag
'''
ret = {}
keys = []
use_defaults = True
for ktag in __opts__.get('filter_events', {}):
if tag != ktag:
continue
keys = __opts__['filter_events'][ktag]['keys']
use_defaults = __opts__['filter_events'][ktag].get('use_defaults', True)
if use_defaults is False:
defaults = []
# For PY3, if something like ".keys()" or ".values()" is used on a dictionary,
# it returns a dict_view and not a list like in PY2. "defaults" should be passed
# in with the correct data type, but don't stack-trace in case it wasn't.
if not isinstance(defaults, list):
defaults = list(defaults)
defaults = list(set(defaults + keys))
for key in defaults:
if key in data:
ret[key] = data[key]
return ret | python | def filter_event(tag, data, defaults):
'''
Accept a tag, a dict and a list of default keys to return from the dict, and
check them against the cloud configuration for that tag
'''
ret = {}
keys = []
use_defaults = True
for ktag in __opts__.get('filter_events', {}):
if tag != ktag:
continue
keys = __opts__['filter_events'][ktag]['keys']
use_defaults = __opts__['filter_events'][ktag].get('use_defaults', True)
if use_defaults is False:
defaults = []
# For PY3, if something like ".keys()" or ".values()" is used on a dictionary,
# it returns a dict_view and not a list like in PY2. "defaults" should be passed
# in with the correct data type, but don't stack-trace in case it wasn't.
if not isinstance(defaults, list):
defaults = list(defaults)
defaults = list(set(defaults + keys))
for key in defaults:
if key in data:
ret[key] = data[key]
return ret | [
"def",
"filter_event",
"(",
"tag",
",",
"data",
",",
"defaults",
")",
":",
"ret",
"=",
"{",
"}",
"keys",
"=",
"[",
"]",
"use_defaults",
"=",
"True",
"for",
"ktag",
"in",
"__opts__",
".",
"get",
"(",
"'filter_events'",
",",
"{",
"}",
")",
":",
"if",... | Accept a tag, a dict and a list of default keys to return from the dict, and
check them against the cloud configuration for that tag | [
"Accept",
"a",
"tag",
"a",
"dict",
"and",
"a",
"list",
"of",
"default",
"keys",
"to",
"return",
"from",
"the",
"dict",
"and",
"check",
"them",
"against",
"the",
"cloud",
"configuration",
"for",
"that",
"tag"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1964-L1994 | train |
saltstack/salt | salt/utils/cloud.py | fire_event | def fire_event(key, msg, tag, sock_dir, args=None, transport='zeromq'):
'''
Fire deploy action
'''
event = salt.utils.event.get_event(
'master',
sock_dir,
transport,
listen=False)
try:
event.fire_event(msg, tag)
except ValueError:
# We're using at least a 0.17.x version of salt
if isinstance(args, dict):
args[key] = msg
else:
args = {key: msg}
event.fire_event(args, tag)
# https://github.com/zeromq/pyzmq/issues/173#issuecomment-4037083
# Assertion failed: get_load () == 0 (poller_base.cpp:32)
time.sleep(0.025) | python | def fire_event(key, msg, tag, sock_dir, args=None, transport='zeromq'):
'''
Fire deploy action
'''
event = salt.utils.event.get_event(
'master',
sock_dir,
transport,
listen=False)
try:
event.fire_event(msg, tag)
except ValueError:
# We're using at least a 0.17.x version of salt
if isinstance(args, dict):
args[key] = msg
else:
args = {key: msg}
event.fire_event(args, tag)
# https://github.com/zeromq/pyzmq/issues/173#issuecomment-4037083
# Assertion failed: get_load () == 0 (poller_base.cpp:32)
time.sleep(0.025) | [
"def",
"fire_event",
"(",
"key",
",",
"msg",
",",
"tag",
",",
"sock_dir",
",",
"args",
"=",
"None",
",",
"transport",
"=",
"'zeromq'",
")",
":",
"event",
"=",
"salt",
".",
"utils",
".",
"event",
".",
"get_event",
"(",
"'master'",
",",
"sock_dir",
","... | Fire deploy action | [
"Fire",
"deploy",
"action"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L1997-L2019 | train |
saltstack/salt | salt/utils/cloud.py | scp_file | def scp_file(dest_path, contents=None, kwargs=None, local_file=None):
'''
Use scp or sftp to copy a file to a server
'''
file_to_upload = None
try:
if contents is not None:
try:
tmpfd, file_to_upload = tempfile.mkstemp()
os.write(tmpfd, contents)
finally:
try:
os.close(tmpfd)
except OSError as exc:
if exc.errno != errno.EBADF:
raise exc
log.debug('Uploading %s to %s', dest_path, kwargs['hostname'])
ssh_args = [
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking=no',
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile=/dev/null',
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
]
if local_file is not None:
file_to_upload = local_file
if os.path.isdir(local_file):
ssh_args.append('-r')
if 'key_filename' in kwargs:
# There should never be both a password and an ssh key passed in, so
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(kwargs['key_filename'])
])
if 'port' in kwargs:
ssh_args.append('-oPort={0}'.format(kwargs['port']))
ssh_args.append(__ssh_gateway_arguments(kwargs))
try:
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
ipaddr = '[{0}]'.format(kwargs['hostname'])
else:
ipaddr = kwargs['hostname']
except socket.error:
ipaddr = kwargs['hostname']
if file_to_upload is None:
log.warning(
'No source file to upload. Please make sure that either file '
'contents or the path to a local file are provided.'
)
cmd = (
'scp {0} {1} {2[username]}@{4}:{3} || '
'echo "put {1} {3}" | sftp {0} {2[username]}@{4} || '
'rsync -avz -e "ssh {0}" {1} {2[username]}@{2[hostname]}:{3}'.format(
' '.join(ssh_args), file_to_upload, kwargs, dest_path, ipaddr
)
)
log.debug('SCP command: \'%s\'', cmd)
retcode = _exec_ssh_cmd(cmd,
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
password_retries=3,
**kwargs)
finally:
if contents is not None:
try:
os.remove(file_to_upload)
except OSError as exc:
if exc.errno != errno.ENOENT:
raise exc
return retcode | python | def scp_file(dest_path, contents=None, kwargs=None, local_file=None):
'''
Use scp or sftp to copy a file to a server
'''
file_to_upload = None
try:
if contents is not None:
try:
tmpfd, file_to_upload = tempfile.mkstemp()
os.write(tmpfd, contents)
finally:
try:
os.close(tmpfd)
except OSError as exc:
if exc.errno != errno.EBADF:
raise exc
log.debug('Uploading %s to %s', dest_path, kwargs['hostname'])
ssh_args = [
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking=no',
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile=/dev/null',
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
]
if local_file is not None:
file_to_upload = local_file
if os.path.isdir(local_file):
ssh_args.append('-r')
if 'key_filename' in kwargs:
# There should never be both a password and an ssh key passed in, so
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(kwargs['key_filename'])
])
if 'port' in kwargs:
ssh_args.append('-oPort={0}'.format(kwargs['port']))
ssh_args.append(__ssh_gateway_arguments(kwargs))
try:
if socket.inet_pton(socket.AF_INET6, kwargs['hostname']):
ipaddr = '[{0}]'.format(kwargs['hostname'])
else:
ipaddr = kwargs['hostname']
except socket.error:
ipaddr = kwargs['hostname']
if file_to_upload is None:
log.warning(
'No source file to upload. Please make sure that either file '
'contents or the path to a local file are provided.'
)
cmd = (
'scp {0} {1} {2[username]}@{4}:{3} || '
'echo "put {1} {3}" | sftp {0} {2[username]}@{4} || '
'rsync -avz -e "ssh {0}" {1} {2[username]}@{2[hostname]}:{3}'.format(
' '.join(ssh_args), file_to_upload, kwargs, dest_path, ipaddr
)
)
log.debug('SCP command: \'%s\'', cmd)
retcode = _exec_ssh_cmd(cmd,
error_msg='Failed to upload file \'{0}\': {1}\n{2}',
password_retries=3,
**kwargs)
finally:
if contents is not None:
try:
os.remove(file_to_upload)
except OSError as exc:
if exc.errno != errno.ENOENT:
raise exc
return retcode | [
"def",
"scp_file",
"(",
"dest_path",
",",
"contents",
"=",
"None",
",",
"kwargs",
"=",
"None",
",",
"local_file",
"=",
"None",
")",
":",
"file_to_upload",
"=",
"None",
"try",
":",
"if",
"contents",
"is",
"not",
"None",
":",
"try",
":",
"tmpfd",
",",
... | Use scp or sftp to copy a file to a server | [
"Use",
"scp",
"or",
"sftp",
"to",
"copy",
"a",
"file",
"to",
"a",
"server"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2077-L2164 | train |
saltstack/salt | salt/utils/cloud.py | ssh_file | def ssh_file(opts, dest_path, contents=None, kwargs=None, local_file=None):
'''
Copies a file to the remote SSH target using either sftp or scp, as
configured.
'''
if opts.get('file_transport', 'sftp') == 'sftp':
return sftp_file(dest_path, contents, kwargs, local_file)
return scp_file(dest_path, contents, kwargs, local_file) | python | def ssh_file(opts, dest_path, contents=None, kwargs=None, local_file=None):
'''
Copies a file to the remote SSH target using either sftp or scp, as
configured.
'''
if opts.get('file_transport', 'sftp') == 'sftp':
return sftp_file(dest_path, contents, kwargs, local_file)
return scp_file(dest_path, contents, kwargs, local_file) | [
"def",
"ssh_file",
"(",
"opts",
",",
"dest_path",
",",
"contents",
"=",
"None",
",",
"kwargs",
"=",
"None",
",",
"local_file",
"=",
"None",
")",
":",
"if",
"opts",
".",
"get",
"(",
"'file_transport'",
",",
"'sftp'",
")",
"==",
"'sftp'",
":",
"return",
... | Copies a file to the remote SSH target using either sftp or scp, as
configured. | [
"Copies",
"a",
"file",
"to",
"the",
"remote",
"SSH",
"target",
"using",
"either",
"sftp",
"or",
"scp",
"as",
"configured",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2167-L2174 | train |
saltstack/salt | salt/utils/cloud.py | win_cmd | def win_cmd(command, **kwargs):
'''
Wrapper for commands to be run against Windows boxes
'''
logging_command = kwargs.get('logging_command', None)
try:
proc = NonBlockingPopen(
command,
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stream_stds=kwargs.get('display_ssh_output', True),
logging_command=logging_command,
)
if logging_command is None:
log.debug(
'Executing command(PID %s): \'%s\'',
proc.pid, command
)
else:
log.debug(
'Executing command(PID %s): \'%s\'',
proc.pid, logging_command
)
proc.poll_and_read_until_finish()
proc.communicate()
return proc.returncode
except Exception as err:
log.exception('Failed to execute command \'%s\'', logging_command)
# Signal an error
return 1 | python | def win_cmd(command, **kwargs):
'''
Wrapper for commands to be run against Windows boxes
'''
logging_command = kwargs.get('logging_command', None)
try:
proc = NonBlockingPopen(
command,
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stream_stds=kwargs.get('display_ssh_output', True),
logging_command=logging_command,
)
if logging_command is None:
log.debug(
'Executing command(PID %s): \'%s\'',
proc.pid, command
)
else:
log.debug(
'Executing command(PID %s): \'%s\'',
proc.pid, logging_command
)
proc.poll_and_read_until_finish()
proc.communicate()
return proc.returncode
except Exception as err:
log.exception('Failed to execute command \'%s\'', logging_command)
# Signal an error
return 1 | [
"def",
"win_cmd",
"(",
"command",
",",
"*",
"*",
"kwargs",
")",
":",
"logging_command",
"=",
"kwargs",
".",
"get",
"(",
"'logging_command'",
",",
"None",
")",
"try",
":",
"proc",
"=",
"NonBlockingPopen",
"(",
"command",
",",
"shell",
"=",
"True",
",",
... | Wrapper for commands to be run against Windows boxes | [
"Wrapper",
"for",
"commands",
"to",
"be",
"run",
"against",
"Windows",
"boxes"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2269-L2302 | train |
saltstack/salt | salt/utils/cloud.py | winrm_cmd | def winrm_cmd(session, command, flags, **kwargs):
'''
Wrapper for commands to be run against Windows boxes using WinRM.
'''
log.debug('Executing WinRM command: %s %s', command, flags)
# rebuild the session to ensure we haven't timed out
session.protocol.transport.build_session()
r = session.run_cmd(command, flags)
return r.status_code | python | def winrm_cmd(session, command, flags, **kwargs):
'''
Wrapper for commands to be run against Windows boxes using WinRM.
'''
log.debug('Executing WinRM command: %s %s', command, flags)
# rebuild the session to ensure we haven't timed out
session.protocol.transport.build_session()
r = session.run_cmd(command, flags)
return r.status_code | [
"def",
"winrm_cmd",
"(",
"session",
",",
"command",
",",
"flags",
",",
"*",
"*",
"kwargs",
")",
":",
"log",
".",
"debug",
"(",
"'Executing WinRM command: %s %s'",
",",
"command",
",",
"flags",
")",
"# rebuild the session to ensure we haven't timed out",
"session",
... | Wrapper for commands to be run against Windows boxes using WinRM. | [
"Wrapper",
"for",
"commands",
"to",
"be",
"run",
"against",
"Windows",
"boxes",
"using",
"WinRM",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2305-L2313 | train |
saltstack/salt | salt/utils/cloud.py | root_cmd | def root_cmd(command, tty, sudo, allow_failure=False, **kwargs):
'''
Wrapper for commands to be run as root
'''
logging_command = command
sudo_password = kwargs.get('sudo_password', None)
if sudo:
if sudo_password is None:
command = 'sudo {0}'.format(command)
logging_command = command
else:
logging_command = 'sudo -S "XXX-REDACTED-XXX" {0}'.format(command)
command = 'sudo -S {0}'.format(command)
log.debug('Using sudo to run command %s', logging_command)
ssh_args = []
if tty:
# Use double `-t` on the `ssh` command, it's necessary when `sudo` has
# `requiretty` enforced.
ssh_args.extend(['-t', '-t'])
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
host_key_checking = 'no'
if known_hosts_file != '/dev/null':
host_key_checking = 'yes'
ssh_args.extend([
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking={0}'.format(host_key_checking),
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile={0}'.format(known_hosts_file),
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
])
if 'key_filename' in kwargs:
# There should never be both a password and an ssh key passed in, so
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(kwargs['key_filename'])
])
if 'ssh_timeout' in kwargs:
ssh_args.extend(['-oConnectTimeout={0}'.format(kwargs['ssh_timeout'])])
ssh_args.extend([__ssh_gateway_arguments(kwargs)])
if 'port' in kwargs:
ssh_args.extend(['-p {0}'.format(kwargs['port'])])
cmd = 'ssh {0} {1[username]}@{1[hostname]} '.format(
' '.join(ssh_args),
kwargs
)
logging_command = cmd + logging_command
cmd = cmd + pipes.quote(command)
hard_timeout = kwargs.get('hard_timeout')
if hard_timeout is not None:
logging_command = 'timeout {0} {1}'.format(hard_timeout, logging_command)
cmd = 'timeout {0} {1}'.format(hard_timeout, cmd)
log.debug('SSH command: \'%s\'', logging_command)
retcode = _exec_ssh_cmd(cmd, allow_failure=allow_failure, **kwargs)
return retcode | python | def root_cmd(command, tty, sudo, allow_failure=False, **kwargs):
'''
Wrapper for commands to be run as root
'''
logging_command = command
sudo_password = kwargs.get('sudo_password', None)
if sudo:
if sudo_password is None:
command = 'sudo {0}'.format(command)
logging_command = command
else:
logging_command = 'sudo -S "XXX-REDACTED-XXX" {0}'.format(command)
command = 'sudo -S {0}'.format(command)
log.debug('Using sudo to run command %s', logging_command)
ssh_args = []
if tty:
# Use double `-t` on the `ssh` command, it's necessary when `sudo` has
# `requiretty` enforced.
ssh_args.extend(['-t', '-t'])
known_hosts_file = kwargs.get('known_hosts_file', '/dev/null')
host_key_checking = 'no'
if known_hosts_file != '/dev/null':
host_key_checking = 'yes'
ssh_args.extend([
# Don't add new hosts to the host key database
'-oStrictHostKeyChecking={0}'.format(host_key_checking),
# Set hosts key database path to /dev/null, i.e., non-existing
'-oUserKnownHostsFile={0}'.format(known_hosts_file),
# Don't re-use the SSH connection. Less failures.
'-oControlPath=none'
])
if 'key_filename' in kwargs:
# There should never be both a password and an ssh key passed in, so
ssh_args.extend([
# tell SSH to skip password authentication
'-oPasswordAuthentication=no',
'-oChallengeResponseAuthentication=no',
# Make sure public key authentication is enabled
'-oPubkeyAuthentication=yes',
# do only use the provided identity file
'-oIdentitiesOnly=yes',
# No Keyboard interaction!
'-oKbdInteractiveAuthentication=no',
# Also, specify the location of the key file
'-i {0}'.format(kwargs['key_filename'])
])
if 'ssh_timeout' in kwargs:
ssh_args.extend(['-oConnectTimeout={0}'.format(kwargs['ssh_timeout'])])
ssh_args.extend([__ssh_gateway_arguments(kwargs)])
if 'port' in kwargs:
ssh_args.extend(['-p {0}'.format(kwargs['port'])])
cmd = 'ssh {0} {1[username]}@{1[hostname]} '.format(
' '.join(ssh_args),
kwargs
)
logging_command = cmd + logging_command
cmd = cmd + pipes.quote(command)
hard_timeout = kwargs.get('hard_timeout')
if hard_timeout is not None:
logging_command = 'timeout {0} {1}'.format(hard_timeout, logging_command)
cmd = 'timeout {0} {1}'.format(hard_timeout, cmd)
log.debug('SSH command: \'%s\'', logging_command)
retcode = _exec_ssh_cmd(cmd, allow_failure=allow_failure, **kwargs)
return retcode | [
"def",
"root_cmd",
"(",
"command",
",",
"tty",
",",
"sudo",
",",
"allow_failure",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"logging_command",
"=",
"command",
"sudo_password",
"=",
"kwargs",
".",
"get",
"(",
"'sudo_password'",
",",
"None",
")",
"i... | Wrapper for commands to be run as root | [
"Wrapper",
"for",
"commands",
"to",
"be",
"run",
"as",
"root"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2316-L2392 | train |
saltstack/salt | salt/utils/cloud.py | check_auth | def check_auth(name, sock_dir=None, queue=None, timeout=300):
'''
This function is called from a multiprocess instance, to wait for a minion
to become available to receive salt commands
'''
event = salt.utils.event.SaltEvent('master', sock_dir, listen=True)
starttime = time.mktime(time.localtime())
newtimeout = timeout
log.debug('In check_auth, waiting for %s to become available', name)
while newtimeout > 0:
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ret = event.get_event(full=True)
if ret is None:
continue
if ret['tag'] == 'salt/minion/{0}/start'.format(name):
queue.put(name)
newtimeout = 0
log.debug('Minion %s is ready to receive commands', name) | python | def check_auth(name, sock_dir=None, queue=None, timeout=300):
'''
This function is called from a multiprocess instance, to wait for a minion
to become available to receive salt commands
'''
event = salt.utils.event.SaltEvent('master', sock_dir, listen=True)
starttime = time.mktime(time.localtime())
newtimeout = timeout
log.debug('In check_auth, waiting for %s to become available', name)
while newtimeout > 0:
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ret = event.get_event(full=True)
if ret is None:
continue
if ret['tag'] == 'salt/minion/{0}/start'.format(name):
queue.put(name)
newtimeout = 0
log.debug('Minion %s is ready to receive commands', name) | [
"def",
"check_auth",
"(",
"name",
",",
"sock_dir",
"=",
"None",
",",
"queue",
"=",
"None",
",",
"timeout",
"=",
"300",
")",
":",
"event",
"=",
"salt",
".",
"utils",
".",
"event",
".",
"SaltEvent",
"(",
"'master'",
",",
"sock_dir",
",",
"listen",
"=",... | This function is called from a multiprocess instance, to wait for a minion
to become available to receive salt commands | [
"This",
"function",
"is",
"called",
"from",
"a",
"multiprocess",
"instance",
"to",
"wait",
"for",
"a",
"minion",
"to",
"become",
"available",
"to",
"receive",
"salt",
"commands"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2395-L2412 | train |
saltstack/salt | salt/utils/cloud.py | ip_to_int | def ip_to_int(ip):
'''
Converts an IP address to an integer
'''
ret = 0
for octet in ip.split('.'):
ret = ret * 256 + int(octet)
return ret | python | def ip_to_int(ip):
'''
Converts an IP address to an integer
'''
ret = 0
for octet in ip.split('.'):
ret = ret * 256 + int(octet)
return ret | [
"def",
"ip_to_int",
"(",
"ip",
")",
":",
"ret",
"=",
"0",
"for",
"octet",
"in",
"ip",
".",
"split",
"(",
"'.'",
")",
":",
"ret",
"=",
"ret",
"*",
"256",
"+",
"int",
"(",
"octet",
")",
"return",
"ret"
] | Converts an IP address to an integer | [
"Converts",
"an",
"IP",
"address",
"to",
"an",
"integer"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2415-L2422 | train |
saltstack/salt | salt/utils/cloud.py | is_public_ip | def is_public_ip(ip):
'''
Determines whether an IP address falls within one of the private IP ranges
'''
if ':' in ip:
# ipv6
if ip.startswith('fe80:'):
# ipv6 link local
return False
return True
addr = ip_to_int(ip)
if 167772160 < addr < 184549375:
# 10.0.0.0/8
return False
elif 3232235520 < addr < 3232301055:
# 192.168.0.0/16
return False
elif 2886729728 < addr < 2887778303:
# 172.16.0.0/12
return False
elif 2130706432 < addr < 2147483647:
# 127.0.0.0/8
return False
return True | python | def is_public_ip(ip):
'''
Determines whether an IP address falls within one of the private IP ranges
'''
if ':' in ip:
# ipv6
if ip.startswith('fe80:'):
# ipv6 link local
return False
return True
addr = ip_to_int(ip)
if 167772160 < addr < 184549375:
# 10.0.0.0/8
return False
elif 3232235520 < addr < 3232301055:
# 192.168.0.0/16
return False
elif 2886729728 < addr < 2887778303:
# 172.16.0.0/12
return False
elif 2130706432 < addr < 2147483647:
# 127.0.0.0/8
return False
return True | [
"def",
"is_public_ip",
"(",
"ip",
")",
":",
"if",
"':'",
"in",
"ip",
":",
"# ipv6",
"if",
"ip",
".",
"startswith",
"(",
"'fe80:'",
")",
":",
"# ipv6 link local",
"return",
"False",
"return",
"True",
"addr",
"=",
"ip_to_int",
"(",
"ip",
")",
"if",
"1677... | Determines whether an IP address falls within one of the private IP ranges | [
"Determines",
"whether",
"an",
"IP",
"address",
"falls",
"within",
"one",
"of",
"the",
"private",
"IP",
"ranges"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2425-L2448 | train |
saltstack/salt | salt/utils/cloud.py | check_name | def check_name(name, safe_chars):
'''
Check whether the specified name contains invalid characters
'''
regexp = re.compile('[^{0}]'.format(safe_chars))
if regexp.search(name):
raise SaltCloudException(
'{0} contains characters not supported by this cloud provider. '
'Valid characters are: {1}'.format(
name, safe_chars
)
) | python | def check_name(name, safe_chars):
'''
Check whether the specified name contains invalid characters
'''
regexp = re.compile('[^{0}]'.format(safe_chars))
if regexp.search(name):
raise SaltCloudException(
'{0} contains characters not supported by this cloud provider. '
'Valid characters are: {1}'.format(
name, safe_chars
)
) | [
"def",
"check_name",
"(",
"name",
",",
"safe_chars",
")",
":",
"regexp",
"=",
"re",
".",
"compile",
"(",
"'[^{0}]'",
".",
"format",
"(",
"safe_chars",
")",
")",
"if",
"regexp",
".",
"search",
"(",
"name",
")",
":",
"raise",
"SaltCloudException",
"(",
"... | Check whether the specified name contains invalid characters | [
"Check",
"whether",
"the",
"specified",
"name",
"contains",
"invalid",
"characters"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2451-L2462 | train |
saltstack/salt | salt/utils/cloud.py | remove_sshkey | def remove_sshkey(host, known_hosts=None):
'''
Remove a host from the known_hosts file
'''
if known_hosts is None:
if 'HOME' in os.environ:
known_hosts = '{0}/.ssh/known_hosts'.format(os.environ['HOME'])
else:
try:
known_hosts = '{0}/.ssh/known_hosts'.format(
pwd.getpwuid(os.getuid()).pwd_dir
)
except Exception:
pass
if known_hosts is not None:
log.debug(
'Removing ssh key for %s from known hosts file %s',
host, known_hosts
)
else:
log.debug('Removing ssh key for %s from known hosts file', host)
cmd = 'ssh-keygen -R {0}'.format(host)
subprocess.call(cmd, shell=True) | python | def remove_sshkey(host, known_hosts=None):
'''
Remove a host from the known_hosts file
'''
if known_hosts is None:
if 'HOME' in os.environ:
known_hosts = '{0}/.ssh/known_hosts'.format(os.environ['HOME'])
else:
try:
known_hosts = '{0}/.ssh/known_hosts'.format(
pwd.getpwuid(os.getuid()).pwd_dir
)
except Exception:
pass
if known_hosts is not None:
log.debug(
'Removing ssh key for %s from known hosts file %s',
host, known_hosts
)
else:
log.debug('Removing ssh key for %s from known hosts file', host)
cmd = 'ssh-keygen -R {0}'.format(host)
subprocess.call(cmd, shell=True) | [
"def",
"remove_sshkey",
"(",
"host",
",",
"known_hosts",
"=",
"None",
")",
":",
"if",
"known_hosts",
"is",
"None",
":",
"if",
"'HOME'",
"in",
"os",
".",
"environ",
":",
"known_hosts",
"=",
"'{0}/.ssh/known_hosts'",
".",
"format",
"(",
"os",
".",
"environ",... | Remove a host from the known_hosts file | [
"Remove",
"a",
"host",
"from",
"the",
"known_hosts",
"file"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2465-L2489 | train |
saltstack/salt | salt/utils/cloud.py | list_nodes_select | def list_nodes_select(nodes, selection, call=None):
'''
Return a list of the VMs that are on the provider, with select fields
'''
if call == 'action':
raise SaltCloudSystemExit(
'The list_nodes_select function must be called '
'with -f or --function.'
)
if 'error' in nodes:
raise SaltCloudSystemExit(
'An error occurred while listing nodes: {0}'.format(
nodes['error']['Errors']['Error']['Message']
)
)
ret = {}
for node in nodes:
pairs = {}
data = nodes[node]
for key in data:
if six.text_type(key) in selection:
value = data[key]
pairs[key] = value
ret[node] = pairs
return ret | python | def list_nodes_select(nodes, selection, call=None):
'''
Return a list of the VMs that are on the provider, with select fields
'''
if call == 'action':
raise SaltCloudSystemExit(
'The list_nodes_select function must be called '
'with -f or --function.'
)
if 'error' in nodes:
raise SaltCloudSystemExit(
'An error occurred while listing nodes: {0}'.format(
nodes['error']['Errors']['Error']['Message']
)
)
ret = {}
for node in nodes:
pairs = {}
data = nodes[node]
for key in data:
if six.text_type(key) in selection:
value = data[key]
pairs[key] = value
ret[node] = pairs
return ret | [
"def",
"list_nodes_select",
"(",
"nodes",
",",
"selection",
",",
"call",
"=",
"None",
")",
":",
"if",
"call",
"==",
"'action'",
":",
"raise",
"SaltCloudSystemExit",
"(",
"'The list_nodes_select function must be called '",
"'with -f or --function.'",
")",
"if",
"'error... | Return a list of the VMs that are on the provider, with select fields | [
"Return",
"a",
"list",
"of",
"the",
"VMs",
"that",
"are",
"on",
"the",
"provider",
"with",
"select",
"fields"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2565-L2592 | train |
saltstack/salt | salt/utils/cloud.py | lock_file | def lock_file(filename, interval=.5, timeout=15):
'''
Lock a file; if it is already locked, then wait for it to become available
before locking it.
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms.
'''
log.trace('Attempting to obtain lock for %s', filename)
lock = filename + '.lock'
start = time.time()
while True:
if os.path.exists(lock):
if time.time() - start >= timeout:
log.warning('Unable to obtain lock for %s', filename)
return False
time.sleep(interval)
else:
break
with salt.utils.files.fopen(lock, 'a'):
pass | python | def lock_file(filename, interval=.5, timeout=15):
'''
Lock a file; if it is already locked, then wait for it to become available
before locking it.
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms.
'''
log.trace('Attempting to obtain lock for %s', filename)
lock = filename + '.lock'
start = time.time()
while True:
if os.path.exists(lock):
if time.time() - start >= timeout:
log.warning('Unable to obtain lock for %s', filename)
return False
time.sleep(interval)
else:
break
with salt.utils.files.fopen(lock, 'a'):
pass | [
"def",
"lock_file",
"(",
"filename",
",",
"interval",
"=",
".5",
",",
"timeout",
"=",
"15",
")",
":",
"log",
".",
"trace",
"(",
"'Attempting to obtain lock for %s'",
",",
"filename",
")",
"lock",
"=",
"filename",
"+",
"'.lock'",
"start",
"=",
"time",
".",
... | Lock a file; if it is already locked, then wait for it to become available
before locking it.
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms. | [
"Lock",
"a",
"file",
";",
"if",
"it",
"is",
"already",
"locked",
"then",
"wait",
"for",
"it",
"to",
"become",
"available",
"before",
"locking",
"it",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2595-L2616 | train |
saltstack/salt | salt/utils/cloud.py | unlock_file | def unlock_file(filename):
'''
Unlock a locked file
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms.
'''
log.trace('Removing lock for %s', filename)
lock = filename + '.lock'
try:
os.remove(lock)
except OSError as exc:
log.trace('Unable to remove lock for %s: %s', filename, exc) | python | def unlock_file(filename):
'''
Unlock a locked file
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms.
'''
log.trace('Removing lock for %s', filename)
lock = filename + '.lock'
try:
os.remove(lock)
except OSError as exc:
log.trace('Unable to remove lock for %s: %s', filename, exc) | [
"def",
"unlock_file",
"(",
"filename",
")",
":",
"log",
".",
"trace",
"(",
"'Removing lock for %s'",
",",
"filename",
")",
"lock",
"=",
"filename",
"+",
"'.lock'",
"try",
":",
"os",
".",
"remove",
"(",
"lock",
")",
"except",
"OSError",
"as",
"exc",
":",
... | Unlock a locked file
Note that these locks are only recognized by Salt Cloud, and not other
programs or platforms. | [
"Unlock",
"a",
"locked",
"file"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2619-L2631 | train |
saltstack/salt | salt/utils/cloud.py | cachedir_index_add | def cachedir_index_add(minion_id, profile, driver, provider, base=None):
'''
Add an entry to the cachedir index. This generally only needs to happen when
a new instance is created. This entry should contain:
.. code-block:: yaml
- minion_id
- profile used to create the instance
- provider and driver name
The intent of this function is to speed up lookups for the cloud roster for
salt-ssh. However, other code that makes use of profile information can also
make use of this function.
'''
base = init_cachedir(base)
index_file = os.path.join(base, 'index.p')
lock_file(index_file)
if os.path.exists(index_file):
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(index_file, mode) as fh_:
index = salt.utils.data.decode(
salt.utils.msgpack.msgpack.load(
fh_, encoding=MSGPACK_ENCODING))
else:
index = {}
prov_comps = provider.split(':')
index.update({
minion_id: {
'id': minion_id,
'profile': profile,
'driver': driver,
'provider': prov_comps[0],
}
})
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(index_file, mode) as fh_:
salt.utils.msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file) | python | def cachedir_index_add(minion_id, profile, driver, provider, base=None):
'''
Add an entry to the cachedir index. This generally only needs to happen when
a new instance is created. This entry should contain:
.. code-block:: yaml
- minion_id
- profile used to create the instance
- provider and driver name
The intent of this function is to speed up lookups for the cloud roster for
salt-ssh. However, other code that makes use of profile information can also
make use of this function.
'''
base = init_cachedir(base)
index_file = os.path.join(base, 'index.p')
lock_file(index_file)
if os.path.exists(index_file):
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(index_file, mode) as fh_:
index = salt.utils.data.decode(
salt.utils.msgpack.msgpack.load(
fh_, encoding=MSGPACK_ENCODING))
else:
index = {}
prov_comps = provider.split(':')
index.update({
minion_id: {
'id': minion_id,
'profile': profile,
'driver': driver,
'provider': prov_comps[0],
}
})
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(index_file, mode) as fh_:
salt.utils.msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file) | [
"def",
"cachedir_index_add",
"(",
"minion_id",
",",
"profile",
",",
"driver",
",",
"provider",
",",
"base",
"=",
"None",
")",
":",
"base",
"=",
"init_cachedir",
"(",
"base",
")",
"index_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"base",
",",
"'in... | Add an entry to the cachedir index. This generally only needs to happen when
a new instance is created. This entry should contain:
.. code-block:: yaml
- minion_id
- profile used to create the instance
- provider and driver name
The intent of this function is to speed up lookups for the cloud roster for
salt-ssh. However, other code that makes use of profile information can also
make use of this function. | [
"Add",
"an",
"entry",
"to",
"the",
"cachedir",
"index",
".",
"This",
"generally",
"only",
"needs",
"to",
"happen",
"when",
"a",
"new",
"instance",
"is",
"created",
".",
"This",
"entry",
"should",
"contain",
":"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2634-L2677 | train |
saltstack/salt | salt/utils/cloud.py | cachedir_index_del | def cachedir_index_del(minion_id, base=None):
'''
Delete an entry from the cachedir index. This generally only needs to happen
when an instance is deleted.
'''
base = init_cachedir(base)
index_file = os.path.join(base, 'index.p')
lock_file(index_file)
if os.path.exists(index_file):
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(index_file, mode) as fh_:
index = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
else:
return
if minion_id in index:
del index[minion_id]
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(index_file, mode) as fh_:
salt.utils.msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file) | python | def cachedir_index_del(minion_id, base=None):
'''
Delete an entry from the cachedir index. This generally only needs to happen
when an instance is deleted.
'''
base = init_cachedir(base)
index_file = os.path.join(base, 'index.p')
lock_file(index_file)
if os.path.exists(index_file):
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(index_file, mode) as fh_:
index = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
else:
return
if minion_id in index:
del index[minion_id]
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(index_file, mode) as fh_:
salt.utils.msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file) | [
"def",
"cachedir_index_del",
"(",
"minion_id",
",",
"base",
"=",
"None",
")",
":",
"base",
"=",
"init_cachedir",
"(",
"base",
")",
"index_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"base",
",",
"'index.p'",
")",
"lock_file",
"(",
"index_file",
")",... | Delete an entry from the cachedir index. This generally only needs to happen
when an instance is deleted. | [
"Delete",
"an",
"entry",
"from",
"the",
"cachedir",
"index",
".",
"This",
"generally",
"only",
"needs",
"to",
"happen",
"when",
"an",
"instance",
"is",
"deleted",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2680-L2704 | train |
saltstack/salt | salt/utils/cloud.py | init_cachedir | def init_cachedir(base=None):
'''
Initialize the cachedir needed for Salt Cloud to keep track of minions
'''
if base is None:
base = __opts__['cachedir']
needed_dirs = (base,
os.path.join(base, 'requested'),
os.path.join(base, 'active'))
for dir_ in needed_dirs:
if not os.path.exists(dir_):
os.makedirs(dir_)
os.chmod(base, 0o755)
return base | python | def init_cachedir(base=None):
'''
Initialize the cachedir needed for Salt Cloud to keep track of minions
'''
if base is None:
base = __opts__['cachedir']
needed_dirs = (base,
os.path.join(base, 'requested'),
os.path.join(base, 'active'))
for dir_ in needed_dirs:
if not os.path.exists(dir_):
os.makedirs(dir_)
os.chmod(base, 0o755)
return base | [
"def",
"init_cachedir",
"(",
"base",
"=",
"None",
")",
":",
"if",
"base",
"is",
"None",
":",
"base",
"=",
"__opts__",
"[",
"'cachedir'",
"]",
"needed_dirs",
"=",
"(",
"base",
",",
"os",
".",
"path",
".",
"join",
"(",
"base",
",",
"'requested'",
")",
... | Initialize the cachedir needed for Salt Cloud to keep track of minions | [
"Initialize",
"the",
"cachedir",
"needed",
"for",
"Salt",
"Cloud",
"to",
"keep",
"track",
"of",
"minions"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2707-L2721 | train |
saltstack/salt | salt/utils/cloud.py | request_minion_cachedir | def request_minion_cachedir(
minion_id,
opts=None,
fingerprint='',
pubkey=None,
provider=None,
base=None,
):
'''
Creates an entry in the requested/ cachedir. This means that Salt Cloud has
made a request to a cloud provider to create an instance, but it has not
yet verified that the instance properly exists.
If the fingerprint is unknown, a raw pubkey can be passed in, and a
fingerprint will be calculated. If both are empty, then the fingerprint
will be set to None.
'''
if base is None:
base = __opts__['cachedir']
if not fingerprint and pubkey is not None:
fingerprint = salt.utils.crypt.pem_finger(key=pubkey, sum_type=(opts and opts.get('hash_type') or 'sha256'))
init_cachedir(base)
data = {
'minion_id': minion_id,
'fingerprint': fingerprint,
'provider': provider,
}
fname = '{0}.p'.format(minion_id)
path = os.path.join(base, 'requested', fname)
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(data, fh_, encoding=MSGPACK_ENCODING) | python | def request_minion_cachedir(
minion_id,
opts=None,
fingerprint='',
pubkey=None,
provider=None,
base=None,
):
'''
Creates an entry in the requested/ cachedir. This means that Salt Cloud has
made a request to a cloud provider to create an instance, but it has not
yet verified that the instance properly exists.
If the fingerprint is unknown, a raw pubkey can be passed in, and a
fingerprint will be calculated. If both are empty, then the fingerprint
will be set to None.
'''
if base is None:
base = __opts__['cachedir']
if not fingerprint and pubkey is not None:
fingerprint = salt.utils.crypt.pem_finger(key=pubkey, sum_type=(opts and opts.get('hash_type') or 'sha256'))
init_cachedir(base)
data = {
'minion_id': minion_id,
'fingerprint': fingerprint,
'provider': provider,
}
fname = '{0}.p'.format(minion_id)
path = os.path.join(base, 'requested', fname)
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(data, fh_, encoding=MSGPACK_ENCODING) | [
"def",
"request_minion_cachedir",
"(",
"minion_id",
",",
"opts",
"=",
"None",
",",
"fingerprint",
"=",
"''",
",",
"pubkey",
"=",
"None",
",",
"provider",
"=",
"None",
",",
"base",
"=",
"None",
",",
")",
":",
"if",
"base",
"is",
"None",
":",
"base",
"... | Creates an entry in the requested/ cachedir. This means that Salt Cloud has
made a request to a cloud provider to create an instance, but it has not
yet verified that the instance properly exists.
If the fingerprint is unknown, a raw pubkey can be passed in, and a
fingerprint will be calculated. If both are empty, then the fingerprint
will be set to None. | [
"Creates",
"an",
"entry",
"in",
"the",
"requested",
"/",
"cachedir",
".",
"This",
"means",
"that",
"Salt",
"Cloud",
"has",
"made",
"a",
"request",
"to",
"a",
"cloud",
"provider",
"to",
"create",
"an",
"instance",
"but",
"it",
"has",
"not",
"yet",
"verifi... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2725-L2760 | train |
saltstack/salt | salt/utils/cloud.py | change_minion_cachedir | def change_minion_cachedir(
minion_id,
cachedir,
data=None,
base=None,
):
'''
Changes the info inside a minion's cachedir entry. The type of cachedir
must be specified (i.e., 'requested' or 'active'). A dict is also passed in
which contains the data to be changed.
Example:
change_minion_cachedir(
'myminion',
'requested',
{'fingerprint': '26:5c:8c:de:be:fe:89:c0:02:ed:27:65:0e:bb:be:60'},
)
'''
if not isinstance(data, dict):
return False
if base is None:
base = __opts__['cachedir']
fname = '{0}.p'.format(minion_id)
path = os.path.join(base, cachedir, fname)
with salt.utils.files.fopen(path, 'r') as fh_:
cache_data = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
cache_data.update(data)
with salt.utils.files.fopen(path, 'w') as fh_:
salt.utils.msgpack.dump(cache_data, fh_, encoding=MSGPACK_ENCODING) | python | def change_minion_cachedir(
minion_id,
cachedir,
data=None,
base=None,
):
'''
Changes the info inside a minion's cachedir entry. The type of cachedir
must be specified (i.e., 'requested' or 'active'). A dict is also passed in
which contains the data to be changed.
Example:
change_minion_cachedir(
'myminion',
'requested',
{'fingerprint': '26:5c:8c:de:be:fe:89:c0:02:ed:27:65:0e:bb:be:60'},
)
'''
if not isinstance(data, dict):
return False
if base is None:
base = __opts__['cachedir']
fname = '{0}.p'.format(minion_id)
path = os.path.join(base, cachedir, fname)
with salt.utils.files.fopen(path, 'r') as fh_:
cache_data = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
cache_data.update(data)
with salt.utils.files.fopen(path, 'w') as fh_:
salt.utils.msgpack.dump(cache_data, fh_, encoding=MSGPACK_ENCODING) | [
"def",
"change_minion_cachedir",
"(",
"minion_id",
",",
"cachedir",
",",
"data",
"=",
"None",
",",
"base",
"=",
"None",
",",
")",
":",
"if",
"not",
"isinstance",
"(",
"data",
",",
"dict",
")",
":",
"return",
"False",
"if",
"base",
"is",
"None",
":",
... | Changes the info inside a minion's cachedir entry. The type of cachedir
must be specified (i.e., 'requested' or 'active'). A dict is also passed in
which contains the data to be changed.
Example:
change_minion_cachedir(
'myminion',
'requested',
{'fingerprint': '26:5c:8c:de:be:fe:89:c0:02:ed:27:65:0e:bb:be:60'},
) | [
"Changes",
"the",
"info",
"inside",
"a",
"minion",
"s",
"cachedir",
"entry",
".",
"The",
"type",
"of",
"cachedir",
"must",
"be",
"specified",
"(",
"i",
".",
"e",
".",
"requested",
"or",
"active",
")",
".",
"A",
"dict",
"is",
"also",
"passed",
"in",
"... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2763-L2798 | train |
saltstack/salt | salt/utils/cloud.py | activate_minion_cachedir | def activate_minion_cachedir(minion_id, base=None):
'''
Moves a minion from the requested/ cachedir into the active/ cachedir. This
means that Salt Cloud has verified that a requested instance properly
exists, and should be expected to exist from here on out.
'''
if base is None:
base = __opts__['cachedir']
fname = '{0}.p'.format(minion_id)
src = os.path.join(base, 'requested', fname)
dst = os.path.join(base, 'active')
shutil.move(src, dst) | python | def activate_minion_cachedir(minion_id, base=None):
'''
Moves a minion from the requested/ cachedir into the active/ cachedir. This
means that Salt Cloud has verified that a requested instance properly
exists, and should be expected to exist from here on out.
'''
if base is None:
base = __opts__['cachedir']
fname = '{0}.p'.format(minion_id)
src = os.path.join(base, 'requested', fname)
dst = os.path.join(base, 'active')
shutil.move(src, dst) | [
"def",
"activate_minion_cachedir",
"(",
"minion_id",
",",
"base",
"=",
"None",
")",
":",
"if",
"base",
"is",
"None",
":",
"base",
"=",
"__opts__",
"[",
"'cachedir'",
"]",
"fname",
"=",
"'{0}.p'",
".",
"format",
"(",
"minion_id",
")",
"src",
"=",
"os",
... | Moves a minion from the requested/ cachedir into the active/ cachedir. This
means that Salt Cloud has verified that a requested instance properly
exists, and should be expected to exist from here on out. | [
"Moves",
"a",
"minion",
"from",
"the",
"requested",
"/",
"cachedir",
"into",
"the",
"active",
"/",
"cachedir",
".",
"This",
"means",
"that",
"Salt",
"Cloud",
"has",
"verified",
"that",
"a",
"requested",
"instance",
"properly",
"exists",
"and",
"should",
"be"... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2801-L2813 | train |
saltstack/salt | salt/utils/cloud.py | delete_minion_cachedir | def delete_minion_cachedir(minion_id, provider, opts, base=None):
'''
Deletes a minion's entry from the cloud cachedir. It will search through
all cachedirs to find the minion's cache file.
Needs `update_cachedir` set to True.
'''
if isinstance(opts, dict):
__opts__.update(opts)
if __opts__.get('update_cachedir', False) is False:
return
if base is None:
base = __opts__['cachedir']
driver = next(six.iterkeys(__opts__['providers'][provider]))
fname = '{0}.p'.format(minion_id)
for cachedir in 'requested', 'active':
path = os.path.join(base, cachedir, driver, provider, fname)
log.debug('path: %s', path)
if os.path.exists(path):
os.remove(path) | python | def delete_minion_cachedir(minion_id, provider, opts, base=None):
'''
Deletes a minion's entry from the cloud cachedir. It will search through
all cachedirs to find the minion's cache file.
Needs `update_cachedir` set to True.
'''
if isinstance(opts, dict):
__opts__.update(opts)
if __opts__.get('update_cachedir', False) is False:
return
if base is None:
base = __opts__['cachedir']
driver = next(six.iterkeys(__opts__['providers'][provider]))
fname = '{0}.p'.format(minion_id)
for cachedir in 'requested', 'active':
path = os.path.join(base, cachedir, driver, provider, fname)
log.debug('path: %s', path)
if os.path.exists(path):
os.remove(path) | [
"def",
"delete_minion_cachedir",
"(",
"minion_id",
",",
"provider",
",",
"opts",
",",
"base",
"=",
"None",
")",
":",
"if",
"isinstance",
"(",
"opts",
",",
"dict",
")",
":",
"__opts__",
".",
"update",
"(",
"opts",
")",
"if",
"__opts__",
".",
"get",
"(",... | Deletes a minion's entry from the cloud cachedir. It will search through
all cachedirs to find the minion's cache file.
Needs `update_cachedir` set to True. | [
"Deletes",
"a",
"minion",
"s",
"entry",
"from",
"the",
"cloud",
"cachedir",
".",
"It",
"will",
"search",
"through",
"all",
"cachedirs",
"to",
"find",
"the",
"minion",
"s",
"cache",
"file",
".",
"Needs",
"update_cachedir",
"set",
"to",
"True",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2816-L2837 | train |
saltstack/salt | salt/utils/cloud.py | list_cache_nodes_full | def list_cache_nodes_full(opts=None, provider=None, base=None):
'''
Return a list of minion data from the cloud cache, rather from the cloud
providers themselves. This is the cloud cache version of list_nodes_full().
'''
if opts is None:
opts = __opts__
if opts.get('update_cachedir', False) is False:
return
if base is None:
base = os.path.join(opts['cachedir'], 'active')
minions = {}
# First, get a list of all drivers in use
for driver in os.listdir(base):
minions[driver] = {}
prov_dir = os.path.join(base, driver)
# Then, get a list of all providers per driver
for prov in os.listdir(prov_dir):
# If a specific provider is requested, filter out everyone else
if provider and provider != prov:
continue
minions[driver][prov] = {}
min_dir = os.path.join(prov_dir, prov)
# Get a list of all nodes per provider
for fname in os.listdir(min_dir):
# Finally, get a list of full minion data
fpath = os.path.join(min_dir, fname)
minion_id = fname[:-2] # strip '.p' from end of msgpack filename
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(fpath, mode) as fh_:
minions[driver][prov][minion_id] = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
return minions | python | def list_cache_nodes_full(opts=None, provider=None, base=None):
'''
Return a list of minion data from the cloud cache, rather from the cloud
providers themselves. This is the cloud cache version of list_nodes_full().
'''
if opts is None:
opts = __opts__
if opts.get('update_cachedir', False) is False:
return
if base is None:
base = os.path.join(opts['cachedir'], 'active')
minions = {}
# First, get a list of all drivers in use
for driver in os.listdir(base):
minions[driver] = {}
prov_dir = os.path.join(base, driver)
# Then, get a list of all providers per driver
for prov in os.listdir(prov_dir):
# If a specific provider is requested, filter out everyone else
if provider and provider != prov:
continue
minions[driver][prov] = {}
min_dir = os.path.join(prov_dir, prov)
# Get a list of all nodes per provider
for fname in os.listdir(min_dir):
# Finally, get a list of full minion data
fpath = os.path.join(min_dir, fname)
minion_id = fname[:-2] # strip '.p' from end of msgpack filename
mode = 'rb' if six.PY3 else 'r'
with salt.utils.files.fopen(fpath, mode) as fh_:
minions[driver][prov][minion_id] = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
return minions | [
"def",
"list_cache_nodes_full",
"(",
"opts",
"=",
"None",
",",
"provider",
"=",
"None",
",",
"base",
"=",
"None",
")",
":",
"if",
"opts",
"is",
"None",
":",
"opts",
"=",
"__opts__",
"if",
"opts",
".",
"get",
"(",
"'update_cachedir'",
",",
"False",
")",... | Return a list of minion data from the cloud cache, rather from the cloud
providers themselves. This is the cloud cache version of list_nodes_full(). | [
"Return",
"a",
"list",
"of",
"minion",
"data",
"from",
"the",
"cloud",
"cache",
"rather",
"from",
"the",
"cloud",
"providers",
"themselves",
".",
"This",
"is",
"the",
"cloud",
"cache",
"version",
"of",
"list_nodes_full",
"()",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2840-L2875 | train |
saltstack/salt | salt/utils/cloud.py | update_bootstrap | def update_bootstrap(config, url=None):
'''
Update the salt-bootstrap script
url can be one of:
- The URL to fetch the bootstrap script from
- The absolute path to the bootstrap
- The content of the bootstrap script
'''
default_url = config.get('bootstrap_script_url',
'https://bootstrap.saltstack.com')
if not url:
url = default_url
if not url:
raise ValueError('Cant get any source to update')
if url.startswith('http') or '://' in url:
log.debug('Updating the bootstrap-salt.sh script to latest stable')
try:
import requests
except ImportError:
return {'error': (
'Updating the bootstrap-salt.sh script requires the '
'Python requests library to be installed'
)}
req = requests.get(url)
if req.status_code != 200:
return {'error': (
'Failed to download the latest stable version of the '
'bootstrap-salt.sh script from {0}. HTTP error: '
'{1}'.format(
url, req.status_code
)
)}
script_content = req.text
if url == default_url:
script_name = 'bootstrap-salt.sh'
else:
script_name = os.path.basename(url)
elif os.path.exists(url):
with salt.utils.files.fopen(url) as fic:
script_content = salt.utils.stringutils.to_unicode(fic.read())
script_name = os.path.basename(url)
# in last case, assuming we got a script content
else:
script_content = url
script_name = '{0}.sh'.format(
hashlib.sha1(script_content).hexdigest()
)
if not script_content:
raise ValueError('No content in bootstrap script !')
# Get the path to the built-in deploy scripts directory
builtin_deploy_dir = os.path.join(
os.path.dirname(__file__),
'deploy'
)
# Compute the search path from the current loaded opts conf_file
# value
deploy_d_from_conf_file = os.path.join(
os.path.dirname(config['conf_file']),
'cloud.deploy.d'
)
# Compute the search path using the install time defined
# syspaths.CONF_DIR
deploy_d_from_syspaths = os.path.join(
config['config_dir'],
'cloud.deploy.d'
)
# Get a copy of any defined search paths, flagging them not to
# create parent
deploy_scripts_search_paths = []
for entry in config.get('deploy_scripts_search_path', []):
if entry.startswith(builtin_deploy_dir):
# We won't write the updated script to the built-in deploy
# directory
continue
if entry in (deploy_d_from_conf_file, deploy_d_from_syspaths):
# Allow parent directories to be made
deploy_scripts_search_paths.append((entry, True))
else:
deploy_scripts_search_paths.append((entry, False))
# In case the user is not using defaults and the computed
# 'cloud.deploy.d' from conf_file and syspaths is not included, add
# them
if deploy_d_from_conf_file not in deploy_scripts_search_paths:
deploy_scripts_search_paths.append(
(deploy_d_from_conf_file, True)
)
if deploy_d_from_syspaths not in deploy_scripts_search_paths:
deploy_scripts_search_paths.append(
(deploy_d_from_syspaths, True)
)
finished = []
finished_full = []
for entry, makedirs in deploy_scripts_search_paths:
# This handles duplicate entries, which are likely to appear
if entry in finished:
continue
else:
finished.append(entry)
if makedirs and not os.path.isdir(entry):
try:
os.makedirs(entry)
except (OSError, IOError) as err:
log.info('Failed to create directory \'%s\'', entry)
continue
if not is_writeable(entry):
log.debug('The \'%s\' is not writeable. Continuing...', entry)
continue
deploy_path = os.path.join(entry, script_name)
try:
finished_full.append(deploy_path)
with salt.utils.files.fopen(deploy_path, 'w') as fp_:
fp_.write(salt.utils.stringutils.to_str(script_content))
except (OSError, IOError) as err:
log.debug('Failed to write the updated script: %s', err)
continue
return {'Success': {'Files updated': finished_full}} | python | def update_bootstrap(config, url=None):
'''
Update the salt-bootstrap script
url can be one of:
- The URL to fetch the bootstrap script from
- The absolute path to the bootstrap
- The content of the bootstrap script
'''
default_url = config.get('bootstrap_script_url',
'https://bootstrap.saltstack.com')
if not url:
url = default_url
if not url:
raise ValueError('Cant get any source to update')
if url.startswith('http') or '://' in url:
log.debug('Updating the bootstrap-salt.sh script to latest stable')
try:
import requests
except ImportError:
return {'error': (
'Updating the bootstrap-salt.sh script requires the '
'Python requests library to be installed'
)}
req = requests.get(url)
if req.status_code != 200:
return {'error': (
'Failed to download the latest stable version of the '
'bootstrap-salt.sh script from {0}. HTTP error: '
'{1}'.format(
url, req.status_code
)
)}
script_content = req.text
if url == default_url:
script_name = 'bootstrap-salt.sh'
else:
script_name = os.path.basename(url)
elif os.path.exists(url):
with salt.utils.files.fopen(url) as fic:
script_content = salt.utils.stringutils.to_unicode(fic.read())
script_name = os.path.basename(url)
# in last case, assuming we got a script content
else:
script_content = url
script_name = '{0}.sh'.format(
hashlib.sha1(script_content).hexdigest()
)
if not script_content:
raise ValueError('No content in bootstrap script !')
# Get the path to the built-in deploy scripts directory
builtin_deploy_dir = os.path.join(
os.path.dirname(__file__),
'deploy'
)
# Compute the search path from the current loaded opts conf_file
# value
deploy_d_from_conf_file = os.path.join(
os.path.dirname(config['conf_file']),
'cloud.deploy.d'
)
# Compute the search path using the install time defined
# syspaths.CONF_DIR
deploy_d_from_syspaths = os.path.join(
config['config_dir'],
'cloud.deploy.d'
)
# Get a copy of any defined search paths, flagging them not to
# create parent
deploy_scripts_search_paths = []
for entry in config.get('deploy_scripts_search_path', []):
if entry.startswith(builtin_deploy_dir):
# We won't write the updated script to the built-in deploy
# directory
continue
if entry in (deploy_d_from_conf_file, deploy_d_from_syspaths):
# Allow parent directories to be made
deploy_scripts_search_paths.append((entry, True))
else:
deploy_scripts_search_paths.append((entry, False))
# In case the user is not using defaults and the computed
# 'cloud.deploy.d' from conf_file and syspaths is not included, add
# them
if deploy_d_from_conf_file not in deploy_scripts_search_paths:
deploy_scripts_search_paths.append(
(deploy_d_from_conf_file, True)
)
if deploy_d_from_syspaths not in deploy_scripts_search_paths:
deploy_scripts_search_paths.append(
(deploy_d_from_syspaths, True)
)
finished = []
finished_full = []
for entry, makedirs in deploy_scripts_search_paths:
# This handles duplicate entries, which are likely to appear
if entry in finished:
continue
else:
finished.append(entry)
if makedirs and not os.path.isdir(entry):
try:
os.makedirs(entry)
except (OSError, IOError) as err:
log.info('Failed to create directory \'%s\'', entry)
continue
if not is_writeable(entry):
log.debug('The \'%s\' is not writeable. Continuing...', entry)
continue
deploy_path = os.path.join(entry, script_name)
try:
finished_full.append(deploy_path)
with salt.utils.files.fopen(deploy_path, 'w') as fp_:
fp_.write(salt.utils.stringutils.to_str(script_content))
except (OSError, IOError) as err:
log.debug('Failed to write the updated script: %s', err)
continue
return {'Success': {'Files updated': finished_full}} | [
"def",
"update_bootstrap",
"(",
"config",
",",
"url",
"=",
"None",
")",
":",
"default_url",
"=",
"config",
".",
"get",
"(",
"'bootstrap_script_url'",
",",
"'https://bootstrap.saltstack.com'",
")",
"if",
"not",
"url",
":",
"url",
"=",
"default_url",
"if",
"not"... | Update the salt-bootstrap script
url can be one of:
- The URL to fetch the bootstrap script from
- The absolute path to the bootstrap
- The content of the bootstrap script | [
"Update",
"the",
"salt",
"-",
"bootstrap",
"script"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L2878-L3007 | train |
saltstack/salt | salt/utils/cloud.py | cache_node_list | def cache_node_list(nodes, provider, opts):
'''
If configured to do so, update the cloud cachedir with the current list of
nodes. Also fires configured events pertaining to the node list.
.. versionadded:: 2014.7.0
'''
if 'update_cachedir' not in opts or not opts['update_cachedir']:
return
base = os.path.join(init_cachedir(), 'active')
driver = next(six.iterkeys(opts['providers'][provider]))
prov_dir = os.path.join(base, driver, provider)
if not os.path.exists(prov_dir):
os.makedirs(prov_dir)
# Check to see if any nodes in the cache are not in the new list
missing_node_cache(prov_dir, nodes, provider, opts)
for node in nodes:
diff_node_cache(prov_dir, node, nodes[node], opts)
path = os.path.join(prov_dir, '{0}.p'.format(node))
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(nodes[node], fh_, encoding=MSGPACK_ENCODING) | python | def cache_node_list(nodes, provider, opts):
'''
If configured to do so, update the cloud cachedir with the current list of
nodes. Also fires configured events pertaining to the node list.
.. versionadded:: 2014.7.0
'''
if 'update_cachedir' not in opts or not opts['update_cachedir']:
return
base = os.path.join(init_cachedir(), 'active')
driver = next(six.iterkeys(opts['providers'][provider]))
prov_dir = os.path.join(base, driver, provider)
if not os.path.exists(prov_dir):
os.makedirs(prov_dir)
# Check to see if any nodes in the cache are not in the new list
missing_node_cache(prov_dir, nodes, provider, opts)
for node in nodes:
diff_node_cache(prov_dir, node, nodes[node], opts)
path = os.path.join(prov_dir, '{0}.p'.format(node))
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(nodes[node], fh_, encoding=MSGPACK_ENCODING) | [
"def",
"cache_node_list",
"(",
"nodes",
",",
"provider",
",",
"opts",
")",
":",
"if",
"'update_cachedir'",
"not",
"in",
"opts",
"or",
"not",
"opts",
"[",
"'update_cachedir'",
"]",
":",
"return",
"base",
"=",
"os",
".",
"path",
".",
"join",
"(",
"init_cac... | If configured to do so, update the cloud cachedir with the current list of
nodes. Also fires configured events pertaining to the node list.
.. versionadded:: 2014.7.0 | [
"If",
"configured",
"to",
"do",
"so",
"update",
"the",
"cloud",
"cachedir",
"with",
"the",
"current",
"list",
"of",
"nodes",
".",
"Also",
"fires",
"configured",
"events",
"pertaining",
"to",
"the",
"node",
"list",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3010-L3034 | train |
saltstack/salt | salt/utils/cloud.py | cache_node | def cache_node(node, provider, opts):
'''
Cache node individually
.. versionadded:: 2014.7.0
'''
if isinstance(opts, dict):
__opts__.update(opts)
if 'update_cachedir' not in __opts__ or not __opts__['update_cachedir']:
return
if not os.path.exists(os.path.join(__opts__['cachedir'], 'active')):
init_cachedir()
base = os.path.join(__opts__['cachedir'], 'active')
provider, driver = provider.split(':')
prov_dir = os.path.join(base, driver, provider)
if not os.path.exists(prov_dir):
os.makedirs(prov_dir)
path = os.path.join(prov_dir, '{0}.p'.format(node['name']))
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(node, fh_, encoding=MSGPACK_ENCODING) | python | def cache_node(node, provider, opts):
'''
Cache node individually
.. versionadded:: 2014.7.0
'''
if isinstance(opts, dict):
__opts__.update(opts)
if 'update_cachedir' not in __opts__ or not __opts__['update_cachedir']:
return
if not os.path.exists(os.path.join(__opts__['cachedir'], 'active')):
init_cachedir()
base = os.path.join(__opts__['cachedir'], 'active')
provider, driver = provider.split(':')
prov_dir = os.path.join(base, driver, provider)
if not os.path.exists(prov_dir):
os.makedirs(prov_dir)
path = os.path.join(prov_dir, '{0}.p'.format(node['name']))
mode = 'wb' if six.PY3 else 'w'
with salt.utils.files.fopen(path, mode) as fh_:
salt.utils.msgpack.dump(node, fh_, encoding=MSGPACK_ENCODING) | [
"def",
"cache_node",
"(",
"node",
",",
"provider",
",",
"opts",
")",
":",
"if",
"isinstance",
"(",
"opts",
",",
"dict",
")",
":",
"__opts__",
".",
"update",
"(",
"opts",
")",
"if",
"'update_cachedir'",
"not",
"in",
"__opts__",
"or",
"not",
"__opts__",
... | Cache node individually
.. versionadded:: 2014.7.0 | [
"Cache",
"node",
"individually"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3037-L3060 | train |
saltstack/salt | salt/utils/cloud.py | missing_node_cache | def missing_node_cache(prov_dir, node_list, provider, opts):
'''
Check list of nodes to see if any nodes which were previously known about
in the cache have been removed from the node list.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0
'''
cached_nodes = []
for node in os.listdir(prov_dir):
cached_nodes.append(os.path.splitext(node)[0])
for node in cached_nodes:
if node not in node_list:
delete_minion_cachedir(node, provider, opts)
if 'diff_cache_events' in opts and opts['diff_cache_events']:
fire_event(
'event',
'cached node missing from provider',
'salt/cloud/{0}/cache_node_missing'.format(node),
args={'missing node': node},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
) | python | def missing_node_cache(prov_dir, node_list, provider, opts):
'''
Check list of nodes to see if any nodes which were previously known about
in the cache have been removed from the node list.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0
'''
cached_nodes = []
for node in os.listdir(prov_dir):
cached_nodes.append(os.path.splitext(node)[0])
for node in cached_nodes:
if node not in node_list:
delete_minion_cachedir(node, provider, opts)
if 'diff_cache_events' in opts and opts['diff_cache_events']:
fire_event(
'event',
'cached node missing from provider',
'salt/cloud/{0}/cache_node_missing'.format(node),
args={'missing node': node},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
) | [
"def",
"missing_node_cache",
"(",
"prov_dir",
",",
"node_list",
",",
"provider",
",",
"opts",
")",
":",
"cached_nodes",
"=",
"[",
"]",
"for",
"node",
"in",
"os",
".",
"listdir",
"(",
"prov_dir",
")",
":",
"cached_nodes",
".",
"append",
"(",
"os",
".",
... | Check list of nodes to see if any nodes which were previously known about
in the cache have been removed from the node list.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0 | [
"Check",
"list",
"of",
"nodes",
"to",
"see",
"if",
"any",
"nodes",
"which",
"were",
"previously",
"known",
"about",
"in",
"the",
"cache",
"have",
"been",
"removed",
"from",
"the",
"node",
"list",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3063-L3094 | train |
saltstack/salt | salt/utils/cloud.py | diff_node_cache | def diff_node_cache(prov_dir, node, new_data, opts):
'''
Check new node data against current cache. If data differ, fire an event
which consists of the new node data.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0
'''
if 'diff_cache_events' not in opts or not opts['diff_cache_events']:
return
if node is None:
return
path = '{0}.p'.format(os.path.join(prov_dir, node))
if not os.path.exists(path):
event_data = _strip_cache_events(new_data, opts)
fire_event(
'event',
'new node found',
'salt/cloud/{0}/cache_node_new'.format(node),
args={'new_data': event_data},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
return
with salt.utils.files.fopen(path, 'r') as fh_:
try:
cache_data = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
except ValueError:
log.warning('Cache for %s was corrupt: Deleting', node)
cache_data = {}
# Perform a simple diff between the old and the new data, and if it differs,
# return both dicts.
# TODO: Return an actual diff
diff = salt.utils.compat.cmp(new_data, cache_data)
if diff != 0:
fire_event(
'event',
'node data differs',
'salt/cloud/{0}/cache_node_diff'.format(node),
args={
'new_data': _strip_cache_events(new_data, opts),
'cache_data': _strip_cache_events(cache_data, opts),
},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
) | python | def diff_node_cache(prov_dir, node, new_data, opts):
'''
Check new node data against current cache. If data differ, fire an event
which consists of the new node data.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0
'''
if 'diff_cache_events' not in opts or not opts['diff_cache_events']:
return
if node is None:
return
path = '{0}.p'.format(os.path.join(prov_dir, node))
if not os.path.exists(path):
event_data = _strip_cache_events(new_data, opts)
fire_event(
'event',
'new node found',
'salt/cloud/{0}/cache_node_new'.format(node),
args={'new_data': event_data},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
)
return
with salt.utils.files.fopen(path, 'r') as fh_:
try:
cache_data = salt.utils.data.decode(
salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
except ValueError:
log.warning('Cache for %s was corrupt: Deleting', node)
cache_data = {}
# Perform a simple diff between the old and the new data, and if it differs,
# return both dicts.
# TODO: Return an actual diff
diff = salt.utils.compat.cmp(new_data, cache_data)
if diff != 0:
fire_event(
'event',
'node data differs',
'salt/cloud/{0}/cache_node_diff'.format(node),
args={
'new_data': _strip_cache_events(new_data, opts),
'cache_data': _strip_cache_events(cache_data, opts),
},
sock_dir=opts.get(
'sock_dir',
os.path.join(__opts__['sock_dir'], 'master')),
transport=opts.get('transport', 'zeromq')
) | [
"def",
"diff_node_cache",
"(",
"prov_dir",
",",
"node",
",",
"new_data",
",",
"opts",
")",
":",
"if",
"'diff_cache_events'",
"not",
"in",
"opts",
"or",
"not",
"opts",
"[",
"'diff_cache_events'",
"]",
":",
"return",
"if",
"node",
"is",
"None",
":",
"return"... | Check new node data against current cache. If data differ, fire an event
which consists of the new node data.
This function will only run if configured to do so in the main Salt Cloud
configuration file (normally /etc/salt/cloud).
.. code-block:: yaml
diff_cache_events: True
.. versionadded:: 2014.7.0 | [
"Check",
"new",
"node",
"data",
"against",
"current",
"cache",
".",
"If",
"data",
"differ",
"fire",
"an",
"event",
"which",
"consists",
"of",
"the",
"new",
"node",
"data",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3097-L3158 | train |
saltstack/salt | salt/utils/cloud.py | _strip_cache_events | def _strip_cache_events(data, opts):
'''
Strip out user-configured sensitive event data. The fields to be stripped
are configured in the main Salt Cloud configuration file, usually
``/etc/salt/cloud``.
.. code-block: yaml
cache_event_strip_fields:
- password
- priv_key
.. versionadded:: 2014.7.0
'''
event_data = copy.deepcopy(data)
strip_fields = opts.get('cache_event_strip_fields', [])
for field in strip_fields:
if field in event_data:
del event_data[field]
return event_data | python | def _strip_cache_events(data, opts):
'''
Strip out user-configured sensitive event data. The fields to be stripped
are configured in the main Salt Cloud configuration file, usually
``/etc/salt/cloud``.
.. code-block: yaml
cache_event_strip_fields:
- password
- priv_key
.. versionadded:: 2014.7.0
'''
event_data = copy.deepcopy(data)
strip_fields = opts.get('cache_event_strip_fields', [])
for field in strip_fields:
if field in event_data:
del event_data[field]
return event_data | [
"def",
"_strip_cache_events",
"(",
"data",
",",
"opts",
")",
":",
"event_data",
"=",
"copy",
".",
"deepcopy",
"(",
"data",
")",
"strip_fields",
"=",
"opts",
".",
"get",
"(",
"'cache_event_strip_fields'",
",",
"[",
"]",
")",
"for",
"field",
"in",
"strip_fie... | Strip out user-configured sensitive event data. The fields to be stripped
are configured in the main Salt Cloud configuration file, usually
``/etc/salt/cloud``.
.. code-block: yaml
cache_event_strip_fields:
- password
- priv_key
.. versionadded:: 2014.7.0 | [
"Strip",
"out",
"user",
"-",
"configured",
"sensitive",
"event",
"data",
".",
"The",
"fields",
"to",
"be",
"stripped",
"are",
"configured",
"in",
"the",
"main",
"Salt",
"Cloud",
"configuration",
"file",
"usually",
"/",
"etc",
"/",
"salt",
"/",
"cloud",
"."... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3161-L3181 | train |
saltstack/salt | salt/utils/cloud.py | _salt_cloud_force_ascii | def _salt_cloud_force_ascii(exc):
'''
Helper method to try its best to convert any Unicode text into ASCII
without stack tracing since salt internally does not handle Unicode strings
This method is not supposed to be used directly. Once
`py:module: salt.utils.cloud` is imported this method register's with
python's codecs module for proper automatic conversion in case of encoding
errors.
'''
if not isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
raise TypeError('Can\'t handle {0}'.format(exc))
unicode_trans = {
# Convert non-breaking space to space
u'\xa0': u' ',
# Convert en dash to dash
u'\u2013': u'-',
}
if exc.object[exc.start:exc.end] in unicode_trans:
return unicode_trans[exc.object[exc.start:exc.end]], exc.end
# There's nothing else we can do, raise the exception
raise exc | python | def _salt_cloud_force_ascii(exc):
'''
Helper method to try its best to convert any Unicode text into ASCII
without stack tracing since salt internally does not handle Unicode strings
This method is not supposed to be used directly. Once
`py:module: salt.utils.cloud` is imported this method register's with
python's codecs module for proper automatic conversion in case of encoding
errors.
'''
if not isinstance(exc, (UnicodeEncodeError, UnicodeTranslateError)):
raise TypeError('Can\'t handle {0}'.format(exc))
unicode_trans = {
# Convert non-breaking space to space
u'\xa0': u' ',
# Convert en dash to dash
u'\u2013': u'-',
}
if exc.object[exc.start:exc.end] in unicode_trans:
return unicode_trans[exc.object[exc.start:exc.end]], exc.end
# There's nothing else we can do, raise the exception
raise exc | [
"def",
"_salt_cloud_force_ascii",
"(",
"exc",
")",
":",
"if",
"not",
"isinstance",
"(",
"exc",
",",
"(",
"UnicodeEncodeError",
",",
"UnicodeTranslateError",
")",
")",
":",
"raise",
"TypeError",
"(",
"'Can\\'t handle {0}'",
".",
"format",
"(",
"exc",
")",
")",
... | Helper method to try its best to convert any Unicode text into ASCII
without stack tracing since salt internally does not handle Unicode strings
This method is not supposed to be used directly. Once
`py:module: salt.utils.cloud` is imported this method register's with
python's codecs module for proper automatic conversion in case of encoding
errors. | [
"Helper",
"method",
"to",
"try",
"its",
"best",
"to",
"convert",
"any",
"Unicode",
"text",
"into",
"ASCII",
"without",
"stack",
"tracing",
"since",
"salt",
"internally",
"does",
"not",
"handle",
"Unicode",
"strings"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3184-L3208 | train |
saltstack/salt | salt/utils/cloud.py | retrieve_password_from_keyring | def retrieve_password_from_keyring(credential_id, username):
'''
Retrieve particular user's password for a specified credential set from system keyring.
'''
try:
import keyring # pylint: disable=import-error
return keyring.get_password(credential_id, username)
except ImportError:
log.error('USE_KEYRING configured as a password, but no keyring module is installed')
return False | python | def retrieve_password_from_keyring(credential_id, username):
'''
Retrieve particular user's password for a specified credential set from system keyring.
'''
try:
import keyring # pylint: disable=import-error
return keyring.get_password(credential_id, username)
except ImportError:
log.error('USE_KEYRING configured as a password, but no keyring module is installed')
return False | [
"def",
"retrieve_password_from_keyring",
"(",
"credential_id",
",",
"username",
")",
":",
"try",
":",
"import",
"keyring",
"# pylint: disable=import-error",
"return",
"keyring",
".",
"get_password",
"(",
"credential_id",
",",
"username",
")",
"except",
"ImportError",
... | Retrieve particular user's password for a specified credential set from system keyring. | [
"Retrieve",
"particular",
"user",
"s",
"password",
"for",
"a",
"specified",
"credential",
"set",
"from",
"system",
"keyring",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3214-L3223 | train |
saltstack/salt | salt/utils/cloud.py | _save_password_in_keyring | def _save_password_in_keyring(credential_id, username, password):
'''
Saves provider password in system keyring
'''
try:
import keyring # pylint: disable=import-error
return keyring.set_password(credential_id, username, password)
except ImportError:
log.error('Tried to store password in keyring, but no keyring module is installed')
return False | python | def _save_password_in_keyring(credential_id, username, password):
'''
Saves provider password in system keyring
'''
try:
import keyring # pylint: disable=import-error
return keyring.set_password(credential_id, username, password)
except ImportError:
log.error('Tried to store password in keyring, but no keyring module is installed')
return False | [
"def",
"_save_password_in_keyring",
"(",
"credential_id",
",",
"username",
",",
"password",
")",
":",
"try",
":",
"import",
"keyring",
"# pylint: disable=import-error",
"return",
"keyring",
".",
"set_password",
"(",
"credential_id",
",",
"username",
",",
"password",
... | Saves provider password in system keyring | [
"Saves",
"provider",
"password",
"in",
"system",
"keyring"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3226-L3235 | train |
saltstack/salt | salt/utils/cloud.py | store_password_in_keyring | def store_password_in_keyring(credential_id, username, password=None):
'''
Interactively prompts user for a password and stores it in system keyring
'''
try:
# pylint: disable=import-error
import keyring
import keyring.errors
# pylint: enable=import-error
if password is None:
prompt = 'Please enter password for {0}: '.format(credential_id)
try:
password = getpass.getpass(prompt)
except EOFError:
password = None
if not password:
# WE should raise something else here to be able to use this
# as/from an API
raise RuntimeError('Invalid password provided.')
try:
_save_password_in_keyring(credential_id, username, password)
except keyring.errors.PasswordSetError as exc:
log.debug('Problem saving password in the keyring: %s', exc)
except ImportError:
log.error('Tried to store password in keyring, but no keyring module is installed')
return False | python | def store_password_in_keyring(credential_id, username, password=None):
'''
Interactively prompts user for a password and stores it in system keyring
'''
try:
# pylint: disable=import-error
import keyring
import keyring.errors
# pylint: enable=import-error
if password is None:
prompt = 'Please enter password for {0}: '.format(credential_id)
try:
password = getpass.getpass(prompt)
except EOFError:
password = None
if not password:
# WE should raise something else here to be able to use this
# as/from an API
raise RuntimeError('Invalid password provided.')
try:
_save_password_in_keyring(credential_id, username, password)
except keyring.errors.PasswordSetError as exc:
log.debug('Problem saving password in the keyring: %s', exc)
except ImportError:
log.error('Tried to store password in keyring, but no keyring module is installed')
return False | [
"def",
"store_password_in_keyring",
"(",
"credential_id",
",",
"username",
",",
"password",
"=",
"None",
")",
":",
"try",
":",
"# pylint: disable=import-error",
"import",
"keyring",
"import",
"keyring",
".",
"errors",
"# pylint: enable=import-error",
"if",
"password",
... | Interactively prompts user for a password and stores it in system keyring | [
"Interactively",
"prompts",
"user",
"for",
"a",
"password",
"and",
"stores",
"it",
"in",
"system",
"keyring"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3238-L3265 | train |
saltstack/salt | salt/utils/cloud.py | _unwrap_dict | def _unwrap_dict(dictionary, index_string):
'''
Accepts index in form of a string
Returns final value
Example: dictionary = {'a': {'b': {'c': 'foobar'}}}
index_string = 'a,b,c'
returns 'foobar'
'''
index = index_string.split(',')
for k in index:
dictionary = dictionary[k]
return dictionary | python | def _unwrap_dict(dictionary, index_string):
'''
Accepts index in form of a string
Returns final value
Example: dictionary = {'a': {'b': {'c': 'foobar'}}}
index_string = 'a,b,c'
returns 'foobar'
'''
index = index_string.split(',')
for k in index:
dictionary = dictionary[k]
return dictionary | [
"def",
"_unwrap_dict",
"(",
"dictionary",
",",
"index_string",
")",
":",
"index",
"=",
"index_string",
".",
"split",
"(",
"','",
")",
"for",
"k",
"in",
"index",
":",
"dictionary",
"=",
"dictionary",
"[",
"k",
"]",
"return",
"dictionary"
] | Accepts index in form of a string
Returns final value
Example: dictionary = {'a': {'b': {'c': 'foobar'}}}
index_string = 'a,b,c'
returns 'foobar' | [
"Accepts",
"index",
"in",
"form",
"of",
"a",
"string",
"Returns",
"final",
"value",
"Example",
":",
"dictionary",
"=",
"{",
"a",
":",
"{",
"b",
":",
"{",
"c",
":",
"foobar",
"}}}",
"index_string",
"=",
"a",
"b",
"c",
"returns",
"foobar"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3268-L3279 | train |
saltstack/salt | salt/utils/cloud.py | run_func_until_ret_arg | def run_func_until_ret_arg(fun, kwargs, fun_call=None,
argument_being_watched=None, required_argument_response=None):
'''
Waits until the function retrieves some required argument.
NOTE: Tested with ec2 describe_volumes and describe_snapshots only.
'''
status = None
while status != required_argument_response:
f_result = fun(kwargs, call=fun_call)
r_set = {}
for d in f_result:
if isinstance(d, list):
d0 = d[0]
if isinstance(d0, dict):
for k, v in six.iteritems(d0):
r_set[k] = v
status = _unwrap_dict(r_set, argument_being_watched)
log.debug(
'Function: %s, Watched arg: %s, Response: %s',
six.text_type(fun).split(' ')[1], argument_being_watched, status
)
time.sleep(5)
return True | python | def run_func_until_ret_arg(fun, kwargs, fun_call=None,
argument_being_watched=None, required_argument_response=None):
'''
Waits until the function retrieves some required argument.
NOTE: Tested with ec2 describe_volumes and describe_snapshots only.
'''
status = None
while status != required_argument_response:
f_result = fun(kwargs, call=fun_call)
r_set = {}
for d in f_result:
if isinstance(d, list):
d0 = d[0]
if isinstance(d0, dict):
for k, v in six.iteritems(d0):
r_set[k] = v
status = _unwrap_dict(r_set, argument_being_watched)
log.debug(
'Function: %s, Watched arg: %s, Response: %s',
six.text_type(fun).split(' ')[1], argument_being_watched, status
)
time.sleep(5)
return True | [
"def",
"run_func_until_ret_arg",
"(",
"fun",
",",
"kwargs",
",",
"fun_call",
"=",
"None",
",",
"argument_being_watched",
"=",
"None",
",",
"required_argument_response",
"=",
"None",
")",
":",
"status",
"=",
"None",
"while",
"status",
"!=",
"required_argument_respo... | Waits until the function retrieves some required argument.
NOTE: Tested with ec2 describe_volumes and describe_snapshots only. | [
"Waits",
"until",
"the",
"function",
"retrieves",
"some",
"required",
"argument",
".",
"NOTE",
":",
"Tested",
"with",
"ec2",
"describe_volumes",
"and",
"describe_snapshots",
"only",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3282-L3305 | train |
saltstack/salt | salt/utils/cloud.py | get_salt_interface | def get_salt_interface(vm_, opts):
'''
Return the salt_interface type to connect to. Either 'public_ips' (default)
or 'private_ips'.
'''
salt_host = salt.config.get_cloud_config_value(
'salt_interface', vm_, opts, default=False,
search_global=False
)
if salt_host is False:
salt_host = salt.config.get_cloud_config_value(
'ssh_interface', vm_, opts, default='public_ips',
search_global=False
)
return salt_host | python | def get_salt_interface(vm_, opts):
'''
Return the salt_interface type to connect to. Either 'public_ips' (default)
or 'private_ips'.
'''
salt_host = salt.config.get_cloud_config_value(
'salt_interface', vm_, opts, default=False,
search_global=False
)
if salt_host is False:
salt_host = salt.config.get_cloud_config_value(
'ssh_interface', vm_, opts, default='public_ips',
search_global=False
)
return salt_host | [
"def",
"get_salt_interface",
"(",
"vm_",
",",
"opts",
")",
":",
"salt_host",
"=",
"salt",
".",
"config",
".",
"get_cloud_config_value",
"(",
"'salt_interface'",
",",
"vm_",
",",
"opts",
",",
"default",
"=",
"False",
",",
"search_global",
"=",
"False",
")",
... | Return the salt_interface type to connect to. Either 'public_ips' (default)
or 'private_ips'. | [
"Return",
"the",
"salt_interface",
"type",
"to",
"connect",
"to",
".",
"Either",
"public_ips",
"(",
"default",
")",
"or",
"private_ips",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3308-L3324 | train |
saltstack/salt | salt/utils/cloud.py | check_key_path_and_mode | def check_key_path_and_mode(provider, key_path):
'''
Checks that the key_path exists and the key_mode is either 0400 or 0600.
Returns True or False.
.. versionadded:: 2016.3.0
provider
The provider name that the key_path to check belongs to.
key_path
The key_path to ensure that it exists and to check adequate permissions
against.
'''
if not os.path.exists(key_path):
log.error(
'The key file \'%s\' used in the \'%s\' provider configuration '
'does not exist.\n', key_path, provider
)
return False
key_mode = stat.S_IMODE(os.stat(key_path).st_mode)
if key_mode not in (0o400, 0o600):
log.error(
'The key file \'%s\' used in the \'%s\' provider configuration '
'needs to be set to mode 0400 or 0600.\n', key_path, provider
)
return False
return True | python | def check_key_path_and_mode(provider, key_path):
'''
Checks that the key_path exists and the key_mode is either 0400 or 0600.
Returns True or False.
.. versionadded:: 2016.3.0
provider
The provider name that the key_path to check belongs to.
key_path
The key_path to ensure that it exists and to check adequate permissions
against.
'''
if not os.path.exists(key_path):
log.error(
'The key file \'%s\' used in the \'%s\' provider configuration '
'does not exist.\n', key_path, provider
)
return False
key_mode = stat.S_IMODE(os.stat(key_path).st_mode)
if key_mode not in (0o400, 0o600):
log.error(
'The key file \'%s\' used in the \'%s\' provider configuration '
'needs to be set to mode 0400 or 0600.\n', key_path, provider
)
return False
return True | [
"def",
"check_key_path_and_mode",
"(",
"provider",
",",
"key_path",
")",
":",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"key_path",
")",
":",
"log",
".",
"error",
"(",
"'The key file \\'%s\\' used in the \\'%s\\' provider configuration '",
"'does not exist.\... | Checks that the key_path exists and the key_mode is either 0400 or 0600.
Returns True or False.
.. versionadded:: 2016.3.0
provider
The provider name that the key_path to check belongs to.
key_path
The key_path to ensure that it exists and to check adequate permissions
against. | [
"Checks",
"that",
"the",
"key_path",
"exists",
"and",
"the",
"key_mode",
"is",
"either",
"0400",
"or",
"0600",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3327-L3357 | train |
saltstack/salt | salt/utils/cloud.py | userdata_template | def userdata_template(opts, vm_, userdata):
'''
Use the configured templating engine to template the userdata file
'''
# No userdata, no need to template anything
if userdata is None:
return userdata
userdata_template = salt.config.get_cloud_config_value(
'userdata_template', vm_, opts, search_global=False, default=None
)
if userdata_template is False:
return userdata
# Use the cloud profile's userdata_template, otherwise get it from the
# master configuration file.
renderer = opts.get('userdata_template') \
if userdata_template is None \
else userdata_template
if renderer is None:
return userdata
else:
render_opts = opts.copy()
render_opts.update(vm_)
rend = salt.loader.render(render_opts, {})
blacklist = opts['renderer_blacklist']
whitelist = opts['renderer_whitelist']
templated = salt.template.compile_template(
':string:',
rend,
renderer,
blacklist,
whitelist,
input_data=userdata,
)
if not isinstance(templated, six.string_types):
# template renderers like "jinja" should return a StringIO
try:
templated = ''.join(templated.readlines())
except AttributeError:
log.warning(
'Templated userdata resulted in non-string result (%s), '
'converting to string', templated
)
templated = six.text_type(templated)
return templated | python | def userdata_template(opts, vm_, userdata):
'''
Use the configured templating engine to template the userdata file
'''
# No userdata, no need to template anything
if userdata is None:
return userdata
userdata_template = salt.config.get_cloud_config_value(
'userdata_template', vm_, opts, search_global=False, default=None
)
if userdata_template is False:
return userdata
# Use the cloud profile's userdata_template, otherwise get it from the
# master configuration file.
renderer = opts.get('userdata_template') \
if userdata_template is None \
else userdata_template
if renderer is None:
return userdata
else:
render_opts = opts.copy()
render_opts.update(vm_)
rend = salt.loader.render(render_opts, {})
blacklist = opts['renderer_blacklist']
whitelist = opts['renderer_whitelist']
templated = salt.template.compile_template(
':string:',
rend,
renderer,
blacklist,
whitelist,
input_data=userdata,
)
if not isinstance(templated, six.string_types):
# template renderers like "jinja" should return a StringIO
try:
templated = ''.join(templated.readlines())
except AttributeError:
log.warning(
'Templated userdata resulted in non-string result (%s), '
'converting to string', templated
)
templated = six.text_type(templated)
return templated | [
"def",
"userdata_template",
"(",
"opts",
",",
"vm_",
",",
"userdata",
")",
":",
"# No userdata, no need to template anything",
"if",
"userdata",
"is",
"None",
":",
"return",
"userdata",
"userdata_template",
"=",
"salt",
".",
"config",
".",
"get_cloud_config_value",
... | Use the configured templating engine to template the userdata file | [
"Use",
"the",
"configured",
"templating",
"engine",
"to",
"template",
"the",
"userdata",
"file"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L3360-L3405 | train |
saltstack/salt | salt/utils/cloud.py | Client.remove_service | def remove_service(self, wait_timeout=10, sleep_wait=1):
'''
Removes the PAExec service and executable that was created as part of
the create_service function. This does not remove any older executables
or services from previous runs, use cleanup() instead for that purpose.
'''
# Stops/remove the PAExec service and removes the executable
log.debug("Deleting PAExec service at the end of the process")
wait_start = time.time()
while True:
try:
self._client._service.delete()
except SCMRException as exc:
log.debug("Exception encountered while deleting service %s", repr(exc))
if time.time() - wait_start > wait_timeout:
raise exc
time.sleep(sleep_wait)
continue
break
# delete the PAExec executable
smb_tree = TreeConnect(
self._client.session,
r"\\{0}\ADMIN$".format(self._client.connection.server_name)
)
log.info("Connecting to SMB Tree %s", smb_tree.share_name)
smb_tree.connect()
wait_start = time.time()
while True:
try:
log.info("Creating open to PAExec file with delete on close flags")
self._client._delete_file(smb_tree, self._exe_file)
except SMBResponseException as exc:
log.debug("Exception deleting file %s %s", self._exe_file, repr(exc))
if time.time() - wait_start > wait_timeout:
raise exc
time.sleep(sleep_wait)
continue
break
log.info("Disconnecting from SMB Tree %s", smb_tree.share_name)
smb_tree.disconnect() | python | def remove_service(self, wait_timeout=10, sleep_wait=1):
'''
Removes the PAExec service and executable that was created as part of
the create_service function. This does not remove any older executables
or services from previous runs, use cleanup() instead for that purpose.
'''
# Stops/remove the PAExec service and removes the executable
log.debug("Deleting PAExec service at the end of the process")
wait_start = time.time()
while True:
try:
self._client._service.delete()
except SCMRException as exc:
log.debug("Exception encountered while deleting service %s", repr(exc))
if time.time() - wait_start > wait_timeout:
raise exc
time.sleep(sleep_wait)
continue
break
# delete the PAExec executable
smb_tree = TreeConnect(
self._client.session,
r"\\{0}\ADMIN$".format(self._client.connection.server_name)
)
log.info("Connecting to SMB Tree %s", smb_tree.share_name)
smb_tree.connect()
wait_start = time.time()
while True:
try:
log.info("Creating open to PAExec file with delete on close flags")
self._client._delete_file(smb_tree, self._exe_file)
except SMBResponseException as exc:
log.debug("Exception deleting file %s %s", self._exe_file, repr(exc))
if time.time() - wait_start > wait_timeout:
raise exc
time.sleep(sleep_wait)
continue
break
log.info("Disconnecting from SMB Tree %s", smb_tree.share_name)
smb_tree.disconnect() | [
"def",
"remove_service",
"(",
"self",
",",
"wait_timeout",
"=",
"10",
",",
"sleep_wait",
"=",
"1",
")",
":",
"# Stops/remove the PAExec service and removes the executable",
"log",
".",
"debug",
"(",
"\"Deleting PAExec service at the end of the process\"",
")",
"wait_start",... | Removes the PAExec service and executable that was created as part of
the create_service function. This does not remove any older executables
or services from previous runs, use cleanup() instead for that purpose. | [
"Removes",
"the",
"PAExec",
"service",
"and",
"executable",
"that",
"was",
"created",
"as",
"part",
"of",
"the",
"create_service",
"function",
".",
"This",
"does",
"not",
"remove",
"any",
"older",
"executables",
"or",
"services",
"from",
"previous",
"runs",
"u... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/cloud.py#L893-L935 | train |
saltstack/salt | salt/crypt.py | dropfile | def dropfile(cachedir, user=None):
'''
Set an AES dropfile to request the master update the publish session key
'''
dfn = os.path.join(cachedir, '.dfn')
# set a mask (to avoid a race condition on file creation) and store original.
with salt.utils.files.set_umask(0o277):
log.info('Rotating AES key')
if os.path.isfile(dfn):
log.info('AES key rotation already requested')
return
if os.path.isfile(dfn) and not os.access(dfn, os.W_OK):
os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)
with salt.utils.files.fopen(dfn, 'wb+') as fp_:
fp_.write(b'')
os.chmod(dfn, stat.S_IRUSR)
if user:
try:
import pwd
uid = pwd.getpwnam(user).pw_uid
os.chown(dfn, uid, -1)
except (KeyError, ImportError, OSError, IOError):
pass | python | def dropfile(cachedir, user=None):
'''
Set an AES dropfile to request the master update the publish session key
'''
dfn = os.path.join(cachedir, '.dfn')
# set a mask (to avoid a race condition on file creation) and store original.
with salt.utils.files.set_umask(0o277):
log.info('Rotating AES key')
if os.path.isfile(dfn):
log.info('AES key rotation already requested')
return
if os.path.isfile(dfn) and not os.access(dfn, os.W_OK):
os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)
with salt.utils.files.fopen(dfn, 'wb+') as fp_:
fp_.write(b'')
os.chmod(dfn, stat.S_IRUSR)
if user:
try:
import pwd
uid = pwd.getpwnam(user).pw_uid
os.chown(dfn, uid, -1)
except (KeyError, ImportError, OSError, IOError):
pass | [
"def",
"dropfile",
"(",
"cachedir",
",",
"user",
"=",
"None",
")",
":",
"dfn",
"=",
"os",
".",
"path",
".",
"join",
"(",
"cachedir",
",",
"'.dfn'",
")",
"# set a mask (to avoid a race condition on file creation) and store original.",
"with",
"salt",
".",
"utils",
... | Set an AES dropfile to request the master update the publish session key | [
"Set",
"an",
"AES",
"dropfile",
"to",
"request",
"the",
"master",
"update",
"the",
"publish",
"session",
"key"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L82-L105 | train |
saltstack/salt | salt/crypt.py | gen_keys | def gen_keys(keydir, keyname, keysize, user=None, passphrase=None):
'''
Generate a RSA public keypair for use with salt
:param str keydir: The directory to write the keypair to
:param str keyname: The type of salt server for whom this key should be written. (i.e. 'master' or 'minion')
:param int keysize: The number of bits in the key
:param str user: The user on the system who should own this keypair
:param str passphrase: The passphrase which should be used to encrypt the private key
:rtype: str
:return: Path on the filesystem to the RSA private key
'''
base = os.path.join(keydir, keyname)
priv = '{0}.pem'.format(base)
pub = '{0}.pub'.format(base)
if HAS_M2:
gen = RSA.gen_key(keysize, 65537, lambda: None)
else:
salt.utils.crypt.reinit_crypto()
gen = RSA.generate(bits=keysize, e=65537)
if os.path.isfile(priv):
# Between first checking and the generation another process has made
# a key! Use the winner's key
return priv
# Do not try writing anything, if directory has no permissions.
if not os.access(keydir, os.W_OK):
raise IOError('Write access denied to "{0}" for user "{1}".'.format(os.path.abspath(keydir), getpass.getuser()))
with salt.utils.files.set_umask(0o277):
if HAS_M2:
# if passphrase is empty or None use no cipher
if not passphrase:
gen.save_pem(priv, cipher=None)
else:
gen.save_pem(
priv,
cipher='des_ede3_cbc',
callback=lambda x: salt.utils.stringutils.to_bytes(passphrase))
else:
with salt.utils.files.fopen(priv, 'wb+') as f:
f.write(gen.exportKey('PEM', passphrase))
if HAS_M2:
gen.save_pub_key(pub)
else:
with salt.utils.files.fopen(pub, 'wb+') as f:
f.write(gen.publickey().exportKey('PEM'))
os.chmod(priv, 0o400)
if user:
try:
import pwd
uid = pwd.getpwnam(user).pw_uid
os.chown(priv, uid, -1)
os.chown(pub, uid, -1)
except (KeyError, ImportError, OSError):
# The specified user was not found, allow the backup systems to
# report the error
pass
return priv | python | def gen_keys(keydir, keyname, keysize, user=None, passphrase=None):
'''
Generate a RSA public keypair for use with salt
:param str keydir: The directory to write the keypair to
:param str keyname: The type of salt server for whom this key should be written. (i.e. 'master' or 'minion')
:param int keysize: The number of bits in the key
:param str user: The user on the system who should own this keypair
:param str passphrase: The passphrase which should be used to encrypt the private key
:rtype: str
:return: Path on the filesystem to the RSA private key
'''
base = os.path.join(keydir, keyname)
priv = '{0}.pem'.format(base)
pub = '{0}.pub'.format(base)
if HAS_M2:
gen = RSA.gen_key(keysize, 65537, lambda: None)
else:
salt.utils.crypt.reinit_crypto()
gen = RSA.generate(bits=keysize, e=65537)
if os.path.isfile(priv):
# Between first checking and the generation another process has made
# a key! Use the winner's key
return priv
# Do not try writing anything, if directory has no permissions.
if not os.access(keydir, os.W_OK):
raise IOError('Write access denied to "{0}" for user "{1}".'.format(os.path.abspath(keydir), getpass.getuser()))
with salt.utils.files.set_umask(0o277):
if HAS_M2:
# if passphrase is empty or None use no cipher
if not passphrase:
gen.save_pem(priv, cipher=None)
else:
gen.save_pem(
priv,
cipher='des_ede3_cbc',
callback=lambda x: salt.utils.stringutils.to_bytes(passphrase))
else:
with salt.utils.files.fopen(priv, 'wb+') as f:
f.write(gen.exportKey('PEM', passphrase))
if HAS_M2:
gen.save_pub_key(pub)
else:
with salt.utils.files.fopen(pub, 'wb+') as f:
f.write(gen.publickey().exportKey('PEM'))
os.chmod(priv, 0o400)
if user:
try:
import pwd
uid = pwd.getpwnam(user).pw_uid
os.chown(priv, uid, -1)
os.chown(pub, uid, -1)
except (KeyError, ImportError, OSError):
# The specified user was not found, allow the backup systems to
# report the error
pass
return priv | [
"def",
"gen_keys",
"(",
"keydir",
",",
"keyname",
",",
"keysize",
",",
"user",
"=",
"None",
",",
"passphrase",
"=",
"None",
")",
":",
"base",
"=",
"os",
".",
"path",
".",
"join",
"(",
"keydir",
",",
"keyname",
")",
"priv",
"=",
"'{0}.pem'",
".",
"f... | Generate a RSA public keypair for use with salt
:param str keydir: The directory to write the keypair to
:param str keyname: The type of salt server for whom this key should be written. (i.e. 'master' or 'minion')
:param int keysize: The number of bits in the key
:param str user: The user on the system who should own this keypair
:param str passphrase: The passphrase which should be used to encrypt the private key
:rtype: str
:return: Path on the filesystem to the RSA private key | [
"Generate",
"a",
"RSA",
"public",
"keypair",
"for",
"use",
"with",
"salt"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L108-L168 | train |
saltstack/salt | salt/crypt.py | _get_key_with_evict | def _get_key_with_evict(path, timestamp, passphrase):
'''
Load a private key from disk. `timestamp` above is intended to be the
timestamp of the file's last modification. This fn is memoized so if it is
called with the same path and timestamp (the file's last modified time) the
second time the result is returned from the memoiziation. If the file gets
modified then the params are different and the key is loaded from disk.
'''
log.debug('salt.crypt._get_key_with_evict: Loading private key')
if HAS_M2:
key = RSA.load_key(path, lambda x: six.b(passphrase))
else:
with salt.utils.files.fopen(path) as f:
key = RSA.importKey(f.read(), passphrase)
return key | python | def _get_key_with_evict(path, timestamp, passphrase):
'''
Load a private key from disk. `timestamp` above is intended to be the
timestamp of the file's last modification. This fn is memoized so if it is
called with the same path and timestamp (the file's last modified time) the
second time the result is returned from the memoiziation. If the file gets
modified then the params are different and the key is loaded from disk.
'''
log.debug('salt.crypt._get_key_with_evict: Loading private key')
if HAS_M2:
key = RSA.load_key(path, lambda x: six.b(passphrase))
else:
with salt.utils.files.fopen(path) as f:
key = RSA.importKey(f.read(), passphrase)
return key | [
"def",
"_get_key_with_evict",
"(",
"path",
",",
"timestamp",
",",
"passphrase",
")",
":",
"log",
".",
"debug",
"(",
"'salt.crypt._get_key_with_evict: Loading private key'",
")",
"if",
"HAS_M2",
":",
"key",
"=",
"RSA",
".",
"load_key",
"(",
"path",
",",
"lambda",... | Load a private key from disk. `timestamp` above is intended to be the
timestamp of the file's last modification. This fn is memoized so if it is
called with the same path and timestamp (the file's last modified time) the
second time the result is returned from the memoiziation. If the file gets
modified then the params are different and the key is loaded from disk. | [
"Load",
"a",
"private",
"key",
"from",
"disk",
".",
"timestamp",
"above",
"is",
"intended",
"to",
"be",
"the",
"timestamp",
"of",
"the",
"file",
"s",
"last",
"modification",
".",
"This",
"fn",
"is",
"memoized",
"so",
"if",
"it",
"is",
"called",
"with",
... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L172-L186 | train |
saltstack/salt | salt/crypt.py | get_rsa_key | def get_rsa_key(path, passphrase):
'''
Read a private key off the disk. Poor man's simple cache in effect here,
we memoize the result of calling _get_rsa_with_evict. This means the first
time _get_key_with_evict is called with a path and a timestamp the result
is cached. If the file (the private key) does not change then its
timestamp will not change and the next time the result is returned from the
cache. If the key DOES change the next time _get_rsa_with_evict is called
it is called with different parameters and the fn is run fully to retrieve
the key from disk.
'''
log.debug('salt.crypt.get_rsa_key: Loading private key')
return _get_key_with_evict(path, six.text_type(os.path.getmtime(path)), passphrase) | python | def get_rsa_key(path, passphrase):
'''
Read a private key off the disk. Poor man's simple cache in effect here,
we memoize the result of calling _get_rsa_with_evict. This means the first
time _get_key_with_evict is called with a path and a timestamp the result
is cached. If the file (the private key) does not change then its
timestamp will not change and the next time the result is returned from the
cache. If the key DOES change the next time _get_rsa_with_evict is called
it is called with different parameters and the fn is run fully to retrieve
the key from disk.
'''
log.debug('salt.crypt.get_rsa_key: Loading private key')
return _get_key_with_evict(path, six.text_type(os.path.getmtime(path)), passphrase) | [
"def",
"get_rsa_key",
"(",
"path",
",",
"passphrase",
")",
":",
"log",
".",
"debug",
"(",
"'salt.crypt.get_rsa_key: Loading private key'",
")",
"return",
"_get_key_with_evict",
"(",
"path",
",",
"six",
".",
"text_type",
"(",
"os",
".",
"path",
".",
"getmtime",
... | Read a private key off the disk. Poor man's simple cache in effect here,
we memoize the result of calling _get_rsa_with_evict. This means the first
time _get_key_with_evict is called with a path and a timestamp the result
is cached. If the file (the private key) does not change then its
timestamp will not change and the next time the result is returned from the
cache. If the key DOES change the next time _get_rsa_with_evict is called
it is called with different parameters and the fn is run fully to retrieve
the key from disk. | [
"Read",
"a",
"private",
"key",
"off",
"the",
"disk",
".",
"Poor",
"man",
"s",
"simple",
"cache",
"in",
"effect",
"here",
"we",
"memoize",
"the",
"result",
"of",
"calling",
"_get_rsa_with_evict",
".",
"This",
"means",
"the",
"first",
"time",
"_get_key_with_ev... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L189-L201 | train |
saltstack/salt | salt/crypt.py | get_rsa_pub_key | def get_rsa_pub_key(path):
'''
Read a public key off the disk.
'''
log.debug('salt.crypt.get_rsa_pub_key: Loading public key')
if HAS_M2:
with salt.utils.files.fopen(path, 'rb') as f:
data = f.read().replace(b'RSA ', b'')
bio = BIO.MemoryBuffer(data)
key = RSA.load_pub_key_bio(bio)
else:
with salt.utils.files.fopen(path) as f:
key = RSA.importKey(f.read())
return key | python | def get_rsa_pub_key(path):
'''
Read a public key off the disk.
'''
log.debug('salt.crypt.get_rsa_pub_key: Loading public key')
if HAS_M2:
with salt.utils.files.fopen(path, 'rb') as f:
data = f.read().replace(b'RSA ', b'')
bio = BIO.MemoryBuffer(data)
key = RSA.load_pub_key_bio(bio)
else:
with salt.utils.files.fopen(path) as f:
key = RSA.importKey(f.read())
return key | [
"def",
"get_rsa_pub_key",
"(",
"path",
")",
":",
"log",
".",
"debug",
"(",
"'salt.crypt.get_rsa_pub_key: Loading public key'",
")",
"if",
"HAS_M2",
":",
"with",
"salt",
".",
"utils",
".",
"files",
".",
"fopen",
"(",
"path",
",",
"'rb'",
")",
"as",
"f",
":"... | Read a public key off the disk. | [
"Read",
"a",
"public",
"key",
"off",
"the",
"disk",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L204-L217 | train |
saltstack/salt | salt/crypt.py | sign_message | def sign_message(privkey_path, message, passphrase=None):
'''
Use Crypto.Signature.PKCS1_v1_5 to sign a message. Returns the signature.
'''
key = get_rsa_key(privkey_path, passphrase)
log.debug('salt.crypt.sign_message: Signing message.')
if HAS_M2:
md = EVP.MessageDigest('sha1')
md.update(salt.utils.stringutils.to_bytes(message))
digest = md.final()
return key.sign(digest)
else:
signer = PKCS1_v1_5.new(key)
return signer.sign(SHA.new(salt.utils.stringutils.to_bytes(message))) | python | def sign_message(privkey_path, message, passphrase=None):
'''
Use Crypto.Signature.PKCS1_v1_5 to sign a message. Returns the signature.
'''
key = get_rsa_key(privkey_path, passphrase)
log.debug('salt.crypt.sign_message: Signing message.')
if HAS_M2:
md = EVP.MessageDigest('sha1')
md.update(salt.utils.stringutils.to_bytes(message))
digest = md.final()
return key.sign(digest)
else:
signer = PKCS1_v1_5.new(key)
return signer.sign(SHA.new(salt.utils.stringutils.to_bytes(message))) | [
"def",
"sign_message",
"(",
"privkey_path",
",",
"message",
",",
"passphrase",
"=",
"None",
")",
":",
"key",
"=",
"get_rsa_key",
"(",
"privkey_path",
",",
"passphrase",
")",
"log",
".",
"debug",
"(",
"'salt.crypt.sign_message: Signing message.'",
")",
"if",
"HAS... | Use Crypto.Signature.PKCS1_v1_5 to sign a message. Returns the signature. | [
"Use",
"Crypto",
".",
"Signature",
".",
"PKCS1_v1_5",
"to",
"sign",
"a",
"message",
".",
"Returns",
"the",
"signature",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L220-L233 | train |
saltstack/salt | salt/crypt.py | verify_signature | def verify_signature(pubkey_path, message, signature):
'''
Use Crypto.Signature.PKCS1_v1_5 to verify the signature on a message.
Returns True for valid signature.
'''
log.debug('salt.crypt.verify_signature: Loading public key')
pubkey = get_rsa_pub_key(pubkey_path)
log.debug('salt.crypt.verify_signature: Verifying signature')
if HAS_M2:
md = EVP.MessageDigest('sha1')
md.update(salt.utils.stringutils.to_bytes(message))
digest = md.final()
return pubkey.verify(digest, signature)
else:
verifier = PKCS1_v1_5.new(pubkey)
return verifier.verify(SHA.new(salt.utils.stringutils.to_bytes(message)), signature) | python | def verify_signature(pubkey_path, message, signature):
'''
Use Crypto.Signature.PKCS1_v1_5 to verify the signature on a message.
Returns True for valid signature.
'''
log.debug('salt.crypt.verify_signature: Loading public key')
pubkey = get_rsa_pub_key(pubkey_path)
log.debug('salt.crypt.verify_signature: Verifying signature')
if HAS_M2:
md = EVP.MessageDigest('sha1')
md.update(salt.utils.stringutils.to_bytes(message))
digest = md.final()
return pubkey.verify(digest, signature)
else:
verifier = PKCS1_v1_5.new(pubkey)
return verifier.verify(SHA.new(salt.utils.stringutils.to_bytes(message)), signature) | [
"def",
"verify_signature",
"(",
"pubkey_path",
",",
"message",
",",
"signature",
")",
":",
"log",
".",
"debug",
"(",
"'salt.crypt.verify_signature: Loading public key'",
")",
"pubkey",
"=",
"get_rsa_pub_key",
"(",
"pubkey_path",
")",
"log",
".",
"debug",
"(",
"'sa... | Use Crypto.Signature.PKCS1_v1_5 to verify the signature on a message.
Returns True for valid signature. | [
"Use",
"Crypto",
".",
"Signature",
".",
"PKCS1_v1_5",
"to",
"verify",
"the",
"signature",
"on",
"a",
"message",
".",
"Returns",
"True",
"for",
"valid",
"signature",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L236-L251 | train |
saltstack/salt | salt/crypt.py | gen_signature | def gen_signature(priv_path, pub_path, sign_path, passphrase=None):
'''
creates a signature for the given public-key with
the given private key and writes it to sign_path
'''
with salt.utils.files.fopen(pub_path) as fp_:
mpub_64 = fp_.read()
mpub_sig = sign_message(priv_path, mpub_64, passphrase)
mpub_sig_64 = binascii.b2a_base64(mpub_sig)
if os.path.isfile(sign_path):
return False
log.trace(
'Calculating signature for %s with %s',
os.path.basename(pub_path), os.path.basename(priv_path)
)
if os.path.isfile(sign_path):
log.trace(
'Signature file %s already exists, please remove it first and '
'try again', sign_path
)
else:
with salt.utils.files.fopen(sign_path, 'wb+') as sig_f:
sig_f.write(salt.utils.stringutils.to_bytes(mpub_sig_64))
log.trace('Wrote signature to %s', sign_path)
return True | python | def gen_signature(priv_path, pub_path, sign_path, passphrase=None):
'''
creates a signature for the given public-key with
the given private key and writes it to sign_path
'''
with salt.utils.files.fopen(pub_path) as fp_:
mpub_64 = fp_.read()
mpub_sig = sign_message(priv_path, mpub_64, passphrase)
mpub_sig_64 = binascii.b2a_base64(mpub_sig)
if os.path.isfile(sign_path):
return False
log.trace(
'Calculating signature for %s with %s',
os.path.basename(pub_path), os.path.basename(priv_path)
)
if os.path.isfile(sign_path):
log.trace(
'Signature file %s already exists, please remove it first and '
'try again', sign_path
)
else:
with salt.utils.files.fopen(sign_path, 'wb+') as sig_f:
sig_f.write(salt.utils.stringutils.to_bytes(mpub_sig_64))
log.trace('Wrote signature to %s', sign_path)
return True | [
"def",
"gen_signature",
"(",
"priv_path",
",",
"pub_path",
",",
"sign_path",
",",
"passphrase",
"=",
"None",
")",
":",
"with",
"salt",
".",
"utils",
".",
"files",
".",
"fopen",
"(",
"pub_path",
")",
"as",
"fp_",
":",
"mpub_64",
"=",
"fp_",
".",
"read",... | creates a signature for the given public-key with
the given private key and writes it to sign_path | [
"creates",
"a",
"signature",
"for",
"the",
"given",
"public",
"-",
"key",
"with",
"the",
"given",
"private",
"key",
"and",
"writes",
"it",
"to",
"sign_path"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L254-L281 | train |
saltstack/salt | salt/crypt.py | private_encrypt | def private_encrypt(key, message):
'''
Generate an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA key object
:param str message: The message to sign
:rtype: str
:return: The signature, or an empty string if the signature operation failed
'''
if HAS_M2:
return key.private_encrypt(message, salt.utils.rsax931.RSA_X931_PADDING)
else:
signer = salt.utils.rsax931.RSAX931Signer(key.exportKey('PEM'))
return signer.sign(message) | python | def private_encrypt(key, message):
'''
Generate an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA key object
:param str message: The message to sign
:rtype: str
:return: The signature, or an empty string if the signature operation failed
'''
if HAS_M2:
return key.private_encrypt(message, salt.utils.rsax931.RSA_X931_PADDING)
else:
signer = salt.utils.rsax931.RSAX931Signer(key.exportKey('PEM'))
return signer.sign(message) | [
"def",
"private_encrypt",
"(",
"key",
",",
"message",
")",
":",
"if",
"HAS_M2",
":",
"return",
"key",
".",
"private_encrypt",
"(",
"message",
",",
"salt",
".",
"utils",
".",
"rsax931",
".",
"RSA_X931_PADDING",
")",
"else",
":",
"signer",
"=",
"salt",
"."... | Generate an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA key object
:param str message: The message to sign
:rtype: str
:return: The signature, or an empty string if the signature operation failed | [
"Generate",
"an",
"M2Crypto",
"-",
"compatible",
"signature"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L284-L297 | train |
saltstack/salt | salt/crypt.py | public_decrypt | def public_decrypt(pub, message):
'''
Verify an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA public key object
:param str message: The signed message to verify
:rtype: str
:return: The message (or digest) recovered from the signature, or an
empty string if the verification failed
'''
if HAS_M2:
return pub.public_decrypt(message, salt.utils.rsax931.RSA_X931_PADDING)
else:
verifier = salt.utils.rsax931.RSAX931Verifier(pub.exportKey('PEM'))
return verifier.verify(message) | python | def public_decrypt(pub, message):
'''
Verify an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA public key object
:param str message: The signed message to verify
:rtype: str
:return: The message (or digest) recovered from the signature, or an
empty string if the verification failed
'''
if HAS_M2:
return pub.public_decrypt(message, salt.utils.rsax931.RSA_X931_PADDING)
else:
verifier = salt.utils.rsax931.RSAX931Verifier(pub.exportKey('PEM'))
return verifier.verify(message) | [
"def",
"public_decrypt",
"(",
"pub",
",",
"message",
")",
":",
"if",
"HAS_M2",
":",
"return",
"pub",
".",
"public_decrypt",
"(",
"message",
",",
"salt",
".",
"utils",
".",
"rsax931",
".",
"RSA_X931_PADDING",
")",
"else",
":",
"verifier",
"=",
"salt",
"."... | Verify an M2Crypto-compatible signature
:param Crypto.PublicKey.RSA._RSAobj key: The RSA public key object
:param str message: The signed message to verify
:rtype: str
:return: The message (or digest) recovered from the signature, or an
empty string if the verification failed | [
"Verify",
"an",
"M2Crypto",
"-",
"compatible",
"signature"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L300-L314 | train |
saltstack/salt | salt/crypt.py | MasterKeys.__get_keys | def __get_keys(self, name='master', passphrase=None):
'''
Returns a key object for a key in the pki-dir
'''
path = os.path.join(self.opts['pki_dir'],
name + '.pem')
if not os.path.exists(path):
log.info('Generating %s keys: %s', name, self.opts['pki_dir'])
gen_keys(self.opts['pki_dir'],
name,
self.opts['keysize'],
self.opts.get('user'),
passphrase)
if HAS_M2:
key_error = RSA.RSAError
else:
key_error = ValueError
try:
key = get_rsa_key(path, passphrase)
except key_error as e:
message = 'Unable to read key: {0}; passphrase may be incorrect'.format(path)
log.error(message)
raise MasterExit(message)
log.debug('Loaded %s key: %s', name, path)
return key | python | def __get_keys(self, name='master', passphrase=None):
'''
Returns a key object for a key in the pki-dir
'''
path = os.path.join(self.opts['pki_dir'],
name + '.pem')
if not os.path.exists(path):
log.info('Generating %s keys: %s', name, self.opts['pki_dir'])
gen_keys(self.opts['pki_dir'],
name,
self.opts['keysize'],
self.opts.get('user'),
passphrase)
if HAS_M2:
key_error = RSA.RSAError
else:
key_error = ValueError
try:
key = get_rsa_key(path, passphrase)
except key_error as e:
message = 'Unable to read key: {0}; passphrase may be incorrect'.format(path)
log.error(message)
raise MasterExit(message)
log.debug('Loaded %s key: %s', name, path)
return key | [
"def",
"__get_keys",
"(",
"self",
",",
"name",
"=",
"'master'",
",",
"passphrase",
"=",
"None",
")",
":",
"path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"opts",
"[",
"'pki_dir'",
"]",
",",
"name",
"+",
"'.pem'",
")",
"if",
"not",
... | Returns a key object for a key in the pki-dir | [
"Returns",
"a",
"key",
"object",
"for",
"a",
"key",
"in",
"the",
"pki",
"-",
"dir"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L383-L407 | train |
saltstack/salt | salt/crypt.py | MasterKeys.get_pub_str | def get_pub_str(self, name='master'):
'''
Return the string representation of a public key
in the pki-directory
'''
path = os.path.join(self.opts['pki_dir'],
name + '.pub')
if not os.path.isfile(path):
key = self.__get_keys()
if HAS_M2:
key.save_pub_key(path)
else:
with salt.utils.files.fopen(path, 'wb+') as wfh:
wfh.write(key.publickey().exportKey('PEM'))
with salt.utils.files.fopen(path) as rfh:
return rfh.read() | python | def get_pub_str(self, name='master'):
'''
Return the string representation of a public key
in the pki-directory
'''
path = os.path.join(self.opts['pki_dir'],
name + '.pub')
if not os.path.isfile(path):
key = self.__get_keys()
if HAS_M2:
key.save_pub_key(path)
else:
with salt.utils.files.fopen(path, 'wb+') as wfh:
wfh.write(key.publickey().exportKey('PEM'))
with salt.utils.files.fopen(path) as rfh:
return rfh.read() | [
"def",
"get_pub_str",
"(",
"self",
",",
"name",
"=",
"'master'",
")",
":",
"path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"opts",
"[",
"'pki_dir'",
"]",
",",
"name",
"+",
"'.pub'",
")",
"if",
"not",
"os",
".",
"path",
".",
"isfile... | Return the string representation of a public key
in the pki-directory | [
"Return",
"the",
"string",
"representation",
"of",
"a",
"public",
"key",
"in",
"the",
"pki",
"-",
"directory"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L409-L424 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.authenticate | def authenticate(self, callback=None):
'''
Ask for this client to reconnect to the origin
This function will de-dupe all calls here and return a *single* future
for the sign-in-- whis way callers can all assume there aren't others
'''
# if an auth is in flight-- and not done-- just pass that back as the future to wait on
if hasattr(self, '_authenticate_future') and not self._authenticate_future.done():
future = self._authenticate_future
else:
future = tornado.concurrent.Future()
self._authenticate_future = future
self.io_loop.add_callback(self._authenticate)
if callback is not None:
def handle_future(future):
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
return future | python | def authenticate(self, callback=None):
'''
Ask for this client to reconnect to the origin
This function will de-dupe all calls here and return a *single* future
for the sign-in-- whis way callers can all assume there aren't others
'''
# if an auth is in flight-- and not done-- just pass that back as the future to wait on
if hasattr(self, '_authenticate_future') and not self._authenticate_future.done():
future = self._authenticate_future
else:
future = tornado.concurrent.Future()
self._authenticate_future = future
self.io_loop.add_callback(self._authenticate)
if callback is not None:
def handle_future(future):
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
return future | [
"def",
"authenticate",
"(",
"self",
",",
"callback",
"=",
"None",
")",
":",
"# if an auth is in flight-- and not done-- just pass that back as the future to wait on",
"if",
"hasattr",
"(",
"self",
",",
"'_authenticate_future'",
")",
"and",
"not",
"self",
".",
"_authentica... | Ask for this client to reconnect to the origin
This function will de-dupe all calls here and return a *single* future
for the sign-in-- whis way callers can all assume there aren't others | [
"Ask",
"for",
"this",
"client",
"to",
"reconnect",
"to",
"the",
"origin"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L558-L579 | train |
saltstack/salt | salt/crypt.py | AsyncAuth._authenticate | def _authenticate(self):
'''
Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations
'''
acceptance_wait_time = self.opts['acceptance_wait_time']
acceptance_wait_time_max = self.opts['acceptance_wait_time_max']
if not acceptance_wait_time_max:
acceptance_wait_time_max = acceptance_wait_time
creds = None
channel = salt.transport.client.AsyncReqChannel.factory(self.opts,
crypt='clear',
io_loop=self.io_loop)
try:
error = None
while True:
try:
creds = yield self.sign_in(channel=channel)
except SaltClientError as exc:
error = exc
break
if creds == 'retry':
if self.opts.get('detect_mode') is True:
error = SaltClientError('Detect mode is on')
break
if self.opts.get('caller'):
# We have a list of masters, so we should break
# and try the next one in the list.
if self.opts.get('local_masters', None):
error = SaltClientError('Minion failed to authenticate'
' with the master, has the '
'minion key been accepted?')
break
else:
print('Minion failed to authenticate with the master, '
'has the minion key been accepted?')
sys.exit(2)
if acceptance_wait_time:
log.info(
'Waiting %s seconds before retry.', acceptance_wait_time
)
yield tornado.gen.sleep(acceptance_wait_time)
if acceptance_wait_time < acceptance_wait_time_max:
acceptance_wait_time += acceptance_wait_time
log.debug(
'Authentication wait time is %s', acceptance_wait_time
)
continue
break
if not isinstance(creds, dict) or 'aes' not in creds:
if self.opts.get('detect_mode') is True:
error = SaltClientError('-|RETRY|-')
try:
del AsyncAuth.creds_map[self.__key(self.opts)]
except KeyError:
pass
if not error:
error = SaltClientError('Attempt to authenticate with the salt master failed')
self._authenticate_future.set_exception(error)
else:
key = self.__key(self.opts)
AsyncAuth.creds_map[key] = creds
self._creds = creds
self._crypticle = Crypticle(self.opts, creds['aes'])
self._authenticate_future.set_result(True) # mark the sign-in as complete
# Notify the bus about creds change
if self.opts.get('auth_events') is True:
event = salt.utils.event.get_event(self.opts.get('__role'), opts=self.opts, listen=False)
event.fire_event(
{'key': key, 'creds': creds},
salt.utils.event.tagify(prefix='auth', suffix='creds')
)
finally:
channel.close() | python | def _authenticate(self):
'''
Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations
'''
acceptance_wait_time = self.opts['acceptance_wait_time']
acceptance_wait_time_max = self.opts['acceptance_wait_time_max']
if not acceptance_wait_time_max:
acceptance_wait_time_max = acceptance_wait_time
creds = None
channel = salt.transport.client.AsyncReqChannel.factory(self.opts,
crypt='clear',
io_loop=self.io_loop)
try:
error = None
while True:
try:
creds = yield self.sign_in(channel=channel)
except SaltClientError as exc:
error = exc
break
if creds == 'retry':
if self.opts.get('detect_mode') is True:
error = SaltClientError('Detect mode is on')
break
if self.opts.get('caller'):
# We have a list of masters, so we should break
# and try the next one in the list.
if self.opts.get('local_masters', None):
error = SaltClientError('Minion failed to authenticate'
' with the master, has the '
'minion key been accepted?')
break
else:
print('Minion failed to authenticate with the master, '
'has the minion key been accepted?')
sys.exit(2)
if acceptance_wait_time:
log.info(
'Waiting %s seconds before retry.', acceptance_wait_time
)
yield tornado.gen.sleep(acceptance_wait_time)
if acceptance_wait_time < acceptance_wait_time_max:
acceptance_wait_time += acceptance_wait_time
log.debug(
'Authentication wait time is %s', acceptance_wait_time
)
continue
break
if not isinstance(creds, dict) or 'aes' not in creds:
if self.opts.get('detect_mode') is True:
error = SaltClientError('-|RETRY|-')
try:
del AsyncAuth.creds_map[self.__key(self.opts)]
except KeyError:
pass
if not error:
error = SaltClientError('Attempt to authenticate with the salt master failed')
self._authenticate_future.set_exception(error)
else:
key = self.__key(self.opts)
AsyncAuth.creds_map[key] = creds
self._creds = creds
self._crypticle = Crypticle(self.opts, creds['aes'])
self._authenticate_future.set_result(True) # mark the sign-in as complete
# Notify the bus about creds change
if self.opts.get('auth_events') is True:
event = salt.utils.event.get_event(self.opts.get('__role'), opts=self.opts, listen=False)
event.fire_event(
{'key': key, 'creds': creds},
salt.utils.event.tagify(prefix='auth', suffix='creds')
)
finally:
channel.close() | [
"def",
"_authenticate",
"(",
"self",
")",
":",
"acceptance_wait_time",
"=",
"self",
".",
"opts",
"[",
"'acceptance_wait_time'",
"]",
"acceptance_wait_time_max",
"=",
"self",
".",
"opts",
"[",
"'acceptance_wait_time_max'",
"]",
"if",
"not",
"acceptance_wait_time_max",
... | Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations | [
"Authenticate",
"with",
"the",
"master",
"this",
"method",
"breaks",
"the",
"functional",
"paradigm",
"it",
"will",
"update",
"the",
"master",
"information",
"from",
"a",
"fresh",
"sign",
"in",
"signing",
"in",
"can",
"occur",
"as",
"often",
"as",
"needed",
... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L582-L661 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.sign_in | def sign_in(self, timeout=60, safe=True, tries=1, channel=None):
'''
Send a sign in request to the master, sets the key information and
returns a dict containing the master publish interface to bind to
and the decrypted aes key for transport decryption.
:param int timeout: Number of seconds to wait before timing out the sign-in request
:param bool safe: If True, do not raise an exception on timeout. Retry instead.
:param int tries: The number of times to try to authenticate before giving up.
:raises SaltReqTimeoutError: If the sign-in request has timed out and :param safe: is not set
:return: Return a string on failure indicating the reason for failure. On success, return a dictionary
with the publication port and the shared AES key.
'''
auth = {}
auth_timeout = self.opts.get('auth_timeout', None)
if auth_timeout is not None:
timeout = auth_timeout
auth_safemode = self.opts.get('auth_safemode', None)
if auth_safemode is not None:
safe = auth_safemode
auth_tries = self.opts.get('auth_tries', None)
if auth_tries is not None:
tries = auth_tries
m_pub_fn = os.path.join(self.opts['pki_dir'], self.mpub)
auth['master_uri'] = self.opts['master_uri']
close_channel = False
if not channel:
close_channel = True
channel = salt.transport.client.AsyncReqChannel.factory(self.opts,
crypt='clear',
io_loop=self.io_loop)
sign_in_payload = self.minion_sign_in_payload()
try:
payload = yield channel.send(
sign_in_payload,
tries=tries,
timeout=timeout
)
except SaltReqTimeoutError as e:
if safe:
log.warning('SaltReqTimeoutError: %s', e)
raise tornado.gen.Return('retry')
if self.opts.get('detect_mode') is True:
raise tornado.gen.Return('retry')
else:
raise SaltClientError('Attempt to authenticate with the salt master failed with timeout error')
finally:
if close_channel:
channel.close()
if not isinstance(payload, dict):
log.error('Sign-in attempt failed: %s', payload)
raise tornado.gen.Return(False)
if 'load' in payload:
if 'ret' in payload['load']:
if not payload['load']['ret']:
if self.opts['rejected_retry']:
log.error(
'The Salt Master has rejected this minion\'s public '
'key.\nTo repair this issue, delete the public key '
'for this minion on the Salt Master.\nThe Salt '
'Minion will attempt to to re-authenicate.'
)
raise tornado.gen.Return('retry')
else:
log.critical(
'The Salt Master has rejected this minion\'s public '
'key!\nTo repair this issue, delete the public key '
'for this minion on the Salt Master and restart this '
'minion.\nOr restart the Salt Master in open mode to '
'clean out the keys. The Salt Minion will now exit.'
)
# Add a random sleep here for systems that are using a
# a service manager to immediately restart the service
# to avoid overloading the system
time.sleep(random.randint(10, 20))
sys.exit(salt.defaults.exitcodes.EX_NOPERM)
# has the master returned that its maxed out with minions?
elif payload['load']['ret'] == 'full':
raise tornado.gen.Return('full')
else:
log.error(
'The Salt Master has cached the public key for this '
'node, this salt minion will wait for %s seconds '
'before attempting to re-authenticate',
self.opts['acceptance_wait_time']
)
raise tornado.gen.Return('retry')
auth['aes'] = self.verify_master(payload, master_pub='token' in sign_in_payload)
if not auth['aes']:
log.critical(
'The Salt Master server\'s public key did not authenticate!\n'
'The master may need to be updated if it is a version of Salt '
'lower than %s, or\n'
'If you are confident that you are connecting to a valid Salt '
'Master, then remove the master public key and restart the '
'Salt Minion.\nThe master public key can be found '
'at:\n%s', salt.version.__version__, m_pub_fn
)
raise SaltClientError('Invalid master key')
if self.opts.get('syndic_master', False): # Is syndic
syndic_finger = self.opts.get('syndic_finger', self.opts.get('master_finger', False))
if syndic_finger:
if salt.utils.crypt.pem_finger(m_pub_fn, sum_type=self.opts['hash_type']) != syndic_finger:
self._finger_fail(syndic_finger, m_pub_fn)
else:
if self.opts.get('master_finger', False):
if salt.utils.crypt.pem_finger(m_pub_fn, sum_type=self.opts['hash_type']) != self.opts['master_finger']:
self._finger_fail(self.opts['master_finger'], m_pub_fn)
auth['publish_port'] = payload['publish_port']
raise tornado.gen.Return(auth) | python | def sign_in(self, timeout=60, safe=True, tries=1, channel=None):
'''
Send a sign in request to the master, sets the key information and
returns a dict containing the master publish interface to bind to
and the decrypted aes key for transport decryption.
:param int timeout: Number of seconds to wait before timing out the sign-in request
:param bool safe: If True, do not raise an exception on timeout. Retry instead.
:param int tries: The number of times to try to authenticate before giving up.
:raises SaltReqTimeoutError: If the sign-in request has timed out and :param safe: is not set
:return: Return a string on failure indicating the reason for failure. On success, return a dictionary
with the publication port and the shared AES key.
'''
auth = {}
auth_timeout = self.opts.get('auth_timeout', None)
if auth_timeout is not None:
timeout = auth_timeout
auth_safemode = self.opts.get('auth_safemode', None)
if auth_safemode is not None:
safe = auth_safemode
auth_tries = self.opts.get('auth_tries', None)
if auth_tries is not None:
tries = auth_tries
m_pub_fn = os.path.join(self.opts['pki_dir'], self.mpub)
auth['master_uri'] = self.opts['master_uri']
close_channel = False
if not channel:
close_channel = True
channel = salt.transport.client.AsyncReqChannel.factory(self.opts,
crypt='clear',
io_loop=self.io_loop)
sign_in_payload = self.minion_sign_in_payload()
try:
payload = yield channel.send(
sign_in_payload,
tries=tries,
timeout=timeout
)
except SaltReqTimeoutError as e:
if safe:
log.warning('SaltReqTimeoutError: %s', e)
raise tornado.gen.Return('retry')
if self.opts.get('detect_mode') is True:
raise tornado.gen.Return('retry')
else:
raise SaltClientError('Attempt to authenticate with the salt master failed with timeout error')
finally:
if close_channel:
channel.close()
if not isinstance(payload, dict):
log.error('Sign-in attempt failed: %s', payload)
raise tornado.gen.Return(False)
if 'load' in payload:
if 'ret' in payload['load']:
if not payload['load']['ret']:
if self.opts['rejected_retry']:
log.error(
'The Salt Master has rejected this minion\'s public '
'key.\nTo repair this issue, delete the public key '
'for this minion on the Salt Master.\nThe Salt '
'Minion will attempt to to re-authenicate.'
)
raise tornado.gen.Return('retry')
else:
log.critical(
'The Salt Master has rejected this minion\'s public '
'key!\nTo repair this issue, delete the public key '
'for this minion on the Salt Master and restart this '
'minion.\nOr restart the Salt Master in open mode to '
'clean out the keys. The Salt Minion will now exit.'
)
# Add a random sleep here for systems that are using a
# a service manager to immediately restart the service
# to avoid overloading the system
time.sleep(random.randint(10, 20))
sys.exit(salt.defaults.exitcodes.EX_NOPERM)
# has the master returned that its maxed out with minions?
elif payload['load']['ret'] == 'full':
raise tornado.gen.Return('full')
else:
log.error(
'The Salt Master has cached the public key for this '
'node, this salt minion will wait for %s seconds '
'before attempting to re-authenticate',
self.opts['acceptance_wait_time']
)
raise tornado.gen.Return('retry')
auth['aes'] = self.verify_master(payload, master_pub='token' in sign_in_payload)
if not auth['aes']:
log.critical(
'The Salt Master server\'s public key did not authenticate!\n'
'The master may need to be updated if it is a version of Salt '
'lower than %s, or\n'
'If you are confident that you are connecting to a valid Salt '
'Master, then remove the master public key and restart the '
'Salt Minion.\nThe master public key can be found '
'at:\n%s', salt.version.__version__, m_pub_fn
)
raise SaltClientError('Invalid master key')
if self.opts.get('syndic_master', False): # Is syndic
syndic_finger = self.opts.get('syndic_finger', self.opts.get('master_finger', False))
if syndic_finger:
if salt.utils.crypt.pem_finger(m_pub_fn, sum_type=self.opts['hash_type']) != syndic_finger:
self._finger_fail(syndic_finger, m_pub_fn)
else:
if self.opts.get('master_finger', False):
if salt.utils.crypt.pem_finger(m_pub_fn, sum_type=self.opts['hash_type']) != self.opts['master_finger']:
self._finger_fail(self.opts['master_finger'], m_pub_fn)
auth['publish_port'] = payload['publish_port']
raise tornado.gen.Return(auth) | [
"def",
"sign_in",
"(",
"self",
",",
"timeout",
"=",
"60",
",",
"safe",
"=",
"True",
",",
"tries",
"=",
"1",
",",
"channel",
"=",
"None",
")",
":",
"auth",
"=",
"{",
"}",
"auth_timeout",
"=",
"self",
".",
"opts",
".",
"get",
"(",
"'auth_timeout'",
... | Send a sign in request to the master, sets the key information and
returns a dict containing the master publish interface to bind to
and the decrypted aes key for transport decryption.
:param int timeout: Number of seconds to wait before timing out the sign-in request
:param bool safe: If True, do not raise an exception on timeout. Retry instead.
:param int tries: The number of times to try to authenticate before giving up.
:raises SaltReqTimeoutError: If the sign-in request has timed out and :param safe: is not set
:return: Return a string on failure indicating the reason for failure. On success, return a dictionary
with the publication port and the shared AES key. | [
"Send",
"a",
"sign",
"in",
"request",
"to",
"the",
"master",
"sets",
"the",
"key",
"information",
"and",
"returns",
"a",
"dict",
"containing",
"the",
"master",
"publish",
"interface",
"to",
"bind",
"to",
"and",
"the",
"decrypted",
"aes",
"key",
"for",
"tra... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L664-L782 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.get_keys | def get_keys(self):
'''
Return keypair object for the minion.
:rtype: Crypto.PublicKey.RSA._RSAobj
:return: The RSA keypair
'''
# Make sure all key parent directories are accessible
user = self.opts.get('user', 'root')
salt.utils.verify.check_path_traversal(self.opts['pki_dir'], user)
if not os.path.exists(self.rsa_path):
log.info('Generating keys: %s', self.opts['pki_dir'])
gen_keys(self.opts['pki_dir'],
'minion',
self.opts['keysize'],
self.opts.get('user'))
key = get_rsa_key(self.rsa_path, None)
log.debug('Loaded minion key: %s', self.rsa_path)
return key | python | def get_keys(self):
'''
Return keypair object for the minion.
:rtype: Crypto.PublicKey.RSA._RSAobj
:return: The RSA keypair
'''
# Make sure all key parent directories are accessible
user = self.opts.get('user', 'root')
salt.utils.verify.check_path_traversal(self.opts['pki_dir'], user)
if not os.path.exists(self.rsa_path):
log.info('Generating keys: %s', self.opts['pki_dir'])
gen_keys(self.opts['pki_dir'],
'minion',
self.opts['keysize'],
self.opts.get('user'))
key = get_rsa_key(self.rsa_path, None)
log.debug('Loaded minion key: %s', self.rsa_path)
return key | [
"def",
"get_keys",
"(",
"self",
")",
":",
"# Make sure all key parent directories are accessible",
"user",
"=",
"self",
".",
"opts",
".",
"get",
"(",
"'user'",
",",
"'root'",
")",
"salt",
".",
"utils",
".",
"verify",
".",
"check_path_traversal",
"(",
"self",
"... | Return keypair object for the minion.
:rtype: Crypto.PublicKey.RSA._RSAobj
:return: The RSA keypair | [
"Return",
"keypair",
"object",
"for",
"the",
"minion",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L784-L803 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.minion_sign_in_payload | def minion_sign_in_payload(self):
'''
Generates the payload used to authenticate with the master
server. This payload consists of the passed in id_ and the ssh
public key to encrypt the AES key sent back from the master.
:return: Payload dictionary
:rtype: dict
'''
payload = {}
payload['cmd'] = '_auth'
payload['id'] = self.opts['id']
if 'autosign_grains' in self.opts:
autosign_grains = {}
for grain in self.opts['autosign_grains']:
autosign_grains[grain] = self.opts['grains'].get(grain, None)
payload['autosign_grains'] = autosign_grains
try:
pubkey_path = os.path.join(self.opts['pki_dir'], self.mpub)
pub = get_rsa_pub_key(pubkey_path)
if HAS_M2:
payload['token'] = pub.public_encrypt(self.token, RSA.pkcs1_oaep_padding)
else:
cipher = PKCS1_OAEP.new(pub)
payload['token'] = cipher.encrypt(self.token)
except Exception:
pass
with salt.utils.files.fopen(self.pub_path) as f:
payload['pub'] = f.read()
return payload | python | def minion_sign_in_payload(self):
'''
Generates the payload used to authenticate with the master
server. This payload consists of the passed in id_ and the ssh
public key to encrypt the AES key sent back from the master.
:return: Payload dictionary
:rtype: dict
'''
payload = {}
payload['cmd'] = '_auth'
payload['id'] = self.opts['id']
if 'autosign_grains' in self.opts:
autosign_grains = {}
for grain in self.opts['autosign_grains']:
autosign_grains[grain] = self.opts['grains'].get(grain, None)
payload['autosign_grains'] = autosign_grains
try:
pubkey_path = os.path.join(self.opts['pki_dir'], self.mpub)
pub = get_rsa_pub_key(pubkey_path)
if HAS_M2:
payload['token'] = pub.public_encrypt(self.token, RSA.pkcs1_oaep_padding)
else:
cipher = PKCS1_OAEP.new(pub)
payload['token'] = cipher.encrypt(self.token)
except Exception:
pass
with salt.utils.files.fopen(self.pub_path) as f:
payload['pub'] = f.read()
return payload | [
"def",
"minion_sign_in_payload",
"(",
"self",
")",
":",
"payload",
"=",
"{",
"}",
"payload",
"[",
"'cmd'",
"]",
"=",
"'_auth'",
"payload",
"[",
"'id'",
"]",
"=",
"self",
".",
"opts",
"[",
"'id'",
"]",
"if",
"'autosign_grains'",
"in",
"self",
".",
"opts... | Generates the payload used to authenticate with the master
server. This payload consists of the passed in id_ and the ssh
public key to encrypt the AES key sent back from the master.
:return: Payload dictionary
:rtype: dict | [
"Generates",
"the",
"payload",
"used",
"to",
"authenticate",
"with",
"the",
"master",
"server",
".",
"This",
"payload",
"consists",
"of",
"the",
"passed",
"in",
"id_",
"and",
"the",
"ssh",
"public",
"key",
"to",
"encrypt",
"the",
"AES",
"key",
"sent",
"bac... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L816-L845 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.decrypt_aes | def decrypt_aes(self, payload, master_pub=True):
'''
This function is used to decrypt the AES seed phrase returned from
the master server. The seed phrase is decrypted with the SSH RSA
host key.
Pass in the encrypted AES key.
Returns the decrypted AES seed key, a string
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'sig': The message signature
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The public key of the sender.
:rtype: str
:return: The decrypted token that was provided, with padding.
:rtype: str
:return: The decrypted AES seed key
'''
if self.opts.get('auth_trb', False):
log.warning('Auth Called: %s', ''.join(traceback.format_stack()))
else:
log.debug('Decrypting the current master AES key')
key = self.get_keys()
if HAS_M2:
key_str = key.private_decrypt(payload['aes'],
RSA.pkcs1_oaep_padding)
else:
cipher = PKCS1_OAEP.new(key)
key_str = cipher.decrypt(payload['aes'])
if 'sig' in payload:
m_path = os.path.join(self.opts['pki_dir'], self.mpub)
if os.path.exists(m_path):
try:
mkey = get_rsa_pub_key(m_path)
except Exception:
return '', ''
digest = hashlib.sha256(key_str).hexdigest()
if six.PY3:
digest = salt.utils.stringutils.to_bytes(digest)
if HAS_M2:
m_digest = public_decrypt(mkey, payload['sig'])
else:
m_digest = public_decrypt(mkey.publickey(), payload['sig'])
if m_digest != digest:
return '', ''
else:
return '', ''
if six.PY3:
key_str = salt.utils.stringutils.to_str(key_str)
if '_|-' in key_str:
return key_str.split('_|-')
else:
if 'token' in payload:
if HAS_M2:
token = key.private_decrypt(payload['token'],
RSA.pkcs1_oaep_padding)
else:
token = cipher.decrypt(payload['token'])
return key_str, token
elif not master_pub:
return key_str, ''
return '', '' | python | def decrypt_aes(self, payload, master_pub=True):
'''
This function is used to decrypt the AES seed phrase returned from
the master server. The seed phrase is decrypted with the SSH RSA
host key.
Pass in the encrypted AES key.
Returns the decrypted AES seed key, a string
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'sig': The message signature
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The public key of the sender.
:rtype: str
:return: The decrypted token that was provided, with padding.
:rtype: str
:return: The decrypted AES seed key
'''
if self.opts.get('auth_trb', False):
log.warning('Auth Called: %s', ''.join(traceback.format_stack()))
else:
log.debug('Decrypting the current master AES key')
key = self.get_keys()
if HAS_M2:
key_str = key.private_decrypt(payload['aes'],
RSA.pkcs1_oaep_padding)
else:
cipher = PKCS1_OAEP.new(key)
key_str = cipher.decrypt(payload['aes'])
if 'sig' in payload:
m_path = os.path.join(self.opts['pki_dir'], self.mpub)
if os.path.exists(m_path):
try:
mkey = get_rsa_pub_key(m_path)
except Exception:
return '', ''
digest = hashlib.sha256(key_str).hexdigest()
if six.PY3:
digest = salt.utils.stringutils.to_bytes(digest)
if HAS_M2:
m_digest = public_decrypt(mkey, payload['sig'])
else:
m_digest = public_decrypt(mkey.publickey(), payload['sig'])
if m_digest != digest:
return '', ''
else:
return '', ''
if six.PY3:
key_str = salt.utils.stringutils.to_str(key_str)
if '_|-' in key_str:
return key_str.split('_|-')
else:
if 'token' in payload:
if HAS_M2:
token = key.private_decrypt(payload['token'],
RSA.pkcs1_oaep_padding)
else:
token = cipher.decrypt(payload['token'])
return key_str, token
elif not master_pub:
return key_str, ''
return '', '' | [
"def",
"decrypt_aes",
"(",
"self",
",",
"payload",
",",
"master_pub",
"=",
"True",
")",
":",
"if",
"self",
".",
"opts",
".",
"get",
"(",
"'auth_trb'",
",",
"False",
")",
":",
"log",
".",
"warning",
"(",
"'Auth Called: %s'",
",",
"''",
".",
"join",
"(... | This function is used to decrypt the AES seed phrase returned from
the master server. The seed phrase is decrypted with the SSH RSA
host key.
Pass in the encrypted AES key.
Returns the decrypted AES seed key, a string
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'sig': The message signature
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The public key of the sender.
:rtype: str
:return: The decrypted token that was provided, with padding.
:rtype: str
:return: The decrypted AES seed key | [
"This",
"function",
"is",
"used",
"to",
"decrypt",
"the",
"AES",
"seed",
"phrase",
"returned",
"from",
"the",
"master",
"server",
".",
"The",
"seed",
"phrase",
"is",
"decrypted",
"with",
"the",
"SSH",
"RSA",
"host",
"key",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L847-L915 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.verify_pubkey_sig | def verify_pubkey_sig(self, message, sig):
'''
Wraps the verify_signature method so we have
additional checks.
:rtype: bool
:return: Success or failure of public key verification
'''
if self.opts['master_sign_key_name']:
path = os.path.join(self.opts['pki_dir'],
self.opts['master_sign_key_name'] + '.pub')
if os.path.isfile(path):
res = verify_signature(path,
message,
binascii.a2b_base64(sig))
else:
log.error(
'Verification public key %s does not exist. You need to '
'copy it from the master to the minions pki directory',
os.path.basename(path)
)
return False
if res:
log.debug(
'Successfully verified signature of master public key '
'with verification public key %s',
self.opts['master_sign_key_name'] + '.pub'
)
return True
else:
log.debug('Failed to verify signature of public key')
return False
else:
log.error(
'Failed to verify the signature of the message because the '
'verification key-pairs name is not defined. Please make '
'sure that master_sign_key_name is defined.'
)
return False | python | def verify_pubkey_sig(self, message, sig):
'''
Wraps the verify_signature method so we have
additional checks.
:rtype: bool
:return: Success or failure of public key verification
'''
if self.opts['master_sign_key_name']:
path = os.path.join(self.opts['pki_dir'],
self.opts['master_sign_key_name'] + '.pub')
if os.path.isfile(path):
res = verify_signature(path,
message,
binascii.a2b_base64(sig))
else:
log.error(
'Verification public key %s does not exist. You need to '
'copy it from the master to the minions pki directory',
os.path.basename(path)
)
return False
if res:
log.debug(
'Successfully verified signature of master public key '
'with verification public key %s',
self.opts['master_sign_key_name'] + '.pub'
)
return True
else:
log.debug('Failed to verify signature of public key')
return False
else:
log.error(
'Failed to verify the signature of the message because the '
'verification key-pairs name is not defined. Please make '
'sure that master_sign_key_name is defined.'
)
return False | [
"def",
"verify_pubkey_sig",
"(",
"self",
",",
"message",
",",
"sig",
")",
":",
"if",
"self",
".",
"opts",
"[",
"'master_sign_key_name'",
"]",
":",
"path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"opts",
"[",
"'pki_dir'",
"]",
",",
"sel... | Wraps the verify_signature method so we have
additional checks.
:rtype: bool
:return: Success or failure of public key verification | [
"Wraps",
"the",
"verify_signature",
"method",
"so",
"we",
"have",
"additional",
"checks",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L917-L956 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.check_auth_deps | def check_auth_deps(self, payload):
'''
Checks if both master and minion either sign (master) and
verify (minion). If one side does not, it should fail.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', 'aes')
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
'''
# master and minion sign and verify
if 'pub_sig' in payload and self.opts['verify_master_pubkey_sign']:
return True
# master and minion do NOT sign and do NOT verify
elif 'pub_sig' not in payload and not self.opts['verify_master_pubkey_sign']:
return True
# master signs, but minion does NOT verify
elif 'pub_sig' in payload and not self.opts['verify_master_pubkey_sign']:
log.error('The masters sent its public-key signature, but signature '
'verification is not enabled on the minion. Either enable '
'signature verification on the minion or disable signing '
'the public key on the master!')
return False
# master does NOT sign but minion wants to verify
elif 'pub_sig' not in payload and self.opts['verify_master_pubkey_sign']:
log.error('The master did not send its public-key signature, but '
'signature verification is enabled on the minion. Either '
'disable signature verification on the minion or enable '
'signing the public on the master!')
return False | python | def check_auth_deps(self, payload):
'''
Checks if both master and minion either sign (master) and
verify (minion). If one side does not, it should fail.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', 'aes')
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
'''
# master and minion sign and verify
if 'pub_sig' in payload and self.opts['verify_master_pubkey_sign']:
return True
# master and minion do NOT sign and do NOT verify
elif 'pub_sig' not in payload and not self.opts['verify_master_pubkey_sign']:
return True
# master signs, but minion does NOT verify
elif 'pub_sig' in payload and not self.opts['verify_master_pubkey_sign']:
log.error('The masters sent its public-key signature, but signature '
'verification is not enabled on the minion. Either enable '
'signature verification on the minion or disable signing '
'the public key on the master!')
return False
# master does NOT sign but minion wants to verify
elif 'pub_sig' not in payload and self.opts['verify_master_pubkey_sign']:
log.error('The master did not send its public-key signature, but '
'signature verification is enabled on the minion. Either '
'disable signature verification on the minion or enable '
'signing the public on the master!')
return False | [
"def",
"check_auth_deps",
"(",
"self",
",",
"payload",
")",
":",
"# master and minion sign and verify",
"if",
"'pub_sig'",
"in",
"payload",
"and",
"self",
".",
"opts",
"[",
"'verify_master_pubkey_sign'",
"]",
":",
"return",
"True",
"# master and minion do NOT sign and d... | Checks if both master and minion either sign (master) and
verify (minion). If one side does not, it should fail.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', 'aes')
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender. | [
"Checks",
"if",
"both",
"master",
"and",
"minion",
"either",
"sign",
"(",
"master",
")",
"and",
"verify",
"(",
"minion",
")",
".",
"If",
"one",
"side",
"does",
"not",
"it",
"should",
"fail",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L984-L1016 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.extract_aes | def extract_aes(self, payload, master_pub=True):
'''
Return the AES key received from the master after the minion has been
successfully authenticated.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:rtype: str
:return: The shared AES key received from the master.
'''
if master_pub:
try:
aes, token = self.decrypt_aes(payload, master_pub)
if token != self.token:
log.error(
'The master failed to decrypt the random minion token'
)
return ''
except Exception:
log.error(
'The master failed to decrypt the random minion token'
)
return ''
return aes
else:
aes, token = self.decrypt_aes(payload, master_pub)
return aes | python | def extract_aes(self, payload, master_pub=True):
'''
Return the AES key received from the master after the minion has been
successfully authenticated.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:rtype: str
:return: The shared AES key received from the master.
'''
if master_pub:
try:
aes, token = self.decrypt_aes(payload, master_pub)
if token != self.token:
log.error(
'The master failed to decrypt the random minion token'
)
return ''
except Exception:
log.error(
'The master failed to decrypt the random minion token'
)
return ''
return aes
else:
aes, token = self.decrypt_aes(payload, master_pub)
return aes | [
"def",
"extract_aes",
"(",
"self",
",",
"payload",
",",
"master_pub",
"=",
"True",
")",
":",
"if",
"master_pub",
":",
"try",
":",
"aes",
",",
"token",
"=",
"self",
".",
"decrypt_aes",
"(",
"payload",
",",
"master_pub",
")",
"if",
"token",
"!=",
"self",... | Return the AES key received from the master after the minion has been
successfully authenticated.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:rtype: str
:return: The shared AES key received from the master. | [
"Return",
"the",
"AES",
"key",
"received",
"from",
"the",
"master",
"after",
"the",
"minion",
"has",
"been",
"successfully",
"authenticated",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1018-L1049 | train |
saltstack/salt | salt/crypt.py | AsyncAuth.verify_master | def verify_master(self, payload, master_pub=True):
'''
Verify that the master is the same one that was previously accepted.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:param bool master_pub: Operate as if minion had no master pubkey when it sent auth request, i.e. don't verify
the minion signature
:rtype: str
:return: An empty string on verification failure. On success, the decrypted AES message in the payload.
'''
m_pub_fn = os.path.join(self.opts['pki_dir'], self.mpub)
m_pub_exists = os.path.isfile(m_pub_fn)
if m_pub_exists and master_pub and not self.opts['open_mode']:
with salt.utils.files.fopen(m_pub_fn) as fp_:
local_master_pub = fp_.read()
if payload['pub_key'].replace('\n', '').replace('\r', '') != \
local_master_pub.replace('\n', '').replace('\r', ''):
if not self.check_auth_deps(payload):
return ''
if self.opts['verify_master_pubkey_sign']:
if self.verify_signing_master(payload):
return self.extract_aes(payload, master_pub=False)
else:
return ''
else:
# This is not the last master we connected to
log.error(
'The master key has changed, the salt master could '
'have been subverted, verify salt master\'s public '
'key'
)
return ''
else:
if not self.check_auth_deps(payload):
return ''
# verify the signature of the pubkey even if it has
# not changed compared with the one we already have
if self.opts['always_verify_signature']:
if self.verify_signing_master(payload):
return self.extract_aes(payload)
else:
log.error(
'The masters public could not be verified. Is the '
'verification pubkey %s up to date?',
self.opts['master_sign_key_name'] + '.pub'
)
return ''
else:
return self.extract_aes(payload)
else:
if not self.check_auth_deps(payload):
return ''
# verify the masters pubkey signature if the minion
# has not received any masters pubkey before
if self.opts['verify_master_pubkey_sign']:
if self.verify_signing_master(payload):
return self.extract_aes(payload, master_pub=False)
else:
return ''
else:
if not m_pub_exists:
# the minion has not received any masters pubkey yet, write
# the newly received pubkey to minion_master.pub
with salt.utils.files.fopen(m_pub_fn, 'wb+') as fp_:
fp_.write(salt.utils.stringutils.to_bytes(payload['pub_key']))
return self.extract_aes(payload, master_pub=False) | python | def verify_master(self, payload, master_pub=True):
'''
Verify that the master is the same one that was previously accepted.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:param bool master_pub: Operate as if minion had no master pubkey when it sent auth request, i.e. don't verify
the minion signature
:rtype: str
:return: An empty string on verification failure. On success, the decrypted AES message in the payload.
'''
m_pub_fn = os.path.join(self.opts['pki_dir'], self.mpub)
m_pub_exists = os.path.isfile(m_pub_fn)
if m_pub_exists and master_pub and not self.opts['open_mode']:
with salt.utils.files.fopen(m_pub_fn) as fp_:
local_master_pub = fp_.read()
if payload['pub_key'].replace('\n', '').replace('\r', '') != \
local_master_pub.replace('\n', '').replace('\r', ''):
if not self.check_auth_deps(payload):
return ''
if self.opts['verify_master_pubkey_sign']:
if self.verify_signing_master(payload):
return self.extract_aes(payload, master_pub=False)
else:
return ''
else:
# This is not the last master we connected to
log.error(
'The master key has changed, the salt master could '
'have been subverted, verify salt master\'s public '
'key'
)
return ''
else:
if not self.check_auth_deps(payload):
return ''
# verify the signature of the pubkey even if it has
# not changed compared with the one we already have
if self.opts['always_verify_signature']:
if self.verify_signing_master(payload):
return self.extract_aes(payload)
else:
log.error(
'The masters public could not be verified. Is the '
'verification pubkey %s up to date?',
self.opts['master_sign_key_name'] + '.pub'
)
return ''
else:
return self.extract_aes(payload)
else:
if not self.check_auth_deps(payload):
return ''
# verify the masters pubkey signature if the minion
# has not received any masters pubkey before
if self.opts['verify_master_pubkey_sign']:
if self.verify_signing_master(payload):
return self.extract_aes(payload, master_pub=False)
else:
return ''
else:
if not m_pub_exists:
# the minion has not received any masters pubkey yet, write
# the newly received pubkey to minion_master.pub
with salt.utils.files.fopen(m_pub_fn, 'wb+') as fp_:
fp_.write(salt.utils.stringutils.to_bytes(payload['pub_key']))
return self.extract_aes(payload, master_pub=False) | [
"def",
"verify_master",
"(",
"self",
",",
"payload",
",",
"master_pub",
"=",
"True",
")",
":",
"m_pub_fn",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"opts",
"[",
"'pki_dir'",
"]",
",",
"self",
".",
"mpub",
")",
"m_pub_exists",
"=",
"os",... | Verify that the master is the same one that was previously accepted.
:param dict payload: The incoming payload. This is a dictionary which may have the following keys:
'aes': The shared AES key
'enc': The format of the message. ('clear', 'pub', etc)
'publish_port': The TCP port which published the message
'token': The encrypted token used to verify the message.
'pub_key': The RSA public key of the sender.
:param bool master_pub: Operate as if minion had no master pubkey when it sent auth request, i.e. don't verify
the minion signature
:rtype: str
:return: An empty string on verification failure. On success, the decrypted AES message in the payload. | [
"Verify",
"that",
"the",
"master",
"is",
"the",
"same",
"one",
"that",
"was",
"previously",
"accepted",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1051-L1127 | train |
saltstack/salt | salt/crypt.py | SAuth.authenticate | def authenticate(self, _=None): # TODO: remove unused var
'''
Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations
'''
acceptance_wait_time = self.opts['acceptance_wait_time']
acceptance_wait_time_max = self.opts['acceptance_wait_time_max']
channel = salt.transport.client.ReqChannel.factory(self.opts, crypt='clear')
if not acceptance_wait_time_max:
acceptance_wait_time_max = acceptance_wait_time
try:
while True:
creds = self.sign_in(channel=channel)
if creds == 'retry':
if self.opts.get('caller'):
# We have a list of masters, so we should break
# and try the next one in the list.
if self.opts.get('local_masters', None):
error = SaltClientError('Minion failed to authenticate'
' with the master, has the '
'minion key been accepted?')
break
else:
print('Minion failed to authenticate with the master, '
'has the minion key been accepted?')
sys.exit(2)
if acceptance_wait_time:
log.info('Waiting %s seconds before retry.', acceptance_wait_time)
time.sleep(acceptance_wait_time)
if acceptance_wait_time < acceptance_wait_time_max:
acceptance_wait_time += acceptance_wait_time
log.debug('Authentication wait time is %s', acceptance_wait_time)
continue
break
self._creds = creds
self._crypticle = Crypticle(self.opts, creds['aes'])
finally:
channel.close() | python | def authenticate(self, _=None): # TODO: remove unused var
'''
Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations
'''
acceptance_wait_time = self.opts['acceptance_wait_time']
acceptance_wait_time_max = self.opts['acceptance_wait_time_max']
channel = salt.transport.client.ReqChannel.factory(self.opts, crypt='clear')
if not acceptance_wait_time_max:
acceptance_wait_time_max = acceptance_wait_time
try:
while True:
creds = self.sign_in(channel=channel)
if creds == 'retry':
if self.opts.get('caller'):
# We have a list of masters, so we should break
# and try the next one in the list.
if self.opts.get('local_masters', None):
error = SaltClientError('Minion failed to authenticate'
' with the master, has the '
'minion key been accepted?')
break
else:
print('Minion failed to authenticate with the master, '
'has the minion key been accepted?')
sys.exit(2)
if acceptance_wait_time:
log.info('Waiting %s seconds before retry.', acceptance_wait_time)
time.sleep(acceptance_wait_time)
if acceptance_wait_time < acceptance_wait_time_max:
acceptance_wait_time += acceptance_wait_time
log.debug('Authentication wait time is %s', acceptance_wait_time)
continue
break
self._creds = creds
self._crypticle = Crypticle(self.opts, creds['aes'])
finally:
channel.close() | [
"def",
"authenticate",
"(",
"self",
",",
"_",
"=",
"None",
")",
":",
"# TODO: remove unused var",
"acceptance_wait_time",
"=",
"self",
".",
"opts",
"[",
"'acceptance_wait_time'",
"]",
"acceptance_wait_time_max",
"=",
"self",
".",
"opts",
"[",
"'acceptance_wait_time_... | Authenticate with the master, this method breaks the functional
paradigm, it will update the master information from a fresh sign
in, signing in can occur as often as needed to keep up with the
revolving master AES key.
:rtype: Crypticle
:returns: A crypticle used for encryption operations | [
"Authenticate",
"with",
"the",
"master",
"this",
"method",
"breaks",
"the",
"functional",
"paradigm",
"it",
"will",
"update",
"the",
"master",
"information",
"from",
"a",
"fresh",
"sign",
"in",
"signing",
"in",
"can",
"occur",
"as",
"often",
"as",
"needed",
... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1214-L1256 | train |
saltstack/salt | salt/crypt.py | Crypticle.encrypt | def encrypt(self, data):
'''
encrypt data with AES-CBC and sign it with HMAC-SHA256
'''
aes_key, hmac_key = self.keys
pad = self.AES_BLOCK_SIZE - len(data) % self.AES_BLOCK_SIZE
if six.PY2:
data = data + pad * chr(pad)
else:
data = data + salt.utils.stringutils.to_bytes(pad * chr(pad))
iv_bytes = os.urandom(self.AES_BLOCK_SIZE)
if HAS_M2:
cypher = EVP.Cipher(alg='aes_192_cbc', key=aes_key, iv=iv_bytes, op=1, padding=False)
encr = cypher.update(data)
encr += cypher.final()
else:
cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
encr = cypher.encrypt(data)
data = iv_bytes + encr
sig = hmac.new(hmac_key, data, hashlib.sha256).digest()
return data + sig | python | def encrypt(self, data):
'''
encrypt data with AES-CBC and sign it with HMAC-SHA256
'''
aes_key, hmac_key = self.keys
pad = self.AES_BLOCK_SIZE - len(data) % self.AES_BLOCK_SIZE
if six.PY2:
data = data + pad * chr(pad)
else:
data = data + salt.utils.stringutils.to_bytes(pad * chr(pad))
iv_bytes = os.urandom(self.AES_BLOCK_SIZE)
if HAS_M2:
cypher = EVP.Cipher(alg='aes_192_cbc', key=aes_key, iv=iv_bytes, op=1, padding=False)
encr = cypher.update(data)
encr += cypher.final()
else:
cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
encr = cypher.encrypt(data)
data = iv_bytes + encr
sig = hmac.new(hmac_key, data, hashlib.sha256).digest()
return data + sig | [
"def",
"encrypt",
"(",
"self",
",",
"data",
")",
":",
"aes_key",
",",
"hmac_key",
"=",
"self",
".",
"keys",
"pad",
"=",
"self",
".",
"AES_BLOCK_SIZE",
"-",
"len",
"(",
"data",
")",
"%",
"self",
".",
"AES_BLOCK_SIZE",
"if",
"six",
".",
"PY2",
":",
"... | encrypt data with AES-CBC and sign it with HMAC-SHA256 | [
"encrypt",
"data",
"with",
"AES",
"-",
"CBC",
"and",
"sign",
"it",
"with",
"HMAC",
"-",
"SHA256"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1406-L1426 | train |
saltstack/salt | salt/crypt.py | Crypticle.decrypt | def decrypt(self, data):
'''
verify HMAC-SHA256 signature and decrypt data with AES-CBC
'''
aes_key, hmac_key = self.keys
sig = data[-self.SIG_SIZE:]
data = data[:-self.SIG_SIZE]
if six.PY3 and not isinstance(data, bytes):
data = salt.utils.stringutils.to_bytes(data)
mac_bytes = hmac.new(hmac_key, data, hashlib.sha256).digest()
if len(mac_bytes) != len(sig):
log.debug('Failed to authenticate message')
raise AuthenticationError('message authentication failed')
result = 0
if six.PY2:
for zipped_x, zipped_y in zip(mac_bytes, sig):
result |= ord(zipped_x) ^ ord(zipped_y)
else:
for zipped_x, zipped_y in zip(mac_bytes, sig):
result |= zipped_x ^ zipped_y
if result != 0:
log.debug('Failed to authenticate message')
raise AuthenticationError('message authentication failed')
iv_bytes = data[:self.AES_BLOCK_SIZE]
data = data[self.AES_BLOCK_SIZE:]
if HAS_M2:
cypher = EVP.Cipher(alg='aes_192_cbc', key=aes_key, iv=iv_bytes, op=0, padding=False)
encr = cypher.update(data)
data = encr + cypher.final()
else:
cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
data = cypher.decrypt(data)
if six.PY2:
return data[:-ord(data[-1])]
else:
return data[:-data[-1]] | python | def decrypt(self, data):
'''
verify HMAC-SHA256 signature and decrypt data with AES-CBC
'''
aes_key, hmac_key = self.keys
sig = data[-self.SIG_SIZE:]
data = data[:-self.SIG_SIZE]
if six.PY3 and not isinstance(data, bytes):
data = salt.utils.stringutils.to_bytes(data)
mac_bytes = hmac.new(hmac_key, data, hashlib.sha256).digest()
if len(mac_bytes) != len(sig):
log.debug('Failed to authenticate message')
raise AuthenticationError('message authentication failed')
result = 0
if six.PY2:
for zipped_x, zipped_y in zip(mac_bytes, sig):
result |= ord(zipped_x) ^ ord(zipped_y)
else:
for zipped_x, zipped_y in zip(mac_bytes, sig):
result |= zipped_x ^ zipped_y
if result != 0:
log.debug('Failed to authenticate message')
raise AuthenticationError('message authentication failed')
iv_bytes = data[:self.AES_BLOCK_SIZE]
data = data[self.AES_BLOCK_SIZE:]
if HAS_M2:
cypher = EVP.Cipher(alg='aes_192_cbc', key=aes_key, iv=iv_bytes, op=0, padding=False)
encr = cypher.update(data)
data = encr + cypher.final()
else:
cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
data = cypher.decrypt(data)
if six.PY2:
return data[:-ord(data[-1])]
else:
return data[:-data[-1]] | [
"def",
"decrypt",
"(",
"self",
",",
"data",
")",
":",
"aes_key",
",",
"hmac_key",
"=",
"self",
".",
"keys",
"sig",
"=",
"data",
"[",
"-",
"self",
".",
"SIG_SIZE",
":",
"]",
"data",
"=",
"data",
"[",
":",
"-",
"self",
".",
"SIG_SIZE",
"]",
"if",
... | verify HMAC-SHA256 signature and decrypt data with AES-CBC | [
"verify",
"HMAC",
"-",
"SHA256",
"signature",
"and",
"decrypt",
"data",
"with",
"AES",
"-",
"CBC"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1428-L1464 | train |
saltstack/salt | salt/crypt.py | Crypticle.dumps | def dumps(self, obj):
'''
Serialize and encrypt a python object
'''
return self.encrypt(self.PICKLE_PAD + self.serial.dumps(obj)) | python | def dumps(self, obj):
'''
Serialize and encrypt a python object
'''
return self.encrypt(self.PICKLE_PAD + self.serial.dumps(obj)) | [
"def",
"dumps",
"(",
"self",
",",
"obj",
")",
":",
"return",
"self",
".",
"encrypt",
"(",
"self",
".",
"PICKLE_PAD",
"+",
"self",
".",
"serial",
".",
"dumps",
"(",
"obj",
")",
")"
] | Serialize and encrypt a python object | [
"Serialize",
"and",
"encrypt",
"a",
"python",
"object"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1466-L1470 | train |
saltstack/salt | salt/crypt.py | Crypticle.loads | def loads(self, data, raw=False):
'''
Decrypt and un-serialize a python object
'''
data = self.decrypt(data)
# simple integrity check to verify that we got meaningful data
if not data.startswith(self.PICKLE_PAD):
return {}
load = self.serial.loads(data[len(self.PICKLE_PAD):], raw=raw)
return load | python | def loads(self, data, raw=False):
'''
Decrypt and un-serialize a python object
'''
data = self.decrypt(data)
# simple integrity check to verify that we got meaningful data
if not data.startswith(self.PICKLE_PAD):
return {}
load = self.serial.loads(data[len(self.PICKLE_PAD):], raw=raw)
return load | [
"def",
"loads",
"(",
"self",
",",
"data",
",",
"raw",
"=",
"False",
")",
":",
"data",
"=",
"self",
".",
"decrypt",
"(",
"data",
")",
"# simple integrity check to verify that we got meaningful data",
"if",
"not",
"data",
".",
"startswith",
"(",
"self",
".",
"... | Decrypt and un-serialize a python object | [
"Decrypt",
"and",
"un",
"-",
"serialize",
"a",
"python",
"object"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/crypt.py#L1472-L1481 | train |
saltstack/salt | salt/pillar/hiera.py | ext_pillar | def ext_pillar(minion_id, # pylint: disable=W0613
pillar, # pylint: disable=W0613
conf):
'''
Execute hiera and return the data
'''
cmd = 'hiera -c {0}'.format(conf)
for key, val in six.iteritems(__grains__):
if isinstance(val, six.string_types):
cmd += ' {0}=\'{1}\''.format(key, val)
try:
data = salt.utils.yaml.safe_load(__salt__['cmd.run'](cmd))
except Exception:
log.critical('Hiera YAML data failed to parse from conf %s', conf)
return {}
return data | python | def ext_pillar(minion_id, # pylint: disable=W0613
pillar, # pylint: disable=W0613
conf):
'''
Execute hiera and return the data
'''
cmd = 'hiera -c {0}'.format(conf)
for key, val in six.iteritems(__grains__):
if isinstance(val, six.string_types):
cmd += ' {0}=\'{1}\''.format(key, val)
try:
data = salt.utils.yaml.safe_load(__salt__['cmd.run'](cmd))
except Exception:
log.critical('Hiera YAML data failed to parse from conf %s', conf)
return {}
return data | [
"def",
"ext_pillar",
"(",
"minion_id",
",",
"# pylint: disable=W0613",
"pillar",
",",
"# pylint: disable=W0613",
"conf",
")",
":",
"cmd",
"=",
"'hiera -c {0}'",
".",
"format",
"(",
"conf",
")",
"for",
"key",
",",
"val",
"in",
"six",
".",
"iteritems",
"(",
"_... | Execute hiera and return the data | [
"Execute",
"hiera",
"and",
"return",
"the",
"data"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/pillar/hiera.py#L29-L44 | train |
saltstack/salt | salt/modules/glusterfs.py | _gluster_output_cleanup | def _gluster_output_cleanup(result):
'''
Gluster versions prior to 6 have a bug that requires tricking
isatty. This adds "gluster> " to the output. Strip it off and
produce clean xml for ElementTree.
'''
ret = ''
for line in result.splitlines():
if line.startswith('gluster>'):
ret += line[9:].strip()
elif line.startswith('Welcome to gluster prompt'):
pass
else:
ret += line.strip()
return ret | python | def _gluster_output_cleanup(result):
'''
Gluster versions prior to 6 have a bug that requires tricking
isatty. This adds "gluster> " to the output. Strip it off and
produce clean xml for ElementTree.
'''
ret = ''
for line in result.splitlines():
if line.startswith('gluster>'):
ret += line[9:].strip()
elif line.startswith('Welcome to gluster prompt'):
pass
else:
ret += line.strip()
return ret | [
"def",
"_gluster_output_cleanup",
"(",
"result",
")",
":",
"ret",
"=",
"''",
"for",
"line",
"in",
"result",
".",
"splitlines",
"(",
")",
":",
"if",
"line",
".",
"startswith",
"(",
"'gluster>'",
")",
":",
"ret",
"+=",
"line",
"[",
"9",
":",
"]",
".",
... | Gluster versions prior to 6 have a bug that requires tricking
isatty. This adds "gluster> " to the output. Strip it off and
produce clean xml for ElementTree. | [
"Gluster",
"versions",
"prior",
"to",
"6",
"have",
"a",
"bug",
"that",
"requires",
"tricking",
"isatty",
".",
"This",
"adds",
"gluster",
">",
"to",
"the",
"output",
".",
"Strip",
"it",
"off",
"and",
"produce",
"clean",
"xml",
"for",
"ElementTree",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L51-L66 | train |
saltstack/salt | salt/modules/glusterfs.py | _gluster_xml | def _gluster_xml(cmd):
'''
Perform a gluster --xml command and log result.
'''
# We will pass the command string as stdin to allow for much longer
# command strings. This is especially useful for creating large volumes
# where the list of bricks exceeds 128 characters.
if _get_version() < (3, 6,):
result = __salt__['cmd.run'](
'script -q -c "gluster --xml --mode=script"', stdin="{0}\n\004".format(cmd)
)
else:
result = __salt__['cmd.run'](
'gluster --xml --mode=script', stdin="{0}\n".format(cmd)
)
try:
root = ET.fromstring(_gluster_output_cleanup(result))
except ET.ParseError:
raise CommandExecutionError('\n'.join(result.splitlines()[:-1]))
if _gluster_ok(root):
output = root.find('output')
if output is not None:
log.info('Gluster call "%s" succeeded: %s',
cmd,
root.find('output').text)
else:
log.info('Gluster call "%s" succeeded', cmd)
else:
log.error('Failed gluster call: %s: %s',
cmd,
root.find('opErrstr').text)
return root | python | def _gluster_xml(cmd):
'''
Perform a gluster --xml command and log result.
'''
# We will pass the command string as stdin to allow for much longer
# command strings. This is especially useful for creating large volumes
# where the list of bricks exceeds 128 characters.
if _get_version() < (3, 6,):
result = __salt__['cmd.run'](
'script -q -c "gluster --xml --mode=script"', stdin="{0}\n\004".format(cmd)
)
else:
result = __salt__['cmd.run'](
'gluster --xml --mode=script', stdin="{0}\n".format(cmd)
)
try:
root = ET.fromstring(_gluster_output_cleanup(result))
except ET.ParseError:
raise CommandExecutionError('\n'.join(result.splitlines()[:-1]))
if _gluster_ok(root):
output = root.find('output')
if output is not None:
log.info('Gluster call "%s" succeeded: %s',
cmd,
root.find('output').text)
else:
log.info('Gluster call "%s" succeeded', cmd)
else:
log.error('Failed gluster call: %s: %s',
cmd,
root.find('opErrstr').text)
return root | [
"def",
"_gluster_xml",
"(",
"cmd",
")",
":",
"# We will pass the command string as stdin to allow for much longer",
"# command strings. This is especially useful for creating large volumes",
"# where the list of bricks exceeds 128 characters.",
"if",
"_get_version",
"(",
")",
"<",
"(",
... | Perform a gluster --xml command and log result. | [
"Perform",
"a",
"gluster",
"--",
"xml",
"command",
"and",
"log",
"result",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L69-L103 | train |
saltstack/salt | salt/modules/glusterfs.py | _iter | def _iter(root, term):
'''
Checks for python2.6 or python2.7
'''
if sys.version_info < (2, 7):
return root.getiterator(term)
else:
return root.iter(term) | python | def _iter(root, term):
'''
Checks for python2.6 or python2.7
'''
if sys.version_info < (2, 7):
return root.getiterator(term)
else:
return root.iter(term) | [
"def",
"_iter",
"(",
"root",
",",
"term",
")",
":",
"if",
"sys",
".",
"version_info",
"<",
"(",
"2",
",",
"7",
")",
":",
"return",
"root",
".",
"getiterator",
"(",
"term",
")",
"else",
":",
"return",
"root",
".",
"iter",
"(",
"term",
")"
] | Checks for python2.6 or python2.7 | [
"Checks",
"for",
"python2",
".",
"6",
"or",
"python2",
".",
"7"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L120-L127 | train |
saltstack/salt | salt/modules/glusterfs.py | peer_status | def peer_status():
'''
Return peer status information
The return value is a dictionary with peer UUIDs as keys and dicts of peer
information as values. Hostnames are listed in one list. GlusterFS separates
one of the hostnames but the only reason for this seems to be which hostname
happens to be used first in peering.
CLI Example:
.. code-block:: bash
salt '*' glusterfs.peer_status
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer status
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
Number of Peers: 2
Hostname: ftp2
Port: 24007
Uuid: cbcb256b-e66e-4ec7-a718-21082d396c24
State: Peer in Cluster (Connected)
Hostname: ftp3
Uuid: 5ea10457-6cb2-427b-a770-7897509625e9
State: Peer in Cluster (Connected)
'''
root = _gluster_xml('peer status')
if not _gluster_ok(root):
return None
result = {}
for peer in _iter(root, 'peer'):
uuid = peer.find('uuid').text
result[uuid] = {'hostnames': []}
for item in peer:
if item.tag == 'hostname':
result[uuid]['hostnames'].append(item.text)
elif item.tag == 'hostnames':
for hostname in item:
if hostname.text not in result[uuid]['hostnames']:
result[uuid]['hostnames'].append(hostname.text)
elif item.tag != 'uuid':
result[uuid][item.tag] = item.text
return result | python | def peer_status():
'''
Return peer status information
The return value is a dictionary with peer UUIDs as keys and dicts of peer
information as values. Hostnames are listed in one list. GlusterFS separates
one of the hostnames but the only reason for this seems to be which hostname
happens to be used first in peering.
CLI Example:
.. code-block:: bash
salt '*' glusterfs.peer_status
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer status
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
Number of Peers: 2
Hostname: ftp2
Port: 24007
Uuid: cbcb256b-e66e-4ec7-a718-21082d396c24
State: Peer in Cluster (Connected)
Hostname: ftp3
Uuid: 5ea10457-6cb2-427b-a770-7897509625e9
State: Peer in Cluster (Connected)
'''
root = _gluster_xml('peer status')
if not _gluster_ok(root):
return None
result = {}
for peer in _iter(root, 'peer'):
uuid = peer.find('uuid').text
result[uuid] = {'hostnames': []}
for item in peer:
if item.tag == 'hostname':
result[uuid]['hostnames'].append(item.text)
elif item.tag == 'hostnames':
for hostname in item:
if hostname.text not in result[uuid]['hostnames']:
result[uuid]['hostnames'].append(hostname.text)
elif item.tag != 'uuid':
result[uuid][item.tag] = item.text
return result | [
"def",
"peer_status",
"(",
")",
":",
"root",
"=",
"_gluster_xml",
"(",
"'peer status'",
")",
"if",
"not",
"_gluster_ok",
"(",
"root",
")",
":",
"return",
"None",
"result",
"=",
"{",
"}",
"for",
"peer",
"in",
"_iter",
"(",
"root",
",",
"'peer'",
")",
... | Return peer status information
The return value is a dictionary with peer UUIDs as keys and dicts of peer
information as values. Hostnames are listed in one list. GlusterFS separates
one of the hostnames but the only reason for this seems to be which hostname
happens to be used first in peering.
CLI Example:
.. code-block:: bash
salt '*' glusterfs.peer_status
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer status
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
Number of Peers: 2
Hostname: ftp2
Port: 24007
Uuid: cbcb256b-e66e-4ec7-a718-21082d396c24
State: Peer in Cluster (Connected)
Hostname: ftp3
Uuid: 5ea10457-6cb2-427b-a770-7897509625e9
State: Peer in Cluster (Connected) | [
"Return",
"peer",
"status",
"information"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L130-L181 | train |
saltstack/salt | salt/modules/glusterfs.py | peer | def peer(name):
'''
Add another node into the peer list.
name
The remote host to probe.
CLI Example:
.. code-block:: bash
salt 'one.gluster.*' glusterfs.peer two
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer probe ftp2
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
#if the "peer" is the local host:
peer probe: success: on localhost not needed
#if the peer was just added:
peer probe: success
#if the peer was already part of the cluster:
peer probe: success: host ftp2 port 24007 already in peer list
'''
if salt.utils.cloud.check_name(name, 'a-zA-Z0-9._-'):
raise SaltInvocationError(
'Invalid characters in peer name "{0}"'.format(name))
cmd = 'peer probe {0}'.format(name)
return _gluster(cmd) | python | def peer(name):
'''
Add another node into the peer list.
name
The remote host to probe.
CLI Example:
.. code-block:: bash
salt 'one.gluster.*' glusterfs.peer two
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer probe ftp2
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
#if the "peer" is the local host:
peer probe: success: on localhost not needed
#if the peer was just added:
peer probe: success
#if the peer was already part of the cluster:
peer probe: success: host ftp2 port 24007 already in peer list
'''
if salt.utils.cloud.check_name(name, 'a-zA-Z0-9._-'):
raise SaltInvocationError(
'Invalid characters in peer name "{0}"'.format(name))
cmd = 'peer probe {0}'.format(name)
return _gluster(cmd) | [
"def",
"peer",
"(",
"name",
")",
":",
"if",
"salt",
".",
"utils",
".",
"cloud",
".",
"check_name",
"(",
"name",
",",
"'a-zA-Z0-9._-'",
")",
":",
"raise",
"SaltInvocationError",
"(",
"'Invalid characters in peer name \"{0}\"'",
".",
"format",
"(",
"name",
")",
... | Add another node into the peer list.
name
The remote host to probe.
CLI Example:
.. code-block:: bash
salt 'one.gluster.*' glusterfs.peer two
GLUSTER direct CLI example (to show what salt is sending to gluster):
$ gluster peer probe ftp2
GLUSTER CLI 3.4.4 return example (so we know what we are parsing):
#if the "peer" is the local host:
peer probe: success: on localhost not needed
#if the peer was just added:
peer probe: success
#if the peer was already part of the cluster:
peer probe: success: host ftp2 port 24007 already in peer list | [
"Add",
"another",
"node",
"into",
"the",
"peer",
"list",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L184-L219 | train |
saltstack/salt | salt/modules/glusterfs.py | create_volume | def create_volume(name, bricks, stripe=False, replica=False, device_vg=False,
transport='tcp', start=False, force=False, arbiter=False):
'''
Create a glusterfs volume
name
Name of the gluster volume
bricks
Bricks to create volume from, in <peer>:<brick path> format. For \
multiple bricks use list format: '["<peer1>:<brick1>", \
"<peer2>:<brick2>"]'
stripe
Stripe count, the number of bricks should be a multiple of the stripe \
count for a distributed striped volume
replica
Replica count, the number of bricks should be a multiple of the \
replica count for a distributed replicated volume
arbiter
If true, specifies volume should use arbiter brick(s). \
Valid configuration limited to "replica 3 arbiter 1" per \
Gluster documentation. Every third brick in the brick list \
is used as an arbiter brick.
.. versionadded:: 2019.2.0
device_vg
If true, specifies volume should use block backend instead of regular \
posix backend. Block device backend volume does not support multiple \
bricks
transport
Transport protocol to use, can be 'tcp', 'rdma' or 'tcp,rdma'
start
Start the volume after creation
force
Force volume creation, this works even if creating in root FS
CLI Examples:
.. code-block:: bash
salt host1 glusterfs.create newvolume host1:/brick
salt gluster1 glusterfs.create vol2 '["gluster1:/export/vol2/brick", \
"gluster2:/export/vol2/brick"]' replica=2 start=True
'''
# If single brick given as a string, accept it
if isinstance(bricks, six.string_types):
bricks = [bricks]
# Error for block devices with multiple bricks
if device_vg and len(bricks) > 1:
raise SaltInvocationError('Block device backend volume does not ' +
'support multiple bricks')
# Validate bricks syntax
for brick in bricks:
try:
peer_name, path = brick.split(':')
if not path.startswith('/'):
raise SaltInvocationError(
'Brick paths must start with / in {0}'.format(brick))
except ValueError:
raise SaltInvocationError(
'Brick syntax is <peer>:<path> got {0}'.format(brick))
# Validate arbiter config
if arbiter and replica != 3:
raise SaltInvocationError('Arbiter configuration only valid ' +
'in replica 3 volume')
# Format creation call
cmd = 'volume create {0} '.format(name)
if stripe:
cmd += 'stripe {0} '.format(stripe)
if replica:
cmd += 'replica {0} '.format(replica)
if arbiter:
cmd += 'arbiter 1 '
if device_vg:
cmd += 'device vg '
if transport != 'tcp':
cmd += 'transport {0} '.format(transport)
cmd += ' '.join(bricks)
if force:
cmd += ' force'
if not _gluster(cmd):
return False
if start:
return start_volume(name)
return True | python | def create_volume(name, bricks, stripe=False, replica=False, device_vg=False,
transport='tcp', start=False, force=False, arbiter=False):
'''
Create a glusterfs volume
name
Name of the gluster volume
bricks
Bricks to create volume from, in <peer>:<brick path> format. For \
multiple bricks use list format: '["<peer1>:<brick1>", \
"<peer2>:<brick2>"]'
stripe
Stripe count, the number of bricks should be a multiple of the stripe \
count for a distributed striped volume
replica
Replica count, the number of bricks should be a multiple of the \
replica count for a distributed replicated volume
arbiter
If true, specifies volume should use arbiter brick(s). \
Valid configuration limited to "replica 3 arbiter 1" per \
Gluster documentation. Every third brick in the brick list \
is used as an arbiter brick.
.. versionadded:: 2019.2.0
device_vg
If true, specifies volume should use block backend instead of regular \
posix backend. Block device backend volume does not support multiple \
bricks
transport
Transport protocol to use, can be 'tcp', 'rdma' or 'tcp,rdma'
start
Start the volume after creation
force
Force volume creation, this works even if creating in root FS
CLI Examples:
.. code-block:: bash
salt host1 glusterfs.create newvolume host1:/brick
salt gluster1 glusterfs.create vol2 '["gluster1:/export/vol2/brick", \
"gluster2:/export/vol2/brick"]' replica=2 start=True
'''
# If single brick given as a string, accept it
if isinstance(bricks, six.string_types):
bricks = [bricks]
# Error for block devices with multiple bricks
if device_vg and len(bricks) > 1:
raise SaltInvocationError('Block device backend volume does not ' +
'support multiple bricks')
# Validate bricks syntax
for brick in bricks:
try:
peer_name, path = brick.split(':')
if not path.startswith('/'):
raise SaltInvocationError(
'Brick paths must start with / in {0}'.format(brick))
except ValueError:
raise SaltInvocationError(
'Brick syntax is <peer>:<path> got {0}'.format(brick))
# Validate arbiter config
if arbiter and replica != 3:
raise SaltInvocationError('Arbiter configuration only valid ' +
'in replica 3 volume')
# Format creation call
cmd = 'volume create {0} '.format(name)
if stripe:
cmd += 'stripe {0} '.format(stripe)
if replica:
cmd += 'replica {0} '.format(replica)
if arbiter:
cmd += 'arbiter 1 '
if device_vg:
cmd += 'device vg '
if transport != 'tcp':
cmd += 'transport {0} '.format(transport)
cmd += ' '.join(bricks)
if force:
cmd += ' force'
if not _gluster(cmd):
return False
if start:
return start_volume(name)
return True | [
"def",
"create_volume",
"(",
"name",
",",
"bricks",
",",
"stripe",
"=",
"False",
",",
"replica",
"=",
"False",
",",
"device_vg",
"=",
"False",
",",
"transport",
"=",
"'tcp'",
",",
"start",
"=",
"False",
",",
"force",
"=",
"False",
",",
"arbiter",
"=",
... | Create a glusterfs volume
name
Name of the gluster volume
bricks
Bricks to create volume from, in <peer>:<brick path> format. For \
multiple bricks use list format: '["<peer1>:<brick1>", \
"<peer2>:<brick2>"]'
stripe
Stripe count, the number of bricks should be a multiple of the stripe \
count for a distributed striped volume
replica
Replica count, the number of bricks should be a multiple of the \
replica count for a distributed replicated volume
arbiter
If true, specifies volume should use arbiter brick(s). \
Valid configuration limited to "replica 3 arbiter 1" per \
Gluster documentation. Every third brick in the brick list \
is used as an arbiter brick.
.. versionadded:: 2019.2.0
device_vg
If true, specifies volume should use block backend instead of regular \
posix backend. Block device backend volume does not support multiple \
bricks
transport
Transport protocol to use, can be 'tcp', 'rdma' or 'tcp,rdma'
start
Start the volume after creation
force
Force volume creation, this works even if creating in root FS
CLI Examples:
.. code-block:: bash
salt host1 glusterfs.create newvolume host1:/brick
salt gluster1 glusterfs.create vol2 '["gluster1:/export/vol2/brick", \
"gluster2:/export/vol2/brick"]' replica=2 start=True | [
"Create",
"a",
"glusterfs",
"volume"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L222-L320 | train |
saltstack/salt | salt/modules/glusterfs.py | list_volumes | def list_volumes():
'''
List configured volumes
CLI Example:
.. code-block:: bash
salt '*' glusterfs.list_volumes
'''
root = _gluster_xml('volume list')
if not _gluster_ok(root):
return None
results = [x.text for x in _iter(root, 'volume')]
return results | python | def list_volumes():
'''
List configured volumes
CLI Example:
.. code-block:: bash
salt '*' glusterfs.list_volumes
'''
root = _gluster_xml('volume list')
if not _gluster_ok(root):
return None
results = [x.text for x in _iter(root, 'volume')]
return results | [
"def",
"list_volumes",
"(",
")",
":",
"root",
"=",
"_gluster_xml",
"(",
"'volume list'",
")",
"if",
"not",
"_gluster_ok",
"(",
"root",
")",
":",
"return",
"None",
"results",
"=",
"[",
"x",
".",
"text",
"for",
"x",
"in",
"_iter",
"(",
"root",
",",
"'v... | List configured volumes
CLI Example:
.. code-block:: bash
salt '*' glusterfs.list_volumes | [
"List",
"configured",
"volumes"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L323-L338 | train |
saltstack/salt | salt/modules/glusterfs.py | status | def status(name):
'''
Check the status of a gluster volume.
name
Volume name
CLI Example:
.. code-block:: bash
salt '*' glusterfs.status myvolume
'''
# Get volume status
root = _gluster_xml('volume status {0}'.format(name))
if not _gluster_ok(root):
# Most probably non-existing volume, the error output is logged
# This return value is easy to test and intuitive
return None
ret = {'bricks': {}, 'nfs': {}, 'healers': {}}
def etree_legacy_wrap(t):
ret = _etree_to_dict(t)
ret['online'] = (ret['status'] == '1')
ret['host'] = ret['hostname']
return ret
# Build a hash to map hostname to peerid
hostref = {}
for node in _iter(root, 'node'):
peerid = node.find('peerid').text
hostname = node.find('hostname').text
if hostname not in ('NFS Server', 'Self-heal Daemon'):
hostref[peerid] = hostname
for node in _iter(root, 'node'):
hostname = node.find('hostname').text
if hostname not in ('NFS Server', 'Self-heal Daemon'):
path = node.find('path').text
ret['bricks'][
'{0}:{1}'.format(hostname, path)] = etree_legacy_wrap(node)
elif hostname == 'NFS Server':
peerid = node.find('peerid').text
true_hostname = hostref[peerid]
ret['nfs'][true_hostname] = etree_legacy_wrap(node)
else:
peerid = node.find('peerid').text
true_hostname = hostref[peerid]
ret['healers'][true_hostname] = etree_legacy_wrap(node)
return ret | python | def status(name):
'''
Check the status of a gluster volume.
name
Volume name
CLI Example:
.. code-block:: bash
salt '*' glusterfs.status myvolume
'''
# Get volume status
root = _gluster_xml('volume status {0}'.format(name))
if not _gluster_ok(root):
# Most probably non-existing volume, the error output is logged
# This return value is easy to test and intuitive
return None
ret = {'bricks': {}, 'nfs': {}, 'healers': {}}
def etree_legacy_wrap(t):
ret = _etree_to_dict(t)
ret['online'] = (ret['status'] == '1')
ret['host'] = ret['hostname']
return ret
# Build a hash to map hostname to peerid
hostref = {}
for node in _iter(root, 'node'):
peerid = node.find('peerid').text
hostname = node.find('hostname').text
if hostname not in ('NFS Server', 'Self-heal Daemon'):
hostref[peerid] = hostname
for node in _iter(root, 'node'):
hostname = node.find('hostname').text
if hostname not in ('NFS Server', 'Self-heal Daemon'):
path = node.find('path').text
ret['bricks'][
'{0}:{1}'.format(hostname, path)] = etree_legacy_wrap(node)
elif hostname == 'NFS Server':
peerid = node.find('peerid').text
true_hostname = hostref[peerid]
ret['nfs'][true_hostname] = etree_legacy_wrap(node)
else:
peerid = node.find('peerid').text
true_hostname = hostref[peerid]
ret['healers'][true_hostname] = etree_legacy_wrap(node)
return ret | [
"def",
"status",
"(",
"name",
")",
":",
"# Get volume status",
"root",
"=",
"_gluster_xml",
"(",
"'volume status {0}'",
".",
"format",
"(",
"name",
")",
")",
"if",
"not",
"_gluster_ok",
"(",
"root",
")",
":",
"# Most probably non-existing volume, the error output is... | Check the status of a gluster volume.
name
Volume name
CLI Example:
.. code-block:: bash
salt '*' glusterfs.status myvolume | [
"Check",
"the",
"status",
"of",
"a",
"gluster",
"volume",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L341-L392 | train |
saltstack/salt | salt/modules/glusterfs.py | info | def info(name=None):
'''
.. versionadded:: 2015.8.4
Return gluster volume info.
name
Optional name to retrieve only information of one volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.info
'''
cmd = 'volume info'
if name is not None:
cmd += ' ' + name
root = _gluster_xml(cmd)
if not _gluster_ok(root):
return None
ret = {}
for volume in _iter(root, 'volume'):
name = volume.find('name').text
ret[name] = _etree_to_dict(volume)
bricks = {}
for i, brick in enumerate(_iter(volume, 'brick'), start=1):
brickkey = 'brick{0}'.format(i)
bricks[brickkey] = {'path': brick.text}
for child in brick:
if not child.tag == 'name':
bricks[brickkey].update({child.tag: child.text})
for k, v in brick.items():
bricks[brickkey][k] = v
ret[name]['bricks'] = bricks
options = {}
for option in _iter(volume, 'option'):
options[option.find('name').text] = option.find('value').text
ret[name]['options'] = options
return ret | python | def info(name=None):
'''
.. versionadded:: 2015.8.4
Return gluster volume info.
name
Optional name to retrieve only information of one volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.info
'''
cmd = 'volume info'
if name is not None:
cmd += ' ' + name
root = _gluster_xml(cmd)
if not _gluster_ok(root):
return None
ret = {}
for volume in _iter(root, 'volume'):
name = volume.find('name').text
ret[name] = _etree_to_dict(volume)
bricks = {}
for i, brick in enumerate(_iter(volume, 'brick'), start=1):
brickkey = 'brick{0}'.format(i)
bricks[brickkey] = {'path': brick.text}
for child in brick:
if not child.tag == 'name':
bricks[brickkey].update({child.tag: child.text})
for k, v in brick.items():
bricks[brickkey][k] = v
ret[name]['bricks'] = bricks
options = {}
for option in _iter(volume, 'option'):
options[option.find('name').text] = option.find('value').text
ret[name]['options'] = options
return ret | [
"def",
"info",
"(",
"name",
"=",
"None",
")",
":",
"cmd",
"=",
"'volume info'",
"if",
"name",
"is",
"not",
"None",
":",
"cmd",
"+=",
"' '",
"+",
"name",
"root",
"=",
"_gluster_xml",
"(",
"cmd",
")",
"if",
"not",
"_gluster_ok",
"(",
"root",
")",
":"... | .. versionadded:: 2015.8.4
Return gluster volume info.
name
Optional name to retrieve only information of one volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.info | [
"..",
"versionadded",
"::",
"2015",
".",
"8",
".",
"4"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L395-L439 | train |
saltstack/salt | salt/modules/glusterfs.py | start_volume | def start_volume(name, force=False):
'''
Start a gluster volume
name
Volume name
force
Force the volume start even if the volume is started
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.start mycluster
'''
cmd = 'volume start {0}'.format(name)
if force:
cmd = '{0} force'.format(cmd)
volinfo = info(name)
if name not in volinfo:
log.error("Cannot start non-existing volume %s", name)
return False
if not force and volinfo[name]['status'] == '1':
log.info("Volume %s already started", name)
return True
return _gluster(cmd) | python | def start_volume(name, force=False):
'''
Start a gluster volume
name
Volume name
force
Force the volume start even if the volume is started
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.start mycluster
'''
cmd = 'volume start {0}'.format(name)
if force:
cmd = '{0} force'.format(cmd)
volinfo = info(name)
if name not in volinfo:
log.error("Cannot start non-existing volume %s", name)
return False
if not force and volinfo[name]['status'] == '1':
log.info("Volume %s already started", name)
return True
return _gluster(cmd) | [
"def",
"start_volume",
"(",
"name",
",",
"force",
"=",
"False",
")",
":",
"cmd",
"=",
"'volume start {0}'",
".",
"format",
"(",
"name",
")",
"if",
"force",
":",
"cmd",
"=",
"'{0} force'",
".",
"format",
"(",
"cmd",
")",
"volinfo",
"=",
"info",
"(",
"... | Start a gluster volume
name
Volume name
force
Force the volume start even if the volume is started
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.start mycluster | [
"Start",
"a",
"gluster",
"volume"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L442-L472 | train |
saltstack/salt | salt/modules/glusterfs.py | stop_volume | def stop_volume(name, force=False):
'''
Stop a gluster volume
name
Volume name
force
Force stop the volume
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.stop_volume mycluster
'''
volinfo = info()
if name not in volinfo:
log.error('Cannot stop non-existing volume %s', name)
return False
if int(volinfo[name]['status']) != 1:
log.warning('Attempt to stop already stopped volume %s', name)
return True
cmd = 'volume stop {0}'.format(name)
if force:
cmd += ' force'
return _gluster(cmd) | python | def stop_volume(name, force=False):
'''
Stop a gluster volume
name
Volume name
force
Force stop the volume
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.stop_volume mycluster
'''
volinfo = info()
if name not in volinfo:
log.error('Cannot stop non-existing volume %s', name)
return False
if int(volinfo[name]['status']) != 1:
log.warning('Attempt to stop already stopped volume %s', name)
return True
cmd = 'volume stop {0}'.format(name)
if force:
cmd += ' force'
return _gluster(cmd) | [
"def",
"stop_volume",
"(",
"name",
",",
"force",
"=",
"False",
")",
":",
"volinfo",
"=",
"info",
"(",
")",
"if",
"name",
"not",
"in",
"volinfo",
":",
"log",
".",
"error",
"(",
"'Cannot stop non-existing volume %s'",
",",
"name",
")",
"return",
"False",
"... | Stop a gluster volume
name
Volume name
force
Force stop the volume
.. versionadded:: 2015.8.4
CLI Example:
.. code-block:: bash
salt '*' glusterfs.stop_volume mycluster | [
"Stop",
"a",
"gluster",
"volume"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L475-L505 | train |
saltstack/salt | salt/modules/glusterfs.py | delete_volume | def delete_volume(target, stop=True):
'''
Deletes a gluster volume
target
Volume to delete
stop : True
If ``True``, stop volume before delete
CLI Example:
.. code-block:: bash
salt '*' glusterfs.delete_volume <volume>
'''
volinfo = info()
if target not in volinfo:
log.error('Cannot delete non-existing volume %s', target)
return False
# Stop volume if requested to and it is running
running = (volinfo[target]['status'] == '1')
if not stop and running:
# Fail if volume is running if stop is not requested
log.error('Volume %s must be stopped before deletion', target)
return False
if running:
if not stop_volume(target, force=True):
return False
cmd = 'volume delete {0}'.format(target)
return _gluster(cmd) | python | def delete_volume(target, stop=True):
'''
Deletes a gluster volume
target
Volume to delete
stop : True
If ``True``, stop volume before delete
CLI Example:
.. code-block:: bash
salt '*' glusterfs.delete_volume <volume>
'''
volinfo = info()
if target not in volinfo:
log.error('Cannot delete non-existing volume %s', target)
return False
# Stop volume if requested to and it is running
running = (volinfo[target]['status'] == '1')
if not stop and running:
# Fail if volume is running if stop is not requested
log.error('Volume %s must be stopped before deletion', target)
return False
if running:
if not stop_volume(target, force=True):
return False
cmd = 'volume delete {0}'.format(target)
return _gluster(cmd) | [
"def",
"delete_volume",
"(",
"target",
",",
"stop",
"=",
"True",
")",
":",
"volinfo",
"=",
"info",
"(",
")",
"if",
"target",
"not",
"in",
"volinfo",
":",
"log",
".",
"error",
"(",
"'Cannot delete non-existing volume %s'",
",",
"target",
")",
"return",
"Fal... | Deletes a gluster volume
target
Volume to delete
stop : True
If ``True``, stop volume before delete
CLI Example:
.. code-block:: bash
salt '*' glusterfs.delete_volume <volume> | [
"Deletes",
"a",
"gluster",
"volume"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L508-L542 | train |
saltstack/salt | salt/modules/glusterfs.py | add_volume_bricks | def add_volume_bricks(name, bricks):
'''
Add brick(s) to an existing volume
name
Volume name
bricks
List of bricks to add to the volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.add_volume_bricks <volume> <bricks>
'''
volinfo = info()
if name not in volinfo:
log.error('Volume %s does not exist, cannot add bricks', name)
return False
new_bricks = []
cmd = 'volume add-brick {0}'.format(name)
if isinstance(bricks, six.string_types):
bricks = [bricks]
volume_bricks = [x['path'] for x in volinfo[name]['bricks'].values()]
for brick in bricks:
if brick in volume_bricks:
log.debug(
'Brick %s already in volume %s...excluding from command',
brick,
name)
else:
new_bricks.append(brick)
if new_bricks:
for brick in new_bricks:
cmd += ' {0}'.format(brick)
return _gluster(cmd)
return True | python | def add_volume_bricks(name, bricks):
'''
Add brick(s) to an existing volume
name
Volume name
bricks
List of bricks to add to the volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.add_volume_bricks <volume> <bricks>
'''
volinfo = info()
if name not in volinfo:
log.error('Volume %s does not exist, cannot add bricks', name)
return False
new_bricks = []
cmd = 'volume add-brick {0}'.format(name)
if isinstance(bricks, six.string_types):
bricks = [bricks]
volume_bricks = [x['path'] for x in volinfo[name]['bricks'].values()]
for brick in bricks:
if brick in volume_bricks:
log.debug(
'Brick %s already in volume %s...excluding from command',
brick,
name)
else:
new_bricks.append(brick)
if new_bricks:
for brick in new_bricks:
cmd += ' {0}'.format(brick)
return _gluster(cmd)
return True | [
"def",
"add_volume_bricks",
"(",
"name",
",",
"bricks",
")",
":",
"volinfo",
"=",
"info",
"(",
")",
"if",
"name",
"not",
"in",
"volinfo",
":",
"log",
".",
"error",
"(",
"'Volume %s does not exist, cannot add bricks'",
",",
"name",
")",
"return",
"False",
"ne... | Add brick(s) to an existing volume
name
Volume name
bricks
List of bricks to add to the volume
CLI Example:
.. code-block:: bash
salt '*' glusterfs.add_volume_bricks <volume> <bricks> | [
"Add",
"brick",
"(",
"s",
")",
"to",
"an",
"existing",
"volume"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L545-L589 | train |
saltstack/salt | salt/modules/glusterfs.py | set_quota_volume | def set_quota_volume(name, path, size, enable_quota=False):
'''
Set quota to glusterfs volume.
name
Name of the gluster volume
path
Folder path for restriction in volume ("/")
size
Hard-limit size of the volume (MB/GB)
enable_quota
Enable quota before set up restriction
CLI Example:
.. code-block:: bash
salt '*' glusterfs.set_quota_volume <volume> <path> <size> enable_quota=True
'''
cmd = 'volume quota {0}'.format(name)
if path:
cmd += ' limit-usage {0}'.format(path)
if size:
cmd += ' {0}'.format(size)
if enable_quota:
if not enable_quota_volume(name):
pass
if not _gluster(cmd):
return False
return True | python | def set_quota_volume(name, path, size, enable_quota=False):
'''
Set quota to glusterfs volume.
name
Name of the gluster volume
path
Folder path for restriction in volume ("/")
size
Hard-limit size of the volume (MB/GB)
enable_quota
Enable quota before set up restriction
CLI Example:
.. code-block:: bash
salt '*' glusterfs.set_quota_volume <volume> <path> <size> enable_quota=True
'''
cmd = 'volume quota {0}'.format(name)
if path:
cmd += ' limit-usage {0}'.format(path)
if size:
cmd += ' {0}'.format(size)
if enable_quota:
if not enable_quota_volume(name):
pass
if not _gluster(cmd):
return False
return True | [
"def",
"set_quota_volume",
"(",
"name",
",",
"path",
",",
"size",
",",
"enable_quota",
"=",
"False",
")",
":",
"cmd",
"=",
"'volume quota {0}'",
".",
"format",
"(",
"name",
")",
"if",
"path",
":",
"cmd",
"+=",
"' limit-usage {0}'",
".",
"format",
"(",
"p... | Set quota to glusterfs volume.
name
Name of the gluster volume
path
Folder path for restriction in volume ("/")
size
Hard-limit size of the volume (MB/GB)
enable_quota
Enable quota before set up restriction
CLI Example:
.. code-block:: bash
salt '*' glusterfs.set_quota_volume <volume> <path> <size> enable_quota=True | [
"Set",
"quota",
"to",
"glusterfs",
"volume",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/glusterfs.py#L632-L665 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.