enviroblyd 0.1.3 → 0.2.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 +40 -6
- data/lib/enviroblyd/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e565c96dd7f26d49788503f35f5dc895baa532d1dbae03723d8fb42fad74e013
|
4
|
+
data.tar.gz: 7166e8a78f7091a0b6a3e3dbfa43359224828a0c94430b93d7e7fe7baa6b3619
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa26f07e720624c75d4e7f69de79cc643ffb58f3de56a9c02be818b3152b56caf4798f688f84be8cc223c728c397da46fd7d23a11258bc311cfcabed4fb3827f
|
7
|
+
data.tar.gz: 8efe981de9ecd6844f2323c171109894d6114284a09587b1f9e6d6d79769d8b434388345fa2068efb3543f681771d96a729420c315096def4c82e7214092114e
|
data/lib/enviroblyd/cli/main.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require "net/http"
|
3
3
|
require "uri"
|
4
|
+
require "open3"
|
4
5
|
|
5
6
|
class Enviroblyd::Cli::Main < Enviroblyd::Base
|
6
7
|
desc "version", "Show version"
|
@@ -13,15 +14,25 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
|
|
13
14
|
API_HOST = ENV.fetch("ENVIROBLYD_API_HOST", "envirobly.com")
|
14
15
|
desc "boot", "Get IMDSv2 metadata"
|
15
16
|
def boot
|
16
|
-
token = http("http://#{IMDS_HOST}/latest/api/token",
|
17
|
+
@token = http("http://#{IMDS_HOST}/latest/api/token",
|
17
18
|
type: Net::HTTP::Put, headers: { "X-aws-ec2-metadata-token-ttl-seconds" => TOKEN_TTL_SECONDS.to_s }).
|
18
19
|
body.chomp("")
|
19
|
-
puts "token: #{token} ."
|
20
|
-
instance_id =
|
21
|
-
headers: { "X-aws-ec2-metadata-token" => token }).
|
20
|
+
puts "token: #{@token} ."
|
21
|
+
instance_id = http("http://#{IMDS_HOST}/latest/meta-data/instance-id",
|
22
|
+
headers: { "X-aws-ec2-metadata-token" => @token }).
|
22
23
|
body.chomp("")
|
23
24
|
puts "instance_id: #{instance_id} ."
|
24
25
|
|
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
|
+
end
|
34
|
+
end
|
35
|
+
|
25
36
|
response = http("https://#{API_HOST}/api/v1/boots/#{instance_id}", retry_interval: 3, retries: 5, backoff: :exponential)
|
26
37
|
puts "/api/v1/boots response code: #{response.code}"
|
27
38
|
end
|
@@ -42,7 +53,7 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
|
|
42
53
|
response =
|
43
54
|
begin
|
44
55
|
http.request(request)
|
45
|
-
rescue
|
56
|
+
rescue
|
46
57
|
nil
|
47
58
|
end
|
48
59
|
|
@@ -53,9 +64,32 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
|
|
53
64
|
exit 1
|
54
65
|
else
|
55
66
|
sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
|
56
|
-
puts "
|
67
|
+
$stderr.puts "Retry #{uri} in #{sleep_time}s"
|
57
68
|
sleep sleep_time
|
58
69
|
http(url, type:, retry_interval:, retries:, backoff:, success_codes:, tries: (tries + 1))
|
59
70
|
end
|
60
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
|
79
|
+
else
|
80
|
+
$stderr.puts "User data does not contain runable script."
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
RUN_TIMEOUT = "5m"
|
85
|
+
def run(script)
|
86
|
+
@stdout = @stderr = @exit_code = nil
|
87
|
+
Open3.popen3("timeout #{RUN_TIMEOUT} /bin/bash") do |stdin, stdout, stderr, thread|
|
88
|
+
stdin.puts script
|
89
|
+
stdin.close
|
90
|
+
@stdout = stdout.read
|
91
|
+
@stderr = stderr.read
|
92
|
+
@exit_code = thread.value.exitstatus
|
93
|
+
end
|
94
|
+
end
|
61
95
|
end
|
data/lib/enviroblyd/version.rb
CHANGED