-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from krauselukas/images-xml
Add images.xml with adjustments to pass rubocop rules
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'open-uri' | ||
require 'nokogiri' | ||
|
||
# Controller for /images.xml | ||
class ImagesController < ApplicationController | ||
attr_accessor :base_url | ||
|
||
def initialize | ||
@base_url = "http://download.opensuse.org" | ||
end | ||
|
||
class MetadataError < RuntimeError; end | ||
|
||
# GET /images.xml | ||
def images; end | ||
|
||
# Returns the first capture of regex applied to the file name of URL's redirection target | ||
def get_version(url, regex) | ||
abs_url = @base_url + url | ||
Rails.cache.fetch("/build_number/#{abs_url}", expires_in: 10.minutes) do | ||
begin | ||
meta = meta_file(abs_url) | ||
name_elem = meta.xpath("//m:metalink//m:file//@name", 'm' => 'urn:ietf:params:xml:ns:metalink').to_s | ||
matches = regex.match(name_elem.to_s) | ||
matches[1] | ||
rescue OpenURI::HTTPError, RuntimeError => e | ||
filename = File.basename(URI.parse(abs_url).path) | ||
raise MetadataError, "Could not get version of #{filename}: #{e}" | ||
end | ||
end | ||
end | ||
|
||
# Returns a string containing a XML <image> element with all necessary content for url and name | ||
def image_element(url, name) | ||
abs_url = @base_url + url | ||
size = content_size(abs_url) | ||
|
||
ret = "<image url=#{abs_url.encode(xml: :attr)} name=#{name.encode(xml: :attr)} size=\"#{size}\">\n" | ||
ret += "<checksum type=\"sha256\" disposition=#{(abs_url + '.sha256').encode(xml: :attr)}/>\n" | ||
ret += "</image>" | ||
ret.html_safe | ||
end | ||
|
||
helper_method :get_version | ||
helper_method :image_element | ||
|
||
protected | ||
|
||
# Returns the content size of url | ||
def content_size(url) | ||
Rails.cache.fetch("/content_size/#{url}", expires_in: 10.minutes) do | ||
begin | ||
meta = meta_file(url) | ||
size_text = meta.xpath("//m:metalink//m:file//m:size[1]//text()", 'm' => 'urn:ietf:params:xml:ns:metalink') | ||
size = Integer(size_text.to_s) | ||
size | ||
rescue OpenURI::HTTPError, RuntimeError | ||
# This is actualy gibibytes, not gigabytes | ||
return 5.gigabytes | ||
end | ||
end | ||
end | ||
|
||
# Returns a Nokogiri XML document of the mirrorbrain metadata for url | ||
def meta_file(url) | ||
Nokogiri::XML(open(url + '.meta4')) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<% | ||
leap_versions = ['42.3', '42.2'] | ||
leap_current = '42.3' | ||
leap_snapshot = '15.0' | ||
|
||
if leap_snapshot != '' then | ||
leap_snapshot_build = get_version("/distribution/leap/#{leap_snapshot}/iso/openSUSE-Leap-#{leap_snapshot}-DVD-x86_64-Current.iso", /Build([0-9.]+)-/) | ||
end | ||
|
||
tw_snapshot = get_version('/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso', /Snapshot(\d+)-/) | ||
%> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<decision name="<%= _("Distribution") %>"> | ||
<% leap_versions.each do |leap_version| %> | ||
<option name="openSUSE Leap <%= leap_version %>" preselected="<%= leap_version == leap_current ? 'true' : 'false' %>"> | ||
<decision name="<%= _("Installation Source") %>"> | ||
<option name="<%= _("DVD") %>" preselected="true"> | ||
<%= image_element("/distribution/leap/#{leap_version}/iso/openSUSE-Leap-#{leap_version}-DVD-x86_64.iso", _("Leap %s DVD") % leap_version) %> | ||
</option> | ||
<option name="<%= _("Network") %>"> | ||
<%= image_element("/distribution/leap/#{leap_version}/iso/openSUSE-Leap-#{leap_version}-NET-x86_64.iso", _("Leap %s Network Installation") % leap_version) %> | ||
</option> | ||
</decision> | ||
</option> | ||
<% end -%> | ||
<option name="<%= _("openSUSE Tumbleweed") %>"> | ||
<decision name="<%= _("Medium") %>"> | ||
<option name="<%= _("Installation") %>" preselected="true"> | ||
<decision name="<%= _("Source") %>"> | ||
<option name="<%= _("DVD") %>" preselected="true"> | ||
<decision name="<%= _("Architecture") %>"> | ||
<option name="<%= _("i586") %>"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-DVD-i586-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s DVD (i586)") % tw_snapshot) %> | ||
</option> | ||
<option name="<%= _("x86_64") %>" preselected="true"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s DVD (x86_64)") % tw_snapshot) %> | ||
</option> | ||
</decision> | ||
</option> | ||
<option name="<%= _("Network") %>"> | ||
<decision name="<%= _("Architecture") %>"> | ||
<option name="<%= _("i586") %>"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-NET-i586-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s Network Installation (i586)") % tw_snapshot) %> | ||
</option> | ||
<option name="<%= _("x86_64") %>" preselected="true"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-NET-x86_64-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s Network Installation (x86_64)") % tw_snapshot) %> | ||
</option> | ||
</decision> | ||
</option> | ||
</decision> | ||
</option> | ||
<option name="<%= _("Live") %>"> | ||
<decision name="<%= _("Desktop") %>"> | ||
<option name="GNOME"> | ||
<decision name="<%= _("Architecture") %>"> | ||
<option name="<%= _("i586") %>"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-GNOME-Live-i686-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s GNOME Live (i686)") % tw_snapshot) %> | ||
</option> | ||
<option name="<%= _("x86_64") %>" preselected="true"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-GNOME-Live-x86_64-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s GNOME Live (x86_64)") % tw_snapshot) %> | ||
</option> | ||
</decision> | ||
</option> | ||
<option name="KDE" preselected="true"> | ||
<decision name="<%= _("Architecture") %>"> | ||
<option name="<%= _("i586") %>"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-i686-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s KDE Live (i686)") % tw_snapshot) %> | ||
</option> | ||
<option name="<%= _("x86_64") %>" preselected="true"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-KDE-Live-x86_64-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s KDE Live (x86_64)") % tw_snapshot) %> | ||
</option> | ||
</decision> | ||
</option> | ||
<option name="<%= _("Rescue") %>"> | ||
<decision name="<%= _("Architecture") %>"> | ||
<option name="<%= _("i586") %>"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-i686-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s Rescue CD (i686)") % tw_snapshot) %> | ||
</option> | ||
<option name="<%= _("x86_64") %>" preselected="true"> | ||
<%= image_element("/tumbleweed/iso/openSUSE-Tumbleweed-Rescue-CD-x86_64-Snapshot#{tw_snapshot}-Media.iso", _("Tumbleweed %s Rescue CD (x86_64)") % tw_snapshot) %> | ||
</option> | ||
</decision> | ||
</option> | ||
</decision> | ||
</option> | ||
</decision> | ||
</option> | ||
<% if leap_snapshot != "" then %> | ||
<option name="openSUSE Leap <%= leap_snapshot %> (In development)"> | ||
<decision name="<%= _("Installation Source") %>"> | ||
<option name="<%= _("DVD") %>" preselected="true"> | ||
<%= image_element("/distribution/leap/#{leap_snapshot}/iso/openSUSE-Leap-#{leap_snapshot}-DVD-x86_64-Build#{leap_snapshot_build}-Media.iso", | ||
_("Leap %s DVD (Build %s)") % [leap_snapshot, leap_snapshot_build]) %> | ||
</option> | ||
<option name="<%= _("Network") %>"> | ||
<%= image_element("/distribution/leap/#{leap_snapshot}/iso/openSUSE-Leap-#{leap_snapshot}-NET-x86_64-Build#{leap_snapshot_build}-Media.iso", | ||
_("Leap %s Network Installation (Build %s)") % [leap_snapshot, leap_snapshot_build]) %> | ||
</option> | ||
</decision> | ||
</option> | ||
<%- end %> | ||
</decision> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters