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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c88f9afa8cae744a95f4b3813a6ba92b273fb259b6b58815baadc60a889bc698
4
- data.tar.gz: e1e3938c06ffa662ad066cca2b49d3f69500a04178e264721c78faf954523135
3
+ metadata.gz: a61c27661f880ff2306805248d6ca4a7eac4f757f337f1380582831b6d85fdce
4
+ data.tar.gz: 82bbdd550838e7676a94970079a7086eb7c2906de86384b41686556a67e7985e
5
5
  SHA512:
6
- metadata.gz: ff3a731d3ba7d43faeb6da6573060db09dd0087ceaba056bdbe9d2d6e71223b00ed6452199d895d8d3fcae4a4cd9504708ea82296628d58c6cff11a3a822dbe3
7
- data.tar.gz: 79a94b0c5a26173b5d10d39da7a513073be4cf280ad6d47c510d211fc8939cfe7ad2eea45e126dc529174ac34ea96b60b5781f3cd4049bd7593927b69b18ef8a
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
@@ -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"
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'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi