mclu initial commit.
[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 os
20 import re
21 import argparse
22 import ConfigParser
23
24 import config
25 from node import Node
26
27 parser = argparse.ArgumentParser (
28     prog='mclu',
29     description='Mini virtualization cluster management tool',
30 )
31 parser.add_argument (
32     '-f',
33     type=file,
34     help='specify location of the configuration file',
35     metavar='MCLU.CONF',
36 )
37
38 # Add subcommands.
39 subparsers = parser.add_subparsers ()
40 import mclu_build
41 mclu_build.cmdline (subparsers)
42 import mclu_console
43 mclu_console.cmdline (subparsers)
44 import mclu_info
45 mclu_info.cmdline (subparsers)
46 import mclu_list
47 mclu_list.cmdline (subparsers)
48 import mclu_migrate
49 mclu_migrate.cmdline (subparsers)
50 import mclu_shutdown
51 mclu_shutdown.cmdline (subparsers)
52 import mclu_start
53 mclu_start.cmdline (subparsers)
54 import mclu_status
55 mclu_status.cmdline (subparsers)
56 import mclu_stop
57 mclu_stop.cmdline (subparsers)
58 import mclu_viewer
59 mclu_viewer.cmdline (subparsers)
60 import mclu_wake
61 mclu_wake.cmdline (subparsers)
62
63 args = parser.parse_args()
64
65 # Default location of the config file if not defined on the command line:
66 if not args.f:
67     config_dir = config.sysconfdir + "/" + config.PACKAGE_NAME
68     args.f = open (config_dir + "/" + "mclu.conf")
69 else:
70     # config_dir is the directory containing mclu.conf
71     config_dir = os.path.dirname (args.f.name)
72
73 # Configuration file default settings.  You cannot set defaults per
74 # section, so we have to rely on setting names not overlapping.
75 conf_defaults = {
76     "home" : os.getenv ("HOME"),
77     "host" : "SET.THIS.IN.MCLU.CONF",
78     "config_dir" : config_dir,
79     "uri" : "qemu+ssh://root@%(host)s/system",
80 }
81
82 # Read the configuration file.
83 conf = ConfigParser.SafeConfigParser (conf_defaults)
84 conf.readfp (args.f)
85
86 # Global configuration.
87 images_dir = conf.get ("global", "images_dir")
88 if not os.path.isdir (images_dir):
89     sys.exit ("configuration error: [globals] 'images_dir' (%s) directory does not exist" % images_dir)
90 xmls_dir = conf.get ("global", "xmls_dir")
91 if not os.path.isdir (xmls_dir):
92     sys.exit ("configuration error: [globals] 'xmls_dir' (%s) directory does not exist", xmls_dir)
93
94 # Get the list of node names.
95 node_names = conf.items ("nodes")
96 node_names = filter (lambda (x, _) : re.search (r'^node', x), node_names)
97 node_names = [ value for _, value in node_names ]
98 if not node_names:
99     sys.exit ("configuration error: [nodes] section in configuration file is empty")
100
101 # Get information about each node.
102 nodes = {}
103 for node_name in node_names:
104     host = conf.get (node_name, "host")
105     if not host:
106         host = node
107     mac = conf.get (node_name, "mac")
108     uri = conf.get (node_name, "uri")
109     node = Node (node_name, host, mac, uri)
110     nodes[node_name] = node
111
112 # A config dict with less-used configuration settings.
113 c = {
114     "config_file" : args.f.name,
115     "config_dir" : config_dir,
116     "images_dir" : images_dir,
117     "node_names" : node_names,
118     "xmls_dir" : xmls_dir,
119
120     "conf" : conf,
121 }
122
123 # Run the subcommand.
124 args.run (c, args, nodes)