Class: Conjure::Service::RailsCodebase

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/service/rails_codebase.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, github_url, branch, app_name, rails_environment) ⇒ RailsCodebase

Returns a new instance of RailsCodebase.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/conjure/service/rails_codebase.rb', line 4

def initialize(host, github_url, branch, app_name, rails_environment)
  @github_url = github_url
  @branch = branch
  @app_name = app_name
  @rails_environment = rails_environment
  @host = host
  github_private_key = Conjure.config.file_contents(:private_key_file).gsub("\n", "\\n")
  github_public_key = Conjure.config.file_contents(:public_key_file).gsub("\n", "\\n")
  @image = @host.images.create(
    label: "codebase",
    base_image: "ubuntu",
    setup_commands: [
      "apt-get install -y git",
      "mkdir -p /root/.ssh; echo '#{github_private_key}' > /root/.ssh/id_rsa",
      "mkdir -p /root/.ssh; echo '#{github_public_key}' > /root/.ssh/id_rsa.pub",
      "chmod -R go-rwx /root/.ssh",
      "echo 'Host github.com\\n\\tStrictHostKeyChecking no\\n' >> /root/.ssh/config",
    ],
    host_volumes: {"/rails_app" => "/#{app_name}"},
  )
end

Instance Method Details

#checkout_codeObject



49
50
51
52
# File 'lib/conjure/service/rails_codebase.rb', line 49

def checkout_code
  Conjure.log "[  repo] Checking out code from git"
  @image.command "git clone -b #{@branch} #{@github_url}"
end

#code_checked_outObject



45
46
47
# File 'lib/conjure/service/rails_codebase.rb', line 45

def code_checked_out
  @image.command("[ -d #{@app_name}/.git ] && echo yes; true").strip == "yes"
end

#configure_databaseObject



59
60
61
62
# File 'lib/conjure/service/rails_codebase.rb', line 59

def configure_database
  Conjure.log "[  repo] Generating database.yml"
  @image.command "echo '#{database_yml}' >/#{@app_name}/config/database.yml"
end

#configure_logsObject



64
65
66
67
68
# File 'lib/conjure/service/rails_codebase.rb', line 64

def configure_logs
  Conjure.log "[  repo] Configuring application logger"
  setup = 'Rails.logger = Logger.new "#{Rails.root}/log/#{Rails.env}.log"'
  @image.command "echo '#{setup}' >/#{@app_name}/config/initializers/z_conjure_logger.rb"
end

#databaseObject



79
80
81
# File 'lib/conjure/service/rails_codebase.rb', line 79

def database
  @database ||= Database.new :docker_host => @host, :codebase => self
end

#database_nameObject



70
71
72
# File 'lib/conjure/service/rails_codebase.rb', line 70

def database_name
  "#{@app_name}_#{@rails_environment}"
end

#database_ymlObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/conjure/service/rails_codebase.rb', line 26

def database_yml
  {
    @rails_environment => {
      "adapter" => database.adapter_name,
      "database" => database.name,
      "encoding" => "utf8",
      "host" => database.ip_address,
      "username" => "root",
      "template" => "template0",
    }
  }.to_yaml
end

#fetch_code_updatesObject



54
55
56
57
# File 'lib/conjure/service/rails_codebase.rb', line 54

def fetch_code_updates
  Conjure.log "[  repo] Fetching code updates from git"
  @image.command "cd #{@app_name}; git reset --hard; git checkout #{@branch}; git pull"
end

#gem_namesObject



74
75
76
77
# File 'lib/conjure/service/rails_codebase.rb', line 74

def gem_names
  gemfile = @image.command "cat #{@app_name}/Gemfile"
  gemfile.scan(/gem ['"]([^'"]+)['"]/).flatten
end

#installObject



39
40
41
42
43
# File 'lib/conjure/service/rails_codebase.rb', line 39

def install
  code_checked_out ? fetch_code_updates : checkout_code
  configure_database
  configure_logs
end