Module: Comodule::Deployment::Helper::Uploader

Defined in:
lib/comodule/deployment/helper/uploader.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



3
4
5
6
7
# File 'lib/comodule/deployment/helper/uploader.rb', line 3

def self.included(receiver)
  unless receiver < ::Comodule::Deployment::Helper::Aws::S3
    receiver.send :include, ::Comodule::Deployment::Helper::Aws::S3
  end
end

Instance Method Details

#archive_filenameObject



93
94
95
# File 'lib/comodule/deployment/helper/uploader.rb', line 93

def archive_filename
  "#{project_name}-#{config.platform_name}.tar.gz"
end

#archive_pathObject



97
98
99
# File 'lib/comodule/deployment/helper/uploader.rb', line 97

def archive_path
  File.join tmp_archives_dir, archive_filename
end

#archive_projectObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/comodule/deployment/helper/uploader.rb', line 126

def archive_project
  unless File.directory?(git_dir)
    puts ".git not found"
    raise
  end

  rm_rf tmp_project_dir

  `git clone #{git_dir} #{tmp_project_dir}`
  rm_rf File.join(tmp_project_dir, '.git') if config.upload_project == 'without_git'

  File.open(File.join(tmp_project_dir, 'project_stamp.txt'), 'w') do |file|
    file.write project_stamp
  end

  gz_path = archive_path
  `( cd #{tmp_project_dir} ; tar cfz #{gz_path} . )`

  gz_path
end

#archive_s3_pathObject



101
102
103
# File 'lib/comodule/deployment/helper/uploader.rb', line 101

def archive_s3_path
  archive_path.sub %r|^#{tmp_dir}/|, ''
end

#archive_s3_public_urlObject



105
106
107
# File 'lib/comodule/deployment/helper/uploader.rb', line 105

def archive_s3_public_url
  s3.bucket.objects[archive_s3_path].public_url secure: true
end

#download_projectObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/comodule/deployment/helper/uploader.rb', line 109

def download_project
  local_dir = File.join(test_dir, 'download_archive')
  re_dir local_dir

  s3_path = archive_s3_path
  filename = File.basename(s3_path)
  local_path = File.join(local_dir, filename)

  File.open(local_path, 'wb') do |file|
    file.write s3.bucket.objects[s3_path].read
  end

  `( cd #{local_dir} ; tar xfz #{filename} )`

  local_path
end

#download_secret_filesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/comodule/deployment/helper/uploader.rb', line 55

def download_secret_files
  return unless config.s3_bucket

  unless deployment?
    re_dir test_download_secret_files_dir
  end

  s3.bucket.objects.with_prefix("#{name}/").each do |s3_obj|
    local_path =
      if deployment?
        s3.cloud_to_local s3_obj.key
      else
        s3_obj.key.sub(%r|^#{name}/|, test_download_secret_files_dir)
      end

    be_dir File.dirname(local_path)

    File.open(local_path, 'w') do |file|
      file.write s3_obj.read
    end
  end
end

#download_secret_files_testObject



49
50
51
52
53
# File 'lib/comodule/deployment/helper/uploader.rb', line 49

def download_secret_files_test
  test_mode do
    download_secret_files
  end
end

#project_stampObject



83
84
85
86
87
# File 'lib/comodule/deployment/helper/uploader.rb', line 83

def project_stamp
  branch = `echo $(cd #{project_root} ; git rev-parse --abbrev-ref HEAD)`.strip
  commit = `echo $(cd #{project_root} ; git rev-parse HEAD)`.strip
  "branch: #{branch}\n" + "commit: #{commit}\n"
end

#secret_filesObject



9
10
11
12
13
14
15
16
# File 'lib/comodule/deployment/helper/uploader.rb', line 9

def secret_files
  (
    Dir.glob(File.join(common_secret_config_dir, '**', '*')) |
    Dir.glob(File.join(secret_config_dir, '**', '*')) <<
    File.join(common_secret_config_path) <<
    File.join(secret_config_path)
  ).find_all { |path| File.file?(path) }
end

#test_download_secret_files_dirObject



45
46
47
# File 'lib/comodule/deployment/helper/uploader.rb', line 45

def test_download_secret_files_dir
  @test_download_secret_files_dir ||= File.join(test_dir, 'download_secret_files')
end

#test_upload_secret_files_dirObject



18
19
20
# File 'lib/comodule/deployment/helper/uploader.rb', line 18

def test_upload_secret_files_dir
  @test_upload_secret_files_dir ||= File.join(test_dir, 'upload_secret_files')
end

#tmp_archives_dirObject



89
90
91
# File 'lib/comodule/deployment/helper/uploader.rb', line 89

def tmp_archives_dir
  @tmp_archives_dir ||= be_dir(File.join(tmp_dir, 'archives'))
end

#upload_archive(path = archive_path) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/comodule/deployment/helper/uploader.rb', line 147

def upload_archive(path = archive_path)
  s3_path = archive_s3_path
  obj = s3.bucket.objects[s3_path]
  obj.write(
    Pathname.new(path),
    server_side_encryption: :aes256
  )
end

#upload_projectObject



79
80
81
# File 'lib/comodule/deployment/helper/uploader.rb', line 79

def upload_project
  upload_archive archive_project
end

#upload_secret_filesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/comodule/deployment/helper/uploader.rb', line 28

def upload_secret_files
  return unless config.upload_secret_files

  re_dir test_upload_secret_files_dir

  secret_files.each do |path|
    s3_path = s3.local_to_cloud(path)

    dir = File.dirname(File.join(test_upload_secret_files_dir, s3_path))
    be_dir dir
    FileUtils.cp path, dir

    obj = s3.bucket.objects[s3_path]
    obj.write Pathname.new(path), server_side_encryption: :aes256
  end
end

#upload_secret_files_testObject



22
23
24
25
26
# File 'lib/comodule/deployment/helper/uploader.rb', line 22

def upload_secret_files_test
  test_mode do
    upload_secret_files
  end
end