class RightScriptSync::Downloader

Attributes

account_id[RW]
agent[RW]
dry_run[RW]
log[RW]
output_path[RW]
password[RW]
username[RW]

Public Class Methods

new(options) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 33
def initialize(options)
  @log = Logger.new(STDOUT)
  @log.level = options[:log_level]
  @account_id = options[:account_id]
  @username = options[:username]
  @password = options[:password]
  @output_path = options[:output_path]
  @dry_run = options[:dry_run] || false
  @api_uri = "https://my.rightscale.com/api/acct/#{@account_id}"
  @site_uri = "https://my.rightscale.com/acct/#{@account_id}"

  @log.debug("account_id:#{@account_id} username:#{@username} password:#{@password}")
  @cookie_jar = Mechanize::CookieJar.new
  @agent = Mechanize.new do |agent|
    #agent.log = @log
    agent.user_agent_alias = 'Mac Safari'
    agent.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
    agent.cookie_jar = @cookie_jar
  end
end

Public Instance Methods

download(url, headers) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 127
def download(url, headers)
  @log.debug("Downloading #{url}")
  @agent.get(url, nil, nil, headers) do |page|
    return page.body
  end
end
download_file(url, headers, file) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 120
def download_file(url, headers, file)
  @log.debug("Downloading #{url} to '#{file}'")
  return if @dry_run
  mkbasedir(file)
  @agent.get(url, nil, nil, headers).save(file)
 end
download_right_script_attachments(right_script_id) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 111
def download_right_script_attachments(right_script_id)
  @log.info("Downloading attachments for #{right_script_id}")
  headers = { 'X-Requested-With' => 'XMLHttpRequest' }
  url = "#{@site_uri}/right_scripts/#{right_script_id}/script_attachments"
  html = download(url, headers)
  #html = File.open("foo.html", "rb").read
  parse_right_script_attachments(html)
end
download_right_scripts() click to toggle source
# File lib/rightscript_sync/downloader.rb, line 102
def download_right_scripts
  @log.info("Downloading RightScripts")
  headers = { 'X-API-VERSION' => '1.0' }
  url = "#{@api_uri}/right_scripts.xml"
  xml = download(url, headers)
  #xml = File.open("right_scripts.xml", "rb").read
  parse_right_scripts(xml)
end
execute() click to toggle source
# File lib/rightscript_sync/downloader.rb, line 208
def execute
  begin
    login
    store_right_scripts      
  rescue Interrupt
    exit
  rescue Exception => e
    puts e.message
    puts e.backtrace.join("\n")
  end
end
login() click to toggle source
# File lib/rightscript_sync/downloader.rb, line 94
def login
  @log.info("Logging in")
  headers = { 'X-API-VERSION' => '1.0' }
  headers['Authorization'] = 'Basic ' + Base64.encode64( @username + ':' + @password )
  url = "#{@api_uri}/login?api_version=1.0"
  download(url, headers)
end
mkbasedir(file) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 134
def mkbasedir(file)
  dirname = File.dirname(file)
  unless File.directory?(dirname)
    @log.debug("Creating directory '#{dirname}'")
    return if @dry_run
    FileUtils.mkdir_p(dirname) 
  end
end
normalize_right_script_name(right_script_name) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 204
def normalize_right_script_name(right_script_name)
  right_script_name.gsub(/[^A-Za-z0-9_\.]+/, '_').downcase
end
parse_right_script_attachments(html) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 75
def parse_right_script_attachments(html)
  doc = Nokogiri::HTML(html)
  doc.encoding = 'UTF-8'
  right_script_attachments = [] 
  xpath_right_script_attachments = doc.xpath('//table[@id="right_scripts_show_script_attachments"]/tbody/tr')
  xpath_right_script_attachments.each do |xpath_right_script_attachment|
    right_script_attachment = {}
    right_script_attachment[:filename] = xpath_right_script_attachment.xpath('td[@data-column_name="Filename"]/a/text()').to_s.strip
    next if right_script_attachment[:filename].nil? || right_script_attachment[:filename].empty?
    right_script_attachment[:uri] = xpath_right_script_attachment.xpath('td[@data-column_name="Filename"]/a/@href').to_s.strip
    right_script_attachment[:size] = xpath_right_script_attachment.xpath('td[@data-column_name="Size"]/text()').to_s.strip
    right_script_attachment[:created_at] = xpath_right_script_attachment.xpath('td[@data-column_name="Created At"]/text()').to_s.strip
    right_script_attachment[:updated_at] = xpath_right_script_attachment.xpath('td[@data-column_name="Updated At"]/text()').to_s.strip
    right_script_attachment[:md5sum] = xpath_right_script_attachment.xpath('td[@data-column_name="md5sum"]/text()').to_s.strip
    right_script_attachments << right_script_attachment
  end
  return right_script_attachments
