7c6ad81b480d8932f5d6fd58cf0acf749d8f70b0
[mclu.git] / mclu.py
1 #!/usr/bin/python
2 # mclu (mini cluster)
3 # Copyright (C) 2014 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import argparse
20 import ConfigParser
21 import os
22 import re
23 import sys
24
25 import config
26 from node import Node
27
28 parser = argparse.ArgumentParser (
29     prog='mclu',
30     description='Mini virtualization cluster management tool',
31 )
32 parser.add_argument (
33     '-f',
34     type=file,
35     help='specify location of the configuration file',
36     metavar='MCLU.CONF',
37 )
38
39 # Add subcommands.
40 subparsers = parser.add_subparsers ()
41 import mclu_build
42 mclu_build.cmdline (subparsers)
43 import mclu_console
44 mclu_console.cmdline (subparsers)
45 import mclu_import
46 mclu_import.cmdline (subparsers)
47 import mclu_info
48 mclu_info.cmdline (subparsers)
49 import mclu_list
50 mclu_list.cmdline (subparsers)
51 import mclu_migrate
52 mclu_migrate.cmdline (subparsers)
53 import mclu_off
54 mclu_off.cmdline (subparsers)
55 import mclu_on
56 mclu_on.cmdline (subparsers)
57 import mclu_reboot
58 mclu_reboot.cmdline (subparsers)
59 import mclu_start
60 mclu_start.cmdline (subparsers)
61 import mclu_status
62 mclu_status.cmdline (subparsers)
63 import mclu_stop
64 mclu_stop.cmdline (subparsers)
65 import mclu_viewer
66 mclu_viewer.cmdline (subparsers)
67
68 args = parser.parse_args()
69
70 # Default location of the config file if not defined on the command line:
71 if not args.f:
72     config_dir = config.sysconfdir + "/" + config.PACKAGE_NAME
73     args.f = open (config_dir + "/" + "mclu.conf")
74 else:
75     # config_dir is the directory containing mclu.conf
76     config_dir = os.path.dirname (args.f.name)
77
78 # Configuration file default settings.  You cannot set defaults per
79 # section, so we have to rely on setting names not overlapping.
80 conf_defaults = {
81     "home" : os.getenv ("HOME"),
82     "host" : "SET.THIS.IN.MCLU.CONF",
83     "config_dir" : config_dir,
84     "uri" : "qemu+ssh://root@%(host)s/system",
85 }
86
87 # Read the configuration file.
88 conf = ConfigParser.SafeConfigParser (conf_defaults)
89 conf.readfp (args.f)
90
91 # Global configuration.
92 images_dir = conf.get ("global", "images_dir")
93 if not os.path.isdir (images_dir):
94     sys.exit ("configuration error: [globals] 'images_dir' (%s) directory does not exist" % images_dir)
95 xmls_dir = conf.get ("global", "xmls_dir")
96 if not os.path.isdir (xmls_dir):
97     sys.exit ("configuration error: [globals] 'xmls_dir' (%s) directory does not exist", xmls_dir)
98
99 # Get the list of node names.
100 node_names = conf.items ("nodes")
101 node_names = filter (lambda (x, _) : re.search (r'^node', x), node_names)
102 node_names = [ value for _, value in node_names ]
103 if not node_names:
104     sys.exit ("configuration error: [nodes] section in configuration file is empty")
105
106 # Get information about each node.
107 nodes = {}
108 for node_name in node_names:
109     host = conf.get (node_name, "host")
110     if not host:
111         host = node_name
112     mac = conf.get (node_name, "mac")
113     uri = conf.get (node_name, "uri")
114     node = Node (node_name, host, mac, uri)
115     nodes[node_name] = node
116
117 # A config dict with less-used configuration settings.
118 c = {
119     "config_file" : args.f.name,
120     "config_dir" : config_dir,
121     "images_dir" : images_dir,
122     "node_names" : node_names,
123     "xmls_dir" : xmls_dir,
124
125     "conf" : conf,
126 }
127
128 # Run the subcommand.
129 args.run (c, args, nodes)