enviroblyd 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 794c9805c66ea61e73486ccc08f71077daa2e2de80e6f9c5d510ff62f038f9f5
4
- data.tar.gz: 40f92e9aafc2351298c65f71ee251eb766a31abc0835ac3170ceb257ba854bfe
3
+ metadata.gz: 329d9e7586d58e8b529d2a94d46be76130efb24bf688a4d53efe17dfeaf3e7e0
4
+ data.tar.gz: 47cf08b7ecf7231460aeed688aa76705abff6cdcfdf770148f9f86f378ee513e
5
5
  SHA512:
6
- metadata.gz: 4d13d6792c28d62cd0ccf872e2368dc9396523cf59d3b67b0f471e8315dad6c66a3a7f8be3d507d2e7d8ef1bae7df06c1397b587a2b56de757426d448693d4be
7
- data.tar.gz: 8e0641adc0ef80bac699bd52702cbccb2ee7cdbe3d5b2d41d19f7f1e3baa27e10c6423d57a0a571d61c9f22c9f7bb088d33b8014f38067831d5bd1083b4d24b6
6
+ metadata.gz: f57916ca0d0615cd05cffcf15527f506a8ca60a47cec03f5d9ea4262b4ce0b298ae764cace907c764c4de9307f870659657610629c98e7ebd8d8c81388000623
7
+ data.tar.gz: a260266cb641e0b7e99d2e35f21cde3fcd82f80570782ce5628c4081c4e6f9773ee35ea4d660514267407ccc000146b3db1b504b60370518653f2b61c13ba719
data/bin/enviroblyd CHANGED
@@ -3,4 +3,12 @@
3
3
 
4
4
  require "enviroblyd"
5
5
 
6
- Enviroblyd::Daemon.start
6
+ daemon = Enviroblyd::Daemon.new
7
+
8
+ trap "INT" do
9
+ puts "Gracefully shutting down..."
10
+ daemon&.shutdown
11
+ exit
12
+ end
13
+
14
+ daemon.listen
@@ -1,23 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open3"
4
+ require "json"
4
5
 
5
6
  class Enviroblyd::Command
6
7
  DEFAULT_TIMEOUT_SECONDS = 5 * 60
7
8
  DEFAULT_RUNTIME = "/bin/bash"
8
9
 
9
- def self.run(params)
10
- Thread.new do
11
- new(params).run
12
- end
13
- end
14
-
15
- def initialize(params)
10
+ def initialize(message)
11
+ params = parse_message message
16
12
  @url = params.fetch "url"
17
13
  @script = params.fetch "script"
18
14
  @runtime = params.fetch "runtime", DEFAULT_RUNTIME
19
15
  @timeout = params.fetch "timeout", DEFAULT_TIMEOUT_SECONDS
20
16
  @stdout = @stderr = @exit_code = nil
17
+ @valid = true
18
+ rescue NoMethodError, KeyError
19
+ @valid = false
20
+ end
21
+
22
+ def valid?
23
+ @valid
21
24
  end
22
25
 
23
26
  def run
@@ -45,4 +48,10 @@ class Enviroblyd::Command
45
48
  }
46
49
  }
47
50
  end
51
+
52
+ def parse_message(message)
53
+ JSON.parse message
54
+ rescue
55
+ nil
56
+ end
48
57
  end
@@ -1,44 +1,56 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "socket"
4
- require "json"
5
4
 
6
5
  class Enviroblyd::Daemon
7
6
  MAX_MESSAGE_SIZE = 6000 # bytes
8
- LISTEN_PORT = ENV.fetch("ENVIROBLYD_PORT", 63106).to_i
7
+ PORT = ENV.fetch("ENVIROBLYD_PORT", 63106).to_i
9
8
 
10
- def self.start
9
+ def initialize
11
10
  imds = Enviroblyd::IMDS.new
12
- host = imds.private_ipv4
13
- new.listen(host) do
14
- Enviroblyd::Web.register
15
- end
11
+ @host = imds.private_ipv4
12
+ @threads = []
13
+ @shutdown = false
16
14
  end
17
15
 
18
- def listen(host)
19
- server = TCPServer.new host, LISTEN_PORT
20
- puts "Listening on #{host}:#{LISTEN_PORT}"
21
-
22
- yield
23
-
24
- loop do
25
- Thread.start(server.accept) do |client|
26
- params =
27
- begin
28
- JSON.parse client.recv(MAX_MESSAGE_SIZE)
29
- rescue
30
- nil
31
- end
32
-
33
- if params.nil?
34
- client.puts "Error parsing JSON"
35
- else
36
- client.puts "OK"
16
+ def listen
17
+ @server = TCPServer.new @host, PORT
18
+ puts "Listening on #{@host}:#{PORT}"
19
+ Enviroblyd::Web.register
20
+
21
+ until @shutdown do
22
+ @threads << Thread.start(@server.accept) do |client|
23
+ message = client.recv(MAX_MESSAGE_SIZE)
24
+ command = Enviroblyd::Command.new message
25
+
26
+ unless command.valid?
27
+ puts "Invalid message received: #{message}"
28
+ client.puts "Invalid message"
29
+ next
37
30
  end
38
31
 
32
+ client.puts "OK"
33
+ client.close
34
+ command.run
35
+ ensure
39
36
  client.close
40
- Enviroblyd::Command.run(params) if params
41
37
  end
38
+
39
+ delete_dead_threads
40
+ GC.start
41
+ GC.compact
42
42
  end
43
43
  end
44
+
45
+ def shutdown
46
+ @threads.each(&:join)
47
+ end
48
+
49
+ private
50
+ def delete_dead_threads
51
+ @threads.each do |thread|
52
+ next if thread.alive?
53
+ @threads.delete thread
54
+ end
55
+ end
44
56
  end
@@ -6,7 +6,8 @@ class Enviroblyd::IMDS
6
6
 
7
7
  def initialize
8
8
  @token = Enviroblyd::Web.http("http://#{IMDS_HOST}/latest/api/token",
9
- type: Net::HTTP::Put, headers: { "X-aws-ec2-metadata-token-ttl-seconds" => TOKEN_TTL_SECONDS.to_s }).
9
+ type: Net::HTTP::Put,
10
+ headers: { "X-aws-ec2-metadata-token-ttl-seconds" => TOKEN_TTL_SECONDS.to_s }).
10
11
  body.strip
11
12
  end
12
13
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.4.5"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -33,8 +33,8 @@ class Enviroblyd::Web
33
33
 
34
34
  def http(url, type: Net::HTTP::Get, params: nil, headers: {}, retry_interval: 3, retries: 10, backoff: :exponential, tries: 1)
35
35
  if retries <= tries
36
- $stderr.puts "Retried #{tries} times. Aborting."
37
- exit 1
36
+ puts "Retried #{url} #{tries} times. Aborting."
37
+ return
38
38
  end
39
39
 
40
40
  uri = URI(url)
@@ -56,7 +56,7 @@ class Enviroblyd::Web
56
56
 
57
57
  if response == :retry || (500..599).include?(response.code.to_i)
58
58
  sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
59
- $stderr.puts "Retry #{uri} in #{sleep_time}s"
59
+ puts "Retry #{uri} in #{sleep_time}s"
60
60
  sleep sleep_time
61
61
  http(url, type:, params:, retry_interval:, retries:, backoff:, tries: (tries + 1))
62
62
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enviroblyd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-06 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor