Class: Revup::DeployTask
- Inherits:
-
Rake::TaskLib
- Object
- Rake::TaskLib
- Revup::DeployTask
- Defined in:
- lib/revup/deploy_task.rb
Constant Summary collapse
- DEFAULT_TO_CLASS_ATTRS =
[:host, :user, :app_dir, :remote_setup, :files_root, :logger, :local_tmp_dir, :remote_tmp_dir]
Instance Attribute Summary collapse
-
#app_dir ⇒ Object
Returns the value of attribute app_dir.
-
#files ⇒ Object
Returns the value of attribute files.
-
#files_root ⇒ Object
Returns the value of attribute files_root.
-
#host ⇒ Object
Returns the value of attribute host.
-
#local_tmp_dir ⇒ Object
Returns the value of attribute local_tmp_dir.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#remote_setup ⇒ Object
Returns the value of attribute remote_setup.
-
#remote_tmp_dir ⇒ Object
Returns the value of attribute remote_tmp_dir.
-
#revision ⇒ Object
Returns the value of attribute revision.
-
#task_name_and_prerequisites ⇒ Object
Returns the value of attribute task_name_and_prerequisites.
-
#user ⇒ Object
Returns the value of attribute user.
-
#versioned_asset_name ⇒ Object
Returns the value of attribute versioned_asset_name.
Instance Method Summary collapse
- #archive_basename ⇒ Object
- #cleanup! ⇒ Object
- #create_archive ⇒ Object
-
#define ⇒ Object
Actual task definition.
- #deploy ⇒ Object
-
#initialize(task_name_and_prerequisites) {|_self| ... } ⇒ DeployTask
constructor
A new instance of DeployTask.
- #local_archive_path ⇒ Object
- #remote_archive_dir ⇒ Object
- #remote_archive_path ⇒ Object
- #upload_and_register ⇒ Object
Constructor Details
#initialize(task_name_and_prerequisites) {|_self| ... } ⇒ DeployTask
Returns a new instance of DeployTask.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/revup/deploy_task.rb', line 41 def initialize(task_name_and_prerequisites) DEFAULT_TO_CLASS_ATTRS.each do |attr| send("#{attr}=", self.class.send(attr)) end @task_name_and_prerequisites = task_name_and_prerequisites @versioned_asset_name = task_name @revision ||= begin result = sh("git rev-parse --short HEAD 2>&1") result unless result.empty? end yield self if block_given? define end |
Instance Attribute Details
#app_dir ⇒ Object
Returns the value of attribute app_dir.
31 32 33 |
# File 'lib/revup/deploy_task.rb', line 31 def app_dir @app_dir end |
#files ⇒ Object
Returns the value of attribute files.
25 26 27 |
# File 'lib/revup/deploy_task.rb', line 25 def files @files end |
#files_root ⇒ Object
Returns the value of attribute files_root.
25 26 27 |
# File 'lib/revup/deploy_task.rb', line 25 def files_root @files_root end |
#host ⇒ Object
Returns the value of attribute host.
27 28 29 |
# File 'lib/revup/deploy_task.rb', line 27 def host @host end |
#local_tmp_dir ⇒ Object
Returns the value of attribute local_tmp_dir.
35 36 37 |
# File 'lib/revup/deploy_task.rb', line 35 def local_tmp_dir @local_tmp_dir end |
#logger ⇒ Object
Returns the value of attribute logger.
39 40 41 |
# File 'lib/revup/deploy_task.rb', line 39 def logger @logger end |
#remote_setup ⇒ Object
Returns the value of attribute remote_setup.
37 38 39 |
# File 'lib/revup/deploy_task.rb', line 37 def remote_setup @remote_setup end |
#remote_tmp_dir ⇒ Object
Returns the value of attribute remote_tmp_dir.
35 36 37 |
# File 'lib/revup/deploy_task.rb', line 35 def remote_tmp_dir @remote_tmp_dir end |
#revision ⇒ Object
Returns the value of attribute revision.
33 34 35 |
# File 'lib/revup/deploy_task.rb', line 33 def revision @revision end |
#task_name_and_prerequisites ⇒ Object
Returns the value of attribute task_name_and_prerequisites.
23 24 25 |
# File 'lib/revup/deploy_task.rb', line 23 def task_name_and_prerequisites @task_name_and_prerequisites end |
#user ⇒ Object
Returns the value of attribute user.
29 30 31 |
# File 'lib/revup/deploy_task.rb', line 29 def user @user end |
#versioned_asset_name ⇒ Object
Returns the value of attribute versioned_asset_name.
21 22 23 |
# File 'lib/revup/deploy_task.rb', line 21 def versioned_asset_name @versioned_asset_name end |
Instance Method Details
#archive_basename ⇒ Object
58 59 60 |
# File 'lib/revup/deploy_task.rb', line 58 def archive_basename "#{versioned_asset_name}-#{revision}" end |
#cleanup! ⇒ Object
122 123 124 125 126 |
# File 'lib/revup/deploy_task.rb', line 122 def cleanup! sh "rm -f '#{local_archive_path}'" ssh "rm -f '#{remote_archive_path}'" ssh "rm -rf '#{remote_archive_dir}'" end |
#create_archive ⇒ Object
99 100 101 102 |
# File 'lib/revup/deploy_task.rb', line 99 def create_archive sh "mkdir -p '#{local_tmp_dir}'" sh "tar -C '#{files_root}' -zcf '#{local_archive_path}' '#{files.sort.join("' '")}'" end |
#define ⇒ Object
Actual task definition
76 77 78 79 80 81 |
# File 'lib/revup/deploy_task.rb', line 76 def define task(task_name_and_prerequisites) do deploy end Rake::Task[task_name].add_description("Deploy build of the `#{versioned_asset_name}' versioned_asset") end |
#deploy ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/revup/deploy_task.rb', line 83 def deploy validate files_root, "Please specify the location of the root of the files that should be deployed." validate files, "Please specify a list of files that should be deployed." validate host, "Please specify the host of the machine to deploy to." validate user, "Please specify the user of the machine to deploy to." validate app_dir, "Please specify the path to the application." validate revision, "Please specify the Git revision hash." create_archive upload_and_register rescue Object => e logger.error("#{e.message}\n\t#{e.backtrace.join("\n\t")}") raise e end |
#local_archive_path ⇒ Object
62 63 64 |
# File 'lib/revup/deploy_task.rb', line 62 def local_archive_path File.join(local_tmp_dir, "#{archive_basename}.tgz") end |
#remote_archive_dir ⇒ Object
70 71 72 |
# File 'lib/revup/deploy_task.rb', line 70 def remote_archive_dir File.join(remote_tmp_dir, archive_basename) end |
#remote_archive_path ⇒ Object
66 67 68 |
# File 'lib/revup/deploy_task.rb', line 66 def remote_archive_path File.join(remote_tmp_dir, "#{archive_basename}.tgz") end |
#upload_and_register ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/revup/deploy_task.rb', line 104 def upload_and_register Net::SSH.start(host, user) do |connection| @ssh = connection ssh "mkdir -p '#{remote_archive_dir}'" # Upload archive logger.info("Transferring `#{local_archive_path}' to `#{host}:#{remote_archive_path}'") @ssh.scp.upload!(local_archive_path, remote_archive_path) # Unpack archive ssh "tar -C '#{remote_archive_dir}' -zxf '#{remote_archive_path}'" # Register revision ssh "cd '#{app_dir}' && /usr/local/rbenv/shims/bundle exec ./script/register_versioned_asset_revision '#{remote_archive_dir}' '#{versioned_asset_name}' '#{revision}'" cleanup! end @ssh = nil end |