Module: Cloner::DockerCompose

Extended by:
ActiveSupport::Concern
Included in:
Internal
Defined in:
lib/cloner/docker_compose.rb

Instance Method Summary collapse

Instance Method Details

#docker_compose_exec(service, command, opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/cloner/docker_compose.rb', line 55

def docker_compose_exec(service, command, opts = {})
  compose_file = opts[:compose_file] || docker_compose_file
  compose_path = opts[:compose_path] || docker_compose_path
  no_tty = opts[:no_tty] != false ? "--no-TTY" : ""
  env_vars = opts[:env] || {}
  
  env_str = env_vars.map { |k, v| "--env #{e k}=#{e v}" }.join(" ")
  
  "cd #{e compose_path} && docker compose -f #{e compose_file} exec #{no_tty} #{env_str} #{e service} #{command}"
end

#docker_compose_fileObject



19
20
21
# File 'lib/cloner/docker_compose.rb', line 19

def docker_compose_file
  "compose.yml"
end

#docker_compose_pathObject



43
44
45
# File 'lib/cloner/docker_compose.rb', line 43

def docker_compose_path
  "."
end

#docker_compose_serviceObject



31
32
33
# File 'lib/cloner/docker_compose.rb', line 31

def docker_compose_service
  nil
end

#local_db_configObject

Default local database config for Docker Compose Override this method to customize your database configuration



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cloner/docker_compose.rb', line 213

def local_db_config
  if local_docker_compose?
    # When using Docker Compose, read from .env file
    config = {
      adapter: 'postgresql',
      host: read_local_env('DB_HOST') || 'localhost',
      port: read_local_env('DB_PORT') || '5432',
      database: read_local_env('DB_NAME'),
      username: read_local_env('DB_USER'),
      password: read_local_env('DB_PASSWORD') || ''
    }.stringify_keys
    
    # Validate required fields
    if config['database'].nil? || config['database'].empty?
      puts "Error: DB_NAME not found in local .env file at #{local_docker_compose_path}/.env"
      puts "Available env vars: #{local_env_vars.keys.join(', ')}"
      puts "Local .env content (first 10 lines):"
      puts local_env_content.lines.first(10).join
      exit 1
    end
    
    if config['username'].nil? || config['username'].empty?
      puts "Error: DB_USER not found in local .env file at #{local_docker_compose_path}/.env"
      puts "Available env vars: #{local_env_vars.keys.join(', ')}"
      exit 1
    end
    
    config
  else
    # Fall back to reading from database.yml
    ar_conf
  end
end

#local_docker_compose?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cloner/docker_compose.rb', line 11

def local_docker_compose?
  false
end

#local_docker_compose_exec(service, command, opts = {}) ⇒ Object



66
67
68
69
70
# File 'lib/cloner/docker_compose.rb', line 66

def local_docker_compose_exec(service, command, opts = {})
  opts[:compose_file] ||= local_docker_compose_file
  opts[:compose_path] ||= local_docker_compose_path
  docker_compose_exec(service, command, opts)
end

#local_docker_compose_fileObject



23
24
25
# File 'lib/cloner/docker_compose.rb', line 23

def local_docker_compose_file
  docker_compose_file
end

#local_docker_compose_pathObject



47
48
49
# File 'lib/cloner/docker_compose.rb', line 47

def local_docker_compose_path
  docker_compose_path
end

#local_docker_compose_serviceObject



35
36
37
# File 'lib/cloner/docker_compose.rb', line 35

def local_docker_compose_service
  docker_compose_service
end

#local_env_contentObject

Helper to read and parse local .env file



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cloner/docker_compose.rb', line 163

def local_env_content
  @local_env_content ||= begin
    # Use docker compose path for .env file
    env_path = File.join(local_docker_compose_path, '.env')
    if File.exist?(env_path)
      content = File.read(env_path)
      content
    else
      ""
    end
  end
end

#local_env_varsObject

Parse local .env content into a hash



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cloner/docker_compose.rb', line 177

