Class: Application

Inherits:
Object
  • Object
show all
Extended by:
Delegation
Defined in:
lib/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

delegate

Constructor Details

#initialize(name, configuration, options = {}) ⇒ Application

Returns a new instance of Application.



6
7
8
9
10
11
12
13
14
15
# File 'lib/application.rb', line 6

def initialize(name, configuration, options={})
  @runner = configuration.runner
  @logger = configuration.logger
  @configuration = configuration
  @options = options
  
  @name = name
  @base_path = configuration.app_base_path(name)
  @git_repo = "[email protected]:mobino/#{name}.git"
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



21
22
23
# File 'lib/application.rb', line 21

def base_path
  @base_path
end

#configurationObject (readonly)

Returns the value of attribute configuration.



20
21
22
# File 'lib/application.rb', line 20

def configuration
  @configuration
end

#git_repoObject (readonly)

Returns the value of attribute git_repo.



22
23
24
# File 'lib/application.rb', line 22

def git_repo
  @git_repo
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/application.rb', line 18

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/application.rb', line 17

def options
  @options
end

Instance Method Details

#current(*args) ⇒ Object



150
151
152
# File 'lib/application.rb', line 150

def current *args
  path 'current', *args
end

#db_createObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/application.rb', line 134

def db_create
  info "Attempting database creation & seeding."
  
  # Try to update the database
  shell "bundle exec rake db:create"
  shell 'bundle exec rake db:seed || true'

  info "Done (db creation & seed)."
rescue Runner::CommandFailed
  warn "Could not migrate the database schema."
end

#db_migrateObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/application.rb', line 124

def db_migrate
  info "Attempting database migration."
  
  # Try to update the database
  shell "bundle exec rake db:migrate"
  
  info "Done (db migration)."
rescue Runner::CommandFailed
  warn "Could not migrate the database schema."
end

#fetch_remoteObject

Fetches the remote refs for the current application. This will not change the currently deployed branch, just makes sure that the repository is up to date.



76
77
78
# File 'lib/application.rb', line 76

def fetch_remote
  shell "git fetch origin"
end

#initial_checkoutObject

Assuming that nothing is there except the application directory (below /srv) usually, this will do the initial checkout.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/application.rb', line 45

def initial_checkout
  # Prepare app environment, if needed. 
  unless current.directory? && shared.directory?
    FileUtils.mkdir_p current
    FileUtils.chown_R 'app', 'app', current

    FileUtils.mkdir_p shared('tmp', 'pids')
    FileUtils.mkdir_p shared('log')
    FileUtils.chown_R 'app', 'app', shared
  end
  
  return false if current('.git').directory?
  
  panic "No checkout in place and no git reference to update to given. Use '--ref'. " \
    unless ref
      
  info "Performing a complete initial installation."

  shell "git clone #{git_repo} ."
  shell "git checkout -B deployed #{ref}"

  File.write path('.ref'), ref
  
  info "Done. (initial installation)"
  return true
end

#path(*args) ⇒ Object



146
147
148
# File 'lib/application.rb', line 146

def path *args
  base_path.join(*args)
end

#refObject



157
158
159
# File 'lib/application.rb', line 157

def ref
  options[:ref] || target_ref
end

#shared(*args) ⇒ Object



153
154
155
# File 'lib/application.rb', line 153

def shared *args
  path 'shared', *args
end

#shell(cmd, opts = {}) ⇒ Object

Runs a command in a subshell in the #current directory. As user ‘app’.



163
164
165
# File 'lib/application.rb', line 163

def shell cmd, opts={}
  @runner.shell_as 'app', cmd, {cwd: current}.merge(opts)
end

#target_refObject



167
168
169
170
171
172
# File 'lib/application.rb', line 167

def target_ref
  ref_path = path('.ref')
  if ref_path.file?
    return File.read(ref_path).strip
  end
end

#updateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/application.rb', line 24

def update
  # initial_checkout returns true if it performs work.
  nothing_there = initial_checkout

  # Save this flag now, since updating the code will modify it. 
  fetch_remote
  update_requested = update_requested?
  
  if nothing_there || update_requested
    update_application
  end
  
  update_configuration
  
  db_create if nothing_there
  db_migrate if update_requested
end

#update_applicationObject



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

def update_application
  info "Performing an app update."
  
  # Write down the update intention (so we can repeat this easily)
  File.write path('.ref'), ref
  
  # Then try to update
  shell "git reset --hard #{ref}"
  
  # Install gems
  shell "bundle install --deployment \
    --without test spec development cucumber mac jruby"
    
  # Create a few directories that the app also wants. 
  %w(tmp log).each do |dir_name|
    begin
      FileUtils.ln_sf shared(dir_name), current
    rescue Errno::EEXIST
      warn "Target directory (#{dir_name}) already exists."
    end
  end
  
  info "Done (app update)."
end

#update_configurationObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/application.rb', line 110

def update_configuration
  info "Updating configuration files (symlinks)."
  
  # Link the configuration files from app base. (base -> current('config'))
  %w(*.yml *.rb).each do |glob|
    Dir[path(glob)].each do |override_file|
      FileUtils.ln_sf override_file, current('config')
    end
  end
  
  # Make sure that whatever owners the files had, they now belong to 'app'.
  FileUtils.chown_R 'app', 'app', current('config')
end

#update_requested?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/application.rb', line 80

def update_requested?
  shell("git diff --shortstat #{ref} deployed") != "" ||
    options[:force]
end