-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathec2.rb
858 lines (776 loc) · 33.6 KB
/
ec2.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#
# Author:: Fletcher Nichol (<[email protected]>)
#
# Copyright:: 2016-2018, Chef Software, Inc.
# Copyright:: 2015-2018, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "benchmark" unless defined?(Benchmark)
require "json" unless defined?(JSON)
require "kitchen"
require_relative "ec2_version"
require_relative "aws/client"
require_relative "aws/instance_generator"
require_relative "aws/standard_platform"
require_relative "aws/standard_platform/amazon"
require_relative "aws/standard_platform/amazon2"
require_relative "aws/standard_platform/centos"
require_relative "aws/standard_platform/debian"
require_relative "aws/standard_platform/rhel"
require_relative "aws/standard_platform/fedora"
require_relative "aws/standard_platform/freebsd"
require_relative "aws/standard_platform/ubuntu"
require_relative "aws/standard_platform/windows"
require "aws-sdk-ec2"
require "aws-sdk-core/waiters/errors"
require "retryable" unless defined?(Retryable)
require "time" unless defined?(Time)
require "etc" unless defined?(Etc)
require "socket" unless defined?(Socket)
module Kitchen
module Driver
# Amazon EC2 driver for Test Kitchen.
#
# @author Fletcher Nichol <[email protected]>
class Ec2 < Kitchen::Driver::Base
kitchen_driver_api_version 2
plugin_version Kitchen::Driver::EC2_VERSION
default_config :region, ENV["AWS_REGION"] || "us-east-1"
default_config :shared_credentials_profile, ENV["AWS_PROFILE"]
default_config :availability_zone, nil
default_config :instance_type, &:default_instance_type
default_config :ebs_optimized, false
default_config :security_group_ids, nil
default_config :security_group_filter, nil
default_config :security_group_cidr_ip, "0.0.0.0/0"
default_config :tags, "created-by" => "test-kitchen"
default_config :user_data do |driver|
if driver.windows_os?
driver.default_windows_user_data
end
end
default_config :private_ip_address, nil
default_config :iam_profile_name, nil
default_config :spot_price, nil
default_config :block_duration_minutes, nil
default_config :retryable_tries, 60
default_config :spot_wait, 60
default_config :retryable_sleep, 5
default_config :aws_access_key_id, nil
default_config :aws_secret_access_key, nil
default_config :aws_session_token, nil
default_config :aws_ssh_key_id, ENV["AWS_SSH_KEY_ID"]
default_config :image_id, &:default_ami
default_config :image_search, nil
default_config :username, nil
default_config :associate_public_ip, nil
default_config :interface, nil
default_config :http_proxy, ENV["HTTPS_PROXY"] || ENV["HTTP_PROXY"]
default_config :retry_limit, 3
default_config :tenancy, "default"
default_config :instance_initiated_shutdown_behavior, nil
default_config :ssl_verify_peer, true
default_config :skip_cost_warning, false
def initialize(*args, &block)
super
end
def self.validation_warn(driver, old_key, new_key)
driver.warn "WARN: The driver[#{driver.class.name}] config key `#{old_key}` " \
"is deprecated, please use `#{new_key}`"
end
def self.validation_error(driver, old_key, new_key)
warn "ERROR: The driver[#{driver.class.name}] config key `#{old_key}` " \
"has been removed, please use `#{new_key}`"
exit!
end
# TODO: remove these in 1.1
deprecated_configs = %i{ebs_volume_size ebs_delete_on_termination ebs_device_name}
deprecated_configs.each do |d|
validations[d] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "block_device_mappings")
end
end
end
validations[:ssh_key] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "transport.ssh_key")
end
end
validations[:ssh_timeout] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "transport.connection_timeout")
end
end
validations[:ssh_retries] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "transport.connection_retries")
end
end
validations[:username] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "transport.username")
end
end
validations[:flavor_id] = lambda do |attr, val, driver|
unless val.nil?
validation_error(driver, attr, "instance_type")
end
end
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/dedicated-instance.html
validations[:tenancy] = lambda do |attr, val, _driver|
unless %w{default host dedicated}.include?(val)
warn "'#{val}' is an invalid value for option '#{attr}'. " \
"Valid values are 'default', 'host', or 'dedicated'."
exit!
end
end
# The access key/secret are now using the priority list AWS uses
# Providing these inside the .kitchen.yml is no longer recommended
validations[:aws_access_key_id] = lambda do |attr, val, _driver|
unless val.nil?
warn "#{attr} is no longer a valid config option, please use " \
"ENV['AWS_ACCESS_KEY_ID'] or ~/.aws/credentials. See " \
"the README for more details"
exit!
end
end
validations[:aws_secret_access_key] = lambda do |attr, val, _driver|
unless val.nil?
warn "#{attr} is no longer a valid config option, please use " \
"ENV['AWS_SECRET_ACCESS_KEY'] or ~/.aws/credentials. See " \
"the README for more details"
exit!
end
end
validations[:aws_session_token] = lambda do |attr, val, _driver|
unless val.nil?
warn "#{attr} is no longer a valid config option, please use " \
"ENV['AWS_SESSION_TOKEN'] or ~/.aws/credentials. See " \
"the README for more details"
exit!
end
end
validations[:instance_initiated_shutdown_behavior] = lambda do |attr, val, _driver|
unless [nil, "stop", "terminate"].include?(val)
warn "'#{val}' is an invalid value for option '#{attr}'. " \
"Valid values are 'stop' or 'terminate'"
exit!
end
end
# empty keys cause failures when tagging and they make no sense
validations[:tags] = lambda do |attr, val, _driver|
# if someone puts the tags each on their own line it's an array not a hash
# @todo we should probably just do the right thing and support this format
if val.class == Array
warn "AWS instance tags must be specified as a single hash, not a tag " \
"on each line. Example: {:foo => 'bar', :bar => 'foo'}"
exit!
end
end
def create(state)
return if state[:server_id]
update_username(state)
info(Kitchen::Util.outdent!(<<-END)) unless config[:skip_cost_warning]
If you are not using an account that qualifies under the AWS
free-tier, you may be charged to run these suites. The charge
should be minimal, but neither Test Kitchen nor its maintainers
are responsible for your incurred costs.
END
# If no security group IDs are specified, create one automatically.
unless config[:security_group_ids] || config[:security_group_filter]
create_security_group(state)
config[:security_group_ids] = [state[:auto_security_group_id]]
end
# If no SSH key pair name is specified, create one automatically.
# If `_disabled`, nullify the key ID to avoid associating the instance with
# an AWS-managed key pair.
case config[:aws_ssh_key_id]
when nil
create_key(state)
config[:aws_ssh_key_id] = state[:auto_key_id]
when "_disable"
info("Disabling AWS-managed SSH key pairs for this EC2 instance.")
info("The key pairs for the kitchen transport config and the AMI must match.")
config[:aws_ssh_key_id] = nil
end
if config[:spot_price]
# Spot instance when a price is set
server = with_request_limit_backoff(state) { submit_spots }
else
# On-demand instance
server = with_request_limit_backoff(state) { submit_server }
end
info("Instance <#{server.id}> requested.")
with_request_limit_backoff(state) do
logging_proc = ->(attempts) { info("Polling AWS for existence, attempt #{attempts}...") }
server.wait_until_exists(before_attempt: logging_proc)
end
state[:server_id] = server.id
info("EC2 instance <#{state[:server_id]}> created.")
# See https://github.com/aws/aws-sdk-ruby/issues/859
# Waiting can fail, so we have to retry on that.
Retryable.retryable(
tries: 10,
sleep: lambda { |n| [2**n, 30].min },
on: ::Aws::EC2::Errors::InvalidInstanceIDNotFound
) do |r, _|
wait_until_ready(server, state)
end
if windows_os? &&
instance.transport[:username] =~ /administrator/i &&
instance.transport[:password].nil?
# If we're logging into the administrator user and a password isn't
# supplied, try to fetch it from the AWS instance
fetch_windows_admin_password(server, state)
end
info("EC2 instance <#{state[:server_id]}> ready (hostname: #{state[:hostname]}).")
instance.transport.connection(state).wait_until_ready
create_ec2_json(state) if /chef/i.match?(instance.provisioner.name)
debug("ec2:create '#{state[:hostname]}'")
rescue Exception
# Clean up any auto-created security groups or keys on the way out.
delete_security_group(state)
delete_key(state)
raise
end
def destroy(state)
if state[:server_id]
server = ec2.get_instance(state[:server_id])
unless server.nil?
instance.transport.connection(state).close
begin
server.terminate
rescue ::Aws::EC2::Errors::InvalidInstanceIDNotFound => e
warn("Received #{e}, instance was probably already destroyed. Ignoring")
end
end
# If we are going to clean up an automatic security group, we need
# to wait for the instance to shut down. This slightly breaks the
# subsystem encapsulation, sorry not sorry.
if state[:auto_security_group_id] && server
server.wait_until_terminated do |waiter|
waiter.max_attempts = config[:retryable_tries]
waiter.delay = config[:retryable_sleep]
waiter.before_attempt do |attempts|
info "Waited #{attempts * waiter.delay}/#{waiter.delay * waiter.max_attempts}s for instance <#{server.id}> to terminate."
end
end
end
info("EC2 instance <#{state[:server_id]}> destroyed.")
state.delete(:server_id)
state.delete(:hostname)
end
# Clean up any auto-created security groups or keys.
delete_security_group(state)
delete_key(state)
end
def image
return @image if defined?(@image)
if config[:image_id]
@image = ec2.resource.image(config[:image_id])
show_chosen_image
else
raise "Neither image_id nor an image_search specified for instance #{instance.name}!" \
" Please specify one or the other."
end
@image
end
def default_instance_type
@instance_type ||= begin
# We default to the free tier (t2.micro for hvm, t1.micro for paravirtual)
if image && image.virtualization_type == "hvm"
info("instance_type not specified. Using free tier t2.micro instance ...")
"t2.micro"
else
info("instance_type not specified. Using free tier t1.micro instance since" \
" image is paravirtual (pick an hvm image to use the superior t2.micro!) ...")
"t1.micro"
end
end
end
# The actual platform is the platform detected from the image
def actual_platform
@actual_platform ||= Aws::StandardPlatform.from_image(self, image) if image
end
def desired_platform
@desired_platform ||= begin
platform = Aws::StandardPlatform.from_platform_string(self, instance.platform.name)
if platform
debug("platform name #{instance.platform.name} appears to be a standard platform." \
" Searching for #{platform} ...")
end
platform
end
end
def default_ami
@default_ami ||= begin
search_platform = desired_platform ||
Aws::StandardPlatform.from_platform_string(self, "ubuntu")
image_search = config[:image_search] || search_platform.image_search
search_platform.find_image(image_search)
end
end
def update_username(state)
# BUG: With the following equality condition on username, if the user specifies 'root'
# as the transport's username then we will overwrite that value with one from the standard
# platform definitions. This seems difficult to handle here as the default username is
# provided by the underlying transport classes, and is often non-nil (eg; 'root'), leaving
# us no way to distinguish a user-set value from the transport's default.
# See https://github.com/test-kitchen/kitchen-ec2/pull/273
if actual_platform &&
instance.transport[:username] == instance.transport.class.defaults[:username]
debug("No SSH username specified: using default username #{actual_platform.username} " \
" for image #{config[:image_id]}, which we detected as #{actual_platform}.")
state[:username] = actual_platform.username
end
end
def ec2
@ec2 ||= Aws::Client.new(
config[:region],
config[:shared_credentials_profile],
config[:http_proxy],
config[:retry_limit],
config[:ssl_verify_peer]
)
end
def instance_generator
@instance_generator = Aws::InstanceGenerator.new(config, ec2, instance.logger)
end
# AWS helper for creating the instance
def submit_server
instance_data = instance_generator.ec2_instance_data
debug("Creating EC2 instance in region #{config[:region]} with properties:")
instance_data.each do |key, value|
debug("- #{key} = #{value.inspect}")
end
ec2.create_instance(instance_data)
end
def config
return super unless @config
@config
end
# Take one config and expand to multiple configs
def expand_config(conf, key)
configs = []
if conf[key] && conf[key].is_a?(Array)
values = conf[key]
values.each do |value|
new_config = conf.clone
new_config[key] = value
configs.push new_config
end
else
configs.push conf
end
configs
end
def submit_spots
configs = [config]
expanded = []
keys = %i{instance_type}
unless config[:subnet_filter]
# => Use explicitly specified subnets
keys << :subnet_id
else
# => Enable cascading through matching subnets
client = ::Aws::EC2::Client.new(region: config[:region])
subnets = client.describe_subnets(
filters: [
{
name: "tag:#{config[:subnet_filter][:tag]}",
values: [config[:subnet_filter][:value]],
},
]
).subnets
raise "A subnet matching '#{config[:subnet_filter][:tag]}:#{config[:subnet_filter][:value]}' does not exist!" unless subnets.any?
configs = subnets.map do |subnet|
new_config = config.clone
new_config[:subnet_id] = subnet.subnet_id
new_config[:subnet_filter] = nil
new_config
end
end
keys.each do |key|
configs.each do |conf|
expanded.push expand_config(conf, key)
end
configs = expanded.flatten
expanded = []
end
errs = []
configs.each do |conf|
@config = conf
return submit_spot
rescue => e
errs.append(e)
end
raise ["Could not create a spot instance:", errs].flatten.join("\n")
end
def submit_spot
debug("Creating EC2 Spot Instance..")
instance_data = instance_generator.ec2_instance_data
config_spot_price = config[:spot_price].to_s
if %w{ondemand on-demand}.include?(config_spot_price)
spot_price = ""
else
spot_price = config_spot_price
end
spot_options = {
# Must use one-time in order to use instance_interruption_behavior=terminate
# spot_instance_type: "one-time", # default
# Must use instance_interruption_behavior=terminate in order to use block_duration_minutes
# instance_interruption_behavior: "terminate", # default
}
if config[:block_duration_minutes]
spot_options[:block_duration_minutes] = config[:block_duration_minutes]
end
unless spot_price == "" # i.e. on-demand
spot_options[:max_price] = spot_price
end
instance_data[:instance_market_options] = {
market_type: "spot",
spot_options: spot_options,
}
# The preferred way to create a spot instance is via request_spot_instances()
# However, it does not allow for tagging to occur at creation time.
# create_instances() allows creation of tagged spot instances, but does
# not retry if the price could not be satisfied immediately.
Retryable.retryable(
tries: config[:spot_wait] / config[:retryable_sleep],
sleep: lambda { |_n| config[:retryable_sleep] },
on: ::Aws::EC2::Errors::SpotMaxPriceTooLow
) do |retries|
c = retries * config[:retryable_sleep]
t = config[:spot_wait]
info "Waited #{c}/#{t}s for spot request to become fulfilled."
ec2.create_instance(instance_data)
end
end
# Normally we could use `server.wait_until_running` but we actually need
# to check more than just the instance state
def wait_until_ready(server, state)
wait_with_destroy(server, state, "to become ready") do |aws_instance|
hostname = hostname(aws_instance, config[:interface])
# We aggressively store the hostname so if the process fails here
# we still have it, even if it will change later
state[:hostname] = hostname
# Euca instances often report ready before they have an IP
ready = aws_instance.exists? &&
aws_instance.state.name == "running" &&
hostname != "0.0.0.0"
if ready && ( hostname.nil? || hostname == "" )
debug("Unable to detect hostname using interface_type #{config[:interface]}. Fallback to ordered mapping")
state[:hostname] = hostname(aws_instance, nil)
end
if ready && windows_os?
output = server.console_output.output
unless output.nil?
output = Base64.decode64(output)
debug "Console output: --- \n#{output}"
end
ready = !!(output =~ /Windows is Ready to use/)
end
ready
end
end
# Poll a block, waiting for it to return true. If it does not succeed
# within the configured time we destroy the instance to save people money
def wait_with_destroy(server, state, status_msg, &block)
wait_log = proc do |attempts|
c = attempts * config[:retryable_sleep]
t = config[:retryable_tries] * config[:retryable_sleep]
info "Waited #{c}/#{t}s for instance <#{state[:server_id]}> #{status_msg}."
end
begin
with_request_limit_backoff(state) do
server.wait_until(
max_attempts: config[:retryable_tries],
delay: config[:retryable_sleep],
before_attempt: wait_log,
&block
)
end
rescue ::Aws::Waiters::Errors::WaiterFailed
error("Ran out of time waiting for the server with id [#{state[:server_id]}]" \
" #{status_msg}, attempting to destroy it")
destroy(state)
raise
end
end
def fetch_windows_admin_password(server, state)
wait_with_destroy(server, state, "to fetch windows admin password") do |aws_instance|
enc = server.client.get_password_data(
instance_id: state[:server_id]
).password_data
# Password data is blank until password is available
!enc.nil? && !enc.empty?
end
pass = with_request_limit_backoff(state) do
server.decrypt_windows_password(File.expand_path(state[:ssh_key] || instance.transport[:ssh_key]))
end
state[:password] = pass
info("Retrieved Windows password for instance <#{state[:server_id]}>.")
end
def with_request_limit_backoff(state)
retries = 0
begin
yield
rescue ::Aws::EC2::Errors::RequestLimitExceeded, ::Aws::Waiters::Errors::UnexpectedError => e
raise unless retries < 5 && e.message.include?("Request limit exceeded")
retries += 1
info("Request limit exceeded for instance <#{state[:server_id]}>." \
" Trying again in #{retries**2} seconds.")
sleep(retries**2)
retry
end
end
#
# Ordered mapping from config name to Fog name. Ordered by preference
# when looking up hostname.
#
INTERFACE_TYPES =
{
"dns" => "public_dns_name",
"public" => "public_ip_address",
"private" => "private_ip_address",
"private_dns" => "private_dns_name",
"id" => "id",
}.freeze
#
# Lookup hostname of provided server. If interface_type is provided use
# that interface to lookup hostname. Otherwise, try ordered list of
# options.
#
def hostname(server, interface_type = nil)
if interface_type
interface_type = INTERFACE_TYPES.fetch(interface_type) do
raise Kitchen::UserError, "Invalid interface [#{interface_type}]"
end
server.send(interface_type)
else
potential_hostname = nil
INTERFACE_TYPES.values.each do |type|
potential_hostname ||= server.send(type)
# AWS returns an empty string if the dns name isn't populated yet
potential_hostname = nil if potential_hostname == ""
end
potential_hostname
end
end
#
# Returns the sudo command to use or empty string if sudo is not configured
#
def sudo_command
instance.provisioner[:sudo] ? instance.provisioner[:sudo_command].to_s : ""
end
def create_ec2_json(state)
if windows_os?
cmd = "New-Item -Force C:\\chef\\ohai\\hints\\ec2.json -ItemType File"
else
debug "Using sudo_command='#{sudo_command}' for ohai hints"
cmd = "#{sudo_command} mkdir -p /etc/chef/ohai/hints; #{sudo_command} touch /etc/chef/ohai/hints/ec2.json"
end
instance.transport.connection(state).execute(cmd)
end
def default_windows_user_data
base_script = Kitchen::Util.outdent!(<<-EOH)
$OSVersion = (get-itemproperty -Path "HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" -Name ProductName).ProductName
If($OSVersion.contains('2016') -Or $OSVersion.contains('2019') -Or $OSVersion -eq 'Windows Server Datacenter') {
New-Item -ItemType Directory -Force -Path 'C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Log'
$logfile='C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Log\\kitchen-ec2.log'
# EC2Launch doesn't init extra disks by default
C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\InitializeDisks.ps1
} Else {
New-Item -ItemType Directory -Force -Path 'C:\\Program Files\\Amazon\\Ec2ConfigService\\Logs'
$logfile='C:\\Program Files\\Amazon\\Ec2ConfigService\\Logs\\kitchen-ec2.log'
}
# Logfile fail-safe in case the directory does not exist
New-Item $logfile -Type file -Force
# Allow script execution
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
#PS Remoting and & winrm.cmd basic config
$enableArgs=@{Force=$true}
$command=Get-Command Enable-PSRemoting
if($command.Parameters.Keys -contains "skipnetworkprofilecheck"){
$enableArgs.skipnetworkprofilecheck=$true
}
Enable-PSRemoting @enableArgs
& winrm.cmd set winrm/config '@{MaxTimeoutms="1800000"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser="50"}' >> $logfile
& winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile
#Firewall Config
& netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any >> $logfile
Set-ItemProperty -Name LocalAccountTokenFilterPolicy -Path HKLM:\\software\\Microsoft\\Windows\\CurrentVersion\\Policies\\system -Value 1
EOH
# Preparing custom static admin user if we defined something other than Administrator
custom_admin_script = ""
if !(instance.transport[:username] =~ /administrator/i) && instance.transport[:password]
custom_admin_script = Kitchen::Util.outdent!(<<-EOH)
"Disabling Complex Passwords" >> $logfile
$seccfg = [IO.Path]::GetTempFileName()
& secedit.exe /export /cfg $seccfg >> $logfile
(Get-Content $seccfg) | Foreach-Object {$_ -replace "PasswordComplexity\\s*=\\s*1", "PasswordComplexity = 0"} | Set-Content $seccfg
& secedit.exe /configure /db $env:windir\\security\\new.sdb /cfg $seccfg /areas SECURITYPOLICY >> $logfile
& cp $seccfg "c:\\"
& del $seccfg
$username="#{instance.transport[:username]}"
$password="#{instance.transport[:password]}"
"Creating static user: $username" >> $logfile
& net.exe user /y /add $username $password >> $logfile
"Adding $username to Administrators" >> $logfile
& net.exe localgroup Administrators /add $username >> $logfile
EOH
end
# Returning the fully constructed PowerShell script to user_data
Kitchen::Util.outdent!(<<-EOH)
<powershell>
#{base_script}
#{custom_admin_script}
</powershell>
EOH
end
def show_chosen_image
# Print some debug stuff
debug("Image for #{instance.name}: #{image.name}. #{image_info(image)}")
if actual_platform
info("Detected platform: #{actual_platform.name} version #{actual_platform.version}" \
" on #{actual_platform.architecture}. Instance Type: #{config[:instance_type]}." \
" Default username: #{actual_platform.username} (default).")
else
debug("No platform detected for #{image.name}.")
end
end
def image_info(image)
root_device = image.block_device_mappings
.find { |b| b.device_name == image.root_device_name }
volume_type = " #{root_device.ebs.volume_type}" if root_device && root_device.ebs
" Architecture: #{image.architecture}," \
" Virtualization: #{image.virtualization_type}," \
" Storage: #{image.root_device_type}#{volume_type}," \
" Created: #{image.creation_date}"
end
# Create a temporary security group for this instance.
#
# @api private
# @param state [Hash] Instance state hash.
# @return [void]
def create_security_group(state)
return if state[:auto_security_group_id]
# Work out which VPC, if any, we are creating in.
vpc_id = if config[:subnet_id]
# Get the VPC ID for the subnet.
subnets = ec2.client.describe_subnets(filters: [{ name: "subnet-id", values: [config[:subnet_id]] }]).subnets
raise "Subnet #{config[:subnet_id]} not found during security group creation" if subnets.empty?
subnets.first.vpc_id
elsif config[:subnet_filter]
subnets = ec2.client.describe_subnets(filters: [{ name: "tag:#{config[:subnet_filter][:tag]}", values: [config[:subnet_filter][:value]] }]).subnets
raise "Subnets with tag '#{config[:subnet_filter][:tag]}=#{config[:subnet_filter][:value]}' not found during security group creation" if subnets.empty?
subnets.first.vpc_id
else
# Try to check for a default VPC.
vpcs = ec2.client.describe_vpcs(filters: [{ name: "isDefault", values: ["true"] }]).vpcs
if vpcs.empty?
# No default VPC so assume EC2-Classic ¯\_(ツ)_/¯
nil
else
# I don't actually know if you can have more than one default VPC?
vpcs.first.vpc_id
end
end
# Create the SG.
params = {
group_name: "kitchen-#{Array.new(8) { rand(36).to_s(36) }.join}",
description: "Test Kitchen for #{instance.name} by #{Etc.getlogin || "nologin"} on #{Socket.gethostname}",
}
params[:vpc_id] = vpc_id if vpc_id
resp = ec2.client.create_security_group(params)
state[:auto_security_group_id] = resp.group_id
info("Created automatic security group #{state[:auto_security_group_id]}")
debug(" in VPC #{vpc_id || "none"}")
# Set up SG rules.
ec2.client.authorize_security_group_ingress(
group_id: state[:auto_security_group_id],
# Allow SSH and WinRM (both plain and TLS).
ip_permissions: [22, 3389, 5985, 5986].map do |port|
{
ip_protocol: "tcp",
from_port: port,
to_port: port,
ip_ranges: Array(config[:security_group_cidr_ip]).map do |cidr_ip|
{ cidr_ip: cidr_ip }
end,
}
end
)
end
# Create a temporary SSH key pair for this instance.
#
# @api private
# @param state [Hash] Instance state hash.
# @return [void]
def create_key(state)
return if state[:auto_key_id]
# Encode a bunch of metadata into the name because that's all we can
# set for a key pair.
name_parts = [
instance.name.gsub(/\W/, ""),
(Etc.getlogin || "nologin").gsub(/\W/, ""),
Socket.gethostname.gsub(/\W/, "")[0..20],
Time.now.utc.iso8601,
Array.new(8) { rand(36).to_s(36) }.join(""),
]
# In a perfect world this would generate the key locally and use ImportKey
# instead for better security, but given the use case that is very likely
# to rapidly exhaust local entropy by creating a lot of keys. So this is
# probably fine. If you want very high security, probably don't use this
# feature anyway.
resp = ec2.client.create_key_pair(key_name: "kitchen-#{name_parts.join("-")}")
state[:auto_key_id] = resp.key_name
info("Created automatic key pair #{state[:auto_key_id]}")
# Write the key out with safe permissions
key_path = "#{config[:kitchen_root]}/.kitchen/#{instance.name}.pem"
File.open(key_path, File::WRONLY | File::CREAT | File::EXCL, 00600) do |f|
f.write(resp.key_material)
end
# Inject the key into the state to be used by the SSH transport, or for
# the Windows password decrypt above in {#fetch_windows_admin_password}.
state[:ssh_key] = key_path
end
# Clean up a temporary security group for this instance.
#
# @api private
# @param state [Hash] Instance state hash.
# @return [void]
def delete_security_group(state)
return unless state[:auto_security_group_id]
info("Removing automatic security group #{state[:auto_security_group_id]}")
ec2.client.delete_security_group(group_id: state[:auto_security_group_id])
state.delete(:auto_security_group_id)
end
# Clean up a temporary SSH key pair for this instance.
#
# @api private
# @param state [Hash] Instance state hash.
# @return [void]
def delete_key(state)
return unless state[:auto_key_id]
info("Removing automatic key pair #{state[:auto_key_id]}")
ec2.client.delete_key_pair(key_name: state[:auto_key_id])
state.delete(:auto_key_id)
File.unlink("#{config[:kitchen_root]}/.kitchen/#{instance.name}.pem")
end
end
end
end