Statistics
| Branch: | Tag: | Revision:

root / tools / ovfconverter @ ea2bcb82

History | View | Annotate | Download (6.2 kB)

1 ced78a66 Agata Murawska
#!/usr/bin/python
2 ced78a66 Agata Murawska
#
3 ced78a66 Agata Murawska
4 ced78a66 Agata Murawska
# Copyright (C) 2011 Google Inc.
5 ced78a66 Agata Murawska
#
6 ced78a66 Agata Murawska
# This program is free software; you can redistribute it and/or modify
7 ced78a66 Agata Murawska
# it under the terms of the GNU General Public License as published by
8 ced78a66 Agata Murawska
# the Free Software Foundation; either version 2 of the License, or
9 ced78a66 Agata Murawska
# (at your option) any later version.
10 ced78a66 Agata Murawska
#
11 ced78a66 Agata Murawska
# This program is distributed in the hope that it will be useful, but
12 ced78a66 Agata Murawska
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 ced78a66 Agata Murawska
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ced78a66 Agata Murawska
# General Public License for more details.
15 ced78a66 Agata Murawska
#
16 ced78a66 Agata Murawska
# You should have received a copy of the GNU General Public License
17 ced78a66 Agata Murawska
# along with this program; if not, write to the Free Software
18 ced78a66 Agata Murawska
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 ced78a66 Agata Murawska
# 02110-1301, USA.
20 ced78a66 Agata Murawska
21 ced78a66 Agata Murawska
22 ced78a66 Agata Murawska
"""Tool to translate between ovf and ganeti backup format.
23 ced78a66 Agata Murawska
24 ced78a66 Agata Murawska
"""
25 ced78a66 Agata Murawska
26 ced78a66 Agata Murawska
import logging
27 ced78a66 Agata Murawska
import optparse
28 ced78a66 Agata Murawska
import os
29 ced78a66 Agata Murawska
30 ced78a66 Agata Murawska
from ganeti import cli
31 ced78a66 Agata Murawska
from ganeti import constants
32 ced78a66 Agata Murawska
from ganeti import errors
33 ced78a66 Agata Murawska
from ganeti import ovf
34 796b5152 Michael Hanselmann
from ganeti import utils
35 ced78a66 Agata Murawska
36 ced78a66 Agata Murawska
37 ced78a66 Agata Murawska
IMPORT_MODE = "import"
38 ced78a66 Agata Murawska
EXPORT_MODE = "export"
39 ced78a66 Agata Murawska
40 ced78a66 Agata Murawska
41 ced78a66 Agata Murawska
def CheckOptions(parser, options_dict, required, forbidden, excluding, mode):
42 ced78a66 Agata Murawska
  """Performes check on the command line options.
43 ced78a66 Agata Murawska
44 ced78a66 Agata Murawska
  Checks whether the required arguments are present and if none of the arguments
45 ced78a66 Agata Murawska
  not supported for the current mode are given.
46 ced78a66 Agata Murawska
47 ced78a66 Agata Murawska
  @type options_dict: list
48 ced78a66 Agata Murawska
  @param options_dict: dictionary containing all the options from the command
49 ced78a66 Agata Murawska
    line
50 ced78a66 Agata Murawska
  @type required: list
51 ced78a66 Agata Murawska
  @param required: list of pairs (option, argument) where 'option' is required
52 ced78a66 Agata Murawska
    in mode 'mode'
53 ced78a66 Agata Murawska
  @type forbidden: list
54 ced78a66 Agata Murawska
  @param forbidden: list of pairs (option, argument) which are not allowed in
55 ced78a66 Agata Murawska
    mode 'mode'
56 ced78a66 Agata Murawska
  @type excluding: list
57 ced78a66 Agata Murawska
  @param excluding: list of pairs (argument1, argument2); each pair contains
58 ced78a66 Agata Murawska
    mutually exclusive arguments
59 ced78a66 Agata Murawska
  @type mode: string
60 ced78a66 Agata Murawska
  @param mode: current mode of the converter
61 ced78a66 Agata Murawska
62 ced78a66 Agata Murawska
  """
63 ced78a66 Agata Murawska
  for (option, argument) in required:
64 ced78a66 Agata Murawska
    if not options_dict[option]:
65 ced78a66 Agata Murawska
      parser.error("Argument %s is required for %s" % (argument, mode))
66 ced78a66 Agata Murawska
  for (option, argument) in forbidden:
67 ced78a66 Agata Murawska
    if options_dict[option]:
68 ced78a66 Agata Murawska
      parser.error("Argument %s is not allowed in %s mode" % (argument, mode))
