diff mbox series

rteval: Add option for downloading kernel

Message ID 20220713202531.398854-1-magodse@redhat.com
State New
Headers show
Series rteval: Add option for downloading kernel | expand

Commit Message

Manasi Godse July 13, 2022, 8:25 p.m. UTC
Added an option -S, --source-download to download a kernel source
tarball from kernel.org to the appropriate loadsource directory.
Acceptable strings for the kernel version are 'linux-5.18.1.tar.xz;,
'linux-5.18.1.tar.gz', 'linux-5.18.1', '5.18.1', 'linux-5.19-rc5'.

Conditions have been added to check for default kernel packaged with
rteval-loads and backing up the tarball if it already exists. This
enhancement simplifies the  manual downloading of kernel to the loadsource
directory.

Signed-off-by: Manasi Godse <magodse@redhat.com>
---
 rteval-cmd             | 61 ++++++++++++++++++++++++++++++++++++++++++
 rteval/rtevalConfig.py |  1 +
 2 files changed, 62 insertions(+)

Comments

Leah Leshchinsky July 15, 2022, 4:25 p.m. UTC | #1
Tested with -S options linux-5.19-rc6, linux-5.17.1.tar.xz, linux-5.17.1.tar.gz, linux-5.17.1, 5.17.1,
results in proper download. Errors properly when -S option specified without argument.
Error message produced when kernel version specified is already downloaded. Option to overwrite properly
creates .bkup file in directory while download is in process, and deletes .bkup file once download is complete.
su -c "./rteval-cmd --duration=1s -S linux-5.17.1.gz -S linux-5.18.1"
results in evaluation of second argument. When download matches default kernel, does not download kernel again.
Various misspellings are also handled (llinux--5.17.1..gz). Not found versions result in error message.

Currently, the program exits after the download is complete. Future improvements may include allowing for additional
actions once the -S option is executed.

Tested-by: Leah Leshchinsky <lleshchi@redhat.com>
John Kacur July 15, 2022, 4:56 p.m. UTC | #2
On Wed, 13 Jul 2022, Manasi Godse wrote:

