Class: ConfCtl::Generation::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/generation/build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Build

Returns a new instance of Build.

Parameters:

  • host (String)


33
34
35
# File 'lib/confctl/generation/build.rb', line 33

def initialize(host)
  @host = host
end

Instance Attribute Details

#currentBoolean

Parameters:

  • current (Boolean)

Returns:

  • (Boolean)


30
31
32
# File 'lib/confctl/generation/build.rb', line 30

def current
  @current
end

#dateTime (readonly)

Returns:

  • (Time)


14
15
16
# File 'lib/confctl/generation/build.rb', line 14

def date
  @date
end

#hostString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/confctl/generation/build.rb', line 8

def host
  @host
end

#nameString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/confctl/generation/build.rb', line 11

def name
  @name
end

#swpin_namesArray<String> (readonly)

Returns:

  • (Array<String>)


20
21
22
# File 'lib/confctl/generation/build.rb', line 20

def swpin_names
  @swpin_names
end

#swpin_pathsHash (readonly)

Returns:

  • (Hash)


23
24
25
# File 'lib/confctl/generation/build.rb', line 23

def swpin_paths
  @swpin_paths
end

#swpin_specsHash (readonly)

Returns:

  • (Hash)


26
27
28
# File 'lib/confctl/generation/build.rb', line 26

def swpin_specs
  @swpin_specs
end

#toplevelString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/confctl/generation/build.rb', line 17

def toplevel
  @toplevel
end

Instance Method Details

#add_gcrootObject



105
106
107
108
109
110
# File 'lib/confctl/generation/build.rb', line 105

def add_gcroot
  GCRoot.add(gcroot_name('toplevel'), toplevel_path)
  swpin_paths.each_key do |name|
    GCRoot.add(gcroot_name("swpin.#{name}"), toplevel_path)
  end
end

#create(toplevel, swpin_paths, swpin_specs, date: nil) ⇒ Object

Parameters:

  • toplevel (String)
  • swpin_paths (Hash)
  • swpin_specs (Hash)
  • date (Time) (defaults to: nil)


41
42
43
44
45
46
47
48
# File 'lib/confctl/generation/build.rb', line 41

def create(toplevel, swpin_paths, swpin_specs, date: nil)
  @toplevel = toplevel
  @swpin_names = swpin_paths.keys
  @swpin_paths = swpin_paths
  @swpin_specs = swpin_specs
  @date = date || Time.now
  @name = date.strftime('%Y-%m-%d--%H-%M-%S')
end

#destroyObject



97
98
99
100
101
102
103
# File 'lib/confctl/generation/build.rb', line 97

def destroy
  remove_gcroot
  File.unlink(toplevel_path)
  swpin_paths.each_key { |name| File.unlink(swpin_path(name)) }
  File.unlink(config_path)
  Dir.rmdir(dir)
end

#dirObject



119
120
121
# File 'lib/confctl/generation/build.rb', line 119

def dir
  @dir ||= File.join(ConfDir.generation_dir, escaped_host, name)
end

#load(name) ⇒ Object

Parameters:

  • name (String)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/confctl/generation/build.rb', line 51

def load(name)
  @name = name

  cfg = JSON.parse(File.read(config_path))
  @toplevel = cfg['toplevel']

  @swpin_names = []
  @swpin_paths = {}
  @swpin_specs = {}

  cfg['swpins'].each do |swpin_name, swpin|
    @swpin_names << swpin_name
    @swpin_paths[swpin_name] = swpin['path']
    @swpin_specs[swpin_name] = Swpins::Spec.for(swpin['spec']['type'].to_sym).new(
      swpin_name,
      swpin['spec']['nix_options'],
      swpin['spec']
    )
  end

  @date = Time.iso8601(cfg['date'])
rescue StandardError => e
  raise Error, "invalid generation '#{name}': #{e.message}"
end

#remove_gcrootObject



112
113
114
115
116
117
# File 'lib/confctl/generation/build.rb', line 112

def remove_gcroot
  GCRoot.remove(gcroot_name('toplevel'))
  swpin_paths.each_key do |name|
    GCRoot.remove(gcroot_name("swpin.#{name}"))
  end
end

#saveObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/confctl/generation/build.rb', line 76

def save
  FileUtils.mkdir_p(dir)
  File.symlink(toplevel, toplevel_path)

  swpin_paths.each do |name, path|
    File.symlink(path, swpin_path(name))
  end

  File.open(config_path, 'w') do |f|
    f.puts(JSON.pretty_generate({
      date: date.iso8601,
      toplevel:,
      swpins: swpin_paths.to_h do |name, path|
        [name, { path:, spec: swpin_specs[name].as_json }]
      end
    }))
  end

  add_gcroot
end