This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
98 lines (84 loc) · 2.78 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
# encoding: utf-8
require 'cookstyle'
require 'rubocop/rake_task'
require 'rspec/core/rake_task'
require 'base64'
require 'chef/cookbook/metadata'
# General tasks
# Cookstyle before rspec so we don't lint vendored cookbooks
desc 'Run all tests except Kitchen (default task)'
task default: %i(lint spec)
# Lint the cookbook
desc 'Run all linters: cookstyle'
task lint: %i(cookstyle)
# Run the whole shebang
desc 'Run all tests'
task test: %i(lint kitchen spec)
# RSpec
desc 'Run chefspec tests'
task :spec do
puts 'Running Chefspec tests'
RSpec::Core::RakeTask.new(:spec)
end
# Rubocop
desc 'Run Cookstyle lint checks'
task :cookstyle do
begin
require 'cookstyle'
require 'rubocop/rake_task'
RuboCop::RakeTask.new(:cookstyle) do |task|
# If we are in CI mode then add formatter options
task.options.concat %w(
--display-cop-names
) if ENV['CI']
# --require rubocop/formatter/checkstyle_formatter
# --format RuboCop::Formatter::CheckstyleFormatter
# -o reports/xml/checkstyle-result.xml
end
rescue
puts ">>> Gem load error: #{e}, omitting style:cookstyle" unless ENV['CI']
end
end
# Automatically generate a changelog for this project. Only loaded if
# the necessary gem is installed.
begin
# read version from metadata
metadata = Chef::Cookbook::Metadata.new
metadata.instance_eval(File.read('metadata.rb'))
# build changelog
require 'github_changelog_generator/task'
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
config.future_release = "v#{metadata.version}"
config.user = 'dev-sec'
config.project = 'chef-os-hardening'
end
rescue LoadError
puts '>>>>> GitHub Changelog Generator not loaded, omitting tasks'
end
desc 'Run kitchen integration tests'
task :kitchen do
concurrency = ENV['CONCURRENCY'] || 1
instance = ENV['INSTANCE'] || ''
args = ENV['CI'] ? '--destroy=always' : ''
sh('sh', '-c', "bundle exec kitchen test -c #{concurrency} #{args} #{instance}")
end
desc 'Run kitchen integration tests on AWS EC2'
task :kitchen_ec2 do
concurrency = ENV['CONCURRENCY'] || 4
instance = ENV['INSTANCE'] || ''
args = ENV['CI'] ? '--destroy=always' : ''
sh('sh', '-c', "bundle exec kitchen test -l info -c #{concurrency} #{args} #{instance}")
end
desc 'Prepare CI environment for DigitalOcean usage'
task :prepare_do_env do
SSH_KEY_FILE = '~/.ssh/ci_id_rsa'.freeze
ENV_VAR_NAME = 'CI_SSH_KEY'.freeze
['DIGITALOCEAN_ACCESS_TOKEN', 'DIGITALOCEAN_SSH_KEY_IDS', ENV_VAR_NAME].each do |var|
raise "Environment variable #{var} should be set" unless ENV[var]
end
ssh_file = File.expand_path(SSH_KEY_FILE)
dir = File.dirname(ssh_file)
Dir.mkdir(dir, 0o700) unless Dir.exist?(dir)
File.open(ssh_file, 'w') { |f| f.puts Base64.decode64(ENV[ENV_VAR_NAME]) }
File.chmod(0o600, ssh_file)
end