enviroblyd 0.2.2 → 0.3.1

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: 8220085dcf938ad62385573bc35fdecd43fc1be67c8c8290809e0e324ffa6b60
4
- data.tar.gz: b34fa0008ebbd596a608e71fd4899f6ed6265441acd4539d8191e632324f57e6
3
+ metadata.gz: 5dbb9c1de2225aad7c7211baf934798dd3a059ce4dabe7efeed4d96b1e804410
4
+ data.tar.gz: c5eb8ba3ed2880049eb6094c30cdfb8a2514f5c22ba770f5a646027f9368ae7f
5
5
  SHA512:
6
- metadata.gz: 83c2d244fb58a3f8bc8c94d9325b9fff0f220bcba0df5acadbae99999e0237403a6646a67623fb27eb6891efff98479ba043b8ae23a832673faca852783bea6e
7
- data.tar.gz: b0bf6a4b127e324f10ff63556ff8b5922355371305d5c19625b17023b4589a07883fef93bb7fcddc49fd48f539923f3b6d06e2e66795852d0af8466321fccff3
6
+ metadata.gz: fbb457cd2d63bd5565c1b9ea0ceb96de79ad4def5e4674d691595cbee5706da25b339d96059f20e0779f77d76368688d16d3fe64c3a56eae5a8ace9d1ef2add0
7
+ data.tar.gz: 2ed04fd30d0522315c2e56fd0a03010eb801c78bc2c17175ad1bc3b744e743dbe105486a1cc465a4de731a6099ced61bb18ada8696e477542f4aff2f4ed58f07
@@ -2,6 +2,8 @@
2
2
  require "net/http"
3
3
  require "uri"
4
4
  require "open3"
5
+ require "fileutils"
6
+ require "pathname"
5
7
 
6
8
  class Enviroblyd::Cli::Main < Enviroblyd::Base
7
9
  desc "version", "Show version"
@@ -12,40 +14,51 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
12
14
  TOKEN_TTL_SECONDS = 30
13
15
  IMDS_HOST = ENV.fetch("ENVIROBLYD_IMDS_HOST", "169.254.169.254")
14
16
  API_HOST = ENV.fetch("ENVIROBLYD_API_HOST", "envirobly.com")
15
- desc "boot", "Get IMDSv2 metadata"
17
+ WORKING_DIR = Pathname.new ENV.fetch("ENVIROBLYD_WORKING_DIR", "/var/envirobly/daemon")
18
+ INITIALIZED_FILE = WORKING_DIR.join "initialized"
19
+ desc "boot", "Start the daemon"
16
20
  def boot
17
- @token = http("http://#{IMDS_HOST}/latest/api/token",
18
- type: Net::HTTP::Put, headers: { "X-aws-ec2-metadata-token-ttl-seconds" => TOKEN_TTL_SECONDS.to_s }).
19
- body.chomp("")
20
- puts "token: #{@token} ."
21
- instance_id = http("http://#{IMDS_HOST}/latest/meta-data/instance-id",
22
- headers: { "X-aws-ec2-metadata-token" => @token }).
23
- body.chomp("")
24
- puts "instance_id: #{instance_id} ."
21
+ # @token = http("http://#{IMDS_HOST}/latest/api/token",
22
+ # type: Net::HTTP::Put, headers: { "X-aws-ec2-metadata-token-ttl-seconds" => TOKEN_TTL_SECONDS.to_s }).
23
+ # body.chomp("")
24
+ # puts "token: #{@token} ."
25
+ # instance_id = http("http://#{IMDS_HOST}/latest/meta-data/instance-id",
26
+ # headers: { "X-aws-ec2-metadata-token" => @token }).
27
+ # body.chomp("")
28
+ # puts "instance_id: #{instance_id} ."
29
+ #
30
+ # response = http("https://#{API_HOST}/api/v1/boots/#{instance_id}", retry_interval: 3, retries: 5, backoff: :exponential)
31
+ # puts "/api/v1/boots response code: #{response.code}"
25
32
 
