def conductor_commandline(): sys.stderr.write('Parsing conductor CLI args.\n') parser = argparse.ArgumentParser(description=u'This should not be invoked ' u'except in a container by ' u'Ansible Container.') parser.add_argument('command', action='store', help=u'Command to run.', choices=[ 'build', 'deploy', 'install', 'push', 'run', 'restart', 'stop', 'destroy' ]) parser.add_argument('--project-name', action='store', help=u'Project name.', required=True) parser.add_argument('--engine', action='store', help=u'Engine name.', required=True) parser.add_argument('--params', action='store', required=False, help=u'Encoded parameters for command.') parser.add_argument('--config', action='store', required=True, help=u'Encoded Ansible Container config.') parser.add_argument('--encoding', action='store', choices=['b64json'], help=u'Encoding used for parameters.', default='b64json') args = parser.parse_args() decoding_fn = globals()['decode_%s' % args.encoding] if args.params: params = decoding_fn(args.params) else: params = {} if params.get('debug'): LOGGING['loggers']['container']['level'] = 'DEBUG' config.dictConfig(LOGGING) containers_config = decoding_fn(args.config) conductor_config = AnsibleContainerConductorConfig(containers_config) logger.debug('Starting Ansible Container Conductor: %s', args.command, services=conductor_config.services) getattr(core, 'conductorcmd_%s' % args.command)( args.engine, args.project_name, conductor_config.services, volume_data=conductor_config.volumes, repository_data=conductor_config.registries, **params)
def test_should_use_dev_overrides(self): self.config.var_file = self.var_file self.config.set_env('dev') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertIn('DEBUG', conductor_config['services']['web']['environment'][0])
def test_should_resolve_lookup(self): self.config.var_file = self.var_file self.config.set_env('prod') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertIn(self.project_path, conductor_config['services']['web']['environment'][0])
def conductor_commandline(): sys.stderr.write('Parsing conductor CLI args.\n') parser = argparse.ArgumentParser(description=u'This should not be invoked ' u'except in a container by ' u'Ansible Container.') parser.add_argument('command', action='store', help=u'Command to run.', choices=['build', 'deploy', 'install', 'push', 'run', 'restart', 'stop', 'destroy']) parser.add_argument('--project-name', action='store', help=u'Project name.', required=True) parser.add_argument('--engine', action='store', help=u'Engine name.', required=True) parser.add_argument('--params', action='store', required=False, help=u'Encoded parameters for command.') parser.add_argument('--config', action='store', required=True, help=u'Encoded Ansible Container config.') parser.add_argument('--encoding', action='store', choices=['b64json'], help=u'Encoding used for parameters.', default='b64json') args = parser.parse_args() decoding_fn = globals()['decode_%s' % args.encoding] if args.params: params = decoding_fn(args.params) else: params = {} if params.get('debug'): LOGGING['loggers']['container']['level'] = 'DEBUG' config.dictConfig(LOGGING) # Copy a filtered subset of the mounted source into /src for use in builds logger.info('Copying build context into Conductor container.') p_obj = subprocess.Popen("rsync -av --filter=':- /_src/.dockerignore' /_src/ /src", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) for stdout_line in iter(p_obj.stdout.readline, b''): logger.debug(stdout_line) p_obj.stdout.close() return_code = p_obj.wait() if return_code: logger.error('Error copying build context: %s', p_obj.stderr.read()) sys.exit(p_obj.returncode) containers_config = decoding_fn(args.config) conductor_config = AnsibleContainerConductorConfig(list_to_ordereddict(containers_config), skip_services=args.command in BYPASS_SERVICE_PROCESSING) logger.debug('Starting Ansible Container Conductor: %s', args.command, services=conductor_config.services) getattr(core, 'conductorcmd_%s' % args.command)( args.engine, args.project_name, conductor_config.services, volume_data=conductor_config.volumes, repository_data=conductor_config.registries, secrets=conductor_config.secrets, **params)
def test_should_give_precedence_to_env_vars(self): # If an environment var exists, it should get precedence. os.environ.update({u'AC_FOO': 'cats'}) self.config.var_file = self.var_file self.config.set_env('prod') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertEqual(conductor_config['services']['web']['environment'][1], 'foo="cats"')
def test_should_give_precedence_to_file_vars(self): # If no environment var, then var defined in var_file should get precedence. if os.environ.get('AC_FOO'): del os.environ['AC_FOO'] self.config.var_file = self.var_file self.config.set_env('prod') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertEqual(conductor_config['services']['web']['environment'][1], 'foo="baz"')
def test_should_give_precedence_to_default_vars(self): # If no environment var and no var_file, then the default value should be used. if os.environ.get('AC_FOO'): del os.environ['AC_FOO'] self.config.cli_var_file = None self.config.set_env('prod') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertEqual(conductor_config['services']['web']['environment'][1], 'foo="bar"')
def test_should_also_replace_pwd_in_volumes(self): # test that ${PWD} gets resolved self.config.var_file = self.var_file self.config.set_env('prod') container.ENV = 'conductor' container.utils.DataLoader = DataLoader container.utils.VariableManager = VariableManager container.utils.RoleInclude = RoleInclude conductor_config = AnsibleContainerConductorConfig(self.config._config) self.assertIn( self.project_path, conductor_config.services['web']['volumes'][1], '{} not in volume[1]: {}'.format( self.project_path, json.dumps(conductor_config.services, indent=4)))