예제 #1
0
 def test_random_str(self):
     assert len(config.random_str()) == 6
예제 #2
0
    def __init__(self, name=None, bundles=[], pubkey=config.DEFAULT_PUBKEY,
                 prefix=config.DEFAULT_NAME_PREFIX, image_name=config.DEFAULT_IMAGE_NAME,
                 subvars=[]):

        """Initialize a node deployment.

        If name is not given, it will generate a random name using
        prefix.  The node name is added to the global substitution
        map, which is used to parameterize templates in scripts
        containing the form {variable_name}.

        The list of bundle names is concatenated with any globally
        common bundle names from which result the set of files to be
        installed, and scripts to be run on the new node.

        The pubkey is concatented with any other public keys loaded
        during configuration and used as the first step in the
        multi-step deployment.  Additional steps represent the scripts
        to be run.

        The image_name is used to determine which set of default
        bundles to install, as well as to actually get the image id in
        deploy()."""

        self.name = name or prefix + config.random_str()
        config.SUBMAP['node_name'] = self.name
        config.SUBMAP['node_shortname'] = self.name.split('.')[0]
        merge_keyvals_into_map(subvars, config.SUBMAP)
        logger.debug('substitution map {0}'.format(config.SUBMAP))

        self.pubkeys = [pubkey]
        self.pubkeys.extend(config.PUBKEYS)

        self.image_name = image_name

        filemap = {}
        scriptmap = provision.collections.OrderedDict() # preserve script run order

        install_bundles = config.DEFAULT_BUNDLES[:]
        if image_name not in config.BOOTSTRAPPED_IMAGE_NAMES:
            install_bundles.extend(config.DEFAULT_BOOTSTRAP_BUNDLES[:])
        install_bundles.extend(bundles)

        for bundle in install_bundles:
            logger.debug('loading bundle {0}'.format(bundle))
            merge(config.BUNDLEMAP[bundle].filemap.items(), filemap)
            merge(config.BUNDLEMAP[bundle].scriptmap.items(), scriptmap, load=True)
        logger.debug('files {0}'.format(filemap.keys()))
        logger.debug('scripts {0}'.format(scriptmap.keys()))

        file_deployments = [libcloud.compute.deployment.FileDeployment(
                target, source) for target, source in filemap.items()]
        logger.debug('len(file_deployments) = {0}'.format(len(file_deployments)))

        self.script_deployments = [script_deployment(path, script, config.SUBMAP)
                                   for path, script in scriptmap.items()]
        logger.debug('len(script_deployments) = {0}'.format(len(self.script_deployments)))

        steps = [libcloud.compute.deployment.SSHKeyDeployment(''.join(self.pubkeys))]
        steps.extend(file_deployments)
        steps.extend(self.script_deployments)
        self.deployment = libcloud.compute.deployment.MultiStepDeployment(steps)