Rewrite to use ansible.
[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 import mclu_build
27 import mclu_console
28 import mclu_import
29 import mclu_info
30 import mclu_list
31 import mclu_migrate
32 import mclu_off
33 import mclu_on
34 import mclu_reboot
35 import mclu_start
36 import mclu_status
37 import mclu_stop
38 import mclu_viewer
39
40 parser = argparse.ArgumentParser (
41     prog='mclu',
42     description='Mini virtualization cluster management tool',
43 )
44 parser.add_argument (
45     '-f',
46     type=file,
47     help='specify location of the configuration file',
48     metavar='MCLU.CONF',
49 )
50
51 # Add subcommands.
52 subparsers = parser.add_subparsers ()
53 mclu_build.cmdline (subparsers)
54 mclu_console.cmdline (subparsers)
55 mclu_import.cmdline (subparsers)
56 mclu_info.cmdline (subparsers)
57 mclu_list.cmdline (subparsers)
58 mclu_migrate.cmdline (subparsers)
59 mclu_off.cmdline (subparsers)
60 mclu_on.cmdline (subparsers)
61 mclu_reboot.cmdline (subparsers)
62 mclu_start.cmdline (subparsers)
63 mclu_status.cmdline (subparsers)
64 mclu_stop.cmdline (subparsers)
65 mclu_viewer.cmdline (subparsers)
66
67 args = parser.parse_args()
68
69 # Default location of the config file if not defined on the command line:
70 if not args.f:
71     config_dir = config.sysconfdir + "/" + config.PACKAGE_NAME
72     args.f = open (config_dir + "/" + "mclu.conf")
73 else:
74     # config_dir is the directory containing mclu.conf
75     config_dir = os.path.dirname (args.f.name)
76
77 # Configuration file default settings.  You cannot set defaults per
78 # section, so we have to rely on setting names not overlapping.
79 conf_defaults = {
80     "home" : os.getenv ("HOME"),
81     "config_dir" : config_dir,
82 }
83
84 # Read the configuration file.
85 conf = ConfigParser.SafeConfigParser (conf_defaults)
86 conf.readfp (args.f)
87
88 # Global configuration.
89 images_dir = conf.get ("global", "images_dir")
90 if not os.path.isdir (images_dir):
91     sys.exit ("configuration error: [globals] 'images_dir' (%s) directory does not exist" % images_dir)
92 xmls_dir = conf.get ("global", "xmls_dir")
93 if not os.path.isdir (xmls_dir):
94     sys.exit ("configuration error: [globals] 'xmls_dir' (%s) directory does not exist", xmls_dir)
95 nodes_group = conf.get ("global", "nodes_group")
96
97 # A config dict with less-used configuration settings.
98 c = {
99     "config_file" : args.f.name,
100     "config_dir" : config_dir,
101     "images_dir" : images_dir,
102     "nodes_group" : nodes_group,
103     "xmls_dir" : xmls_dir,
104
105     "conf" : conf,
106 }
107
108 # Run the subcommand.
109 args.run (c, args)