docker-rails 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 796f9a53d45a20066b265a383d0e649c69f3a870
4
- data.tar.gz: c265056920b6b21936b3726b780ceb08b28875b3
3
+ metadata.gz: 624744c054f732376d581da291aa2a1130e2a95b
4
+ data.tar.gz: c764f8d565d3ec58200ca31a67164bf6391001f3
5
5
  SHA512:
6
- metadata.gz: 880934be3caa2524812e4ddf8a18f089cf1333473549c59b49919fd4917912ddc27a93a1fbada92d5b3d5d03c33606163260bfaed647a19817fd50503fb82f0f
7
- data.tar.gz: e8c352b19260fc47ace95a562f66a72d43df67dd622f83a50cd23f605e668bd23097a93d713a3cda9d6db242c2957c8d7235e193e21411a4873e874e3f4ad782
6
+ metadata.gz: b5b11391ff8287cb3b213e19c60d4932eaf613ecb9e03cf24617940e4a5ae339b607e1b5f73a804810930be60efd41a1d45aee3c76b674dda3218b5926e14b48
7
+ data.tar.gz: 0727cd30a851867474794b0556f8c0064d5d8a79222aa903783a4948813595d84af8c3e741eca15feda654138f93564cfe6fc44af2bd3a05712e6cbf52109320
data/README.md CHANGED
@@ -15,25 +15,50 @@ A simplified pattern to execute rails applications within Docker (with a CI buil
15
15
 
16
16
  ## Usage
17
17
 
18
+ ### CI
19
+
20
+ CI, the reason this is built. Do it all, do it consistently, do it concurrently, do it easily, and always cleanup after yourself.
21
+
22
+ ```bash
23
+ bundle exec docker-rails ci --build=222 test
24
+ ```
25
+
26
+ or with the environment variable option
27
+
28
+ ```bash
29
+ DOCKER_RAILS_BUILD=222 bundle exec docker-rails ci test
30
+ ```
31
+
32
+ or for local testing (uses `1` for build)
33
+
34
+ ```bash
35
+ bundle exec docker-rails ci test
36
+ ```
37
+
38
+ ### General CLI
39
+
40
+ Almost all of the commands below are in support of the `ci` command, so why not give access directly to them. Helpful additions include `bash_connect` to connect to a running container and `exec` the equivalent of `docker-compose run` (but thor complained and we can't use reserverd word `run`)
41
+
18
42
  ```bash
19
43
  Commands:
20
- docker-rails bash <target> <service_name> # Open a bash shell to a running container e.g. bundle exec docker-rails bash --build=222 development db
21
- docker-rails ci <target> # Execute the works, everything with cleanup included e.g. bundle exec docker-rails ci --build=222 test
22
- docker-rails cleanup <target> # Runs container cleanup functions stop, rm_volumes, rm_compose, rm_dangling, ps_all e.g. bundle exec docker-rails cleanup --build=222 development
23
- docker-rails compose <target> # Writes a resolved docker-compose.yml file e.g. bundle exec docker-rails compose --build=222 test
24
- docker-rails db_check <db> # Runs db_check e.g. bundle exec docker-rails db_check mysql
25
- docker-rails gems_volume <command> # Gems volume management e.g. bundle exec docker-rails gems_volume create
26
- docker-rails help [COMMAND] # Describe available commands or one specific command
27
- docker-rails ps <target> # List containers for the target compose configuration e.g. bundle exec docker-rails ps --build=222 development
28
- docker-rails ps_all # List all remaining containers regardless of state e.g. bundle exec docker-rails ps_all
29
- docker-rails rm_compose # Remove generated docker_compose file e.g. bundle exec docker-rails rm_compose --build=222 development
30
- docker-rails rm_dangling # Remove danging images e.g. bundle exec docker-rails rm_dangling
31
- docker-rails rm_volumes <target> # Stop all running containers and remove corresponding volumes for the given build/target e.g. bundle exec docker-rails rm_volumes --build=222 development
32
- docker-rails stop <target> # Stop all running containers for the given build/target e.g. bundle exec docker-rails stop --build=222 development
33
- docker-rails up <target> # Up the docker-compose configuration for the given build/target. Use -d for detached mode. e.g. bundle exec docker-rails up -d --build=222 test
44
+ docker-rails bash_connect <target> <service_name> # Open a bash shell to a running container e.g. bundle exec docker-rails bash --build=222 development db
45
+ docker-rails build <target> # Build for the given build/target e.g. bundle exec docker-rails build --build=222 development
46
+ docker-rails ci <target> # Execute the works, everything with cleanup included e.g. bundle exec docker-rails ci --build=222 test
47
+ docker-rails cleanup <target> # Runs container cleanup functions stop, rm_volumes, rm_compose, rm_dangling, ps_all e.g. bundle exec docker-rails cleanup --build=222 development
48
+ docker-rails compose <target> # Writes a resolved docker-compose.yml file e.g. bundle exec docker-rails compose --build=222 test
49
+ docker-rails db_check <db> # Runs db_check e.g. bundle exec docker-rails db_check mysql
50
+ docker-rails exec <target> <service_name> <command> # Run an arbitrary command on a given service container e.g. bundle exec docker-rails exec --build=222 development db bash
51
+ docker-rails gems_volume <command> # Gems volume management e.g. bundle exec docker-rails gems_volume create
52
+ docker-rails help [COMMAND] # Describe available commands or one specific command
53
+ docker-rails ps <target> # List containers for the target compose configuration e.g. bundle exec docker-rails ps --build=222 development
54
+ docker-rails ps_all # List all remaining containers regardless of state e.g. bundle exec docker-rails ps_all
55
+ docker-rails rm_dangling # Remove danging images e.g. bundle exec docker-rails rm_dangling
56
+ docker-rails rm_volumes <target> # Stop all running containers and remove corresponding volumes for the given build/target e.g. bundle exec docker-rails rm_volumes --build=222 development
57
+ docker-rails stop <target> # Stop all running containers for the given build/target e.g. bundle exec docker-rails stop --build=222 development
58
+ docker-rails up <target> # Up the docker-compose configuration for the given build/target. Use -d for detached mode. e.g. bundle exec docker-rails up -d --build=222 test
34
59
 
35
60
  Options:
36
- -b, [--build=BUILD] # Build name e.g. 123
61
+ -b, [--build=BUILD] # Build name e.g. 123. Can also be specified as environment variable DOCKER_RAILS_BUILD
37
62
  # Default: 1
38
63
  ```
39
64
 
@@ -27,6 +27,6 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency 'activerecord'
28
28
 
29
29
  s.add_dependency 'docker-api'
30
- s.add_dependency 'dry-config', '>= 1.2.6'
30
+ s.add_dependency 'dry-config', '>= 1.2.7'
31
31
  s.add_dependency 'thor'
32
32
  end
@@ -23,7 +23,11 @@ module Docker
23
23
  end
24
24
 
25
25
  def configure(options)
26
- ENV['DOCKER_RAILS_BUILD'] = @build = options[:build]
26
+ # Allow CLI option `build` to fallback to an env variable DOCKER_RAILS_BUILD. Note that CLI provides a default build value of 1, so check against the default and existence of the env var.
27
+ build = options[:build]
28
+ build = ENV['DOCKER_RAILS_BUILD'] if build.to_i == 1 && !ENV['DOCKER_RAILS_BUILD'].nil?
29
+
30
+ ENV['DOCKER_RAILS_BUILD'] = @build = build
27
31
  @target = options[:target]
28
32
 
29
33
  # load the docker-rails.yml
@@ -64,6 +68,11 @@ module Docker
64
68
  exec_compose 'up', false, options #unless skip? :up
65
69
  end
66
70
 
71
+ def exec_build
72
+ # Run the compose configuration
73
+ exec_compose 'build'
74
+ end
75
+
67
76
  def exec_ps
68
77
  # Run the compose configuration
69
78
  exec_compose 'ps'
@@ -75,7 +84,7 @@ module Docker
75
84
  @compose_config.each_key do |service_name|
76
85
  stop(service_name)
77
86
  end
78
- # puts "\nDone."
87
+ puts 'Done.'
79
88
  end
80
89
 
81
90
  def exec_remove_volumes
@@ -84,14 +93,17 @@ module Docker
84
93
  @compose_config.each_key do |service_name|
85
94
  rm_v(service_name)
86
95
  end
87
- # puts "\nDone."
96
+ puts 'Done.'
88
97
  end
89
98
 
90
99
  def rm_dangling
91
100
  puts "\n\nCleaning up dangling images..."
92
101
  puts '-----------------------------'
93
- exec 'docker images --filter dangling=true -q | xargs docker rmi'
94
- # puts "\nDone."
102
+
103
+ list_images_cmd = 'docker images --filter dangling=true -q'
104
+ output = exec list_images_cmd, true
105
+ exec "#{list_images_cmd} | xargs docker rmi" if !output.nil? && output.length > 0
106
+ puts 'Done.'
95
107
  end
96
108
 
97
109
  def show_all_containers
@@ -100,7 +112,12 @@ module Docker
100
112
  system 'docker ps -a'
101
113
  end
102
114
 
103
- def exec_bash(service_name)
115
+ def exec_run(service_name, command)
116
+ # Run the compose configuration
117
+ exec_compose "run #{service_name} #{command}"
118
+ end
119
+
120
+ def exec_bash_connect(service_name)
104
121
  # docker exec -it 222_db_1 bash
105
122
  container_name = get_container_name(service_name)
106
123
  exec "docker exec -it #{container_name} bash"
@@ -4,7 +4,7 @@ module Docker
4
4
  class Main < Thor
5
5
  # default_task :help
6
6
 
7
- class_option :build, aliases: ['-b'], type: :string, desc: 'Build name e.g. 123', default: '1'
7
+ class_option :build, aliases: ['-b'], type: :string, desc: 'Build name e.g. 123. Can also be specified as environment variable DOCKER_RAILS_BUILD', default: '1'
8
8
 
9
9
  desc 'db_check <db>', 'Runs db_check e.g. bundle exec docker-rails db_check mysql'
10
10
  subcommand 'db_check', Docker::Rails::CLI::DbCheck
@@ -28,6 +28,7 @@ module Docker
28
28
  invoke CLI::GemsVolume, :create, [target], options
29
29
  invoke :before
30
30
  begin
31
+ invoke :build # on CI - always build to ensure dockerfile hasn't been altered - small price to pay for consistent CI.
31
32
  invoke :up
32
33
  ensure
33
34
  invoke :cleanup
@@ -61,6 +62,14 @@ module Docker
61
62
  app.exec_up(compose_options)
62
63
  end
63
64
 
65
+ desc 'build <target>', 'Build for the given build/target e.g. bundle exec docker-rails build --build=222 development'
66
+
67
+ def build(target)
68
+ invoke :compose
69
+ App.configured(target, options).exec_build
70
+ end
71
+
72
+
64
73
  desc 'compose <target>', 'Writes a resolved docker-compose.yml file e.g. bundle exec docker-rails compose --build=222 test'
65
74
 
66
75
  def compose(target)
@@ -89,7 +98,7 @@ module Docker
89
98
  App.configured(target, options).exec_remove_volumes
90
99
  end
91
100
 
92
- desc 'rm_compose', 'Remove generated docker_compose file e.g. bundle exec docker-rails rm_compose --build=222 development'
101
+ desc 'rm_compose', 'Remove generated docker_compose file e.g. bundle exec docker-rails rm_compose --build=222 development', hide: true
93
102
 
94
103
  def rm_compose(build = nil, target = nil)
95
104
  App.instance.rm_compose
@@ -114,15 +123,27 @@ module Docker
114
123
  App.instance.show_all_containers
115
124
  end
116
125
 
117
- desc 'bash <target> <service_name>', 'Open a bash shell to a running container e.g. bundle exec docker-rails bash --build=222 development db'
126
+ desc 'bash_connect <target> <service_name>', 'Open a bash shell to a running container e.g. bundle exec docker-rails bash --build=222 development db'
127
+
128
+ def bash_connect(target, service_name)
129
+ # init singleton with full options
130
+ app = App.configured(target, options)
131
+
132
+ invoke :compose, [target], []
133
+
134
+ app.exec_bash_connect(service_name)
135
+ end
136
+
137
+
138
+ desc 'exec <target> <service_name> <command>', 'Run an arbitrary command on a given service container e.g. bundle exec docker-rails exec --build=222 development db bash'
118
139
 
119
- def bash(target, service_name)
140
+ def exec(target, service_name, command)
120
141
  # init singleton with full options
121
142
  app = App.configured(target, options)
122
143
 
123
144
  invoke :compose, [target], []
124
145
 
125
- app.exec_bash(service_name)
146
+ app.exec_run(service_name, command)
126
147
  end
127
148
 
128
149
 
@@ -13,16 +13,21 @@ module Docker
13
13
  end
14
14
 
15
15
  def load!(environment, *filenames)
16
- if environment.nil?
17
- puts 'Environment unspecified, generating docker-compose.yml based on root :compose yaml key.'
18
- environment = 'docker-compose'
19
- end
20
16
 
17
+ # reject nil target environments
18
+ raise 'Target environment unspecified.' if environment.nil?
19
+
20
+ # default the filename if unspecified
21
21
  if filenames.empty?
22
22
  puts 'Using docker-rails.yml'
23
23
  filenames = ['docker-rails.yml']
24
24
  end
25
25
 
26
+ # reject unknown target environments
27
+ config = load_unpruned(environment, *filenames)
28
+ raise "Unknown target environment '#{environment.to_sym}'" if config[environment.to_sym].nil?
29
+
30
+ # finally, load the config as internal state
26
31
  super(environment, *filenames)
27
32
  end
28
33
 
@@ -1,5 +1,5 @@
1
1
  module Docker
2
2
  module Rails
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -19,22 +19,28 @@ describe Docker::Rails::Config do
19
19
  expect(config.production).to be_nil
20
20
  end
21
21
 
22
- it 'should read default env and file' do
23
- Dir.chdir(File.dirname(__FILE__)) do
24
- config.clear
25
- config.load!(nil)
26
- end
27
-
28
- assert_common_top_level_settings
22
+ it 'should fail if the target environment does not exist' do
23
+ expect {
24
+ Dir.chdir(File.dirname(__FILE__)) do
25
+ config.clear
26
+ config.load!(:foo)
27
+ end
28
+ }.to raise_error /Unknown target environment/
29
+ end
29
30
 
30
- # ensure no unnecessary environments make it into the resolved configuration
31
- expect(config.development).to be_nil
32
- expect(config.production).to be_nil
31
+ it 'should fail if the target environment is nil' do
32
+ expect {
33
+ Dir.chdir(File.dirname(__FILE__)) do
34
+ config.clear
35
+ config.load!(nil)
36
+ end
37
+ }.to raise_error /Target environment unspecified/
33
38
  end
34
39
 
40
+
35
41
  context ':development' do
36
42
 
37
- before(:each){
43
+ before(:each) {
38
44
  Dir.chdir(File.dirname(__FILE__)) do
39
45
  config.clear
40
46
  config.load!(:development)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-18 00:00:00.000000000 Z
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 1.2.6
103
+ version: 1.2.7
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: 1.2.6
110
+ version: 1.2.7
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: thor
113
113
  requirement: !ruby/object:Gem::Requirement