Class: Chef::Provider::Deploy

Inherits:
Chef::Provider show all
Includes:
Mixin::Command, Mixin::FromFile
Defined in:
lib/chef/provider/deploy.rb,
lib/chef/provider/deploy/revision.rb,
lib/chef/provider/deploy/timestamped.rb

Direct Known Subclasses

Revision, Timestamped

Defined Under Namespace

Classes: Revision, Timestamped

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Unix

#popen4

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::FromFile

#class_from_file, #from_file

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(new_resource, run_context) ⇒ Deploy

Returns a new instance of Deploy



33
34
35
36
37
38
39
40
41
42
# File 'lib/chef/provider/deploy.rb', line 33

def initialize(new_resource, run_context)
  super(new_resource, run_context)

  @scm_provider = new_resource.scm_provider.new(new_resource, run_context)

  # @configuration is not used by Deploy, it is only for backwards compat with
  # chef-deploy or capistrano hooks that might use it to get environment information
  @configuration = @new_resource.to_hash
  @configuration[:environment] = @configuration[:environment] && @configuration[:environment]["RAILS_ENV"]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#previous_release_pathObject (readonly)

Returns the value of attribute previous_release_path



31
32
33
# File 'lib/chef/provider/deploy.rb', line 31

def previous_release_path
  @previous_release_path
end

#release_pathObject (readonly)

Returns the value of attribute release_path



31
32
33
# File 'lib/chef/provider/deploy.rb', line 31

def release_path
  @release_path
end

#scm_providerObject (readonly)

Returns the value of attribute scm_provider



31
32
33
# File 'lib/chef/provider/deploy.rb', line 31

def scm_provider
  @scm_provider
end

Instance Method Details

#action_deployObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef/provider/deploy.rb', line 61

def action_deploy
  save_release_state

  if deployed?(release_path)
    if current_release?(release_path) 
      Chef::Log.debug("#{@new_resource} is the latest version")
    else
      action_rollback
    end
  else

    with_rollback_on_error do
      deploy
      @new_resource.updated_by_last_action(true)
    end
  end
end

#action_force_deployObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/provider/deploy.rb', line 79

def action_force_deploy
  if deployed?(release_path)
    Chef::Log.info("Already deployed app at #{release_path}, forcing.")
    FileUtils.rm_rf(release_path)
    Chef::Log.info("#{@new_resource} forcing deploy of already deployed app at #{release_path}")
  end

  # Alternatives:
  # * Move release_path directory before deploy and move it back when error occurs
  # * Rollback to previous commit
  # * Do nothing - because deploy is force, it will be retried in short time
  # Because last is simpliest, keep it
  deploy
  @new_resource.updated_by_last_action(true)
end

#action_rollbackObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/provider/deploy.rb', line 95

def action_rollback
  if release_path
    rp_index = all_releases.index(release_path)
    raise RuntimeError, "There is no release to rollback to!" unless rp_index
    rp_index += 1
    releases_to_nuke = all_releases[rp_index..-1]
  else
    @release_path = all_releases[-2]
    raise RuntimeError, "There is no release to rollback to!" unless @release_path
    releases_to_nuke = [ all_releases.last ]
  end

  rollback

  releases_to_nuke.each do |i|
    Chef::Log.info "#{@new_resource} removing release: #{i}"
    FileUtils.rm_rf i
    release_deleted(i)
  end
  @new_resource.updated_by_last_action(true)
end

#all_releasesObject



204
205
206
# File 'lib/chef/provider/deploy.rb', line 204

def all_releases
  Dir.glob(@new_resource.deploy_to + "/releases/*").sort
end

#callback(what, callback_code = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/provider/deploy.rb', line 142

def callback(what, callback_code=nil)
  @collection = Chef::ResourceCollection.new
  case callback_code
  when Proc
    Chef::Log.info "#{@new_resource} running callback #{what}"
    recipe_eval(&callback_code)
  when String
    callback_file = "#{release_path}/#{callback_code}"
    unless ::File.exist?(callback_file)
      raise RuntimeError, "Can't find your callback file #{callback_file}"
    end
    run_callback_from_file(callback_file)
  when nil
    run_callback_from_file("#{release_path}/deploy/#{what}.rb")
  else
    raise RuntimeError, "You gave me a callback I don't know what to do with: #{callback_code.inspect}"
  end
