def test_get_singleton_instance_twice(self):
        i = DeployerConfigManager.get_instance()
        i2 = DeployerConfigManager.get_instance()

        self.assertEqual(i,
                         i2,
                         msg="Both DeployerConfigManager instances \
                         are the same")
 def __init__(self):
     '''
     Initialise a configuration manager that will be used to provide
     the configurations based on which deployer instances are created.  
     '''
     self.dcm = DeployerConfigManager.get_instance()
     self.dcm.init_configuration()
    def test_get_pbs_config_class(self):
        mock_config_class = Mock(spec=PBSProPlatformConfig)

        dcm = DeployerConfigManager.get_instance()
        dcm._software['pbspro'] = mock_config_class

        self.assertRaises(ValueError, dcm.get_platform_configuration,
                          'openstack')
    def test_get_platform_configuration_valid(self):
        mock_config_class = Mock(spec=OpenStackPlatformConfig)

        dcm = DeployerConfigManager.get_instance()
        # Inject the mocked platform config, with the correct key, into the
        # config manager's list of platforms.
        dcm._software['openstack'] = mock_config_class
        c = dcm.get_platform_configuration('openstack')
        self.assertTrue(isinstance(c, OpenStackPlatformConfig))
Exemple #5
0
def libhpc_run_job():
    # Begin by checking if the config file directories exist, if not create them
    # expanduser with ~ directly seems to fail when running python process under a different
    # user and the USER and HOME environment variables are not correctly set. Using uid and
    # getpwuid seems to operate correctly to get the username and home directory in
    # these cases.
    uid = os.getuid()
    username = pwd.getpwuid(os.getuid())[0]
    LOG.debug('Looking up user home directory for uid <%s>, username <%s>.' %
              (uid, username))
    user_home = expanduser('~' + username)
    LOG.debug('Using user home directory <%s>.' % user_home)
    platform_config_dir = os.path.join(user_home, '.libhpc', 'config',
                                       'platform')
    software_config_dir = os.path.join(user_home, '.libhpc', 'config',
                                       'software')
    if not os.path.exists(platform_config_dir):
        os.makedirs(platform_config_dir)
        LOG.debug('Created platform config directory...')
    if not os.path.exists(software_config_dir):
        os.makedirs(software_config_dir)
        LOG.debug('Created software config directory...')

    parser = argparse.ArgumentParser(description='Run an HPC job on the '
                                     'specified platform.')
    subparsers = parser.add_subparsers(title='Available subcommands')

    list_parser = subparsers.add_parser(
        'list', help='List registered platforms and software')
    run_parser = subparsers.add_parser('run',
                                       help='Run jobs using the '
                                       'specified software and platform.')

    list_parser.add_argument('info',
                             help="Value can be either 'platforms' or "
                             "'software' to list details of the registered "
                             "compute platforms or software")

    run_parser.add_argument('-p',
                            type=str,
                            required=True,
                            dest="platform",
                            help="The ID or full path to a YAML file "
                            "representing the platform to use to run the job.")
    run_parser.add_argument('-j',
                            type=str,
                            required=True,
                            dest="job_spec",
                            help="Full path to a job specification file "
                            "defining the job to run.")
    run_parser.add_argument('-s',
                            type=str,
                            required=False,
                            dest="software_to_deploy",
                            help="The software ID or full path to a YAML file "
                            "representing the software to deploy on the "
                            "specified platform.")
    run_parser.add_argument('-i',
                            type=str,
                            required=False,
                            dest="ip_file",
                            help="The full path for a file that should have "
                            "IP addresses of the started cloud nodes written "
                            "to it once the nodes are started and accessible.")

    args = parser.parse_args()

    LOG.debug('Args: %s' % str(args))

    ldt = LibhpcDeployerTool()

    # If the list subcommand has been specified, find out what info to list
    if hasattr(args, 'info'):
        try:
            config_names = ldt.list_configuration(args.info)
            if args.info == 'platforms':
                print 'Platform configurations:\n'
            elif args.info == 'software':
                print 'Software configurations:\n'
            for item in config_names:
                print('\t\t%s' % item)
        except ValueError as e:
            LOG.debug('Unable to list configurations: [%s]' % str(e))
            list_parser.print_help()
            exit()

    elif hasattr(args, 'platform'):
        # Load the platform configuration
        platform_config = None
        try:
            platform = args.platform
            if os.path.isfile(platform):
                # raise NotImplementedError('Support for using a YAML file '
                #    'describing the platform to use for running a job is not '
                #    'yet implemented. Please use a platform ID instead.')
                # Check if the specified job spec parameter is a YAML file that
                # we can open.
                #try:
                #    job_config = JobConfiguration.from_yaml(jobspec)
                #except JobConfigurationError as e:
                #    LOG.debug('Unable to read the YAML configuration from '
                #              'the specified YAML file <%s>: %s'
                #              % (jobspec, str(e)))
                dcm = DeployerConfigManager.get_instance()
                conf = dcm.load_platform_config(platform, resource=False)
                platform_config = dcm.read_platform_config(conf)
            elif platform in ldt.dcm.get_platform_names():
                LOG.debug(
                    'We have a platform configuration ID <%s> to '
                    'identify the platform to use for running this task.' %
                    (platform))
                platform_config = platform
            else:
                print(
                    'The specified platform file/ID <%s> is not recognised. ' %
                    (platform))
                run_parser.print_help()
                exit()
        except ValueError as e:
            LOG.debug('Unable to run job: [%s]' % str(e))
            run_parser.print_help()
            exit()

        # Load the job specification
        job_config = None
        try:
            jobspec = args.job_spec
            if os.path.isfile(jobspec):
                # Check if the specified job spec parameter is a YAML file that
                # we can open.
                try:
                    job_config = JobConfiguration.from_yaml(jobspec)
                except JobConfigurationError as e:
                    LOG.debug('Unable to read the YAML configuration from '
                              'the specified YAML file <%s>: %s' %
                              (jobspec, str(e)))
            else:
                print(
                    '\nERROR: Unable to find the specified job '
                    'specification: %s\n' % (jobspec))
                exit()
        except ValueError as e:
            LOG.debug('Unable to run job: [%s]' % str(e))
            run_parser.print_help()
            exit()

        # Check if we have a software config specified
        software_config = None
        if args.software_to_deploy:
            software_config = args.software_to_deploy
            LOG.debug('We have a software config specified: <%s>' %
                      software_config)

        # Check if an ip file was specified
        ip_file = None
        if args.ip_file:
            ip_file = args.ip_file
            LOG.debug('We have an ip_file specified: <%s>' % ip_file)

        ldt.run_job(platform_config, job_config, software_config, ip_file)
    else:
        parser.print_help()
        LOG.debug('No expected values were present in the parsed input '
                  'data.' % str(e))
        exit()
Exemple #6
0
    def __init__(self):
        self.dcm = DeployerConfigManager.get_instance()
        self.scm = SoftwareConfigManager.get_instance()

        self.dcm.init_configuration()
        self.scm.init_configuration()
 def test_get_instance_via_func(self):
     i = DeployerConfigManager.get_instance()
     self.assertIsInstance(i, DeployerConfigManager)
 def test_create_instance_via_constructor(self):
     with self.assertRaises(Exception) as c:
         DeployerConfigManager()
     self.assertTrue(
         'This is a singleton class, use get_instance ' +
         ' to access the single instance of this class.' in c.exception)