Class: Rebi::ZipHelper

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/rebi/zip_helper.rb

Constant Summary collapse

EB_IGNORE =
".ebignore"

Instance Method Summary collapse

Methods included from Log

#colorize, #colorize_prefix, #error, #error_label, #h1, #h2, #h3, #h4, #hstatus, #log, #log_label

Constructor Details

#initializeZipHelper

Returns a new instance of ZipHelper.



8
9
10
# File 'lib/rebi/zip_helper.rb', line 8

def initialize
  # raise Rebi::Error::NoGit.new("Not a git repository") unless git?
end

Instance Method Details

#ebignore?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rebi/zip_helper.rb', line 110

def ebignore?
  File.exist?(EB_IGNORE)
end

#ebignore_specObject



100
101
102
103
104
105
106
107
108
# File 'lib/rebi/zip_helper.rb', line 100

def ebignore_spec
  if ebignore?
    path_spec = PathSpec.from_filename(EB_IGNORE)
    path_spec.add(".git")
    return path_spec
  else
    return PathSpec.new(".git")
  end
end

#gen(env_conf, opts = {}) ⇒ Object

Create zip archivement



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
94
95
96
97
98
# File 'lib/rebi/zip_helper.rb', line 51

def gen env_conf,opts={}
  log("Creating zip archivement", env_conf.name)
  start = Time.now
  ebextensions = env_conf.ebextensions
  tmp_file = raw_zip_archive opts
  tmp_folder = Dir.mktmpdir
  Zip::File.open(tmp_file.path) do |z|
    ebextensions.each do |ex_folder|

      z.remove_folder ex_folder unless ex_folder == ".ebextension"
      Dir.glob("#{ex_folder}/*.config") do |fname|
        next unless File.file?(fname)
        next unless y = YAML::load(ErbHelper.new(File.read(fname), env_conf).result)
        basename = File.basename(fname)
        target = ".ebextensions/#{basename}"
        tmp_yaml = "#{tmp_folder}/#{basename}"
        File.open(tmp_yaml, 'w') do |f|
          f.write y.to_yaml
        end
        z.remove target if z.find_entry target
        z.add target, tmp_yaml
      end
    end
    dockerrun_file = env_conf.dockerrun || "Dockerrun.aws.json"

    if File.exists?(dockerrun_file)
      dockerrun = JSON.parse ErbHelper.new(File.read(dockerrun_file), env_conf).result
      tmp_dockerrun = "#{tmp_folder}/Dockerrun.aws.json"
      File.open(tmp_dockerrun, 'w') do |f|
        f.write dockerrun.to_json
      end
      z.remove env_conf.dockerrun if z.find_entry env_conf.dockerrun
      z.remove "Dockerrun.aws.json" if z.find_entry "Dockerrun.aws.json"
      z.add "Dockerrun.aws.json", tmp_dockerrun
    end

  end

  FileUtils.rm_rf tmp_folder


  log("Zip was created in: #{Time.now - start}s", env_conf.name)
  return {
    label: Time.now.strftime("app_#{env_conf.name}_#{version_label}_%Y%m%d_%H%M%S"),
    file: File.open(tmp_file.path),
    message: message,
  }
end

#git?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/rebi/zip_helper.rb', line 12

def git?
  `git status 2>&1`
  $?.success?
end

#ls_filesObject



17
18
19
# File 'lib/rebi/zip_helper.rb', line 17

def ls_files
  `git ls-files 2>&1`.split("\n")
end

#messageObject



46
47
48
# File 'lib/rebi/zip_helper.rb', line 46

def message
  git? ? `git log --oneline -1`.chomp.split(" ")[1..-1].join(" ")[0..190] : "Deploy #{Time.now.strftime("%Y/%m/%d %H:%M")}"
end

#raw_zip_archive(opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rebi/zip_helper.rb', line 21

def raw_zip_archive opts={}
  tmp_file = Tempfile.new("git_archive")
  if !git? || ebignore?
    Zip::File.open(tmp_file.path, Zip::File::CREATE) do |z|
      spec = ebignore_spec
      Dir.glob("**/*").each do |f|
        next if ebignore_spec.match f
        if File.directory?(f)
          z.mkdir f unless z.find_entry f
        else
          z.add f, f
        end
      end
    end
  else
    commit_id = opts[:staged] ? `git write-tree`.chomp : "HEAD"
    system "git archive --format=zip #{commit_id} > #{tmp_file.path}"
  end
  return tmp_file
end

#version_labelObject



42
43
44
# File 'lib/rebi/zip_helper.rb', line 42

def version_label
  git? ? `git describe --always --abbrev=8`.chomp : SecureRandom.hex[0, 8]
end