end

#cleanup!Object



196
197
198
199
200
201
202
# File 'lib/chef/provider/deploy.rb', line 196

def cleanup!
  all_releases[0..-6].each do |old_release|
    Chef::Log.info "#{@new_resource} removing old release #{old_release}"
    FileUtils.rm_rf(old_release)
    release_deleted(old_release)
  end
end

#copy_cached_repoObject



226
227
228
229
230
231
# File 'lib/chef/provider/deploy.rb', line 226

def copy_cached_repo
  FileUtils.mkdir_p(@new_resource.deploy_to + "/releases")
  run_command(:command => "cp -RPp #{::File.join(@new_resource.destination, ".")} #{release_path}")
  Chef::Log.info "#{@new_resource} copied the cached checkout to #{release_path}"
  release_created(release_path)
end


287
288
# File 'lib/chef/provider/deploy.rb', line 287

def create_dirs_before_symlink
end

#deployObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/provider/deploy.rb', line 117

def deploy
  enforce_ownership
  verify_directories_exist
  update_cached_repo
  copy_cached_repo
  install_gems
  enforce_ownership
  callback(:before_migrate, @new_resource.before_migrate)
  migrate
  callback(:before_symlink, @new_resource.before_symlink)
  symlink
  callback(:before_restart, @new_resource.before_restart)
  restart
  callback(:after_restart, @new_resource.after_restart)
  cleanup!
  Chef::Log.info "#{@new_resource} deployed to #{@new_resource.deploy_to}"
end

#enforce_ownershipObject



233
234
235
236
237
# File 'lib/chef/provider/deploy.rb', line 233

def enforce_ownership
  FileUtils.chown_R(@new_resource.user, @new_resource.group, @new_resource.deploy_to)
  Chef::Log.info("#{@new_resource} set user to #{@new_resource.user}") if @new_resource.user
  Chef::Log.info("#{@new_resource} set group to #{@new_resource.group}") if @new_resource.group
end


244
245
246
247
248
249
250
251
252
253
# File 'lib/chef/provider/deploy.rb', line 244

def link_current_release_to_production
  FileUtils.rm_f(@new_resource.current_path)
  begin
    FileUtils.ln_sf(release_path, @new_resource.current_path)
  rescue => e
    raise Chef::Exceptions::FileNotFound.new("Cannot symlink current release to production: #{e.message}")
  end
  Chef::Log.info "#{@new_resource} linked release #{release_path} into production at #{@new_resource.current_path}"
  enforce_ownership
end


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/chef/provider/deploy.rb', line 267

def link_tempfiles_to_current_release
  dirs_info = @new_resource.create_dirs_before_symlink.join(",")
  @new_resource.create_dirs_before_symlink.each do |dir| 
    create_dir_unless_exists(release_path + "/#{dir}")
  end
  Chef::Log.info("#{@new_resource} created directories before symlinking #{dirs_info}")

  links_info = @new_resource.symlinks.map { |src, dst| "#{src} => #{dst}" }.join(", ")
  @new_resource.symlinks.each do |src, dest|
    begin
      FileUtils.ln_sf(::File.join(@new_resource.shared_path, src), ::File.join(release_path, dest))
    rescue => e
      raise Chef::Exceptions::FileNotFound.new("Cannot symlink shared data #{::File.join(@new_resource.shared_path, src)} to #{::File.join(release_path, dest)}: #{e.message}")
    end
  end
  Chef::Log.info("#{@new_resource} linked shared paths into current release: #{links_info}")
  run_symlinks_before_migrate
  enforce_ownership
end

#load_current_resourceObject



44
45
46
# File 'lib/chef/provider/deploy.rb', line 44

def load_current_resource
  @release_path = @new_resource.deploy_to + "/releases/#{release_slug}"
end

#migrateObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/chef/provider/deploy.rb', line 161

