-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathRakefile
339 lines (297 loc) · 12.5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
require 'rake/packagetask'
require 'uri'
require 'net/http'
require 'json'
require 'zip'
require 'date'
require 'nokogiri'
require 'mixlib/shellout'
require './lib/chef/azure/helpers/erb.rb'
PACKAGE_NAME = "ChefExtensionHandler"
MANIFEST_NAME = "publishDefinitionXml"
EXTENSION_VERSION = "1.0"
CHEF_BUILD_DIR = "pkg"
PESTER_VER_TAG = "2.0.4" # we lock down to specific tag version
PESTER_GIT_URL = 'https://github.com/pester/Pester.git'
PESTER_SANDBOX = './PESTER_SANDBOX'
LINUX_PACKAGE_LIST = [
{"ChefExtensionHandler/*.sh" => "#{CHEF_BUILD_DIR}/"},
{"ChefExtensionHandler/bin/*.sh" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.py" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.rb" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/chef-client" => "#{CHEF_BUILD_DIR}/bin"},
{"*.gem" => "#{CHEF_BUILD_DIR}/gems"},
{"ChefExtensionHandler/HandlerManifest.json.nix" => "#{CHEF_BUILD_DIR}/HandlerManifest.json"}
]
WINDOWS_PACKAGE_LIST = [
{"ChefExtensionHandler/*.cmd" => "#{CHEF_BUILD_DIR}/"},
{"ChefExtensionHandler/bin/*.bat" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.ps1" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.psm1" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.rb" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/chef-client" => "#{CHEF_BUILD_DIR}/bin"},
{"*.gem" => "#{CHEF_BUILD_DIR}/gems"},
{"ChefExtensionHandler/HandlerManifest.json" => "#{CHEF_BUILD_DIR}/HandlerManifest.json"}
]
PREVIEW = "deploy_to_preview"
PRODUCTION = "deploy_to_production"
GOV = "deploy_to_gov"
DELETE_FROM_PREVIEW = "delete_from_preview"
DELETE_FROM_PRODUCTION = "delete_from_production"
DELETE_FROM_GOV = "delete_from_gov"
CONFIRM_PUBLIC = "confirm_public_deployment"
CONFIRM_INTERNAL = "confirm_internal_deployment"
DEPLOY_INTERNAL = "deploy_to_internal"
DEPLOY_PUBLIC = "deploy_to_public"
desc "Cleans up the package sandbox"
task :clean do
puts "Cleaning Chef Package..."
FileUtils.rm_f(Dir.glob("*.zip"))
puts "Deleting #{CHEF_BUILD_DIR}"
FileUtils.rm_rf(Dir.glob("#{CHEF_BUILD_DIR}"))
puts "Deleting gem file..."
FileUtils.rm_f(Dir.glob("*.gem"))
FileUtils.rm_f(Dir.glob("publish-template.json"))
end
desc "Builds a azure chef extension gem."
task :gem => [:clean] do
puts "Building gem file..."
puts %x{gem build *.gemspec}
end
desc "Builds the azure chef extension package Ex: build[platform, extension_version], default is build[windows]."
task :build, [:target_type, :extension_version, :confirmation_required] => [:gem] do |t, args|
args.with_defaults(:target_type => "windows",
:extension_version => "1216.16.6.1",
:confirmation_required => "false")
puts "Build called with args(#{args.target_type}, #{args.extension_version})"
# Get user confirmation if we are downloading correct version.
if args.confirmation_required == "true"
confirm!("build")
end
puts "Building #{args.target_type} package..."
# setup the sandbox
FileUtils.mkdir_p CHEF_BUILD_DIR
FileUtils.mkdir_p "#{CHEF_BUILD_DIR}/bin"
FileUtils.mkdir_p "#{CHEF_BUILD_DIR}/gems"
# Copy platform specific files to package dir
puts "Copying #{args.target_type} scripts to package directory..."
package_list = if args.target_type == "windows"
WINDOWS_PACKAGE_LIST
else
LINUX_PACKAGE_LIST
end
package_list.each do |rule|
src = rule.keys.first
dest = rule[src]
puts "Copy: src [#{src}] => dest [#{dest}]"
if File.directory?(dest)
FileUtils.cp_r Dir.glob(src), dest
else
FileUtils.cp Dir.glob(src).first, dest
end
end
date_tag = Date.today.strftime("%Y%m%d")
# Write a release tag file to zip. This will help during testing
# to check if package was synced in PIR.
FileUtils.touch "#{CHEF_BUILD_DIR}/version_#{args.extension_version}_#{date_tag}_#{args.target_type}"
puts "\nCreating a zip package..."
puts "#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip\n\n"
Zip::File.open("#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip", Zip::File::CREATE) do |zipfile|
Dir[File.join("#{CHEF_BUILD_DIR}/", '**', '**')].each do |file|
zipfile.add(file.sub("#{CHEF_BUILD_DIR}/", ''), file)
end
end
end
def is_internal?(args)
is_internal = if args.internal_or_public == CONFIRM_INTERNAL
true
elsif args.internal_or_public == CONFIRM_PUBLIC
false
end
end
def confirm!(type)
print "Do you wish to proceed? (y/n)"
proceed = STDIN.gets.chomp() == 'y'
if not proceed
puts "Exiting #{type} request."
exit
end
end
desc "List extension versions"
task :list_versions do
resource_type = "Microsoft.Compute/sharedVMExtensions/versions"
system("az resource list --resource-type #{resource_type}")
end
desc "Publishes the azure chef extension package using publish.json Ex: publish[deploy_type, platform, extension_version], default is build[preview,windows]."
task :publish, [:deploy_type, :target_type, :extension_version, :chef_deploy_namespace, :operation, :internal_or_public, :confirmation_required] => [:build] do |t, args|
args.with_defaults(
:deploy_type => PREVIEW,
:target_type => "windows",
:extension_version => EXTENSION_VERSION,
:chef_deploy_namespace => "Chef.Bootstrap.WindowsAzure.Test",
:operation => "new",
:internal_or_public => CONFIRM_INTERNAL,
:confirmation_required => "true"
)
storageAccount="azurechefextensions"
storageContainer="published-packages"
puts "**Publish called with args:\n#{args}\n\n"
puts "Continuing with publish request..."
puts <<-CONFIRMATION
*****************************************
This task creates a chef extension package and publishes to Azure #{args.deploy_type}.
Details:
-------
Publish To: ** #{args.deploy_type.gsub(/deploy_to_/, "")} **
Extension Version: #{args.extension_version}
Build branch: #{%x{git rev-parse --abbrev-ref HEAD}}
Type: #{is_internal?(args) ? "Internal build" : "Public release"}
****************************************
CONFIRMATION
if args.confirmation_required == 'true'
confirm!("publish")
end
date_tag = Date.today.strftime("%Y%m%d")
package="#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip"
puts "Creating template file"
data=File.read(__dir__+"/publish-template-default.json")
data_hash=JSON.parse(data)
if args.target_type=='windows'
data_hash['variables']['typeName']= 'ChefClient'
data_hash['variables']['supportedOS']='Windows'
else
data_hash['variables']['typeName']= 'LinuxChefClient'
data_hash['variables']['supportedOS']='Linux'
end
if args.internal_or_public == CONFIRM_PUBLIC
data_hash['variables']['isInternalExtension']= 'false'
else
data_hash['variables']['isInternalExtension']= 'true'
end
data_hash['variables']['version']=args.extension_version
data_hash['variables']['regions']=["*"]
if args.deploy_type == GOV
data_hash['variables']['mediaLink']="https://#{storageAccount}.blob.core.usgovcloudapi.net/#{storageContainer}/#{package}"
#https://azurechefextensions.blob.core.usgovcloudapi.net/published-packages/ChefExtensionHandler_1216.16.6.6_20220421_ubuntu.zip
else
data_hash['variables']['mediaLink']="https://#{storageAccount}.blob.core.windows.net/#{storageContainer}/#{package}"
end
# https://extpublish.blob.core.windows.net/extension/ChefExtensionHandler
# puts(data_hash)
File.write(__dir__+"/publish-template.json", JSON.dump(data_hash))
puts "Deploying package to storage account"
upload_to_storage(package,storageAccount,storageContainer)
# CONFIRMATION
# Get user confirmation, since we are publishing a new build to Azure.
puts ("Deploying the template please confirm if you would like to continue")
if args.confirmation_required == "true"
confirm!("publish")
end
deploy_template(args)
end
desc "Publishes the azure chef extension package using publish.json Ex: publish[deploy_type, platform, extension_version], default is build[preview,windows]."
task :promote_regions, [:deploy_type, :target_type, :extension_version, :chef_deploy_namespace, :operation, :internal_or_public, :confirmation_required, :region1, :region2] => [:build] do |t, args|
args.with_defaults(
:deploy_type => PREVIEW,
:target_type => "windows",
:extension_version => EXTENSION_VERSION,
:chef_deploy_namespace => "Chef.Bootstrap.WindowsAzure.Test",
:operation => "new",
:internal_or_public => CONFIRM_INTERNAL,
:confirmation_required => "true",
:region1 => "East US"
)
storageAccount="azurechefextensions"
storageContainer="published-packages"
puts "**Publish called with args:\n#{args}\n\n"
puts "Continuing with publish request..."
puts <<-CONFIRMATION
*****************************************
This task creates a chef extension package and publishes to Azure #{args.deploy_type}.
Details:
-------
Publish To: ** #{args.deploy_type.gsub(/deploy_to_/, "")} **
Extension Version: #{args.extension_version}
Build branch: #{%x{git rev-parse --abbrev-ref HEAD}}
Type: #{is_internal?(args) ? "Internal build" : "Public release"}
****************************************
CONFIRMATION
if args.confirmation_required == 'true'
confirm!("publish")
end
date_tag = Date.today.strftime("%Y%m%d")
package="#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip"
puts "Creating template file"
data=File.read(__dir__+"/publish-template-default.json")
data_hash=JSON.parse(data)
if args.target_type=='windows'
data_hash['variables']['typeName']= 'ChefClient'
data_hash['variables']['supportedOS']='Windows'
else
data_hash['variables']['typeName']= 'LinuxChefClient'
data_hash['variables']['supportedOS']='Linux'
end
if args.internal_or_public == CONFIRM_PUBLIC
data_hash['variables']['isInternalExtension']= 'false'
else
data_hash['variables']['isInternalExtension']= 'true'
end
data_hash['variables']['version']=args.extension_version
if args.region2 == nil
data_hash['variables']['regions']=["#{args.region1}"]
else
data_hash['variables']['regions']=args.region1,args.region2
end
if args.deploy_type == GOV
data_hash['variables']['mediaLink']="https://#{storageAccount}.blob.core.usgovcloudapi.net/#{storageContainer}/#{package}"
#https://azurechefextensions.blob.core.usgovcloudapi.net/published-packages/ChefExtensionHandler_1216.16.6.6_20220421_ubuntu.zip
else
data_hash['variables']['mediaLink']="https://#{storageAccount}.blob.core.windows.net/#{storageContainer}/#{package}"
end
#puts(data_hash)
File.write(__dir__+"/publish-template.json", JSON.dump(data_hash))
puts "Deploying package to storage account"
upload_to_storage(package,storageAccount,storageContainer)
# CONFIRMATION
# Get user confirmation, since we are publishing a new build to Azure.
puts ("Deploying the template please confirm if you would like to continue")
if args.confirmation_required == "true"
confirm!("publish")
end
deploy_template(args)
end
def deploy_template(args)
template=__dir__+"/publish-template.json"
group_name = "ExtensionPublishing"
if args.target_type == "windows"
resgrp = "azure-chef-extension-window"
begin
cli_cmd = Mixlib::ShellOut.new("az deployment group create --name #{group_name} --resource-group #{resgrp} --template-file #{template}")
result = cli_cmd.run_command
result.error!
puts "The windows extension has been successfully published."
rescue Mixlib::ShellOut::ShellCommandFailed => e
puts "The winddows extension publishing is failing while deploying #{template}"
end
else
resgrp = "azure-chef-extension-linux"
begin
cli_cmd = Mixlib::ShellOut.new("az deployment group create --name #{group_name} --resource-group #{resgrp} --template-file #{template}")
result = cli_cmd.run_command
result.error!
puts "The linux extension has been successfully published."
rescue Mixlib::ShellOut::ShellCommandFailed => e
puts "The linux extension publishing is failing while deploying #{template}"
end
end
end
def upload_to_storage(package,storageAccount,storageContainer)
begin
cli_cmd = Mixlib::ShellOut.new("az storage blob upload --account-name #{storageAccount} --container-name #{storageContainer} --name #{package} --file #{package}")
result = cli_cmd.run_command
result.error!
puts "The #{package} has been succesfully uploaded to storage account #{storageAccount} in #{storageContainer} container."
rescue Mixlib::ShellOut::ShellCommandFailed => e
puts "The upload has failed for #{package} to storage account #{storageAccount} in #{storageContainer} container"
end
end