def cmd(stackname, command=None, username=DEPLOY_USER, clean_output=False, concurrency=None, node=None): if command is None: utils.errcho("Please specify a command e.g. ./bldr cmd:%s,ls" % stackname) exit(1) LOG.info("Connecting to: %s", stackname) instances = _check_want_to_be_running(stackname) if not instances: return # take out the load of crap that Fabric prints mangling the useful output # of a remote command custom_settings = {} if clean_output: custom_settings['fabric.state.output'] = { 'status': False, 'running': False } custom_settings['output_prefix'] = False try: with settings(**custom_settings): return stack_all_ec2_nodes( stackname, (remote, {'command': command}), username=username, abort_on_prompts=True, concurrency=concurrency_for(stackname, concurrency), node=int(node) if node else None ) except CommandException as e: LOG.error(e) exit(2)
def _clone_project_formula(furl): """clones a formula to `./cloned-projects/$formulaname`, if it doesn't already exist. if it does exist, it attempts to update it with a `git pull`.""" destination = config.CLONED_PROJECT_FORMULA_DIR # /path/to/builder/cloned-projects fpath = os.path.join(destination, os.path.basename( furl)) # /path/to/builder/cloned-projects/builder-base-formula cmd = "cd %s; git clone %s" % (destination, furl) if os.path.exists(fpath): cmd = "cd %s; git pull" % (fpath, ) with settings(warn_only=True): local(cmd)
def set_up_stack(cls, project, explicitly_start=False): switch_in_test_settings() # to re-use an existing stack, ensure cls.reuse_existing_stack is True # this will read the instance name from a temporary file (if it exists) and # look for that, creating it if doesn't exist yet # also ensure cls.cleanup is False so the instance isn't destroyed after tests complete cls.reuse_existing_stack = config.TWI_REUSE_STACK cls.cleanup = config.TWI_CLEANUP cls.stacknames = [] cls.environment = generate_environment_name() # cls.temp_dir, cls.rm_temp_dir = utils.tempdir() # debugging only, where we keep an instance up between processes cls.state, cls.statefile = {}, '/tmp/.open-test-instances.txt' if cls.reuse_existing_stack and os.path.exists(cls.statefile): # evidence of a previous instance and we've been told to re-use old instances old_state = json.load(open(cls.statefile, 'r')) old_env = old_state.get('environment') # test if the old stack still exists ... if old_env and core.describe_stack(project + "--" + old_env, allow_missing=True): cls.state = old_state cls.environment = old_env else: # nope. old statefile is bogus, delete it os.unlink(cls.statefile) cls.state['environment'] = cls.environment # will be saved later with settings(abort_on_prompts=True): cls.stackname = '%s--%s' % (project, cls.environment) cls.stacknames.append(cls.stackname) if cls.cleanup: LOG.info("ensure_destroyed %s", cls.stackname) cfn.ensure_destroyed(cls.stackname) cls.context, cls.cfn_template, _ = cfngen.generate_stack( project, stackname=cls.stackname) cls.region = cls.context['aws']['region'] LOG.info("create_stack %s", cls.stackname) bootstrap.create_stack(cls.stackname) if explicitly_start: LOG.info("start %s", cls.stackname) lifecycle.start(cls.stackname)
def parse_validate_repolist(fdata, *repolist): "returns a list of triples" known_formulas = fdata.get('formula-dependencies', []) known_formulas.extend([fdata['formula-repo'], fdata['private-repo']]) known_formula_map = OrderedDict( zip(map(os.path.basename, known_formulas), known_formulas)) arglist = [] for user_string in repolist: if '@' not in user_string: print('skipping %r, no revision component' % user_string) continue repo, rev = user_string.split('@') if not rev.strip(): print('skipping %r, empty revision component' % user_string) continue if repo not in known_formula_map: print('skipping %r, unknown formula. known formulas: %s' % (repo, ', '.join(known_formula_map.keys()))) continue arglist.append((repo, known_formula_map[repo], rev)) # test given revisions actually exist in formulas for name, _, revision in arglist: path = join(config.PROJECT_PATH, "cloned-projects", name) if not os.path.exists(path): LOG.warn( "couldn't find formula %r locally, revision check skipped", path) continue with lcd(path), settings(warn_only=True): ensure( local("git fetch --quiet")['succeeded'], "failed to fetch remote refs for %s" % path) ensure( local("git cat-file -e %s^{commit}" % revision)['succeeded'], "failed to find ref %r in %s" % (revision, name)) return arglist
def test_create(self): with settings(abort_on_prompts=True): project = 'dummy1' stackname = '%s--%s' % (project, self.environment) cfn.ensure_destroyed(stackname) self.stacknames.append(stackname) cfngen.generate_stack(project, stackname=stackname) bootstrap.create_stack(stackname) buildvars.switch_revision(stackname, 'master') buildvars.force(stackname, 'answer', 'forty-two') cfn.cmd(stackname, "ls -l /", username=BOOTSTRAP_USER, concurrency='parallel') cfn.cmd(stackname, "ls -l /", username=BOOTSTRAP_USER, concurrency='parallel', node=1) cfn.download_file(stackname, "/bin/ls", "ls", use_bootstrap_user="******") self.assertTrue(os.path.isfile("./ls")) cfn.download_file(stackname, "/bin/less", "venv/bin/", use_bootstrap_user="******") self.assertTrue(os.path.isfile("./venv/bin/less")) cfn.download_file(stackname, "/bin/pwd", "subfolder/pwd", use_bootstrap_user="******") self.assertTrue(os.path.isfile("./subfolder/pwd")) lifecycle.stop_if_running_for( stackname, minimum_minutes=60 * 24 * 365 ) # should exercise the code but do nothing, as this test's instance can't have been running for a year