-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
72 lines (58 loc) · 2.26 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
require "erb"
require "rake"
require 'yaml'
OPENWRT_VERSION="23.05.5"
PLATFORM="x86"
PLATFORM_TYPE="64"
DOWNLOAD_BASE="https://downloads.openwrt.org/releases/#{OPENWRT_VERSION}/targets/#{PLATFORM}/#{PLATFORM_TYPE}/"
SDK_BASE="openwrt-imagebuilder-#{OPENWRT_VERSION}-#{PLATFORM}-#{PLATFORM_TYPE}.Linux-x86_64"
task :default => :generate_all
task :generate_all => :install_sdk do
if (! (File.exists? 'secrets.yml'))
raise "\n \t >>> Please decrypt secrets.yml.gpg first \n\n\n"
end
secrets = YAML.load_file("secrets.yml")
# Default Generate config for all nodes
nodes = YAML.load_file("nodes.yml")
nodes.values.each {|v| generate_node v,secrets}
end
def generate_node(node_cfg,secrets)
dir_name = "#{SDK_BASE}/files_generated"
prepare_directory(dir_name)
#Evaluate templates
Dir.glob("#{dir_name}/**/*.erb").each do |erb_file|
basename = erb_file.gsub '.erb',''
process_erb(node_cfg,erb_file,basename,secrets)
end
generate_firmware(node_cfg['hostname'], node_cfg['profile'], node_cfg['packages'])
end
def prepare_directory(dir_name)
# Clean up
FileUtils.rm_r dir_name if File.exists? dir_name
# Prepare
FileUtils.cp_r 'files', dir_name, :preserve => true
end
def process_erb(node,erb,base,secrets)
@node = node
@secrets = secrets
template = ERB.new File.new(erb).read
File.open(base, 'w') { |file| file.write(template.result) }
FileUtils.rm erb
end
def generate_firmware(node_name,profile,packages)
FileUtils.rm_r "#{SDK_BASE}/bin/" if File.exists? "#{SDK_BASE}/bin/"
puts "Exec: make -C '#{SDK_BASE}' image PROFILE=#{profile} PACKAGES='#{packages}' FILES=./files_generated"
system("make -C '#{SDK_BASE}' image PROFILE=#{profile} PACKAGES='#{packages}' FILES=./files_generated")
Dir.glob("#{SDK_BASE}/bin/targets/#{PLATFORM}/#{PLATFORM_TYPE}/openwrt-*").each do |bin_file|
new_name = File.basename(bin_file).gsub "openwrt-#{OPENWRT_VERSION}-#{PLATFORM}-#{PLATFORM_TYPE}-#{profile}","#{node_name}"
puts "Moving #{bin_file} to bin/#{new_name} "
FileUtils.mv(bin_file, "bin/#{new_name}")
end
end
task :install_sdk do
sdk_archive = "#{SDK_BASE}.tar.xz"
unless File.exists? SDK_BASE
system("wget #{DOWNLOAD_BASE}#{sdk_archive}") unless File.exists? sdk_archive
system("tar xf #{sdk_archive}")
end
end