diff --git a/appengine_war.gradle b/appengine_war.gradle index 9f776e974..09828a4bc 100644 --- a/appengine_war.gradle +++ b/appengine_war.gradle @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -apply from: "${rootDir.path}/projects.gradle" apply plugin: 'war' def environment = rootProject.environment @@ -68,6 +67,10 @@ appengine { if (!rootProject.prodOrSandboxEnv) { version = 'GCLOUD_CONFIG' } + + // Don't set gcpProject directly, it gets overriden in ./build.gradle. + // Do -P environment={crash,alpha} instead. For sandbox/production, + // use Spinnaker. projectId = gcpProject } } @@ -92,4 +95,15 @@ explodeWar.doLast { rootProject.deploy.dependsOn appengineDeployAll rootProject.stage.dependsOn appengineStage -appengineDeployAll.dependsOn rootProject.verifyDeployment + +// Impose verification for all of the deployment tasks. We haven't found a +// better way to do this other than to apply to each of them independently. +// If a new task gets added, it will still fail if "environment" is not defined +// because gcpProject is null. We just won't get as friendly an error message. +appengineDeployAll.configure rootProject.verifyDeploymentConfig +appengineDeploy.configure rootProject.verifyDeploymentConfig +appengineDeployCron.configure rootProject.verifyDeploymentConfig +appengineDeployDispatch.configure rootProject.verifyDeploymentConfig +appengineDeployDos.configure rootProject.verifyDeploymentConfig +appengineDeployIndex.configure rootProject.verifyDeploymentConfig +appengineDeployQueue.configure rootProject.verifyDeploymentConfig diff --git a/build.gradle b/build.gradle index 9b085e009..8253f8417 100644 --- a/build.gradle +++ b/build.gradle @@ -109,21 +109,64 @@ task deploy { description = 'Deploys all services to App Engine.' } -task verifyDeployment { - group = 'deployment' - description = 'Ensure that one cannot deploy to production or sandbox.' - doFirst { - if (rootProject.prodOrSandboxEnv) { - throw new GradleException("Cannot deploy to production or sandbox."); - } - } -} - task stage { group = 'deployment' description = 'Generates application directories for all services.' } +// App-engine environment configuration. We set up all of the variables in +// the root project. + +def environments = ['production', 'sandbox', 'alpha', 'crash'] + +// TODO(mmuller): Move this into internal specialization code. +def projects = ['production': 'domain-registry', + 'sandbox' : 'domain-registry-sandbox', + 'alpha' : 'domain-registry-alpha', + 'crash' : 'domain-registry-crash'] + +def gcpProject = null + +if (environment == '') { + // Keep the project null, this will prevent deployment. Set the + // environment to "alpha" because other code needs this property to + // explode the war file. + environment = 'alpha' +} else if (environment != 'production' && environment != 'sandbox') { + gcpProject = projects[environment] + if (gcpProject == null) { + throw new GradleException("-Penvironment must be one of ${environments}.") + } +} + +rootProject.ext.environment = environment +rootProject.ext.gcpProject = gcpProject +rootProject.ext.prodOrSandboxEnv = environment in ['production', 'sandbox'] + +// Function to verify that the deployment parameters have been set. +def verifyDeploymentParams() { + if (prodOrSandboxEnv) { + // Do not deploy to prod or sandbox. Print a prominent error in bright red. + System.err.println('\033[31;1m-----------------------------------------------------------------') + System.err.println('*** DANGER WILL ROBINSON!') + System.err.println('*** You may not deploy to production or sandbox from gradle. Do a') + System.err.println('*** release from Spinnaker, see deployment playbook.') + System.err.println('-----------------------------------------------------------------') + throw new GradleException('Aborting. See prominent error above.') + } else if (gcpProject == null) { + def error = 'You must specify -P environment={alpha,crash}' + System.err.println("\033[33;1m${error}\033[0m") + throw GradleException("Aborting: ${error}") + } +} + +// Closure that we can just drop into all of our deployment tasks. +rootProject.ext.verifyDeploymentConfig = { + doFirst { verifyDeploymentParams() } +} + +// Subproject configuration. + allprojects { // Skip no-op project if (project.name == 'services') return diff --git a/gradle.properties b/gradle.properties index 73510aea0..16ef21165 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,6 +8,9 @@ verboseTestOutput=false flowDocsFile= enableDependencyLocking=true +# GAE Environment for deployment and staging. +environment= + # Cloud SQL properties # A registry environment name (e.g., 'alpha') or a host[:port] string diff --git a/projects.gradle b/projects.gradle deleted file mode 100644 index 6b061fb82..000000000 --- a/projects.gradle +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2019 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -def environments = ['production', 'sandbox', 'alpha', 'crash'] - -def projects = ['production': 'domain-registry', - 'sandbox' : 'domain-registry-sandbox', - 'alpha' : 'domain-registry-alpha', - 'crash' : 'domain-registry-crash'] - - -def environment = rootProject.findProperty("environment") - -if (environment == null) { - environment = 'production' -} - -def gcpProject = projects[environment] - -if (gcpProject == null) { - throw new GradleException("-Penvironment must be one of ${environments}.") -} - -rootProject.ext.environment = environment -rootProject.ext.gcpProject = gcpProject -rootProject.ext.prodOrSandboxEnv = ['production', 'sandbox'].contains(environment)