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, #node

Instance Method Summary collapse

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods included from Mixin::FromFile

#class_from_file, #from_file

Methods inherited from Chef::Provider

#action_nothing, build_from_file

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Constructor Details

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Deploy

Returns a new instance of Deploy.



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

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)
  
  # NOTE: workaround for CHEF-577
  @definitions ||= Hash.new
  @collection = Chef::ResourceCollection.new
  
  @scm_provider = @new_resource.scm_provider.new(@node, @new_resource)
  
  # @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

#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



62
63
64
65
66
67
68
# File 'lib/chef/provider/deploy.rb', line 62

def action_deploy
  if all_releases.include?(release_path)
    Chef::Log.info("Already deployed app at #{release_path}, skipping. Use action :force_deploy to force.")
  else
    deploy
  end
end

#action_force_deployObject



70
71
72
73
74
75
76
# File 'lib/chef/provider/deploy.rb', line 70

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

#action_rollbackObject

Raises:

  • (RuntimeError)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/provider/deploy.rb', line 78

def action_rollback
  @release_path = all_releases[-2]
  raise RuntimeError, "There is no release to rollback to!" unless @release_path
  release_to_nuke = all_releases.last
  Chef::Log.info "rolling back to previous release: #{release_path}"
  symlink
  Chef::Log.info "removing last release: #{release_to_nuke}"
  FileUtils.rm_rf release_to_nuke
  Chef::Log.info "restarting with previous release"
  restart
end

#all_releasesObject



169
170
171
# File 'lib/chef/provider/deploy.rb', line 169

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

#callback(what, callback_code = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/provider/deploy.rb', line 107

def callback(what, callback_code=nil)
  @collection = Chef::ResourceCollection.new
  case callback_code
  when Proc
    Chef::Log.info "Running callback #{what} code block"
    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



161
162
163
164
165
166
167
# File 'lib/chef/provider/deploy.rb', line 161

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

#copy_cached_repoObject



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

def copy_cached_repo
  Chef::Log.info "copying the cached checkout to #{release_path}"
  FileUtils.mkdir_p(@new_resource.deploy_to + "/releases")
  FileUtils.cp_r("#{@new_resource.destination}.", release_path, :preserve => true)
  release_created(release_path)
end


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

def create_dirs_before_symlink
end

#deployObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/provider/deploy.rb', line 90

def deploy
  Chef::Log.info "deploying branch: #{@new_resource.branch}"
  enforce_ownership
  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!
end

#enforce_ownershipObject



185
186
187
188
# File 'lib/chef/provider/deploy.rb', line 185

def enforce_ownership
  Chef::Log.info "ensuring proper ownership"
  FileUtils.chown_R(@new_resource.user, @new_resource.group, @new_resource.deploy_to)
end


190
191
192
193
194
195
# File 'lib/chef/provider/deploy.rb', line 190

def link_current_release_to_production
  Chef::Log.info "Linking release #{release_path} into production at #{@new_resource.current_path}"
  FileUtils.rm_f(@new_resource.current_path)
  FileUtils.ln_sf(release_path, @new_resource.current_path)
  enforce_ownership
end


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

def link_shared_db_config_to_current_release
  links_info = @new_resource.symlink_before_migrate.map { |src, dst| "#{src} => #{dst}" }.join(", ")
  Chef::Log.info "Making pre-migration symlinks: #{links_info}"
  @new_resource.symlink_before_migrate.each do |src, dest|
    FileUtils.ln_sf(@new_resource.shared_path + "/#{src}", release_path + "/#{dest}")
  end
end


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

def link_tempfiles_to_current_release
  dirs_info = @new_resource.create_dirs_before_symlink.join(",")
  Chef::Log.info("creating directories before symlink: #{dirs_info}")
  @new_resource.create_dirs_before_symlink.each { |dir| FileUtils.mkdir_p(release_path + "/#{dir}") }
  
  links_info = @new_resource.symlinks.map { |src, dst| "#{src} => #{dst}" }.join(", ")
  Chef::Log.info("Linking shared paths into current release: #{links_info}")
  @new_resource.symlinks.each do |src, dest|
    FileUtils.ln_sf(@new_resource.shared_path + "/#{src}",  release_path + "/#{dest}")
  end
  link_shared_db_config_to_current_release
  enforce_ownership
end

#load_current_resourceObject



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

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

#migrateObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/provider/deploy.rb', line 126

def migrate
  if @new_resource.migrate
    enforce_ownership
    link_shared_db_config_to_current_release
    
    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  "Migrating: running #{@new_resource.migration_command} as #{@new_resource.user} " +
                    "with environment #{env_info}"
    run_command(run_options(:command => @new_resource.migration_command, :cwd=>release_path))
  end
end

#purge_tempfiles_from_current_releaseObject



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

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

#restartObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/provider/deploy.rb', line 149

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

#run(command, &block) ⇒ Object



56
57
58
59
60
# File 'lib/chef/provider/deploy.rb', line 56

def run(command, &block)
  exec = execute(command, &block)
  exec.user(@new_resource.user)
  exec
end

#sudo(command, &block) ⇒ Object



52
53
54
# File 'lib/chef/provider/deploy.rb', line 52

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


142
143
144
145
146
147
# File 'lib/chef/provider/deploy.rb', line 142

def symlink
  Chef::Log.info "Symlinking"
  purge_tempfiles_from_current_release
  link_tempfiles_to_current_release
  link_current_release_to_production
end

#update_cached_repoObject



173
174
175
176
# File 'lib/chef/provider/deploy.rb', line 173

def update_cached_repo
  Chef::Log.info "updating the cached checkout"
  @scm_provider.action_sync
end