root / ncclient / manager.py @ 53e1a046
History | View | Annotate | Download (6.8 kB)
1 |
# Copyright 2009 Shikhar Bhushan
|
---|---|
2 |
#
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
# you may not use this file except in compliance with the License.
|
5 |
# You may obtain a copy of the License at
|
6 |
#
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
#
|
9 |
# Unless required by applicable law or agreed to in writing, software
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
# See the License for the specific language governing permissions and
|
13 |
# limitations under the License.
|
14 |
|
15 |
"""This module is a thin layer of abstraction around the library. It exposes all core functionality."""
|
16 |
|
17 |
import capabilities |
18 |
import operations |
19 |
import transport |
20 |
|
21 |
import logging |
22 |
|
23 |
logger = logging.getLogger('ncclient.manager')
|
24 |
|
25 |
CAPABILITIES = [ |
26 |
"urn:ietf:params:netconf:base:1.0",
|
27 |
"urn:ietf:params:netconf:capability:writable-running:1.0",
|
28 |
"urn:ietf:params:netconf:capability:candidate:1.0",
|
29 |
"urn:ietf:params:netconf:capability:confirmed-commit:1.0",
|
30 |
"urn:ietf:params:netconf:capability:rollback-on-error:1.0",
|
31 |
"urn:ietf:params:netconf:capability:startup:1.0",
|
32 |
"urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp",
|
33 |
"urn:ietf:params:netconf:capability:validate:1.0",
|
34 |
"urn:ietf:params:netconf:capability:xpath:1.0",
|
35 |
"urn:liberouter:params:netconf:capability:power-control:1.0"
|
36 |
"urn:ietf:params:netconf:capability:interleave:1.0"
|
37 |
] |
38 |
"""A list of URI's representing the client's capabilities. This is used during the initial capability exchange. Modify this if you need to announce some capability not already included."""
|
39 |
|
40 |
OPERATIONS = { |
41 |
"get": operations.Get,
|
42 |
"get_config": operations.GetConfig,
|
43 |
"dispatch": operations.Dispatch,
|
44 |
"edit_config": operations.EditConfig,
|
45 |
"copy_config": operations.CopyConfig,
|
46 |
"validate": operations.Validate,
|
47 |
"commit": operations.Commit,
|
48 |
"discard_changes": operations.DiscardChanges,
|
49 |
"delete_config": operations.DeleteConfig,
|
50 |
"lock": operations.Lock,
|
51 |
"unlock": operations.Unlock,
|
52 |
"close_session": operations.CloseSession,
|
53 |
"kill_session": operations.KillSession,
|
54 |
"poweroff_machine": operations.PoweroffMachine,
|
55 |
"reboot_machine": operations.RebootMachine
|
56 |
} |
57 |
"""Dictionary of 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."""
|
58 |
|
59 |
def connect_ssh(*args, **kwds): |
60 |
"""Initialize a :class:`Manager` over the SSH transport. For documentation of arguments see :meth:`ncclient.transport.SSHSession.connect`.
|
61 |
|
62 |
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`.
|
63 |
"""
|
64 |
session = transport.SSHSession(capabilities.Capabilities(CAPABILITIES)) |
65 |
session.load_known_hosts() |
66 |
session.connect(*args, **kwds) |
67 |
return Manager(session)
|
68 |
|
69 |
connect = connect_ssh |
70 |
"Same as :func:`connect_ssh`, since SSH is the default (and currently, the only) transport."
|
71 |
|
72 |
class OpExecutor(type): |
73 |
|
74 |
def __new__(cls, name, bases, attrs): |
75 |
def make_wrapper(op_cls): |
76 |
def wrapper(self, *args, **kwds): |
77 |
return self.execute(op_cls, *args, **kwds) |
78 |
wrapper.func_doc = op_cls.request.func_doc |
79 |
return wrapper
|
80 |
for op_name, op_cls in OPERATIONS.iteritems(): |
81 |
attrs[op_name] = make_wrapper(op_cls) |
82 |
return super(OpExecutor, cls).__new__(cls, name, bases, attrs) |
83 |
|
84 |
class Manager(object): |
85 |
|
86 |
"""For details on the expected behavior of the operations and their parameters refer to :rfc:`4741`.
|
87 |
|
88 |
Manager instances are also context managers so you can use it like this::
|
89 |
|
90 |
with manager.connect("host") as m:
|
91 |
# do your stuff
|
92 |
|
93 |
... or like this::
|
94 |
|
95 |
m = manager.connect("host")
|
96 |
try:
|
97 |
# do your stuff
|
98 |
finally:
|
99 |
m.close_session()
|
100 |
"""
|
101 |
|
102 |
__metaclass__ = OpExecutor |
103 |
|
104 |
def __init__(self, session, timeout=30): |
105 |
self._session = session
|
106 |
self._async_mode = False |
107 |
self._timeout = timeout
|
108 |
self._raise_mode = operations.RaiseMode.ALL
|
109 |
|
110 |
def __enter__(self): |
111 |
return self |
112 |
|
113 |
def __exit__(self, *args): |
114 |
self.close_session()
|
115 |
return False |
116 |
|
117 |
def __set_timeout(self, timeout): |
118 |
self._timeout = timeout
|
119 |
|
120 |
def __set_async_mode(self, mode): |
121 |
self._async_mode = mode
|
122 |
|
123 |
def __set_raise_mode(self, mode): |
124 |
assert(mode in (operations.RaiseMode.NONE, operations.RaiseMode.ERRORS, operations.RaiseMode.ALL)) |
125 |
self._raise_mode = mode
|
126 |
|
127 |
def execute(self, cls, *args, **kwds): |
128 |
return cls(self._session, |
129 |
async=self._async_mode,
|
130 |
timeout=self._timeout,
|
131 |
raise_mode=self._raise_mode).request(*args, **kwds)
|
132 |
|
133 |
def locked(self, target): |
134 |
"""Returns a context manager for a lock on a datastore, where *target* is the name of the configuration datastore to lock, e.g.::
|
135 |
|
136 |
with m.locked("running"):
|
137 |
# do your stuff
|
138 |
|
139 |
... instead of::
|
140 |
|
141 |
m.lock("running")
|
142 |
try:
|
143 |
# do your stuff
|
144 |
finally:
|
145 |
m.unlock("running")
|
146 |
"""
|
147 |
return operations.LockContext(self._session, target) |
148 |
|
149 |
@property
|
150 |
def client_capabilities(self): |
151 |
":class:`~ncclient.capabilities.Capabilities` object representing the client's capabilities."
|
152 |
return self._session._client_capabilities |
153 |
|
154 |
@property
|
155 |
def server_capabilities(self): |
156 |
":class:`~ncclient.capabilities.Capabilities` object representing the server's capabilities."
|
157 |
return self._session._server_capabilities |
158 |
|
159 |
@property
|
160 |
def session_id(self): |
161 |
"`session-id` assigned by the NETCONF server."
|
162 |
return self._session.id |
163 |
|
164 |
@property
|
165 |
def connected(self): |
166 |
"Whether currently connected to the NETCONF server."
|
167 |
return self._session.connected |
168 |
|
169 |
async_mode = property(fget=lambda self: self._async_mode, fset=__set_async_mode) |
170 |
"Specify whether operations are executed asynchronously (`True`) or synchronously (`False`) (the default)."
|
171 |
|
172 |
timeout = property(fget=lambda self: self._timeout, fset=__set_timeout) |
173 |
"Specify the timeout for synchronous RPC requests."
|
174 |
|
175 |
raise_mode = property(fget=lambda self: self._raise_mode, fset=__set_raise_mode) |
176 |
"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`."
|