fix manage db code style.

Change-Id: I824e6b6275b5bffce970c447020c237779b7932a
This commit is contained in:
xiaodongwang 2014-01-28 12:58:22 -08:00
parent 16ebf00865
commit 2f5c551505

View File

@ -1,10 +1,10 @@
#!/usr/bin/python #!/usr/bin/python
"""utility binary to manage database."""
import logging import logging
import os import os
import os.path import os.path
import re import re
import shutil import shutil
import sys
from flask.ext.script import Manager from flask.ext.script import Manager
@ -45,7 +45,7 @@ flags.add('print_config_properties',
default='') default='')
manager = Manager(app, usage="Perform database operations") app_manager = Manager(app, usage="Perform database operations")
TABLE_MAPPING = { TABLE_MAPPING = {
@ -62,14 +62,14 @@ TABLE_MAPPING = {
} }
@manager.command @app_manager.command
def list_config(): def list_config():
"List the configuration" "List the configuration"
for key, value in app.config.items(): for key, value in app.config.items():
print key, value print key, value
@manager.command @app_manager.command
def createdb(): def createdb():
"Creates database from sqlalchemy models" "Creates database from sqlalchemy models"
if setting.DATABASE_TYPE == 'file': if setting.DATABASE_TYPE == 'file':
@ -79,13 +79,13 @@ def createdb():
if setting.DATABASE_TYPE == 'file': if setting.DATABASE_TYPE == 'file':
os.chmod(setting.DATABASE_FILE, 0777) os.chmod(setting.DATABASE_FILE, 0777)
@manager.command @app_manager.command
def dropdb(): def dropdb():
"Drops database from sqlalchemy models" "Drops database from sqlalchemy models"
database.drop_db() database.drop_db()
@manager.command @app_manager.command
def createtable(): def createtable():
"""Create database table by --table_name""" """Create database table by --table_name"""
table_name = flags.OPTIONS.table_name table_name = flags.OPTIONS.table_name
@ -95,7 +95,7 @@ def createtable():
print '--table_name should be in %s' % TABLE_MAPPING.keys() print '--table_name should be in %s' % TABLE_MAPPING.keys()
@manager.command @app_manager.command
def droptable(): def droptable():
"""Drop database table by --talbe_name""" """Drop database table by --talbe_name"""
table_name = flags.OPTIONS.table_name table_name = flags.OPTIONS.table_name
@ -105,9 +105,10 @@ def droptable():
print '--table_name should be in %s' % TABLE_MAPPING.keys() print '--table_name should be in %s' % TABLE_MAPPING.keys()
@manager.command @app_manager.command
def sync_from_installers(): def sync_from_installers():
"""set adapters in Adapter table from installers.""" """set adapters in Adapter table from installers."""
# TODO(xiaodong): Move the code to config_manager.
manager = config_manager.ConfigManager() manager = config_manager.ConfigManager()
adapters = manager.get_adapters() adapters = manager.get_adapters()
target_systems = set() target_systems = set()
@ -127,32 +128,8 @@ def sync_from_installers():
session.add(Role(**role)) session.add(Role(**role))
@manager.command def _get_switch_ips(switch):
def sync_switch_configs(): """Helper function to get switch ips."""
"""Set switch configs in SwitchConfig table from setting.
.. note::
the switch config is stored in SWITCHES list in setting config.
for each entry in the SWITCHES, its type is dict and must contain
fields 'switch_ips' and 'filter_ports'.
The format of switch_ips is
<ip_blocks>.<ip_blocks>.<ip_blocks>.<ip_blocks>.
ip_blocks consists of ip_block separated by comma.
ip_block can be an integer and a range of integer like xx-xx.
The example of switch_ips is like: xxx.xxx.xxx-yyy,xxx-yyy.xxx,yyy
The format of filter_ports consists of list of
<port_prefix><port_range> separated by comma. port_range can be an
integer or a rnage of integer like xx-xx.
The example of filter_ports is like: ae1-5,20-40.
"""
if not hasattr(setting, 'SWITCHES') or not setting.SWITCHES:
logging.info('no switch configs to set')
return
switch_configs = []
port_pat = re.compile(r'(\D*)(\d+(?:-\d+)?)')
for switch in setting.SWITCHES:
ips = [] ips = []
blocks = switch['switch_ips'].split('.') blocks = switch['switch_ips'].split('.')
ip_blocks_list = [] ip_blocks_list = []
@ -194,7 +171,12 @@ def sync_switch_configs():
ips.append('.'.join(prefix)) ips.append('.'.join(prefix))
logging.debug('found switch ips: %s', ips) logging.debug('found switch ips: %s', ips)
return ips
def _get_switch_filter_ports(switch):
"""Helper function to get switch filter ports."""
port_pat = re.compile(r'(\D*)(\d+(?:-\d+)?)')
filter_ports = [] filter_ports = []
for port_range in switch['filter_ports'].split(','): for port_range in switch['filter_ports'].split(','):
if not port_range: if not port_range:
@ -221,11 +203,49 @@ def sync_switch_configs():
else: else:
filter_ports.append('%s%s' % (port_prefix, port_range)) filter_ports.append('%s%s' % (port_prefix, port_range))
for ip in ips: logging.debug('filter ports: %s', filter_ports)
return filter_ports
def _get_switch_config():
"""Helper function to get switch config."""
switch_configs = []
if not hasattr(setting, 'SWITCHES') or not setting.SWITCHES:
logging.info('no switch configs to set')
return switch_configs
for switch in setting.SWITCHES:
ips = _get_switch_ips(switch)
filter_ports = _get_switch_filter_ports(switch)
for ip_addr in ips:
for filter_port in filter_ports: for filter_port in filter_ports:
switch_configs.append( switch_configs.append(
{'ip': ip, 'filter_port': filter_port}) {'ip': ip_addr, 'filter_port': filter_port})
logging.debug('switch configs: %s', switch_configs)
return switch_configs
@app_manager.command
def sync_switch_configs():
"""Set switch configs in SwitchConfig table from setting.
.. note::
the switch config is stored in SWITCHES list in setting config.
for each entry in the SWITCHES, its type is dict and must contain
fields 'switch_ips' and 'filter_ports'.
The format of switch_ips is
<ip_blocks>.<ip_blocks>.<ip_blocks>.<ip_blocks>.
ip_blocks consists of ip_block separated by comma.
ip_block can be an integer and a range of integer like xx-xx.
The example of switch_ips is like: xxx.xxx.xxx-yyy,xxx-yyy.xxx,yyy
The format of filter_ports consists of list of
<port_prefix><port_range> separated by comma. port_range can be an
integer or a rnage of integer like xx-xx.
The example of filter_ports is like: ae1-5,20-40.
"""
switch_configs = _get_switch_config()
switch_config_tuples = set([]) switch_config_tuples = set([])
with database.session() as session: with database.session() as session:
session.query(SwitchConfig).delete(synchronize_session='fetch') session.query(SwitchConfig).delete(synchronize_session='fetch')
@ -243,6 +263,7 @@ def sync_switch_configs():
def _get_clusters(): def _get_clusters():
"""Helper function to get clusters from flag --clusters."""
clusters = {} clusters = {}
logging.debug('get clusters from flag: %s', flags.OPTIONS.clusters) logging.debug('get clusters from flag: %s', flags.OPTIONS.clusters)
for clusterid_and_hostnames in flags.OPTIONS.clusters.split(';'): for clusterid_and_hostnames in flags.OPTIONS.clusters.split(';'):
@ -290,6 +311,8 @@ def _get_clusters():
def _clean_clusters(clusters): def _clean_clusters(clusters):
"""Helper function to clean clusters."""
# TODO(xiaodong): Move the code to config manager.
manager = config_manager.ConfigManager() manager = config_manager.ConfigManager()
logging.info('clean cluster hosts: %s', clusters) logging.info('clean cluster hosts: %s', clusters)
with database.session() as session: with database.session() as session:
@ -345,7 +368,7 @@ def _clean_clusters(clusters):
manager.sync() manager.sync()
@manager.command @app_manager.command
def clean_clusters(): def clean_clusters():
"""Delete clusters and hosts. """Delete clusters and hosts.
@ -359,6 +382,8 @@ def clean_clusters():
def _clean_installation_progress(clusters): def _clean_installation_progress(clusters):
"""Helper function to clean installation progress."""
# TODO(xiaodong): Move the code to config manager.
logging.info('clean installation progress for cluster hosts: %s', logging.info('clean installation progress for cluster hosts: %s',
clusters) clusters)
with database.session() as session: with database.session() as session:
@ -424,7 +449,7 @@ def _clean_installation_progress(clusters):
}, synchronize_session='fetch') }, synchronize_session='fetch')
@manager.command @app_manager.command
def clean_installation_progress(): def clean_installation_progress():
"""Clean clusters and hosts installation progress. """Clean clusters and hosts installation progress.
@ -438,6 +463,8 @@ def clean_installation_progress():
def _reinstall_hosts(clusters): def _reinstall_hosts(clusters):
"""Helper function to reinstall hosts."""
# TODO(xiaodong): Move the code to config_manager.
logging.info('reinstall cluster hosts: %s', clusters) logging.info('reinstall cluster hosts: %s', clusters)
manager = config_manager.ConfigManager() manager = config_manager.ConfigManager()
with database.session() as session: with database.session() as session:
@ -505,7 +532,7 @@ def _reinstall_hosts(clusters):
manager.sync() manager.sync()
@manager.command @app_manager.command
def reinstall_hosts(): def reinstall_hosts():
"""Reinstall hosts in clusters. """Reinstall hosts in clusters.
@ -518,18 +545,8 @@ def reinstall_hosts():
os.system('service rsyslog restart') os.system('service rsyslog restart')
@manager.command def _get_fake_switch_machines(switch_ips, switch_machines):
def set_fake_switch_machine(): """Helper function to get fake switch machines."""
"""Set fake switches and machines.
.. note::
--fake_switches_vendor is the vendor name for all fake switches.
the default value is 'huawei'
--fake_switches_file is the filename which stores all fake switches
and fake machines.
each line in fake_switches_files presents one machine.
the format of each line <switch_ip>,<switch_port>,<vlan>,<mac>.
"""
missing_flags = False missing_flags = False
if not flags.OPTIONS.fake_switches_vendor: if not flags.OPTIONS.fake_switches_vendor:
print 'the flag --fake_switches_vendor should be specified' print 'the flag --fake_switches_vendor should be specified'
@ -546,19 +563,11 @@ def set_fake_switch_machine():
missing_flags = True missing_flags = True
if missing_flags: if missing_flags:
return return False
switch_ips = []
switch_machines = {}
vendor = flags.OPTIONS.fake_switches_vendor
credential = {
'version' : 'v2c',
'community' : 'public',
}
try: try:
with open(flags.OPTIONS.fake_switches_file) as f: with open(flags.OPTIONS.fake_switches_file) as switch_file:
for line in f: for line in switch_file:
line = line.strip() line = line.strip()
switch_ip, switch_port, vlan, mac = line.split(',', 3) switch_ip, switch_port, vlan, mac = line.split(',', 3)
if switch_ip not in switch_ips: if switch_ip not in switch_ips:
@ -574,6 +583,32 @@ def set_fake_switch_machine():
logging.error('failed to parse file %s', logging.error('failed to parse file %s',
flags.OPTIONS.fake_switches_file) flags.OPTIONS.fake_switches_file)
logging.exception(error) logging.exception(error)
return False
return True
@app_manager.command
def set_fake_switch_machine():
"""Set fake switches and machines.
.. note::
--fake_switches_vendor is the vendor name for all fake switches.
the default value is 'huawei'
--fake_switches_file is the filename which stores all fake switches
and fake machines.
each line in fake_switches_files presents one machine.
the format of each line <switch_ip>,<switch_port>,<vlan>,<mac>.
"""
# TODO(xiaodong): Move the main code to config manager.
switch_ips = []
switch_machines = {}
vendor = flags.OPTIONS.fake_switches_vendor
credential = {
'version' : 'v2c',
'community' : 'public',
}
if not _get_fake_switch_machines(switch_ips, switch_machines):
return return
with database.session() as session: with database.session() as session:
@ -597,6 +632,7 @@ def set_fake_switch_machine():
def _get_config_properties(): def _get_config_properties():
"""Helper function to get config properties."""
if not flags.OPTIONS.search_config_properties: if not flags.OPTIONS.search_config_properties:
logging.info('the flag --search_config_properties is not specified.') logging.info('the flag --search_config_properties is not specified.')
return {} return {}
@ -620,6 +656,7 @@ def _get_config_properties():
def _get_print_properties(): def _get_print_properties():
"""Helper function to get what properties to print."""
if not flags.OPTIONS.print_config_properties: if not flags.OPTIONS.print_config_properties:
logging.info('the flag --print_config_properties is not specified.') logging.info('the flag --print_config_properties is not specified.')
return [] return []
@ -638,6 +675,8 @@ def _get_print_properties():
def _match_config_properties(config, config_properties): def _match_config_properties(config, config_properties):
"""Helper function to check if config properties are match."""
# TODO(xiaodong): Move the code to config manager.
ref = config_reference.ConfigReference(config) ref = config_reference.ConfigReference(config)
for property_name, property_value in config_properties.items(): for property_name, property_value in config_properties.items():
config_value = ref.get(property_name) config_value = ref.get(property_name)
@ -661,6 +700,7 @@ def _match_config_properties(config, config_properties):
def _print_config_properties(config, config_properties): def _print_config_properties(config, config_properties):
"""Helper function to print config properties."""
ref = config_reference.ConfigReference(config) ref = config_reference.ConfigReference(config)
print_properties = [] print_properties = []
for property_name in config_properties: for property_name in config_properties:
@ -675,7 +715,7 @@ def _print_config_properties(config, config_properties):
print ';'.join(print_properties) print ';'.join(print_properties)
@manager.command @app_manager.command
def search_hosts(): def search_hosts():
"""Search hosts by properties. """Search hosts by properties.
@ -697,7 +737,7 @@ def search_hosts():
_print_config_properties(host.config, print_properties) _print_config_properties(host.config, print_properties)
@manager.command @app_manager.command
def search_clusters(): def search_clusters():
"""Search clusters by properties. """Search clusters by properties.
@ -722,4 +762,4 @@ def search_clusters():
if __name__ == "__main__": if __name__ == "__main__":
flags.init() flags.init()
logsetting.init() logsetting.init()
manager.run() app_manager.run()