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 +4 -4
- data/bin/enviroblyd +9 -1
- data/lib/enviroblyd/command.rb +16 -7
- data/lib/enviroblyd/daemon.rb +39 -27
- data/lib/enviroblyd/imds.rb +2 -1
- data/lib/enviroblyd/version.rb +1 -1
- data/lib/enviroblyd/web.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 329d9e7586d58e8b529d2a94d46be76130efb24bf688a4d53efe17dfeaf3e7e0
|
4
|
+
data.tar.gz: 47cf08b7ecf7231460aeed688aa76705abff6cdcfdf770148f9f86f378ee513e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f57916ca0d0615cd05cffcf15527f506a8ca60a47cec03f5d9ea4262b4ce0b298ae764cace907c764c4de9307f870659657610629c98e7ebd8d8c81388000623
|
7
|
+
data.tar.gz: a260266cb641e0b7e99d2e35f21cde3fcd82f80570782ce5628c4081c4e6f9773ee35ea4d660514267407ccc000146b3db1b504b60370518653f2b61c13ba719
|
data/bin/enviroblyd
CHANGED
data/lib/enviroblyd/command.rb
CHANGED
@@ -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
|
10
|
-
|
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
|
data/lib/enviroblyd/daemon.rb
CHANGED
@@ -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
|
-
|
7
|
+
PORT = ENV.fetch("ENVIROBLYD_PORT", 63106).to_i
|
9
8
|
|
10
|
-
def
|
9
|
+
def initialize
|
11
10
|
imds = Enviroblyd::IMDS.new
|
12
|
-
host = imds.private_ipv4
|
13
|
-
|
14
|
-
|
15
|
-
end
|
11
|
+
@host = imds.private_ipv4
|
12
|
+
@threads = []
|
13
|
+
@shutdown = false
|
16
14
|
end
|
17
15
|
|
18
|
-
def listen
|
19
|
-
server = TCPServer.new host,
|
20
|
-
puts "Listening on #{host}:#{
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
data/lib/enviroblyd/imds.rb
CHANGED
@@ -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,
|
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
|
|
data/lib/enviroblyd/version.rb
CHANGED
data/lib/enviroblyd/web.rb
CHANGED
@@ -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
|
-
|
37
|
-
|
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
|
-
|
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
|
+
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-
|
11
|
+
date: 2024-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|