aboutsummaryrefslogtreecommitdiffstats
path: root/vicn/resource/linux/physical.py
blob: 8decb517de9c934be1995a7ca19cc42e69efb4e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import stat
import logging
import subprocess
import shlex

from netmodel.model.type            import String, Integer
from netmodel.util.misc             import is_local_host
from netmodel.util.socket           import check_port
from vicn.core.attribute            import Attribute
from vicn.core.commands             import Command, ReturnValue
from vicn.core.exception            import ResourceNotFound, VICNException
from vicn.core.task                 import Task, task
from vicn.resource.linux.keypair    import Keypair
from vicn.resource.node             import Node, DEFAULT_USERNAME
from vicn.resource.node             import DEFAULT_SSH_PUBLIC_KEY
from vicn.resource.node             import DEFAULT_SSH_PRIVATE_KEY

log = logging.getLogger(__name__)

CMD_SSH_COPY_ID = 'ssh-copy-id {ssh_options} -i {public_key} -p {port} ' \
                  '{user}@{host}'
CMD_SSH = 'ssh {ssh_options} -i {private_key} -p {port} ' \
          '{user}@{host} {command}' 
CMD_SSH_NF = 'ssh -n -f {ssh_options} -i {private_key} -p {port} ' \
             '{user}@{host} {command}'

FN_KEY = os.path.expanduser(os.path.join(
            '~', '.vicn', 'ssh_client_cert', 'ssh_client_key'))

class Physical(Node):
    """
    Resource: Physical

    Physical node
    """

    hostname = Attribute(String, description = 'Hostname or IP address',
            mandatory = True)
    ssh_port = Attribute(Integer, description = 'SSH port number',
            default = 22)

    #--------------------------------------------------------------------------
    # Constructor and Accessors
    #--------------------------------------------------------------------------

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.node_with_kernel = self

    #--------------------------------------------------------------------------
    # Resource lifecycle
    #--------------------------------------------------------------------------

    def __subresources__(self):
        """
        Require a SSH keypair to be present for authentication on nodes
        """
        return Keypair(node = None, key = FN_KEY)

    def __initialize__(self):
        """
        Initialization require the ssh port to be open on the node, and the ssh
        public key to be copied on the remote node.
        """
        if not check_port(self.hostname, self.ssh_port):
            raise VICNException

        tasks = list()
        modes = (True, False) if DEFAULT_USERNAME != 'root' else (True,) 
        for as_root in modes:
            tasks.append(self._setup_ssh_key(as_root))
        return Task.__concurrent__(*tasks)

    __delete__ = None

    #--------------------------------------------------------------------------
    # Internal methods
    #--------------------------------------------------------------------------

    @task
    def _setup_ssh_key(self, as_root):
        os.chmod(DEFAULT_SSH_PUBLIC_KEY, stat.S_IRUSR | stat.S_IWUSR)
        os.chmod(DEFAULT_SSH_PRIVATE_KEY, stat.S_IRUSR | stat.S_IWUSR)
        cmd_params = {
            'public_key'    : DEFAULT_SSH_PUBLIC_KEY,
            'ssh_options'   : '', 
            'port'          : self.ssh_port,
            'user'          : 'root' if as_root else DEFAULT_USERNAME,
            'host'          : self.hostname,
        }
        
        c = Command(CMD_SSH_COPY_ID, parameters = cmd_params)

        return self._do_execute_process(c.full_commandline, output=False)

    #--------------------------------------------------------------------------
    # Public API
    #--------------------------------------------------------------------------

    def _do_execute_process(self, command, output = False):
        p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
        if output:
            out, err = p.communicate()
            return ReturnValue(p.returncode, stdout=out, stderr=err)

        p.wait()
        return ReturnValue(p.returncode)

    def _do_execute_ssh(self, command, output=False, as_root=False, 
            ssh_options=None):
        if not ssh_options:
            ssh_options = dict()
        cmd_params = {
            'private_key'   : DEFAULT_SSH_PRIVATE_KEY,
            'ssh_options'   : ' '.join(['-o {}={}'.format(k, v) 
                    for k, v in ssh_options.items()]),
            'port'          : self.ssh_port,
            'user'          : 'root' if as_root else DEFAULT_USERNAME,
            'host'          : self.hostname,
            'command'       : shlex.quote(command), 
        }

        if (command[-1] != '&'):
            c = Command(CMD_SSH, parameters = cmd_params)
        else:
            c = Command(CMD_SSH_NF, parameters = cmd_params)

        return self._do_execute_process(c.full_commandline_nobashize, output)

    def execute(self, command, output=False, as_root=False):
        if is_local_host(self.hostname):
            rv = self._do_execute_process(command, output = output)
        else:
            rv = self._do_execute_ssh(command, output = output, 
                    as_root = as_root)
        return rv