Merge branch 'master' of git.carlh.net:git/cdist
[cdist.git] / cdistvm
1 #!/usr/bin/python
2
3 import argparse
4 import subprocess
5 import shlex
6 import time
7 import os
8 import tempfile
9 import shutil
10
11 class Error(Exception):
12     def __init__(self, value):
13         self.value = value
14     def __str__(self):
15         return self.value
16     def __repr__(self):
17         return str(self)
18
19 parser = argparse.ArgumentParser()
20 parser.add_argument('command')
21 parser.add_argument('-p', '--project', help='project name')
22 parser.add_argument('--minor', help='minor version number bump', action='store_true')
23 parser.add_argument('--micro', help='micro version number bump', action='store_true')
24 parser.add_argument('--major', help='major version to return with latest', type=int)
25 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
26 parser.add_argument('-o', '--output', help='output directory', default='.')
27 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
28 parser.add_argument('-t', '--target', help='target')
29 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
30 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
31 parser.add_argument('-w', '--work', help='override default work directory')
32 parser.add_argument('-g', '--git-prefix', help='override configured git prefix')
33 args = parser.parse_args()
34
35 cdist_cmd = 'cdist %s -t host ' % args.command
36 if args.project is not None:
37     cdist_cmd += '-p %s ' % args.project
38 if args.minor is True:
39     cdist_cmd += '--minor '
40 if args.micro is True:
41     cdist_cmd += '--micro '
42 if args.major is True:
43     cdist_cmd += '--major '
44 if args.checkout is not None:
45     cdist_cmd += '--checkout %s ' % args.checkout
46 if args.output is not None:
47     cdist_cmd += '--output cdistvm '
48 if args.quiet is True:
49     cdist_cmd += '--quiet '
50 if args.keep is True:
51     cdist_cmd += '--keep '
52 if args.debug is True:
53     cdist_cmd += '--debug '
54 if args.work is not None:
55     cdist_cmd += '--work %s ' % args.work
56 if args.git_prefix is not None:
57     cdist_cmd += '--git-prefix %s ' % args.git_prefix
58
59 def command(c):
60     r = os.system(c)
61     if (r >> 8):
62         raise Error('command %s failed' % c)
63
64 ports = { 'fedora-22-32': 2000,
65           'fedora-22-64': 2001,
66           'fedora-23-32': 2002,
67           'fedora-23-64': 2003,
68           'arch-64': 2004,
69           'fedora-24-32': 2005,
70           'fedora-24-64': 2006,
71           'fedora-25-32': 2007,
72           'fedora-25-64': 2008,
73         }
74
75 vbox = subprocess.Popen('vboxheadless --startvm %s' % args.target, shell=True)
76
77 ok = False
78 while ok == False:
79     time.sleep(10)
80     try:
81         command('ssh -p %d carl@localhost "rm -rf cdistvm /var/tmp/tmp*"' % ports[args.target])
82         ok = True
83     except Error as e:
84         print('Ignoring: %s' % e)
85         pass
86
87 command('ssh -p %d carl@localhost %s' % (ports[args.target], cdist_cmd))
88 if args.command in ['package', 'doxygen', 'manual', 'changelog', 'pot']:
89     tmp = tempfile.mkdtemp()
90     command('scp -r -P %d carl@localhost:cdistvm/* %s/' % (ports[args.target], tmp))
91     command('scp -r %s/* %s/' % (tmp, args.output))
92     shutil.rmtree(tmp)
93
94 try:
95     command('ssh -p %d carl@localhost "sudo /sbin/poweroff"' % ports[args.target])
96 except Error:
97     pass
98
99 print("wait for vm to terminate...")
100 vbox.wait()