summaryrefslogtreecommitdiff
path: root/novaclient/v2/contrib/baremetal.py
blob: d23a38e53cd71c6d89d6e500ef6c410550b393fb (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    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.

"""
Baremetal interface (v2 extension).
"""

from novaclient import base
from novaclient.i18n import _
from novaclient.openstack.common import cliutils
from novaclient import utils


class BareMetalNode(base.Resource):
    """
    A baremetal node (typically a physical server or an empty VM).
    """

    def __repr__(self):
        return "<BareMetalNode: %s>" % self.id


class BareMetalNodeInterface(base.Resource):
    """
    An interface belonging to a baremetal node.
    """

    def __repr__(self):
        return "<BareMetalNodeInterface: %s>" % self.id


class BareMetalNodeManager(base.ManagerWithFind):
    """
    Manage :class:`BareMetalNode` resources.
    """
    resource_class = BareMetalNode

    def create(self,
               service_host,
               cpus,
               memory_mb,
               local_gb,
               prov_mac_address,
               pm_address=None,
               pm_user=None,
               pm_password=None,
               terminal_port=None):
        """
        Create a baremetal node.

        :param service_host: Name of controlling compute host
        :param cpus: Number of CPUs in the node
        :param memory_mb: Megabytes of RAM in the node
        :param local_gb: Gigabytes of local storage in the node
        :param pm_address: Power management IP for the node
        :param pm_user: Username for the node's power management
        :param pm_password: Password for the node's power management
        :param prov_mac_address: MAC address to provision the node
        :param terminal_port: ShellInABox port
        :rtype: :class:`BareMetalNode`
        """
        body = {'node': {'service_host': service_host,
                         'cpus': cpus,
                         'memory_mb': memory_mb,
                         'local_gb': local_gb,
                         'pm_address': pm_address,
                         'pm_user': pm_user,
                         'pm_password': pm_password,
                         'prov_mac_address': prov_mac_address,
                         'terminal_port': terminal_port}}

        return self._create('/os-baremetal-nodes', body, 'node')

    def delete(self, node):
        """
        Delete a baremetal node.

        :param node: The :class:`BareMetalNode` to delete.
        """
        self._delete('/os-baremetal-nodes/%s' % base.getid(node))

    def get(self, node_id):
        """
        Get a baremetal node.

        :param node_id: The ID of the node to delete.
        :rtype: :class:`BareMetalNode`
        """
        return self._get("/os-baremetal-nodes/%s" % node_id, 'node')

    def list(self):
        """
        Get a list of all baremetal nodes.

        :rtype: list of :class:`BareMetalNode`
        """
        return self._list('/os-baremetal-nodes', 'nodes')

    def add_interface(self, node_id, address, datapath_id=0, port_no=0):
        """
        Add an interface to a baremetal node.

        :param node_id: The ID of the node to modify.
        :param address: The MAC address to add.
        :param datapath_id: Datapath ID of OpenFlow switch for the interface
        :param port_no: OpenFlow port number for the interface
        :rtype: :class:`BareMetalNodeInterface`
        """
        body = {'add_interface': {'address': address,
                                  'datapath_id': datapath_id,
                                  'port_no': port_no}}
        url = '/os-baremetal-nodes/%s/action' % node_id
        _resp, body = self.api.client.post(url, body=body)
        return BareMetalNodeInterface(self, body['interface'])

    def remove_interface(self, node_id, address):
        """
        Remove an interface from a baremetal node.

        :param node_id: The ID of the node to modify.
        :param address: The MAC address to remove.
        :rtype: bool
        """
        req_body = {'remove_interface': {'address': address}}
        url = '/os-baremetal-nodes/%s/action' % node_id
        self.api.client.post(url, body=req_body)

    def list_interfaces(self, node_id):
        """
        List the interfaces on a baremetal node.

        :param node_id: The ID of the node to list.
        :rtype: list
        """
        interfaces = []
        node = self._get("/os-baremetal-nodes/%s" % node_id, 'node')
        for interface in node.interfaces:
            interface_object = BareMetalNodeInterface(self, interface)
            interfaces.append(interface_object)
        return interfaces


@cliutils.arg(
    'service_host',
    metavar='<service_host>',
    help=_('Name of nova compute host which will control this baremetal '
           'node'))
@cliutils.arg(
    'cpus',
    metavar='<cpus>',
    type=int,
    help=_('Number of CPUs in the node'))
@cliutils.arg(
    'memory_mb',
    metavar='<memory_mb>',
    type=int,
    help=_('Megabytes of RAM in the node'))
@cliutils.arg(
    'local_gb',
    metavar='<local_gb>',
    type=int,
    help=_('Gigabytes of local storage in the node'))
@cliutils.arg(
    'prov_mac_address',
    metavar='<prov_mac_address>',
    help=_('MAC address to provision the node'))
@cliutils.arg(
    '--pm_address', default=None,
    metavar='<pm_address>',
    help=_('Power management IP for the node'))
@cliutils.arg(
    '--pm_user', default=None,
    metavar='<pm_user>',
    help=_('Username for the node\'s power management'))
@cliutils.arg(
    '--pm_password', default=None,
    metavar='<pm_password>',
    help=_('Password for the node\'s power management'))
@cliutils.arg(
    '--terminal_port', default=None,
    metavar='<terminal_port>',
    type=int,
    help=_('ShellInABox port?'))
def do_baremetal_node_create(cs, args):
    """Create a baremetal node."""
    node = cs.baremetal.create(args.service_host, args.cpus,
                               args.memory_mb, args.local_gb,
                               args.prov_mac_address,
                               pm_address=args.pm_address,
                               pm_user=args.pm_user,
                               pm_password=args.pm_password,
                               terminal_port=args.terminal_port)
    _print_baremetal_resource(node)


@cliutils.arg(
    'node',
    metavar='<node>',
    help=_('ID of the node to delete.'))
def do_baremetal_node_delete(cs, args):
    """Remove a baremetal node and any associated interfaces."""
    node = _find_baremetal_node(cs, args.node)
    cs.baremetal.delete(node)


def _translate_baremetal_node_keys(collection):
    convert = [('service_host', 'host'),
               ('local_gb', 'disk_gb'),
               ('prov_mac_address', 'mac_address'),
               ('pm_address', 'pm_address'),
               ('pm_user', 'pm_username'),
               ('pm_password', 'pm_password'),
               ('terminal_port', 'terminal_port'),
               ]
    for item in collection:
        keys = item.__dict__.keys()
        for from_key, to_key in convert:
            if from_key in keys and to_key not in keys:
                setattr(item, to_key, item._info[from_key])


def _print_baremetal_nodes_list(nodes):
    """Print the list of baremetal nodes."""

    def _parse_address(fields):
        macs = []
        for interface in fields.interfaces:
            macs.append(interface['address'])
        return ', '.join("%s" % i for i in macs)

    formatters = {
        'MAC Address': _parse_address
    }

    _translate_baremetal_node_keys(nodes)
    utils.print_list(nodes, [
        'ID',
        'Host',
        'Task State',
        'CPUs',
        'Memory_MB',
        'Disk_GB',
        'MAC Address',
        'PM Address',
        'PM Username',
        'PM Password',
        'Terminal Port',
    ], formatters=formatters)


def do_baremetal_node_list(cs, _args):
    """Print list of available baremetal nodes."""
    nodes = cs.baremetal.list()
    _print_baremetal_nodes_list(nodes)


def _find_baremetal_node(cs, node):
    """Get a node by ID."""
    return utils.find_resource(cs.baremetal, node)


def _print_baremetal_resource(resource):
    """Print details of a baremetal resource."""
    info = resource._info.copy()
    utils.print_dict(info)


def _print_baremetal_node_interfaces(interfaces):
    """Print interfaces of a baremetal node."""
    utils.print_list(interfaces, [
        'ID',
        'Datapath_ID',
        'Port_No',
        'Address',
    ])


@cliutils.arg(
    'node',
    metavar='<node>',
    help=_("ID of node"))
def do_baremetal_node_show(cs, args):
    """Show information about a baremetal node."""
    node = _find_baremetal_node(cs, args.node)
    _print_baremetal_resource(node)


@cliutils.arg(
    'node',
    metavar='<node>',
    help=_("ID of node"))
@cliutils.arg(
    'address',
    metavar='<address>',
    help=_("MAC address of interface"))
@cliutils.arg(
    '--datapath_id',
    default=0,
    metavar='<datapath_id>',
    help=_("OpenFlow Datapath ID of interface"))
@cliutils.arg(
    '--port_no',
    default=0,
    metavar='<port_no>',
    help=_("OpenFlow port number of interface"))
def do_baremetal_interface_add(cs, args):
    """Add a network interface to a baremetal node."""
    bmif = cs.baremetal.add_interface(args.node, args.address,
                                      args.datapath_id, args.port_no)
    _print_baremetal_resource(bmif)


@cliutils.arg('node', metavar='<node>', help=_("ID of node"))
@cliutils.arg(
    'address',
    metavar='<address>',
    help=_("MAC address of interface"))
def do_baremetal_interface_remove(cs, args):
    """Remove a network interface from a baremetal node."""
    cs.baremetal.remove_interface(args.node, args.address)


@cliutils.arg('node', metavar='<node>', help=_("ID of node"))
def do_baremetal_interface_list(cs, args):
    """List network interfaces associated with a baremetal node."""
    interfaces = cs.baremetal.list_interfaces(args.node)
    _print_baremetal_node_interfaces(interfaces)