root / ncclient / manager.py @ d771dffc
History | View | Annotate | Download (4 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 |
import operations |
17 |
import transport |
18 |
|
19 |
OPERATIONS = { |
20 |
'get': operations.Get,
|
21 |
'get-config': operations.GetConfig,
|
22 |
'edit-config': operations.EditConfig,
|
23 |
'copy-config': operations.CopyConfig,
|
24 |
'validate': operations.Validate,
|
25 |
'commit': operations.Commit,
|
26 |
'discard-changes': operations.DiscardChanges,
|
27 |
'delete-config': operations.DeleteConfig,
|
28 |
'lock': operations.Lock,
|
29 |
'unlock': operations.Unlock,
|
30 |
'close_session': operations.CloseSession,
|
31 |
'kill-session': operations.KillSession,
|
32 |
} |
33 |
|
34 |
def connect_ssh(*args, **kwds): |
35 |
session = transport.SSHSession(capabilities.CAPABILITIES) |
36 |
session.load_system_host_keys() |
37 |
session.connect(*args, **kwds) |
38 |
return Manager(session)
|
39 |
|
40 |
connect = connect_ssh # default session type
|
41 |
|
42 |
class Manager: |
43 |
|
44 |
"Thin layer of abstraction for the API."
|
45 |
|
46 |
RAISE_ALL, RAISE_ERROR, RAISE_NONE = range(3) |
47 |
|
48 |
def __init__(self, session, rpc_errors=Manager.RAISE_ALL): |
49 |
self._session = session
|
50 |
self._raise = rpc_error
|
51 |
|
52 |
def do(self, op, *args, **kwds): |
53 |
op = OPERATIONS[op](self._session)
|
54 |
reply = op.request(*args, **kwds) |
55 |
if not reply.ok: |
56 |
if self._raise == Manager.RAISE_ALL: |
57 |
raise reply.error
|
58 |
elif self._raise == Manager.RAISE_ERROR: |
59 |
for error in reply.errors: |
60 |
if error.severity == 'error': |
61 |
raise error
|
62 |
return reply
|
63 |
|
64 |
def __enter__(self): |
65 |
pass
|
66 |
|
67 |
def __exit__(self, *args): |
68 |
self.close()
|
69 |
return False |
70 |
|
71 |
def locked(self, target): |
72 |
"""Returns a context manager for use withthe 'with' statement.
|
73 |
`target` is the datastore to lock, e.g. 'candidate
|
74 |
"""
|
75 |
return operations.LockContext(self._session, target) |
76 |
|
77 |
get = lambda self, *args, **kwds: self.do('get', *args, **kwds).data |
78 |
|
79 |
get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds).data |
80 |
|
81 |
edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds) |
82 |
|
83 |
copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds) |
84 |
|
85 |
validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds) |
86 |
|
87 |
commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds) |
88 |
|
89 |
discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds) |
90 |
|
91 |
delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds) |
92 |
|
93 |
lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds) |
94 |
|
95 |
unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds) |
96 |
|
97 |
close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds) |
98 |
|
99 |
kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds) |
100 |
|
101 |
def close(self): |
102 |
try: # try doing it clean |
103 |
self.close_session()
|
104 |
except:
|
105 |
pass
|
106 |
if self._session.connected: # if that didn't work... |
107 |
self._session.close()
|
108 |
|
109 |
@property
|
110 |
def session(self, session): |
111 |
return self._session |
112 |
|
113 |
def get_capabilities(self, whose): |
114 |
if whose in ('manager', 'client'): |
115 |
return self._session._client_capabilities |
116 |
elif whose in ('agent', 'server'): |
117 |
return self._session._server_capabilities |
118 |
|
119 |
@property
|
120 |
def capabilities(self): |
121 |
return self._session._client_capabilities |