> Added an option -S, --source-download to download a kernel source
> tarball from kernel.org to the appropriate loadsource directory.
> Acceptable strings for the kernel version are 'linux-5.18.1.tar.xz;,
> 'linux-5.18.1.tar.gz', 'linux-5.18.1', '5.18.1', 'linux-5.19-rc5'.
> 
> Conditions have been added to check for default kernel packaged with
> rteval-loads and backing up the tarball if it already exists. This
> enhancement simplifies the  manual downloading of kernel to the loadsource
> directory.
> 
> Signed-off-by: Manasi Godse <magodse@redhat.com>
> ---
>  rteval-cmd             | 61 ++++++++++++++++++++++++++++++++++++++++++
>  rteval/rtevalConfig.py |  1 +
>  2 files changed, 62 insertions(+)
> 
> diff --git a/rteval-cmd b/rteval-cmd
> index 4598ba514ddc..dcd06dc30ba4 100755
> --- a/rteval-cmd
> +++ b/rteval-cmd
> @@ -37,8 +37,11 @@
>  import sys
>  import os
>  import time
> +import re
> +import shutil
>  import optparse
>  import tempfile
> +import requests
>  import lxml.etree
>  from rteval.Log import Log
>  from rteval import RtEval, rtevalConfig
> @@ -46,6 +49,7 @@ from rteval.modules.loads import LoadModules
>  from rteval.modules.measurement import MeasurementModules
>  from rteval.version import RTEVAL_VERSION
>  from rteval.misc import invert_cpulist, compress_cpulist
> +from rteval.modules.loads.kcompile import ModuleParameters
>  
>  def summarize(repfile, xslt):
>      """ Summarize an already existing XML report """
> @@ -158,6 +162,10 @@ def parse_options(cfg, parser, cmdargs):
>      parser.add_option("-V", "--version", dest="rteval___version",
>                        action='store_true', default=False,
>                        help='print rteval version and exit')
> +    parser.add_option("-S", "--source-download", dest="rteval___srcdownload",
> +                        type="string", default=None, metavar="KERNEL_VERSION",
> +                        help='download a source kernel from kernel.org')
> +
>  
>      if not cmdargs:
>          cmdargs = ["--help"]
> @@ -254,6 +262,59 @@ if __name__ == '__main__':
>          measuremods.SetupModuleOptions(parser)
>          cmd_args = parse_options(config, parser, sys.argv[1:])
>  
> +        # download kernel tarball
> +        if rtevcfg.srcdownload:
> +            logger.log(Log.DEBUG, f"Kernel Version to download = {rtevcfg.srcdownload}")
> +
> +            # handle a kernel version like linux-5.19-rc5
> +            if 'rc' in rtevcfg.srcdownload:
> +                kernel_prefix = re.search(r"\d{1,2}\.\d{1,3}\-[a-z]*\d{1,2}", rtevcfg.srcdownload).group(0)
> +                url = "https://git.kernel.org/torvalds/t/"
> +            else:
> +                kernel_prefix = re.search(r"\d{1,2}\.\d{1,3}\.*\d{1,2}", rtevcfg.srcdownload).group(0)
> +                major_version = re.search(r"\d{1,2}", kernel_prefix).group(0)
> +                url = "https://kernel.org/pub/linux/kernel/v" + major_version + ".x/"
> +
> +
> +            if rtevcfg.srcdownload.endswith(".gz") or 'rc' in rtevcfg.srcdownload:
> +                rtevcfg.srcdownload = "linux-" + kernel_prefix + ".tar.gz"
> +            else:
> +                rtevcfg.srcdownload = "linux-" + kernel_prefix + ".tar.xz"
> +            tarfl = os.path.join(rtevcfg.srcdir, rtevcfg.srcdownload)
> +
> +            # if default kernel packages with rteval-loads exists, do not download/overwrite
> +            default_kernel_file = ModuleParameters().get('source').get('default')
> +            if os.path.exists(tarfl):
> +                if rtevcfg.srcdownload == default_kernel_file:
> +                    sys.exit("Default kernel already exists, will not download")
> +                prompt = input("Kernel already exists, download and overwrite anyways? (y/n)  ")
> +                prompt = prompt.lower()
> +                if prompt in ('no', 'n'):
> +                    sys.exit("Exiting")
> +                elif prompt in ('yes','y'):
> +                    # backup the existing kernel in case it needs to be restored later
> +                    shutil.move(tarfl, tarfl + ".bkup")
> +                else:
> +                    sys.exit("Invalid option. Exiting")
> +
> +            url = url + rtevcfg.srcdownload
> +            print(f"Downloading kernel {url}")
> +            downloaded_file = requests.get(url)
> +            if downloaded_file.status_code != 200:
> +                # restore the kernel file if it exists
> +                if os.path.exists(tarfl + ".bkup"):
> +                    shutil.move(tarfl + ".bkup", tarfl)
> +                sys.exit(f"Could not download tar file {rtevcfg.srcdownload}, status code {downloaded_file.status_code}")
> +            with open(tarfl, 'wb') as fd:
> +                fd.write(downloaded_file.content)
> +            logger.log(Log.DEBUG, f"Kernel source {rtevcfg.srcdownload} downloaded successfully")
> +            logger.log(Log.DEBUG, f"Downloaded to directory location: {rtevcfg.srcdir}")
> +            # download was successful, delete the backup file if it exists
> +            if os.path.exists(tarfl + ".bkup"):
> +                os.remove(tarfl + ".bkup")
> +            sys.exit(0)
> +
> +
>          # if we only specified one set of cpus (loads or measurement)
>          # default the other to the inverse of the specified list
>          ldcfg = config.GetSection('loads')
> diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py
> index 56bbc9ee0de6..decd36ed18ab 100644
> --- a/rteval/rtevalConfig.py
> +++ b/rteval/rtevalConfig.py
> @@ -97,6 +97,7 @@ default_config = {
>          'xslt_histogram': default_config_search(['rteval_histogram_raw.xsl'], os.path.isfile),
>          'report_interval': '600',
>          'logging'    : False,
> +        'srcdownload': None,
>          }
>      }
>  
> -- 
> 2.31.1
> 
> 
    Tested-by: Leah Leshchinsky <lleshchi@redhat.com>
    - Two small tweaks, non-standard "anyways" changed to "anyway", and
      added the words "and exit" to the end of the help option
    Signed-off-by: John Kacur <jkacur@redhat.com>
diff mbox series

Patch

diff --git a/rteval-cmd b/rteval-cmd
index 4598ba514ddc..dcd06dc30ba4 100755
--- a/rteval-cmd
+++ b/rteval-cmd
@@ -37,8 +37,11 @@ 
 import sys
 import os
 import time
+import re
+import shutil
 import optparse
 import tempfile
+import requests
 import lxml.etree
 from rteval.Log import Log
 from rteval import RtEval, rtevalConfig
