mirror of
https://github.com/awalsh128/cache-apt-pkgs-action.git
synced 2025-09-20 18:17:15 +00:00
Merge pull request #1 from awalsh128/staging
Fixed shell script and added a restore path for testing.
This commit is contained in:
commit
6a876a63d6
|
@ -22,5 +22,5 @@ runs:
|
||||||
key: cache-apt-pkgs_${{ inputs.cache_key }}
|
key: cache-apt-pkgs_${{ inputs.cache_key }}
|
||||||
|
|
||||||
- name: Install Packages
|
- name: Install Packages
|
||||||
run: ${{ github.action_path }}/run.sh "~/cache-apt-pkgs" "${{ inputs.packages }}"
|
run: ${{ github.action_path }}/run.sh / ~/cache-apt-pkgs ${{ inputs.packages }}
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
139
run.sh
139
run.sh
|
@ -1,84 +1,147 @@
|
||||||
#!/bin/bash -x
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Directory that holds the cached packages.
|
||||||
cache_dir=$1
|
cache_dir=$1
|
||||||
packages="${@:2}"
|
# Root directory to untar the cached packages to.
|
||||||
|
# Typically filesystem root '/' but can be changed for testing.
|
||||||
|
cache_restore_root=$2
|
||||||
|
# List of the packages to use.
|
||||||
|
packages="${@:3}"
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
# Validate command line arguments.
|
||||||
|
# Globals:
|
||||||
|
# cache_dir
|
||||||
|
# Arguments:
|
||||||
|
# None
|
||||||
|
# Outputs:
|
||||||
|
# None
|
||||||
|
# Side effects:
|
||||||
|
# Exits if validation fails.
|
||||||
|
##############################################################################
|
||||||
validate_args() {
|
validate_args() {
|
||||||
echo -n "Validating action arguments... ";
|
echo -n "* Validating action arguments... ";
|
||||||
if [ ! -d "$cache_dir" ]; then
|
if [ ! -d "$cache_dir" ]; then
|
||||||
echo "aborted.\nCache directory '$cache_dir' does not exist."
|
echo "aborted."
|
||||||
return 1
|
echo "Cache directory '$cache_dir' does not exist." >&2
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [ $packages = "" ]; then
|
if [ "$packages" == "" ]; then
|
||||||
echo "aborted.\nPackages argument cannot be empty."
|
echo "aborted."
|
||||||
return 2
|
echo "* Packages argument cannot be empty." >&2
|
||||||
|
exit 2
|
||||||
fi
|
fi
|
||||||
for package in $packages; do
|
for package in $packages; do
|
||||||
if apt-cache search ^$package$ | grep $package; then
|
apt-cache search ^$package$ | grep $package > /dev/null
|
||||||
echo "aborted.\nPackage '$package' not found."
|
if [ $? -ne 0 ]; then
|
||||||
return 3
|
echo "aborted."
|
||||||
|
echo "* Package '$package' not found." >&2
|
||||||
|
exit 3
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "done."
|
echo "done."
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
# Get cached package file path.
|
||||||
|
# Globals:
|
||||||
|
# cache_dir
|
||||||
|
# Arguments:
|
||||||
|
# package name
|
||||||
|
# Outputs:
|
||||||
|
# Writes cached package file path to stdout.
|
||||||
|
##############################################################################
|
||||||
|
get_cache_filepath() {
|
||||||
|
echo "$cache_dir/$1.tar.gz"
|
||||||
|
}
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
# Clean the cache directory of unused packages.
|
||||||
|
# Globals:
|
||||||
|
# cache_dir
|
||||||
|
# Arguments:
|
||||||
|
# None
|
||||||
|
# Outputs:
|
||||||
|
# None
|
||||||
|
# Side effects:
|
||||||
|
# Removes unused cached packages from filesystem.
|
||||||
|
##############################################################################
|
||||||
clean_cache() {
|
clean_cache() {
|
||||||
for dir in `ls $cache_dir`; do
|
for listed_filepath in $(find $cache_dir -maxdepth 1 -type f); do
|
||||||
remove=true
|
remove=true
|
||||||
for package in $packages; do
|
for package in $packages; do
|
||||||
if [ $dir == $package ]; then
|
cache_filepath=$(get_cache_filepath $package)
|
||||||
|
if [ $listed_filepath == $cache_filepath ]; then
|
||||||
remove=false
|
remove=false
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
[ $remove ] && rm -fr $cache_dir/$dir
|
if [ $remove == true ]; then
|
||||||
|
rm -f $listed_filepath
|
||||||
|
echo "* Removed unused cached file $listed_filepath."
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
# Restore cached package.
|
||||||
|
# Globals:
|
||||||
|
# cache_restore_root
|
||||||
|
# Arguments:
|
||||||
|
# package
|
||||||
|
# Outputs:
|
||||||
|
# None
|
||||||
|
# Side effects;
|
||||||
|
# Untars files to filesystem and performs an APT upgrade.
|
||||||
|
##############################################################################
|
||||||
restore_pkg() {
|
restore_pkg() {
|
||||||
package=$1
|
package=$1
|
||||||
package_dir=$2
|
cache_filepath=$(get_cache_filepath $package)
|
||||||
|
|
||||||
echo -n "Restoring $package from cache $package_dir... "
|
echo "* Restoring $package from cache $cache_filepath... "
|
||||||
sudo cp --verbose --force --recursive $package_dir/* /
|
tar -xf $cache_filepath -C $cache_restore_root
|
||||||
sudo apt-get --yes --only-upgrade install $package
|
sudo apt-get --yes --only-upgrade install $package
|
||||||
echo "done."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
# Install package and cache it to the filesystem.
|
||||||
|
# Globals:
|
||||||
|
# None
|
||||||
|
# Arguments:
|
||||||
|
# package
|
||||||
|
# Outputs:
|
||||||
|
# None
|
||||||
|
# Side effects;
|
||||||
|
# Performs an APT install of the package and creates the cached package.
|
||||||
|
##############################################################################
|
||||||
install_and_cache_pkg() {
|
install_and_cache_pkg() {
|
||||||
package=$1
|
package=$1
|
||||||
package_dir=$2
|
cache_filepath=$(get_cache_filepath $package)
|
||||||
|
|
||||||
echo -n "Clean installing $package... "
|
echo "* Clean installing $package... "
|
||||||
sudo apt-get --yes install $package
|
sudo apt-get --yes install $package
|
||||||
echo "done."
|
|
||||||
|
|
||||||
echo -n "Caching $package to $package_dir..."
|
echo "* Caching $package to $cache_filepath..."
|
||||||
mkdir --parents $package_dir
|
# Pipe all package files (no folders) to Tar.
|
||||||
# Pipe all package files (no folders) to copy command.
|
dpkg -L $package |
|
||||||
sudo dpkg -L $package |
|
|
||||||
while IFS= read -r f; do
|
while IFS= read -r f; do
|
||||||
if test -f $f; then echo $f; fi;
|
if test -f $f; then echo $f; fi;
|
||||||
done |
|
done |
|
||||||
xargs cp -p -t $package_dir
|
xargs tar -czf $cache_filepath -C /
|
||||||
echo "done."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validate_code = validate_args
|
validate_args
|
||||||
if validate_code -ne 0; then
|
|
||||||
exit $validate_code
|
|
||||||
fi
|
|
||||||
|
|
||||||
clean_cache
|
clean_cache
|
||||||
|
|
||||||
for package in $packages; do
|
for package in $packages; do
|
||||||
package_dir=$cache_dir/$package
|
echo "* Processing package $package..."
|
||||||
if [ -d $package_dir ]; then
|
cache_filepath=$(get_cache_filepath $package)
|
||||||
restore_pkg $package $package_dir
|
echo $cache_filepath
|
||||||
|
if [ -f $cache_filepath ]; then
|
||||||
|
restore_pkg $package
|
||||||
else
|
else
|
||||||
install_and_cache_pkg $package $package_dir
|
install_and_cache_pkg $package
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Action complete. ${#packages[@]} packages installed."
|
echo "Action complete. ${#packages[@]} package(s) installed."
|
||||||
|
|
Loading…
Reference in a new issue