4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Helpers for testing import-export daemon"""
29 from ganeti import constants
30 from ganeti import utils
31 from ganeti import objects
32 from ganeti import serializer
35 RETRY_INTERVAL = (0.1, 1.1, 1)
36 TIMEOUT = int(os.getenv("TIMEOUT", 30))
37 VALIDITY = int(os.getenv("VALIDITY", 1))
46 sys.stderr.write("%0.6f, pid %s: %s\n" % (time.time(), os.getpid(), line))
50 def _GetImportExportData(filename):
52 data = utils.ReadFile(filename)
53 except EnvironmentError, err:
54 Log("%s = %s", filename, err)
55 if err.errno != errno.ENOENT:
57 raise utils.RetryAgain()
59 Log("%s = %s", filename, data.strip())
61 return objects.ImportExportStatus.FromDict(serializer.LoadJson(data))
64 def _CheckConnected(filename):
65 if not _GetImportExportData(filename).connected:
67 raise utils.RetryAgain()
72 def _CheckListenPort(filename):
73 port = _GetImportExportData(filename).listen_port
76 raise utils.RetryAgain()
77 Log("Listening on %s", port)
81 def WaitForListenPort(filename):
82 return utils.Retry(_CheckListenPort, RETRY_INTERVAL, TIMEOUT,
86 def WaitForConnected(filename):
87 utils.Retry(_CheckConnected, RETRY_INTERVAL, TIMEOUT, args=(filename, ))
91 (filename, what) = sys.argv[1:]
93 Log("Running helper for %s %s", filename, what)
95 if what == "listen-port":
96 print WaitForListenPort(filename)
97 elif what == "connected":
98 WaitForConnected(filename)
99 elif what == "gencert":
100 utils.GenerateSelfSignedSslCert(filename, validity=VALIDITY)
102 raise Exception("Unknown command '%s'" % what)
105 if __name__ == "__main__":