diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ec5a3e0 --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/Procfile.docker b/Procfile.docker new file mode 100644 index 0000000..b3fc609 --- /dev/null +++ b/Procfile.docker @@ -0,0 +1,2 @@ +web: bundle exec unicorn -c ./config/unicorn.rb +worker: bundle exec sidekiq -c 5 -r ./app.rb diff --git a/README.md b/README.md index 21e089d..208855f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ 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 @@ -48,7 +49,7 @@ cd citygram bundle install ``` -#### Configure Environment +##### Configure Environment Make sure your PostgreSQL server is running, then in the terminal run: @@ -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``` diff --git a/bin/citygram b/bin/citygram new file mode 100755 index 0000000..0fb21a2 --- /dev/null +++ b/bin/citygram @@ -0,0 +1 @@ +docker-compose run --service-ports citygram diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..3a3db65 --- /dev/null +++ b/bin/setup @@ -0,0 +1,3 @@ +docker-compose build +docker-compose run citygram rake db:docker:create +docker-compose run citygram rake db:migrate \ No newline at end of file diff --git a/bin/spec b/bin/spec new file mode 100755 index 0000000..f2a6447 --- /dev/null +++ b/bin/spec @@ -0,0 +1 @@ +docker-compose run citygram rake test \ No newline at end of file diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..73473e9 --- /dev/null +++ b/bin/test @@ -0,0 +1 @@ +docker-compose run citygram bundle exec rake diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a35fa61 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/lib/database_helper.rb b/lib/database_helper.rb index 24fa147..607d6a8 100644 --- a/lib/database_helper.rb +++ b/lib/database_helper.rb @@ -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 diff --git a/lib/tasks/database.rake b/lib/tasks/database.rake index 48d1c4a..363ca68 100644 --- a/lib/tasks/database.rake +++ b/lib/tasks/database.rake @@ -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 diff --git a/lib/tasks/docker.rake b/lib/tasks/docker.rake new file mode 100644 index 0000000..0e9380c --- /dev/null +++ b/lib/tasks/docker.rake @@ -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