kolla-cli/kollacli/group.py

114 lines
3.6 KiB
Python

# Copyright(c) 2015, Oracle and/or its affiliates. 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.
import logging
import traceback
from kollacli.ansible.inventory import Inventory
from kollacli.exceptions import CommandError
from cliff.command import Command
from cliff.lister import Lister
class GroupAdd(Command):
"""Add group to open-stack-kolla"""
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(GroupAdd, self).get_parser(prog_name)
parser.add_argument('groupname', metavar='<groupname>',
help='group')
return parser
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
inventory = Inventory.load()
inventory.add_group(groupname)
Inventory.save(inventory)
except CommandError as e:
raise e
except Exception as e:
raise Exception(traceback.format_exc())
class GroupRemove(Command):
"""Remove group from openstack-kolla"""
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(GroupRemove, self).get_parser(prog_name)
parser.add_argument('groupname', metavar='<groupname>',
help='group name')
return parser
def take_action(self, parsed_args):
try:
groupname = parsed_args.groupname.strip()
inventory = Inventory.load()
inventory.remove_group(groupname)
Inventory.save(inventory)
except CommandError as e:
raise e
except Exception as e:
raise Exception(traceback.format_exc())
class GroupListservices(Lister):
"""List all groups and their services"""
log = logging.getLogger(__name__)
def take_action(self, parsed_args):
try:
inventory = Inventory.load()
data = []
group_services = inventory.get_group_services()
if group_services:
for (groupname, servicenames) in group_services.items():
data.append((groupname, servicenames))
else:
data.append(('', ''))
return (('Group Name', 'Services'), sorted(data))
except CommandError as e:
raise e
except Exception as e:
raise Exception(traceback.format_exc())
class GroupListhosts(Lister):
"""List all groups and their hosts"""
log = logging.getLogger(__name__)
def take_action(self, parsed_args):
try:
inventory = Inventory.load()
data = []
group_hosts = inventory.get_group_hosts()
if group_hosts:
for (groupname, hostnames) in group_hosts.items():
data.append((groupname, hostnames))
else:
data.append(('', ''))
return (('Group Name', 'Hosts'), sorted(data))
except CommandError as e:
raise e
except Exception as e:
raise Exception(traceback.format_exc())