예제 #1
0
def write_generated_fields(config):
    """Write gen fields to --generated_fields_path if set, else --project_yaml."""
    if FLAGS.generated_fields_path:
        utils.write_yaml_file(config.generated_fields,
                              FLAGS.generated_fields_path)
    else:
        config.root['generated_fields'] = config.generated_fields
        field_generation.rewrite_generated_fields_back(FLAGS.project_yaml,
                                                       config.root)
예제 #2
0
def setup_project(config, project_yaml, output_yaml_path, output_cleanup_path):
  """Run the full process for initalizing a single new project.

  Note: for projects that have already been deployed, only the updatable steps
  will be run.

  Args:
    config (ProjectConfig): The config of a single project to setup.
    project_yaml (str): Path of the project config YAML.
    output_yaml_path (str): Path to output resulting root config in JSON.
    output_cleanup_path (str): Path to output cleanup shell script.

  Returns:
    A boolean, true if the project was deployed successfully, false otherwise.
  """
  project_id = config.project['project_id']
  steps = _SETUP_STEPS + config.extra_steps

  starting_step = field_generation.get_generated_fields_copy(
      project_id, config.root).get('failed_step', 1)

  deployed = field_generation.is_deployed(project_id, config.root)

  total_steps = len(steps)
  for step_num in range(starting_step, total_steps + 1):
    step = steps[step_num - 1]
    project_id = config.project['project_id']
    logging.info('%s: step %d/%d (%s)', project_id, step_num, total_steps,
                 step.description)

    if deployed and not step.updatable:
      logging.info('Step %d is not updatable, skipping', step_num)
      continue

    try:
      output = step.func(config)
      if output and output.cleanup_commands:
        with open(output_cleanup_path, 'a') as f:
          for cmd in output.cleanup_commands:
            f.write('# {}\n'.format(cmd))
    except Exception as e:  # pylint: disable=broad-except
      traceback.print_exc()
      logging.error('%s: setup failed on step %s: %s', project_id, step_num, e)
      logging.error(
          'Failure information has been written to --output_yaml_path. '
          'Please ensure the config at --project_yaml is updated with any '
          'changes from the config at --output_yaml_path and re-run the script'
          '(Note: only applicable if --output_yaml_path != --project_yaml)')

      # only record failed step if project was undeployed, an update can always
      # start from the beginning
      if not deployed:
        field_generation.get_generated_fields_ref(
            project_id, config.root)['failed_step'] = step_num
        field_generation.rewrite_generated_fields_back(project_yaml,
                                                       output_yaml_path,
                                                       config.root)

      return False

    field_generation.rewrite_generated_fields_back(project_yaml,
                                                   output_yaml_path,
                                                   config.root)

  # if this deployment was resuming from a previous failure, remove the
  # failed step as it is done
  if field_generation.is_generated_fields_exist(project_id, config.root):
    field_generation.get_generated_fields_ref(project_id, config.root,
                                              False).pop('failed_step', None)
  field_generation.rewrite_generated_fields_back(project_yaml, output_yaml_path,
                                                 config.root)
  logging.info('Setup completed successfully.')

  return True