chiark / gitweb /
Merge branch 'python-vagrant-copy-caches' into 'master'
[fdroidserver.git] / tests / extra / manual-vmtools-test.py
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import logging
6 import textwrap
7 import tempfile
8 import inspect
9 from argparse import ArgumentParser
10
11 localmodule = os.path.realpath(
12     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..', '..'))
13 print('localmodule: ' + localmodule)
14 if localmodule not in sys.path:
15     sys.path.insert(0, localmodule)
16
17 from fdroidserver.vmtools import get_build_vm
18
19
20 def main(args):
21
22     if args.provider != None:
23         if args.provider not in ('libvirt', 'virtualbox'):
24             logging.critical('provider: %s not supported.', args.provider)
25             sys.exit(1)
26
27     with tempfile.TemporaryDirectory() as tmpdir:
28
29         # define a simple vagrant vm 'x'
30         x_dir = os.path.join(tmpdir, 'x')
31         os.makedirs(x_dir)
32         with open(os.path.join(x_dir, 'Vagrantfile'), 'w') as f:
33             f.write(textwrap.dedent("""\
34                 Vagrant.configure("2") do |config|
35                     config.vm.box = "debian/jessie64"
36                     config.vm.synced_folder ".", "/vagrant", disabled: true
37                     config.ssh.insert_key = false
38                 end
39                 """))
40         # define another simple vagrant vm 'y' which uses 'x' as a base box
41         y_dir = os.path.join(tmpdir, 'y')
42         os.makedirs(y_dir)
43         with open(os.path.join(y_dir, 'Vagrantfile'), 'w') as f:
44             f.write(textwrap.dedent("""\
45                 Vagrant.configure("2") do |config|
46                     config.vm.box = "x"
47                     config.vm.synced_folder ".", "/vagrant", disabled: true
48                 end
49                 """))
50
51         # vagrant file for packaging 'x' box
52         vgrntf=textwrap.dedent("""\
53             Vagrant.configure("2") do |config|
54
55                 config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_version: "4", nfs_udp: false
56
57                 config.vm.provider :libvirt do |libvirt|
58                     libvirt.driver = "kvm"
59                     libvirt.connect_via_ssh = false
60                     libvirt.username = "root"
61                     libvirt.storage_pool_name = "default"
62                 end
63             end
64             """)
65
66         # create a box: x
67         if not args.skip_create_x:
68             x = get_build_vm(x_dir, provider=args.provider)
69             x.destroy()
70             x.up(provision=True)
71             x.halt()
72             x.package(output='x.box', vagrantfile=vgrntf, keep_box_file=False)
73             x.box_remove('x')
74             x.box_add('x', 'x.box')
75
76         # use previously created box to spin up a new vm
77         if not args.skip_create_y:
78             y = get_build_vm(y_dir, provider=args.provider)
79             y.destroy()
80             y.up()
81
82         # create and restore a snapshot
83         if not args.skip_snapshot_y:
84             y = get_build_vm(y_dir, provider=args.provider)
85
86             if y.snapshot_exists('clean'):
87                 y.destroy()
88                 y.up()
89
90             y.suspend()
91             y.snapshot_create('clean')
92             y.up()
93
94             logging.info('snapshot \'clean\' exsists: %r', y.snapshot_exists('clean'))
95
96             # test if snapshot exists
97             se = y.snapshot_exists('clean')
98             logging.info('snapshot \'clean\' available: %r', se)
99
100             # revert snapshot
101             y.suspend()
102             logging.info('asdf %s', y.snapshot_revert('clean'))
103             y.resume()
104
105         # cleanup
106         if not args.skip_clean:
107             x = get_build_vm(x_dir, provider=args.provider)
108             y = get_build_vm(y_dir, provider=args.provider)
109             y.destroy()
110             x.destroy()
111             x.box_remove('x')
112
113 if __name__ == '__main__':
114     logging.basicConfig(format='%(message)s', level=logging.DEBUG)
115     
116     parser = ArgumentParser(description="""\
117 This is intended for manually testing vmtools.py
118
119 NOTE: Should this test-run fail it might leave traces of vagrant VMs or boxes
120       on your system. Those vagrant VMs are named 'x' and 'y'.
121     """)
122     parser.add_argument('--provider', help="Force this script use supplied "
123                         "provider instead using our auto provider lookup. "
124                         "Supported values: 'libvirt', 'virtualbox'")
125     parser.add_argument('--skip-create-x', action="store_true", default=False,
126                         help="Skips: Creating 'x' vm, packaging it into a "
127                         "a box and adding it to vagrant.")
128     parser.add_argument('--skip-create-y', action="store_true", default=False,
129                         help="Skips: Creating 'y' vm. Depends on having "
130                         "box 'x' added to vagrant.")
131     parser.add_argument('--skip-snapshot-y', action="store_true", default=False,
132                         help="Skips: Taking a snapshot and restoring a "
133                         "a snapshot of 'y' vm. Requires 'y' mv to be "
134                         "present.")
135     parser.add_argument('--skip-clean', action="store_true", default=False,
136                         help="Skips: Cleaning up mv images and vagrant "
137                         "metadata on the system.")
138     args = parser.parse_args()
139
140     main(args)