enviroblyd 0.1.3 → 0.2
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 +39 -5
- 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: a61c27661f880ff2306805248d6ca4a7eac4f757f337f1380582831b6d85fdce
|
4
|
+
data.tar.gz: 82bbdd550838e7676a94970079a7086eb7c2906de86384b41686556a67e7985e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43f1691bbbb5a00f2ac39ff1d991cf5330d62d5be49541f46d312bb70b62e5e5536ff05a5c314b7ca8dc42b868fb6db54866ec1ca27a490e5c7b4429cd972bf1
|
7
|
+
data.tar.gz: 2e4c114de8590593c522b28f5a372fa558f11dbd74fd2a6e9ce32cd9c6d2643d7179ad099082e238a313a1b756571b5fc2f22de97784e02ff988ca4d762f6f95
|
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
|
@@ -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