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

Rewrite toolset.sh in Python #219

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.toolset.yml
.idea
.PKGINFO
.SIGN.RSA.alpine-*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Restricts Pieman to only preparing or upgrading the toolset which is located in

Specifies the time zone of the system.

##### TOOLSET_CODENAME="v4-amy"
##### TOOLSET_CODENAME="v5-farnsworth"

Specifies the toolset codename. The parameter allows users and developers to switch between different toolsets. Each codename is connected to its directory in `${TOOLSET_DIR}` which, in turn, contains the target toolset. When a codename is passed via `${TOOLSET_CODENAME}` but there is no such directory in `${TOOLSET_DIR}`, the process of creating of the directory and installing the toolset into it will be initiated.

Expand Down
2 changes: 1 addition & 1 deletion essentials.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ MENDER_CLIENT_REVISION="1.7.x"

PIEMAN_MAJOR_VER=0

PIEMAN_MINOR_VER=18
PIEMAN_MINOR_VER=19

PYTHON_MAJOR_VER=3

Expand Down
14 changes: 0 additions & 14 deletions helpers/apk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Gets the Alpine Package Keeper (APK) version for the specified version of
# Alpine Linux.
# Globals:
# None
# Arguments:
# Version of Alpine Linux
# Returns:
# Alpine Package Keeper version
get_apk_tools_version() {
local alpine_version=$1

apk_tools_version.py --alpine-version="${alpine_version}"
}

# Runs apk.static to build a chroot environment.
# Globals:
# BASE_PACKAGES
Expand Down
87 changes: 9 additions & 78 deletions helpers/toolset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,21 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Removes
# * the specified files and directories;
# * the .partial file in the current directory.
# Globals:
# None
# Arguments:
# Target directory
# Returns:
# None
finalise_installation() {
for i in "$@"; do
rm -rf "${i}"
done

rm -f .partial
build_toolset() {
build_toolset.py ${PIEMAN_DIR}/.toolset.yml
}

# Gets qemu-user-static 3.1 from Ubuntu 19.04 "Disco Dingo".
# Runs the preprocessor against toolset.yml, located in the root directory of
# Pieman.
# Globals:
# None
# PIEMAN_DIR
# PIEMAN_UTILS_DIR
# PYTHON
# Arguments:
# None
# Returns:
# None
get_qemu_emulation_binary() {
local package

do_wget http://mirrors.kernel.org/ubuntu/dists/focal/universe/binary-amd64/Packages.xz

xz -d Packages.xz

package="$(grep "Filename: pool/universe/q/qemu/qemu-user-static" Packages | awk '{print $2}')"

do_wget http://security.ubuntu.com/ubuntu/"${package}"

ar x "$(basename "${package}")"

tar xJf data.tar.xz

cp usr/bin/qemu-aarch64-static .
cp usr/bin/qemu-arm-static .

# cleanup
rm control.tar.xz
rm data.tar.xz
rm debian-binary
rm Packages
rm "$(basename "${package}")"
rm -r usr
}

# Checks if the specified Toolset component is partially installed, and if so,
# cleans up its directory and initializes it for the installation, creating the
# .partial file there.
# Globals:
# None
# Arguments:
# Target directory
# Returns:
# 0 if the specified component directory was initialized
# 1 if there was no need to initialize the specified component directory
init_installation_if_needed() {
local dir=$1

create_dir "${dir}"
if [ -z "$(ls -A "${dir}")" ] || [ -f "${dir}"/.partial ]; then
rm -rf "${dir:?}"/*

touch "${dir}"/.partial

return 0
fi

return 1
run_preprocessor_against_toolset_yml() {
preprocessor.py ${PIEMAN_DIR}/toolset.yml ${PIEMAN_DIR}/.toolset.yml
}

# Figures out the number of CPU cores which are available on the current
# machine.
# Globals:
# None
# Arguments:
# None
# Returns:
# Number of available cores
number_of_cores() {
grep -c ^processor /proc/cpuinfo
}
5 changes: 4 additions & 1 deletion pieman.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def_bool_var SUDO_REQUIRE_PASSWORD true

def_var TIME_ZONE "Etc/UTC"

def_var TOOLSET_CODENAME "v4-amy"
def_var TOOLSET_CODENAME "v5-farnsworth"

def_var TOOLSET_DIR "${PIEMAN_DIR}/toolset"

Expand Down Expand Up @@ -214,6 +214,9 @@ info "checking toolset ${TOOLSET_CODENAME}"
if [ ! -d "${TOOLSET_FULL_PATH}" ]; then
info "building toolset ${TOOLSET_CODENAME} since it does not exist"
fi

run_preprocessor_against_toolset_yml

. toolset.sh

# shellcheck source=./pieman/pieman/build_status_codes
Expand Down
117 changes: 0 additions & 117 deletions pieman/bin/apk_tools_version.py

This file was deleted.

62 changes: 62 additions & 0 deletions pieman/bin/build_toolset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/python3
# Copyright (C) 2020 Evgeny Golyshev <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.

import os
import sys
from argparse import ArgumentParser

from yaml.scanner import ScannerError

from pieman import toolset, util


def main():
"""The main entry point. """

parser = ArgumentParser()
parser.add_argument('yml_file', help='path to the Toolset YAML file')
args = parser.parse_args()

try:
os.environ['TOOLSET_FULL_PATH']
except KeyError:
util.fatal('The TOOLSET_FULL_PATH environment variable is undefined.')
sys.exit(1)

try:
toolset_tree = toolset.ToolsetProcessor(args.yml_file)
except ScannerError as exp:
util.fatal('{}'.format(exp))
sys.exit(1)
except AttributeError as exp:
util.fatal('{}'.format(exp))
sys.exit(1)
except ModuleNotFoundError as exp:
util.fatal('{}'.format(exp))
sys.exit(1)
except toolset.MissingRequiredFields as exp:
util.fatal('{}'.format(exp))
sys.exit(1)

for name, module in toolset_tree:
for flavour in module['flavours']:
flavour_name = next(iter(flavour))
mod = module['imported']
mod.run(**flavour[flavour_name])


if __name__ == '__main__':
main()
44 changes: 44 additions & 0 deletions pieman/bin/preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python3
# Copyright (C) 2020 Evgeny Golyshev <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.

import sys
from argparse import ArgumentParser

from yaml.scanner import ScannerError

from pieman import toolset


def main():
"""The main entry point. """

parser = ArgumentParser()
parser.add_argument('infile', help='path to the file to be processed')
parser.add_argument('outfile', help='path to the result file')
args = parser.parse_args()

try:
toolset.PreProcessor(args.infile, args.outfile)
except ScannerError as exp:
sys.stderr.write('{}\n'.format(exp))
sys.exit(1)
except toolset.UndefinedVariable as exp:
sys.stderr.write('{}\n'.format(exp))
sys.exit(1)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion pieman/pieman/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""The Pieman tools. """

__version__ = '0.18.0'
__version__ = '0.19.0'
Loading