-
Notifications
You must be signed in to change notification settings - Fork 0
/
packer.rb
149 lines (125 loc) · 4.3 KB
/
packer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env ruby
require 'yaml'
require 'fileutils'
require 'openssl'
require 'io/console'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: packer.rb command environment [options]"
opts.on("-p:", "--password:", "Password for encryption/decryption") do |v|
options[:password] = v
end
opts.on("-f", "--force-replace", "Force to replace existing files") do |v|
options[:force_replace] = v
end
end.parse!
PACKS_DIR = "#{Dir.pwd}/Packs"
PACKER_TMP_DIR = "#{Dir.pwd}/.packer"
PACKER_FILE = "#{Dir.pwd}/Packerfile"
abort("Packerfile is not found") unless File.exists?(PACKER_FILE)
config = YAML.load_file(PACKER_FILE)
abort("Packerfile is not a yaml") if config.inspect == "false"
config.each { |k, v| v.flatten! }
command, environment = ARGV
COMMANDS = %w(pack unpack)
abort("Pass command (#{COMMANDS.join('|')}) as arg0") unless COMMANDS.include?(command)
abort("Pass environment as arg1") unless environment
abort("Packerfile doesn't know environment: #{environment}") unless config.keys.include?(environment)
files = config[environment].map { |f| File.expand_path(f) }
case command
when 'pack'
missing_files = files.reject { |f| File.exists?(f) }
abort("#{missing_files.count} files are not found:\n#{missing_files.join("\n")}") unless missing_files.empty?
password = options[:password]
unless password
print "Password: "
password = STDIN.gets.chomp
end
tmp_pack_dir = "#{PACKER_TMP_DIR}/#{Time.now.to_i}"
tmp_pack = "#{tmp_pack_dir}/#{environment}"
files.each do |f|
dest = tmp_pack + f.sub(Dir.pwd, '')
FileUtils.mkdir_p(File.dirname(dest))
FileUtils.cp(f, dest)
end
FileUtils.cd("#{tmp_pack}/..") do
system("zip -qr #{environment}.zip *")
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.encrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac(password, 'salt', 2000, cipher.key_len + cipher.iv_len, 'sha256')
cipher.key = key_iv[0, cipher.key_len]
cipher.iv = key_iv[cipher.key_len, cipher.iv_len]
File.open("#{environment}.pack", 'wb') do |output|
File.open("#{environment}.zip", 'rb') do |input|
buff = buff || ""
while input.read(4096, buff)
output << cipher.update(buff)
end
output << cipher.final
end
end
end
FileUtils.mkdir_p(PACKS_DIR)
FileUtils.mv("#{tmp_pack}.pack", "#{PACKS_DIR}")
FileUtils.rm_rf(tmp_pack_dir)
when 'unpack'
pack = "#{PACKS_DIR}/#{environment}.pack"
abort("File is not found:\n#{pack}") unless File.exists?(pack)
password = options[:password]
unless password
print "Password: "
password = STDIN.noecho(&:gets).chomp
print "\n\n"
end
tmp_pack_dir = "#{PACKER_TMP_DIR}/#{Time.now.to_i}"
tmp_pack = "#{tmp_pack_dir}/#{environment}"
FileUtils.mkdir_p(tmp_pack_dir)
FileUtils.cp(pack, tmp_pack_dir)
FileUtils.cd(tmp_pack_dir) do
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.decrypt
key_iv = OpenSSL::PKCS5.pbkdf2_hmac(password, 'salt', 2000, cipher.key_len + cipher.iv_len, 'sha256')
cipher.key = key_iv[0, cipher.key_len]
cipher.iv = key_iv[cipher.key_len, cipher.iv_len]
File.open("#{environment}.zip", 'wb') do |output|
File.open("#{environment}.pack", 'rb') do |input|
buff = buff || ""
while input.read(4096, buff)
output << cipher.update(buff)
end
begin
output << cipher.final
rescue OpenSSL::Cipher::CipherError => e
abort("Failed with error: #{e.message}")
end
end
end
system("unzip -q #{environment}.zip")
end
files = Dir["#{tmp_pack}/**/*"].reject { |f| File.directory?(f) }
files_to = files.map { |f| Dir.pwd + f.sub(tmp_pack, '') }
unless options[:force_replace]
replacing_files = files_to.select { |f| File.exists?(f) }
unless replacing_files.empty?
print "#{replacing_files.count} files will be replaced:\n#{replacing_files.join("\n")}\n\n"
print "Replace files? [y/n]: "
loop do
answer = STDIN.noecho(&:gets).chomp
if %w(n no).include?(answer)
print "n\n"
exit 1
end
if %w(y yes).include?(answer)
print "y\n"
break
end
end
end
end
files.zip(files_to).each do |from, to|
FileUtils.mkdir_p(File.dirname(to))
FileUtils.cp(from, to)
end
FileUtils.rm_rf(tmp_pack_dir)
end