Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce docker as a test runner #197

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ruby:2.2.3

# ExecJS runtime -- not optimal
RUN apt-get update && apt-get install curl --yes
RUN curl --silent --location https://deb.nodesource.com/setup_0.12 | bash -
RUN apt-get install --yes nodejs

RUN mkdir -p /usr/src/app/citygram
WORKDIR /usr/src/app/citygram

COPY Gemfile /usr/src/app/citygram/
COPY Gemfile.lock /usr/src/app/citygram/

RUN /usr/local/bundle/bin/bundle install

WORKDIR /usr/src/app/citygram

# docker-compose will mount the working directory as the volume during that pass
2 changes: 2 additions & 0 deletions Procfile.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bundle exec unicorn -c ./config/unicorn.rb
worker: bundle exec sidekiq -c 5 -r ./app.rb
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ First, follow the instructions to install each of the following:
* Install Redis - `brew install redis` on OS X, available from your package manager on Linux or [direct download](http://redis.io/download)

Then, in the command line, run the following to copy the citygram code locally and install all Ruby package dependencies:
##### Install Dependencies

```
git clone https://github.com/codeforamerica/citygram.git
cd citygram
bundle install
```

#### Configure Environment
##### Configure Environment

Make sure your PostgreSQL server is running, then in the terminal run:

Expand Down Expand Up @@ -124,3 +125,37 @@ Run all tests in the `spec/` directory, by running:
```
rake
```

##### Running tests

Run all tests in the `spec/` directory.

```
rake
```



#### Docker Setup (experimental)

Citygram is experimenting with a Docker-based approach to development to see if it aids in developer onboarding.

There is no requirement other than Docker and a command-line.

```
bin/setup
bin/test
```

The first command will download and build all images needed to run Citygram in a local container.
The second command runs tests. You can make changes and run tests from there.

##### Docker Migrations

If your changes need migrations, this command will run them:

```
docker-compose run citygram bundle exec rake db:migrate
```

In fact, most arbitrary commands will work inside the container if you preface them with ```docker-compose run citygram```
1 change: 1 addition & 0 deletions bin/citygram
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose run --service-ports citygram
3 changes: 3 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker-compose build
docker-compose run citygram rake db:docker:create
docker-compose run citygram rake db:migrate
1 change: 1 addition & 0 deletions bin/spec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose run citygram rake test
1 change: 1 addition & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose run citygram bundle exec rake
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
db:
image: mdillon/postgis
redis:
image: redis:3.0.3
citygram:
build: .
env_file: .env.sample
environment:
DB_NAME: citygram_development
PG_USER: postgres
PG_HOST: citygram_db_1
DATABASE_URL: postgres://postgres@citygram_db_1/citygram_development
REDIS_PROVIDER: REDIS_URL
REDIS_URL: redis://redis:6379/
command: bundle exec rackup -o 0.0.0.0 -p 9292
volumes:
- .:/usr/src/app/citygram
ports:
- "9292:9292"
links:
- db
- redis
42 changes: 41 additions & 1 deletion lib/database_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,48 @@ def self.drop_db
pg_command("dropdb #{db_name}")
end

def self.docker_create_db
con = pg_connection
@exists = false
con.set_notice_receiver{|result| @exists = true }
val = con.exec(<<-SQL
DO
$do$
BEGIN

IF EXISTS (SELECT 1 FROM pg_database WHERE datname = '#{db_name}') THEN
RAISE NOTICE 'Citygram database exists, you are ready to test.';
END IF;
END
$do$
SQL
)
if !@exists
puts "Creating DB"
pg_connection.exec("create database #{db_name}")
end
end

def self.docker_drop_db
database.disconnect
pg_connection.exec("drop database #{db_name}")
end

def self.docker_reset
docker_drop_db
docker_create_db
migrate_db
end

def self.pg_connection
PG.connect(
host: ENV["PG_HOST"],
user: ENV["PG_USER"]
)
end

def self.schema_dump
`rm #{schema_path}`
`rm #{schema_path}` if File.exist?(schema_path)
pg_command("pg_dump -i -s -x -O -f #{schema_path} #{db_name}")
end

Expand Down
17 changes: 17 additions & 0 deletions lib/tasks/database.rake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ namespace :db do
task :schema_dump do
Citygram::DatabaseHelper.schema_dump
end

namespace :docker do
desc 'Reset the database'
task :reset do
Citygram::DatabaseHelper.docker_reset
end

desc 'Drop the database'
task :drop do
Citygram::DatabaseHelper.docker_drop_db
end

desc 'Create the database'
task :create do
Citygram::DatabaseHelper.docker_create_db
end
end
end

namespace :g do
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/docker.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace :docker do
desc "Set up all containers needed for system operation"
task :setup do
system("docker create redis:3.0.3")
system("docker create mdillon/postgis")
system("docker build -t citygram .")
end
end