@@ -46,6 +49,7 @@  from rteval.modules.loads import LoadModules
 from rteval.modules.measurement import MeasurementModules
 from rteval.version import RTEVAL_VERSION
 from rteval.misc import invert_cpulist, compress_cpulist
+from rteval.modules.loads.kcompile import ModuleParameters
 
 def summarize(repfile, xslt):
     """ Summarize an already existing XML report """
@@ -158,6 +162,10 @@  def parse_options(cfg, parser, cmdargs):
     parser.add_option("-V", "--version", dest="rteval___version",
                       action='store_true', default=False,
                       help='print rteval version and exit')
+    parser.add_option("-S", "--source-download", dest="rteval___srcdownload",
+                        type="string", default=None, metavar="KERNEL_VERSION",
+                        help='download a source kernel from kernel.org')
+
 
     if not cmdargs:
         cmdargs = ["--help"]
@@ -254,6 +262,59 @@  if __name__ == '__main__':
         measuremods.SetupModuleOptions(parser)
         cmd_args = parse_options(config, parser, sys.argv[1:])
 
+        # download kernel tarball
+        if rtevcfg.srcdownload:
+            logger.log(Log.DEBUG, f"Kernel Version to download = {rtevcfg.srcdownload}")
+
+            # handle a kernel version like linux-5.19-rc5
+            if 'rc' in rtevcfg.srcdownload:
+                kernel_prefix = re.search(r"\d{1,2}\.\d{1,3}\-[a-z]*\d{1,2}", rtevcfg.srcdownload).group(0)
+                url = "https://git.kernel.org/torvalds/t/"
+            else:
+                kernel_prefix = re.search(r"\d{1,2}\.\d{1,3}\.*\d{1,2}", rtevcfg.srcdownload).group(0)
+                major_version = re.search(r"\d{1,2}", kernel_prefix).group(0)
+                url = "https://kernel.org/pub/linux/kernel/v" + major_version + ".x/"
+
+
+            if rtevcfg.srcdownload.endswith(".gz") or 'rc' in rtevcfg.srcdownload:
+                rtevcfg.srcdownload = "linux-" + kernel_prefix + ".tar.gz"
+            else:
+                rtevcfg.srcdownload = "linux-" + kernel_prefix + ".tar.xz"
+            tarfl = os.path.join(rtevcfg.srcdir, rtevcfg.srcdownload)
+
+            # if default kernel packages with rteval-loads exists, do not download/overwrite
+            default_kernel_file = ModuleParameters().get('source').get('default')
+            if os.path.exists(tarfl):
+                if rtevcfg.srcdownload == default_kernel_file:
+                    sys.exit("Default kernel already exists, will not download")
+                prompt = input("Kernel already exists, download and overwrite anyways? (y/n)  ")
+                prompt = prompt.lower()
+                if prompt in ('no', 'n'):
+                    sys.exit("Exiting")
+                elif prompt in ('yes','y'):
+                    # backup the existing kernel in case it needs to be restored later
+                    shutil.move(tarfl, tarfl + ".bkup")
+                else:
+                    sys.exit("Invalid option. Exiting")
+
+            url = url + rtevcfg.srcdownload
+            print(f"Downloading kernel {url}")
+            downloaded_file = requests.get(url)
+            if downloaded_file.status_code != 200:
+                # restore the kernel file if it exists
+                if os.path.exists(tarfl + ".bkup"):
+                    shutil.move(tarfl + ".bkup", tarfl)
+                sys.exit(f"Could not download tar file {rtevcfg.srcdownload}, status code {downloaded_file.status_code}")
+            with open(tarfl, 'wb') as fd:
+                fd.write(downloaded_file.content)
+            logger.log(Log.DEBUG, f"Kernel source {rtevcfg.srcdownload} downloaded successfully")
+            logger.log(Log.DEBUG, f"Downloaded to directory location: {rtevcfg.srcdir}")
+            # download was successful, delete the backup file if it exists
+            if os.path.exists(tarfl + ".bkup"):
+                os.remove(tarfl + ".bkup")
+            sys.exit(0)
+
+
         # if we only specified one set of cpus (loads or measurement)
         # default the other to the inverse of the specified list
         ldcfg = config.GetSection('loads')
diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py
index 56bbc9ee0de6..decd36ed18ab 100644
--- a/rteval/rtevalConfig.py
+++ b/rteval/rtevalConfig.py
@@ -97,6 +97,7 @@  default_config = {
         'xslt_histogram': default_config_search(['rteval_histogram_raw.xsl'], os.path.isfile),
         'report_interval': '600',
         'logging'    : False,
+        'srcdownload': None,
         }
     }