Class: RightScriptSync::Downloader
- Inherits:
-
Object
- Object
- RightScriptSync::Downloader
- Defined in:
- lib/rightscript_sync/downloader.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#cookie_jar ⇒ Object
Returns the value of attribute cookie_jar.
-
#dry_run ⇒ Object
Returns the value of attribute dry_run.
-
#log ⇒ Object
Returns the value of attribute log.
-
#output_path ⇒ Object
Returns the value of attribute output_path.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #download(url, headers) ⇒ Object
- #download_file(url, headers, file) ⇒ Object
- #download_right_script_attachments(right_script_id) ⇒ Object
- #download_right_scripts ⇒ Object
- #execute ⇒ Object
-
#initialize(options) ⇒ Downloader
constructor
A new instance of Downloader.
- #login ⇒ Object
- #mkbasedir(file) ⇒ Object
- #normalize_right_script_name(right_script_name) ⇒ Object
- #parse_right_script_attachments(html) ⇒ Object
- #parse_right_scripts(xml) ⇒ Object
- #store_file(file, data) ⇒ Object
- #store_metadata(file, data) ⇒ Object
- #store_right_script(right_script) ⇒ Object
- #store_right_script_attachment(right_script_attachment, right_script_attachment_path) ⇒ Object
- #store_right_script_attachments(right_script, right_script_path) ⇒ Object
- #store_right_scripts ⇒ Object
Constructor Details
#initialize(options) ⇒ Downloader
Returns a new instance of Downloader.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rightscript_sync/downloader.rb', line 33 def initialize() @log = Logger.new(STDOUT) @log.level = [:log_level] @account_id = [:account_id] @username = [:username] @password = [:password] @output_path = [:output_path] @dry_run = [: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}") = 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. = end end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def account_id @account_id end |
#agent ⇒ Object
Returns the value of attribute agent.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def agent @agent end |
#cookie_jar ⇒ Object
Returns the value of attribute cookie_jar.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def end |
#dry_run ⇒ Object
Returns the value of attribute dry_run.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def dry_run @dry_run end |
#log ⇒ Object
Returns the value of attribute log.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def log @log end |
#output_path ⇒ Object
Returns the value of attribute output_path.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def output_path @output_path end |
#password ⇒ Object
Returns the value of attribute password.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
31 32 33 |
# File 'lib/rightscript_sync/downloader.rb', line 31 def username @username end |
Instance Method Details
#download(url, headers) ⇒ Object
127 128 129 130 131 132 |
# 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) ⇒ Object
120 121 122 123 124 125 |
# 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) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/rightscript_sync/downloader.rb', line 111 def (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 (html) end |
#download_right_scripts ⇒ Object
102 103 104 105 106 107 108 109 |
# 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 ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rightscript_sync/downloader.rb', line 208 def execute begin login store_right_scripts rescue Interrupt exit rescue Exception => e puts e. puts e.backtrace.join("\n") end end |
#login ⇒ Object
94 95 96 97 98 99 100 |
# 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) ⇒ Object
134 135 136 137 138 139 140 141 |
# 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) ⇒ Object
204 205 206 |
# 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) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rightscript_sync/downloader.rb', line 75 def (html) doc = Nokogiri::HTML(html) doc.encoding = 'UTF-8' = [] = doc.xpath('//table[@id="right_scripts_show_script_attachments"]/tbody/tr') .each do || = {} [:filename] = .xpath('td[@data-column_name="Filename"]/a/text()').to_s.strip next if [:filename].nil? || [:filename].empty? [:uri] = .xpath('td[@data-column_name="Filename"]/a/@href').to_s.strip [:size] = .xpath('td[@data-column_name="Size"]/text()').to_s.strip [:created_at] = .xpath('td[@data-column_name="Created At"]/text()').to_s.strip [:updated_at] = .xpath('td[@data-column_name="Updated At"]/text()').to_s.strip [:md5sum] = .xpath('td[@data-column_name="md5sum"]/text()').to_s.strip << end return end |
#parse_right_scripts(xml) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
151 152 153 154 155 156 157 |
# 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) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/rightscript_sync/downloader.rb', line 143 def (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) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# 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_path}/metadata.yml" (, right_script) (right_script, right_script_path) end |
#store_right_script_attachment(right_script_attachment, right_script_attachment_path) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rightscript_sync/downloader.rb', line 159 def (, ) @log.info("Storing RightScript attachment '#{right_script_attachment[:filename]}' (#{right_script_attachment[:size]}) #{right_script_attachment[:updated_at]}") = + '/' + [:filename] headers = {} url = [:uri] if File.exists?() if Digest::MD5.file() == [:md5sum] @log.info("Already downloaded #{right_script_attachment_file_path} with #{right_script_attachment[:md5sum]} md5") return end end download_file(url, headers, ) File.utime(Time.parse([:updated_at]), Time.parse([:created_at]), ) unless @dry_run @log.info("Attachment stored to #{right_script_attachment_file_path}") end |
#store_right_script_attachments(right_script, right_script_path) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/rightscript_sync/downloader.rb', line 175 def (right_script, right_script_path) = "#{right_script_path}/attachments" @log.info("Storing RightScript attachments for '#{right_script[:name]}' to #{right_script_attachment_path}") (right_script[:id]).each do || (, ) end end |
#store_right_scripts ⇒ Object
197 198 199 200 201 202 |
# 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 |