Skip to content

Commit 19bb742

Browse files
committed
Automatically filter yum installation platform options based on what's in the repo.
1 parent e918f61 commit 19bb742

File tree

4 files changed

+136
-89
lines changed

4 files changed

+136
-89
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ pynliner==0.8.0
1010
Babel==2.6.0
1111
bleach==3.1.4
1212
PyYAML==3.13
13+
zstandard==0.23.0
14+
rpmfile==2.1.0

templates/downloads/js/yum.js

Lines changed: 110 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -55,112 +55,135 @@ function uses_systemd(plat) {
5555
}
5656

5757
function get_platform_text(p) {
58-
var a = p.split('-');
58+
const a = p.split('-');
5959
return get_platform_name(a[0], a[1]) + ' version ' + a[1];
6060
}
6161

6262
window.onload = function() {
63-
for (var p in supported_versions) {
64-
var opt = document.createElement('option');
65-
opt.text = supported_versions[p];
66-
document.getElementById('version').add(opt);
67-
}
68-
69-
loadPlatforms();
70-
archChanged();
71-
}
72-
73-
function verChanged() {
74-
/* Just update like the architecture changed */
75-
archChanged();
76-
}
77-
78-
function loadPlatforms() {
79-
var platbox = document.getElementById('platform');
80-
81-
while (platbox.options.length > 0) {
82-
platbox.options.remove(0);
83-
}
84-
var opt = document.createElement('option');
85-
opt.text = '* Select your platform';
86-
opt.value = -1;
87-
platbox.add(opt);
88-
89-
platkeys = Object.keys(repodata['platforms']).sort();
90-
for (var pp in platkeys) {
91-
var opt = document.createElement('option');
92-
opt.text = get_platform_text(platkeys[pp]);
93-
opt.value = platkeys[pp];
94-
platbox.add(opt);
95-
}
96-
97-
platChanged();
63+
const platbox = document.getElementById('platform');
64+
const platkeys = Object.keys(repodata['platforms']).sort();
65+
66+
let opt = document.createElement('option');
67+
opt.text = '* Select your platform';
68+
opt.value = "-1";
69+
platbox.add(opt);
70+
71+
for (const pp in platkeys) {
72+
opt = document.createElement('option');
73+
opt.text = get_platform_text(platkeys[pp]);
74+
opt.value = platkeys[pp];
75+
platbox.add(opt);
76+
}
77+
78+
platChanged()
9879
}
9980

10081
function platChanged() {
101-
var plat = document.getElementById('platform').value;
102-
var archbox = document.getElementById('arch');
82+
const plat = document.getElementById('platform').value;
83+
const archbox = document.getElementById('arch');
10384

104-
while (archbox.options.length > 0) {
105-
archbox.options.remove(0);
106-
}
85+
while (archbox.options.length > 0) {
86+
archbox.options.remove(0);
87+
}
10788

108-
if (plat == -1) {
109-
archChanged();
110-
return;
111-
}
89+
if (!plat || plat === "-1") {
90+
archChanged();
91+
return;
92+
}
11293

113-
for (a in repodata['platforms'][plat].sort().reverse()) {
114-
var opt = document.createElement('option');
115-
opt.text = opt.value = repodata['platforms'][plat][a];
116-
archbox.add(opt);
117-
}
94+
let opt = document.createElement('option');
95+
opt.text = '* Select your architecture';
96+
opt.value = "-1";
97+
archbox.add(opt);
11898

119-
archChanged();
99+
for (const a in repodata['platforms'][plat].sort((a, b) => a['arch'].localeCompare(b['arch']))) {
100+
opt = document.createElement('option');
101+
opt.text = opt.value = repodata['platforms'][plat][a]['arch'];
102+
archbox.add(opt);
103+
}
104+
105+
archChanged();
120106
}
121107

