-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathRakefile
More file actions
131 lines (114 loc) · 3.52 KB
/
Rakefile
File metadata and controls
131 lines (114 loc) · 3.52 KB
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
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'time'
require_relative 'lib/oxidized/version'
gemspec = Gem::Specification.load(Dir['*.gemspec'].first)
gemfile = [gemspec.name, gemspec.version].join('-') + '.gem'
# Integrate Rubocop if available
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
rescue LoadError
task :rubocop do
puts 'Install rubocop to run its rake tasks'
end
end
desc 'Validate gemspec'
task :gemspec do
gemspec.validate
end
desc 'Run minitest'
task :test do
Rake::TestTask.new do |t|
t.libs << 'spec'
t.test_files = FileList['spec/**/*_spec.rb']
t.ruby_opts = ['-W:deprecated']
# Don't display ambiguity warning between regexp and division in models
t.warning = false
t.verbose = true
end
end
task build: %i[chmod version_set]
desc 'Set Gem Version'
task :version_set do
Oxidized.version_set
Bundler::GemHelper.instance.gemspec.version = Oxidized::VERSION
end
desc 'Remove gems'
task :clean do
FileUtils.rm_rf 'pkg'
end
desc 'Tag the release'
task :tag do
system "git tag #{gemspec.version} -m 'Release #{gemspec.version}'"
end
desc 'Push to rubygems'
task push: :tag do
system "gem push pkg/#{gemfile}"
end
desc 'Normalise file permissions'
task :chmod do
xbit = %w[
bin/oxidized
bin/console
extra/auto-reload-config.runit
extra/nagios_check_failing_nodes.rb
extra/oxidized-report-git-commits
extra/oxidized.init
extra/oxidized.init.d
extra/oxidized.runit
extra/syslog.rb
extra/update-ca-certificates.runit
extra/device2yaml.rb
]
dirs = []
%x(git ls-files -z).split("\x0").reject { |f| f.match(/^(test|spec|features)\//) }.each do |file|
dirs.push(File.dirname(file))
xbit.include?(file) ? File.chmod(0o0755, file) : File.chmod(0o0644, file)
end
dirs.sort.uniq.each { |dir| File.chmod(0o0755, dir) }
end
# Build the container image with docker or podman
def command_available?(command)
system("which #{command} > /dev/null 2>&1")
end
def docker_needs_root?
!system('docker info > /dev/null 2>&1')
end
desc 'Build the container image with docker or podman'
task :build_container do
branch_name = %x(git rev-parse --abbrev-ref HEAD).chop.gsub '/', '_'
sha_hash = %x(git rev-parse --short HEAD).chop
sha_hash_long = %x(git rev-parse HEAD).chop
image_tag = "#{branch_name}-#{sha_hash}"
created_time = Time.now.iso8601
# Build-Args for consistent labels
build_args = [
"--label org.opencontainers.image.title=oxidized",
"--label org.opencontainers.image.description='Local build of Oxidized'",
"--label org.opencontainers.image.url=https://github.com/ytti/oxidized",
"--label org.opencontainers.image.source=https://github.com/ytti/oxidized",
"--label org.opencontainers.image.created=#{created_time}",
"--label org.opencontainers.image.ref.name=#{image_tag}",
"--label org.opencontainers.image.licenses=Apache-2.0",
"--label org.opencontainers.image.version=#{image_tag}",
"--label org.opencontainers.image.revision=#{sha_hash_long}",
"-t oxidized:#{image_tag}",
"-t oxidized:latest"
].join(' ')
# Prefer podman if available as it runs rootless
if command_available?('podman')
sh "podman build #{build_args} ."
elsif command_available?('docker')
if docker_needs_root?
puts 'docker needs root to build the image. Using sudo...'
sh "sudo docker build #{build_args} ."
else
sh "docker build #{build_args} ."
end
else
puts 'You need Podman or Docker to build the container image.'
exit 1
end
end
task default: %i[rubocop test]