summaryrefslogtreecommitdiffstats
path: root/vicn/resource/node.py
blob: bfb2f9ec1b9b251a465f0a21d5f1dd5edd85ac50 (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
#!/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 logging
import os

from netmodel.model.type                import Double, String, Self
from vicn.core.address_mgr              import AddressManager
from vicn.core.attribute                import Attribute
from vicn.core.resource                 import Resource

log = logging.getLogger(__name__)

DEFAULT_USERNAME = 'root'
DEFAULT_SSH_PRIVATE_KEY = os.path.join(os.path.dirname(__file__),
        '..', '..', 'config', 'ssh_client_cert', 'ssh_client_key')
DEFAULT_SSH_PUBLIC_KEY = os.path.join(os.path.dirname(__file__),
        '..', '..', 'config', 'ssh_client_cert', 'ssh_client_key.pub')

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

    x = Attribute(Double, description = 'X coordinate',
            default = 0.0)
    y = Attribute(Double, description = 'Y coordinate',
            default = 0.0)
    category = Attribute(String)
    os = Attribute(String, description = 'OS',
            default = 'ubuntu',
            choices = ['debian', 'ubuntu'])
    dist = Attribute(String, description = 'Distribution name',
            default = 'xenial',
            choices = ['trusty', 'xenial', 'sid'])
    arch = Attribute(String, description = 'Architecture',
            default = 'amd64',
            choices = ['amd64'])
    node_with_kernel = Attribute(Self,
            description = 'Node on which the kernel sits',
            ro = True)

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

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

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

    @property
    def host_interface(self):
        """
        We assume that any unmanaged interface associated to the host is the
        main host interface. It should thus be declared in the JSON topology.
        We might later perform some kind of auto discovery.

        This unmanaged interface is only required to get the device_name:
          - to create Veth (need a parent)
          - to ssh a node, get its ip address (eg for the repo)
          - to avoid loops in type specification

        It is used for all nodes to provide network connectivity.
        """

        for interface in self.interfaces:
            if not interface.managed or interface.owner is not None:
                return interface

        raise Exception('Cannot find host interface for node {}: {}'.format(
                    self, self.interfaces))

    def execute(self, command, output = False, as_root = False):
        raise NotImplementedError