Permalink
Browse files

- add a logging helper

- handles exceptions in actions
- add ability to download files easily
  • Loading branch information...
1 parent 57455fe commit c1ea6c5fe8b6dd5acc4cf69653053ed7c8e49439 @ripienaar committed Jul 12, 2010
Showing with 40 additions and 7 deletions.
  1. +5 −1 lib/ec2boot/actions.rb
  2. +35 −6 lib/ec2boot/util.rb
View
@@ -21,7 +21,11 @@ def run_actions(ud, md, config)
meth = "#{type}_action"
if respond_to?(meth)
- send(meth, action, ud, md, config)
+ begin
+ send(meth, action, ud, md, config)
+ rescue Exception => e
+ Util.log("Failed to run action #{meth}: #{e.class}: #{e}")
+ end
else
# no such method
end
View
@@ -1,6 +1,15 @@
module EC2Boot
class Util
- def self.get_url(url)
+ # Fetches a url, it will retry 5 times if it still
+ # failed it will return ""
+ #
+ # If an optional file is specified it will write
+ # the retrieved data into the file in an efficient way
+ # in this case return data will be true or false
+ #
+ # raises URLNotFound for 404s and URLFetchFailed for
+ # other non 200 status codes
+ def self.get_url(url, file=nil)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 3
@@ -9,17 +18,31 @@ def self.get_url(url)
retries = 5
begin
- result = Net::HTTP.get URI.parse(url)
- request = Net::HTTP::Get.new(uri.request_uri)
- response = http.request(request)
+ if file
+ dest_file = File.open(file, "w")
+ response = http.get(uri.path) do |r|
+ dest_file.write r
+ end
+ close dest_file
+ else
+ response = http.get(uri.path)
+ end
raise URLNotFound if response.code == "404"
raise URLFetchFailed, "#{url}: #{response.code}" unless response.code == "200"
if response.code == "200"
- return response.body
+ if file
+ return true
+ else
+ return response.body
+ end
else
- return ""
+ if file
+ return false
+ else
+ return ""
+ end
end
rescue URLFetchFailed => e
retries -= 1
@@ -28,6 +51,12 @@ def self.get_url(url)
end
end
+ # Logs to stdout and syslog
+ def self.log(msg)
+ puts "#{Time.now}> #{msg}"
+ system("logger msg")
+ end
+
def self.write_facts(ud, md, config)
File.open(config.facts_file, "w") do |facts|

0 comments on commit c1ea6c5

Please sign in to comment.