def init_bundles(self):

        # bundle_credential members { 'port', 'hostname', 'username',
        #                             'password' 'key_filename', 'h_flag' }

        troy._logger.info('Initializing Bundle Manager')

        self.bm = BundleManager()

        cg = self.session.get_config ('troy:bundle')
        finished_job_trace = cg['finished_job_trace']

        # FIXME: not sure if the new resource config contains all needed data...
        for sect in self.session.get_config ('troy:resources'):
            cs = self.session.cfg.get_config(sect)

            cred = { 'port': int(cs['port'].get_value()),
                     'hostname': cs['endpoint'].get_value(),
                     'cluster_type': cs['type'].get_value(),
                     'username': cs['username'].get_value(),
                     'password': cs['password'].get_value(),
                     'key_filename': cs['ssh_key'].get_value(),
                     'h_flag': cs['h_flag'].get_value()
            }
            self.bm.add_cluster(cred, finished_job_trace)

        if 'pilot_size' in self.cfg :
            pilot_size = int(self.cfg['pilot_size'])
        self.cluster_list = self.bm.get_cluster_list()

        if not self.cluster_list:
            raise RuntimeError ('No clusters available in Bundle Manager. You might want to check your config file.')
class PLUGIN_CLASS (troy.PluginBase):
    """
    This plugin is not used at this point.

    **Configuration Options:**

    * `guard`: documentation for guard
    """

    __metaclass__ = ru.Singleton


    # --------------------------------------------------------------------------
    #
    def __init__(self):

        troy.PluginBase.__init__ (self, PLUGIN_DESCRIPTION)


    # --------------------------------------------------------------------------
    #
    def init(self):

        troy._logger.debug ("init plugin %s (bundles)" % self.name)

        self.guard = self.cfg.get ('guard', None)

        self.init_bundles()


    # --------------------------------------------------------------------------
    #
    def init_bundles(self):

        # bundle_credential members { 'port', 'hostname', 'username',
        #                             'password' 'key_filename', 'h_flag' }

        troy._logger.info('Initializing Bundle Manager')

        self.bm = BundleManager()

        cg = self.session.get_config ('troy:bundle')
        finished_job_trace = cg['finished_job_trace']

        # FIXME: not sure if the new resource config contains all needed data...
        for sect in self.session.get_config ('troy:resources'):
            cs = self.session.cfg.get_config(sect)

            cred = { 'port': int(cs['port'].get_value()),
                     'hostname': cs['endpoint'].get_value(),
                     'cluster_type': cs['type'].get_value(),
                     'username': cs['username'].get_value(),
                     'password': cs['password'].get_value(),
                     'key_filename': cs['ssh_key'].get_value(),
                     'h_flag': cs['h_flag'].get_value()
            }
            self.bm.add_cluster(cred, finished_job_trace)

        if 'pilot_size' in self.cfg :
            pilot_size = int(self.cfg['pilot_size'])
        self.cluster_list = self.bm.get_cluster_list()

        if not self.cluster_list:
            raise RuntimeError ('No clusters available in Bundle Manager. You might want to check your config file.')

    # --------------------------------------------------------------------------
    #
    def check_resource_availability(self, overlay_desc):

        resource_request = { 'p_procs': overlay_desc.cores, 'est_runtime': overlay_desc.walltime }

        predictions = {}
        for cluster in self.cluster_list:
            predictions[cluster] = self.bm.resource_predict(cluster, resource_request)

        # Find entries that are not -1
        usable = filter(lambda x: x != -1, predictions.values())
        if not usable:
            raise RuntimeError ('No resources available that can fulfill this request!')

    # --------------------------------------------------------------------------
    #
    def derive_overlay(self, workload, guard=LOWER_LIMIT):
        """
        Based on obtained bundle information, derive a useful overlay
        description.  Guard is respected.
        """

        # Determine the number of cores required
        if self.guard == UPPER_LIMIT:
            # We don't have any concurrency mechanisms yet,
            # so assume all concurrent.
            cores = len(workload.tasks)
        elif self.guard == LOWER_LIMIT:
            # We don't have any concurrency mechanisms yet,
            # so lower limit is 1
            cores = 1
        else:
            raise RuntimeError('Unknown guard: "%d') % self.guard

        ovl_descr = troy.OverlayDescription (
            {
                # Ask for as many pilots as tasks
                'cores' : cores,
            })

        troy._logger.info('planner derive ol: derive overlay for workload: '
                          '%s' % ovl_descr)

        # Check if there is at least one bundle that can satisfy our request
        # TODO: How to communicate back to application?
        self.check_resource_availability(ovl_descr)

        return ovl_descr
import json
import sys
from bundle import BundleManager

success = True

print "******    Step 01: create a BundleManager instance    ******"
bm = BundleManager()
if bm:
    print "SUCCEED!"
else:
    print "FAILED!"
    sys.exit(1)

print "******    Step 02: check BundleManager's active resource list    ******"
l = bm.get_cluster_list()
if l:
    print l
    print "SUCCEED!"
else:
    print "FAILED!"
    sys.exit(1)

print "******    Step 03: show each resource cluster's configuration    ******"
for c in l:
    print "Cluster {}:".format(c)
    cc = bm.get_cluster_configuration(c)
    if cc:
        print json.dumps(cc, sort_keys=True, indent=4)
    else:
        success = False