Added support for reading in / writing out host data to the etc/hosts.yml file. Added support for calling Kolla ansible start logic. Made host add / remove / list methods functional.

This commit is contained in:
Borne Mace 2015-07-17 17:11:05 -07:00
parent 6e8a302748
commit 727c83a5b1
6 changed files with 113 additions and 13 deletions

4
etc/hosts.yml Normal file
View File

@ -0,0 +1,4 @@
blue2: {IPAddr: addr, Services: '', Zone: ''}
blue3: {IPAddr: addr3, Services: '', Zone: ''}
host1: {IPAddr: ip, Services: '', Zone: ''}
host2: {IPAddr: ip, Services: '', Zone: ''}

View File

@ -1,6 +1,9 @@
import logging
import os
from kollaclient.i18n import _
from kollaclient.utils import get_kolla_etc
from kollaclient.utils import get_kolla_home
from cliff.command import Command
@ -12,7 +15,17 @@ class Deploy(Command):
def take_action(self, parsed_args):
self.log.info(_("deploy"))
self.app.stdout.write(parsed_args)
self.app.stdout.write(''.join(parsed_args))
class Install(Command):
"Install"
log = logging.getLogger(__name__)
def take_action(self, parsed_args):
self.log.info(_("install"))
self.app.stdout.write(''.join(parsed_args))
class List(Command):
@ -31,8 +44,18 @@ class Start(Command):
log = logging.getLogger(__name__)
def take_action(self, parsed_args):
kollaHome = get_kolla_home()
kollaEtc = get_kolla_etc()
self.log.info(_("start"))
self.app.stdout.write(parsed_args)
self.log.info(kollaHome)
cmd = 'ansible-playbook -i'
cmd = cmd + kollaHome + '/ansible/inventory/all-in-one'
cmd = cmd + ' -e @' + kollaEtc + '/kolla/defaults.yml'
cmd = cmd + ' -e @' + kollaEtc + '/kolla/globals.yml'
cmd = cmd + ' -e @' + kollaEtc + '/kolla/passwords.yml'
cmd = cmd + ' ' + kollaHome + '/ansible/site.yml'
self.log.info(cmd)
os.system(cmd)
class Stop(Command):
@ -42,7 +65,6 @@ class Stop(Command):
def take_action(self, parsed_args):
self.log.info(_("stop"))
self.app.stdout.write(parsed_args)
class Sync(Command):
@ -52,7 +74,6 @@ class Sync(Command):
def take_action(self, parsed_args):
self.log.info(_("sync"))
self.app.stdout.write(parsed_args)
class Upgrade(Command):
@ -62,4 +83,3 @@ class Upgrade(Command):
def take_action(self, parsed_args):
self.log.info(_("upgrade"))
self.app.stdout.write(parsed_args)

View File

@ -1,6 +1,8 @@
import logging
from kollaclient.i18n import _
from kollaclient.utils import read_etc_yaml
from kollaclient.utils import save_etc_yaml
from cliff.command import Command
@ -10,9 +12,26 @@ class HostAdd(Command):
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(HostAdd, self).get_parser(prog_name)
parser.add_argument('hostname')
parser.add_argument('ipaddress')
# TODO(bmace) error if args missing
return parser
def take_action(self, parsed_args):
self.log.info(_("host add"))
self.app.stdout.write(parsed_args)
hostname = parsed_args.hostname
ipaddr = parsed_args.ipaddress
contents = read_etc_yaml('hosts.yml')
for host, hostdata in contents.items():
if host == hostname:
# TODO(bmace) fix message
self.log.info(_("host already exists"))
return
hostEntry = {hostname: {'Services': '', 'IPAddr':
ipaddr, 'Zone': ''}}
contents.update(hostEntry)
save_etc_yaml('hosts.yml', contents)
class HostRemove(Command):
@ -20,9 +39,25 @@ class HostRemove(Command):
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(HostRemove, self).get_parser(prog_name)
parser.add_argument('hostname')
# TODO(bmace) error if arg missing
return parser
def take_action(self, parsed_args):
self.log.info(_("host remove"))
self.app.stdout.write(parsed_args)
hostname = parsed_args.hostname
contents = read_etc_yaml('hosts.yml')
foundHost = False
for host, hostdata in contents.items():
if host == hostname:
foundHost = True
if foundHost:
del contents[hostname]
else:
# TODO(bmace) fix message
self.log.info("no host by name (" + hostname + ") found")
save_etc_yaml('hosts.yml', contents)
class HostList(Command):
@ -32,7 +67,11 @@ class HostList(Command):
def take_action(self, parsed_args):
self.log.info(_("host list"))
self.app.stdout.write(parsed_args)
contents = read_etc_yaml('hosts.yml')
# TODO(bmace) fix output format
for host, hostdata in contents.items():
self.log.info(host)
self.log.info(hostdata)
class HostSetzone(Command):

35
kollaclient/utils.py Normal file
View File

@ -0,0 +1,35 @@
import os
import yaml
def get_env(var, default):
value = os.environ.get(var, None)
if value:
return value
return default
def get_kolla_home():
return get_env("KOLLA_HOME", "/opt/kolla")
def get_kolla_etc():
return get_env("KOLLA_ETC", "/etc")
def get_client_etc():
return get_env("KOLLA_CLIENT_ETC", "/etc/kollaclient/")
def get_client_home():
return get_env("KOLLA_CLIENT_HOME", "/opt/kollaclient/")
def read_etc_yaml(fileName):
with open(get_client_etc() + fileName, 'r') as f:
return yaml.load(f)
def save_etc_yaml(fileName, contents):
with open(get_client_etc() + fileName, 'w') as f:
f.write(yaml.dump(contents))

View File

@ -1,6 +1,7 @@
pbr>=0.11,<2.0
six>=1.9.0
Babel>=1.3
cliff>=1.10.0 # Apache-2.0
docker-py>=1.3
oslo.i18n>=1.5.0 # Apache-2.0
pbr>=0.11,<2.0
PyYAML
six>=1.9.0

View File

@ -47,6 +47,7 @@ kolla.client =
property_set = kollaclient.property:PropertySet
property_list = kollaclient.property:PropertyList
deploy = kollaclient.common:Deploy
install = kollaclient.common:Install
list = kollaclient.common:List
start = kollaclient.common:Start
stop = kollaclient.common:Stop