Skip to content

Commit

Permalink
introduce a docker volume and network
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Mar 17, 2019
1 parent 2473e94 commit 548f870
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 22 deletions.
4 changes: 4 additions & 0 deletions dashboard.ru
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ require_relative 'dashboard'

use Rack::Static, urls: Dir['*.js', 'edition*'].map {|file| "/#{file}"}

map '/logs' do
run Rack::Directory.new(ENV['HOME'] + '/logs')
end

app = Proc.new do |env|
case env['PATH_INFO']
when '/'
Expand Down
4 changes: 2 additions & 2 deletions edition4/makedepot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5363,8 +5363,8 @@ class ActiveRecord::ConnectionAdapters::Mysql2Adapter
end
else
require 'mysql2'
client = Mysql2::Client.new :host=>'localhost',
:username=>'username', :password => 'password'
client = Mysql2::Client.new host: ENV['MYSQL_HOST'] || 'localhost',
username: 'username', password: 'password'
begin
dbs = client.query('show databases').map {|row| row['Database']}
unless dbs.include? 'depot_production'
Expand Down
2 changes: 1 addition & 1 deletion setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

# set up mysql
mysql_root = '-u root'
mysql_root = (File.exist?('/.dockerenv') ? '' : '-u root')
if ENV['MYSQL_ROOT_PASSWD']
mysql_root += " -p#{ENV['MYSQL_ROOT_PASSWD']}"
elsif system("mysql -u root -proot < /dev/null 2>&0")
Expand Down
92 changes: 74 additions & 18 deletions setup/Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#
# Quick start:
# rake awdwr:dashboard -- installs, configures, and starts a dashboard
# rake awdwr:shell -- shells into the main (dashboard) container
# rake clobber -- removes all containers, images, etc
#
# Containers:
# awdwr-mysql: a mysql server
# awdwr-main: runs the dashboard and can be shelled into
#
# Ports:
# 3333: dashboard, visit http://localhost:3333/ to see the dashboard
# 3334: rails server (maps to port 3000 on the container)
#
# Networks:
# awdwr: used to connect the main dashboard container to mysql
#
# Volumes:
# awdwr-main: testrails "edition4" work directory
#

namespace :mysql do
task :setup do
task :setup => 'awdwr:network' do
next if `docker container ls -a`.include? 'awdwr-mysql'
sh "docker run --network host --name awdwr-mysql " +
sh "docker run --network awdwr --name awdwr-mysql " +
"-e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server"

# wait for container to initialize
Expand All @@ -16,15 +37,16 @@ namespace :mysql do
# grant permissions
cmd = '|docker exec -i awdwr-mysql mysql --user=root --password=root'
open(cmd, 'w') do |f|
f.write "CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';\n"
f.write "GRANT ALL PRIVILEGES ON depot_production.* TO " +
"'username'@'localhost';\n"
f.write "show grants for 'username'@'localhost';\n"
f.write "CREATE USER 'username' IDENTIFIED BY 'password';\n"
f.write "GRANT ALL PRIVILEGES ON depot_production.* TO 'username';\n"
f.write "SHOW GRANTS FOR 'username';\n"
f.write "CREATE USER 'awdwr';\n"
f.write "GRANT ALL PRIVILEGES ON depot_production.* TO 'awdwr';\n"
end
end

task :clobber => :stop do
next unless `docker container ls`.include? 'awdwr-mysql'
next unless `docker container ls -a`.include? 'awdwr-mysql'
system "docker stop awdwr-mysql"
system "docker container rm awdwr-mysql"
end
Expand All @@ -33,7 +55,11 @@ namespace :mysql do
system "docker logs awdwr-mysql"
end

task :ssh => :setup do
task :shell => :setup do
system "docker exec -it awdwr-mysql /bin/bash"
end

task :client => :setup do
system "docker exec -it awdwr-mysql mysql --user=root --password=root"
end

Expand All @@ -53,25 +79,46 @@ namespace :mysql do
end

namespace :awdwr do
task :build => 'mysql:setup' do
sh 'docker build -t awdwr-main --network=host .'
task :network do
next if `docker network ls`.include? 'awdwr'
system 'docker network create awdwr'
end

task :build => ['mysql:setup', :network] do
sh 'docker build --tag awdwr-main --network awdwr .'
end

task :volume do
next if `docker volume ls`.include? 'awdwr-main'
system "docker volume create awdwr-main"
end

task :dashboard do
task :dashboard => [:volume, :network] do
unless `docker image ls`.include? 'awdwr-main'
Rake::Task["awdwr:build"].execute
end

unless `docker ps -a`.include? 'awdwr-main'
awdwr = File.expand_path("..", __dir__)
sh "docker run -p 3333:3333 -p 3334:3000 " +
"-v #{awdwr}:/srv/awdwr:delegated " +
'--name awdwr-main -w /home/awdwr/git/awdwr -d awdwr-main ' +
'/bin/bash --login -c "rackup -p 3333 -o 0.0.0.0 dashboard.ru"'
sh %W(
docker run --name awdwr-main
--publish 3333:3333 --publish 3334:3000
--volume #{awdwr}:/srv/awdwr:delegated
--mount source=awdwr-main,target=/home/awdwr/git/awdwr/edition4/work
--workdir /home/awdwr/git/awdwr
--network awdwr
--env MYSQL_HOST=awdwr-mysql
--detach awdwr-main
/bin/bash --login -c "shotgun -p 3333 -o 0.0.0.0 dashboard.ru"
).join(' ')
end
end

task :ssh do
task :logs do
system "docker logs awdwr-main"
end

task :shell do
unless `docker ps -a`.include? 'awdwr-main'
Rake::Task["awdwr:dashboard"].execute
end
Expand Down Expand Up @@ -100,8 +147,17 @@ namespace :awdwr do
end

task :clobber => :clean do
next unless `docker image ls`.include? 'awdwr-main'
system "docker image rm awdwr-main"
if `docker image ls`.include? 'awdwr-main'
system "docker image rm awdwr-main"
end

if `docker network ls`.include? 'awdwr'
system "docker network rm awdwr"
end

if `docker volume ls`.include? 'awdwr-main'
system "docker volume rm awdwr-main"
end
end
end

Expand Down
10 changes: 9 additions & 1 deletion setup/docker-setup
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ if ! grep -q 'alias sync-' ~/.bashrc ; then
bash_reload=true
fi

# clone awdwr into the git directory
# clone awdwr into the git directory; create work directory
if [[ ! -e ~/git/awdwr ]]; then
git clone https://github.com/rubys/awdwr.git
mkdir -p awdwr/edition4/work
fi

cd -

# install shotgun
if ! [ -x "$(command -v shotgun)" ]; then
gem install shotgun
fi

# common setup
export MYSQL_HOST=awdwr-mysql
source './common-setup'

0 comments on commit 548f870

Please sign in to comment.