root / ncclient / manager.py @ a7cb58ce
History | View | Annotate | Download (3.6 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 |
import capabilities |
16 |
from operations import OPERATIONS |
17 |
import transport |
18 |
|
19 |
|
20 |
def ssh_connect(*args, **kwds): |
21 |
session = transport.SSHSession(capabilities.CAPABILITIES) |
22 |
session.load_system_host_keys() |
23 |
session.connect(*args, **kwds) |
24 |
return Manager(session)
|
25 |
|
26 |
connect = ssh_connect # default session type
|
27 |
|
28 |
#: Raise all errors
|
29 |
RAISE_ALL = 0
|
30 |
#:
|
31 |
RAISE_ERR = 1
|
32 |
#:
|
33 |
RAISE_NONE = 2
|
34 |
|
35 |
class Manager: |
36 |
|
37 |
"Thin layer of abstraction for the ncclient API."
|
38 |
|
39 |
def __init__(self, session): |
40 |
self._session = session
|
41 |
self._rpc_error_action = RAISE_ALL
|
42 |
|
43 |
def set_rpc_error_action(self, action): |
44 |
self._rpc_error_handling = option
|
45 |
|
46 |
def do(self, op, *args, **kwds): |
47 |
op = OPERATIONS[op](self._session)
|
48 |
reply = op.request(*args, **kwds) |
49 |
if not reply.ok: |
50 |
if self._raise == RAISE_ALL: |
51 |
raise reply.error
|
52 |
elif self._raise == RAISE_ERROR: |
53 |
for error in reply.errors: |
54 |
if error.severity == 'error': |
55 |
raise error
|
56 |
return reply
|
57 |
|
58 |
def __enter__(self): |
59 |
pass
|
60 |
|
61 |
def __exit__(self, *args): |
62 |
self.close()
|
63 |
return False |
64 |
|
65 |
def locked(self, target): |
66 |
"""Returns a context manager for use with the 'with' statement.
|
67 |
|
68 |
:arg target: name of the datastore to lock
|
69 |
:type target: `string`
|
70 |
"""
|
71 |
return operations.LockContext(self._session, target) |
72 |
|
73 |
def get(self, filter=None): |
74 |
pass
|
75 |
|
76 |
def get_config(self, source, filter=None): |
77 |
pass
|
78 |
|
79 |
def copy_config(self, source, target): |
80 |
pass
|
81 |
|
82 |
def validate(self, source): |
83 |
pass
|
84 |
|
85 |
def commit(self, target): |
86 |
pass
|
87 |
|
88 |
def discard_changes(self): |
89 |
pass
|
90 |
|
91 |
def delete_config(self, target): |
92 |
pass
|
93 |
|
94 |
def lock(self, target): |
95 |
pass
|
96 |
|
97 |
def unlock(self, target): |
98 |
pass
|
99 |
|
100 |
def close_session(self): |
101 |
pass
|
102 |
|
103 |
def kill_session(self, session_id): |
104 |
pass
|
105 |
|
106 |
def confirmed_commit(self, timeout=None): |
107 |
pass
|
108 |
|
109 |
def confirm(self): |
110 |
# give confirmation
|
111 |
pass
|
112 |
|
113 |
def discard_changes(self): |
114 |
pass
|
115 |
|
116 |
lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds) |
117 |
|
118 |
unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds) |
119 |
|
120 |
close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds) |
121 |
|
122 |
kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds) |
123 |
|
124 |
def close(self): |
125 |
try: # try doing it clean |
126 |
self.close_session()
|
127 |
except Exception as e: |
128 |
logger.debug('error doing <close-session> -- %r' % e)
|
129 |
if self._session.connected: # if that didn't work... |
130 |
self._session.close()
|
131 |
|
132 |
@property
|
133 |
def session(self, session): |
134 |
return self._session |
135 |
|
136 |
@property
|
137 |
def client_capabilities(self): |
138 |
return self._session._client_capabilities |
139 |
|
140 |
@property
|
141 |
def server_capabilities(self): |
142 |
return self._session._server_capabilities |
143 |
|
144 |
@property
|
145 |
def session_id(self): |
146 |
return self._session.id |