Class: Rebi::ZipHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

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

Constructor Details

#initialize(env_conf, opts = {}) ⇒ ZipHelper

Returns a new instance of ZipHelper.



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

def initialize env_conf,opts={}
  @env_conf = env_conf
  @opts = opts
  # raise Rebi::Error::NoGit.new("Not a git repository") unless git?
end

Instance Attribute Details

#env_confObject (readonly)

Returns the value of attribute env_conf.



6
7
8
# File 'lib/rebi/zip_helper.rb', line 6

def env_conf
  @env_conf
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/rebi/zip_helper.rb', line 6

def opts
  @opts
end

Instance Method Details

#ebignoreObject



126
127
128
# File 'lib/rebi/zip_helper.rb', line 126

def ebignore
  env_conf.ebignore
end

#ebignore?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rebi/zip_helper.rb', line 122

def ebignore?
  File.exist?(ebignore)
end

#ebignore_specObject



112
113
114
115
116
117
118
119
120
# File 'lib/rebi/zip_helper.rb', line 112

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

#genObject

Create zip archivement



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
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rebi/zip_helper.rb', line 57

def gen
  log("Creating zip archivement", env_conf.name)
  start = Time.now
  ebextensions = env_conf.ebextensions
  tmp_file = raw_zip_archive
  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 == ".ebextensions"
      Dir.glob("#{ex_folder}/*.config*") do |fname|
        next unless File.file?(fname)

        basename = File.basename(fname)
        source_file = fname

        if fname.match(/\.erb$/)
          next unless y = YAML::load(ErbHelper.new(File.read(fname), env_conf).result)
          basename = basename.gsub(/\.erb$/,'')
          source_file = "#{tmp_folder}/#{basename}"
          File.open(source_file, 'w') do |f|
            f.write y.to_yaml
          end
        end

        target = ".ebextensions/#{basename}"

        z.remove target if z.find_entry target
        z.remove fname if z.find_entry fname
        z.add target, source_file
      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)


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

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

#ls_filesObject



23
24
25
# File 'lib/rebi/zip_helper.rb', line 23

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

#messageObject



52
53
54
# File 'lib/rebi/zip_helper.rb', line 52

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_archiveObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rebi/zip_helper.rb', line 27

def raw_zip_archive
  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 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 = staged? ? `git write-tree`.chomp : "HEAD"
    system "git archive --format=zip #{commit_id} > #{tmp_file.path}"
  end
  return tmp_file
end

#staged?Boolean

Returns:

  • (Boolean)


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

def staged?
  opts[:staged]
end

#version_labelObject



48
49
50
# File 'lib/rebi/zip_helper.rb', line 48

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