Class: Revup::DeployTask

Inherits:
Rake::TaskLib
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(versioned_asset_name_and_prerequisites) {|_self| ... } ⇒ DeployTask

Returns a new instance of DeployTask.

Yields:

  • (_self)

Yield Parameters:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/revup/deploy_task.rb', line 41

def initialize(versioned_asset_name_and_prerequisites)
  DEFAULT_TO_CLASS_ATTRS.each do |attr|
    send("#{attr}=", self.class.send(attr))
  end

  @versioned_asset_name_and_prerequisites = versioned_asset_name_and_prerequisites
  @versioned_asset_name = @versioned_asset_name_and_prerequisites.is_a?(Hash) ?
    @versioned_asset_name_and_prerequisites.keys.first : @versioned_asset_name_and_prerequisites

  yield self if block_given?
  define
end

Instance Attribute Details

#app_dirObject

Returns the value of attribute app_dir.



31
32
33
# File 'lib/revup/deploy_task.rb', line 31

def app_dir
  @app_dir
end

#filesObject

Returns the value of attribute files.



25
26
27
# File 'lib/revup/deploy_task.rb', line 25

def files
  @files
end

#files_rootObject

Returns the value of attribute files_root.



25
26
27
# File 'lib/revup/deploy_task.rb', line 25

def files_root
  @files_root
end

#hostObject

Returns the value of attribute host.



27
28
29
# File 'lib/revup/deploy_task.rb', line 27

def host
  @host
end

#local_tmp_dirObject

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

#loggerObject

Returns the value of attribute logger.



39
40
41
# File 'lib/revup/deploy_task.rb', line 39

def logger
  @logger
end

#remote_setupObject

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_dirObject

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

#revisionObject

Returns the value of attribute revision.



33
34
35
# File 'lib/revup/deploy_task.rb', line 33

def revision
  @revision
end

#userObject

Returns the value of attribute user.



29
30
31
# File 'lib/revup/deploy_task.rb', line 29

def user
  @user
end

#versioned_asset_nameObject

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

#versioned_asset_name_and_prerequisitesObject

Returns the value of attribute versioned_asset_name_and_prerequisites.



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

def versioned_asset_name_and_prerequisites
  @versioned_asset_name_and_prerequisites
end

Instance Method Details

#archive_basenameObject



61
62
63
# File 'lib/revup/deploy_task.rb', line 61

def archive_basename
  "#{@versioned_asset_name}-#{@revision}"
end

#cleanup!Object



125
126
127
128
129
# File 'lib/revup/deploy_task.rb', line 125

def cleanup!
  sh  "rm -f '#{local_archive_path}'"
  ssh "rm -f '#{remote_archive_path}'"
  ssh "rm -rf '#{remote_archive_dir}'"
end

#create_archiveObject



102
103
104
105
# File 'lib/revup/deploy_task.rb', line 102

def create_archive
  sh "mkdir -p '#{@local_tmp_dir}'"
  sh "tar -C '#{@files_root}' -zcf '#{local_archive_path}' '#{@files.sort.join("' '")}'"
end

#defineObject

Actual task definition



79
80
81
82
83
84
# File 'lib/revup/deploy_task.rb', line 79

def define
  desc "Deploy build of the `#{@versioned_asset_name}' versioned_asset"
  task(@versioned_asset_name_and_prerequisites) do
    deploy
  end
end

#deployObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/revup/deploy_task.rb', line 86

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_pathObject



65
66
67
# File 'lib/revup/deploy_task.rb', line 65

def local_archive_path
  File.join(@local_tmp_dir, "#{archive_basename}.tgz")
end

#remote_archive_dirObject



73
74
75
# File 'lib/revup/deploy_task.rb', line 73

def remote_archive_dir
  File.join(@remote_tmp_dir, archive_basename)
end

#remote_archive_pathObject



69
70
71
# File 'lib/revup/deploy_task.rb', line 69

def remote_archive_path
  File.join(@remote_tmp_dir, "#{archive_basename}.tgz")
end

#upload_and_registerObject



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

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}' && bundle exec ./script/register_versioned_asset_revision '#{remote_archive_dir}' '#{versioned_asset_name}' '#{revision}'"

    cleanup!
  end
  @ssh = nil
end