Repository navigation
Expand file tree
/
Copy pathsetup.rb
More file actions
executable file
·219 lines (189 loc) · 6.66 KB
/
Copy pathsetup.rb
File metadata and controls
executable file
·219 lines (189 loc) · 6.66 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
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require "optparse"
require "rbconfig"
ROOT = File.expand_path(__dir__)
def git_capture(*args)
output = IO.popen(["git", "-C", ROOT, *args], err: File::NULL, &:read)
$?.success? ? output.strip : nil
end
def git_success?(*args)
system("git", "-C", ROOT, *args, out: File::NULL, err: File::NULL)
end
def auto_update_checkout
return unless Process.uid.zero?
return if ENV["DEV_BOXER_SKIP_AUTO_UPDATE"] == "1"
return unless Dir.exist?(File.join(ROOT, ".git"))
branch = git_capture("rev-parse", "--abbrev-ref", "HEAD")
return if branch.nil? || branch.empty? || branch == "HEAD"
unless git_success?("diff", "--quiet") && git_success?("diff", "--cached", "--quiet")
warn "Dev Boxer checkout has local modifications; skipping auto-update."
return
end
before = git_capture("rev-parse", "HEAD")
return unless before
unless git_success?("pull", "--ff-only", "--quiet", "origin", branch)
warn "Could not auto-update Dev Boxer; continuing with the current checkout."
return
end
after = git_capture("rev-parse", "HEAD")
return if after.nil? || after == before
warn "Updated Dev Boxer to #{after[0, 7]}; restarting setup."
ENV["DEV_BOXER_SKIP_AUTO_UPDATE"] = "1"
exec RbConfig.ruby, File.join(ROOT, "setup.rb"), *ARGV
end
auto_update_checkout
$LOAD_PATH.unshift(File.join(ROOT, "lib"))
require "dev_boxer"
DEFAULT_CONFIG = File.join(ROOT, "config.yml")
options = {
config: DEFAULT_CONFIG,
modules_dir: File.join(ROOT, "lib", "dev_boxer", "modules"),
templates_dir: File.join(ROOT, "templates"),
skip: [],
non_interactive: false,
reconfigure: false,
wizard_only: false,
}
config_explicit = false
parser = OptionParser.new do |opts|
opts.banner = "Usage: setup.rb [options]"
opts.on("--dry-run", "List modules without running them") { options[:dry_run] = true }
opts.on("--only NAME", "Run only this module") { |v| options[:only] = v }
opts.on("--from NAME", "Start from this module and run subsequent") { |v| options[:from] = v }
opts.on("--skip NAME", "Skip this module (repeatable)") { |v| options[:skip] << v }
opts.on("--config PATH", "Path to config.yml (default: ./config.yml)") { |v| options[:config] = v; config_explicit = true }
opts.on("--modules-dir DIR","Path to modules directory") { |v| options[:modules_dir] = v }
opts.on("--templates-dir DIR","Path to templates directory") { |v| options[:templates_dir] = v }
opts.on("--wizard-only", "Run first-run setup wizard, then exit") { options[:wizard_only] = true }
opts.on("--reconfigure", "Force the setup wizard before running") { options[:reconfigure] = true }
opts.on("--non-interactive","Fail instead of prompting for config") { options[:non_interactive] = true }
opts.on("-h", "--help", "Show this help") { puts opts; exit 0 }
end
begin
parser.parse!
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
warn e.message
warn parser
exit 2
end
log = DevBoxer::Log.new
interactive = $stdin.tty? && $stdout.tty?
run_wizard = lambda do |force: false|
if options[:non_interactive] || !interactive
warn "Config file is missing or incomplete: #{options[:config]}"
warn "Run setup interactively first, or create config.yml and secrets.yml manually."
exit 2
end
DevBoxer::Wizard.run(config_path: options[:config], force: force)
rescue DevBoxer::Wizard::Prompter::InputClosed => e
warn e.message
exit 2
end
load_config = lambda do
DevBoxer::Config.load(options[:config])
end
if options[:wizard_only]
run_wizard.call(force: options[:reconfigure])
begin
DevBoxer::Config.validate!(load_config.call)
rescue DevBoxer::Config::Invalid => e
warn e.message
exit 2
end
exit 0
end
run_wizard.call(force: true) if options[:reconfigure]
config =
if File.exist?(options[:config])
load_config.call
elsif config_explicit
warn "Config file not found: #{options[:config]}"
exit 2
else
run_wizard.call
load_config.call
end
unless options[:dry_run]
errors = DevBoxer::Config.validation_errors(config)
unless errors.empty?
if options[:non_interactive] || !interactive
warn "Config is incomplete:"
errors.each { |error| warn "- #{error}" }
exit 2
end
warn "Config is incomplete; starting first-run setup."
run_wizard.call(force: true)
config = load_config.call
begin
DevBoxer::Config.validate!(config)
rescue DevBoxer::Config::Invalid => e
warn e.message
exit 2
end
end
end
mods = DevBoxer::Modules.discover(options[:modules_dir])
secrets_path = DevBoxer::Config.secrets_path_for(options[:config])
preflight = DevBoxer::Preflight.new(
config: config,
shell: DevBoxer::Shell.new,
log: log,
interactive: interactive && !options[:non_interactive],
secrets_path: secrets_path,
)
runner = DevBoxer::Runner.new(
modules: mods,
config: config,
log: log,
templates_dir: options[:templates_dir],
config_path: options[:config],
secrets_path: secrets_path,
interactive: interactive && !options[:non_interactive],
preflight: preflight,
)
begin
runner.run(
only: options[:only],
from: options[:from],
skip: options[:skip],
dry_run: options[:dry_run],
)
rescue DevBoxer::Runner::UnknownModule => e
warn e.message
exit 1
rescue DevBoxer::SetupError => e
# A check before the first step refused something; it says what to do.
lines = e.message.lines.map(&:chomp)
log.error(lines.shift)
lines.each { |line| log.info(" #{line}") }
exit 1
rescue DevBoxer::Runner::ModuleFailed => e
# Print what went wrong and how to resume, not a backtrace; the backtrace
# goes to a log file for whoever needs it.
error = e.cause || e
lines = error.message.lines.map(&:chomp)
log_path = File.join(File.dirname(options[:config]), "last-error.log")
begin
File.write(log_path, "#{Time.now.utc} module #{e.module_name}\n#{error.class}: #{error.message}\n" \
"#{Array(error.backtrace).join("\n")}\n")
rescue SystemCallError
log_path = nil
end
if error.is_a?(DevBoxer::SetupError)
log.error(lines.shift)
else
log.error("The #{e.module_name} step failed: #{lines.shift}")
lines = lines.last(40)
end
lines.each { |line| log.info(" #{line}") }
unless error.is_a?(DevBoxer::SetupError)
log.info(" Next step: fix the cause above, then resume with: #{e.resume_command}")
log.info(" Every step is safe to re-run.")
end
log.info(" (Details for a bug report: #{log_path})") if log_path
exit 1
rescue DevBoxer::Wizard::Prompter::InputClosed => e
warn e.message
exit 2
end