Class: ConfCtl::Nix
Class Method Summary collapse
-
.stateless(show_trace: false, max_jobs: 'auto') ⇒ Nix
Create a new instance without access to Settings, i.e.
Instance Method Summary collapse
- #activate(machine, generation, action) ⇒ Boolean
- #activate_with_rollback(machine, generation, action) ⇒ Boolean
-
#build_attributes(hosts: [], swpin_paths: {}, host_swpin_specs: {}, time: nil) {|type, progress, total, path| ... } ⇒ Hash<String, Generation::Build>
Build config.system.build.toplevel for selected hosts.
- #collect_garbage(machine) {|progress| ... } ⇒ Boolean
- #confctl_settings ⇒ Object
- #copy(machine, paths) {|progress, total, path| ... } ⇒ Boolean
-
#eval_core_swpins ⇒ Hash
Evaluate swpins for host.
-
#eval_host_swpins(hosts) ⇒ Hash
Evaluate swpins for hosts.
-
#initialize(conf_dir: nil, show_trace: false, max_jobs: nil, cores: nil) ⇒ Nix
constructor
A new instance of Nix.
-
#list_machine_fqdns ⇒ Array<String>
Returns an array with machine fqdns.
-
#list_machines ⇒ Hash
Return machines and their config in a hash.
- #list_swpins_channels ⇒ Object
-
#module_options ⇒ Array
Returns an array with options from all confctl modules.
- #run_command_in_shell(packages: [], command: nil) ⇒ Boolean
- #set_carried_profile(machine, toplevel) ⇒ Boolean
- #set_profile(machine, toplevel) ⇒ Boolean
Methods included from Utils::File
#replace_symlink, #unlink_if_exists
Constructor Details
#initialize(conf_dir: nil, show_trace: false, max_jobs: nil, cores: nil) ⇒ Nix
Returns a new instance of Nix.
19 20 21 22 23 24 25 |
# File 'lib/confctl/nix.rb', line 19 def initialize(conf_dir: nil, show_trace: false, max_jobs: nil, cores: nil) @conf_dir = conf_dir || ConfDir.path @show_trace = show_trace @max_jobs = max_jobs || Settings.instance.max_jobs @cores = cores @cmd = SystemCommand.new end |
Class Method Details
Instance Method Details
#activate(machine, generation, action) ⇒ Boolean
260 261 262 263 264 |
# File 'lib/confctl/nix.rb', line 260 def activate(machine, generation, action) args = [File.join(generation.toplevel, 'bin/switch-to-configuration'), action] MachineControl.new(machine).execute!(*args).success? end |
#activate_with_rollback(machine, generation, action) ⇒ Boolean
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/confctl/nix.rb', line 270 def activate_with_rollback(machine, generation, action) check_file = File.join('/run', "confctl-confirm-#{SecureRandom.hex(3)}") timeout = machine['autoRollback']['timeout'] logger = NullLogger.new args = [ generation.auto_rollback, '-t', timeout, generation.toplevel, action, check_file ] activation_success = nil activation_thread = Thread.new do activation_success = MachineControl.new(machine).execute!(*args).success? end # Wait for the configuration to be switched t = Time.now loop do out, = MachineControl.new(machine, logger:).execute!('cat', check_file, '2>/dev/null') stripped = out.strip break if stripped == 'switched' || ((t + timeout + 10) < Time.now && stripped != 'switching') sleep(1) end # Confirm it 10.times do break if MachineControl.new(machine, logger:).execute!('sh', '-c', "'echo confirmed > #{check_file}'").success? end activation_thread.join activation_success end |
#build_attributes(hosts: [], swpin_paths: {}, host_swpin_specs: {}, time: nil) {|type, progress, total, path| ... } ⇒ Hash<String, Generation::Build>
Build config.system.build.toplevel for selected hosts
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/confctl/nix.rb', line 185 def build_attributes(hosts: [], swpin_paths: {}, host_swpin_specs: {}, time: nil, &block) with_argument({ confDir: conf_dir, build: :toplevel, machines: hosts }) do |arg| time ||= Time.now ret_generations = {} out_link = File.join(cache_dir, 'build', "#{SecureRandom.hex(4)}.build") cmd_args = [ '--arg', 'jsonArg', arg, '--out-link', out_link, (show_trace ? '--show-trace' : nil), (max_jobs ? ['--max-jobs', max_jobs.to_s] : nil), (cores ? ['--cores', cores.to_s] : nil), ConfCtl.nix_asset('evaluator.nix') ].flatten.compact nb = NixBuild.new(cmd_args, swpin_paths) nb.run(&block) begin host_results = JSON.parse(File.read(out_link)) host_results.each do |host, result| host_generations = Generation::BuildList.new(host) generation = host_generations.find(result['attribute'], swpin_paths) if generation.nil? generation = Generation::Build.new(host) generation.create( result['attribute'], result['autoRollback'], swpin_paths, host_swpin_specs[host], date: time ) generation.save end host_generations.current = generation ret_generations[host] = generation end ensure unlink_if_exists(out_link) end ret_generations end end |
#collect_garbage(machine) {|progress| ... } ⇒ Boolean
355 356 357 358 |
# File 'lib/confctl/nix.rb', line 355 def collect_garbage(machine, &) gc = NixCollectGarbage.new(machine) gc.run!(&).success? end |
#confctl_settings ⇒ Object
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 |
# File 'lib/confctl/nix.rb', line 27 def confctl_settings out_link = File.join( cache_dir, 'build', 'settings.json' ) with_cache(out_link) do with_argument({ confDir: conf_dir, build: :confctl }) do |arg| cmd_args = [ 'nix-build', '--arg', 'jsonArg', arg, '--out-link', out_link, (show_trace ? '--show-trace' : nil), (max_jobs ? ['--max-jobs', max_jobs.to_s] : nil), (cores ? ['--cores', cores.to_s] : nil), ConfCtl.nix_asset('evaluator.nix') ].flatten.compact cmd.run(*cmd_args) end end demodulify(JSON.parse(File.read(out_link))['confctl']) end |
#copy(machine, paths) {|progress, total, path| ... } ⇒ Boolean
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/confctl/nix.rb', line 244 def copy(machine, paths, &) if machine.localhost? true elsif machine.carried? cp = NixCopy.new(machine.carrier_machine.target_host, paths) cp.run!(&).success? else cp = NixCopy.new(machine.target_host, paths) cp.run!(&).success? end end |
#eval_core_swpins ⇒ Hash
Evaluate swpins for host
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 |
# File 'lib/confctl/nix.rb', line 114 def eval_core_swpins with_argument({ confDir: conf_dir, build: :evalCoreSwpins }) do |arg| out_link = File.join( cache_dir, 'build', 'core.swpins' ) cmd_args = [ 'nix-build', '--arg', 'jsonArg', arg, '--out-link', out_link, (show_trace ? '--show-trace' : nil), (max_jobs ? ['--max-jobs', max_jobs.to_s] : nil), (cores ? ['--cores', cores.to_s] : nil), ConfCtl.nix_asset('evaluator.nix') ].flatten.compact out, = cmd.run(*cmd_args).stdout JSON.parse(File.read(out.strip)) end end |
#eval_host_swpins(hosts) ⇒ Hash
Evaluate swpins for hosts
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 |
# File 'lib/confctl/nix.rb', line 144 def eval_host_swpins(hosts) with_argument({ confDir: conf_dir, build: :evalHostSwpins, machines: hosts }, core_swpins: true) do |arg| out_link = File.join( cache_dir, 'build', "#{Digest::SHA256.hexdigest(hosts.join(','))[0..11]}.swpins" ) cmd_args = [ 'nix-build', '--arg', 'jsonArg', arg, '--out-link', out_link, (show_trace ? '--show-trace' : nil), (max_jobs ? ['--max-jobs', max_jobs.to_s] : nil), (cores ? ['--cores', cores.to_s] : nil), ConfCtl.nix_asset('evaluator.nix') ].flatten.compact out, = cmd.run(*cmd_args) JSON.parse(File.read(out.strip)) end end |
#list_machine_fqdns ⇒ Array<String>
Returns an array with machine fqdns
67 68 69 70 71 72 |
# File 'lib/confctl/nix.rb', line 67 def list_machine_fqdns nix_instantiate({ confDir: conf_dir, build: :list })['machines'] end |
#list_machines ⇒ Hash
Return machines and their config in a hash
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 |
# File 'lib/confctl/nix.rb', line 76 def list_machines out_link = File.join( cache_dir, 'build', 'machine-list.json' ) with_cache(out_link) do with_argument({ confDir: conf_dir, build: :info }, core_swpins: true) do |arg| cmd_args = [ 'nix-build', '--arg', 'jsonArg', arg, '--out-link', out_link, (show_trace ? '--show-trace' : nil), (max_jobs ? ['--max-jobs', max_jobs.to_s] : nil), (cores ? ['--cores', cores.to_s] : nil), ConfCtl.nix_asset('evaluator.nix') ].flatten.compact cmd.run(*cmd_args) end end demodulify(JSON.parse(File.read(out_link))) end |
#list_swpins_channels ⇒ Object
105 106 107 108 109 110 |
# File 'lib/confctl/nix.rb', line 105 def list_swpins_channels nix_instantiate({ confDir: conf_dir, build: :listSwpinsChannels }) end |
#module_options ⇒ Array
Returns an array with options from all confctl modules
58 59 60 61 62 63 |
# File 'lib/confctl/nix.rb', line 58 def = nix_instantiate({ confDir: conf_dir, build: :moduleOptions }) end |
#run_command_in_shell(packages: [], command: nil) ⇒ Boolean
338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/confctl/nix.rb', line 338 def run_command_in_shell(packages: [], command: nil) args = ['nix-shell'] if packages.any? args << '-p' args.concat(packages) end args << '--command' args << command cmd.run!(*args, env: { 'shellHook' => nil }).success? end |
#set_carried_profile(machine, toplevel) ⇒ Boolean
325 326 327 328 329 330 331 332 333 |
# File 'lib/confctl/nix.rb', line 325 def set_carried_profile(machine, toplevel) args = [ 'carrier-env', '-p', machine.profile, '--set', toplevel ] MachineControl.new(machine.carrier_machine).execute!(*args).success? end |
#set_profile(machine, toplevel) ⇒ Boolean
312 313 314 315 316 317 318 319 320 |
# File 'lib/confctl/nix.rb', line 312 def set_profile(machine, toplevel) args = [ 'nix-env', '-p', machine.profile, '--set', toplevel ] MachineControl.new(machine).execute!(*args).success? end |