Exemplo n.º 1
0
 def test_generating_service_list(self):
     """
     Need to make sure that the service list is generated correctly even
     if a service doesn't define a service name.
     """
     ctrlfile = Controlfile(self.controlfile)
     self.assertEqual(
         ctrlfile.get_list_of_services(),
         frozenset(["foo", "bar", "baz", "required", "all", "optional"]))
Exemplo n.º 2
0
 def test_replace(self):
     """Make sure that prefixing works for strings and lists"""
     temp_dir = tempfile.TemporaryDirectory()
     controlfile = join(temp_dir.name, 'Controlfile')
     conf = {
         "services": {
             "testmeta": {
                 "services": {
                     "test": {
                         "image": "busybox",
                         "container": {
                             "name": "test",
                         }
                     }
                 }
             }
         },
         "options": {
             "image": {
                 "replace": "registry.{FOO}.com/alpine"
             }
         },
         "vars": {
             "FOO": "example"
         }
     }
     with open(controlfile, 'w') as f:
         f.write(json.dumps(conf))
     ctrlfile = Controlfile(controlfile)
     self.assertEqual(ctrlfile.services['test'].image,
                      'registry.example.com/alpine')
Exemplo n.º 3
0
 def test_union(self):
     """Make sure that prefixing works for strings and lists"""
     temp_dir = tempfile.TemporaryDirectory()
     controlfile = join(temp_dir.name, 'Controlfile')
     conf = {
         "services": {
             "testmeta": {
                 "services": {
                     "test": {
                         "image": "busybox",
                         "container": {
                             "name": "test",
                             "volumes": ["vardata:/var/lib/{FOO}"]
                         }
                     }
                 }
             }
         },
         "options": {
             "volumes": {
                 "union": ["{FOO}:/home"]
             }
         },
         "vars": {
             "FOO": "example"
         }
     }
     with open(controlfile, 'w') as f:
         f.write(json.dumps(conf))
     ctrlfile = Controlfile(controlfile)
     self.assertEqual(ctrlfile.services['test'].volumes_for(prod=False),
                      ['vardata:/var/lib/example', 'example:/home'])
Exemplo n.º 4
0
 def test_suffix(self):
     """Make sure that suffixing works for strings and lists"""
     temp_dir = tempfile.TemporaryDirectory()
     controlfile = join(temp_dir.name, 'Controlfile')
     conf = {
         "services": {
             "testmeta": {
                 "services": {
                     "test": {
                         "image": "busybox",
                         "container": {
                             "name": "test"
                         }
                     }
                 }
             }
         },
         "options": {
             "name": {
                 "suffix": ".{FOO}"
             }
         },
         "vars": {
             "FOO": "example"
         }
     }
     with open(controlfile, 'w') as f:
         f.write(json.dumps(conf))
     ctrlfile = Controlfile(controlfile)
     self.assertEqual(ctrlfile.services['test']['name'], 'test.example')
Exemplo n.º 5
0
 def test_optional_services(self):
     """
     Make sure that containers that aren't required to be started are put
     in the optional services list.
     """
     ctrlfile = Controlfile(self.controlfile)
     self.assertIn('baz', ctrlfile.services['all'])
     self.assertIn('baz', ctrlfile.services['optional'])
     self.assertNotIn('baz', ctrlfile.services['required'])
Exemplo n.º 6
0
 def test_single_service_controlfile(self):
     """Make sure that we don't break single service controlfiles"""
     ctrlfile = Controlfile(self.controlfile)
     self.assertIn('example', ctrlfile.services.keys())
     self.assertEqual(ctrlfile.services['example'].image,
                      self.conf['image'])
     self.assertEqual(ctrlfile.services['example'].controlfile,
                      self.controlfile)
     self.assertEqual(ctrlfile.services['example'].volumes_for(prod=False),
                      self.conf['container']['volumes'])
     self.assertEqual(ctrlfile.services['example']['dns_search'],
                      self.conf['container']['dns_search'])
Exemplo n.º 7
0
def main(args):
    """create the parser and decide how to run"""
    # Shut up requests because the user has to make a conscious choice to be
    # insecure
    global options
    import requests
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    console_loghandler = logging.StreamHandler()
    signal.signal(signal.SIGINT, sigint_handler)

    parser = build_parser()
    parser.parse_args(args, namespace=options)
    console_loghandler.setLevel(logging.INFO)
    if options.debug:
        console_loghandler.setLevel(logging.DEBUG)
    module_logger.addHandler(console_loghandler)
    module_logger.debug("switching to debug logging")

    # Read in a Controlfile if one exists
    ctrlfile_location = abspath(options.controlfile)
    while dirname(ctrlfile_location) != '/' and not exists(ctrlfile_location):
        s = split(ctrlfile_location)
        ctrlfile_location = join(dirname(s[0]), s[1])
    module_logger.debug('controlfile location: %s', ctrlfile_location)
    try:
        ctrl = Controlfile(ctrlfile_location, options.as_me)
    except FileNotFoundError as error:
        module_logger.critical(error)
        sys.exit(2)
    except InvalidControlfile as error:
        module_logger.critical(error)
        sys.exit(2)

    if not dclient:
        print('Docker is not running. Please start docker.', file=sys.stderr)
        sys.exit(2)

    # If no services were specified on the command line, default to required
    if len(options.services) == 0:
        module_logger.debug(
            'No options specified. Using required service list')
        options.services = ctrl.required_services()
    # Flatten the service list by replacing metaservices with their service lists
    module_logger.debug(ctrl.services.keys())
    # module_logger.debug(ctrl.services['js'])
    # for name in (name
    #              for name in options.services
    #              if isinstance(ctrl.services[name], MetaService)):
    #     options.services += ctrl.services[name].services
    #     options.services.remove(name)
    options.services = flatten(ctrl.services[name].services
                               for name in options.services)

    # Override image name if only one service discovered
    if options.image and len(options.services) == 1:
        ctrl.services[options.services[0]]['image'] = options.image
        module_logger.debug(vars(ctrl.services[options.services[0]]))
    elif options.image and len(options.services) > 1:
        module_logger.info(
            'Ignoring image specified in arguments. Too many services.')
    # Override container name if only one service
    if options.name and len(options.services) == 1:
        ctrl.services[options.services[0]]['name'] = options.name
        module_logger.debug(vars(ctrl.services[options.services[0]]))
    elif options.name and len(options.services) > 1:
        module_logger.info('Ignoring container name specified in arguments. '
                           'Too many services to start')
    # Override dockerfile location if only one service discovered
    if options.dockerfile and len(options.services) == 1:
        ctrl.services[options.services[0]]['dockerfile'] = options.image
        module_logger.debug(vars(ctrl.services[options.services[0]]))
    elif options.dockerfile and len(options.services) > 1:
        module_logger.info(
            'Ignoring dockerfile specified in arguments. Too many services.')
    module_logger.debug(vars(options))

    ret = function_dispatch(options, ctrl)

    if not ret:
        sys.exit(1)