Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge d623efb into df037c8
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieay committed Aug 3, 2016
2 parents df037c8 + d623efb commit 5ab18f2
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 16 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Expand Up @@ -36,16 +36,15 @@ before_deploy:
- export GOPATH=${HOME}/Godeps/_workspace
- go get github.com/concourse/autopilot
- cf install-plugin -f $GOPATH/bin/autopilot
- travis_retry curl -L -o $HOME/18f.zip "https://github.com/18F/18f-cli/archive/release.zip"
- unzip $HOME/18f.zip -d $HOME/18f-cli
- bundle exec rake git:dump_sha
deploy:
- provider: script
script: bash $HOME/18f-cli/18f-cli-release/bin/deploy -o 18f-acq -s staging micropurchase-staging
script: "./bin/deploy.sh staging"
skip_cleanup: true
on:
branch: develop
- provider: script
script: bash $HOME/18f-cli/18f-cli-release/bin/deploy -o 18f-acq -s staging micropurchase
script: "./bin/deploy.sh production"
skip_cleanup: true
on:
branch: master
16 changes: 14 additions & 2 deletions app/credentials/c2_credentials.rb
@@ -1,13 +1,25 @@
require 'concerns/user_provided_service'

class C2Credentials
extend UserProvidedService

def self.host
ENV['C2_HOST']
end

def self.oauth_key
ENV['MICROPURCHASE_C2_OAUTH_KEY']
if use_env_var?
ENV['MICROPURCHASE_C2_OAUTH_KEY']
else
credentials('micropurchase-c2')['oauth_key']
end
end

def self.oauth_secret
ENV['MICROPURCHASE_C2_OAUTH_SECRET']
if use_env_var?
ENV['MICROPURCHASE_C2_OAUTH_SECRET']
else
credentials('micropurchase-c2')['oauth_secret']
end
end
end
21 changes: 21 additions & 0 deletions app/credentials/concerns/user_provided_service.rb
@@ -0,0 +1,21 @@
module UserProvidedService
def credentials(service_name)
user_provided_service(service_name)['credentials']
end

def use_env_var?
Rails.env.development? || Rails.env.test?
end

def user_provided_service(name)
user_provided_services.find { |service| service['name'] == name }
end

def user_provided_services
vcap_services['user-provided']
end

def vcap_services
JSON.parse(ENV['VCAP_SERVICES'])
end
end
10 changes: 9 additions & 1 deletion app/credentials/data_dot_gov_credentials.rb
@@ -1,5 +1,13 @@
require 'concerns/user_provided_service'

class DataDotGovCredentials
extend UserProvidedService

def self.api_key
ENV["DATA_DOT_GOV_API_KEY"]
if use_env_var?
ENV['DATA_DOT_GOV_API_KEY']
else
credentials('data-dot-gov')['api_key']
end
end
end
16 changes: 14 additions & 2 deletions app/credentials/github_credentials.rb
@@ -1,9 +1,21 @@
require 'concerns/user_provided_service'

class GithubCredentials
extend UserProvidedService

def self.client_id
ENV["MICROPURCHASE_GITHUB_CLIENT_ID"]
if use_env_var?
ENV['MICROPURCHASE_GITHUB_CLIENT_ID']
else
credentials('micropurchase-github')['client_id']
end
end

def self.secret
ENV["MICROPURCHASE_GITHUB_SECRET"]
if use_env_var?
ENV['MICROPURCHASE_GITHUB_SECRET']
else
credentials('micropurchase-github')['secret']
end
end
end
28 changes: 24 additions & 4 deletions app/credentials/smtp_credentials.rb
@@ -1,17 +1,37 @@
require 'concerns/user_provided_service'

class SMTPCredentials
extend UserProvidedService

def self.smtp_password
ENV["MICROPURCHASE_SMTP_SMTP_PASSWORD"]
if use_env_var?
ENV['MICROPURCHASE_SMTP_SMTP_PASSWORD']
else
credentials('micropurchase-smtp')['smtp_password']
end
end

