Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch npm from buildchroot to schroot #469

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 13 additions & 55 deletions classes/npm.bbclass
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
# cp package-lock.json /path/to/recipe/files/npm-shrinkwrap.json

inherit dpkg-raw
inherit buildchroot

SBUILD_FLAVOR = "npm"

NPMPN ?= "${PN}"
NPM_SHRINKWRAP ?= "file://npm-shrinkwrap.json"
Expand Down Expand Up @@ -70,43 +71,26 @@ python() {
d.setVar('NPM_MAPPED_NAME', mapped_name)
}

BUILDROOT = "${BUILDCHROOT_DIR}/${PP}"
SCHROOT_MOUNTS = "${WORKDIR}"

npm_fetch_do_mounts() {
mkdir -p ${BUILDROOT}
sudo mount --bind ${WORKDIR} ${BUILDROOT}

buildchroot_do_mounts
schroot_create_configs
insert_mounts
}

npm_fetch_undo_mounts() {
i=0
while ! sudo umount ${BUILDROOT}; do
sleep 0.1
if [ `expr $i % 100` -eq 0 ] ; then
bbwarn "${BUILDROOT}: Couldn't unmount ($i), retrying..."
fi
if [ $i -ge 10000 ]; then
bbfatal "${BUILDROOT}: Couldn't unmount after timeout"
fi
i=`expr $i + 1`
done
sudo rmdir ${BUILDROOT}
remove_mounts
schroot_delete_configs
}

def get_npm_bundled_tgz(d):
return "{0}-{1}-bundled.tgz".format(d.getVar('NPM_MAPPED_NAME'),
d.getVar('PV'))

def runcmd(d, cmd, dir):
def runcmd(d, cmd):
import subprocess
import os

uid = os.geteuid()
gid = os.getegid()
chrootcmd = "sudo -E chroot --userspec={0}:{1} ".format(uid, gid)
chrootcmd += d.getVar('BUILDCHROOT_DIR')
chrootcmd += " sh -c 'cd {0}/{1}; {2}'".format(d.getVar('PP'), dir, cmd)
chrootcmd = "schroot -c {0} -- {1}".format(d.getVar('SBUILD_CHROOT'), cmd)
bb.note("Running " + chrootcmd)
(retval, output) = subprocess.getstatusoutput(chrootcmd)
if retval:
Expand All @@ -124,34 +108,6 @@ def apply_mirrors_in_shrinkwrap(path, pattern, subst):
with open(path, 'w') as f:
json.dump(data, f, indent=2)

do_install_npm() {
install_cmd="sudo -E chroot ${BUILDCHROOT_DIR} \
apt-get install -y -o Debug::pkgProblemResolver=yes \
--no-install-recommends"

npm_fetch_do_mounts

E="${@ bb.utils.export_proxies(d)}"
deb_dl_dir_import "${BUILDCHROOT_DIR}" ${BASE_DISTRO}-${BASE_DISTRO_CODENAME}
sudo -E chroot ${BUILDCHROOT_DIR} \
apt-get update \
-o Dir::Etc::sourcelist="sources.list.d/isar-apt.list" \
-o Dir::Etc::sourceparts="-" \
-o APT::Get::List-Cleanup="0"
${install_cmd} --download-only ${NPM_CLASS_PACKAGE}
deb_dl_dir_export "${BUILDCHROOT_DIR}" ${BASE_DISTRO}-${BASE_DISTRO_CODENAME}
${install_cmd} ${NPM_CLASS_PACKAGE}

npm_fetch_undo_mounts
}
do_install_npm[depends] += "${BUILDCHROOT_DEP}"
do_install_npm[depends] += "${@d.getVarFlag('do_apt_fetch', 'depends')}"
do_install_npm[depends] += "${@(d.getVar('NPM_CLASS_PACKAGE') + ':do_deploy_deb') if d.getVar('OWN_NPM_CLASS_PACKAGE') == '1' else ''}"
do_install_npm[lockfiles] += "${REPO_ISAR_DIR}/isar.lock"
do_install_npm[network] += "${TASK_USE_SUDO}"

addtask install_npm before do_fetch

python fetch_npm() {
import json, os, shutil, re

Expand Down Expand Up @@ -211,7 +167,7 @@ python fetch_npm() {
json_objs = {'dependencies': { npmpn: '' }}
json.dump(json_objs, outfile, indent=2)

runcmd(d, "npm ci --global-style --ignore-scripts --verbose", "fetch-tmp")
runcmd(d, "npm ci --global-style --ignore-scripts --verbose")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dropping "fetch-tmp"?

Copy link
Collaborator

@jan-kiszka jan-kiszka Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that is because we are running this in a schroot, no longer in WORKDIR. But then there are more places where this became obsolete. Still looks inconsistent.


package_filename = "node_modules/" + npmpn + "/package.json"
with open(package_filename) as infile:
Expand All @@ -227,17 +183,19 @@ python fetch_npm() {

os.rename("node_modules/" + npmpn, "package")

runcmd(d, "tar czf package.tgz --exclude .bin package", "fetch-tmp")
runcmd(d, "tar czf package.tgz --exclude .bin package")

shutil.copyfile("package.tgz", bundled_tgz)
with open(bundled_tgz_hash, 'w') as hash_file:
hash_file.write(fetch_hash)

os.chdir(old_cwd)

bb.build.exec_func("npm_fetch_undo_mounts", d)
}
do_fetch[postfuncs] += "fetch_npm"
do_fetch[cleandirs] += "${WORKDIR}/fetch-tmp"
do_fetch[depends] += "${SCHROOT_DEP}"

python clean_npm() {
import os
Expand Down