Add package description + bugfixes

This commit is contained in:
zawz 2020-05-22 01:57:21 +02:00
parent 0bb7711a8f
commit 59485e7970
13 changed files with 222 additions and 195 deletions

View file

@ -2,4 +2,4 @@ SSH_ADDR=example.com
SSH_USER=zpkg SSH_USER=zpkg
HTTP_ADDR=example.com HTTP_ADDR=example.com
HTTP_PATH=zpkg HTTP_PATH=zpkg
PKG_PATH='$HOME/pkg' PKG_PATH=pkg

View file

@ -23,7 +23,7 @@ Optional:
wget http://zpkg.zawz.net/install.sh wget http://zpkg.zawz.net/install.sh
sh install.sh sh install.sh
``` ```
> By default the config is installed to /etc/zpkg. > By default the config is installed to /etc/zpkg
> This can be changed with the -c option > This can be changed with the -c option
If you wish to use another repository, substitute `zpkg.zawz.net` for your desired target If you wish to use another repository, substitute `zpkg.zawz.net` for your desired target
@ -37,7 +37,7 @@ sudo rm -rd /etc/zpkg
### Using ### Using
See `zpkg -h` See `zpkg -h` for details
## Deploy on a server ## Deploy on a server
@ -49,7 +49,7 @@ To deploy on a server you need:
- HTTP server - HTTP server
- dedicated zpkg user - dedicated zpkg user
You need to be able to SSH to the zpkg user. SSH keys are recommended You need to be able to SSH to the zpkg user, SSH keys are recommended
### Process ### Process
@ -64,20 +64,22 @@ You need to be able to SSH to the zpkg user. SSH keys are recommended
``` ```
. .
+-- DEPS +-- DEPS
+-- DESC
+-- ROOT +-- ROOT
| +-- / | +-- /
+-- HOME +-- HOME
+-- ~ +-- ~
``` ```
- The ROOT directory repsesents the root filesystem - The ROOT directory represents the root filesystem
- The HOME directory represents the home directory of the user - The HOME directory represents the home directory of the user
- The DEPS file contains dependency packages separated by spaces or newlines. Dependencies are package names from the repository - The DEPS file contains dependency packages separated by spaces or newlines. Dependencies are package names from the repository
- The DESC file contains the description of the package
### Deploying packages ### Deploying packages
`zpkg deploy <dir...>` `zpkg deploy <dir...>`
> Target directories are structured as described above > Target directories are structured as described above
> The package name is the name of the directory > The name of the directory is the package name
## Functionality ## Functionality
@ -90,4 +92,4 @@ You need to be able to SSH to the zpkg user. SSH keys are recommended
- Versions - Versions
- Multi-repo - Multi-repo
- Hooks

View file

@ -3,7 +3,7 @@
# config # config
# no spaces in srcdir or ordered files # no spaces in srcdir or ordered files
SRCDIR=src SRCDIR=src
ordered='options.sh config.sh main.sh' ordered='env.sh options.sh config.sh main.sh'
# process # process
COMMENTSCRIPT="/^\s*#/d;s/\s*#[^\"']*$//" COMMENTSCRIPT="/^\s*#/d;s/\s*#[^\"']*$//"

View file

@ -7,7 +7,7 @@ _zpkg_completion()
_cw1_file="deploy" _cw1_file="deploy"
if [ "$COMP_CWORD" = "1" ] ; then if [ "$COMP_CWORD" = "1" ] ; then
_compwords=$_cw1 _compwords=$_cw1
elif [ "$COMP_CWORD" -gt "1" ] && [ -n "$(echo "$_cw1_pkgw" | grep -w "${COMP_WORDS[1]}" 2>/dev/null)" ] ; then elif [ "$COMP_CWORD" -gt "1" ] && echo "$_cw1_pkgw" | grep -qw -- "${COMP_WORDS[1]}" ; then
_compwords=$(zpkg list-all 2>/dev/null) _compwords=$(zpkg list-all 2>/dev/null)
fi fi
COMPREPLY=($(compgen -W "$_compwords" "${COMP_WORDS[$COMP_CWORD]}" 2>/dev/null)) COMPREPLY=($(compgen -W "$_compwords" "${COMP_WORDS[$COMP_CWORD]}" 2>/dev/null))

View file

@ -21,7 +21,7 @@ then
fi fi
if [ ! -f "$config_file" ] if [ ! -f "$config_file" ]
then then
echo "Error: no config file '$config_file'" > /dev/stderr echo "Error: no config file '$config_file'" >&2
exit 1 exit 1
fi fi

View file

@ -6,28 +6,27 @@ package()
src="$1" src="$1"
pkg="$2" pkg="$2"
echo "Packaging $(getname "$src"): $(du -sh "$src" | awk '{print $1}')iB" echo "Packaging $(getname "$src"): $(du -sh "$src" | awk '{print $1}')iB"
tmpdir="/tmp/zpkg_$(random_string 5)"
mkdir -p "$tmpdir"
if [ ! -d "$src/ROOT" ] && [ ! -d "$src/HOME" ] && [ ! -f "$src/DEPS" ] if [ ! -d "$src/ROOT" ] && [ ! -d "$src/HOME" ] && [ ! -f "$src/DEPS" ]
then then
tmpdir="/tmp/zpkg$(random_string 5)" mkdir -p "$tmpdir/package"
mkdir -p "$tmpdir" cp -r "$src" "$tmpdir/package/ROOT"
cp -r "$src" "$tmpdir/ROOT" else
src="$tmpdir" cp -r "$src" "$tmpdir/package"
clean_needed=true
fi fi
( (
cd "$src" cd "$tmpdir/package"
if which pv >/dev/null 2>&1 if which pv >/dev/null 2>&1
then then
tar -cf - --owner=0 --group=0 -P * | pv -s "$(du -sb . | awk '{print $1}')" | xz > "$pkg" tar -cf - --owner=0 --group=0 -P * | pv -s "$(du -sb . | awk '{print $1}')" | xz > "../$pkg"
else else
tar -cvJf - --owner=0 --group=0 * > "$pkg" tar -cvJf - --owner=0 --group=0 * > "../$pkg"
fi fi
) )
mv "$src/$pkg" ./ mv "$tmpdir/$pkg" ./
if [ -n "$clean_needed" ] rm -rd "$tmpdir"
then
rm -rd "$tmpdir"
fi
} }
deploy_package() deploy_package()

View file

@ -1,9 +1,11 @@
#!/bin/sh #!/bin/sh
# $1 = package name , $2 = output
fetch_package() fetch_package()
{ {
wget "$HTTP_ADDRESS/$1.tar.xz" -q --show-progress -O "$1.tar.xz" 2>&1 out="$2"
return $? [ -z "$out" ] && out="$1.tar.xz"
wget "$HTTP_ADDRESS/$1.tar.xz" -q --show-progress -O "$out" 2>&1
} }
# $1 = prefix # $1 = prefix
@ -13,7 +15,7 @@ fetch_pkglist()
$1 mv pkglist pkglist_bak 2>/dev/null $1 mv pkglist pkglist_bak 2>/dev/null
if ! $1 wget "$HTTP_ADDRESS/pkglist" -q --show-progress -O pkglist 2>&1 if ! $1 wget "$HTTP_ADDRESS/pkglist" -q --show-progress -O pkglist 2>&1
then then
echo "Couldn't fetch server data" > /dev/stderr echo "Couldn't fetch server data" >&2
$1 mv pkglist_bak pkglist 2>/dev/null $1 mv pkglist_bak pkglist 2>/dev/null
return 1 return 1
else else

View file

@ -39,20 +39,22 @@ move_files()
install_package() install_package()
{ {
tmpdir="/tmp/zpkg$(random_string 5)" tmpdir="/tmp/zpkg_$(random_string 5)"
mkdir -p "$tmpdir" mkdir -p "$tmpdir"
( (
cd "$tmpdir" cd "$tmpdir"
if ! fetch_package "$1" if ! fetch_package "$1"
then then
echo "Package '$1' not found" > /dev/stderr echo "Package '$1' not found" >&2
return 1 return 1
fi fi
unpack "$1.tar.xz" sudo cp "$1.tar.xz" "$PKG_PATH"
sudo mv "$1.tar.xz" "$PKG_PATH" unpack "$1.tar.xz" || return $?
move_files ROOT / sudo 2>/dev/null move_files ROOT / sudo 2>/dev/null
move_files HOME "$HOME" 2>/dev/null move_files HOME "$HOME" 2>/dev/null
add_package_entry "$1" add_package_entry "$1"
) || return $? )
ret=$?
rm -rd "$tmpdir" 2>/dev/null rm -rd "$tmpdir" 2>/dev/null
return $ret
} }

