enviroblyd 0.1.2 → 0.2

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: 4662a74b3e3eb17cad2ef6c6f511fc79e4460eb9a40635a8f476e50076b075e7
4
- data.tar.gz: 4fba6adca14dd3247decf29333e6452e544c91b9772012c494c1e452cdee74ae
3
+ metadata.gz: a61c27661f880ff2306805248d6ca4a7eac4f757f337f1380582831b6d85fdce
4
+ data.tar.gz: 82bbdd550838e7676a94970079a7086eb7c2906de86384b41686556a67e7985e
5
5
  SHA512:
6
- metadata.gz: 9ea39b7b42bdd486e064ef256524fcb5ca42030bcc3a91d51382436b0f5c035c8da438cc4d5f094aec714319429df255dc717fb74b7240e44cb051d86bd2e987
7
- data.tar.gz: eeb5ea402402c421d298da8dd2e641fa8c0c46e39dfd5e27abffe89752aa180df8c5a965461d337ca097446486a27f47e25a265b48142df9018245d7cd43f4cf
6
+ metadata.gz: 43f1691bbbb5a00f2ac39ff1d991cf5330d62d5be49541f46d312bb70b62e5e5536ff05a5c314b7ca8dc42b868fb6db54866ec1ca27a490e5c7b4429cd972bf1
7
+ data.tar.gz: 2e4c114de8590593c522b28f5a372fa558f11dbd74fd2a6e9ce32cd9c6d2643d7179ad099082e238a313a1b756571b5fc2f22de97784e02ff988ca4d762f6f95
@@ -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
@@ -39,17 +50,46 @@ class Enviroblyd::Cli::Main < Enviroblyd::Base
39
50
 
40
51
  yield request if block_given?
41
52
 
42
- response = http.request(request)
43
- if success_codes.include?(response.code.to_i)
53
+ response =
54
+ begin
55
+ http.request(request)
56
+ rescue Net::OpenTimeout, Net::ReadTimeout, Errno::EHOSTUNREACH, Errno::EHOSTDOWN
57
+ nil
58
+ end
59
+
60
+ if response && success_codes.include?(response.code.to_i)
44
61
  response
45
62
  elsif retries <= tries
46
63
  $stderr.puts "Retried #{tries} times. Aborting."
47
64
  exit 1
48
65
  else
49
66
  sleep_time = (backoff == :exponential) ? (retry_interval * tries) : retry_interval
50
- puts "Try #{uri} in #{sleep_time}s"
67
+ $stderr.puts "Retry #{uri} in #{sleep_time}s"
51
68
  sleep sleep_time
52
69
  http(url, type:, retry_interval:, retries:, backoff:, success_codes:, tries: (tries + 1))
53
70
  end
54
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
55
95
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enviroblyd
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2"
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.2
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi