Skip to content

Commit

Permalink
libnodectld: introduce VpsPostStart class
Browse files Browse the repository at this point in the history
After VPS is started, nodectld reads its SSH host keys and os-release
information. Since both require running osctl ct show, it's now done
through a single point so as to not run it multiple times needlessly.
  • Loading branch information
aither64 committed Nov 21, 2024
1 parent 037e0ad commit ec564b0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 68 deletions.
10 changes: 6 additions & 4 deletions libnodectld/lib/nodectld/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,20 @@ module NodeCtld
batch_size: 50
},

vps_post_start: {
default_schedule_delay: 15
},

vps_ssh_host_keys: {
enable: true,
update_vps_delay: 1,
update_all_interval: 24 * 60 * 60,
default_schedule_delay: 15
update_all_interval: 24 * 60 * 60
},

vps_os_release: {
enable: true,
update_vps_delay: 1,
update_all_interval: 24 * 60 * 60,
default_schedule_delay: 15
update_all_interval: 24 * 60 * 60
},

dataset_expander: {
Expand Down
3 changes: 1 addition & 2 deletions libnodectld/lib/nodectld/ct_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def process_event(event)
end

if vps_id > 0 && event[:opts][:state] == 'running'
VpsSshHostKeys.schedule_update_vps(vps_id)
VpsOsRelease.schedule_update_vps(vps_id)
VpsPostStart.run(vps_id)
end

if vps_id > 0 && event[:opts][:state] == 'stopped'
Expand Down
39 changes: 8 additions & 31 deletions libnodectld/lib/nodectld/vps_os_release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class VpsOsRelease
].freeze

class << self
%i[update_vps schedule_update_vps].each do |v|
%i[update_ct].each do |v|
define_method(v) do |*args, **kwargs, &block|
instance.send(v, *args, **kwargs, &block)
end
Expand All @@ -47,27 +47,11 @@ def initialize
@update_all_thread = Thread.new { update_all_worker }
end

# @param vps_id [Integer]
def update_vps(vps_id)
# @param ct [OsCtlContainer]
def update_ct(ct)
return unless enable?

@update_vps_queue.insert(vps_id)
end

# @param vps_id [Integer]
# @param in_seconds [Integer, nil]
def schedule_update_vps(vps_id, in_seconds: nil)
return unless enable?

in_seconds ||= $CFG.get(:vps_os_release, :default_schedule_delay)
log(:info, "Scheduling os-release update for VPS #{vps_id} in #{in_seconds}s")

Thread.new do
sleep(in_seconds)
update_vps(vps_id)
end

nil
@update_vps_queue.insert(ct)
end

def enable?
Expand All @@ -82,8 +66,8 @@ def log_type

def update_vps_worker
loop do
vps_or_id = @update_vps_queue.pop
update_vps_os_release(vps_or_id)
ct = @update_vps_queue.pop
update_vps_os_release(ct)
sleep($CFG.get(:vps_os_release, :update_vps_delay))
end
end
Expand Down Expand Up @@ -114,15 +98,8 @@ def update_all_worker
end
end

# @param vps [OsCtlContainer, Integer]
def update_vps_os_release(ct_or_id)
ct =
if ct_or_id.is_a?(Integer)
OsCtlContainer.new(osctl_parse(%i[ct show], [ct_or_id]))
else
ct_or_id
end

# @param ct [OsCtlContainer]
def update_vps_os_release(ct)
log(:info, "Updating os-release of VPS #{ct.id}")

if ct.in_ct_boot?
Expand Down
46 changes: 46 additions & 0 deletions libnodectld/lib/nodectld/vps_post_start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'libosctl'
require 'singleton'

module NodeCtld
class VpsPostStart
include Singleton
include OsCtl::Lib::Utils::Log
include OsCtl::Lib::Utils::System
include Utils::OsCtl

class << self
%i[run].each do |v|
define_method(v) do |*args, **kwargs, &block|
instance.send(v, *args, **kwargs, &block)
end
end
end

# @param vps_id [Integer]
# @param in_seconds [Integer, nil]
def run(vps_id, in_seconds: nil)
in_seconds ||= $CFG.get(:vps_post_start, :default_schedule_delay)

Thread.new do
sleep(in_seconds)
post_run(vps_id)
end

nil
end

def log_type
'vps-post-start'
end

protected

def post_run(vps_id)
ct = OsCtlContainer.new(osctl_parse(%i[ct show], [vps_id]))
return if ct.state != 'running'

VpsSshHostKeys.update_ct(ct)
VpsOsRelease.update_ct(ct)
end
end
end
39 changes: 8 additions & 31 deletions libnodectld/lib/nodectld/vps_ssh_host_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class VpsSshHostKeys
include Utils::OsCtl

class << self
%i[update_vps schedule_update_vps].each do |v|
%i[update_ct].each do |v|
define_method(v) do |*args, **kwargs, &block|
instance.send(v, *args, **kwargs, &block)
end
Expand All @@ -28,27 +28,11 @@ def initialize
@update_all_thread = Thread.new { update_all_worker }
end

# @param vps_id [Integer]
def update_vps(vps_id)
# @param ct [OsCtlContainer]
def update_ct(ct)
return unless enable?

@update_vps_queue.insert(vps_id)
end

# @param vps_id [Integer]
# @param in_seconds [Integer, nil]
def schedule_update_vps(vps_id, in_seconds: nil)
return unless enable?

in_seconds ||= $CFG.get(:vps_ssh_host_keys, :default_schedule_delay)
log(:info, "Scheduling ssh host key update for VPS #{vps_id} in #{in_seconds}s")

Thread.new do
sleep(in_seconds)
update_vps(vps_id)
end

nil
@update_vps_queue.insert(ct)
end

def enable?
Expand All @@ -65,8 +49,8 @@ def log_type

def update_vps_worker
loop do
vps_or_id = @update_vps_queue.pop
update_vps_keys(vps_or_id)
ct = @update_vps_queue.pop
update_vps_keys(ct)
sleep($CFG.get(:vps_ssh_host_keys, :update_vps_delay))
end
end
Expand Down Expand Up @@ -97,15 +81,8 @@ def update_all_worker
end
end

# @param ct [OsCtlContainer, Integer]
def update_vps_keys(ct_or_id)
ct =
if ct_or_id.is_a?(Integer)
OsCtlContainer.new(osctl_parse(%i[ct show], [ct_or_id]))
else
ct_or_id
end

# @param ct [OsCtlContainer]
def update_vps_keys(ct)
log(:info, "Updating keys of VPS #{ct.id}")
t = Time.now

Expand Down

0 comments on commit ec564b0

Please sign in to comment.