Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 4f650d54

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
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, RAISE_ERROR, RAISE_NONE = range(3)
29

    
30
class Manager:
31

    
32
    "Thin layer of abstraction for the ncclient API."
33

    
34
    def __init__(self, session):
35
        self._session = session
36
        self._rpc_error_handling = RAISE_ALL
37

    
38
    def set_rpc_error_option(self, option):
39
        self._rpc_error_handling = option
40

    
41
    def do(self, op, *args, **kwds):
42
        op = operations.OPERATIONS[op](self._session)
43
        reply = op.request(*args, **kwds)
44
        if not reply.ok:
45
            if self._raise == RAISE_ALL:
46
                raise reply.error
47
            elif self._raise == RAISE_ERROR:
48
                for error in reply.errors:
49
                    if error.severity == 'error':
50
                        raise error
51
        return reply
52

    
53
    def __enter__(self):
54
        pass
55

    
56
    def __exit__(self, *args):
57
        self.close()
58
        return False
59

    
60
    def locked(self, target):
61
        """Returns a context manager for use with the 'with' statement.
62
        `target` is the datastore to lock, e.g. 'candidate
63
        """
64
        return operations.LockContext(self._session, target)
65

    
66
    get = lambda self, *args, **kwds: self.do('get', *args, **kwds).data
67

    
68
    get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds).data
69

    
70
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
71

    
72
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
73

    
74
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
75

    
76
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
77

    
78
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
79

    
80
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
81

    
82
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
83

    
84
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
85

    
86
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
87

    
88
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
89

    
90
    def close(self):
91
        try: # try doing it clean
92
            self.close_session()
93
        except:
94
            pass
95
        if self._session.connected: # if that didn't work...
96
            self._session.close()
97

    
98
    @property
99
    def session(self, session):
100
        return self._session
101

    
102
    def get_capabilities(self, whose):
103
        if whose in ('manager', 'client'):
104
            return self._session._client_capabilities
105
        elif whose in ('agent', 'server'):
106
            return self._session._server_capabilities
107

    
108
    @property
109
    def capabilities(self):
110
        return self._session._client_capabilities
111

    
112
    @property
113
    def server_capabilities(self):
114
        return self._session._server_capabilities