Class: ChefApply::Startup
- Inherits:
-
Object
- Object
- ChefApply::Startup
show all
- Defined in:
- lib/chef_apply/startup.rb
Defined Under Namespace
Classes: ConfigPathInvalid, ConfigPathNotProvided, UnsupportedInstallation
Constant Summary
collapse
- T =
ChefApply::Text.cli
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(argv) ⇒ Startup
Returns a new instance of Startup.
28
29
30
31
32
33
34
|
# File 'lib/chef_apply/startup.rb', line 28
def initialize(argv)
@term_init = false
@argv = argv.clone
init_terminal
end
|
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
25
26
27
|
# File 'lib/chef_apply/startup.rb', line 25
def argv
@argv
end
|
Instance Method Details
#create_default_config ⇒ Object
114
115
116
117
118
119
|
# File 'lib/chef_apply/startup.rb', line 114
def create_default_config
UI::Terminal.output T.creating_config(Config.default_location)
UI::Terminal.output ""
FileUtils.mkdir_p(Config::WS_BASE_PATH)
FileUtils.touch(Config.default_location)
end
|
#custom_config_path ⇒ Object
Look for a user-supplied config path by manually parsing the option. Note that we can’t use Mixlib::CLI for this. To ensure that ChefApply::CLI initializes with correct option defaults, we need to have configuration loaded before initializing it.
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/chef_apply/startup.rb', line 172
def custom_config_path
argv.each_with_index do |arg, index|
if arg == "--config-path" || arg == "-c"
next_arg = argv[index + 1]
raise ConfigPathNotProvided.new if next_arg.nil?
raise ConfigPathInvalid.new(next_arg) unless File.file?(next_arg) && File.readable?(next_arg)
return next_arg
end
end
nil
end
|
#first_run_tasks ⇒ Object
107
108
109
110
111
112
|
# File 'lib/chef_apply/startup.rb', line 107
def first_run_tasks
return if Dir.exist?(Config::WS_BASE_PATH)
create_default_config
setup_telemetry
end
|
#init_terminal ⇒ Object
94
95
96
|
# File 'lib/chef_apply/startup.rb', line 94
def init_terminal
UI::Terminal.init($stdout)
end
|
#load_config ⇒ Object
162
163
164
165
166
|
# File 'lib/chef_apply/startup.rb', line 162
def load_config
path = custom_config_path
Config.custom_location(path) unless path.nil?
Config.load
end
|
#run(enforce_license: false) ⇒ Object
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
|
# File 'lib/chef_apply/startup.rb', line 36
def run(enforce_license: false)
verify_not_in_chefdk
first_run_tasks
setup_workstation_user_directories
setup_error_handling
load_config
setup_logging
start_telemeter_upload
start_chef_apply(enforce_license: enforce_license)
rescue ConfigPathInvalid => e
UI::Terminal.output(T.error.bad_config_file(e.path))
rescue ConfigPathNotProvided
UI::Terminal.output(T.error.missing_config_path)
rescue UnsupportedInstallation
UI::Terminal.output(T.error.unsupported_installation)
rescue Mixlib::Config::UnknownConfigOptionError => e
if e.message =~ /.*unsupported config value (.*)[.]+$/
UI::Terminal.output(T.error.invalid_config_key($1, Config.location))
else
UI::Terminal.output(T.error.unknown_config_error(e.message, Config.location))
end
rescue Tomlrb::ParseError => e
UI::Terminal.output(T.error.unknown_config_error(e.message, Config.location))
end
|
#setup_error_handling ⇒ Object
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/chef_apply/startup.rb', line 151
def setup_error_handling
Thread.report_on_exception = false
end
|
#setup_logging ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/chef_apply/startup.rb', line 185
def setup_logging
ChefApply::Log.setup(Config.log.location, Config.log.level.to_sym)
ChefApply::Log.info("Initialized logger")
ChefConfig.logger = ChefApply::Log
Chef::Log.init(ChefApply::Log.stream)
Chef::Log.level = ChefApply::Log.level
end
|
#setup_telemetry ⇒ Object
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/chef_apply/startup.rb', line 121
def setup_telemetry
require "securerandom" unless defined?(SecureRandom)
installation_id = SecureRandom.uuid
File.write(Config.telemetry_installation_identifier_file, installation_id)
UI::Terminal.output T.telemetry_enabled(Config.location)
UI::Terminal.output ""
end
|
#setup_workstation_user_directories ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/chef_apply/startup.rb', line 143
def setup_workstation_user_directories
FileUtils.mkdir_p(Config::WS_BASE_PATH)
FileUtils.mkdir_p(Config.base_log_directory)
FileUtils.mkdir_p(Config.telemetry_path)
end
|
#start_chef_apply(enforce_license: false) ⇒ Object
197
198
199
200
|
# File 'lib/chef_apply/startup.rb', line 197
def start_chef_apply(enforce_license: false)
require_relative "cli"
ChefApply::CLI.new(@argv).run(enforce_license: enforce_license)
end
|
#start_telemeter_upload ⇒ Object
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/chef_apply/startup.rb', line 132
def start_telemeter_upload
cfg = {
enabled: Config.telemetry[:enabled],
dev_mode: Config.telemetry[:dev_mode],
payload_dir: Config.telemetry_path,
installation_identifier_file: Config.telemetry_installation_identifier_file,
session_file: Config.telemetry_session_file,
}
Chef::Telemeter.setup(cfg)
end
|
#verify_not_in_chefdk ⇒ Object
Verify that chef-run gem is not executing out of ChefDK by checking the runtime path of this file.
NOTE: This is imperfect - someone could theoretically install chefdk to a path other than the default.
103
104
105
|
# File 'lib/chef_apply/startup.rb', line 103
def verify_not_in_chefdk
raise UnsupportedInstallation.new if script_path =~ /chefdk/
end
|