/
mstrap.cr
111 lines (98 loc) · 2.78 KB
/
mstrap.cr
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
require "colorize"
require "ecr"
require "file_utils"
require "hcl"
require "http/client"
require "json"
require "log"
require "openssl"
require "option_parser"
require "term-prompt"
require "uri"
require "yaml"
require "./mstrap/version"
require "./mstrap/paths"
require "./mstrap/errors"
require "./mstrap/dsl/*"
require "./mstrap/dsl"
require "./mstrap/platform/*"
require "./mstrap/platform"
require "./mstrap/cli_options"
require "./mstrap/def"
require "./mstrap/defs/**"
require "./mstrap/profile_fetcher"
require "./mstrap/user"
require "./mstrap/configuration"
require "./mstrap/supports/**"
require "./mstrap/web_bootstrapper"
require "./mstrap/runtime"
require "./mstrap/runtimes/**"
require "./mstrap/project"
require "./mstrap/step"
require "./mstrap/templates/**"
require "./mstrap/steps/**"
# :nodoc:
module Term
module Screen
def size_from_readline
# Always return default size to avoid linking to readline
DEFAULT_SIZE
end
end
end
# Defines top-level constants and shared utilities
module MStrap
Log = ::Log.for("mstrap")
LogFormatter = ::Log::Formatter.new do |entry, io|
if io.tty?
io << entry.message
else
label = entry.severity.none? ? "ANY" : entry.severity.to_s
io << "[" << entry.timestamp << " PID#" << Process.pid << "] "
io << label.rjust(5) << " -- : " << entry.message
end
end
@@debug = false
# Set debug mode for `mstrap`
def self.debug=(value)
@@debug = value
end
# Returns whether or not `mstrap` is in debug mode.
#
# NOTE: This is not the same as whether it was _compiled_ in debug mode.
def self.debug?
@@debug
end
# Sets up Log instance that can be used to log to the mstrap log file.
# When `MStrap.debug?` is set to `true`, this also logs messages to `STDOUT`.
def self.initialize_logger! : Nil
if debug?
FileUtils.mkdir_p(Paths::RC_DIR, 0o755)
log_file = File.new(MStrap::Paths::LOG_FILE, "a+")
writer = IO::MultiWriter.new(log_file, STDOUT)
backend = ::Log::IOBackend.new(writer)
backend.formatter = LogFormatter
::Log.builder.clear
::Log.builder.bind "*", :debug, backend
else
FileUtils.mkdir_p(Paths::RC_DIR, 0o755)
file = File.new(MStrap::Paths::LOG_FILE, "a+")
backend = ::Log::IOBackend.new(file)
backend.formatter = LogFormatter
::Log.builder.clear
::Log.builder.bind "*", :info, backend
end
nil
end
# Returns whether or not the `mstrap` environment file (`env.sh`) has been
# loaded into the environment.
def self.mstrapped?
ENV["MSTRAP"]? == "true"
end
# Returns a TLS client that uses a local version of the cURL CA bundle.
def self.tls_client
OpenSSL::SSL::Context::Client.new.tap do |client|
client.ca_certificates = Paths::CA_CERT_BUNDLE
end
end
end