1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import os
5import logging
6
7from google.appengine.api import app_identity
8from google.appengine.ext import ndb
9
10
11def _is_devserver():
12  server_software = os.environ.get('SERVER_SOFTWARE', '')
13  return server_software and server_software.startswith('Development')
14
15_DEFAULT_CATAPULT_PATH = '/catapult'
16_DEFAULT_TARGET = 'prod'
17if _is_devserver():
18  _DEFAULT_TARGET = 'test'
19
20_CONFIG_KEY_NAME = 'pi_cloud_mapper_config_%s' % _DEFAULT_TARGET
21
22_DEFAULT_CONTROL_BUCKET_PATH = 'gs://%s/%s' % (
23    app_identity.get_default_gcs_bucket_name(), _DEFAULT_TARGET)
24
25_DEFAULT_SOURCE_DISK_IMAGE = ('https://www.googleapis.com/compute/v1/projects/'
26  'debian-cloud/global/images/debian-8-jessie-v20151104')
27
28_GCE_DEFAULT_ZONE = 'us-central1-f'
29_GCE_DEFAULT_MACHINE_TYPE = 'n1-standard-1'
30
31
32class CloudConfig(ndb.Model):
33  control_bucket_path = ndb.StringProperty(default=_DEFAULT_CONTROL_BUCKET_PATH)
34  setup_scheme = 'http' if _is_devserver() else 'https'
35  default_corpus = ndb.StringProperty(
36      default='%s://%s' % (
37          setup_scheme, app_identity.get_default_version_hostname()))
38  urlfetch_service_id = ndb.StringProperty(default='')
39  gce_project_name = ndb.StringProperty(
40      default=app_identity.get_application_id())
41  gce_source_disk_image = ndb.StringProperty(default=_DEFAULT_SOURCE_DISK_IMAGE)
42  gce_zone = ndb.StringProperty(default=_GCE_DEFAULT_ZONE)
43  gce_machine_type = ndb.StringProperty(default=_GCE_DEFAULT_MACHINE_TYPE)
44  trace_upload_bucket = ndb.StringProperty(
45      default='%s/traces' % app_identity.get_default_gcs_bucket_name())
46  catapult_path = ndb.StringProperty(default=_DEFAULT_CATAPULT_PATH)
47
48
49def Get():
50  config = CloudConfig.get_by_id(_CONFIG_KEY_NAME)
51  if not config:
52    logging.warning('CloudConfig found, creating a default one.')
53    config = CloudConfig(id=_CONFIG_KEY_NAME)
54
55    if 'GCS_BUCKET_NAME' in os.environ:
56      config.trace_upload_bucket = os.environ['GCS_BUCKET_NAME']
57
58    config.put()
59
60  return config
61