69 ced78a66 Agata Murawska
  for (arg1, arg2) in excluding:
70 ced78a66 Agata Murawska
    if options_dict[arg1] and options_dict[arg2]:
71 ced78a66 Agata Murawska
      parser.error("Arguments %s and %s exclude each other" % (arg1, arg2))
72 ced78a66 Agata Murawska
73 ced78a66 Agata Murawska
74 ced78a66 Agata Murawska
def ParseOptions():
75 ced78a66 Agata Murawska
  """Parses the command line options and arguments.
76 ced78a66 Agata Murawska
77 ced78a66 Agata Murawska
  In case of mismatching parameters, it will show the correct usage and exit.
78 ced78a66 Agata Murawska
79 ced78a66 Agata Murawska
  @rtype: tuple
80 ced78a66 Agata Murawska
  @return: (mode, sourcefile to read from, additional options)
81 ced78a66 Agata Murawska
82 ced78a66 Agata Murawska
  """
83 ced78a66 Agata Murawska
  usage = ("%%prog {%s|%s} <source-cfg-file> [options...]" %
84 ced78a66 Agata Murawska
           (IMPORT_MODE, EXPORT_MODE))
85 ced78a66 Agata Murawska
  parser = optparse.OptionParser(usage=usage)
86 ced78a66 Agata Murawska
87 ced78a66 Agata Murawska
  #global options
88 ced78a66 Agata Murawska
  parser.add_option(cli.DEBUG_OPT)
89 ced78a66 Agata Murawska
  parser.add_option(cli.VERBOSE_OPT)
90 ced78a66 Agata Murawska
  parser.add_option("-n", "--name", dest="name", action="store",
91 ced78a66 Agata Murawska
                    help="Name of the instance")
92 ced78a66 Agata Murawska
  parser.add_option("--output-dir", dest="output_dir",
93 ced78a66 Agata Murawska
                    help="Path to the output directory")
94 ced78a66 Agata Murawska
95 ced78a66 Agata Murawska
  #import options
96 ced78a66 Agata Murawska
  import_group = optparse.OptionGroup(parser, "Import options")
97 ced78a66 Agata Murawska
  import_group.add_option(cli.BACKEND_OPT)
98 ced78a66 Agata Murawska
  import_group.add_option(cli.DISK_OPT)
99 ced78a66 Agata Murawska
  import_group.add_option(cli.DISK_TEMPLATE_OPT)
100 ced78a66 Agata Murawska
  import_group.add_option(cli.HYPERVISOR_OPT)
101 ced78a66 Agata Murawska
  import_group.add_option(cli.NET_OPT)
102 ced78a66 Agata Murawska
  import_group.add_option(cli.NONICS_OPT)
103 ced78a66 Agata Murawska
  import_group.add_option(cli.OS_OPT)
104 ced78a66 Agata Murawska
  import_group.add_option(cli.OSPARAMS_OPT)
105 ced78a66 Agata Murawska
  import_group.add_option(cli.TAG_ADD_OPT)
106 ced78a66 Agata Murawska
  parser.add_option_group(import_group)
107 ced78a66 Agata Murawska
108 ced78a66 Agata Murawska
  #export options
109 ced78a66 Agata Murawska
  export_group = optparse.OptionGroup(parser, "Export options")
110 ced78a66 Agata Murawska
  export_group.add_option("--compress", dest="compression",
111 ced78a66 Agata Murawska
                          action="store_true", default=False,
112 ced78a66 Agata Murawska
                          help="The exported disk will be compressed to tar.gz")
113 ced78a66 Agata Murawska
  export_group.add_option("--external", dest="ext_usage",
114 ced78a66 Agata Murawska
                          action="store_true", default=False,
115 ced78a66 Agata Murawska
                          help="The package will be used externally (ommits the"
116 ced78a66 Agata Murawska
                               " Ganeti-specific parts of configuration)")
117 ced78a66 Agata Murawska
  export_group.add_option("-f", "--format", dest="disk_format",
118 ced78a66 Agata Murawska
                          action="store",
119 ced78a66 Agata Murawska
                          choices=("raw", "cow", "vmdk"),
120 ced78a66 Agata Murawska
                          help="Disk format for export (one of raw/cow/vmdk)")
121 ced78a66 Agata Murawska
  export_group.add_option("--ova", dest="ova_package",
122 ced78a66 Agata Murawska
                          action="store_true", default=False,
123 ced78a66 Agata Murawska
                          help="Export everything into OVA package")
124 ced78a66 Agata Murawska
  parser.add_option_group(export_group)
125 ced78a66 Agata Murawska
126 ced78a66 Agata Murawska
  options, args = parser.parse_args()
