Class: Capistrano::SCM::Archive

Inherits:
Plugin
  • Object
show all
Defined in:
lib/capistrano-scm-local_copy.rb

Overview

SCM archive

Instance Method Summary collapse

Instance Method Details

#define_tasksObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/capistrano-scm-local_copy.rb', line 16

def define_tasks
  namespace :archive do
    archive_plugin = self
    task :create_release do
      archive_name = fetch(:archive_name)
      on release_roles :all do
        execute :mkdir, '-p', release_path
        temp_file = capture(:mktemp)
        upload!(archive_name, temp_file)
        execute :mv, '-fv', temp_file, archive_name
        execute :unzip, '-q -d', release_path, archive_name
        execute :rm, '-fv', archive_name, temp_file
      end
    end

    desc 'Create archive'
    task :create_archive do
      archive_name = fetch(:archive_name)
      puts "Create archive #{archive_name}"
      include_dir = fetch(:include_dir)
      excludes = fetch(:excludes) + [archive_name]
      run_locally do
        archive_objects = FileList[include_dir]
        excludes.each do |file|
          archive_objects = archive_objects.exclude(file)
        end

        Zip::File.open(archive_name, Zip::File::CREATE) do |zip_file|
          archive_objects.each do |object|
            if File.directory?(object)
              Dir.glob("#{object}/**/*.*").each do |child_file|
                zip_file.add(child_file, File.join('./', child_file))
              end
            else
              zip_file.add(object, File.join('./', object))
            end
          end
        end
      end
    end

    task :set_current_revision do
      set :current_revision, archive_plugin.md5(fetch(:archive_name))
    end

    Rake::Task['deploy:log_revision'].clear_actions
    desc 'log revision'
    task :log_revision do
      on release_roles :all do
        execute :echo,
                '"from local files,',
                "(archive md5: #{fetch(:current_revision)})",
                "deployed as release #{fetch(:release_timestamp)}",
                "by #{local_user}\"",
                '>>',
                revision_log

      end
    end

    task :clean do
      archive_name = fetch(:archive_name)
      puts "Clean: remove #{archive_name}"
      FileUtils.rm(archive_name, force: true)
    end

    task :clean_all do
      puts 'Clean deploy_*.zip archives'
      Dir.glob('./deploy_*.zip').each { |file| FileUtils.rm(file, force: true) }
    end

    before :create_archive, :clean_all
    before :clean, :set_current_revision
    after :create_release, :clean
    before :create_release, :create_archive

  end
end

#md5(file) ⇒ Object



95
96
97
# File 'lib/capistrano-scm-local_copy.rb', line 95

def md5(file)
  Digest::MD5.hexdigest File.open(file, 'r').read
end

#random_string(length = 40) ⇒ Object



99
100
101
# File 'lib/capistrano-scm-local_copy.rb', line 99

def random_string(length = 40)
  (0...length).map { rand(97..122).chr }.join
end

#register_hooksObject



103
104
105
106
107
# File 'lib/capistrano-scm-local_copy.rb', line 103

def register_hooks
  after 'deploy:new_release_path', 'archive:create_release'
  before 'deploy:set_current_revision', 'archive:set_current_revision'
  before 'deploy:log_revision', 'archive:log_revision'
end

#set_defaultsObject



9
10
11
12
13
14
# File 'lib/capistrano-scm-local_copy.rb', line 9

def set_defaults
  set_if_empty :archive_name, 'deploy_' + random_string(10) + '.zip'
  set_if_empty :include_dir, '*'
  set_if_empty :tar_roles, :all
  set_if_empty :excludes, ''
end