def generate_stack_from_input(pname, instance_id=None): """creates a new CloudFormation file for the given project.""" if not instance_id: default_instance_id = core_utils.ymd() instance_id = utils.uin("instance id", default_instance_id) stackname = core.mk_stackname(pname, instance_id) more_context = {'stackname': stackname} # prompt user for alternate configurations pdata = project.project_data(pname) if 'aws-alt' in pdata: def helpfn(altkey): try: return pdata['aws-alt'][altkey]['description'] except KeyError: return None default = 'skip this step' alt_config = [default] + pdata['aws-alt'].keys() alt_config = utils._pick('alternative config', alt_config, helpfn=helpfn) if alt_config != default: more_context['alt-config'] = alt_config cfngen.generate_stack(pname, **more_context) return stackname
def _update_remote_bvars(stackname, buildvars): LOG.info('updating %r with new vars %r', stackname, buildvars) encoded = encode_bvars(buildvars) fid = core_utils.ymd(fmt='%Y%m%d%H%M%S') # make a backup remote_sudo( 'if [ -f /etc/build-vars.json.b64 ]; then cp /etc/build-vars.json.b64 /tmp/build-vars.json.b64.%s; fi;' % fid) upload(StringIO(encoded), "/etc/build-vars.json.b64", use_sudo=True) LOG.info("%r updated. backup written to /tmp/build-vars.json.b64.%s", stackname, fid)
def _update_remote_bvars(stackname, buildvars): LOG.info('updating %r with new vars %r', stackname, buildvars) # not all projects have a 'revision' #ensure(core_utils.hasallkeys(buildvars, ['revision']), "buildvars missing key 'revision'") encoded = encode_bvars(buildvars) fid = core_utils.ymd(fmt='%Y%m%d%H%M%S') # make a backup remote_sudo('if [ -f /etc/build-vars.json.b64 ]; then cp /etc/build-vars.json.b64 /tmp/build-vars.json.b64.%s; fi;' % fid) upload(StringIO(encoded), "/etc/build-vars.json.b64", use_sudo=True) LOG.info("%r updated", stackname)
def _update_remote_bvars(stackname, buildvars): LOG.info('updating %r with new vars %r', stackname, buildvars) # not all projects have a 'revision' #ensure(core_utils.hasallkeys(buildvars, ['revision']), "buildvars missing key 'revision'") encoded = encode_bvars(buildvars) fid = core_utils.ymd(fmt='%Y%m%d%H%M%S') cmds = [ # make a backup 'if [ -f /etc/build-vars.json.b64 ]; then cp /etc/build-vars.json.b64 /tmp/build-vars.json.b64.%s; fi;' % fid, ] lmap(sudo, cmds) put(StringIO(encoded), "/etc/build-vars.json.b64", use_sudo=True) LOG.info("%r updated", stackname)
def _update_remote_bvars(stackname, bvars): LOG.info('updating %r with new vars %r',stackname, bvars) assert core_utils.hasallkeys(bvars, ['branch']) #, 'revision']) # we don't use 'revision' with stack_conn(stackname): encoded = _encode_bvars(bvars) fid = core_utils.ymd(fmt='%Y%m%d%H%M%S') cmds = [ # make a backup 'if [ -f /etc/build-vars.json.b64 ]; then cp /etc/build-vars.json.b64 /tmp/build-vars.json.b64.%s; fi;' % fid, # purge any mention of build vars 'rm -f /etc/build*vars.*', ] map(sudo, cmds) put(StringIO(encoded), "/etc/build-vars.json.b64", use_sudo=True) LOG.info("%r updated", stackname)
def _update_remote_bvars(stackname, buildvars): LOG.info('updating %r with new vars %r', stackname, buildvars) assert core_utils.hasallkeys( buildvars, ['branch']) # , 'revision']) # we don't use 'revision' encoded = encode_bvars(buildvars) fid = core_utils.ymd(fmt='%Y%m%d%H%M%S') cmds = [ # make a backup 'if [ -f /etc/build-vars.json.b64 ]; then cp /etc/build-vars.json.b64 /tmp/build-vars.json.b64.%s; fi;' % fid, ] map(sudo, cmds) put(StringIO(encoded), "/etc/build-vars.json.b64", use_sudo=True) LOG.info("%r updated", stackname)
def generate_stack_from_input(pname, instance_id=None, alt_config=None): """creates a new CloudFormation file for the given project.""" instance_id = instance_id or utils.uin("instance id", core_utils.ymd()) stackname = core.mk_stackname(pname, instance_id) checks.ensure_stack_does_not_exist(stackname) more_context = {'stackname': stackname} pdata = project.project_data(pname) if alt_config: ensure( 'aws-alt' in pdata, "alternative configuration name given, but project has no alternate configurations" ) # prompt user for alternate configurations if pdata['aws-alt']: default = 'skip' def helpfn(altkey): if altkey == default: return 'uses the default configuration' try: return pdata['aws-alt'][altkey]['description'] except KeyError: return None if instance_id in pdata['aws-alt'].keys(): LOG.info( "instance-id found in known alternative configurations. using configuration %r", instance_id) more_context['alt-config'] = instance_id else: alt_config_choices = [default] + list(pdata['aws-alt'].keys()) if not alt_config: alt_config = utils._pick('alternative config', alt_config_choices, helpfn=helpfn) if alt_config != default: more_context['alt-config'] = alt_config # TODO: return the templates used here, so that they can be passed down to # bootstrap.create_stack() without relying on them implicitly existing # on the filesystem cfngen.generate_stack(pname, **more_context) return stackname
def create_stack(pname): """creates a new CloudFormation template for the given project.""" default_instance_id = core_utils.ymd() inst_id = utils.uin("instance id", default_instance_id) stackname = core.mk_stackname(pname, inst_id) more_context = {'instance_id': stackname} # prompt user for alternate configurations pdata = project.project_data(pname) if pdata.has_key('aws-alt'): def helpfn(altkey): try: return pdata['aws-alt'][altkey]['description'] except KeyError: return None default = 'skip this step' alt_config = [default] + pdata['aws-alt'].keys() alt_config = utils._pick('alternative config', alt_config, helpfn=helpfn) if alt_config != default: more_context['alt-config'] = alt_config cfngen.generate_stack(pname, **more_context) return stackname
def generate_stack_from_input(pname, instance_id=None, alt_config=None): """creates a new CloudFormation file for the given project.""" instance_id = instance_id or utils.uin("instance id", core_utils.ymd()) stackname = core.mk_stackname(pname, instance_id) checks.ensure_stack_does_not_exist(stackname) more_context = {'stackname': stackname} pdata = project.project_data(pname) if alt_config: ensure('aws-alt' in pdata, "alternative configuration name given, but project has no alternate configurations") # prompt user for alternate configurations if pdata['aws-alt']: default = 'skip' def helpfn(altkey): if altkey == default: return 'uses the default configuration' try: return pdata['aws-alt'][altkey]['description'] except KeyError: return None if instance_id in pdata['aws-alt'].keys(): LOG.info("instance-id found in known alternative configurations. using configuration %r", instance_id) more_context['alt-config'] = instance_id else: alt_config_choices = [default] + list(pdata['aws-alt'].keys()) if not alt_config: alt_config = utils._pick('alternative config', alt_config_choices, helpfn=helpfn) if alt_config != default: more_context['alt-config'] = alt_config # TODO: return the templates used here, so that they can be passed down to # bootstrap.create_stack() without relying on them implicitly existing # on the filesystem cfngen.generate_stack(pname, **more_context) return stackname
def ami_name(stackname): # elife-api.2015-12-31 return "%s.%s" % (core.project_name_from_stackname(stackname), utils.ymd())
def check_user_input(pname, instance_id=None, alt_config=None): "marshals user input and checks it for correctness" instance_id = instance_id or utils.uin("instance id", core_utils.ymd()) stackname = core.mk_stackname(pname, instance_id) pdata = project.project_data(pname) # alt-config given, die if it doesn't exist if alt_config: ensure( 'aws-alt' in pdata, "alt-config %r given, but project has no alternate configurations" % alt_config) # if the requested instance-id matches a known alt-config, we'll use that alt-config. warn user. if instance_id in pdata['aws-alt'].keys(): LOG.warn("instance-id %r found in alt-config list, using that.", instance_id) alt_config = instance_id # no alt-config given but alt-config options exist, prompt user if not alt_config and pdata['aws-alt']: default_choice = 'skip' def helpfn(altkey): if altkey == default_choice: return 'uses the default configuration' try: return pdata['aws-alt'][altkey]['description'] except KeyError: return None alt_config_choice_list = [default_choice] + list( pdata['aws-alt'].keys()) alt_config_choice = utils._pick('alternative config', alt_config_choice_list, helpfn=helpfn) if alt_config_choice != default_choice: alt_config = alt_config_choice # check the alt-config isn't unique and if it *is* unique, that an instance using it doesn't exist yet. # note: it is *technically* possible that an instance is using a unique configuration but # that its instance-id *is not* the name of the alt-config passed in. # For example, if `journal--prod` didn't exist, I could create `journal--foo` using the `prod` config. if alt_config and alt_config in pdata['aws-alt'] and pdata['aws-alt'][ alt_config]['unique']: dealbreaker = core.mk_stackname(pname, alt_config) # "project 'journal' config 'prod' is marked as unique!" # "checking for any instance named 'journal--prod' ..." print("project %r config %r is marked as unique!" % (pname, alt_config)) print("checking for any instance named %r ..." % (dealbreaker, )) try: checks.ensure_stack_does_not_exist(dealbreaker) except checks.StackAlreadyExistsProblem: # "stack 'journal--prod' exists, cannot re-use unique configuration 'prod'" msg = "stack %r exists, cannot re-use unique configuration %r." % ( dealbreaker, alt_config) raise TaskExit(msg) # check that the instance we want to create doesn't exist try: print("checking %r doesn't exist." % stackname) checks.ensure_stack_does_not_exist(stackname) except checks.StackAlreadyExistsProblem as e: msg = 'stack %r already exists.' % e.stackname raise TaskExit(msg) more_context = {'stackname': stackname} if alt_config: more_context['alt-config'] = alt_config return more_context