127 ced78a66 Agata Murawska
  if len(args) != 2:
128 ced78a66 Agata Murawska
    parser.error("Wrong number of arguments")
129 ced78a66 Agata Murawska
  mode = args.pop(0)
130 ced78a66 Agata Murawska
  input_path = os.path.abspath(args.pop(0))
131 ced78a66 Agata Murawska
132 ced78a66 Agata Murawska
  if mode == IMPORT_MODE:
133 ced78a66 Agata Murawska
    required = []
134 ced78a66 Agata Murawska
    forbidden = [
135 ced78a66 Agata Murawska
      ("compression", "--compress"),
136 ced78a66 Agata Murawska
      ("disk_format", "--format"),
137 ced78a66 Agata Murawska
      ("ext_usage", "--external"),
138 ced78a66 Agata Murawska
      ("ova_package", "--ova"),
139 ced78a66 Agata Murawska
    ]
140 ced78a66 Agata Murawska
    excluding = [("nics", "no_nics")]
141 ced78a66 Agata Murawska
  elif mode == EXPORT_MODE:
142 ced78a66 Agata Murawska
    required = [("disk_format", "--format")]
143 ced78a66 Agata Murawska
    forbidden = [
144 ced78a66 Agata Murawska
      ("beparams", "--backend-parameters"),
145 ced78a66 Agata Murawska
      ("disk_template", "--disk-template"),
146 ced78a66 Agata Murawska
      ("disks", "--disk"),
147 ced78a66 Agata Murawska
      ("hypervisor", "--hypervisor-parameters"),
148 ced78a66 Agata Murawska
      ("nics", "--net"),
149 ced78a66 Agata Murawska
      ("no_nics", "--no-nics"),
150 ced78a66 Agata Murawska
      ("os", "--os-type"),
151 ced78a66 Agata Murawska
      ("osparams", "--os-parameters"),
152 ced78a66 Agata Murawska
      ("tags", "--tags"),
153 ced78a66 Agata Murawska
    ]
154 ced78a66 Agata Murawska
    excluding = []
155 ced78a66 Agata Murawska
  else:
156 ced78a66 Agata Murawska
    parser.error("First argument should be either '%s' or '%s'" %
157 ced78a66 Agata Murawska
                 (IMPORT_MODE, EXPORT_MODE))
158 ced78a66 Agata Murawska
159 ced78a66 Agata Murawska
  options_dict = vars(options)
160 ced78a66 Agata Murawska
  CheckOptions(parser, options_dict, required, forbidden, excluding, mode)
161 ced78a66 Agata Murawska
162 ced78a66 Agata Murawska
  return (mode, input_path, options)
163 ced78a66 Agata Murawska
164 ced78a66 Agata Murawska
165 ced78a66 Agata Murawska
def main():
166 ced78a66 Agata Murawska
  """Main routine.
167 ced78a66 Agata Murawska
168 ced78a66 Agata Murawska
  """
169 ced78a66 Agata Murawska
  (mode, input_path, options) = ParseOptions()
170 796b5152 Michael Hanselmann
171 796b5152 Michael Hanselmann
  utils.SetupToolLogging(options.debug, options.verbose)
172 796b5152 Michael Hanselmann
173 ced78a66 Agata Murawska
  logging.info("Chosen %s mode, reading the %s file", mode, input_path)
174 ced78a66 Agata Murawska
  assert mode in (IMPORT_MODE, EXPORT_MODE)
175 ced78a66 Agata Murawska
  converter = None
176 ced78a66 Agata Murawska
  try:
177 ced78a66 Agata Murawska
    if mode == IMPORT_MODE:
178 ced78a66 Agata Murawska
      converter = ovf.OVFImporter(input_path, options)
179 ced78a66 Agata Murawska
    elif mode == EXPORT_MODE:
180 ced78a66 Agata Murawska
      converter = ovf.OVFExporter(input_path, options)
181 ced78a66 Agata Murawska
    converter.Parse()
182 ced78a66 Agata Murawska
    converter.Save()
183 ced78a66 Agata Murawska
  except errors.OpPrereqError, err:
184 ced78a66 Agata Murawska
    if converter:
185 ced78a66 Agata Murawska
      converter.Cleanup()
186 ced78a66 Agata Murawska
    logging.exception(err)
187 ced78a66 Agata Murawska
    return constants.EXIT_FAILURE
188 ced78a66 Agata Murawska
189 ced78a66 Agata Murawska
190 ced78a66 Agata Murawska
if __name__ == "__main__":
191 ced78a66 Agata Murawska
  main()