convert netapi calls to client calls

This commit is contained in:
Vernon Cole 2017-10-12 20:04:21 -06:00
parent 2261f0b3c5
commit 33fb7331a9
No known key found for this signature in database
GPG Key ID: C4FE8A79AB2E140C

View File

@ -19,7 +19,7 @@ import logging
# Import salt libs
import salt.utils.cloud
import salt.config as config
import salt.netapi
import salt.client
import salt.ext.six as six
if six.PY3:
import ipaddress
@ -32,6 +32,7 @@ from salt.exceptions import SaltCloudException, SaltCloudSystemExit
log = logging.getLogger(__name__)
try:
# noinspection PyUnresolvedReferences
from impacket.smbconnection import SessionError as smbSessionError
from impacket.smb3 import SessionError as smb3SessionError
HAS_IMPACKET = True
@ -39,7 +40,9 @@ except ImportError:
HAS_IMPACKET = False
try:
# noinspection PyUnresolvedReferences
from winrm.exceptions import WinRMTransportError
# noinspection PyUnresolvedReferences
from requests.exceptions import (
ConnectionError, ConnectTimeout, ReadTimeout, SSLError,
ProxyError, RetryError, InvalidSchema)
@ -55,24 +58,6 @@ def __virtual__():
return True
def _get_connection_info():
'''
Return connection information for the passed VM data
'''
vm_ = get_configured_provider()
try:
ret = {'username': vm_['username'],
'password': vm_['password'],
'eauth': vm_['eauth'],
'vm': vm_,
}
except KeyError:
raise SaltCloudException(
'Configuration must define salt-api "username", "password" and "eauth"')
return ret
def avail_locations(call=None):
'''
This function returns a list of locations available.
@ -81,7 +66,7 @@ def avail_locations(call=None):
salt-cloud --list-locations my-cloud-provider
[ saltify will always returns an empty dictionary ]
[ saltify will always return an empty dictionary ]
'''
return {}
@ -127,8 +112,6 @@ def list_nodes(call=None):
returns a list of dictionaries of defined standard fields.
salt-api setup required for operation.
..versionadded:: Oxygen
'''
@ -172,8 +155,8 @@ def list_nodes_full(call=None):
salt-cloud -F
returns a list of dictionaries.
for 'saltify' minions, returns dict of grains (enhanced).
salt-api setup required for operation.
..versionadded:: Oxygen
'''
@ -200,16 +183,9 @@ def _list_nodes_full(call=None):
'''
List the nodes, ask all 'saltify' minions, return dict of grains.
'''
local = salt.netapi.NetapiClient(__opts__)
cmd = {'client': 'local',
'tgt': 'salt-cloud:driver:saltify',
'fun': 'grains.items',
'arg': '',
'tgt_type': 'grain',
}
cmd.update(_get_connection_info())
return local.run(cmd)
local = salt.client.LocalClient()
return local.cmd('salt-cloud:driver:saltify', 'grains.items', '',
tgt_type='grain')
def list_nodes_select(call=None):
@ -226,15 +202,8 @@ def show_instance(name, call=None):
'''
List the a single node, return dict of grains.
'''
local = salt.netapi.NetapiClient(__opts__)
cmd = {'client': 'local',
'tgt': 'name',
'fun': 'grains.items',
'arg': '',
'tgt_type': 'glob',
}
cmd.update(_get_connection_info())
ret = local.run(cmd)
local = salt.client.LocalClient()
ret = local.cmd(name, 'grains.items')
ret.update(_build_required_items(ret))
return ret
@ -365,14 +334,21 @@ def destroy(name, call=None):
.. versionadded:: Oxygen
Disconnect a minion from the master, and remove its keys.
Optionally, (if ``remove_config_on_destroy`` is ``True``),
disables salt-minion from running on the minion, and
erases the Salt configuration files from it.
Optionally, (if ``shutdown_on_destroy`` is ``True``),
orders the minion to halt.
CLI Example:
.. code-block:: bash
salt-cloud --destroy mymachine
salt-api setup required for operation.
'''
if call == 'function':
raise SaltCloudSystemExit(
@ -391,15 +367,9 @@ def destroy(name, call=None):
transport=opts['transport']
)
local = salt.netapi.NetapiClient(opts)
cmd = {'client': 'local',
'tgt': name,
'fun': 'grains.get',
'arg': ['salt-cloud'],
}
cmd.update(_get_connection_info())
vm_ = cmd['vm']
my_info = local.run(cmd)
vm_ = get_configured_provider()
local = salt.client.LocalClient()
my_info = local.cmd(name, 'grains.get', ['salt-cloud'])
try:
vm_.update(my_info[name]) # get profile name to get config value
except (IndexError, TypeError):
@ -407,25 +377,22 @@ def destroy(name, call=None):
if config.get_cloud_config_value(
'remove_config_on_destroy', vm_, opts, default=True
):
cmd.update({'fun': 'service.disable', 'arg': ['salt-minion']})
ret = local.run(cmd) # prevent generating new keys on restart
ret = local.cmd(name, # prevent generating new keys on restart
'service.disable',
['salt-minion'])
if ret and ret[name]:
log.info('disabled salt-minion service on %s', name)
cmd.update({'fun': 'config.get', 'arg': ['conf_file']})
ret = local.run(cmd)
ret = local.cmd(name, 'config.get', ['conf_file'])
if ret and ret[name]:
confile = ret[name]
cmd.update({'fun': 'file.remove', 'arg': [confile]})
ret = local.run(cmd)
ret = local.cmd(name, 'file.remove', [confile])
if ret and ret[name]:
log.info('removed minion %s configuration file %s',
name, confile)
cmd.update({'fun': 'config.get', 'arg': ['pki_dir']})
ret = local.run(cmd)
ret = local.cmd(name, 'config.get', ['pki_dir'])
if ret and ret[name]:
pki_dir = ret[name]
cmd.update({'fun': 'file.remove', 'arg': [pki_dir]})
ret = local.run(cmd)
ret = local.cmd(name, 'file.remove', [pki_dir])
if ret and ret[name]:
log.info(
'removed minion %s key files in %s',
@ -435,8 +402,7 @@ def destroy(name, call=None):
if config.get_cloud_config_value(
'shutdown_on_destroy', vm_, opts, default=False
):
cmd.update({'fun': 'system.shutdown', 'arg': ''})
ret = local.run(cmd)
ret = local.cmd(name, 'system.shutdown')
if ret and ret[name]:
log.info('system.shutdown for minion %s successful', name)
@ -456,8 +422,6 @@ def reboot(name, call=None):
'''
Reboot a saltify minion.
salt-api setup required for operation.
..versionadded:: Oxygen
name
@ -475,13 +439,5 @@ def reboot(name, call=None):
'The reboot action must be called with -a or --action.'
)
local = salt.netapi.NetapiClient(__opts__)
cmd = {'client': 'local',
'tgt': name,
'fun': 'system.reboot',
'arg': '',
}
cmd.update(_get_connection_info())
ret = local.run(cmd)
return ret
local = salt.client.LocalClient()
return local.cmd(name, 'system.reboot')