-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
159 lines (134 loc) · 4.47 KB
/
Rakefile
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
150
151
152
153
154
155
156
157
158
159
#!/env ruby
require 'rake'
require 'rake/clean'
require 'sys/filesystem'
require 'json'
require 'mkmf'
require_relative 'myfun'
NEEDED_FREE_SPACE = 25600
PACKER_TEMPLATE = File.join WORKSPACE, "blackarch-template.json"
$PYTHON = nil
$PACKER = nil
$VAGRANT = nil
class VagrantCLI
def initialize(profile_name='core')
@profile_name = profile_name
end
def cmd(command)
system({'BLACKARCH_PROFILE' => @profile_name}, "#{$VAGRANT} #{command}") || abort("error: #{command}")
end
def up()
cmd 'up'
end
def destroy()
cmd 'destroy -f'
end
def package(target)
cmd "package --output #{target}"
end
def script(name, sudo=true)
sudo_path = sudo ? '/usr/bin/sudo' : ''
cmd("ssh --command='#{sudo_path} /bin/bash /vagrant/scripts/#{name}'")
end
end
namespace :check do
desc "Check needed free space for building"
task :free_space do
stat = Sys::Filesystem.stat(WORKSPACE)
mb_available = stat.block_size * stat.blocks_available / 1024 / 1024
puts "Free space in directory #{WORKSPACE} is #{mb_available/1024}Gb"
abort("For building need ~25G free space, but avalible only ~#{mb_available/1024}Gb") if mb_available < NEEDED_FREE_SPACE
end
desc "Check present python3 interpreter"
task :python do
$PYTHON = find_executable 'python3'
abort("Can't find python3 executable") if $PYTHON.nil?
version = %x{ #{$PYTHON} -c "import sys; print(sys.version.split()[0])" }
abort("Can't detect version of #{$PYTHON}") if version.strip.empty?
puts " #{$PYTHON}\n Python #{version}"
end
desc "Check present packer-io"
task :packer do
$PACKER = find_executable('packer') || find_executable('packer-io')
abort("Can't find packer-io executable") if $PACKER.nil?
version = %x{ #{$PACKER} version }
abort("Can't detect version of #{$PACKER}") if version.strip.empty?
puts " #{$PACKER}\n #{version}"
end
desc "Check present vagrant"
task :vagrant do
$VAGRANT = find_executable 'vagrant'
abort("Can't find vagrant executable") if $VAGRANT.nil?
version = %x{ #{$VAGRANT} --version }
abort("Can't detect version of #{$VAGRANT}") if version.strip.empty?
puts " #{$VAGRANT}\n #{version}"
end
end
desc "check all requirements"
task :check => ['check:python', 'check:packer', 'check:vagrant', 'check:free_space']
desc "Generating variables"
task :generate_variables => "check:python" do
iso_url, iso_checksum = %x[ #{$PYTHON} #{WORKSPACE}/isourl.py ].split
json_struct = { :headless => 'true',
:created_at => Time.now.strftime("%Y%d%m"),
:iso_checksum => iso_checksum,
:iso_url => iso_url }
puts "Variables wroten to file '#{VAR_FILE}'"
File.write VAR_FILE, json_struct.to_json
end
namespace :build do
desc "Build core"
task :core => [:generate_variables, "check:packer"] do
target = "./output/#{box_file('core')}"
if File.exist?(target)
puts "Skip task: file #{target} already exist"
next
end
was_good = system("#{$PACKER} build #{ENV['PACKERARGS'] || ""} -var-file=#{VAR_FILE} -only=virtualbox-iso #{PACKER_TEMPLATE}")
abort('Something happened') if was_good.nil?
end
desc 'Build common'
task :common => ['check:vagrant', :core] do
target = "./output/#{box_file('common')}"
if File.exist?(target)
puts "Skip task: file #{target} already exist"
next
end
box = VagrantCLI.new 'core'
box.up
box.script 'install-yaourt.sh', false
box.script 'deploy-common.sh'
box.script 'deploy-openvas9.sh'
box.script 'cleanup.sh'
box.package target
box.destroy
end
desc 'Build full'
task :full => ['check:vagrant', :common] do
target = "./output/#{box_file('full')}"
if File.exist?(target)
puts "Skip task: file #{target} already exist"
next
end
box = VagrantCLI.new 'common'
box.up
box.script 'deploy-full.sh'
box.script 'cleanup.sh'
box.package target
box.destroy
end
end
desc "Build all"
task :build => "build:full"
desc "Clean builds"
task :clean => 'check:vagrant' do
for profile_name in ['core', 'common', 'full'] do
box = VagrantCLI.new profile_name
box.destroy
system "#{$VAGRANT} box remove --force --box-version=0 --provider=virtualbox #{box_name(profile_name)}"
end
Rake::Cleaner.cleanup_files(FileList['output/*.box', '.vagrant', 'variables.json', 'mkmf.log', 'output-virtualbox-iso'])
end
task :clean_cache do
Rake::Cleaner.cleanup_files(FileList['packer_cache/*.iso', 'pkg_cache/*.pkg.*'])
end