26
- process_user_data
27
- unless @exit_code.nil?
28
- puts @stdout
29
- $stderr.puts @stderr
30
- unless @exit_code == 0
31
- $stderr.puts "User data script exited with code: #{@exit_code}. Aborting."
32
- exit 1
33
+ if File.exist?(INITIALIZED_FILE)
34
+ puts "Skipping initialization because #{INITIALIZED_FILE} exists."
35
+ else
36
+ init_url = ENV.fetch "ENVIROBLYD_INIT_URL"
37
+ puts "Init URL: #{init_url}"
38
+ response = http(init_url, type: Net::HTTP::Put, retry_interval: 3, retries: 10, backoff: :exponential)
39
+ puts "Init response code: #{response.code}"
40
+
41
+ if response.code.to_i == 200
42
+ FileUtils.mkdir_p WORKING_DIR
43
+ File.write INITIALIZED_FILE, init_url
33
44
  end
34
45
  end
35
-
36
- response = http("https://#{API_HOST}/api/v1/boots/#{instance_id}", retry_interval: 3, retries: 5, backoff: :exponential)
37
- puts "/api/v1/boots response code: #{response.code}"
38
46
  end
39
47
 
40
48
  private
41
- def http(url, type: Net::HTTP::Get, headers: {}, retry_interval: 2, retries: 30, backoff: false, success_codes: 200..299, tries: 1)
49
+ def http(url, type: Net::HTTP::Get, headers: {}, retry_interval: 2, retries: 30, backoff: false, tries: 1)
50
+ if retries <= tries
51
+ $stderr.puts "Retried #{tries} times. Aborting."
52
+ exit 1
53
+ end
54
+
42
55
  uri = URI(url)
43
56
  http = Net::HTTP.new uri.host, uri.port
44
57
  http.use_ssl = true if uri.scheme == "https"
45
58
  http.open_timeout = 5
46
59
  http.read_timeout = 5
47
60
 
48
- request = type.new(uri, headers)
61
+ request = type.new(uri, default_headers.merge(headers))
49
62
  # request.content_type = CONTENT_TYPE
50
63
 
51
64
  yield request if block_given?
@@ -54,30 +67,17 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
54
67
  begin
55
68
  http.request(request)
56
69
  rescue
57
- nil
70
+ :retry
58
71
  end
59
72
 
60
- if response && success_codes.include?(response.code.to_i)
61
- response
62
- elsif retries <= tries
63
- $stderr.puts "Retried #{tries} times. Aborting."
64
- exit 1
65
- else
73
+ # https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-1xxx-errors/
74
+ if response == :retry || (500..599).include?(response.code.to_i)
66
75
  sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
67
76
  $stderr.puts "Retry #{uri} in #{sleep_time}s"
68
77
  sleep sleep_time
69
- http(url, type:, retry_interval:, retries:, backoff:, success_codes:, tries: (tries + 1))
70
- end
71
- end
72
-
73
- def process_user_data
74
- response = http("http://#{IMDS_HOST}/latest/user-data",
75
- headers: { "X-aws-ec2-metadata-token" => @token })
76
-
77
- if response.body.start_with?("#!/bin/bash")
78
- run response.body
78
+ http(url, type:, retry_interval:, retries:, backoff:, tries: (tries + 1))
79
79
  else
80
- $stderr.puts "User data does not contain runable script."
80
+ response
81
81
  end
82
82
  end
83
83
 
@@ -92,4 +92,9 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
92
92
  @exit_code = thread.value.exitstatus
93
93
  end
94
94
  end
95
+
96
+ USER_AGENT = "enviroblyd #{Enviroblyd::VERSION}"
97
+ def default_headers
98
+ { "User-Agent" => USER_AGENT }
99
+ end
95
100
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.1"
5
5
  end
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.2.2
4
+ version: 0.3.1
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-02 00:00:00.000000000 Z
11
+ date: 2024-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 3.5.14
87
+ rubygems_version: 3.5.18
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Envirobly instance daemon