2016-06-14 09:49:24 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2013-03-06 15:18:04 -06:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Host action implementations"""
|
|
|
|
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
|
|
|
|
2016-05-14 15:12:57 +08:00
|
|
|
from openstackclient.i18n import _
|
2013-03-06 15:18:04 -06:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListHost(command.Lister):
|
2022-12-06 21:44:23 -05:00
|
|
|
_description = _("DEPRECATED: List hosts")
|
2013-03-06 15:18:04 -06:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
2022-12-06 21:44:23 -05:00
|
|
|
parser = super().get_parser(prog_name)
|
2013-03-06 15:18:04 -06:00
|
|
|
parser.add_argument(
|
|
|
|
"--zone",
|
|
|
|
metavar="<zone>",
|
2023-05-08 10:48:54 +01:00
|
|
|
help=_("Only return hosts in the availability zone"),
|
2016-05-14 15:12:57 +08:00
|
|
|
)
|
2013-03-06 15:18:04 -06:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
2022-12-06 21:44:23 -05:00
|
|
|
compute_client = self.app.client_manager.sdk_connection.compute
|
2023-05-08 10:48:54 +01:00
|
|
|
columns = ("Host Name", "Service", "Zone")
|
2022-12-06 21:44:23 -05:00
|
|
|
|
|
|
|
self.log.warning(
|
|
|
|
"API has been deprecated. "
|
|
|
|
"Please consider using 'hypervisor list' instead."
|
|
|
|
)
|
|
|
|
|
|
|
|
# doing this since openstacksdk has decided not to support this
|
|
|
|
# deprecated command
|
2023-05-08 10:48:54 +01:00
|
|
|
hosts = (
|
|
|
|
compute_client.get('/os-hosts', microversion='2.1')
|
|
|
|
.json()
|
|
|
|
.get('hosts')
|
|
|
|
)
|
2022-12-06 21:44:23 -05:00
|
|
|
|
|
|
|
if parsed_args.zone is not None:
|
|
|
|
filtered_hosts = []
|
|
|
|
for host in hosts:
|
|
|
|
if host['zone'] == parsed_args.zone:
|
|
|
|
filtered_hosts.append(host)
|
|
|
|
|
|
|
|
hosts = filtered_hosts
|
|
|
|
|
|
|
|
return columns, (utils.get_dict_properties(s, columns) for s in hosts)
|
2013-03-06 15:18:04 -06:00
|
|
|
|
|
|
|
|
2016-02-19 22:52:38 +08:00
|
|
|
class SetHost(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Set host properties")
|
|
|
|
|
2016-02-19 22:52:38 +08:00
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(SetHost, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2023-05-08 10:48:54 +01:00
|
|
|
"host", metavar="<host>", help=_("Host to modify (name only)")
|
2016-02-19 22:52:38 +08:00
|
|
|
)
|
|
|
|
status = parser.add_mutually_exclusive_group()
|
|
|
|
status.add_argument(
|
2023-05-08 10:48:54 +01:00
|
|
|
'--enable', action='store_true', help=_("Enable the host")
|
2016-02-19 22:52:38 +08:00
|
|
|
)
|
|
|
|
status.add_argument(
|
2023-05-08 10:48:54 +01:00
|
|
|
'--disable', action='store_true', help=_("Disable the host")
|
2016-02-19 22:52:38 +08:00
|
|
|
)
|
|
|
|
maintenance = parser.add_mutually_exclusive_group()
|
|
|
|
maintenance.add_argument(
|
|
|
|
'--enable-maintenance',
|
|
|
|
action='store_true',
|
2023-05-08 10:48:54 +01:00
|
|
|
help=_("Enable maintenance mode for the host"),
|
2016-02-19 22:52:38 +08:00
|
|
|
)
|
|
|
|
maintenance.add_argument(
|
|
|
|
'--disable-maintenance',
|
|
|
|
action='store_true',
|
2023-05-08 10:48:54 +01:00
|
|
|
help=_("Disable maintenance mode for the host"),
|
2016-02-19 22:52:38 +08:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
kwargs = {}
|
|
|
|
|
|
|
|
if parsed_args.enable:
|
2016-06-20 19:40:26 +08:00
|
|
|
kwargs['status'] = 'enable'
|
2016-02-19 22:52:38 +08:00
|
|
|
if parsed_args.disable:
|
2016-06-20 19:40:26 +08:00
|
|
|
kwargs['status'] = 'disable'
|
2016-02-19 22:52:38 +08:00
|
|
|
if parsed_args.enable_maintenance:
|
2016-06-20 19:40:26 +08:00
|
|
|
kwargs['maintenance_mode'] = 'enable'
|
2016-02-19 22:52:38 +08:00
|
|
|
if parsed_args.disable_maintenance:
|
2016-06-20 19:40:26 +08:00
|
|
|
kwargs['maintenance_mode'] = 'disable'
|
2016-02-19 22:52:38 +08:00
|
|
|
|
|
|
|
compute_client = self.app.client_manager.compute
|
2016-06-20 19:40:26 +08:00
|
|
|
|
2023-05-08 10:48:54 +01:00
|
|
|
compute_client.api.host_set(parsed_args.host, **kwargs)
|
2016-02-19 22:52:38 +08:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ShowHost(command.Lister):
|
2022-12-06 21:44:23 -05:00
|
|
|
_description = _("DEPRECATED: Display host details")
|
2013-03-06 15:18:04 -06:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
2022-12-06 21:44:23 -05:00
|
|
|
parser = super().get_parser(prog_name)
|
2023-05-08 10:48:54 +01:00
|
|
|
parser.add_argument("host", metavar="<host>", help=_("Name of host"))
|
2013-03-06 15:18:04 -06:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
2022-12-06 21:44:23 -05:00
|
|
|
compute_client = self.app.client_manager.sdk_connection.compute
|
2023-05-08 10:48:54 +01:00
|
|
|
columns = ("Host", "Project", "CPU", "Memory MB", "Disk GB")
|
2018-03-05 14:18:41 -06:00
|
|
|
|
2022-12-06 21:44:23 -05:00
|
|
|
self.log.warning(
|
|
|
|
"API has been deprecated. "
|
|
|
|
"Please consider using 'hypervisor show' instead."
|
|
|
|
)
|
|
|
|
|
|
|
|
# doing this since openstacksdk has decided not to support this
|
|
|
|
# deprecated command
|
2023-05-08 10:48:54 +01:00
|
|
|
resources = (
|
|
|
|
compute_client.get(
|
|
|
|
'/os-hosts/' + parsed_args.host, microversion='2.1'
|
|
|
|
)
|
|
|
|
.json()
|
|
|
|
.get('host')
|
|
|
|
)
|
2022-12-06 21:44:23 -05:00
|
|
|
|
|
|
|
data = []
|
|
|
|
if resources is not None:
|
|
|
|
for resource in resources:
|
|
|
|
data.append(resource['resource'])
|
2018-03-05 14:18:41 -06:00
|
|
|
|
2022-12-06 21:44:23 -05:00
|
|
|
return columns, (utils.get_dict_properties(s, columns) for s in data)
|