Initial commit for ovfconverter tool
[ganeti-local] / lib / ovf.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Converter tools between ovf and ganeti config file
23
24 """
25
26 import os.path
27 import shutil
28
29 from ganeti import errors
30 from ganeti import utils
31
32
33 class Converter(object):
34   """Converter class for OVF packages.
35
36   Converter is a class above both ImporterOVF and ExporterOVF. It's purpose is
37   to provide a common interface for the two.
38
39   @type options: optparse.Values
40   @ivar options: options parsed from the command line
41   @type output_dir: string
42   @ivar output_dir: directory to which the results of conversion shall be
43     written
44   @type temp_file_manager: L{utils.TemporaryFileManager}
45   @ivar temp_file_manager: container for temporary files created during
46     conversion
47   @type temp_dir: string
48   @ivar temp_dir: temporary directory created then we deal with OVA
49
50   """
51   def __init__(self, input_path, options):
52     """Initialize the converter.
53
54     @type input_path: string
55     @param input_path: path to the Converter input file
56     @type options: optparse.Values
57     @param options: command line options
58
59     @raise errors.OpPrereqError: if file does not exist
60
61     """
62     input_path = os.path.abspath(input_path)
63     if not os.path.isfile(input_path):
64       raise errors.OpPrereqError("File does not exist: %s" % input_path)
65     self.options = options
66     self.temp_file_manager = utils.TemporaryFileManager()
67     self.temp_dir = None
68     self.output_dir = None
69     self._ReadInputData(input_path)
70
71   def _ReadInputData(self, input_path):
72     """Reads the data on which the conversion will take place.
73
74     @type input_path: string
75     @param input_path: absolute path to the Converter input file
76
77     """
78     raise NotImplementedError()
79
80   def Parse(self):
81     """Parses the data and creates a structure containing all required info.
82
83     """
84     raise NotImplementedError()
85
86   def Save(self):
87     """Saves the gathered configuration in an apropriate format.
88
89     """
90     raise NotImplementedError()
91
92   def Cleanup(self):
93     """Cleans the temporary directory, if one was created.
94
95     """
96     self.temp_file_manager.Cleanup()
97     if self.temp_dir:
98       shutil.rmtree(self.temp_dir)
99       self.temp_dir = None
100
101
102 class OVFImporter(Converter):
103   def _ReadInputData(self, input_path):
104     pass
105
106   def Parse(self):
107     pass
108
109   def Save(self):
110     pass
111
112
113 class OVFExporter(Converter):
114   def _ReadInputData(self, input_path):
115     pass
116
117   def Parse(self):
118     pass
119
120   def Save(self):
121     pass