Class: Revup::DeployTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/revup/deploy_task.rb

Constant Summary collapse

DEFAULT_TO_CLASS_ATTRS =
[:host, :app_dir, :remote_setup, :files_root, :logger, :local_tmp_dir, :remote_tmp_dir]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DeployTask.

Yields:

  • (_self)

Yield Parameters:



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

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

  @component_name_and_prerequisites = component_name_and_prerequisites
  @component_name = @component_name_and_prerequisites.is_a?(Hash) ?
    @component_name_and_prerequisites.keys.first : @component_name_and_prerequisites

  yield self if block_given?
  define
end

Instance Attribute Details

#app_dirObject

Returns the value of attribute app_dir.



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

def app_dir
  @app_dir
end

#component_nameObject

Returns the value of attribute component_name.



21
22
23
# File 'lib/revup/deploy_task.rb', line 21

def component_name
  @component_name
end

#component_name_and_prerequisitesObject

Returns the value of attribute component_name_and_prerequisites.



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

def component_name_and_prerequisites
  @component_name_and_prerequisites
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.



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

def local_tmp_dir
  @local_tmp_dir
end

#loggerObject

Returns the value of attribute logger.



37
38
39
# File 'lib/revup/deploy_task.rb', line 37

def logger
  @logger
end

#remote_setupObject

Returns the value of attribute remote_setup.



35
36
37
# File 'lib/revup/deploy_task.rb', line 35

def remote_setup
  @remote_setup
end

#remote_tmp_dirObject

Returns the value of attribute remote_tmp_dir.



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

def remote_tmp_dir
  @remote_tmp_dir
end

#revisionObject

Returns the value of attribute revision.



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

def revision
  @revision
end

Instance Method Details

#archive_basenameObject



59
60
61
# File 'lib/revup/deploy_task.rb', line 59

def archive_basename
  "#{@component_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_archiveObject



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

#defineObject

Actual task definition



77
78
79
80
81
82
# File 'lib/revup/deploy_task.rb', line 77

def define
  desc "Deploy build of the `#{@component_name}' component"
  task(@component_name_and_prerequisites) do
    deploy
  end
end

#deployObject



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

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 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



63
64
65
# File 'lib/revup/deploy_task.rb', line 63

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

#remote_archive_dirObject



71
72
73
# File 'lib/revup/deploy_task.rb', line 71

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

#remote_archive_pathObject



67
68
69
# File 'lib/revup/deploy_task.rb', line 67

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

#upload_and_registerObject



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, ENV['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_component_revision '#{remote_archive_dir}' '#{component_name}' '#{revision}'"

    cleanup!
  end
  @ssh = nil
end