-
Notifications
You must be signed in to change notification settings - Fork 0
/
rakefile.rb
142 lines (125 loc) · 4.02 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
# rubocop:disable Metrics/BlockLength
task default: %i[version rubocop foodcritic spec kitchen cleanup]
task nokitchen: %i[version rubocop foodcritic spec]
desc 'Set cookbook version.'
task :version do
version = ENV['BUILD_NUMBER'] ? "0.2.#{ENV['BUILD_NUMBER']}" : '0.2.1'
IO.write('version.txt', version)
end
desc 'Foodcritic test.'
task :foodcritic do
load_gems('colorize')
puts 'Running Foodcritic tests...'.green
sh 'chef exec foodcritic . -G -f any'
end
desc 'Rubocop test.'
task :rubocop do
load_gems('colorize')
puts 'Running Rubocop tests...'.green
sh 'chef exec rubocop'
end
desc 'ChefSpec test.'
task :spec do
load_gems('colorize minitest-chef-handler')
puts 'Running ChefSpec tests...'.green
cmd = 'chef exec rspec --color -f documentation ' \
'-tunit --pattern "spec/**/*_spec.rb"'
sh cmd.to_s
end
desc 'Test Kitchen.'
task :kitchen, [:type] => %i[check_env_vars assume_role] do |_t, args|
task_thread = []
kitchen_complete = false
start_time = Time.now
%w[kitchen assume_role].each do |task|
task_thread << Thread.new do
case task
when 'kitchen'
load_gems('colorize')
puts 'Running Test Kitchen...'.green
type = args[:type] || 'test'
begin
sh "chef exec kitchen #{type} -c"
rescue StandardError => e
Rake::Task[:cleanup].invoke if type == 'test'
raise e.inspect
end
kitchen_complete = true
when 'assume_role'
while kitchen_complete == false
current_time = Time.now
elapsed_time = (current_time.to_f - start_time.to_f).to_i
if elapsed_time > 3000
Rake::Task[:assume_role].reenable
Rake::Task[:assume_role].invoke
start_time = Time.now
end
sleep 10
end
end
end
end
task_thread.each(&:join)
end
desc 'Cleanup after yourself!'
task :cleanup, [:cookbook_name] => [:assume_role] do |_t, args|
load_gems('colorize')
puts "Clean up AWS in case test kitchen doesn't. . .".green
cookbook_name = args.cookbook_name || File.basename(Dir.getwd)
instance_cmd = 'aws ec2 describe-instances --filters ' \
"\"Name=tag:Name,Values=#{cookbook_name}\" " \
'"Name=instance-state-name,Values=running"'
instance = `#{instance_cmd}`
unless JSON.parse(instance)['Reservations'].empty?
id = JSON.parse(instance)['Reservations'][0]['Instances'][0]['InstanceId']
sh "aws ec2 terminate-instances --instance-ids #{id}"
end
puts "Cookbook #{cookbook_name} is cleaned up.".green
end
desc 'AWS Assume Role.'
task assume_role: :check_env_vars do
load_gems('json colorize')
puts "Assuming role for #{ENV['EC2_ROLE_ARN']}".green
assume_role_cmd = 'aws sts assume-role ' \
"--role-arn=#{ENV['EC2_ROLE_ARN']} " \
'--role-session-name=temp_session'
data = JSON.parse(`#{assume_role_cmd}`)
access_key = data['Credentials']['AccessKeyId']
secret_key = data['Credentials']['SecretAccessKey']
token = data['Credentials']['SessionToken']
system "aws configure set aws_access_key_id #{access_key}"
system "aws configure set aws_secret_access_key #{secret_key}"
system "aws configure set aws_session_token #{token}"
system "aws configure set region #{ENV['EC2_REGION']}"
ENV['AWS_ACCESS_KEY_ID'] = nil
ENV['AWS_SECRET_ACCESS_KEY'] = nil
puts "Role '#{ENV['EC2_ROLE_ARN']}' has been assumed.".green
end
desc 'Verify environment variables are set.'
task :check_env_vars do
load_gems('colorize')
@vars = []
@file = File.new('docker-compose.yml', 'r')
while (line = @file.gets)
@vars << line.scan(/^\s+\b[A-Z0-9_]+:\s+\${(\w+)/).join('')
end
req_env_vars = @vars.reject(&:empty?)
msg = ''
req_env_vars.uniq.each do |env|
next if env.include? '_DIR'
msg += "\n#{env}" if ENV[env].to_s.empty?
end
raise 'Missing Environment Variables. . .'.red + msg.red unless msg.empty?
end
private
def load_gems(gems)
gems.split(' ').each do |g|
begin
gem g
rescue Gem::LoadError
system "gem install #{g} --no-document"
Gem.clear_paths
end
require g
end
end