end
parse_right_scripts(xml) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 54
def parse_right_scripts(xml)
  doc = Nokogiri::XML(xml)
  doc.encoding = 'UTF-8'
  right_scripts = [] 
  xpath_right_scripts = doc.xpath('/right-scripts[@type="array"]/right-script')
  xpath_right_scripts.each do |xpath_right_script|
    right_script = {}
    right_script[:name] = xpath_right_script.at_xpath('name/text()').to_s.strip
    right_script[:updated_at] = xpath_right_script.at_xpath('updated-at/text()').to_s.strip
    right_script[:created_at] = xpath_right_script.at_xpath('created-at/text()').to_s.strip
    right_script[:is_head_version] = xpath_right_script.at_xpath('is-head-version/text()').to_s.strip
    right_script[:href] = xpath_right_script.at_xpath('href/text()').to_s.strip
    right_script[:id] = right_script[:href].gsub(/^.*\//, '').to_i
    right_script[:version] = xpath_right_script.at_xpath('version/text()').to_s.strip.to_i
    right_script[:script] = xpath_right_script.at_xpath('script/text()').to_s.strip
    right_script[:description] = xpath_right_script.at_xpath('description/text()').to_s.strip
    right_scripts << right_script
  end
  return right_scripts
end
store_file(file, data) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 151
def store_file(file, data)
  return if @dry_run
  mkbasedir(file)
  File.open(file, 'w') do |fh|
    fh.write(data)
  end
end
store_metadata(file, data) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 143
def store_metadata(file, data)
  return if @dry_run
  mkbasedir(file)
  File.open(file, 'w') do |fh|
    YAML.dump(data, fh)
  end
end
store_right_script(right_script) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 183
def store_right_script(right_script)
  @log.info("Storing RightScript (#{right_script[:name]})")
  right_script_path = "#{@output_path}/#{right_script[:id]}/#{normalize_right_script_name(right_script[:name])}/#{right_script[:version]}"

  right_script_file_path = "#{right_script_path}/script.txt"
  store_file(right_script_file_path, right_script[:script])
  File.utime(Time.parse(right_script[:updated_at]), Time.parse(right_script[:created_at]), right_script_file_path) unless @dry_run

  right_script_metadata_path = "#{right_script_path}/metadata.yml"
  store_metadata(right_script_metadata_path, right_script)

  store_right_script_attachments(right_script, right_script_path)
end
store_right_script_attachment(right_script_attachment, right_script_attachment_path) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 159
def store_right_script_attachment(right_script_attachment, right_script_attachment_path)
  @log.info("Storing RightScript attachment '#{right_script_attachment[:filename]}' (#{right_script_attachment[:size]}) #{right_script_attachment[:updated_at]}")
  right_script_attachment_file_path = right_script_attachment_path + '/' + right_script_attachment[:filename]
  headers = {}
  url = right_script_attachment[:uri]
  if File.exists?(right_script_attachment_file_path)
    if Digest::MD5.file(right_script_attachment_file_path) == right_script_attachment[:md5sum]
      @log.info("Already downloaded #{right_script_attachment_file_path} with #{right_script_attachment[:md5sum]} md5")
      return
    end
  end
  download_file(url, headers, right_script_attachment_file_path)
  File.utime(Time.parse(right_script_attachment[:updated_at]), Time.parse(right_script_attachment[:created_at]), right_script_attachment_file_path) unless @dry_run
  @log.info("Attachment stored to #{right_script_attachment_file_path}")
end
store_right_script_attachments(right_script, right_script_path) click to toggle source
# File lib/rightscript_sync/downloader.rb, line 175
def store_right_script_attachments(right_script, right_script_path)
  right_script_attachment_path = "#{right_script_path}/attachments"
  @log.info("Storing RightScript attachments for '#{right_script[:name]}' to #{right_script_attachment_path}")
  download_right_script_attachments(right_script[:id]).each do |right_script_attachment|
    store_right_script_attachment(right_script_attachment, right_script_attachment_path)
  end
end
store_right_scripts() click to toggle source
# File lib/rightscript_sync/downloader.rb, line 197
def store_right_scripts
  @log.info("Storing RightScripts")
  download_right_scripts.each do |right_script|
    store_right_script(right_script)
  end
end