def local_env_vars
  @local_env_vars ||= begin
    vars = {}
    local_env_content.each_line do |line|
      line = line.strip
      next if line.empty? || line.start_with?('#')
      
      # Handle KEY=VALUE format
      if match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/)
        key = match[1]
        value = match[2]
        
        # Remove surrounding quotes if present
        value = value.gsub(/^["']|["']$/, '') if value
        
        vars[key] = value
      end
    end
    vars
  end
end

#read_local_env(key = nil) ⇒ Object

Helper to read a specific env var from local .env file



200
201
202
203
204
205
206
207
208
209
# File 'lib/cloner/docker_compose.rb', line 200

def read_local_env(key = nil)
  if key.nil?
    # Return all env vars
    local_env_vars
  else
    # Return specific key
    value = local_env_vars[key]
    value
  end
end

#read_remote_env(key = nil) ⇒ Object

Helper to read a specific env var from remote .env file



151
152
153
154
155
156
157
158
159
160
# File 'lib/cloner/docker_compose.rb', line 151

def read_remote_env(key = nil)
  if key.nil?
    # Return all env vars
    remote_env_vars
  else
    # Return specific key
    value = remote_env_vars[key]
    value
  end
end

#remote_db_configObject

Default remote database config for Docker Compose Override this method to customize your database configuration



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/cloner/docker_compose.rb', line 249

def remote_db_config
  if remote_docker_compose?
    # When using Docker Compose, read from .env file
    config = {
      adapter: 'postgresql',
      host: read_remote_env('DB_HOST') || 'db',
      port: read_remote_env('DB_PORT') || '5432',
      database: read_remote_env('DB_NAME'),
      username: read_remote_env('DB_USER'),
      password: read_remote_env('DB_PASSWORD') || ''
    }.stringify_keys
    
    
    # Validate required fields
    if config['database'].nil? || config['database'].empty?
      puts "Error: DB_NAME not found in remote .env file at #{remote_docker_compose_path}/.env"
      puts "Available env vars: #{remote_env_vars.keys.join(', ')}"
      puts "Remote .env content (first 10 lines):"
      puts remote_env_content.lines.first(10).join
      exit 1
    end
    
    if config['username'].nil? || config['username'].empty?
      puts "Error: DB_USER not found in remote .env file at #{remote_docker_compose_path}/.env"
      puts "Available env vars: #{remote_env_vars.keys.join(', ')}"
      exit 1
    end
    
    config
  else
    # Fall back to reading from database.yml
    ar_r_conf
  end
end

#remote_docker_compose?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/cloner/docker_compose.rb', line 15

def remote_docker_compose?
  false
end

#remote_docker_compose_exec(service, command, opts = {}) ⇒ Object



72
73
74
75
76
# File 'lib/cloner/docker_compose.rb', line 72

def remote_docker_compose_exec(service, command, opts = {})
  opts[:compose_file] ||= remote_docker_compose_file
  opts[:compose_path] ||= remote_docker_compose_path
  docker_compose_exec(service, command, opts)
end

#remote_docker_compose_fileObject



27
28
29
# File 'lib/cloner/docker_compose.rb', line 27

def remote_docker_compose_file
  docker_compose_file
end

#remote_docker_compose_pathObject



51
52
53
# File 'lib/cloner/docker_compose.rb', line 51

def remote_docker_compose_path
  remote_app_path
end

#remote_docker_compose_serviceObject



39
40
41
# File 'lib/cloner/docker_compose.rb', line 39

def remote_docker_compose_service
  docker_compose_service
end

#remote_env_contentObject

Helper to read and parse remote .env file



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cloner/docker_compose.rb', line 91

def remote_env_content
  @remote_env_content ||= begin
    content = ""
    do_ssh do |ssh|
      # Use docker compose path, not app path
      env_path = "#{remote_docker_compose_path}/.env"
      # First check if file exists
      check_cmd = "test -f #{e env_path} && echo 'EXISTS' || echo 'NOT_EXISTS'"
      check_ret = ssh_exec!(ssh, check_cmd)
      
      if check_ret && check_ret[0] && check_ret[0].strip == 'NOT_EXISTS'
        puts "ERROR: .env file not found at #{env_path}"
        puts "Try running: ssh #{ssh_user}@#{ssh_host} 'ls -la #{remote_docker_compose_path}/'"
        exit 1
      end
      
      # Then read the file
      read_cmd = "cat #{e env_path} 2>&1"
      ret = ssh_exec!(ssh, read_cmd)
      
      # ssh_exec! returns [stdout, stderr, exit_code, ...]
      content = ret[0] if ret && ret[0]
      
      if content.to_s.strip == 'FILE NOT FOUND'
        puts "ERROR: .env file not found at #{env_path}"
        puts "Try running: ssh #{ssh_user}@#{ssh_host} 'ls -la #{remote_docker_compose_path}/'"
        exit 1
      end
    end
    content || ""
  end
end

#remote_env_varsObject

Parse .env content into a hash



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cloner/docker_compose.rb', line 125

def remote_env_vars
  @remote_env_vars ||= begin
    vars = {}
    content = remote_env_content
    content.each_line.with_index do |line, idx|
      original_line = line
      line = line.strip
      next if line.empty? || line.start_with?('#')
      
      # Handle KEY=VALUE format
      if match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/)
        key = match[1]
        value = match[2]
        
        # Remove surrounding quotes if present
        value = value.gsub(/^["']|["']$/, '') if value
        
        vars[key] = value
      else
      end
    end
    vars
  end
end

#use_docker_compose?Boolean

Docker Compose configuration methods

Returns:

  • (Boolean)


7
8
9
# File 'lib/cloner/docker_compose.rb', line 7

def use_docker_compose?
  false
end

#wrap_command(command, local: true) ⇒ Object

Helper to wrap commands with docker compose when needed



79
80
81
82
83
84
85
86
87
88
# File 'lib/cloner/docker_compose.rb', line 79

def wrap_command(command, local: true)
  if local && local_docker_compose?
    service = local_docker_compose_service
    return local_docker_compose_exec(service, command) if service
  elsif !local && remote_docker_compose?
    service = remote_docker_compose_service
    return remote_docker_compose_exec(service, command) if service
  end
  command
end