[gimp-web] tools: move the OC connection into a separate module file.



commit c48323d598df11797e1a6842b1d0896005100c6a
Author: Jehan <jehan girinstud io>
Date:   Sun Sep 25 21:57:13 2022 +0200

    tools: move the OC connection into a separate module file.
    
    It will allow to easily reuse these pieces of code from other scripts.
    For instance, I'd like to make scripts for statistics gathering from the
    mirrorbits remote instance.

 tools/downloads/gimpoc.py         | 153 ++++++++++++++++++++++++++++++++++++++
 tools/downloads/update-mirrors.py | 103 +++----------------------
 2 files changed, 162 insertions(+), 94 deletions(-)
---
diff --git a/tools/downloads/gimpoc.py b/tools/downloads/gimpoc.py
new file mode 100644
index 00000000..84fe84bf
--- /dev/null
+++ b/tools/downloads/gimpoc.py
@@ -0,0 +1,153 @@
+#!/usr/bin/env python3
+
+"""
+gimpoc.py -- Module to interact with GIMP's OpenShift instance
+Copyright (C) 2022 Jehan
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <https://www.gnu.org/licenses/>.
+"""
+
+import os
+import requests
+import subprocess
+import sys
+import tarfile
+
+class Error(Exception):
+  '''
+  Exception raised by API in this module.
+
+  Attributes:
+      code: exit code which we advise to use with sys.exit() if you
+            consider this a fatale error.
+      message: message to output on stderr.
+  '''
+
+  def __init__(self, code, message):
+      self.code = code
+      self.message = message
+      super().__init__(self.message)
+
+oc = os.path.dirname(__file__) + '/oc'
+
+def setup(keep_oc, download_oc):
+  '''
+  Setting up the local oc environment.
+
+  Returns: an updated keep_oc value to be used with cleanup() function once you
+  are done.
+  '''
+  oc_tar = oc + '.tar'
+
+  if keep_oc is None:
+    if download_oc:
+      keep_oc = False
+    else:
+      keep_oc = True
+
+  if download_oc:
+    if os.path.exists(oc):
+      raise Error(os.EX_CANTCREAT,
+                  'The file "{}" already exists. Please delete it or run with 
--no-download-oc.\n'.format(oc))
+    if os.path.exists(oc_tar):
+      raise Error(os.EX_CANTCREAT,
+                 'The file "{}" already exists. Please delete it.\n'.format(oc_tar))
+
+    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 download_oc:
+      err_msg = "The file '{}' didn't exists. Download failed.\n".format(oc)
+    else:
+      err_msg = "The file '{}' didn't exists. Do not use --no-download-oc option.\n".format(oc)
+    raise Error(os.EX_UNAVAILABLE, err_msg)
+  
+  return keep_oc
+
+def cleanup(keep_oc):
+  if not keep_oc:
+    os.remove(oc)
+
+def connect(oc_token):
+  '''
+  Connecting to OpenShift.
+
+  Returns: the podname to use in further commands.
+  '''
+  if 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(oc_token),
+                            shell=True)
+    except subprocess.CalledProcessError:
+      raise Error(os.EX_NOUSER, 'Failed connection. Check your token.\n')
+
+  print('Switch to GIMP project…')
+  try:
+    subprocess.check_call(oc + " project gimp", shell=True)
+  except subprocess.CalledProcessError:
+    err_msg = '\nOpenShift command failed. Are you logged-in?\n'
+    err_msg += '* Go to https://console-openshift-console.apps.openshift4.gnome.org/\n'
+    err_msg += '* Log-in in your browser.\n'
+    err_msg += '* Click your name in top-right of OpenShift page.\n'
+    err_msg += '* Select "Copy login command".\n'
+    err_msg += '* Click "Display Token".\n'
+    err_msg += '* Call me again adding --oc-token <the-copied-token>.\n\n'
+    err_msg += 'If you are logged-in already, failure to switch to "gimp" project may indicate an access 
issue.\n'
+    raise Error(os.EX_NOUSER, err_msg)
+
+  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:
+    err_msg = '\nOpenShift command failed. Are you logged-in?\n'
+    err_msg += '* Go to https://console-openshift-console.apps.openshift4.gnome.org/\n'
+    err_msg += '* Log-in in your browser.\n'
+    err_msg += '* Click your name in top-right of OpenShift page.\n'
+    err_msg += '* Select "Copy login command".\n'
+    err_msg += '* Click "Display Token".\n'
+    err_msg += '* Call me again adding --oc-token <the-copied-token>.\n'
+    raise Error(os.EX_NOUSER, err_msg)
+
+  print(' {}'.format(podname))
+  return podname
+
+def get_mirrors(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()
+    return mirrors
+  except subprocess.CalledProcessError:
+    raise Error(os.EX_UNAVAILABLE, 'OpenShift command failed. Please check the code.\n')
diff --git a/tools/downloads/update-mirrors.py b/tools/downloads/update-mirrors.py
index aa674f08..408aa0b3 100755
--- a/tools/downloads/update-mirrors.py
+++ b/tools/downloads/update-mirrors.py
@@ -2,13 +2,11 @@
 
 import argparse
 import importlib
+import gimpoc
 import json
 import os
 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',
@@ -22,97 +20,17 @@ parser.add_argument('--no-download-oc', dest='download_oc', action='store_false'
 
 args = parser.parse_args()
 
-oc = os.path.dirname(__file__) + '/oc'
-oc_tar = oc + '.tar'
-
-if args.keep_oc is None:
-  if args.download_oc:
-    args.keep_oc = False
-  else:
-    args.keep_oc = True
-
-if args.download_oc:
-  if os.path.exists(oc):
-    sys.stderr.write('The file "{}" already exists. Please delete it or run with 
--no-download-oc.\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('\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\n')
-  sys.stderr.write('If you are logged-in already, failure to switch to "gimp" project may indicate an access 
issue.\n')
-  sys.exit(os.EX_NOUSER)
-
-sys.stdout.write('Obtaining last pod name…')
+keep_oc = args.keep_oc
 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)
+  keep_oc = gimpoc.setup(keep_oc, args.download_oc)
+  podname = gimpoc.connect(args.oc_token)
+except gimpoc.Error as err:
+  sys.stderr.write(err.message)
+  sys.exit(err.code)
 
-print(' {}'.format(podname))
+mirrors = gimpoc.get_mirrors(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)
+gimpoc.cleanup(keep_oc)
 
 with open(args.mirrorsfile, 'wb') as f:
   for http in mirrors:
@@ -131,9 +49,6 @@ except json.decoder.JSONDecodeError as error:
   sys.stderr.write("Invalid JSON file '{}': {}\n".format(mirrorsjson, error))
   sys.exit(os.EX_DATAERR)
 
-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]