예제 #1
0
 def test_get_generated_fields_ref(self):
   yaml = ruamel.yaml.YAML()
   overall_root = yaml.load(TEST_YAML_CONTENT)
   project1_gf = field_generation.get_generated_fields_ref(
       'data-project-01', overall_root, False)
   project1_gf_modified = field_generation.get_generated_fields_ref(
       'data-project-01', overall_root, False)
   self.assertIs(project1_gf_modified, project1_gf)
예제 #2
0
 def test_get_generated_fields_ref_not_exist(self):
   yaml = ruamel.yaml.YAML()
   overall_root = yaml.load(TEST_YAML_CONTENT)
   project2_gf = field_generation.get_generated_fields_ref(
       'data-project-02', overall_root)
   self.assertFalse(project2_gf)
   project2_gf_modified = field_generation.get_generated_fields_ref(
       'data-project-02', overall_root)
   self.assertIs(project2_gf_modified, project2_gf)
예제 #3
0
def add_project_generated_fields(config):
  """Adds a generated_fields block to a project definition."""
  project_id = config.project['project_id']
  generated_fields = field_generation.get_generated_fields_ref(
      project_id, config.root)
  generated_fields[
      'log_sink_service_account'] = utils.get_log_sink_service_account(
          _LOG_SINK_NAME, project_id)

  gce_instance_info = utils.get_gce_instance_info(project_id)
  if gce_instance_info:
    generated_fields['gce_instance_info'] = gce_instance_info
예제 #4
0
def create_new_project(config):
  """Creates the new GCP project."""
  project_id = config.project['project_id']

  overall_config = config.root['overall']
  org_id = overall_config.get('organization_id')
  folder_id = config.project.get('folder_id', overall_config.get('folder_id'))

  create_project_command = ['projects', 'create', project_id]
  if folder_id:
    create_project_command.extend(['--folder', folder_id])
  elif org_id:
    create_project_command.extend(['--organization', org_id])
  else:
    logging.info('Deploying without a parent organization or folder.')
  # Create the new project.
  runner.run_gcloud_command(create_project_command, project_id=None)
  generated_fields = field_generation.get_generated_fields_ref(
      project_id, config.root)
  generated_fields['project_number'] = utils.get_project_number(project_id)
예제 #5
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
예제 #6
0
 def test_get_ref_not_exist_nor_create(self):
   yaml = ruamel.yaml.YAML()
   overall_root = yaml.load(TEST_YAML_CONTENT)
   with self.assertRaises(utils.InvalidConfigError):
     field_generation.get_generated_fields_ref('data-project-02', overall_root,
                                               False)