View file

@ -5,160 +5,135 @@ if [ -z "$opt_f" ] ; then
fi fi
if [ -n "$1" ] case "$1" in
then list) awk '{print $1}' "$PKG_PATH/installed" 2>/dev/null ;;
list-all) awk '{print $1}' "$PKG_PATH/pkglist" 2>/dev/null ;;
if [ "$1" = "list" ] update-database) fetch_pkglist sudo ;;
fetch)
if [ -z "$2" ]
then then
awk '{print $1}' "$PKG_PATH/installed" 2>/dev/null echo "No package specified" >&2
else
elif [ "$1" = "list-all" ]
then
awk '{print $1}' "$PKG_PATH/pkglist" 2>/dev/null
elif [ "$1" = "fetch" ]
then
if [ -z "$2" ]
then
echo "No package specified" > /dev/stderr
else
shift 1
for I in $*
do
fetch_package "$I"
done
fi
elif [ "$1" = "show" ]
then
if [ -z "$2" ]
then
echo "No package specified" > /dev/stderr
else
shift 1
for I in $*
do
if is_installed "$I"
then
view_package "$I"
else
echo "No package '$I' installed" > /dev/stderr
fi
done
fi
elif [ "$1" = "deps" ]
then
if [ -z "$2" ]
then
echo "No package specified" > /dev/stderr
else
shift 1
resolve_deps $*
fi
elif [ "$1" = "install" ]
then
fetch_pkglist sudo || exit $?
if [ -z "$2" ]
then
echo "No package specified" > /dev/stderr
else
shift 1
pkglist=$(LOG=true resolve_packages $*) || exit $?
pkglist=$(INCLUDE_PACKAGES=true resolve_deps $* | tr '\n' ' ')
echo "Installing packages: $pkglist"
for I in $pkglist
do
if is_installed "$I"
then
remove_package "$I"
fi
install_package "$I"
done
fi
elif [ "$1" = "remove" ]
then
if [ -z "$2" ]
then
echo "No package specified" > /dev/stderr
else
shift 1
for I in $*
do
remove_package "$I"
done
fi
elif [ "$1" = "update-database" ]
then
fetch_pkglist sudo
elif [ "$1" = "update" ]
then
fetch_pkglist sudo || exit 1
r_pkg=$(removed_packages)
o_pkg=$(outdated_packages)
if [ -n "$r_pkg" ]
then
echo "Removing packages: "$r_pkg
for I in $r_pkg
do
remove_package $I
done
fi
if [ -n "$o_pkg" ]
then
echo "Updating packages: "$o_pkg
for I in $o_pkg
do
remove_package $I
install_package $I
done
fi
elif [ "$1" = "list-outdated" ]
then
tmpdir="/tmp/zpkg$(random_string 5)"
virtual_config_path "$tmpdir" || exit $?
fetch_pkglist sudo > /dev/null || exit $?
outdated_packages
sudo rm -rd "$tmpdir"
elif [ "$1" = "list-removed" ]
then
tmpdir="/tmp/zpkg$(random_string 5)"
virtual_config_path "$tmpdir" || exit $?
fetch_pkglist sudo > /dev/null || exit $?
removed_packages
sudo rm -rd "$tmpdir"
elif [ "$1" = "deploy" ]
then
shift 1 shift 1
for I in $* for I in $*
do do
deploy_folder "$I" || exit 1 fetch_package "$I"
done done
update_remote_database
else
usage
fi fi
;;
else show)
usage if [ -z "$2" ]
fi then
echo "No package specified" >&2
else
shift 1
for I in $*
do
if is_installed "$I"
then
view_package "$I"
else
wget "$HTTP_ADDRESS/$1.tar.xz" -q -O - 2>/dev/null | view_package_file - || { echo "Could not fetch package '$I'" >&2 ; return 1 ; }
fi
done
fi
;;
deps)
if [ -z "$2" ]
then
echo "No package specified" >&2
else
shift 1
resolve_deps $* || exit $?
fi
;;
show)
if [ -z "$2" ]
then
echo "No package specified" >&2
else
shift 1
for N
do
package_info $N || exit $?
done
fi
;;
install)
fetch_pkglist sudo || exit $?
if [ -z "$2" ]
then
echo "No package specified" >&2
else
shift 1
pkglist=$(LOG=true resolve_packages $*) || exit $?
pkglist=$(INCLUDE_PACKAGES=true resolve_deps $* | tr '\n' ' ')
echo "Installing packages: $pkglist"
for I in $pkglist
do
if is_installed "$I"
then
remove_package "$I"
fi
install_package "$I"
done
fi
;;
remove)
if [ -z "$2" ]
then
echo "No package specified" >&2
else
shift 1
for I in $*
do
remove_package "$I"
done
fi
;;
update)
fetch_pkglist sudo || exit 1
r_pkg=$(removed_packages)
o_pkg=$(outdated_packages)
if [ -n "$r_pkg" ]
then
echo "Removing packages: "$r_pkg
for I in $r_pkg
do
remove_package $I
done
fi
if [ -n "$o_pkg" ]
then
echo "Updating packages: "$o_pkg
for I in $o_pkg
do
remove_package $I
install_package $I
done
fi
;;
list-outdated)
tmpdir="/tmp/zpkg_$(random_string 5)"
virtual_config_path "$tmpdir" || exit $?
fetch_pkglist > /dev/null || exit $?
outdated_packages
rm -rd "$tmpdir"
;;
list-removed)
tmpdir="/tmp/zpkg_$(random_string 5)"
virtual_config_path "$tmpdir" || exit $?
fetch_pkglist > /dev/null || exit $?
removed_packages
rm -rd "$tmpdir"
;;
deploy)
shift 1
for I in $*
do
deploy_folder "$I" || exit 1
done
update_remote_database
;;
*) usage && exit 1 ;;
esac

