forked from DataDog/dd-agent-omnibus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile.rb
155 lines (132 loc) · 5.13 KB
/
Rakefile.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
150
151
152
153
154
155
require 'json'
require 'ohai'
@ohai = Ohai::System.new.tap { |o| o.all_plugins(%w{platform}) }.data
def linux?()
%w(rhel debian fedora suse gentoo slackware arch exherbo).include? @ohai['platform_family']
end
def osx?()
@ohai['platform_family'] == 'mac_os_x'
end
def windows?()
@ohai['platform_family'] == 'windows'
end
if windows?
PROJECT_DIR='c:\dd-agent-omnibus'
FSROOT="/c/"
else
PROJECT_DIR='/dd-agent-omnibus'
FSROOT="/"
end
namespace :agent do
desc 'Cleanup generated files'
task :clean do |t|
puts "Clean up generated files"
unless windows?
sh "rm -rf #{FSROOT}var/cache/omnibus/pkg/*"
sh "rm -f #{FSROOT}etc/init.d/datadog-agent"
sh "rm -rf #{FSROOT}etc/dd-agent"
end
sh "rm -rf #{FSROOT}opt/datadog-agent"
t.reenable
end
desc 'Pull the integrations repo'
task :'pull-integrations' do
integration_branch = ENV['INTEGRATION_BRANCH'] || 'master'
# FSROOT is "/" on linux, "c:\" on windows. Let's not clobber the filesystem
# if someone forgets to set INTEGRATIONS_REPO
raise 'INTEGRATIONS_REPO not set!' unless ENV['INTEGRATIONS_REPO']
sh "rm -rf #{FSROOT}#{ENV['INTEGRATIONS_REPO']}"
sh "git clone https://github.com/DataDog/#{ENV['INTEGRATIONS_REPO']}.git /#{ENV['INTEGRATIONS_REPO']} || true"
sh "cd /#{ENV['INTEGRATIONS_REPO']} && git checkout #{integration_branch}"
sh "cd /#{ENV['INTEGRATIONS_REPO']} && git fetch --all"
sh "cd /#{ENV['INTEGRATIONS_REPO']} && git checkout dd-check-#{ENV['INTEGRATION']}-#{integration_branch} || git checkout #{integration_branch}"
sh "cd /#{ENV['INTEGRATIONS_REPO']} && git reset --hard"
end
desc 'Build an integration'
task :'build-integration' do
Rake::Task["agent:clean"].invoke
Rake::Task["env:import-rpm-key"].invoke
Rake::Task["agent:pull-integrations"].invoke
if ENV['BUILD_ALL_INTEGRATIONS'] || !ENV['INTEGRATION']
Rake::Task["agent:build-all-integrations"].invoke
elsif ENV['INTEGRATION']
checks = ENV['INTEGRATION'].split(',')
checks.each do |check|
prepare_and_execute_build(check)
end
end
end
desc 'Build all integrations'
task :'build-all-integrations' do
checks = Dir.glob("/#{ENV['INTEGRATIONS_REPO']}/*/")
checks.each do |check|
check.slice! "/#{ENV['INTEGRATIONS_REPO']}/"
check.slice! "/"
prepare_and_execute_build(check)
Rake::Task["agent:clean"].invoke
end
end
end
namespace :env do
desc 'Import signing RPM key'
task :'import-rpm-key' do
# If an RPM_SIGNING_PASSPHRASE has been passed, let's import the signing key
if ENV.has_key?('RPM_SIGNING_PASSPHRASE') and not ENV['RPM_SIGNING_PASSPHRASE'].empty?
sh "if [ \"$RPM_SIGNING_PASSPHRASE\" ]; then gpg --import /keys/RPM-SIGNING-KEY.private; fi"
end
end
end
def prepare_and_execute_build(integration, dont_error_on_build: false)
sh "cd #{PROJECT_DIR} && bundle update"
puts "building integration #{integration}"
manifest = JSON.parse(File.read("/#{ENV['INTEGRATIONS_REPO']}/#{integration}/manifest.json"))
# The manifest should always have a version
integration_version = manifest['version']
if linux?
manifest['supported_os'].include?('linux') || return
elsif windows?
manifest['supported_os'].include?('windows') || return
elsif osx?
manifest['supported_os'].include?('osx') || return
end
header = erb_header({
'name' => "#{integration}",
'version' => "#{integration_version}",
'build_iteration' => "#{ENV['BUILD_ITERATION']}",
'integrations_repo' => "#{ENV['INTEGRATIONS_REPO']}",
'guid' => "#{manifest['guid']}",
'description' => "#{manifest['description']}"
})
#`(echo '#{header}' && cat #{PROJECT_DIR}/resources/datadog-integrations/project.rb.erb) | erb > #{PROJECT_DIR}/config/projects/dd-check-#{integration}.rb`
sh "(echo \"#{header}\" && cat #{PROJECT_DIR}/resources/datadog-integrations/project.rb.erb) | erb > #{PROJECT_DIR}/config/projects/dd-check-#{integration}.rb"
header = erb_header({
'name' => "#{integration}",
'project_dir' => "#{PROJECT_DIR}",
'integrations_repo' => "#{ENV['INTEGRATIONS_REPO']}"
})
sh "(echo \"#{header}\" && cat #{PROJECT_DIR}/resources/datadog-integrations/software.rb.erb) | erb > #{PROJECT_DIR}/config/software/dd-check-#{integration}-software.rb"
if windows?
FileUtils.mkdir_p("#{PROJECT_DIR}/resources/dd-check-#{integration}/msi")
FileUtils.cp_r("#{PROJECT_DIR}/resources/datadog-integrations/msi", "#{PROJECT_DIR}/resources/dd-check-#{integration}")
end
if windows?
sh "cd #{PROJECT_DIR} && bundle exec omnibus --version"
build_cmd = "cd #{PROJECT_DIR} && bundle exec omnibus build --log-level debug dd-check-#{integration}"
else
sh "cd #{PROJECT_DIR} && omnibus --version"
build_cmd = "cd #{PROJECT_DIR} && bin/omnibus build dd-check-#{integration} --output_manifest=false"
end
if dont_error_on_build
build_cmd += " || true"
end
sh build_cmd
end
def erb_header(variables)
# ERB does not support setting template variables on the command line
# this method generates a header usable by a ERB file
out = ""
variables.each do |key, value|
out += "<% #{key}='#{value}' %>"
end
out
end