def migrate
  run_symlinks_before_migrate

  if @new_resource.migrate
    enforce_ownership

    environment = @new_resource.environment
    env_info = environment && environment.map do |key_and_val|
      "#{key_and_val.first}='#{key_and_val.last}'"
    end.join(" ")

    Chef::Log.info "#{@new_resource} migrating #{@new_resource.user} with environment #{env_info}"
    run_command(run_options(:command => @new_resource.migration_command, :cwd=>release_path, :command_log_level => :info))
  end
end

#purge_tempfiles_from_current_releaseObject



290
291
292
293
294
# File 'lib/chef/provider/deploy.rb', line 290

def purge_tempfiles_from_current_release
  log_info = @new_resource.purge_before_symlink.join(", ")
  @new_resource.purge_before_symlink.each { |dir| FileUtils.rm_rf(release_path + "/#{dir}") }
  Chef::Log.info("#{@new_resource} purged directories in checkout #{log_info}")
end

#restartObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/chef/provider/deploy.rb', line 184

def restart
  if restart_cmd = @new_resource.restart_command
    if restart_cmd.kind_of?(Proc)
      Chef::Log.info("#{@new_resource} restarting app with embedded recipe")
      recipe_eval(&restart_cmd)
    else
      Chef::Log.info("#{@new_resource} restarting app")
      run_command(run_options(:command => @new_resource.restart_command, :cwd => @new_resource.current_path))
    end
  end
end

#rollbackObject



135
136
137
138
139
140
# File 'lib/chef/provider/deploy.rb', line 135

def rollback
  Chef::Log.info "#{@new_resource} rolling back to previous release #{release_path}"
  symlink
  Chef::Log.info "#{@new_resource} restarting with previous release"
  restart
end

#run(command, &block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/chef/provider/deploy.rb', line 52

def run(command, &block)
  exec = execute(command, &block)
  exec.user(@new_resource.user) if @new_resource.user
  exec.group(@new_resource.group) if @new_resource.group
  exec.cwd(release_path) unless exec.cwd
  exec.environment(@new_resource.environment) unless exec.environment
  exec
end

#run_scm_syncObject



217
218
219
# File 'lib/chef/provider/deploy.rb', line 217

def run_scm_sync
  @scm_provider.action_sync
end


255
256
257
258
259
260
261
262
263
264
265
# File 'lib/chef/provider/deploy.rb', line 255

def run_symlinks_before_migrate
  links_info = @new_resource.symlink_before_migrate.map { |src, dst| "#{src} => #{dst}" }.join(", ")
  @new_resource.symlink_before_migrate.each do |src, dest|
    begin
      FileUtils.ln_sf(@new_resource.shared_path + "/#{src}", release_path + "/#{dest}")
    rescue => e
      raise Chef::Exceptions::FileNotFound.new("Cannot symlink #{@new_resource.shared_path}/#{src} to #{release_path}/#{dest} before migrate: #{e.message}")
    end
  end
  Chef::Log.info "#{@new_resource} made pre-migration symlinks"
end

#sudo(command, &block) ⇒ Object



48
49
50
# File 'lib/chef/provider/deploy.rb', line 48

def sudo(command,&block)
  execute(command, &block)
end

#svn_force_exportObject



221
222
223
224
# File 'lib/chef/provider/deploy.rb', line 221

def svn_force_export
  Chef::Log.info "#{@new_resource} exporting source repository"
  @scm_provider.action_force_export
end


177
178
179
180
181
182
# File 'lib/chef/provider/deploy.rb', line 177

def symlink
  purge_tempfiles_from_current_release
  link_tempfiles_to_current_release
  link_current_release_to_production
  Chef::Log.info "#{@new_resource} updated symlinks"
end

#update_cached_repoObject



208
209
210
211
212
213
214
215
# File 'lib/chef/provider/deploy.rb', line 208

def update_cached_repo
  @scm_provider.load_current_resource
  if @new_resource.svn_force_export
    svn_force_export
  else
    run_scm_sync
  end
end

#verify_directories_existObject



239
240
241
242
# File 'lib/chef/provider/deploy.rb', line 239

def verify_directories_exist
  create_dir_unless_exists(@new_resource.deploy_to)
  create_dir_unless_exists(@new_resource.shared_path)
end