Class: Wellcar::Commands::Dock

Inherits:
Base
  • Object
show all
Defined in:
lib/wellcar/commands/dock.rb

Instance Attribute Summary

Attributes inherited from Base

#app_name, #domain_name, #github_account, #ruby_version, #use_bundler_2

Instance Method Summary collapse

Methods inherited from Base

#build_development_environment, #clean_docker, #core_docker_templates, #create_directories, #create_wellcar_folder, #write_files

Constructor Details

#initialize(github_account, domain_name) ⇒ Dock

Returns a new instance of Dock.



4
5
6
7
# File 'lib/wellcar/commands/dock.rb', line 4

def initialize(, domain_name)
  self. = 
  self.domain_name = domain_name
end

Instance Method Details

#call(options, args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wellcar/commands/dock.rb', line 84

def call(options, args)
  create_wellcar_folder
  determine_variables
  prepare_templates
  check_existing_files options[:"force-new"]
  create_directories
  write_files
  update_database_config
  clean_docker
  # This assumes bin/bundle is available, which will not be the case.
  # The gem set up done in this call should be part of a Dockerfile, but needs
  # a script to manage some conditional logic.
  # tidy_gemfile
  build_development_environment

  puts <<-DONE
    Your Rails app has been dockerised!

    You can use wellcar to interact with the Docker containers and the Rails application as you develop your app.

    Run `wellcar` for help with commands.
    DONE
end

#check_existing_files(force_new) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wellcar/commands/dock.rb', line 51

def check_existing_files(force_new)
  files_exist = docker_files.select {|file| file.exist? }.any?
  if force_new && files_exist
    puts "--force-new set. Overwriting **all** existing docker files."

    if Dir.exist? ".env"
      label = Time.new.strftime("%Y%m%d%H%M%S")
      puts ".env will be backed up to .wellcar/env-#{label}"
      destination = ".wellcar/env-#{label}"
      FileUtils.copy_entry ".env", destination
      exit unless Dir.exist? destination
    end
  elsif files_exist
    puts "Found existing docker files. Please check your app source tree before trying to dockerise this app with wellcar again."
    exit 3
  end
end

#determine_variablesObject



37
38
39
40
41
# File 'lib/wellcar/commands/dock.rb', line 37

def determine_variables
  self.app_name = interpret_app_name
  self.ruby_version = interpret_ruby_version
  self.use_bundler_2 = interpret_bundler_2
end

#docker_filesObject



47
48
49
# File 'lib/wellcar/commands/dock.rb', line 47

def docker_files
  @files ||= core_docker_templates.push(Wellcar::Templates::DockerCompose.new(app_name, ))
end

#interpret_app_nameObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wellcar/commands/dock.rb', line 9

def interpret_app_name
  app_name = File.read("./config/application.rb")
    .split("\n")
    .select {|line| line =~ /^module / }
    .first
    &.split
    &.last
    &.downcase

  return app_name unless app_name.nil?

  puts "Cannot determine app name from config/application.rb. wellcar expects to find one module in this file named for the application."
  exit 5
end

#interpret_bundler_2Object



30
31
32
33
34
35
# File 'lib/wellcar/commands/dock.rb', line 30

def interpret_bundler_2
  b2 = File.file?("Gemfile.lock") &&
    File.read("Gemfile.lock").split("BUNDLED WITH")&.last.strip >= "2.0.0"
  puts "Install bundler 2? #{b2}"
  b2
end

#interpret_ruby_versionObject



24
25
26
27
28
# File 'lib/wellcar/commands/dock.rb', line 24

def interpret_ruby_version
  File.file?(".ruby_version") ?
    File.read(".ruby_version").split.first :
    "2.6.6"
end

#prepare_templatesObject



43
44
45
# File 'lib/wellcar/commands/dock.rb', line 43

def prepare_templates
  docker_files
end

#tidy_gemfileObject

This does not currently work, as it is called before bundle is available. I’m debating how to include this “gem tidying” aspect of adding Docker to an existing project.



79
80
81
82
# File 'lib/wellcar/commands/dock.rb', line 79

def tidy_gemfile
  %w(sqlite3 figaro dotenv).each {|gem| system "docker-compose run --rm bin/bundle remove #{gem}" }
  system "docker-compose run --rm bin/bundle add pg" unless system("docker-compose run --rm bin/bundle info pg")
end

#update_database_configObject



69
70
71
72
73
74
75
# File 'lib/wellcar/commands/dock.rb', line 69

def update_database_config
  Wellcar::Templates::DatabaseYaml.new(app_name).update
  puts <<-DB
     ... updated development settings for database.yml.
    Now using dockerised PostgreSQL server. Previous config saved as config/database.pre_docker.yml
    DB
end