dockerize_rails 1.0.3.beta.1

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.
@@ -0,0 +1,48 @@
1
+ module DockerizeRails
2
+ module Constants
3
+ SHELL_SCRIPT_FILE_NAME = 'dockerw'.freeze
4
+
5
+ DOCKERIZE_RAILS_CONFIG_FILE_NAME = '.dockerize.yml'.freeze
6
+ DOCKER_COMPOSE_FILE_NAME = 'docker-compose.yml'.freeze
7
+ DOCKERIGNORE_FILE_NAME = '.dockerignore'.freeze
8
+
9
+ RAILS_DIRECTORY_NAME = 'rails'.freeze
10
+ MYSQL_DIRECTORY_NAME = 'mysql'.freeze
11
+ PG_DIRECTORY_NAME = 'postgresql'.freeze
12
+ CONFIG_DIRECTORY_NAME = '.dockerized'.freeze
13
+ DATA_DIRECTORY_NAME = 'data_dir'.freeze
14
+ SQL_DIRECTORY_NAME = 'sql'.freeze
15
+
16
+ DATABASE_HOST_LINKED = 'linked'.freeze
17
+ DATABASE_HOST_REMOTE = 'remote'.freeze
18
+
19
+ COMMANDS = {
20
+ configure: {
21
+ aliases: %I[configure c rc cr],
22
+ help: "Generates '#{DOCKERIZE_RAILS_CONFIG_FILE_NAME}'".freeze,
23
+ params: {
24
+ :'--skip-desc' => 'generates shorter config file, skipping all descriptions'.freeze
25
+ }
26
+ },
27
+ dockerize: {
28
+ aliases: %I[dockerize dc d],
29
+ help: 'Generates docker config files'.freeze
30
+ },
31
+ docker_info: {
32
+ aliases: %I[docker_info di],
33
+ help: 'Shows Docker information'.freeze
34
+ },
35
+ undockerize: {
36
+ aliases: %I[undockerize ud du u dd],
37
+ help: 'Removes docker configurations'.freeze,
38
+ params: {
39
+ :'--purge' => "also removes #{Constants::DOCKERIZE_RAILS_CONFIG_FILE_NAME}".freeze
40
+ }
41
+ },
42
+ help: {
43
+ aliases: %I[help h],
44
+ help: 'prints this message'.freeze
45
+ }
46
+ }.freeze
47
+ end
48
+ end
@@ -0,0 +1,9 @@
1
+ module DockerizeRails
2
+ module DockerHelper
3
+ require 'docker'
4
+
5
+ def self.version
6
+ Docker.version
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,149 @@
1
+ module DockerizeRails
2
+ class DRConfig
3
+ ATTRIBUTES = %i[
4
+ application_name ruby_version application_env application_port postgres_version mysql_version
5
+ database_host_type database_host_name mysql_root_pass database_user_name database_user_pass
6
+ databases
7
+ ].freeze
8
+
9
+ @application_name = File.basename PATHS.current
10
+
11
+ @ruby_version = 'latest'
12
+ @application_env = 'development'
13
+ @application_port = '5000'
14
+
15
+ @postgres_version = 'alpine'
16
+ @mysql_version = 'latest'
17
+
18
+ @database_host_type = Constants::DATABASE_HOST_LINKED
19
+ @database_host_name = 'databasehost'
20
+ @database_user_name = 'user'
21
+ @database_user_pass = 'pass'
22
+
23
+ @mysql_root_pass = 'root'
24
+
25
+ @databases = {}
26
+
27
+ def self.load_dockerize_rails_config
28
+ dockerize_rails_config_file = File.join(PATHS.current, Constants::DOCKERIZE_RAILS_CONFIG_FILE_NAME)
29
+ if File.exist? dockerize_rails_config_file
30
+ dockerize_rails_config = YAML.load_file(dockerize_rails_config_file)
31
+ ATTRIBUTES.each do |attr|
32
+ attr_conf = dockerize_rails_config[attr.to_s]
33
+ send("#{attr}=", attr_conf) unless attr_conf.to_s.empty?
34
+ end
35
+ else
36
+ puts "\nDockerizeRails config file not generated...".yellow
37
+ puts "Run 'bundle exec dock configure' to generate configuration file.\n".yellow
38
+ end
39
+ end
40
+
41
+ def self.remote_database?
42
+ database_host_type == Constants::DATABASE_HOST_REMOTE
43
+ end
44
+
45
+ def self.linked_database?
46
+ database_host_type == Constants::DATABASE_HOST_LINKED
47
+ end
48
+
49
+ # rubocop:disable Metrics/AbcSize
50
+ def self.to_yaml_str
51
+ "---
52
+ # Set application name
53
+ #
54
+ # Default is Rails Application Directory Name
55
+ application_name: #{application_name}
56
+
57
+ # Set docker container's rails environment to production, staging, or development
58
+ # Make sure application is properly configured to run in 'application_env' (#{application_env})
59
+ #
60
+ # Default is #{application_env}
61
+ application_env: #{application_env}
62
+
63
+ # Set docker container's rails port to access from local machine
64
+ # Make sure it doesn't conflict with any of the ports active in host machine
65
+ #
66
+ # Default #{application_port}
67
+ application_port: #{application_port}
68
+
69
+ # Set ruby version
70
+ # Visit: https://hub.docker.com/_/ruby/ for list of available versions
71
+ #
72
+ # Default is #{ruby_version}
73
+ ruby_version: #{ruby_version}
74
+
75
+ # Set database host type
76
+ #
77
+ # Available values are '#{Constants::DATABASE_HOST_LINKED}', '#{Constants::DATABASE_HOST_REMOTE}'
78
+ # Default #{database_host_type}
79
+ database_host_type: #{database_host_type}
80
+
81
+ # Hostname to connect to database
82
+ # Ignored if 'database_host_type' is '#{Constants::DATABASE_HOST_LINKED}'
83
+ database_host_name: #{database_host_name}
84
+
85
+ # Set root password for docker database container
86
+ # it doesn't make any changes in your computer's database
87
+ # It will be used only for MySQL database
88
+ #
89
+ # Default #{mysql_root_pass}
90
+ mysql_root_pass: #{mysql_root_pass}
91
+
92
+ # Set postgres database version if application uses postgres
93
+ # Visit: https://hub.docker.com/_/postgres/ for list of available versions
94
+ #
95
+ # Default is #{postgres_version}
96
+ postgres_version: #{postgres_version}
97
+
98
+ # Set mysql database version if application uses mysql
99
+ # Visit: https://hub.docker.com/_/mysql/ for list of available versions
100
+ #
101
+ # Default is #{mysql_version}
102
+ mysql_version: #{mysql_version}
103
+
104
+ # Set database properties
105
+
106
+ # Default database username for container's internal use
107
+ # It has no impact on host machine's database
108
+ # Default #{database_user_name}
109
+ database_user_name: #{database_user_name}
110
+
111
+ # Default database password for container's internal use
112
+ # It has no impact on host machine's database
113
+ # Default #{database_user_pass}
114
+ database_user_pass: #{database_user_pass}
115
+ "
116
+ end
117
+
118
+ def self.to_yaml
119
+ "---
120
+ application_name: #{application_name}
121
+ application_env: #{application_env}
122
+ application_port: #{application_port}
123
+ ruby_version: #{ruby_version}
124
+ database_host_type: #{database_host_type}
125
+ database_host_name: #{database_host_name}
126
+ mysql_root_pass: #{mysql_root_pass}
127
+ postgres_version: #{postgres_version}
128
+ mysql_version: #{mysql_version}
129
+ database_user_name: #{database_user_name}
130
+ database_user_pass: #{database_user_pass}
131
+ "
132
+ end
133
+ # rubocop:enable Metrics/AbcSize
134
+
135
+ def self.to_hash
136
+ Hash[ATTRIBUTES.map do |accessor|
137
+ [accessor, send(accessor.to_s)]
138
+ end]
139
+ end
140
+
141
+ class << self
142
+ attr_accessor(*ATTRIBUTES)
143
+ def database_host_name
144
+ return 'databasehost' if database_host_type == Constants::DATABASE_HOST_LINKED
145
+ @database_host_name
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,54 @@
1
+ module DockerizeRails
2
+ class DRNameSpace
3
+ require 'ostruct'
4
+
5
+ @namespace = OpenStruct.new
6
+
7
+ attr_reader :namespace
8
+
9
+ def self.load
10
+ DRConfig.load_dockerize_rails_config
11
+ load_from_app_config
12
+ load_from_dockerize_rails_config
13
+ end
14
+
15
+ def self.load_from_app_config
16
+ app_c = ConfigLoader.app_config
17
+ databases = {}
18
+ app_c.keys.each do |env|
19
+ adapter = app_c[env]['adapter']
20
+ databases[env] =
21
+ if adapter.start_with?('mysql')
22
+ 'mysql'
23
+ elsif adapter.start_with?('postgresql')
24
+ 'postgresql'
25
+ elsif adapter.start_with?('sqlite')
26
+ 'sqlite'
27
+ end
28
+ end
29
+ @namespace.databases = databases
30
+ DRConfig.databases = databases
31
+ end
32
+
33
+ def self.load_from_dockerize_rails_config
34
+ DRConfig.to_hash.map do |key, value|
35
+ @namespace.send("#{key}=", value)
36
+ end
37
+ end
38
+
39
+ def self.add_hash(hash)
40
+ hash.map do |key, value|
41
+ @namespace.send("#{attr}=", rc[key] || value)
42
+ end
43
+ end
44
+
45
+ def self.eval_i
46
+ @namespace.instance_eval { binding }
47
+ end
48
+
49
+ class << self
50
+ private :load_from_app_config
51
+ private :load_from_dockerize_rails_config
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,62 @@
1
+ module DockerizeRails
2
+ module Helpers
3
+ require 'ostruct'
4
+
5
+ def self.processed_commands
6
+ Hash[Constants::COMMANDS.keys.map do |key|
7
+ [key, Constants::COMMANDS[key][:aliases].map(&:to_s)]
8
+ end]
9
+ end
10
+
11
+ def self.parse_opts
12
+ [ARGV[0].to_s, args: ARGV[1..ARGV.size].to_a]
13
+ end
14
+
15
+ def self.ensure_rails_root
16
+ unless PATHS.rails_root?
17
+ puts "\n'#{`pwd`.strip}' is not a rails application root.\n".red
18
+ exit(1)
19
+ end
20
+ 0
21
+ end
22
+
23
+ def self.params_help(command)
24
+ if command.key? :params
25
+ [command[:params].keys.map do |param|
26
+ "\n #{('[' + param.to_s + ']').ljust(15, ' ')} -- #{command[:params][param]}"
27
+ end,
28
+ "\n"].join
29
+ else
30
+ ''
31
+ end
32
+ end
33
+
34
+ # rubocop:disable Metrics/MethodLength
35
+ # rubocop:disable Metrics/AbcSize
36
+ def self.help
37
+ ['
38
+ Usage: rocker <command>
39
+ or: bundle exec dock <command>
40
+
41
+ commands:
42
+ ',
43
+ Constants::COMMANDS.keys.map do |key|
44
+ command = Constants::COMMANDS[key]
45
+ " #{command[:aliases].map(&:to_s).join(', ').ljust(30, ' ')}" \
46
+ " - #{command[:help]}" + params_help(command)
47
+ end,
48
+ '
49
+ '].join("\n")
50
+ end
51
+ # rubocop:enable Metrics/MethodLength
52
+ # rubocop:enable Metrics/MethodLength
53
+
54
+ def self.print_formatted_info(name, value)
55
+ print name.ljust(15, ' ').yellow, value.blue
56
+ end
57
+
58
+ class << self
59
+ private :params_help
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,51 @@
1
+ module DockerizeRails
2
+ module PATHS
3
+ def self.gem_root
4
+ File.expand_path '../..', File.dirname(__FILE__)
5
+ end
6
+
7
+ def self.current
8
+ Dir.pwd
9
+ end
10
+
11
+ def self.rails_root?
12
+ File.exist? File.join(current, 'bin', 'rails')
13
+ end
14
+
15
+ def self.resources(name = '')
16
+ File.join(gem_root, 'resources', name)
17
+ end
18
+
19
+ def self.config_directory
20
+ File.join(current, Constants::CONFIG_DIRECTORY_NAME)
21
+ end
22
+
23
+ def self.rails_directory
24
+ File.join(config_directory, Constants::RAILS_DIRECTORY_NAME)
25
+ end
26
+
27
+ def self.mysql_directory
28
+ File.join(config_directory, Constants::MYSQL_DIRECTORY_NAME)
29
+ end
30
+
31
+ def self.postgresql_directory
32
+ File.join(config_directory, Constants::PG_DIRECTORY_NAME)
33
+ end
34
+
35
+ def self.data_directory(db_dir_name)
36
+ File.join(config_directory, db_dir_name, Constants::DATA_DIRECTORY_NAME)
37
+ end
38
+
39
+ def self.sql_directory(db_dir_name)
40
+ File.join(config_directory, db_dir_name, Constants::SQL_DIRECTORY_NAME)
41
+ end
42
+
43
+ def self.relative(base, target)
44
+ Pathname.new(target).relative_path_from(Pathname.new(base)).to_s
45
+ end
46
+
47
+ def self.relative_from_current(target)
48
+ relative(current, target)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ module DockerizeRails
2
+ module Templates
3
+ ROOT_TEMPLATES = %W[
4
+ #{Constants::DOCKERIGNORE_FILE_NAME}
5
+ #{Constants::DOCKER_COMPOSE_FILE_NAME}
6
+ #{Constants::SHELL_SCRIPT_FILE_NAME}
7
+ ].freeze
8
+
9
+ RAILS_TEMPLATES = %w[Dockerfile entry-point.sh secrets.yml].freeze
10
+ MYSQL_TEMPLATES = %W[Dockerfile #{Constants::SQL_DIRECTORY_NAME}/initdb-mysql.sql].freeze
11
+ POSTGRES_TEMPLATES = %w[Dockerfile].freeze
12
+
13
+ ROOT_DIRECTORIES = %W[#{Constants::CONFIG_DIRECTORY_NAME}].freeze
14
+ RAILS_DIRECTORIES = %W[#{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::RAILS_DIRECTORY_NAME}].freeze
15
+ MYSQL_DIRECTORIES = %W[
16
+ #{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::MYSQL_DIRECTORY_NAME}
17
+ #{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::MYSQL_DIRECTORY_NAME}/#{Constants::SQL_DIRECTORY_NAME}
18
+ #{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::MYSQL_DIRECTORY_NAME}/#{Constants::DATA_DIRECTORY_NAME}
19
+ ].freeze
20
+ PG_DIRECTORIES = %W[
21
+ #{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::PG_DIRECTORY_NAME}
22
+ #{Constants::CONFIG_DIRECTORY_NAME}/#{Constants::PG_DIRECTORY_NAME}/#{Constants::DATA_DIRECTORY_NAME}
23
+ ].freeze
24
+
25
+ EXECUTABLES = %W[#{Constants::SHELL_SCRIPT_FILE_NAME}].freeze
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module DockerizeRails
2
+ VERSION = '1.0.3.beta.1'.freeze
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'dockerize_rails/version'
2
+ require 'dockerize_rails/constants'
3
+ require 'dockerize_rails/templates'
4
+ require 'dockerize_rails/paths'
5
+ require 'dockerize_rails/helpers'
6
+ require 'dockerize_rails/dr_config'
7
+ require 'dockerize_rails/config_loader'
8
+ require 'dockerize_rails/dr_name_space'
9
+ require 'dockerize_rails/config_generator'
10
+ require 'dockerize_rails/docker_helper'
11
+ require 'dockerize_rails/command_line_methods'
12
+ require 'dockerize_rails/command_line'
13
+
14
+ module DockerizeRails
15
+ private_constant :PATHS
16
+ private_constant :Helpers
17
+ private_constant :DRNameSpace
18
+ private_constant :ConfigLoader
19
+ private_constant :DRConfig
20
+ private_constant :ConfigGenerator
21
+ private_constant :DockerHelper
22
+ private_constant :Constants
23
+ private_constant :Templates
24
+ private_constant :CommandLineMethods
25
+ end
26
+
27
+ class DockerizeRailsCLI
28
+ extend DockerizeRails::CommandLine
29
+ end
@@ -0,0 +1,25 @@
1
+ .idea/*
2
+ .bundle
3
+ test
4
+ vendor/bundle
5
+ vendor/ruby
6
+ log/*
7
+ tmp/*
8
+ !/log/.keep
9
+ !/tmp/.keep
10
+ .byebug_history
11
+ config/database.yml
12
+ .ruby-gemset
13
+ .ruby-version
14
+ public/uploads
15
+ public/assets
16
+
17
+ <%= Constants::DOCKERIZE_RAILS_CONFIG_FILE_NAME %>
18
+ <%= Constants::DOCKER_COMPOSE_FILE_NAME %>
19
+ <%= Constants::DOCKERIGNORE_FILE_NAME %>
20
+ <%= Constants::SHELL_SCRIPT_FILE_NAME %>
21
+ <% if DRConfig.linked_database? %>
22
+ <% if databases.values.include?('mysql') || databases.values.include?('postgresql') %>
23
+ <%= "#{Constants::CONFIG_DIRECTORY_NAME}/#{databases[application_env]}/#{Constants::DATA_DIRECTORY_NAME}/*" %>
24
+ <% end %>
25
+ <% end %>
@@ -0,0 +1,47 @@
1
+ version: '3'
2
+
3
+ services:
4
+ rails:
5
+ build:
6
+ context: .
7
+ dockerfile: ./<%= "#{Constants::CONFIG_DIRECTORY_NAME}/rails/Dockerfile" %>
8
+ expose:
9
+ - '<%= application_port %>'
10
+ ports:
11
+ - '<%= "#{application_port}:#{application_port}" %>'
12
+ <% if DRConfig.linked_database? %>
13
+ <% if databases.values.include?('mysql') || databases.values.include?('postgresql') %>
14
+ links:
15
+ - '<%= databases[application_env] %>:<%= database_host_name %>'
16
+ depends_on:
17
+ - <%= databases[application_env] %>
18
+ <% end %>
19
+ <% end %>
20
+
21
+ <% if DRConfig.linked_database? %>
22
+ <% if databases.values.include?('mysql') || databases.values.include?('postgresql') %>
23
+ <%= databases[application_env] %>:
24
+ build:
25
+ context: .
26
+ dockerfile: ./<%= "#{Constants::CONFIG_DIRECTORY_NAME}/#{databases[application_env]}/Dockerfile" %>
27
+ volumes:
28
+ - './<%= "#{Constants::CONFIG_DIRECTORY_NAME}/#{databases[application_env]}/#{Constants::DATA_DIRECTORY_NAME}" %>:/var/lib/<%= databases[application_env] %>:rw'
29
+ ports:
30
+ <% if databases[application_env] == 'postgresql' %>
31
+ - '5400:5432'
32
+ <% elsif databases[application_env] == 'mysql' %>
33
+ - '3300:3306'
34
+ <% end %>
35
+ environment:
36
+ <% if databases[application_env] == 'postgresql' %>
37
+ - POSTGRES_USER=<%= database_user_name %>
38
+ - POSTGRES_PASSWORD=<%= database_user_pass %>
39
+ <% elsif databases[application_env] == 'mysql' %>
40
+ - MYSQL_ROOT_PASSWORD=<%= mysql_root_pass %>
41
+ <% if database_user_name != 'root' %>
42
+ - MYSQL_USER=<%= database_user_name %>
43
+ - MYSQL_PASSWORD=<%= database_user_pass %>
44
+ <% end %>
45
+ <% end %>
46
+ <% end %>
47
+ <% end %>