enviroblyd 0.4.1 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enviroblyd/command.rb +7 -4
- data/lib/enviroblyd/daemon.rb +14 -20
- data/lib/enviroblyd/imds.rb +22 -0
- data/lib/enviroblyd/version.rb +1 -1
- data/lib/enviroblyd/web.rb +44 -48
- data/lib/enviroblyd.rb +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20310001470bfb7d27a249ce226a8754b5c73594227c8dd9de29375cdfb525d7
|
4
|
+
data.tar.gz: df8c884d9ccf0895116c19bfe2ab6c9ee0571397e3e2900b64a311e130301897
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78e2c025fd71934402535993f38e1e8e31c98fb2190d6e8a01e8a9ec42f4c8063012f1889228e7802ecb2b9abe190a20155d63f11c1ae74aad2e335d26d5d76f
|
7
|
+
data.tar.gz: 109616a3d9bec031e964126b5142b65997ea4046da301ae97fb9c1f4a21029269a3888c9e6da8f553e02a0548caa2f1214ca7adf8eab026b6e60efbcfecc5a2f
|
data/lib/enviroblyd/command.rb
CHANGED
@@ -6,8 +6,13 @@ class Enviroblyd::Command
|
|
6
6
|
DEFAULT_TIMEOUT_SECONDS = 5 * 60
|
7
7
|
DEFAULT_RUNTIME = "/bin/bash"
|
8
8
|
|
9
|
+
def self.run(params)
|
10
|
+
Thread.new do
|
11
|
+
new(params).run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
9
15
|
def initialize(params)
|
10
|
-
@web = Enviroblyd::Web.new
|
11
16
|
@url = params.fetch "url"
|
12
17
|
@script = params.fetch "script"
|
13
18
|
@runtime = params.fetch "runtime", DEFAULT_RUNTIME
|
@@ -17,7 +22,6 @@ class Enviroblyd::Command
|
|
17
22
|
|
18
23
|
def run
|
19
24
|
puts "Command #{@url} starting"
|
20
|
-
|
21
25
|
Open3.popen3("timeout #{@timeout} #{@runtime}") do |stdin, stdout, stderr, thread|
|
22
26
|
stdin.puts @script
|
23
27
|
stdin.close
|
@@ -25,11 +29,10 @@ class Enviroblyd::Command
|
|
25
29
|
@stderr = stderr.read
|
26
30
|
@exit_code = thread.value.exitstatus
|
27
31
|
end
|
28
|
-
|
29
32
|
puts "Command #{@url} exited with #{@exit_code}"
|
30
33
|
$stdout.flush
|
31
34
|
|
32
|
-
|
35
|
+
Enviroblyd::Web.http(@url, type: Net::HTTP::Put, params: to_complete_params)
|
33
36
|
end
|
34
37
|
|
35
38
|
private
|
data/lib/enviroblyd/daemon.rb
CHANGED
@@ -8,42 +8,36 @@ class Enviroblyd::Daemon
|
|
8
8
|
LISTEN_PORT = ENV.fetch("ENVIROBLYD_PORT", 63106).to_i
|
9
9
|
|
10
10
|
def self.start
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
imds = Enviroblyd::IMDS.new
|
12
|
+
host = imds.private_ipv4
|
13
|
+
new.listen(host) do
|
14
|
+
Enviroblyd::Web.register
|
15
|
+
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def listen
|
19
|
-
server = TCPServer.new LISTEN_PORT
|
20
|
-
puts "Listening on
|
18
|
+
def listen(host)
|
19
|
+
server = TCPServer.new host, LISTEN_PORT
|
20
|
+
puts "Listening on #{host}:#{LISTEN_PORT}"
|
21
|
+
|
22
|
+
yield
|
21
23
|
|
22
24
|
loop do
|
23
25
|
Thread.start(server.accept) do |client|
|
24
|
-
message = client.recv(MAX_MESSAGE_SIZE)
|
25
|
-
|
26
26
|
params =
|
27
27
|
begin
|
28
|
-
JSON.parse
|
28
|
+
JSON.parse client.recv(MAX_MESSAGE_SIZE)
|
29
29
|
rescue
|
30
|
-
|
30
|
+
:invalid_json
|
31
31
|
end
|
32
32
|
|
33
|
-
if params
|
33
|
+
if params == :invalid_json
|
34
34
|
client.puts "Error parsing JSON"
|
35
35
|
else
|
36
|
-
puts "Received valid JSON:"
|
37
|
-
puts params
|
38
36
|
client.puts "OK"
|
39
37
|
end
|
40
38
|
|
41
|
-
# TODO: Handle Broken pipe (Errno::EPIPE) (client closing connection before we write back)
|
42
39
|
client.close
|
43
|
-
|
44
|
-
Thread.new do
|
45
|
-
Enviroblyd::Command.new(params).run
|
46
|
-
end
|
40
|
+
Enviroblyd::Command.run(params)
|
47
41
|
end
|
48
42
|
end
|
49
43
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Enviroblyd::IMDS
|
4
|
+
TOKEN_TTL_SECONDS = 30
|
5
|
+
IMDS_HOST = ENV.fetch("ENVIROBLYD_IMDS_HOST", "169.254.169.254")
|
6
|
+
|
7
|
+
def initialize
|
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 }).
|
10
|
+
body.strip
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(key)
|
14
|
+
Enviroblyd::Web.http("http://#{IMDS_HOST}/latest/meta-data/#{key}",
|
15
|
+
headers: { "X-aws-ec2-metadata-token" => @token }).
|
16
|
+
body.strip
|
17
|
+
end
|
18
|
+
|
19
|
+
def private_ipv4
|
20
|
+
get "private_ipv4"
|
21
|
+
end
|
22
|
+
end
|
data/lib/enviroblyd/version.rb
CHANGED
data/lib/enviroblyd/web.rb
CHANGED
@@ -10,67 +10,63 @@ require "json"
|
|
10
10
|
class Enviroblyd::Web
|
11
11
|
USER_AGENT = "enviroblyd #{Enviroblyd::VERSION}"
|
12
12
|
CONTENT_TYPE = "application/json"
|
13
|
-
# TOKEN_TTL_SECONDS = 30
|
14
|
-
# IMDS_HOST = ENV.fetch("ENVIROBLYD_IMDS_HOST", "169.254.169.254")
|
15
13
|
API_HOST = ENV.fetch("ENVIROBLYD_API_HOST", "envirobly.com")
|
16
14
|
WORKING_DIR = Pathname.new ENV.fetch("ENVIROBLYD_WORKING_DIR", "/var/envirobly/daemon")
|
17
15
|
INITIALIZED_FILE = WORKING_DIR.join "initialized"
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
class << self
|
18
|
+
def register
|
19
|
+
if File.exist?(INITIALIZED_FILE)
|
20
|
+
puts "Skipping initialization because #{INITIALIZED_FILE} exists."
|
21
|
+
else
|
22
|
+
init_url = ENV.fetch "ENVIROBLYD_INIT_URL"
|
23
|
+
puts "Init URL: #{init_url}"
|
24
|
+
response = http(init_url, type: Net::HTTP::Put)
|
25
|
+
puts "Init response code: #{response.code}"
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
if response.code.to_i == 200
|
28
|
+
FileUtils.mkdir_p WORKING_DIR
|
29
|
+
File.write INITIALIZED_FILE, init_url
|
30
|
+
end
|
31
31
|
end
|
32
32
|
end
|
33
|
-
end
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
def http(url, type: Net::HTTP::Get, params: nil, headers: {}, retry_interval: 3, retries: 10, backoff: :exponential, tries: 1)
|
35
|
+
if retries <= tries
|
36
|
+
$stderr.puts "Retried #{tries} times. Aborting."
|
37
|
+
exit 1
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
uri = URI(url)
|
41
|
+
http = Net::HTTP.new uri.host, uri.port
|
42
|
+
http.use_ssl = true if uri.scheme == "https"
|
43
|
+
http.open_timeout = 10
|
44
|
+
http.read_timeout = 10
|
46
45
|
|
47
|
-
|
48
|
-
|
46
|
+
request = type.new(uri, default_headers.merge(headers))
|
47
|
+
request.content_type = CONTENT_TYPE
|
48
|
+
request.body = JSON.dump(params) unless params.nil?
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
response =
|
51
|
+
begin
|
52
|
+
http.request(request)
|
53
|
+
rescue
|
54
|
+
:retry
|
55
|
+
end
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
:
|
57
|
+
if response == :retry || (500..599).include?(response.code.to_i)
|
58
|
+
sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
|
59
|
+
$stderr.puts "Retry #{uri} in #{sleep_time}s"
|
60
|
+
sleep sleep_time
|
61
|
+
http(url, type:, params:, retry_interval:, retries:, backoff:, tries: (tries + 1))
|
62
|
+
else
|
63
|
+
response
|
59
64
|
end
|
60
|
-
|
61
|
-
# https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-1xxx-errors/
|
62
|
-
if response == :retry || (500..599).include?(response.code.to_i)
|
63
|
-
sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
|
64
|
-
$stderr.puts "Retry #{uri} in #{sleep_time}s"
|
65
|
-
sleep sleep_time
|
66
|
-
http(url, type:, retry_interval:, retries:, backoff:, tries: (tries + 1))
|
67
|
-
else
|
68
|
-
response
|
69
65
|
end
|
70
|
-
end
|
71
66
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
67
|
+
private
|
68
|
+
def default_headers
|
69
|
+
{ "User-Agent" => USER_AGENT }
|
70
|
+
end
|
71
|
+
end
|
76
72
|
end
|
data/lib/enviroblyd.rb
CHANGED
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.
|
4
|
+
version: 0.4.3
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/enviroblyd/cli/main.rb
|
69
69
|
- lib/enviroblyd/command.rb
|
70
70
|
- lib/enviroblyd/daemon.rb
|
71
|
+
- lib/enviroblyd/imds.rb
|
71
72
|
- lib/enviroblyd/version.rb
|
72
73
|
- lib/enviroblyd/web.rb
|
73
74
|
homepage: https://github.com/envirobly/enviroblyd
|