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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05071a7be6ad5145537f06b8466841984969e4c89759ea929ee0f1a28a6457cf
4
- data.tar.gz: 79c5c57647395378dddd409ae90075e888e7e1999c997efd1c1e736b6a871fbb
3
+ metadata.gz: 20310001470bfb7d27a249ce226a8754b5c73594227c8dd9de29375cdfb525d7
4
+ data.tar.gz: df8c884d9ccf0895116c19bfe2ab6c9ee0571397e3e2900b64a311e130301897
5
5
  SHA512:
6
- metadata.gz: 54cdd680ffb67c14da0795fee35ef925e44795fc91876917377b8c7be8e6719b4ecb6c8cb826ad3d941d6856680a7aff6700f7a0de284ec0847e026bc60e1acf
7
- data.tar.gz: a71d5071c3bb1001f6f35ef5603a617b88aec4d1a54c338945afd1df43e6a764ce441b2324cc3a932f2b97df5ee5947221136ad86694bd7bae78922b10a5bd9c
6
+ metadata.gz: 78e2c025fd71934402535993f38e1e8e31c98fb2190d6e8a01e8a9ec42f4c8063012f1889228e7802ecb2b9abe190a20155d63f11c1ae74aad2e335d26d5d76f
7
+ data.tar.gz: 109616a3d9bec031e964126b5142b65997ea4046da301ae97fb9c1f4a21029269a3888c9e6da8f553e02a0548caa2f1214ca7adf8eab026b6e60efbcfecc5a2f
@@ -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
- @web.http(@url, type: Net::HTTP::Put, params: to_complete_params)
35
+ Enviroblyd::Web.http(@url, type: Net::HTTP::Put, params: to_complete_params)
33
36
  end
34
37
 
35
38
  private
@@ -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
- web = Enviroblyd::Web.new
12
- web.register
13
-
14
- daemon = new
15
- daemon.listen
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 port #{LISTEN_PORT}"
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 message
28
+ JSON.parse client.recv(MAX_MESSAGE_SIZE)
29
29
  rescue
30
- nil
30
+ :invalid_json
31
31
  end
32
32
 
33
- if params.nil?
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.3"
5
5
  end
@@ -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
- def register
20
- if File.exist?(INITIALIZED_FILE)
21
- puts "Skipping initialization because #{INITIALIZED_FILE} exists."
22
- else
23
- init_url = ENV.fetch "ENVIROBLYD_INIT_URL"
24
- puts "Init URL: #{init_url}"
25
- response = http(init_url, type: Net::HTTP::Put, retry_interval: 3, retries: 10, backoff: :exponential)
26
- puts "Init response code: #{response.code}"
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
- if response.code.to_i == 200
29
- FileUtils.mkdir_p WORKING_DIR
30
- File.write INITIALIZED_FILE, init_url
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
- def http(url, type: Net::HTTP::Get, headers: {}, retry_interval: 2, retries: 30, backoff: false, tries: 1, params: nil)
36
- if retries <= tries
37
- $stderr.puts "Retried #{tries} times. Aborting."
38
- exit 1
39
- end
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
- uri = URI(url)
42
- http = Net::HTTP.new uri.host, uri.port
43
- http.use_ssl = true if uri.scheme == "https"
44
- http.open_timeout = 5
45
- http.read_timeout = 5
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
- request = type.new(uri, default_headers.merge(headers))
48
- request.content_type = CONTENT_TYPE
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
- unless params.nil?
51
- request.body = JSON.dump params
52
- end
50
+ response =
51
+ begin
52
+ http.request(request)
53
+ rescue
54
+ :retry
55
+ end
53
56
 
54
- response =
55
- begin
56
- http.request(request)
57
- rescue
58
- :retry
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
- private
73
- def default_headers
74
- { "User-Agent" => USER_AGENT }
75
- end
67
+ private
68
+ def default_headers
69
+ { "User-Agent" => USER_AGENT }
70
+ end
71
+ end
76
72
  end
data/lib/enviroblyd.rb CHANGED
@@ -8,5 +8,8 @@ end
8
8
  require "zeitwerk"
9
9
 
10
10
  loader = Zeitwerk::Loader.for_gem
11
+ loader.inflector.inflect(
12
+ "imds" => "IMDS"
13
+ )
11
14
  loader.setup
12
15
  loader.eager_load
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.1
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-04 00:00:00.000000000 Z
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