enviroblyd 0.3 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enviroblyd/cli/main.rb +45 -40
- data/lib/enviroblyd/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dbb9c1de2225aad7c7211baf934798dd3a059ce4dabe7efeed4d96b1e804410
|
4
|
+
data.tar.gz: c5eb8ba3ed2880049eb6094c30cdfb8a2514f5c22ba770f5a646027f9368ae7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbb457cd2d63bd5565c1b9ea0ceb96de79ad4def5e4674d691595cbee5706da25b339d96059f20e0779f77d76368688d16d3fe64c3a56eae5a8ace9d1ef2add0
|
7
|
+
data.tar.gz: 2ed04fd30d0522315c2e56fd0a03010eb801c78bc2c17175ad1bc3b744e743dbe105486a1cc465a4de731a6099ced61bb18ada8696e477542f4aff2f4ed58f07
|
data/lib/enviroblyd/cli/main.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
19
|
-
|
20
|
-
puts "token: #{@token} ."
|
21
|
-
instance_id = http("http://#{IMDS_HOST}/latest/meta-data/instance-id",
|
22
|
-
|
23
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# end
|
34
|
-
# end
|
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}"
|
35
40
|
|
36
|
-
|
37
|
-
|
41
|
+
if response.code.to_i == 200
|
42
|
+
FileUtils.mkdir_p WORKING_DIR
|
43
|
+
File.write INITIALIZED_FILE, init_url
|
44
|
+
end
|
45
|
+
end
|
38
46
|
end
|
39
47
|
|
40
48
|
private
|
41
|
-
def http(url, type: Net::HTTP::Get, headers: {}, retry_interval: 2, retries: 30, backoff: false,
|
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
|
-
|
70
|
+
:retry
|
58
71
|
end
|
59
72
|
|
60
|
-
|
61
|
-
|
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:,
|
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
|
-
|
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
|
data/lib/enviroblyd/version.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:
|
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-
|
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.
|
87
|
+
rubygems_version: 3.5.18
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Envirobly instance daemon
|