forked from boundary/boundary_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovision_meter.rb
More file actions
executable file
·117 lines (100 loc) · 3.04 KB
/
Copy pathprovision_meter.rb
File metadata and controls
executable file
·117 lines (100 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
###
### Copyright 2011, Boundary
###
### Licensed under the Apache License, Version 2.0 (the "License");
### you may not use this file except in compliance with the License.
### You may obtain a copy of the License at
###
### http://www.apache.org/licenses/LICENSE-2.0
###
### Unless required by applicable law or agreed to in writing, software
### distributed under the License is distributed on an "AS IS" BASIS,
### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
### See the License for the specific language governing permissions and
### limitations under the License.
###
#!/usr/bin/ruby
require "rubygems"
require "excon"
require "json"
require "base64"
require "fileutils"
API_HOST = "api.boundary.com"
EC2_INTERNAL = "http://169.254.169.254/latest/meta-data/"
TAGS = ["ami-id",
"hostname",
"instance-id",
"instance-type",
"kernel-id",
"local-hostname",
"local-ipv4",
"mac",
"placement/availability-zone",
"public-hostname",
"public-ipv4",
"reservation-id",
"security-groups"]
def auth_encode(creds)
auth = Base64.encode64(creds).strip
auth.gsub("\n","")
end
def create_meter(id, headers)
hostname = Socket.gethostbyname(Socket.gethostname).first
if hostname == nil || hostname.include?("localhost")
abort("Hostname set to localhost or nil, exiting.")
end
body = {:name => hostname}
url = "https://#{API_HOST}/#{id}/meters"
response = Excon.post(url, :headers => headers, :body => body.to_json)
if response.headers["Location"]
puts "Meter created at #{response.headers["Location"]}"
response.headers["Location"]
else
abort("No location header received, error creating meter!")
end
end
def ec2_tags(url, headers)
begin
if Excon.get(EC2_INTERNAL).status == 200
puts "Auto generating ec2 tags for this meter ..."
TAGS.each do |tag|
response = Excon.get("#{EC2_INTERNAL}#{tag}")
if response.status == 200
Excon.put("#{url}/tags/#{response.body}", :headers => headers, :body => "")
else
# do nothing
end
end
Excon.put("#{url}/tags/ec2", :headers => headers, :body => "")
end
rescue Exception => e
# do nothing
end
end
def download_file(url, headers, filename)
puts "downloading #{filename.gsub(".pem", "")} for #{url}"
pem = Excon.get("#{url}/#{filename}", :headers => headers)
begin
file = File.new("./#{filename}", "w")
file.puts(pem.body)
rescue Exception => e
puts "something went wrong with writing #{filename}:"
puts e
ensure
file.close
FileUtils.chmod(0600, "./#{filename}")
end
end
def main
if ARGV[0] == "-i" && ARGV[2] == "-a"
auth = auth_encode("#{ARGV[3]}:")
headers = {"Authorization" => "Basic #{auth}", "Content-Type" => "application/json"}
url = create_meter(ARGV[1], headers)
ec2_tags(url, headers)
download_file(url, headers, "cert.pem")
download_file(url, headers, "key.pem")
else
puts "./provision_meter.rb -i ORGID -a APIKEY"
end
end
main