Class: ConfCtl::Nix

Inherits:
Object
  • Object
show all
Includes:
Utils::File
Defined in:
lib/confctl/nix.rb

Class Method Summary collapse

Instance Method Summary collapse

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.



18
19
20
21
22
23
24
# File 'lib/confctl/nix.rb', line 18

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

.stateless(show_trace: false, max_jobs: 'auto') ⇒ Nix

Create a new instance without access to Settings, i.e. when called outside of cluster configuration directory.

Returns:



12
13
14
# File 'lib/confctl/nix.rb', line 12

def self.stateless(show_trace: false, max_jobs: 'auto')
  new(show_trace:, max_jobs:)
end

Instance Method Details

#activate(machine, toplevel, action) ⇒ Boolean

Parameters:

  • machine (Machine)
  • toplevel (String)
  • action (String)

Returns:

  • (Boolean)


258
259
260
261
262
# File 'lib/confctl/nix.rb', line 258

def activate(machine, toplevel, action)
  args = [File.join(toplevel, 'bin/switch-to-configuration'), action]

  MachineControl.new(machine).execute!(*args).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

Parameters:

  • hosts (Array<Machine>) (defaults to: [])
  • swpin_paths (Hash) (defaults to: {})
  • host_swpin_specs (Hash) (defaults to: {})
  • time (Time) (defaults to: nil)

Yield Parameters:

  • type (:build, :fetch)
  • progress (Integer)
  • total (Integer)
  • path (String)

Returns:



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
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/confctl/nix.rb', line 184

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_toplevels = JSON.parse(File.read(out_link))
      host_toplevels.each do |host, toplevel|
        host_generations = Generation::BuildList.new(host)
        generation = host_generations.find(toplevel, swpin_paths)

        if generation.nil?
          generation = Generation::Build.new(host)
          generation.create(
            toplevel,
            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

Parameters:

Yield Parameters:

Returns:

  • (Boolean)


310
311
312
313
# File 'lib/confctl/nix.rb', line 310

def collect_garbage(machine, &)
  gc = NixCollectGarbage.new(machine)
  gc.run!(&).success?
end

#confctl_settingsObject



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
# File 'lib/confctl/nix.rb', line 26

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, toplevel) {|progress, total, path| ... } ⇒ Boolean

Parameters:

  • machine (Machine)
  • toplevel (String)

Yield Parameters:

  • progress (Integer)
  • total (Integer)
  • path (String)

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
250
251
252
# File 'lib/confctl/nix.rb', line 242

def copy(machine, toplevel, &)
  if machine.localhost?
    true
  elsif machine.carried?
    cp = NixCopy.new(machine.carrier_machine.target_host, toplevel)
    cp.run!(&).success?
  else
    cp = NixCopy.new(machine.target_host, toplevel)
    cp.run!(&).success?
  end
end

#eval_core_swpinsHash

Evaluate swpins for host

Returns:

  • (Hash)


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
# File 'lib/confctl/nix.rb', line 113

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(host) ⇒ Hash

Evaluate swpins for host

Parameters:

  • host (String)

Returns:

  • (Hash)


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
# File 'lib/confctl/nix.rb', line 143

def eval_host_swpins(host)
  with_argument({
    confDir: conf_dir,
    build: :evalHostSwpins,
    machines: [host]
  }, core_swpins: true) do |arg|
    out_link = File.join(
      cache_dir,
      'build',
      "#{ConfCtl.safe_host_name(host)}.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))[host]
  end
end

#list_machine_fqdnsArray<String>

Returns an array with machine fqdns

Returns:

  • (Array<String>)


66
67
68
69
70
71
# File 'lib/confctl/nix.rb', line 66

def list_machine_fqdns
  nix_instantiate({
    confDir: conf_dir,
    build: :list
  })['machines']
end

#list_machinesHash

Return machines and their config in a hash

Returns:

  • (Hash)


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
# File 'lib/confctl/nix.rb', line 75

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_channelsObject



104
105
106
107
108
109
# File 'lib/confctl/nix.rb', line 104

def list_swpins_channels
  nix_instantiate({
    confDir: conf_dir,
    build: :listSwpinsChannels
  })
end

#module_optionsArray

Returns an array with options from all confctl modules

Returns:

  • (Array)


57
58
59
60
61
62
# File 'lib/confctl/nix.rb', line 57

def module_options
  options = nix_instantiate({
    confDir: conf_dir,
    build: :moduleOptions
  })
end

#run_command_in_shell(packages: [], command: nil) ⇒ Boolean

Parameters:

  • packages (Array<String>) (defaults to: [])
  • command (String) (defaults to: nil)

Returns:

  • (Boolean)


293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/confctl/nix.rb', line 293

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

Parameters:

  • machine (Machine)
  • toplevel (String)

Returns:

  • (Boolean)


280
281
282
283
284
285
286
287
288
# File 'lib/confctl/nix.rb', line 280

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

Parameters:

  • machine (Machine)
  • toplevel (String)

Returns:

  • (Boolean)


267
268
269
270
271
272
273
274
275
# File 'lib/confctl/nix.rb', line 267

def set_profile(machine, toplevel)
  args = [
    'nix-env',
    '-p', machine.profile,
    '--set', toplevel
  ]

  MachineControl.new(machine).execute!(*args).success?
end