#!/usr/bin/python # mclu (mini cluster) # Copyright (C) 2014 Red Hat Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os import re import argparse import ConfigParser import config from node import Node parser = argparse.ArgumentParser ( prog='mclu', description='Mini virtualization cluster management tool', ) parser.add_argument ( '-f', type=file, help='specify location of the configuration file', metavar='MCLU.CONF', ) # Add subcommands. subparsers = parser.add_subparsers () import mclu_build mclu_build.cmdline (subparsers) import mclu_console mclu_console.cmdline (subparsers) import mclu_info mclu_info.cmdline (subparsers) import mclu_list mclu_list.cmdline (subparsers) import mclu_migrate mclu_migrate.cmdline (subparsers) import mclu_shutdown mclu_shutdown.cmdline (subparsers) import mclu_start mclu_start.cmdline (subparsers) import mclu_status mclu_status.cmdline (subparsers) import mclu_stop mclu_stop.cmdline (subparsers) import mclu_viewer mclu_viewer.cmdline (subparsers) import mclu_wake mclu_wake.cmdline (subparsers) args = parser.parse_args() # Default location of the config file if not defined on the command line: if not args.f: config_dir = config.sysconfdir + "/" + config.PACKAGE_NAME args.f = open (config_dir + "/" + "mclu.conf") else: # config_dir is the directory containing mclu.conf config_dir = os.path.dirname (args.f.name) # Configuration file default settings. You cannot set defaults per # section, so we have to rely on setting names not overlapping. conf_defaults = { "home" : os.getenv ("HOME"), "host" : "SET.THIS.IN.MCLU.CONF", "config_dir" : config_dir, "uri" : "qemu+ssh://root@%(host)s/system", } # Read the configuration file. conf = ConfigParser.SafeConfigParser (conf_defaults) conf.readfp (args.f) # Global configuration. images_dir = conf.get ("global", "images_dir") if not os.path.isdir (images_dir): sys.exit ("configuration error: [globals] 'images_dir' (%s) directory does not exist" % images_dir) xmls_dir = conf.get ("global", "xmls_dir") if not os.path.isdir (xmls_dir): sys.exit ("configuration error: [globals] 'xmls_dir' (%s) directory does not exist", xmls_dir) # Get the list of node names. node_names = conf.items ("nodes") node_names = filter (lambda (x, _) : re.search (r'^node', x), node_names) node_names = [ value for _, value in node_names ] if not node_names: sys.exit ("configuration error: [nodes] section in configuration file is empty") # Get information about each node. nodes = {} for node_name in node_names: host = conf.get (node_name, "host") if not host: host = node mac = conf.get (node_name, "mac") uri = conf.get (node_name, "uri") node = Node (node_name, host, mac, uri) nodes[node_name] = node # A config dict with less-used configuration settings. c = { "config_file" : args.f.name, "config_dir" : config_dir, "images_dir" : images_dir, "node_names" : node_names, "xmls_dir" : xmls_dir, "conf" : conf, } # Run the subcommand. args.run (c, args, nodes)