def self.smtp_username
ENV["MICROPURCHASE_SMTP_SMTP_USERNAME"]
if use_env_var?
ENV['MICROPURCHASE_SMTP_SMTP_USERNAME']
else
credentials('micropurchase-smtp')['smtp_username']
end
end

def self.default_url_host
ENV["MICROPURCHASE_SMTP_DEFAULT_URL_HOST"]
if use_env_var?
ENV['MICROPURCHASE_SMTP_DEFAULT_URL_HOST']
else
credentials('micropurchase-smtp')['default_url_host']
end
end

def self.default_from
ENV["MICROPURCHASE_SMTP_DEFAULT_FROM"]
if use_env_var?
ENV['MICROPURCHASE_SMTP_DEFAULT_FROM']
else
credentials('micropurchase-smtp')['default_from']
end
end
end
24 changes: 24 additions & 0 deletions bin/deploy.sh
@@ -0,0 +1,24 @@
set -e

API="https://api.cloud.gov"
ORG="18f-acq"
SPACE=$1

if [ $# -ne 1 ]; then
echo "Usage: deploy <space>"
exit
fi

if [ $SPACE = 'production' ]; then
NAME="micropurchase"
MANIFEST="manifest.yml"
elif [ $SPACE = 'staging' ]; then
NAME="micropurchase-staging"
MANIFEST="manifest-staging.yml"
else
echo "Unknown space: $SPACE"
exit
fi

cf login --a $API --u $CF_USERNAME --p $CF_PASSWORD --o $ORG -s $SPACE
cf zero-downtime-push $NAME -f $MANIFEST
28 changes: 25 additions & 3 deletions docs/deployment.md
Expand Up @@ -27,9 +27,8 @@ Cloud.gov allows you to set environment variables manually, but they are wiped
out by a zero-downtime deploy. To get around this issue, we are accessing
environment variables via `Credentials` classes locally.

The classes pick up environment variables set in the shell by the 18F-cli's
deploy script; more information about how it works can be found in [that
project](https://github.com/18F/18f-cli/blob/develop/bin/deploy).
The classes pick up environment variables set in the shell by the
`UserProvidedService` module.

If you're not using Cloud Foundry to deploy, just set the environment variables
directly in your system.
Expand Down Expand Up @@ -100,6 +99,29 @@ Steps to set new environment variables:
`MICROPURCHASE_GITHUB_SECRET` env vars ('micropurchase-github' + 'client_id'
and 'micropurchase-github' + 'secret')

1. Add the service to the manifests:

```
# manifest.yml
services:
- micropurchase-github
```

```
# manifest-staging.yml
services:
- micropurchase-github
```

1. If you want to bind your service to the app before deploying, you can do so
manually.

```bash
$ cf bind-service micropurchase-staging micropurchase-github
```

1. The service keys will automatically be bound to your app and translated into
environment variables on deploy (which happens via Travis CI).

Expand Down
21 changes: 21 additions & 0 deletions manifest-staging.yml
@@ -0,0 +1,21 @@
---
applications:
- name: micropurchase-staging
memory: 1G
instances: 1
domain: 18f.gov
host: micropurchase-staging
buildpack: https://github.com/cloudfoundry/ruby-buildpack.git
services:
- data-dot-gov
- micropurchase-c2
- micropurchase-github
- micropurchase-psql
- micropurchase-smtp
- new-relic
command: script/start
env:
RAILS_ENV: production
RACK_ENV: production
C2_HOST: 'https://c2-staging.18f.gov'
ROOT_URL: 'https://micropurchase-staging.18f.gov'
21 changes: 21 additions & 0 deletions manifest.yml
@@ -0,0 +1,21 @@
---
applications:
- name: micropurchase
memory: 1G
instances: 2
domain: 18f.gov
host: micropurchase
buildpack: https://github.com/cloudfoundry/ruby-buildpack.git
services:
- data-dot-gov
- micropurchase-c2
- micropurchase-github
- micropurchase-psql
- micropurchase-smtp
- new-relic
command: script/start
env:
RAILS_ENV: production
RACK_ENV: production
C2_HOST: 'https://cap.18f.gov'
ROOT_URL: 'https://micropurchase.18f.gov'

0 comments on commit 5ab18f2

Please sign in to comment.