122108
function archChanged() {
123-
var ver = document.getElementById('version').value;
124-
var plat = document.getElementById('platform').value;
125-
var arch = document.getElementById('arch').value;
126-
var scriptBox = document.getElementById('script-box')
127-
128-
if (!plat || plat == -1) {
129-
document.getElementById('copy-btn').style.display = 'none';
130-
scriptBox.innerHTML = 'Select version and platform above';
131-
return;
109+
const plat = document.getElementById('platform').value;
110+
const arch = document.getElementById('arch').value;
111+
const verbox = document.getElementById('version');
112+
113+
while (verbox.options.length > 0) {
114+
verbox.options.remove(0);
115+
}
116+
117+
if (!arch || arch === "-1") {
118+
verChanged();
119+
return;
120+
}
121+
122+
let opt = document.createElement('option');
123+
opt.text = '* Select your required PostgreSQL version';
124+
opt.value = "-1";
125+
verbox.add(opt);
126+
127+
let versions = []
128+
for (const a in repodata['platforms'][plat]) {
129+
if (repodata['platforms'][plat][a]['arch'] === arch) {
130+
versions = repodata['platforms'][plat][a]['versions']
131+
break
132132
}
133+
}
133134

134-
var pinfo = repodata['platforms'][plat];
135-
var shortver = ver.replace('.', '');
136-
137-
var url = 'https://download.postgresql.org/pub/repos/yum/reporpms/' + plat + '-' + arch + '/pgdg-' + get_rpm_prefix(plat) +'-repo-latest.noarch.rpm';
138-
139-
var installer = get_installer(plat);
140-
scriptBox.innerHTML = '# Install the repository RPM:\n';
141-
scriptBox.innerHTML += 'sudo ' + installer + ' install -y ' + url + '\n\n';
142-
143-
if (disable_module_on(plat)) {
144-
scriptBox.innerHTML += '# Disable the built-in PostgreSQL module:\n';
145-
scriptBox.innerHTML += 'sudo dnf -qy module disable postgresql\n\n';
146-
}
135+
for (const a in versions.sort()) {
136+
if (supported_versions.includes(parseInt(versions[a]))) {
137+
opt = document.createElement('option');
138+
opt.text = opt.value = versions[a];
139+
verbox.add(opt);
140+
}
141+
}
147142

148-
scriptBox.innerHTML += '# Install PostgreSQL:\n';
149-
scriptBox.innerHTML += 'sudo ' + installer + ' install -y postgresql' + shortver + '-server\n\n';
143+
verChanged();
144+
}
150145

151-
scriptBox.innerHTML += '# Optionally initialize the database and enable automatic start:\n';
152-
if (uses_systemd(plat)) {
153-
var setupcmd = 'postgresql-' + shortver + '-setup';
154-
if (ver < 10) {
155-
setupcmd = 'postgresql' + shortver + '-setup';
156-
}
157-
scriptBox.innerHTML += 'sudo /usr/pgsql-' + ver + '/bin/' + setupcmd + ' initdb\nsudo systemctl enable postgresql-' + ver + '\nsudo systemctl start postgresql-' + ver;
158-
}
159-
else {
160-
scriptBox.innerHTML += 'sudo service postgresql-' + ver + ' initdb\nsudo chkconfig postgresql-' + ver + ' on\nsudo service postgresql-' + ver + ' start';
161-
}
146+
function verChanged() {
147+
var ver = document.getElementById('version').value;
148+
var plat = document.getElementById('platform').value;
149+
var arch = document.getElementById('arch').value;
150+
var scriptBox = document.getElementById('script-box')
151+
152+
if (!ver || ver === "-1") {
153+
document.getElementById('copy-btn').style.display = 'none';
154+
scriptBox.innerHTML = 'Select platform, architecture, and version above';
155+
return;
156+
}
157+
158+
var shortver = ver.replace('.', '');
159+
160+
var url = 'https://download.postgresql.org/pub/repos/yum/reporpms/' + plat + '-' + arch + '/pgdg-' + get_rpm_prefix(plat) +'-repo-latest.noarch.rpm';
161+
162+
var installer = get_installer(plat);
163+
scriptBox.innerHTML = '# Install the repository RPM:\n';
164+
scriptBox.innerHTML += 'sudo ' + installer + ' install -y ' + url + '\n\n';
165+
166+
if (disable_module_on(plat)) {
167+
scriptBox.innerHTML += '# Disable the built-in PostgreSQL module:\n';
168+
scriptBox.innerHTML += 'sudo dnf -qy module disable postgresql\n\n';
169+
}
170+
171+
scriptBox.innerHTML += '# Install PostgreSQL:\n';
172+
scriptBox.innerHTML += 'sudo ' + installer + ' install -y postgresql' + shortver + '-server\n\n';
173+
174+
scriptBox.innerHTML += '# Optionally initialize the database and enable automatic start:\n';
175+
if (uses_systemd(plat)) {
176+
var setupcmd = 'postgresql-' + shortver + '-setup';
177+
if (ver < 10) {
178+
setupcmd = 'postgresql' + shortver + '-setup';
179+
}
180+
scriptBox.innerHTML += 'sudo /usr/pgsql-' + ver + '/bin/' + setupcmd + ' initdb\nsudo systemctl enable postgresql-' + ver + '\nsudo systemctl start postgresql-' + ver;
181+
}
182+
else {
183+
scriptBox.innerHTML += 'sudo service postgresql-' + ver + ' initdb\nsudo chkconfig postgresql-' + ver + ' on\nsudo service postgresql-' + ver + ' start';
184+
}
162185

163-
document.getElementById('copy-btn').style.display = 'block';
186+
document.getElementById('copy-btn').style.display = 'block';
164187
}
165188

166189
/* Event handlers */

templates/pages/download/linux/redhat.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ <h2>PostgreSQL Yum Repository</h2>
5454
To use the PostgreSQL Yum Repository, follow these steps:
5555
</p>
5656
<ol>
57-
<li>Select version: <select id="version" class="custom-select"></select><br/></li>
5857
<li>Select platform: <select id="platform" class="custom-select"></select></li>
5958
<li>Select architecture: <select id="arch" class="custom-select"></select></li>
59+
<li>Select version: <select id="version" class="custom-select"></select><br/></li>
6060
<li>Copy, paste and run the relevant parts of the setup script:
6161
<div class="pg-script-container">
6262
<pre id="script-box" class="code"></pre>

tools/ftp/spider_yum.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import re
66
import json
77
import requests
8+
import rpmfile
89
from collections import defaultdict
910
from tempfile import NamedTemporaryFile
1011

1112
re_platformdir = re.compile(r'^(\w+)-(\d+)-([^-]+)$')
13+
re_reporpm = re.compile(r'^pgdg-(\w+)-repo-latest.noarch.rpm$')
14+
re_repofile = re.compile(r'^./etc/yum.repos.d/pgdg-([a-zA-Z0-9]+)-all.repo$')
15+
re_reposection = re.compile(r'^\[pgdg([0-9][0-9])]$')
1216

1317
if __name__ == "__main__":
1418
parser = argparse.ArgumentParser(description="Spider repo RPMs")
@@ -21,10 +25,28 @@
2125
for repodir in os.listdir('{0}/reporpms'.format(args.yumroot)):
2226
m = re_platformdir.match(repodir)
2327
if m:
28+
# Find the latest repo RPM
29+
versions = []
30+
path = '{0}/reporpms/{1}'.format(args.yumroot, repodir)
31+
for reporpm in os.listdir(path):
32+
if re_reporpm.match(reporpm):
33+
with rpmfile.open('{0}/{1}'.format(path, reporpm)) as rpm:
34+
35+
# Find the repo config file
36+
for member in rpm.getmembers():
37+
if re_repofile.match(member.name):
38+
fd = rpm.extractfile(member.name)
39+
repos = str(fd.read()).split('\\n')
40+
41+
# Get the supported versions
42+
for repo in repos:
43+
if re_reposection.match(repo):
44+
versions.append(re_reposection.match(repo).group(1))
45+
2446
platname = m.group(1)
2547
platver = m.group(2)
2648
arch = m.group(3)
27-
platforms['{0}-{1}'.format(platname, platver)].append(arch)
49+
platforms['{0}-{1}'.format(platname, platver)].append({"arch": arch, "versions": versions})
2850

2951
j = json.dumps({'platforms': platforms})
3052

0 commit comments

Comments
 (0)