Class: Messhy::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/messhy/installer.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, dry_run: false) ⇒ Installer

Returns a new instance of Installer.



14
15
16
17
18
19
20
# File 'lib/messhy/installer.rb', line 14

def initialize(config, dry_run: false)
  @config = config
  @dry_run = dry_run
  @ssh_executor = SSHExecutor.new(config)
  @node_keys = load_existing_keys
  @psk_map = load_existing_psks
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/messhy/installer.rb', line 12

def config
  @config
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



12
13
14
# File 'lib/messhy/installer.rb', line 12

def dry_run
  @dry_run
end

#ssh_executorObject (readonly)

Returns the value of attribute ssh_executor.



12
13
14
# File 'lib/messhy/installer.rb', line 12

def ssh_executor
  @ssh_executor
end

Instance Method Details

#generate_keys(skip: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/messhy/installer.rb', line 97

def generate_keys(skip: nil)
  puts '==> Generating WireGuard keys (no deploy)'
  config.validate!
  puts "\n==> Installing WireGuard on nodes..."
  install_wireguard_on_all_nodes(skip: skip)
  puts "\n==> Generating WireGuard keys..."
  generate_all_keys(skip: skip)
  puts "✓ Keys stored in #{secrets_dir}" unless dry_run
end

#purge_all(skip: nil) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/messhy/installer.rb', line 123

def purge_all(skip: nil)
  config.each_node do |node_name, _|
    next if skip && node_name == skip

    ssh_executor.purge_wireguard(node_name)
  end
end

#restart_all(skip: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/messhy/installer.rb', line 113

def restart_all(skip: nil)
  puts '==> Restarting WireGuard on all nodes...'
  config.each_node do |node_name, _|
    next if skip && node_name == skip

    ssh_executor.restart_wireguard(node_name)
  end
  puts '✓ All nodes restarted'
end

#restart_node(node_name) ⇒ Object



107
108
109
110
111
# File 'lib/messhy/installer.rb', line 107

def restart_node(node_name)
  puts "==> Restarting WireGuard on: #{node_name}"
  ssh_executor.restart_wireguard(node_name)
  puts '✓ Restarted'
end

#setup(skip: nil) ⇒ Object



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
# File 'lib/messhy/installer.rb', line 22

def setup(skip: nil)
  puts '==> Setting up WireGuard mesh network'
  puts "Environment: #{config.environment}"
  puts "Nodes: #{config.node_names.join(', ')}"
  puts

  # Validate config
  config.validate!

  # Purge existing WireGuard configs
  puts '==> Cleaning up existing WireGuard installations...'
  purge_all(skip: skip) unless dry_run

  # Install WireGuard on all nodes (ensures wg binary exists for keygen)
  puts "\n==> Installing WireGuard on nodes..."
  install_wireguard_on_all_nodes(skip: skip)

  # Generate keys for all nodes
  puts "\n==> Generating WireGuard keys..."
  generate_all_keys(skip: skip)

  # Build configs
  puts "\n==> Building mesh configurations..."
  mesh_builder = MeshBuilder.new(config, @node_keys, @psk_map || {})
  configs = mesh_builder.build_all_configs

  # Upload configs and start WireGuard
  puts "\n==> Deploying configurations..."
  deploy_configs(configs, skip: skip)

  # Force restart all nodes
  puts "\n==> Restarting WireGuard on all nodes..."
  restart_all(skip: skip) unless dry_run

  # Verify connectivity
  puts "\n==> Verifying mesh connectivity..."
  verify_mesh(skip: skip)

  if config.dns_enabled?
    puts "\n==> Setting up mesh DNS..."
    DnsManager.new(config, ssh_executor: ssh_executor, dry_run: dry_run, skip: skip).setup
  end

  puts "\n✓ WireGuard mesh setup complete!"
end

#setup_node(node_name) ⇒ Object

Raises:



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
# File 'lib/messhy/installer.rb', line 68

def setup_node(node_name)
  puts "==> Setting up node: #{node_name}"

  raise Error, "Node not found: #{node_name}" unless config.node_config(node_name)

  config.validate!

  # Install WireGuard first so key generation works
  puts "\n==> Installing WireGuard tools..."
  ssh_executor.install_wireguard(node_name) unless dry_run

  # Load or generate keys for all nodes (needed for mesh config)
  puts "\n==> Ensuring key material exists..."
  generate_all_keys

  # Build and deploy config
  mesh_builder = MeshBuilder.new(config, @node_keys, @psk_map || {})
  config_content = mesh_builder.build_config_for_node(node_name)

  if dry_run
    puts "[DRY RUN] Would upload WireGuard config to #{node_name}"
  else
    ssh_executor.upload_config(node_name, config_content)
    ssh_executor.enable_and_start_wireguard(node_name)
  end

  puts "✓ Node #{node_name} setup complete"
end