summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-05-01 08:38:23 +0300
committerPaul Buetow <paul@buetow.org>2025-05-01 08:38:23 +0300
commitb155941b9f02f95306ffb8bc4192c048dfc56d5e (patch)
tree6a314475fceb3546ca530be4f70e72ec86a04bc6
parent77cb9f83f937425123a8f2c583f9913badce6633 (diff)
more on this
-rw-r--r--wireguardmeshgenerator.rb29
1 files changed, 12 insertions, 17 deletions
diff --git a/wireguardmeshgenerator.rb b/wireguardmeshgenerator.rb
index 179c9c3..b58b2dd 100644
--- a/wireguardmeshgenerator.rb
+++ b/wireguardmeshgenerator.rb
@@ -8,8 +8,6 @@ require 'yaml'
require 'optparse'
-CONFIG = YAML.load_file('wireguardmeshgenerator.yaml').freeze
-
class KeyTool
def initialize(myself)
raise 'Wireguard tool not found' unless
@@ -47,7 +45,8 @@ class KeyTool
end
end
-PeerSnippet = Struct.new(:myself, :domain, :wgdomain, :allowed_ips, :endpoint) do
+PeerSnippet = Struct.new(:myself, :domain, :wgdomain,
+ :allowed_ips, :endpoint) do
def to_s
keytool = KeyTool.new(myself)
<<~PEER_CONFIG
@@ -122,7 +121,6 @@ InstallConfig = Struct.new(:myself, :hosts) do
if [ ! -d #{@config_path} ]; then
#{@sudo_cmd} mkdir -p #{@config_path}
#{@sudo_cmd} mv -v wg0.conf #{@config_path}
- #{@sudo_cmd} #{@restart_cmd}
fi
SH
raise "Unable to install Wireguard config on #{myself}" unless
@@ -131,20 +129,16 @@ InstallConfig = Struct.new(:myself, :hosts) do
self
end
- def reload!
- puts "Reloading Wireguard config on #{myself}"
- ssh <<~SH
- #{@sudo_cmd} #{@restart_cmd}
- SH
- raise "Unable to reload Wireguard config on #{myself}" unless
+ def restart!
+ puts "Restarting Wireguard on #{myself}"
+ ssh "#{@sudo_cmd} #{@restart_cmd}"
+ raise "Unable to restart Wireguard on #{myself}" unless
$CHILD_STATUS.success?
-
- self
end
private
- def ssh(command) = Net::SSH.start(myself, @ssh_user) { |s| s.exec!(command) }
+ def ssh(cmd) = Net::SSH.start(myself, @ssh_user) { _1.exec!(cmd) }
end
begin
@@ -161,14 +155,15 @@ begin
end
end.parse!
- CONFIG['hosts'].each_key do |hostname|
- WireguardConfig.new(hostname, CONFIG['hosts']).generate! if
+ conf = YAML.load_file('wireguardmeshgenerator.yaml').freeze
+ conf['hosts'].each_key do |hostname|
+ WireguardConfig.new(hostname, conf['hosts']).generate! if
options[:generate]
- InstallConfig.new(hostname, CONFIG['hosts']).upload!.install!.reload! if
+ InstallConfig.new(hostname, conf['hosts']).upload!.install!.restart! if
options[:install]
- WireguardConfig.new(hostname, CONFIG['hosts']).clean! if
+ WireguardConfig.new(hostname, conf['hosts']).clean! if
options[:clean]
end
rescue StandardError => e