View file

@ -2,8 +2,8 @@
usage() usage()
{ {
echo "$fname [options] <operation>" echo "$fname [options] <operation>
echo '
Operations: Operations:
update Update packages update Update packages
update-database Update only database update-database Update only database
@ -11,6 +11,7 @@ Operations:
remove <pkg...> Remove packages remove <pkg...> Remove packages
fetch <pkg...> Fetch packages into current directory fetch <pkg...> Fetch packages into current directory
show <pkg...> Show package files show <pkg...> Show package files
deps <pkg...> Show dependencies of package
list List currently installed packages list List currently installed packages
list-all List all packages in repository list-all List all packages in repository
list-outdated List outdated packages list-outdated List outdated packages
@ -21,10 +22,43 @@ Admin operations:
Options: Options:
-h Display this help -h Display this help
-c <path> Custom config path -c <path> Custom config path
-f Force running even when root -f Force running when root
' "
} }
error() { error() {
printf "\033[1;31m%s\033[0m\n" "$1" >&2 printf "\033[1;31m%s\033[0m\n" "$1" >&2
} }
# $1 = package name
package_info() {
unset cleanup
status="not installed"
grep -wq "^$1" "$PKG_PATH/pkglist" 2>/dev/null || { echo "Package '$I' not found" && return 1; }
grep -wq "^$1" "$PKG_PATH/installed" 2>/dev/null && status=installed
if [ "$status" = "installed" ] && [ -f "$PKG_PATH/$1.tar.xz" ]
then
pkg="$PKG_PATH/$1.tar.xz"
else
tmpdir="/tmp/zpkg_$(random_string 5)"
pwd=$(pwd)
mkdir "$tmpdir"
fetch_package "$1" >/dev/null 2>&1 || { echo "Error fetching package" >&2 && ret=$?; }
pkg="$1.tar.xz"
fi
deps=$(deps "$1")
desc=$(desc "$pkg" 2>/dev/null)
csize=$(stat -c '%s' "$pkg" | numfmt --to=iec-i --suffix=B --padding 6)
isize=$(xz -dc "$pkg" | wc -c | numfmt --to=iec-i --suffix=B --padding 6)
[ -n "$cleanup" ] && { cd "$pwd"; rm -rd "$tmpdir"; }
[ -n "$ret" ] && return $ret
printf "Name: %s\n" "$1"
printf "Description: %s\n" "$desc"
echo ""
printf "Status: %s\n" "$status"
printf "Dependencies: %s\n" "$(deps "$1" | tr -s ' \n' ' ')"
printf "Package size: %s\n" "$csize"
printf "Installed size: %s\n" "$isize"
}

