1 # Copyright 2009 Shikhar Bhushan
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 def connect_ssh(*args, **kwds):
21 session = transport.SSHSession(capabilities.CAPABILITIES)
22 session.load_system_host_keys()
23 session.connect(*args, **kwds)
24 return Manager(session)
26 connect = connect_ssh # default session type
28 RAISE_ALL, RAISE_ERROR, RAISE_NONE = range(3)
32 "Thin layer of abstraction for the API."
34 def __init__(self, session, rpc_error=RAISE_ALL):
35 self._session = session
36 self._raise = rpc_error
38 def do(self, op, *args, **kwds):
39 op = operations.OPERATIONS[op](self._session)
40 reply = op.request(*args, **kwds)
42 if self._raise == RAISE_ALL:
44 elif self._raise == RAISE_ERROR:
45 for error in reply.errors:
46 if error.severity == 'error':
53 def __exit__(self, *args):
57 def locked(self, target):
58 """Returns a context manager for use withthe 'with' statement.
59 `target` is the datastore to lock, e.g. 'candidate
61 return operations.LockContext(self._session, target)
63 get = lambda self, *args, **kwds: self.do('get', *args, **kwds).data
65 get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds).data
67 edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
69 copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
71 validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
73 commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
75 discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
77 delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
79 lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
81 unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
83 close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
85 kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
88 try: # try doing it clean
92 if self._session.connected: # if that didn't work...
96 def session(self, session):
99 def get_capabilities(self, whose):
100 if whose in ('manager', 'client'):
101 return self._session._client_capabilities
102 elif whose in ('agent', 'server'):
103 return self._session._server_capabilities
106 def capabilities(self):
107 return self._session._client_capabilities