Class: Yad::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/yad/core.rb

Class Method Summary collapse

Class Method Details

.build_cleanup_command(max_release_count, releases_directory, all_releases) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/yad/core.rb', line 45

def self.build_cleanup_command(max_release_count, releases_directory, all_releases)
  if all_releases.length <= max_release_count
    "echo keeping all releases"
  else
    releases_to_remove = (all_releases - all_releases.last(max_release_count)).map { |release| File.join(releases_directory, release) }.join(" ")
    "rm -rf #{releases_to_remove}"
  end
end

.build_files_array(delimited_file_names) ⇒ Object



30
31
32
33
34
35
# File 'lib/yad/core.rb', line 30

def self.build_files_array(delimited_file_names)
  return [] unless delimited_file_names
  files = delimited_file_names.split(",").map { |f| f.strip }.flatten
  files = files.reject { |f| File.directory?(f) || File.basename(f)[0] == ?. }
  files
end

.build_remove_directory_command(directory_to_remove) ⇒ Object



26
27
28
# File 'lib/yad/core.rb', line 26

def self.build_remove_directory_command(directory_to_remove)
  "rm -rf #{directory_to_remove}"
end

.build_revision_log_command(timestamp, inline_revision_identifier_command, new_release_directory, deployment_directory) ⇒ Object



41
42
43
# File 'lib/yad/core.rb', line 41

def self.build_revision_log_command(timestamp, inline_revision_identifier_command, new_release_directory, deployment_directory)
  "echo #{timestamp} $USER #{inline_revision_identifier_command} #{File.basename(new_release_directory)} >> #{deployment_directory}/revisions.log"
end

.build_rollback_command(current_release_symlink, previous_release_directory, latest_release_directory) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/yad/core.rb', line 54

def self.build_rollback_command(current_release_symlink, previous_release_directory, latest_release_directory)
  if previous_release_directory.nil? || previous_release_directory == latest_release_directory || latest_release_directory.nil?
    "echo no previous release for rollback"
  else
    "rm -f #{current_release_symlink}; ln -s #{previous_release_directory} #{current_release_symlink} && rm -rf #{latest_release_directory}"
  end
end

