Jekyll_Ubuntu_Releases.rb 3.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'open-uri'
require 'open_uri_redirections'
require 'json'

module Jekyll_Ubuntu_Releases

  class Fetcher
    def self.fetchFor(site)
      site.data['ubuntu_releases'] = Hash.new
      site.data['ubuntu_releases']['all'] = get_all_releases()

      site.data['ubuntu_releases']['latest'] = Hash.new
      site.data['ubuntu_releases']['latest']['release'] = Hash.new
      site.data['ubuntu_releases']['latest']['lts'] = Hash.new
      site.data['ubuntu_releases']['all'].each do |release|
        if release['active_and_supported'] then
17
18
          site.data['ubuntu_releases']['latest']['release'] = release
          if release['lts'] then
19
20
21
22
23
24
25
            site.data['ubuntu_releases']['latest']['lts'] = release
          end
        end
      end

      release_version = get_precise_version(site.data['ubuntu_releases']['latest']['release']['version'])
      lts_version = get_precise_version(site.data['ubuntu_releases']['latest']['lts']['version'])
26
27
28
29
      site.data['ubuntu_releases']['latest']['release']['version'] = release_version['desktop']
      site.data['ubuntu_releases']['latest']['lts']['version'] = lts_version['desktop']
      site.data['ubuntu_releases']['latest']['release']['version-server'] = release_version['server']
      site.data['ubuntu_releases']['latest']['lts']['version-server'] = lts_version['server']
30
31
32
33
34
    end

    def self.get_all_releases()
      all_releases_data = []
      series = nil
35
      URI('https://api.launchpad.net/devel/ubuntu/series').open('r', :allow_redirections => :all) do |series_content|
36
37
38
        series = JSON.parse(series_content.read)
      end
      cloud_images = nil
39
      URI('https://cloud-images.ubuntu.com/releases/streams/v1/com.ubuntu.cloud:released:download.json').open('r', :allow_redirections => :all) do |cloud_images_content|
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        cloud_images = JSON.parse(cloud_images_content.read)
      end
      (series['total_size'].to_i-1).downto(0).each do |i|
        series_entry = series['entries'][i]
        release_version = series_entry['version']
        cloud_images_entry = cloud_images['products']["com.ubuntu.cloud:server:#{release_version}:arm64"]
        release_data = Hash.new
        release_data['version'] = release_version
        release_data['name'] = series_entry['title'].delete_prefix('The ')
        release_data['lts'] = !cloud_images_entry.nil? ? cloud_images_entry['release_title'].downcase.include?('lts') : false
        release_data['released'] = series_entry['datereleased']
        release_data['eol'] = !cloud_images_entry.nil? ? cloud_images_entry['support_eol'] : nil
        release_data['active_and_supported'] = series_entry['active'] && series_entry['supported']
        all_releases_data.push(release_data)
      end
      return all_releases_data
    end

    def self.get_precise_version(base_version)
59
60
61
62
63
64
      URI('https://releases.ubuntu.com/'+base_version+'/SHA256SUMS').open('r', :allow_redirections => :all) do |remote_content|
        content_string = remote_content.read
        return {
          'server' => /[0-9]+\.[0-9]+(\.[0-9])*-live-server/.match(content_string).to_s.gsub('-live-server', ''),
          'desktop' => /[0-9]+\.[0-9]+(\.[0-9])*-desktop/.match(content_string).to_s.gsub('-desktop', '')
        }
65
66
67
68
69
70
71
72
73
      end
    end
  end

  Jekyll::Hooks.register :site, :post_read do |site|
    Fetcher.fetchFor(site)
  end

end