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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c88f9afa8cae744a95f4b3813a6ba92b273fb259b6b58815baadc60a889bc698
4
- data.tar.gz: e1e3938c06ffa662ad066cca2b49d3f69500a04178e264721c78faf954523135
3
+ metadata.gz: e565c96dd7f26d49788503f35f5dc895baa532d1dbae03723d8fb42fad74e013
4
+ data.tar.gz: 7166e8a78f7091a0b6a3e3dbfa43359224828a0c94430b93d7e7fe7baa6b3619
5
5
  SHA512:
6
- metadata.gz: ff3a731d3ba7d43faeb6da6573060db09dd0087ceaba056bdbe9d2d6e71223b00ed6452199d895d8d3fcae4a4cd9504708ea82296628d58c6cff11a3a822dbe3
7
- data.tar.gz: 79a94b0c5a26173b5d10d39da7a513073be4cf280ad6d47c510d211fc8939cfe7ad2eea45e126dc529174ac34ea96b60b5781f3cd4049bd7593927b69b18ef8a
6
+ metadata.gz: aa26f07e720624c75d4e7f69de79cc643ffb58f3de56a9c02be818b3152b56caf4798f688f84be8cc223c728c397da46fd7d23a11258bc311cfcabed4fb3827f
7
+ data.tar.gz: 8efe981de9ecd6844f2323c171109894d6114284a09587b1f9e6d6d79769d8b434388345fa2068efb3543f681771d96a729420c315096def4c82e7214092114e
@@ -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 = http("http://#{IMDS_HOST}/latest/meta-data/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 Net::OpenTimeout, Net::ReadTimeout, Errno::EHOSTUNREACH, Errno::EHOSTDOWN
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 "Try #{uri} in #{sleep_time}s"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enviroblyd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi