Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 4de03d63

History | View | Annotate | Download (3.5 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

    
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)
25

    
26
connect = connect_ssh # default session type
27

    
28
RAISE_ALL, RAISE_ERROR, RAISE_NONE = range(3)
29

    
30
class Manager:
31
    
32
    "Thin layer of abstraction for the API."
33
    
34
    def __init__(self, session, rpc_error=RAISE_ALL):
35
        self._session = session
36
        self._raise = rpc_error
37

    
38
    def do(self, op, *args, **kwds):
39
        op = operations.OPERATIONS[op](self._session)
40
        reply = op.request(*args, **kwds)
41
        if not reply.ok:
42
            if self._raise == RAISE_ALL:
43
                raise reply.error
44
            elif self._raise == RAISE_ERROR:
45
                for error in reply.errors:
46
                    if error.severity == 'error':
47
                        raise error
48
        return reply
49
    
50
    def __enter__(self):
51
        pass
52
    
53
    def __exit__(self, *args):
54
        self.close()
55
        return False
56
    
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
60
        """
61
        return operations.LockContext(self._session, target)
62
     
63
    get = lambda self, *args, **kwds: self.do('get', *args, **kwds).data
64
    
65
    get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds).data
66
    
67
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
68
    
69
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
70
    
71
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
72
    
73
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
74
    
75
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
76
    
77
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
78
    
79
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
80
    
81
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
82
    
83
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
84
    
85
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
86
    
87
    def close(self):
88
        try: # try doing it clean
89
            self.close_session()
90
        except:
91
            pass
92
        if self._session.connected: # if that didn't work...
93
            self._session.close()
94
    
95
    @property
96
    def session(self, session):
97
        return self._session
98
    
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
104
    
105
    @property
106
    def capabilities(self):
107
        return self._session._client_capabilities