Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Update project deployment tooling, add docker-compose development environment #226

Merged
merged 4 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"apps": [
{
"root": "src",
"outDir": "dist",
"outDir": "dist/_site",
"assets": [
"assets",
"favicon.ico"
Expand Down
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ violations.xml

# Vagrant
.vagrant/
ansible/*.retry
ansible/roles/azavea.*
deployment/ansible/*.retry
deployment/ansible/roles/azavea.*

.env

deployment/terraform/.terraform
*.tfvars
*.tfplan
*.tfstate*
37 changes: 12 additions & 25 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ node {
}

env.AWS_PROFILE = 'climate'
env.GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
env.CLIMATE_CHANGE_LAB_PORT = 4422
env.CC_SETTINGS_BUCKET = 'staging-us-east-1-climate-lab-config'
env.CC_SITE_BUCKET = 'staging-us-east-1-climate-lab-site'

// set environment variables for staging or production; default to staging
env.API_HOST = 'https://app.staging.climate.azavea.com';
env.S3_WEBSITE_CONFIG_DIR = ".";
env.CLOUDFRONT_ID_VAR = "CCLAB_AWS_STAGING_CLOUDFRONT_ID";

if (env.BRANCH_NAME == 'master') {
env.API_HOST = 'https://app.climate.azavea.com';
env.S3_WEBSITE_CONFIG_DIR = "production_s3/";
env.CLOUDFRONT_ID_VAR = "CCLAB_AWS_PRODUCTION_CLOUDFRONT_ID";
env.CC_SETTINGS_BUCKET = 'production-us-east-1-climate-lab-config'
env.CC_SITE_BUCKET = 'production-us-east-1-climate-lab-site'
}

stage('setup') {
wrap([$class: 'AnsiColorBuildWrapper']) {
sh 'scripts/setup'
sh 'scripts/update'
}
}

Expand All @@ -34,12 +32,12 @@ node {
export const apiHost = \'${env.API_HOST}\';
export const defaultScenario = \'RCP85\';\n"""

sh 'vagrant ssh -c "/vagrant/scripts/test --jenkins"'
sh './scripts/test --jenkins'

step([$class: 'WarningsPublisher',
parserConfigurations: [[
parserName: 'JSLint',
pattern: 'violations.xml'
pattern: 'jenkins/violations.xml'
]],
// mark build unstable if there are any linter warnings
unstableTotalAll: '0',
Expand All @@ -50,28 +48,17 @@ node {

stage('cibuild') {
wrap([$class: 'AnsiColorBuildWrapper']) {
sh 'vagrant ssh -c "/vagrant/scripts/cibuild"'
sh './scripts/cibuild'
}
}

// Publish to S3
stage('publish') {
stage('infra') {
if (env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master') {
// Decode the credentials stored within Jenkins.
withCredentials([[$class: 'StringBinding',
credentialsId: 'CCLAB_AWS_S3_ACCESS_KEY',
variable: 'CCLAB_AWS_S3_ACCESS_KEY'],
[$class: 'StringBinding',
credentialsId: 'CCLAB_AWS_S3_SECRET_ACCESS_KEY',
variable: 'CCLAB_AWS_S3_SECRET_ACCESS_KEY'],
[$class: 'StringBinding',
credentialsId: "${env.CLOUDFRONT_ID_VAR}",
variable: 'CCLAB_AWS_CLOUDFRONT_ID']]) {
wrap([$class: 'AnsiColorBuildWrapper']) {
writeFile file: '.env', text: """export CCLAB_AWS_S3_ACCESS_KEY="${env.CCLAB_AWS_S3_ACCESS_KEY}"\nexport CCLAB_AWS_S3_SECRET_ACCESS_KEY="${env.CCLAB_AWS_S3_SECRET_ACCESS_KEY}"\nexport CCLAB_AWS_CLOUDFRONT_ID="${env.CCLAB_AWS_CLOUDFRONT_ID}"\nexport S3_WEBSITE_CONFIG_DIR="${env.S3_WEBSITE_CONFIG_DIR}"\n"""
sh 'vagrant ssh -c "/vagrant/scripts/cipublish"'
sh 'docker-compose -f docker-compose.ci.yml run --rm terraform ./scripts/infra plan'
sh 'docker-compose -f docker-compose.ci.yml run --rm terraform ./scripts/infra apply'
}
}
}
}

Expand Down Expand Up @@ -100,7 +87,7 @@ node {
} finally {
stage('cleanup') {
wrap([$class: 'AnsiColorBuildWrapper']) {
sh 'vagrant halt'
sh 'docker-compose down -v'
}
}
}
Expand Down
47 changes: 22 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

### Requirements

Requires node.js 6.11+ and npm 3.10.10+ for Angular CLI compatibility.
Requires node.js 6.11+ and yarn 0.24.0+ for Angular CLI compatibility.

Alternatively, you can bring up the vagrant VM that has the dependencies installed.

### Setup

If your development host machine meets the requirements above, simply:

- Clone this repo and run `npm install`
- Clone this repo and run `yarn install`
- `cp example/constants.ts.example src/app/constants.ts`
- Edit `constants.ts` to set the API server name and API key
- `npm run serve`
- `yarn run serve`

The site will then be available at [http://localhost:4200](http://localhost:4200) on your host machine.

Expand All @@ -25,55 +25,52 @@ _Recommended only if you don't have the requirements above installed on your hos

Requires ansible 2.2+.

To use the vagrant machine, first start it with:
To use the vagrant machine, first start and provision it with:
```bash
vagrant up
./scripts/setup
```

Then to start the development server within the VM:

```bash
vagrant ssh
cd /vagrant
npm run serve:vm # Replaces `npm run serve` to include VM specific serve options
./scripts/server --vm # Replaces `npm run serve` to include VM specific serve options
```

The site will then be available at [http://localhost:4200](http://localhost:4200) on your host machine.

Note that if your host OS differs from the VM, you may need to run

```
npm rebuild node-sass
```

within the environment (host or VM) before running other build scripts in that environment.

### Getting started

| Command | Purpose | Use When ... |
|------|---------|--------------|
| `npm run serve` | Serve project in dev mode using Angular CLI | For quick browser updating with refresh |
| `npm run lint` | Run typescript linter | To clean up your angular and .ts files |
| `npm run test` | Run project tests | |
| `npm run build:prod` | Build production version of application | When ready to deploy |
| `scripts/update` | Update project dependencies | |
| `scripts/server` | Serve project in dev mode using Angular CLI | For quick browser updating with refresh |
| `scripts/test` | Run project tests | |
| `scripts/console` | Run commands inside of a container | |
| `scripts/cibuild` | Build production version of application | Before deploying |
| `scripts/infra` | Apply terraform changes and deploy static site | When ready to deploy |

Navigate to [http://localhost:4200](http://localhost:4200) in your browser.

Additional commands available in package.json.

### Deployment

_All deployment steps should be done within the vagrant vm provided with this project. The vm is provisioned with the tools necessary to deploy._

First, build the application via `npm run build:prod`. The static site will be built in the `dist/` directory.

Copy the environment file with `cp .env.example .env` and add your AWS access/secret keys and CloudFront distribution ID to it.
First, build the application via `scripts/cibuild`. The static site will be built in the `dist/_site` directory.

Before publishing, ensure the correct `apiHost` is set for the target environment in `src/app/constants.ts` (staging host, or production).

Push changes to the staging site with `s3_website push`
Push changes to the staging site with docker-compose:

To push changes to the production site, specify the production S3 configuration with:
```
s3_website push --config-dir=production_s3/
```bash
$ export CC_SETTINGS_BUCKET=staging-us-east-1-climate-lab-site
$ export CC_SITE_BUCKET=staging-us-east-1-climate-lab-config
$ docker-compose -f docker-compose.ci.yml run --rm terraform scripts/infra plan
$ docker-compose -f docker-compose.ci.yml run --rm terraform scripts/infra apply
```

To push changes to the production site, set `CC_SETTINGS_BUCKET=production-us-east-1-climate-lab-site`
and `CC_SITE_BUCKET=production-us-east-1-climate-lab-site`
11 changes: 9 additions & 2 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ Vagrant.configure("2") do |config|
app.vm.network "private_network", ip: ENV.fetch("CLIMATE_CHANGE_PRIVATE_IP", "192.168.8.111")

app.vm.synced_folder '.', ROOT_VM_DIR, type: "nfs", mount_options: MOUNT_OPTIONS
app.vm.synced_folder "~/.aws", "/home/vagrant/.aws"

app.vm.provision "ansible" do |ansible|
ansible.galaxy_role_file = "ansible/roles.yml"
ansible.playbook = "ansible/climate-change-lab.yml"
ansible.galaxy_role_file = "deployment/ansible/roles.yml"
ansible.playbook = "deployment/ansible/climate-change-lab.yml"
ansible.groups = ANSIBLE_GROUPS.merge(ANSIBLE_ENV_GROUPS)
ansible.raw_arguments = ["--timeout=60"]
end

app.vm.provision :shell do |shell|
shell.inline = <<-SHELL
grep "cd /vagrant" /home/vagrant/.bashrc || echo "cd /vagrant" >> /home/vagrant/.bashrc
SHELL
end

app.vm.network "forwarded_port", guest: 4200, host: ENV.fetch("CLIMATE_CHANGE_LAB_PORT", 4200),
auto_correct: true
app.ssh.forward_x11 = true
Expand Down
13 changes: 0 additions & 13 deletions ansible/climate-change-lab.yml

This file was deleted.

3 changes: 0 additions & 3 deletions ansible/roles/climate-change-lab.app/defaults/main.yml

This file was deleted.

27 changes: 0 additions & 27 deletions ansible/roles/climate-change-lab.app/tasks/main.yml

This file was deleted.

13 changes: 13 additions & 0 deletions deployment/ansible/climate-change-lab.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- hosts: app
become: true

pre_tasks:
- name: Update APT cache
apt: update_cache=yes

roles:
- { role: "azavea.ntp" }
- { role: "azavea.aws-cli" }
- { role: "climate-change-lab.docker" }
- { role: "climate-change-lab.environment" }
3 changes: 3 additions & 0 deletions deployment/ansible/group_vars/all
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker_compose_version: 1.15.0
aws_cli_version: 1.11.*
aws_profile: climate
14 changes: 14 additions & 0 deletions deployment/ansible/roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- src: azavea.aws-cli
version: 0.1.0

- src: azavea.docker
version: 4.0.0

- src: azavea.ntp
version: 0.2.0

- src: azavea.pip
version: 1.0.0

- src: azavea.python-security
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
dependencies:
- { role: azavea.pip }
- { role: azavea.python-security }
- { role: azavea.docker }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Install Docker Compose
pip: name=docker-compose version="{{ docker_compose_version }}"

- name: Add Ansible user to Docker group
user: name="{{ ansible_user }}"
groups=docker
append=yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: Set Environment variable for AWS profile
lineinfile: dest=/etc/environment regexp=^AWS_PROFILE line="AWS_PROFILE={{ aws_profile }}"
Loading