public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Anton Mikanovich <amikan@ilbers.de>
To: isar-users@googlegroups.com
Cc: Anton Mikanovich <amikan@ilbers.de>
Subject: [PATCH 2/2] meta: Cover bash isar-apt usages by isar.lock
Date: Fri, 12 Jul 2024 09:56:46 +0300	[thread overview]
Message-ID: <20240712065646.2156320-3-amikan@ilbers.de> (raw)
In-Reply-To: <20240712065646.2156320-1-amikan@ilbers.de>

Cover isar-apt repo usages by ${REPO_ISAR_DIR}/isar.lock to prevent any
conflicts or races.

Introduce lockrun.py helper script to execute bash code covered by
fcntl locks in bitbake compatible way. It means the same lock files as
used in lockfiles task flag can be used with lockrun.py.
Key -r is used for shared locking on read operations.

Usage examples:

${SCRIPTSDIR}/lockrun.py -r -f "${REPO_ISAR_DIR}/isar.lock" -c "true"

${SCRIPTSDIR}/lockrun.py -r -f "${REPO_ISAR_DIR}/isar.lock" -s <<EOF
     true
EOF

Signed-off-by: Anton Mikanovich <amikan@ilbers.de>
---
 meta/classes/deb-dl-dir.bbclass            |  3 +-
 meta/classes/image-tools-extension.bbclass |  2 +
 scripts/lockrun.py                         | 44 ++++++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100755 scripts/lockrun.py

diff --git a/meta/classes/deb-dl-dir.bbclass b/meta/classes/deb-dl-dir.bbclass
index 2d8739dc..55e56c50 100644
--- a/meta/classes/deb-dl-dir.bbclass
+++ b/meta/classes/deb-dl-dir.bbclass
@@ -90,7 +90,8 @@ deb_dl_dir_export() {
     export owner=$(id -u):$(id -g)
     mkdir -p "${pc}"
 
-    isar_debs="\$(find '${REPO_ISAR_DIR}/${DISTRO}' -name '*.deb' -print)"
+    isar_debs="$(${SCRIPTSDIR}/lockrun.py -r -f '${REPO_ISAR_DIR}/isar.lock' -c \
+    "find '${REPO_ISAR_DIR}/${DISTRO}' -name '*.deb' -print")"
 
     flock "${pc}".lock sudo -Es << 'EOSUDO'
         set -e
diff --git a/meta/classes/image-tools-extension.bbclass b/meta/classes/image-tools-extension.bbclass
index 46bdf78b..e8ace8f5 100644
--- a/meta/classes/image-tools-extension.bbclass
+++ b/meta/classes/image-tools-extension.bbclass
@@ -46,6 +46,7 @@ imager_run() {
 
         E="${@ isar_export_proxies(d)}"
         deb_dl_dir_import ${schroot_dir} ${distro}
+        ${SCRIPTSDIR}/lockrun.py -r -f "${REPO_ISAR_DIR}/isar.lock" -s <<EOAPT
         schroot -r -c ${session_id} -d / -u root -- sh -c " \
             apt-get update \
                 -o Dir::Etc::SourceList='sources.list.d/isar-apt.list' \
@@ -54,6 +55,7 @@ imager_run() {
             apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends -y \
                 --allow-unauthenticated --allow-downgrades --download-only install \
                 ${local_install}"
+EOAPT
 
         deb_dl_dir_export ${schroot_dir} ${distro}
         schroot -r -c ${session_id} -d / -u root -- sh -c " \
diff --git a/scripts/lockrun.py b/scripts/lockrun.py
new file mode 100755
index 00000000..58a598dd
--- /dev/null
+++ b/scripts/lockrun.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+#
+# Helper script to use bitbake locks in shell
+# Copyright (c) 2024, ilbers GmbH
+
+import argparse
+import os
+import subprocess
+import sys
+
+sys.path.append(os.path.join(os.path.dirname(__file__), '../bitbake/lib'))
+
+from bb import utils
+
+
+def parse_args():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-f', '--file', help='Lock file name.', required=True)
+    parser.add_argument(
+        '-r', '--read', action="store_true", help='Use read (shared) locking.'
+    )
+    group = parser.add_mutually_exclusive_group(required=True)
+    group.add_argument('-c', '--command', help='Command(s) to execute.')
+    group.add_argument(
+        '-s',
+        '--shell',
+        action="store_true",
+        help='Execute commands from stdin.',
+    )
+    return parser.parse_args()
+
+
+if __name__ == "__main__":
+    args = parse_args()
+
+    with utils.fileslocked([args.file], shared=args.read):
+        if args.shell:
+            cmd = sys.stdin.read()
+        else:
+            cmd = args.command
+        try:
+            subprocess.run(cmd, check=True, shell=True)
+        except subprocess.CalledProcessError as e:
+            exit(e.returncode)
-- 
2.34.1

-- 
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/isar-users/20240712065646.2156320-3-amikan%40ilbers.de.

  parent reply	other threads:[~2024-07-12  6:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-12  6:56 [PATCH 0/2] Protect isar-apt usages in shell Anton Mikanovich
2024-07-12  6:56 ` [PATCH 1/2] deb-dl-dir: Avoid multiple find run on export Anton Mikanovich
2024-07-12  6:56 ` Anton Mikanovich [this message]
2024-07-23  7:38 ` [PATCH 0/2] Protect isar-apt usages in shell Uladzimir Bely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240712065646.2156320-3-amikan@ilbers.de \
    --to=amikan@ilbers.de \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox