• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This tool can be used to set up a base container for test. For example,
6  python lxc.py -s -p /tmp/container
7This command will download and setup base container in directory /tmp/container.
8After that command finishes, you can run lxc command to work with the base
9container, e.g.,
10  lxc-start -P /tmp/container -n base -d
11  lxc-attach -P /tmp/container -n base
12"""
13
14import argparse
15import logging
16
17import common
18from autotest_lib.client.bin import utils
19from autotest_lib.site_utils import lxc
20from autotest_lib.site_utils.lxc import base_image
21
22
23def parse_options():
24    """Parse command line inputs.
25
26    @raise argparse.ArgumentError: If command line arguments are invalid.
27    """
28    parser = argparse.ArgumentParser()
29    parser.add_argument('-s', '--setup', action='store_true',
30                        default=False,
31                        help='Set up base container.')
32    parser.add_argument('-p', '--path', type=str,
33                        help='Directory to store the container.',
34                        default=lxc.DEFAULT_CONTAINER_PATH)
35    parser.add_argument('-f', '--force_delete', action='store_true',
36                        default=False,
37                        help=('Force to delete existing containers and rebuild '
38                              'base containers.'))
39    parser.add_argument('-n', '--name', type=str,
40                        help='Name of the base container.',
41                        default=lxc.BASE)
42    options = parser.parse_args()
43    if not options.setup and not options.force_delete:
44        raise argparse.ArgumentError(
45            'Use --setup to setup a base container, or --force_delete to '
46            'delete all containers in given path.')
47    return options
48
49
50def main():
51    """main script."""
52    # Force to run the setup as superuser.
53    # TODO(dshi): crbug.com/459344 Set remove this enforcement when test
54    # container can be unprivileged container.
55    if utils.sudo_require_password():
56        logging.warn('SSP requires root privilege to run commands, please '
57                     'grant root access to this process.')
58        utils.run('sudo true')
59
60    options = parse_options()
61    image = base_image.BaseImage(options.path, lxc.BASE)
62    if options.setup:
63        image.setup(name=options.name, force_delete=options.force_delete)
64    elif options.force_delete:
65        image.cleanup()
66
67
68if __name__ == '__main__':
69    main()
70