-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
306 lines (247 loc) · 10.2 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright 2009 Shikhar Bhushan
# Copyright 2011 Leonidas Poulopoulos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module is a thin layer of abstraction around the library.
It exposes all core functionality.
"""
import capabilities
import operations
import transport
import logging
from ncclient.xml_ import *
logger = logging.getLogger('ncclient.manager')
OPERATIONS = {
"get": operations.Get,
"get_config": operations.GetConfig,
"dispatch": operations.Dispatch,
"edit_config": operations.EditConfig,
"copy_config": operations.CopyConfig,
"validate": operations.Validate,
"commit": operations.Commit,
"discard_changes": operations.DiscardChanges,
"delete_config": operations.DeleteConfig,
"lock": operations.Lock,
"unlock": operations.Unlock,
"close_session": operations.CloseSession,
"kill_session": operations.KillSession,
"poweroff_machine": operations.PoweroffMachine,
"reboot_machine": operations.RebootMachine,
}
"""
Dictionary of base method names and corresponding :class:`~ncclient.operations.RPC`
subclasses. It is used to lookup operations, e.g. `get_config` is mapped to
:class:`~ncclient.operations.GetConfig`. It is thus possible to add additional
operations to the :class:`Manager` API.
"""
VENDOR_OPERATIONS = {}
def make_device_handler(device_params):
"""
Create a device handler object that provides device specific parameters and
functions, which are called in various places throughout our code.
If no device_params are defined or the "name" in the parameter dict is not
known then a default handler will be returned.
"""
if device_params is None:
device_params = {}
device_name = device_params.get("name", "default")
# Attempt to import device handler class. All device handlers are
# in a module called "ncclient.devices.<devicename>" and in a class named
# "<devicename>DeviceHandler", with the first letter capitalized.
class_name = "%sDeviceHandler" % device_name.capitalize()
devices_module_name = "ncclient.devices.%s" % device_name
dev_module_obj = __import__(devices_module_name)
handler_module_obj = getattr(getattr(dev_module_obj, "devices"), device_name)
class_obj = getattr(handler_module_obj, class_name)
handler_obj = class_obj(device_params)
return handler_obj
def connect_ssh(*args, **kwds):
"""
Initialize a :class:`Manager` over the SSH transport.
For documentation of arguments see :meth:`ncclient.transport.SSHSession.connect`.
The underlying :class:`ncclient.transport.SSHSession` is created with
:data:`CAPABILITIES`. It is first instructed to
:meth:`~ncclient.transport.SSHSession.load_known_hosts` and then
all the provided arguments are passed directly to its implementation
of :meth:`~ncclient.transport.SSHSession.connect`.
To invoke advanced vendor related operation add device_params =
{'name':'<vendor_alias>'} in connection paramerers. For the time,
'junos' and 'nexus' are supported for Juniper and Cisco Nexus respectively.
"""
# Extract device parameter dict, if it was passed into this function. Need to
# remove it from kwds, since the session.connect() doesn't like extra stuff in
# there.
if "device_params" in kwds:
device_params = kwds["device_params"]
del kwds["device_params"]
else:
device_params = None
device_handler = make_device_handler(device_params)
device_handler.add_additional_ssh_connect_params(kwds)
global VENDOR_OPERATIONS
VENDOR_OPERATIONS.update(device_handler.add_additional_operations())
session = transport.SSHSession(device_handler)
session.load_known_hosts()
try:
session.connect(*args, **kwds)
except Exception as ex:
if session.transport:
session.close()
raise ex
return Manager(session, device_handler, **kwds)
def connect_ioproc(*args, **kwds):
if "device_params" in kwds:
device_params = kwds["device_params"]
del kwds["device_params"]
import_string = 'ncclient.transport.third_party.'
import_string += device_params['name'] + '.ioproc'
third_party_import = __import__(import_string, fromlist=['IOProc'])
else:
device_params = None
device_handler = make_device_handler(device_params)
global VENDOR_OPERATIONS
VENDOR_OPERATIONS.update(device_handler.add_additional_operations())
session = third_party_import.IOProc(device_handler)
session.connect()
return Manager(session, device_handler, **kwds)
def connect(*args, **kwds):
if "host" in kwds:
host = kwds["host"]
if host != 'localhost':
return connect_ssh(*args, **kwds)
else:
return connect_ioproc(*args, **kwds)
class OpExecutor(type):
def __new__(cls, name, bases, attrs):
def make_wrapper(op_cls):
def wrapper(self, *args, **kwds):
return self.execute(op_cls, *args, **kwds)
wrapper.func_doc = op_cls.request.func_doc
return wrapper
for op_name, op_cls in OPERATIONS.iteritems():
attrs[op_name] = make_wrapper(op_cls)
return super(OpExecutor, cls).__new__(cls, name, bases, attrs)
def __call__(cls, *args, **kwargs):
def make_wrapper(op_cls):
def wrapper(self, *args, **kwds):
return self.execute(op_cls, *args, **kwds)
wrapper.func_doc = op_cls.request.func_doc
return wrapper
if VENDOR_OPERATIONS:
for op_name, op_cls in VENDOR_OPERATIONS.iteritems():
setattr(cls, op_name, make_wrapper(op_cls))
return super(OpExecutor, cls).__call__(*args, **kwargs)
class Manager(object):
"""
For details on the expected behavior of the operations and their
parameters refer to :rfc:`4741`.
Manager instances are also context managers so you can use it like this::
with manager.connect("host") as m:
# do your stuff
... or like this::
m = manager.connect("host")
try:
# do your stuff
finally:
m.close_session()
"""
__metaclass__ = OpExecutor
def __init__(self, session, device_handler, timeout=30, *args, **kwargs):
self._session = session
self._async_mode = False
self._timeout = timeout
self._raise_mode = operations.RaiseMode.ALL
self._device_handler = device_handler
def __enter__(self):
return self
def __exit__(self, *args):
self.close_session()
return False
def __set_timeout(self, timeout):
self._timeout = timeout
def __set_async_mode(self, mode):
self._async_mode = mode
def __set_raise_mode(self, mode):
assert(mode in (operations.RaiseMode.NONE, operations.RaiseMode.ERRORS, operations.RaiseMode.ALL))
self._raise_mode = mode
def execute(self, cls, *args, **kwds):
return cls(self._session,
device_handler=self._device_handler,
async=self._async_mode,
timeout=self._timeout,
raise_mode=self._raise_mode).request(*args, **kwds)
def locked(self, target):
"""Returns a context manager for a lock on a datastore, where
*target* is the name of the configuration datastore to lock, e.g.::
with m.locked("running"):
# do your stuff
... instead of::
m.lock("running")
try:
# do your stuff
finally:
m.unlock("running")
"""
return operations.LockContext(self._session, self._device_handler, target)
def scp(self):
return self._session.scp()
def session(self):
raise NotImplementedError
def __getattr__(self, method):
"""Parse args/kwargs correctly in order to build XML element"""
def _missing(*args, **kwargs):
m = method.replace('_', '-')
root = new_ele(m)
if args:
for arg in args:
sub_ele(root, arg)
r = self.rpc(root)
return r
return _missing
@property
def client_capabilities(self):
""":class:`~ncclient.capabilities.Capabilities` object representing
the client's capabilities."""
return self._session._client_capabilities
@property
def server_capabilities(self):
""":class:`~ncclient.capabilities.Capabilities` object representing
the server's capabilities."""
return self._session._server_capabilities
@property
def channel_id(self):
return self._session._channel_id
@property
def channel_name(self):
return self._session._channel_name
@property
def session_id(self):
"""`session-id` assigned by the NETCONF server."""
return self._session.id
@property
def connected(self):
"""Whether currently connected to the NETCONF server."""
return self._session.connected
async_mode = property(fget=lambda self: self._async_mode,
fset=__set_async_mode)
"""Specify whether operations are executed asynchronously (`True`) or
synchronously (`False`) (the default)."""
timeout = property(fget=lambda self: self._timeout, fset=__set_timeout)
"""Specify the timeout for synchronous RPC requests."""
raise_mode = property(fget=lambda self: self._raise_mode,
fset=__set_raise_mode)
"""Specify which errors are raised as :exc:`~ncclient.operations.RPCError`
exceptions. Valid values are the constants defined in
:class:`~ncclient.operations.RaiseMode`.
The default value is :attr:`~ncclient.operations.RaiseMode.ALL`."""