-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rake
137 lines (118 loc) · 3.58 KB
/
build.rake
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
require 'chef/cookbook/metadata'
require 'jsonlint/rake_task'
task default: [:build, 'build:destroy', 'build:cucumber']
task validate: ['validate:berks', 'validate:rubocop', 'validate:foodcritic',
'validate:reek', 'validate:jsonlint', 'validate:knife',
'validate:chefspec']
task build: [:validate, 'build:converge', 'build:verify']
task clean: ['build:destroy']
task deploy: ['deploy:delete', 'deploy:upload', 'berkshelf:delete']
###############################################################################
# validation tasks
###############################################################################
namespace :validate do
desc 'Runs Berks to gather cookbook dependencies'
task :berks do
FileUtils.rm_f('Berksfile.lock')
sh 'berks install'
end
desc 'Runs ChefSpec tests'
task :chefspec do
sh 'rspec'
end
desc 'Runs Foodcritic chef cookbook lint tool'
task :foodcritic do
sh 'foodcritic . --epic-fail any'
end
desc 'Runs Knife cookbook syntax error checks'
task :knife do
cb_path = File.realdirpath(File.dirname(Dir.pwd))
sh "knife cookbook test #{metadata.name} --cookbook-path #{cb_path}"
end
desc 'Runs Reek Code smell detector'
task :reek do
sh 'reek .'
end
desc 'Runs JsonLint static code analyzer'
JsonLint::RakeTask.new do |t|
t.paths = %w(
./**/*.json
)
end
desc 'Runs Rubocop static code analyzer'
task :rubocop do
sh 'rubocop . -a'
end
end
###############################################################################
# build tasks
###############################################################################
namespace :build do
desc 'Runs Kitchen converge steps'
task :converge do
sh 'kitchen converge'
end
desc 'Runs Kitchen verify steps'
task :verify do
sh 'kitchen verify'
end
desc 'Runs Kitchen destroy steps'
task :destroy do
sh 'kitchen destroy'
rm_rf '.kitchen'
end
desc 'Runs Cucumber acceptance tests'
task :cucumber do
sh 'cucumber'
end
end
###############################################################################
# deploy tasks
###############################################################################
namespace :deploy do
desc 'Deletes cookbook from the chef server'
task :delete do
cfg = metadata
if cookbook_exists?(cfg)
sh "knife cookbook delete #{cfg.name} #{cfg.version} -y"
end
end
desc 'Uploads cookbook to the chef server'
task upload: ['validate:berks'] do
fail 'cookbook exists, will not overwrite' if cookbook_exists?
sh 'berks upload --no-ssl-verify'
end
end
###############################################################################
# berkshelf tasks
###############################################################################
namespace :berkshelf do
desc 'Deletes cookbook from the berkshelf'
task :delete do
cfg = metadata
name = cfg.name
version = cfg.version
if in_berkshelf?(name, version)
sh "berks shelf uninstall #{name} -v #{version} -f"
end
end
end
###############################################################################
# functions
###############################################################################
def metadata
metadata = Chef::Cookbook::Metadata.new
metadata.from_file('metadata.rb')
metadata
end
def cookbook_exists?(cfg = metadata)
name = cfg.name
version = cfg.version
found = `knife cookbook list -a | awk '$1 ~/#{name}$/ && /#{version}/'`
found.length > 0
end
def in_berkshelf?(name, version)
sh "berks shelf show #{name} -v #{version}" do |ok, _res|
ok
end
end