View file

@ -19,7 +19,7 @@ remove_package()
archive="$(pwd)/$1.tar.xz" archive="$(pwd)/$1.tar.xz"
if [ ! -f "$archive" ] || ! grep -q -w "^$1" installed if [ ! -f "$archive" ] || ! grep -q -w "^$1" installed
then then
echo "Package '$1' not installed" > /dev/stderr echo "Package '$1' not installed" >&2
return 1 return 1
fi fi
echo "Removing $1" echo "Removing $1"

View file

@ -28,7 +28,7 @@ root_check()
{ {
if [ "$(id | cut -d'=' -f2 | cut -d'(' -f1)" -eq 0 ] if [ "$(id | cut -d'=' -f2 | cut -d'(' -f1)" -eq 0 ]
then then
echo "Cannot run as root" > /dev/stderr echo "Cannot run as root" >&2
return 1 return 1
fi fi
return 0 return 0

View file

@ -3,7 +3,13 @@
deps() deps()
{ {
cd "$PKG_PATH" cd "$PKG_PATH"
grep -w "^$1" pkglist | cut -d' ' -f3- l=$(grep -w "^$1" pkglist) || return $?
echo "$l" | cut -d' ' -f3-
}
# $1 = pkg file
desc() {
tar -xOf "$1" DESC
} }
resolve_packages() resolve_packages()
@ -14,7 +20,7 @@ resolve_packages()
do do
if ! grep -wq "^$I" pkglist 2>/dev/null if ! grep -wq "^$I" pkglist 2>/dev/null
then then
[ "$LOG" = "true" ] && echo "Package '$I' not found" > /dev/stderr [ "$LOG" = "true" ] && echo "Package '$I' not found" >&2
RET=1 RET=1
else else
echo "$I" echo "$I"
@ -27,12 +33,14 @@ resolve_packages()
resolve_deps() resolve_deps()
{ {
ALLDEPS="" ALLDEPS=""
RET=0
for I in $* for I in $*
do do
ALLDEPS="$ALLDEPS $(deps $I)" ALLDEPS="$ALLDEPS $(deps $I)" || { echo "Package '$I' not found" >&2 ; RET=$((RET+1)) ; }
done done
[ "$INCLUDE_PACKAGES" = "true" ] && ALLDEPS="$ALLDEPS $*" [ "$INCLUDE_PACKAGES" = "true" ] && ALLDEPS="$ALLDEPS $*"
echo "$ALLDEPS" | tr -s ' \n' '\n' | sort | uniq | sed '/^$/d' echo "$ALLDEPS" | tr -s ' \n' '\n' | sort | uniq | sed '/^$/d'
return $RET
} }
is_installed() is_installed()
@ -42,10 +50,15 @@ is_installed()
return $? return $?
} }
view_package() # $1 = file
{ view_package_file() {
cd "$PKG_PATH" tree=$(tar -tJf "$1" 2>/dev/null) || exit $?
tar -tf "$1.tar.xz" | sed "s|^ROOT/|/|g ; /\/$/d ; s|^HOME/|$HOME/|g ; /^DEPS/d" echo "$tree" | sed "s|^ROOT/|/|g ; /\/$/d ; s|^HOME/|$HOME/|g ; /^DEPS/d" 2>/dev/null
}
# $1 = package name
view_package() {
cd "$PKG_PATH" && view_package_file "$1.tar.xz"
} }
removed_packages() removed_packages()