dockrails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f5568873ad53a84d13c5a2d2f500a61091516ee
4
+ data.tar.gz: eaa4edcea9f1a27b523429b36ea3417b85253be5
5
+ SHA512:
6
+ metadata.gz: 6ff9801d88d448337b7c159adeda7cd72179ad93410f1d0a2b81e524d590e96df076566efc57edbc36a3d256028cfce26f1b06680eeab54c355b5ff84d37300b
7
+ data.tar.gz: be8d7428bb39d486b79aa8620e857dda836a96706bebf81b30ff93e0bfbc0bb0428bb343ddd07dea1a2a5d1acdd37f26bf866dc4a78cdef7498b6f1ff67a212d
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Guillaume Montard
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/dockrails ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+ require 'dockrails'
6
+
7
+ program :name, 'dockrails'
8
+ program :version, Dockrails::VERSION
9
+ program :description, 'CLI to generate and Run a Rails environment with Docker'
10
+
11
+ command :init do |c|
12
+ c.syntax = 'railsdock init'
13
+ c.summary = 'Initialize your Rails Docker environment'
14
+ c.description = ''
15
+ c.example 'description', 'railsdock init'
16
+ c.action do |args, options|
17
+
18
+ existing_files = ["Dockerfile", "docker-compose.yml", "docker-sync.yml"].map { |file| File.exist?(file) }
19
+ if existing_files.include?(true)
20
+ say "We detected an existing Docker install in this folder"
21
+ answer = agree "Do you want to continue and override it?"
22
+ exit if !answer
23
+ end
24
+
25
+ rails_app = ask "\nType the name of your Rails App folder"
26
+ if !system("find #{rails_app} > /dev/null")
27
+ say "\nUnable to find your Rails application folder in the current path, please try again"
28
+ say "If you want to create a new Rails app: rails new my_app -B -d postgresql"
29
+ exit
30
+ end
31
+
32
+ generate = Dockrails::Generate.new(app_path: rails_app)
33
+ generate.create_folders
34
+ generate.create_dockerfile
35
+ generate.create_start_script
36
+ generate.create_docker_compose
37
+ generate.create_docker_sync
38
+
39
+ say "\nSuccessful dockerization!
40
+ Please take a look at the different files generated:
41
+ - Dockerfile
42
+ - docker-compose.yml
43
+ - docker-sync.yml"
44
+
45
+ say "\nStart your dockerized Rails Environement: railsdock start"
46
+ say "Familiarize yourself with the different commands: railsdock --help"
47
+ end
48
+ end
49
+
50
+ command :start do |c|
51
+ c.syntax = 'railsdock start'
52
+ c.summary = 'Start the docker stack containers'
53
+ c.description = ''
54
+ c.example 'description', 'railsdock start'
55
+ c.action do |args, options|
56
+ system("docker-sync-stack start")
57
+ end
58
+ end
59
+
60
+ command :clean do |c|
61
+ c.syntax = 'railsdock clean'
62
+ c.summary = 'Clean the docker stack containers'
63
+ c.description = ''
64
+ c.example 'description', 'railsdock clean'
65
+ c.action do |args, options|
66
+ system("docker-sync-stack clean")
67
+ end
68
+ end
69
+
70
+ command :run do |c|
71
+ c.syntax = 'railsdock run [container] [command]'
72
+ c.summary = 'Run a command on a specific container'
73
+ c.description = ''
74
+ c.example 'description', 'railsdock run [web|job|db|redis] bundle install'
75
+ c.action do |args, options|
76
+ if args.size < 2
77
+ say "\n Please specify a container and command to run, ex: railsdock run app bundle install"
78
+ else
79
+ system("docker-compose run #{args.join(" ")}")
80
+ end
81
+ end
82
+ end
83
+
84
+ command :attach do |c|
85
+ c.syntax = 'railsdock attach [container]'
86
+ c.summary = 'Attach TTY to a specific container (usefull for ByeBug or Pry)'
87
+ c.description = 'toto'
88
+ c.action do |args, options|
89
+ if args.size < 1
90
+ say "\n Please specify a container, ex: railsdock attach app"
91
+ else
92
+ system("docker attach $(docker-compose ps -q #{args[0]})")
93
+ end
94
+ end
95
+ end
data/lib/dockrails.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "dockrails/version"
2
+ require "dockrails/generate"
@@ -0,0 +1,192 @@
1
+ module Dockrails
2
+ class Generate
3
+
4
+ def initialize(app_path:)
5
+ check_system_requirements
6
+ @app_path = app_path
7
+ @config = ask_configuration
8
+ end
9
+
10
+ def check_system_requirements
11
+ requirement = Hash.new
12
+ requirement["unison"] = system("which unison > /dev/null")
13
+ requirement["docker"] = system("which docker > /dev/null")
14
+ requirement["docker-sync"] = system("which docker-sync > /dev/null")
15
+ requirement["docker-compose"] = system("which docker-compose > /dev/null")
16
+
17
+ requirement.each do |k,v|
18
+ if v == false
19
+ say "please install \"#{k}\" in order to continue\n"
20
+ end
21
+ end
22
+ end
23
+
24
+ def ask_configuration
25
+ config = Hash.new
26
+ config["ruby-version"] = choose("\nChoose a ruby version?", "latest", "2.4", "2.3", "2.2")
27
+ config["db"] = choose("\nChoose a DB Engine?", :pgsql, :mysql)
28
+ config["db_name"] = ask "\nChoose a database name"
29
+ config["db_user"] = ask "\nChoose a database username"
30
+ config["db_password"] = ask "\nChoose a database password"
31
+ config["redis"] = agree("\nDo you need a Redis DB?")
32
+ config["sidekiq"] = agree("\nDo you need a SideKiq Container") if config["redis"]
33
+
34
+ user_agree = agree "\nSummary:
35
+ - Ruby version: #{config["ruby-version"]}
36
+ - DB Engine: #{config["db"]}
37
+ - DB Name: #{config["db_name"]}
38
+ - DB Username: #{config["db_user"]}
39
+ - DB Password: #{config["db_password"]}
40
+ - Redis? #{config["redis"]}
41
+ - Job Container? #{config["sidekiq"] ||= "false"}\n
42
+ Is this correct?"
43
+
44
+ unless user_agree
45
+ ask_configuration
46
+ end
47
+
48
+ return(config)
49
+ end
50
+
51
+ def create_folders
52
+ system("rm -rf data")
53
+ system("mkdir -p data/sql")
54
+ system("mkdir -p data/redis") if @config["redis"]
55
+ end
56
+
57
+ def create_dockerfile
58
+ File.open("Dockerfile", 'w') do |f|
59
+ f.write("FROM ruby:#{@config["ruby-version"]}
60
+ RUN apt-get update && apt-get install -y \
61
+ build-essential \
62
+ wget \
63
+ git-core \
64
+ libxml2 \
65
+ libxml2-dev \
66
+ libxslt1-dev \
67
+ nodejs \
68
+ imagemagick \
69
+ libmagickcore-dev \
70
+ libmagickwand-dev \
71
+ libpq-dev \
72
+ && rm -rf /var/lib/apt/lists/*
73
+
74
+ RUN mkdir /app
75
+ RUN mkdir -p /root/.ssh/
76
+
77
+ WORKDIR /app
78
+
79
+ RUN ssh-keyscan -H github.com >> ~/.ssh/known_hosts
80
+ ENV GEM_HOME /bundle
81
+ ENV PATH $GEM_HOME/bin:$PATH
82
+ ENV BUNDLE_PATH /bundle
83
+ ENV BUNDLE_BIN /bundle/
84
+ RUN gem install bundler -v '1.10.6' \
85
+ && bundle config --global path \"$GEM_HOME\" \
86
+ && bundle config --global bin \"$GEM_HOME/bin\"")
87
+ end
88
+ end
89
+
90
+ def create_start_script
91
+ system("mkdir -p #{@app_path}/scripts")
92
+ File.open("#{@app_path}/scripts/start-dev.sh", "w") do |f|
93
+ f.write("#!/bin/bash
94
+ bundle check || bundle install
95
+ rm -rf tmp/pids/* && bundle exec rails server --port 3000 --binding 0.0.0.0")
96
+ end
97
+ end
98
+
99
+ def create_docker_compose
100
+ File.open("docker-compose.yml", 'w') do |f|
101
+ f.write "version: '2'\n"
102
+ f.write "services:\n"
103
+ f.write " db:\n"
104
+
105
+ case @config["db"] when :mysql
106
+ f.write " image: mysql\n"
107
+ f.write " volumes:\n"
108
+ f.write " - ./data/sql:/var/lib/mysql\n"
109
+ f.write " ports:\n"
110
+ f.write " - \"3306:3306\"\n"
111
+ f.write " environment:\n"
112
+ f.write " MYSQL_DATABASE: #{@config["db_name"]}\n"
113
+ f.write " MYSQL_USER: #{@config["db_user"]}\n"
114
+ f.write " MYSQL_PASSWORD: #{@config["db_password"]}\n"
115
+ f.write " MYSQL_ROOT_PASSWORD: #{@config["db_password"]}\n"
116
+ when :pgsql
117
+ f.write " image: postgres\n"
118
+ f.write " volumes:\n"
119
+ f.write " - ./data/sql:/var/lib/postgresql/data\n"
120
+ f.write " ports:\n"
121
+ f.write " - \"5432:5432\"\n"
122
+ f.write " environment:\n"
123
+ f.write " POSTGRES_DB: #{@config["db_name"]}\n"
124
+ f.write " POSTGRES_USER: #{@config["db_user"]}\n"
125
+ f.write " POSTGRES_PASSWORD: #{@config["db_password"]}\n"
126
+ end
127
+
128
+ if @config["redis"]
129
+ f.write "\n redis:\n"
130
+ f.write " image: redis\n"
131
+ f.write " volumes:\n"
132
+ f.write " - ./data/redis:/data\n"
133
+ f.write " ports:\n"
134
+ f.write " - \"6379:6379\"\n"
135
+ end
136
+
137
+ f.write "\n web:\n"
138
+ f.write " build: .\n"
139
+ f.write " command: sh scripts/start-dev.sh\n"
140
+ f.write " volumes:\n"
141
+ f.write " - #{@app_path}-web-sync:/app:rw\n"
142
+ f.write " - #{@app_path}-bundle-sync:/bundle:rw\n"
143
+ f.write " - ./keys:/root/.ssh/\n"
144
+ f.write " ports:\n"
145
+ f.write " - \"3000:3000\"\n"
146
+ f.write " environment:\n"
147
+ f.write " REDIS_URL: redis://redis:6379\n" if @config["redis"]
148
+ f.write " DB_USER: #{@config["db_user"]}\n"
149
+ f.write " DB_PASSWORD: #{@config["db_password"]}\n"
150
+ f.write " links:\n"
151
+ f.write " - db\n"
152
+ f.write " - redis\n" if @config["redis"]
153
+ f.write " tty: true\n"
154
+ f.write " stdin_open: true\n"
155
+
156
+ if @config["redis"] && @config["sidekiq"]
157
+ f.write "\n job:\n"
158
+ f.write " build: .\n"
159
+ f.write " command: bundle exec sidekiq -C config/sidekiq.yml\n"
160
+ f.write " volumes:\n"
161
+ f.write " - #{@app_path}-web-sync:/app:rw\n"
162
+ f.write " - #{@app_path}-bundle-sync:/bundle:rw\n"
163
+ f.write " - ./keys:/root/.ssh/\n"
164
+ f.write " environment:\n"
165
+ f.write " REDIS_URL: redis://redis:6379\n"
166
+ f.write " links:\n"
167
+ f.write " - db\n"
168
+ f.write " - redis\n"
169
+ end
170
+
171
+ f.write "\nvolumes:\n"
172
+ f.write " #{@app_path}-web-sync:\n"
173
+ f.write " external: true\n"
174
+ f.write " #{@app_path}-bundle-sync:\n"
175
+ f.write " external: true\n"
176
+ end
177
+ end
178
+
179
+ def create_docker_sync
180
+ File.open("docker-sync.yml", 'w') do |f|
181
+ f.write "version: '2'\n"
182
+ f.write "syncs:\n"
183
+ f.write " #{@app_path}-web-sync:\n"
184
+ f.write " src: './#{@app_path}'\n"
185
+ f.write " dest: '/app'\n"
186
+ f.write " #{@app_path}-bundle-sync:\n"
187
+ f.write " src: './bundle'\n"
188
+ f.write " dest: '/bundle'\n"
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,3 @@
1
+ module Dockrails
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dockrails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Montard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docker-sync
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.3
41
+ description: Docker + Rails + Mac + Dev = <3
42
+ email:
43
+ executables:
44
+ - dockrails
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - bin/dockrails
50
+ - lib/dockrails.rb
51
+ - lib/dockrails/generate.rb
52
+ - lib/dockrails/version.rb
53
+ homepage: https://github.com/gmontard/dockrails/
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.0.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.5.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Simple CLI to Generate and Run a Rails environment with Docker!
77
+ test_files: []