.build_setup_command(deployment_directory, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yad/core.rb', line 3

def self.build_setup_command(deployment_directory, options = {})
  default_options = { :umask => '02',
    :shared_subdirectories => [] }
  
  options = default_options.merge(options)
  dirs = [File.join(deployment_directory, "releases"),
          File.join(deployment_directory, "scm"),
          File.join(deployment_directory, "shared"),
          File.join(deployment_directory, "shared", "config")
         ]
  options[:shared_subdirectories].each do |subdirectory|
    dirs << File.join(deployment_directory, "shared", subdirectory) unless subdirectory == 'config'
  end
  "umask #{options[:umask]} && mkdir -p #{dirs.join(' ')}"
end

.build_update_source_code_command(checkout_command, export_command, new_release_directory) ⇒ Object



19
20
21
22
23
24
# File 'lib/yad/core.rb', line 19

def self.build_update_source_code_command(checkout_command, export_command, new_release_directory)
  [checkout_command,
   export_command,
   "chmod -R g+w #{new_release_directory}"
  ].join(" && ")
end


37
38
39
# File 'lib/yad/core.rb', line 37

def self.build_update_symlink_command(current_release_symlink, new_release_directory)
  "rm -f #{current_release_symlink} && ln -s #{new_release_directory} #{current_release_symlink}"
end

.define_tasksObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/yad/core.rb', line 62

def self.define_tasks
  return if @tasks_already_defined
  @tasks_already_defined = true
  namespace :yad do
    
    desc "Prepares one or more servers for deployment"
    remote_task :setup do
      Rake::Task['yad:core:setup_deployment'].invoke
      Rake::Task.invoke_if_defined('yad:framework:setup', :framework)
    end
    
    desc "Copies files to the shared/config directory using FILES=a,b,c (e.g. rake vlad:upload_config_files FILES=config/database.yml,config/maintenance.html)"
    remote_task :upload_config_files do
      files = Yad::Core.build_files_array(ENV["FILES"])
      if files.empty?
        puts("Please specify at least one file to upload (via the FILES environment variable)")
      else
        files.each do |file|
          rsync(file, "#{target_host}:#{File.join(shared_config_path, File.basename(file))}")
        end
        puts("uploaded #{files.inspect} to #{target_host}")
      end
    end
    
    desc "Creates the database for the application"
    remote_task :create_db, :roles => :app do
      Rake::Task.invoke_if_defined('yad:db:create', :db, "Please specify the database delegate via the :db variable")
    end
    
    desc "Updates the application servers to the latest version"
    remote_task :update, :roles => :app do
      Rake::Task.invoke_if_defined('yad:scm:update', :scm)
      Rake::Task.invoke_if_defined('yad:framework:update', :framework)
      Rake::Task['yad:core:update_symlink'].invoke
    end

    desc "Runs migrations for the database for the application"
    remote_task :migrate_db, :roles => :app do
      Rake::Task.invoke_if_defined('yad:db:migrate', :db, "Please specify the database delegate via the :db variable")
    end
    
    desc "Starts the application server"
    remote_task :start_app, :roles => :app do
      Rake::Task.invoke_if_defined('yad:app:start', :app, "Please specify the app delegate via the :app variable")
    end
    
    desc "Starts the web server"
    remote_task :start_web, :roles => :web do
      Rake::Task.invoke_if_defined('yad:web:start', :web, "Please specify the web delegate via the :web variable")
    end
    
    "Stops the web server"
    remote_task :stop_web, :roles => :web do
      Rake::Task.invoke_if_defined('yad:web:stop', :web, "Please specify the web delegate via the :web variable")
    end
    
    desc "Stops the application server"
    remote_task :stop_app, :roles => :app do
      Rake::Task.invoke_if_defined('yad:app:stop', :app, "Please specify the app delegate via the :app variable")
    end
    
    desc "Turns on the maintenance page for the application"
    remote_task :turn_on_maintenance, :roles => :web do
      Rake::Task.invoke_if_defined('yad:maintenance:turn_on', :maintenance, "Please specify the maintenance delegate via the :maintenance variable")
    end
    
    desc "Turns off the maintenance page for the application"
    remote_task :turn_off_maintenance, :roles => :web do
      Rake::Task.invoke_if_defined('yad:maintenance:turn_off', :maintenance, "Please specify the maintenance delegate via the :maintenance variable")
    end
    
    desc "Rolls back to the previous release, but DOES NOT restart the application"
    remote_task :rollback do
      cmd = Yad::Core.build_rollback_command(current_path, previous_release, latest_release)
      if cmd.length == 0
        puts("could not rollback the code because there is no previous release")
      else
        run(cmd)
        puts("rolled back to #{File.basename(previous_release)} on #{target_host}")
      end
    end
    
    desc "Cleans up old releases"
    remote_task :cleanup do
      cmd = Yad::Core.build_cleanup_command(keep_releases, releases_path, releases)
      run(cmd)
      puts("old releases cleaned up on #{target_host}")
    end

    remote_task :invoke do
      # TODO:
    end
    
    namespace :core do

      remote_task :setup_deployment, :roles => :app do
        options = Rake::RemoteTask.get_options_hash(:umask, :shared_subdirectories)
        cmd = Yad::Core.build_setup_command(deploy_to, options)
        run(cmd)
        puts("Yad set up on #{target_host}")
      end

      remote_task :update_symlink, :roles => :app do
        begin
          symlinked = false
          cmd = Yad::Core.build_update_symlink_command(current_path, release_path)
          run(cmd)
          puts("'current' symlink updated on #{target_host}")
          symlinked = true
          scm_value = Rake::RemoteTask.fetch(:scm, false)
          if scm_value
            scm_class = eval("Yad::Scm::#{scm_value.to_s.classify}")
            options = Rake::RemoteTask.get_options_hash(:revision)
            inline_command = scm_class.build_inline_revision_identifier_command(scm_path, options)
          else
            inline_command = 'none'
          end
          cmd = Yad::Core.build_revision_log_command(Time.now.utc.strftime("%Y%m%d%H%M.%S"), inline_command, release_path, deploy_to)
          run(cmd)
        rescue => e
          if releases.length > 1 && symlinked then
            cmd = Yad::Core.build_update_symlink_command(current_path, previous_release)
            run(cmd)
          end
          cmd = Yad::Core.build_remove_directory_command(release_path)
          run(cmd)
          raise e
        end
      end
      
    end

  end
end