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
16
# 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"
  @git_repo = "[email protected]:#{name}"
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#git_repoObject (readonly)

Returns the value of attribute git_repo.



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

def git_repo
  @git_repo
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#current(*args) ⇒ Object



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

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

#db_createObject



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

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



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

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.



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

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.



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
71
# File 'lib/application.rb', line 46

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



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

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

#refObject



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

def ref
  options[:ref] || target_ref
end

#shared(*args) ⇒ Object



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

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

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

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



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

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

#target_refObject



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

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

#updateObject



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

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



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

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



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

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)


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

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