Skip to content

Commit 5083a46

Browse files
committed
feat: add parse func to edition, add url_of release func
Add HTTP module Add Data module
1 parent c1707fc commit 5083a46

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

lib/rootfs/data.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "distro/ubuntu"
4+
5+
module RootFS
6+
module Data
7+
def from_sha256sum(str, keywords)
8+
results = []
9+
lines = str.split("\n")
10+
lines.each do |line|
11+
arr = line.split(" ")
12+
file = arr[1].delete_prefix("*")
13+
14+
all_matched = true
15+
keywords.each do |keyword|
16+
all_matched = false unless file.include?(keyword)
17+
end
18+
results.push(file) if all_matched
19+
end
20+
results
21+
end
22+
end
23+
end

lib/rootfs/distro/ubuntu/edition.rb

+34
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module RootFS
44
module Distro
55
module Ubuntu
6+
extend self
67
# https://ubuntu.com/download
78
# https://wiki.ubuntu.com/Releases
89

@@ -38,6 +39,39 @@ module Ubuntu
3839
}.freeze
3940

4041
CODENAME_VERSION = {}.merge(DEV, INTERIM, LTS, ESM).freeze
42+
43+
def parse_edition(any)
44+
err_msg = "Valid Ubuntu edition: #{EDITION}"
45+
46+
puts err_msg unless any
47+
48+
str = any.to_s
49+
puts err_msg if str.empty?
50+
51+
EDITION.each do |edition|
52+
return { edition: edition } if any.include?(edition)
53+
end
54+
puts err_msg
55+
end
56+
57+
def parse_release(any)
58+
str = any.instance_of?(0.1.class) ? format("%.2f", any) : any.to_s
59+
sym = str.to_sym
60+
if CODENAME_VERSION.key?(sym)
61+
version = CODENAME_VERSION[sym]
62+
dev = DEV.key?(sym) ? true : false
63+
64+
{ type: "codename", codename: sym, version: version, dev: dev }
65+
elsif CODENAME_VERSION.value?(str)
66+
codename = CODENAME_VERSION.key(str)
67+
dev = DEV.value?(str) ? true : false
68+
69+
{ type: "version", codename: codename, version: str, dev: dev }
70+
else
71+
keys_values = CODENAME_VERSION.keys + CODENAME_VERSION.values
72+
puts "Valid Ubuntu release: #{keys_values}"
73+
end
74+
end
4175
end
4276
end
4377
end
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "edition"
4+
5+
class MyClass
6+
extend RootFS::Distro::Ubuntu
7+
include RootFS::Distro::Ubuntu
8+
end
9+
10+
u = MyClass.new
11+
12+
p u.parse_edition("fedora")
13+
p u.parse_edition("cloud")
14+
15+
p u.parse_release(1)
16+
p u.parse_release(20.04)
17+
p u.parse_release(:focal)
18+
p u.parse_release(22.10)
19+
p u.parse_release(:lunar)
20+
p u.parse_release(23.04)

lib/rootfs/distro/ubuntu/url.rb

+25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# frozen_string_literal: true
22

3+
require_relative "edition"
4+
35
module RootFS
46
module Distro
57
module Ubuntu
8+
extend self
69
# http://releases.ubuntu.com/
710
# http://cdimage.ubuntu.com/
811
# https://cloud-images.ubuntu.com/
@@ -29,6 +32,28 @@ module Ubuntu
2932
daily: "https://cloud-images.ubuntu.com/minimal/daily/{codename}/current/SHA256SUMS"
3033
}
3134
}.freeze
35+
36+
def url_of(edi, rel, daily = false)
37+
hash = RootFS::Distro::Ubuntu.parse_edition(edi)
38+
return unless hash
39+
40+
edition = hash[:edition]
41+
42+
hash = RootFS::Distro::Ubuntu.parse_release(rel)
43+
return unless hash
44+
45+
type = hash[:type]
46+
codename = hash[:codename]
47+
version = hash[:version]
48+
dev = hash[:dev]
49+
daily ||= type == "codename"
50+
51+
url_tmpl = !daily ? EDITION_URL[edition.to_sym][:release] : EDITION_URL[edition.to_sym][:daily]
52+
top_dir = url_tmpl.start_with?("http://cdimage.ubuntu.com") && dev && daily
53+
codename = "" if top_dir
54+
55+
url_tmpl.gsub("{version}", version).gsub("{codename}", codename.to_s)
56+
end
3257
end
3358
end
3459
end

lib/rootfs/distro/ubuntu/url_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "url"
4+
5+
class MyClass
6+
extend RootFS::Distro::Ubuntu
7+
include RootFS::Distro::Ubuntu
8+
end
9+
10+
u = MyClass.new
11+
12+
p u.url_of("desktop", 20.04)
13+
p u.url_of("desktop", 23.04, true)
14+
p u.url_of("server", 20.04)
15+
p u.url_of("base", 20.04)
16+
p u.url_of("base", "focal")
17+
p u.url_of("cloud", "focal")
18+
p u.url_of("cloud", 22.04)
19+
p u.url_of("minimal", 22.04)
20+
p u.url_of("minimal", "jammy")

lib/rootfs/http.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require "faraday"
4+
5+
module RootFS
6+
module HTTP
7+
def get(url)
8+
resp = Faraday.get(url)
9+
code = resp.status
10+
if code >= 300 && code < 400
11+
new_url = resp.headers["Location"]
12+
get(new_url)
13+
else
14+
resp
15+
end
16+
end
17+
18+
def head(url)
19+
resp = Faraday.head(url)
20+
code = resp.status
21+
if code >= 300 && code < 400
22+
new_url = resp.headers["Location"]
23+
head(new_url)
24+
else
25+
resp
26+
end
27+
end
28+
29+
def valid?(url)
30+
resp = head(url)
31+
code = resp.status
32+
code >= 200 && code < 300
33+
end
34+
end
35+
end

lib/rootfs/http_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "http"
4+
5+
class MyClass
6+
extend RootFS::HTTP
7+
include RootFS::HTTP
8+
end
9+
10+
u = MyClass.new
11+
url = "https://cloud-images.ubuntu.com/minimal/releases/jammy/release/SHA256SUMS"
12+
gentoo = "https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/20221225T170313Z/stage3-amd64-systemd-20221225T170313Z.tar.xz"
13+
14+
p u.valid?(gentoo)

0 commit comments

Comments
 (0)