[gimp-web] tools: update-mirrors.py now uses the new infrastructure.



commit 096787bb8890a8b619c684219ae231949597e49b
Author: Jehan <jehan girinstud io>
Date:   Fri Sep 2 23:52:43 2022 +0200

    tools: update-mirrors.py now uses the new infrastructure.
    
    Note that generated downloads.http.txt will now be
    alphabetically-ordered by URL, as `mirrorbits list` command may return
    the mirrors in any order (and we want to avoid rewriting the file every
    time we check mirrors).
    
    Also we will now use /pub/ as our new root, because it's how the URL is
    set in Mirrorbits.

 tools/downloads/update-mirrors.py | 106 ++++++++++++++++++++++++++++++++------
 1 file changed, 91 insertions(+), 15 deletions(-)
---
diff --git a/tools/downloads/update-mirrors.py b/tools/downloads/update-mirrors.py
index f6eaae91..69898dcd 100755
--- a/tools/downloads/update-mirrors.py
+++ b/tools/downloads/update-mirrors.py
@@ -3,33 +3,106 @@
 import argparse
 import importlib
 import os
-import paramiko
+import os.path
+import requests
+import subprocess
 import sys
+import tarfile
 
 parser = argparse.ArgumentParser(description='Update the list of mirrors as per download.gimp.org 
configuration.')
 parser.add_argument('--mirrorsfile', metavar='<file>', default=os.path.dirname(__file__) + 
'/downloads.http.txt',
                     help='A file with one download mirror per line, either https:// or http://. This script 
will override the existing file.')
-parser.add_argument('--ssh-user', metavar='<ssh-user>', default=None, required=True,
-                    help='Your SSH user to download.gimp.org aka master.gnome.org server.')
+parser.add_argument('--oc-token', metavar='<oc-token>', default=None, required=False,
+                    help='Your OpenShift connection token.')
+parser.add_argument('--keep-oc', action='store_true',
+                    help='Do not delete the downloaded oc client.')
+parser.add_argument('--no-download-oc', dest='download_oc', action='store_false',
+                    help='Use a previously downloaded oc client.')
 
 args = parser.parse_args()
 
-if args.ssh_user is None:
-  sys.stderr.write('No SSH user provided\n')
+oc = os.path.dirname(__file__) + '/oc'
+oc_tar = oc + '.tar'
+
+if args.download_oc:
+  if os.path.exists(oc):
+    sys.stderr.write('The file "{}" already exists. Please delete it.\n'.format(oc))
+    sys.exit(os.EX_CANTCREAT)
+  if os.path.exists(oc_tar):
+    sys.stderr.write('The file "{}" already exists. Please delete it.\n'.format(oc_tar))
+    sys.exit(os.EX_CANTCREAT)
+
+  print('Downloading OpenShift client…')
+  with requests.get("https://downloads-openshift-console.apps.openshift4.gnome.org/amd64/linux/oc.tar";, 
stream=True) as r:
+    with open(oc_tar, 'wb') as fd:
+      for chunk in r.iter_content(chunk_size=65536):
+        fd.write(chunk)
+
+  print('Extracting OpenShift client…')
+  with tarfile.open(oc_tar) as tar:
+    tar.extract('oc', os.path.dirname(__file__))
+
+  os.remove(oc_tar)
+
+if not os.path.exists(oc):
+  if args.download_oc:
+    sys.stderr.write("The file '{}' didn't exists. Download failed.\n".format(oc))
+  else:
+    sys.stderr.write("The file '{}' didn't exists. Do not use --no-download-oc option.\n".format(oc))
+  sys.exit(os.EX_UNAVAILABLE)
+
+if args.oc_token is not None:
+  print('Logging-in to OpenShift…\n')
+  try:
+    subprocess.check_call(oc + " login --token={} 
--server=https://api.openshift4.gnome.org:6443".format(args.oc_token),
+                          shell=True)
+  except subprocess.CalledProcessError:
+    sys.stderr.write('Failed connection. Check your token.\n')
+    sys.exit(os.EX_NOUSER)
+
+print('Switch to GIMP project…')
+try:
+  subprocess.check_call(oc + " project gimp", shell=True)
+except subprocess.CalledProcessError:
+  sys.stderr.write('Failed to switch to "gimp" project. Do you have access?\n')
+  sys.exit(os.EX_NOUSER)
+
+sys.stdout.write('Obtaining last pod name…')
+try:
+  output = subprocess.check_output(oc + " get pods --selector app=mirrorbits", shell=True)
+  lines = output.decode('utf-8').split('\n')
+  podname = [ line.split() for line in lines if line.startswith('mirrorbits-') ][-1][0]
+except subprocess.CalledProcessError:
+  sys.stderr.write('\nOpenShift command failed. Are you logged-in?\n')
+  sys.stderr.write('* Go to https://console-openshift-console.apps.openshift4.gnome.org/\n')
+  sys.stderr.write('* Log-in in your browser.\n')
+  sys.stderr.write('* Click your name in top-right of OpenShift page.\n')
+  sys.stderr.write('* Select "Copy login command".\n')
+  sys.stderr.write('* Click "Display Token".\n')
+  sys.stderr.write('* Call me again adding --oc-token <the-copied-token>.\n')
   sys.exit(os.EX_NOUSER)
 
-ssh = paramiko.SSHClient()
-ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
-ssh.load_system_host_keys()
-cmd = "cat /etc/httpd/download.gimp.org.map |grep '^mirrors\|^video' | sed 
's$^\(mirrors\|video\)\\s*\([^\s]\)$https://\\2$' | sed 's$|$\\nhttps://$g' | sort -u"
-ssh.connect('download.gimp.org', username=args.ssh_user)
-# It will use your keys automatically.
-ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
-mirrors=ssh_stdout.read()
+print(' {}'.format(podname))
+
+print('Obtaining mirror list…')
+try:
+  output = subprocess.check_output(oc + " exec {}  -- mb list -http -rsync -ftp".format(podname), shell=True)
+  lines = output.decode('utf-8').split('\n')
+
+  for l in lines:
+    if l.startswith('Identifier'):
+      lines = lines[lines.index(l)+1:]
+      break
+  # Alphabetically ordered mirror list.
+  mirrors = [ line.split()[1] for line in lines if line.strip() != '' ]
+  mirrors.sort()
+except subprocess.CalledProcessError:
+  sys.stderr.write('\nOpenShift command failed. Please check the code.\n')
+  sys.exit(os.EX_UNAVAILABLE)
 
 with open(args.mirrorsfile, 'wb') as f:
-  f.write(mirrors)
-  f.write(b'https://download.gimp.org/gimp/\n')
+  for http in mirrors:
+    f.write(http.encode('utf-8') + b'\n')
 
 print("Mirrors updated in {}.".format(args.mirrorsfile))
 
@@ -39,6 +112,9 @@ print("Now comparing to {} ...".format(mirrorsjson))
 cmp = importlib.import_module("cmp-mirrors")
 same = cmp.cmp_mirror_files(args.mirrorsfile, mirrorsjson)
 
+if not args.keep_oc:
+  os.remove(oc)
+
 if same:
   print("Both files look to match. Please double-check before committing the change.")
   sys.exit(os.EX_OK)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]