sup rev 29

Online Hub is now usable (can be see on SCN) - many improvments and SQLite functions to create packages.sql
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 27 22:21:15 2017 +0100 (2017-02-27)
parents 3de890dde88b
children cf6506c8f9ad
files server/sup/sup-admin.cgi server/sup/sup.cgi
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/server/sup/sup-admin.cgi	Mon Feb 27 22:21:15 2017 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# TinyCM/TazBug Plugin - Admin part splitted so less code sourced by
     1.7 +# TinyCM core spript. echo ENGLISH Only :-). Admin tools: handle SQLite
     1.8 +# packages.md5 for the mirror, clean any unconform, buggy or corrupted 
     1.9 +# packages in wok.
    1.10 +#
    1.11 +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License
    1.12 +# Author: Christophe Lincoln <pankso@slitaz.org>
    1.13 +#
    1.14 +. /lib/libtaz.sh
    1.15 +
    1.16 +case " $(GET sup) " in
    1.17 +
    1.18 +	*\ admin\ *)
    1.19 +		d="SUP Admin tools"
    1.20 +		header
    1.21 +		html_header
    1.22 +		user_box
    1.23 +		if ! check_auth && admin_user; then
    1.24 +			echo "Only for admins"; html_footer; exit 0
    1.25 +		fi
    1.26 +		cat << EOT
    1.27 +<h2>${d}</h2>
    1.28 +<div id="tools">
    1.29 +	<a href="?sup">SUP Hub</a>
    1.30 +	<a href='?sup=db&amp;command=create'>Create SQL db</a>
    1.31 +	<a href="?sup=db&amp;command=testsuite">SQL Testsuite</a>
    1.32 +</div>
    1.33 +
    1.34 +<h3>SQL Packages db</h3>
    1.35 +<pre>
    1.36 +EOT
    1.37 +		# SQLite commands for ?sup=db&sqlite=command
    1.38 +		if [ -f "$pkgsdb" ]; then
    1.39 +			cat << EOT			
    1.40 +SQLite version   : $(sqlite3 -version | cut -d " " -f 1)
    1.41 +Database tables  : $(sqlite3 ${pkgsdb} '.tables')
    1.42 +Database size    : $(du -mh ${pkgsdb} | cut -d "	" -f 1)
    1.43 +EOT
    1.44 +			sqlite3 ${pkgsdb} 'SELECT * FROM info' \
    1.45 +				| awk '{printf "Timestamp        : %s %s\n",$1,$2 }'
    1.46 +			echo "</pre>"
    1.47 +		else
    1.48 +			echo "WARNING: missing : $pkgsdb"
    1.49 +		fi
    1.50 +		
    1.51 +		# Check/admin wok: remove packagage
    1.52 +		#
    1.53 +		#echo "<h3>Public wok</h3>"
    1.54 +		#echo "<pre>"
    1.55 +		#echo "Checking public wok..."
    1.56 +		#for pkg in $(ls $content/sup/wok); do
    1.57 +			#echo "$pkg"
    1.58 +		#done
    1.59 +		#echo "</pre>"
    1.60 +		
    1.61 +		html_footer && exit 0 ;;
    1.62 +	
    1.63 +	*\ db\ *)
    1.64 +		d="SUP packages.sql"
    1.65 +		command=$(GET command)
    1.66 +		header
    1.67 +		html_header
    1.68 +		user_box
    1.69 +		if ! check_auth && admin_user; then
    1.70 +			echo "Only for admins"; html_footer; exit 0
    1.71 +		fi
    1.72 +		
    1.73 +		# Tools
    1.74 +		cat << EOT
    1.75 +<h2>${d}</h2>
    1.76 +<div id="tools">
    1.77 +	<a href="?sup=admin">Admin tools</a>
    1.78 +	<a href="?sup=db&amp;command=testsuite">SQL Testsuite</a>
    1.79 +</div>
    1.80 +EOT
    1.81 +		
    1.82 +		# Used by command=create
    1.83 +		create_pkgs_table() {
    1.84 +			sqlite3 ${pkgsdb} << EOT
    1.85 +CREATE TABLE pkgs(
    1.86 +	pkg PRIMARY KEY,
    1.87 +	version,
    1.88 +	short_desc,
    1.89 +	maintainer,
    1.90 +	license,
    1.91 +	website,
    1.92 +	sup_deps,
    1.93 +	depends,
    1.94 +	build_date,
    1.95 +	md5sum UNIQUE
    1.96 +);
    1.97 +EOT
    1.98 +		}
    1.99 +		create_info_table() {
   1.100 +			sqlite3 ${pkgsdb} << EOT
   1.101 +CREATE TABLE info(
   1.102 +	timestamp PRIMARY KEY,
   1.103 +	pkgs_nb
   1.104 +);
   1.105 +EOT
   1.106 +		}
   1.107 +		
   1.108 +		# Handle SQlite commands
   1.109 +		[ "$command" ] && echo "<h3>Command: ${command}</h3>"
   1.110 +		case "$command" in
   1.111 +		
   1.112 +			create)
   1.113 +				timestart="$(date -u +%s)"
   1.114 +				rm -f ${pkgsdb}
   1.115 +				echo "<pre>"
   1.116 +				echo -n "Creating pkgs table..."
   1.117 +				create_pkgs_table; status
   1.118 +				echo -n "Creating info table..."
   1.119 +				create_info_table; status
   1.120 +				
   1.121 +				for pkg in $(ls $wok)
   1.122 +				do
   1.123 +					. ${wok}/${pkg}/receip
   1.124 +					# Faster ? | awk '{printf $1}'
   1.125 +					sum=$(md5sum ${packages}/${PACKAGE}-${VERSION}.sup | cut -d " " -f 1)
   1.126 +					echo -n "Inserting: $PACKAGE $VERSION"
   1.127 +					sqlite3 ${pkgsdb} << EOT
   1.128 +INSERT INTO pkgs VALUES(
   1.129 +	"$PACKAGE",
   1.130 +	"$VERSION",
   1.131 +	"$SHORT_DESC",
   1.132 +	"$MAINTAINER",
   1.133 +	"$LICENSE",
   1.134 +	"$WEB_SITE",
   1.135 +	"$SUP_DEPS",
   1.136 +	"$DEPENDS",
   1.137 +	"$build_date",
   1.138 +	"$sum"
   1.139 +);
   1.140 +EOT
   1.141 +					status
   1.142 +				done
   1.143 +				
   1.144 +				timestamp="$(date "+%Y-%m-%d %H:%M")"
   1.145 +				pkgs_nb=$(ls $wok | wc -l)
   1.146 +				sqlite3 ${pkgsdb} << EOT
   1.147 +INSERT INTO info VALUES("$timestamp", "$pkgs_nb");
   1.148 +EOT
   1.149 +				gentime=$(( $(date -u +%s) - ${timestart} ))
   1.150 +				echo "Database generated in: \
   1.151 +<span class='float-right value'>$gentime sec</span>"
   1.152 +				echo "</pre>" ;;
   1.153 +			
   1.154 +			testsuite)
   1.155 +				# SUP SQLite commands testsuite
   1.156 +				echo "<pre>"
   1.157 +				
   1.158 +				echo "Running: <span class='value'>SELECT pkg FROM pkgs LIMIT 4</span>"
   1.159 +				sqlite3 ${pkgsdb} 'SELECT pkg FROM pkgs LIMIT 4' && newline
   1.160 +				
   1.161 +				# .schema
   1.162 +				echo "<span class='value'>CREATE statements</span>"
   1.163 +				echo "-----------------"
   1.164 +				sqlite3 ${pkgsdb} '.schema'; newline
   1.165 +				
   1.166 +				echo "</pre>" ;;
   1.167 +				
   1.168 +			*) echo "<pre>Unknow or command not implemented</pre>" ;;
   1.169 +			
   1.170 +		esac
   1.171 +	
   1.172 +		echo "<pre>"
   1.173 +		if [ -f "$pkgsdb" ]; then
   1.174 +			echo -n "SQLite version : "; sqlite3 -version | cut -d " " -f 1
   1.175 +			echo -n "Database size  : "; du -mh ${pkgsdb} | cut -d "	" -f 1
   1.176 +		else
   1.177 +			echo "<span class='error'>WARNING: missing : $pkgsdb</span>"
   1.178 +		fi
   1.179 +		echo "</pre>"
   1.180 +		
   1.181 +		html_footer && exit 0 ;;
   1.182 +esac
     2.1 --- a/server/sup/sup.cgi	Mon Feb 27 17:44:20 2017 +0000
     2.2 +++ b/server/sup/sup.cgi	Mon Feb 27 22:21:15 2017 +0100
     2.3 @@ -4,8 +4,8 @@
     2.4  #
     2.5  # This plugin is part of the SPI tools and services. More information
     2.6  # at http://hg.slitaz.org/sup/file/ && http://scn.slitaz.org/?d=en/sup
     2.7 -# packages.db is a SQLite table with all mirrored packages info, just
     2.8 -# for the fun of playing with SQLite :-)
     2.9 +# packages.sql is a SQLite table with all mirrored packages info, just
    2.10 +# for the fun of playing witg SQLite :-)
    2.11  #
    2.12  # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
    2.13  # Author: Christophe Lincoln <pankso@slitaz.org>
    2.14 @@ -27,7 +27,8 @@
    2.15  wok="$supdb/wok"
    2.16  packages="$supdb/packages"
    2.17  suplog="$cache/log/sup.log"
    2.18 -		
    2.19 +pkgsdb="$packages/packages.sql"
    2.20 +
    2.21  # Keep the highlighter minimal :-)
    2.22  receip_highlighter() {
    2.23  	sed	-e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
    2.24 @@ -36,7 +37,7 @@
    2.25  }
    2.26  
    2.27  case " $(GET sup) " in
    2.28 -	
    2.29 +
    2.30  	*\ pkg\ *)
    2.31  		pkg="$(GET name)"
    2.32  		d="SUP: $pkg"
    2.33 @@ -46,11 +47,11 @@
    2.34  		if ! . ${wok}/${pkg}/receip; then
    2.35  			echo "Missing or corrupted receip" && exit 1
    2.36  		fi
    2.37 -		
    2.38 +
    2.39  		cat << EOT
    2.40  <h2>$(gettext "Package:") $PACKAGE $VERSION</h2>
    2.41  <div id="tools">
    2.42 -	<a title="Right click to download link: $PACKAGE-$VERSION.sup" 
    2.43 +	<a title="Right click to download link: $PACKAGE-$VERSION.sup"
    2.44  		href='$packages/$PACKAGE-$VERSION.sup'>Download</a>
    2.45  EOT
    2.46  		# Tools for logged users
    2.47 @@ -73,7 +74,7 @@
    2.48  		# Get package maintainer/user info from SCN/SliTaz user DB
    2.49  		if ! . $(fgrep -l "MAIL=\"${MAINTAINER}\"" ${PEOPLE}/*/account.conf); then
    2.50  			USER="unknow"
    2.51 -		fi	
    2.52 +		fi
    2.53  		cat << EOT
    2.54  <div>
    2.55  	<a href="?user=$USER">$(get_gravatar $MAINTAINER 24)</a>
    2.56 @@ -97,8 +98,8 @@
    2.57  </pre>
    2.58  EOT
    2.59  		html_footer && exit 0 ;;
    2.60 -	
    2.61 -	*\ upload\ *) 
    2.62 +
    2.63 +	*\ upload\ *)
    2.64  		# HTML Form: use cloud plugin CSS style
    2.65  		header
    2.66  		html_header
    2.67 @@ -115,7 +116,7 @@
    2.68  
    2.69  <div id="cloud-upload">
    2.70  
    2.71 -	<form method="post" 
    2.72 +	<form method="post"
    2.73  		action="plugins/sup/sup.cgi?sup=supQA&amp;user=$user&amp;mail=$MAIL"
    2.74  		enctype="multipart/form-data">
    2.75  		<input type="file" name="supfile" size="150" />
    2.76 @@ -125,9 +126,9 @@
    2.77  </div>
    2.78  EOT
    2.79  		html_footer && exit 0 ;;
    2.80 -	
    2.81 +
    2.82  	*\ supQA\ *)
    2.83 -		# SUP Upload Quality assurance. Check package in cache before 
    2.84 +		# SUP Upload Quality assurance. Check package in cache before
    2.85  		# publishing. We need full path for upload to work ../../
    2.86  		#
    2.87  		# This is the geeky part for users, QA output in cmdline style
    2.88 @@ -140,32 +141,28 @@
    2.89  		wok="$scn/content/sup/wok"
    2.90  		packages="$scn/content/sup/packages"
    2.91  		cache="$scn/cache/sup/${supfile%.sup}"
    2.92 -		pkgsdb="$scn/content/sup/packages/packages.db"
    2.93 +		pkgsdb="$scn/content/sup/packages/packages.sql"
    2.94  		suplog="$scn/cache/log/sup.log"
    2.95  		error=0
    2.96 -		
    2.97 +
    2.98  		# clean_error "Message"
    2.99  		clean_error() {
   2.100  			echo "<span class='error'>ERROR: ${1}</span>"
   2.101 -			[ -d "$cache" ] && rm -rf ${cache} 
   2.102 +			[ -d "$cache" ] && rm -rf ${cache}
   2.103  			[ -d "$(dirname $tmpfile)" ] && rm -rf $(dirname $tmpfile)
   2.104  		}
   2.105 -		
   2.106 +
   2.107  		# Use COOKIE to make sure user is logged in SCN/SUP Hub
   2.108  		user="$(echo $(COOKIE auth) | cut -d ':' -f 1)"
   2.109  		if [ "$(COOKIE auth)" ] && [ "$user" != "$(GET user)" ]; then
   2.110  			clean_error "user auth cookie"; exit 1
   2.111  		fi
   2.112 -		
   2.113 -		#if [ ! -f "$pkgsdb" ]; then
   2.114 -			#echo "<pre>ERROR: missing $pkgsdb</pre>" && exit 1
   2.115 -		#fi
   2.116 -		
   2.117 +
   2.118  		# Is it a .sup file ?
   2.119  		if [ "$supfile" != "${supfile%.sup}.sup" ]; then
   2.120  			clean_error "not a .sup package: $supfile"; exit 1
   2.121  		fi
   2.122 -		
   2.123 +
   2.124  		cat << EOT
   2.125  <!DOCTYPE html>
   2.126  <html lang="en">
   2.127 @@ -181,17 +178,22 @@
   2.128  SUP Hub user   : <a href="${host}?user=$user">$user</a>
   2.129  Package file   : ${supfile}
   2.130  EOT
   2.131 +		# Server sanity Check
   2.132 +		if [ ! -f "$pkgsdb" ]; then
   2.133 +			clean_error "missing : $(basename $pkgsdb)"
   2.134 +			echo "</pre>" && exit 1
   2.135 +		fi
   2.136  
   2.137  		mkdir -p ${cache}
   2.138  		if ! mv -f ${tmpfile} ${cache}/${supfile}; then
   2.139 -			clean_error "moving: ${tmpfile} to ${supfile}" 
   2.140 +			clean_error "moving: ${tmpfile} to ${supfile}"
   2.141  			echo "</pre>" && exit 1
   2.142  		fi
   2.143 -		
   2.144 +
   2.145  		# Show MD5sum
   2.146  		echo -e "MD5sum         : $(md5sum $cache/$supfile | cut -d ' ' -f 1)\n"
   2.147 -		
   2.148 -		# Extract receip: sup cook already checks for empty var. Make
   2.149 +
   2.150 +		# Extract receip: sup cook already check vor empty var. Make
   2.151  		# sure SCN user name match package MAINTAINER.
   2.152  		gettext "Extracting receip..."
   2.153  		cd ${cache}
   2.154 @@ -199,24 +201,23 @@
   2.155  			echo ""
   2.156  			clean_error "Can't extract receip"
   2.157  			echo "</pre>" && exit 1
   2.158 -		fi
   2.159 -		status
   2.160 -		
   2.161 +		fi; status
   2.162 +
   2.163  		if ! . receip; then
   2.164  			clean_error "Can't source receip"
   2.165  			echo "</pre>" && exit 1
   2.166  		fi
   2.167 -		echo "build_date: $build_date"
   2.168 -		
   2.169 +		echo "Build date: <span class='float-right value'>$build_date</span>"
   2.170 +
   2.171  		# README
   2.172  		gettext "Checking for a README file..."
   2.173  		cpio -i README --quiet < ${supfile}
   2.174  		if [ -f "README" ]; then
   2.175 -			echo " <span class='color32'>$(gettext 'yes')</span>"
   2.176 +			echo " <span class='float-right color32'>$(gettext 'yes')</span>"
   2.177  		else
   2.178 -			echo " <span class='value'>$(gettext 'no')</span>"
   2.179 +			echo " <span class='float-right value'>$(gettext 'no')</span>"
   2.180  		fi
   2.181 -		
   2.182 +
   2.183  		# Logged user is maintainer ?
   2.184  		if [ "$MAINTAINER" != "$(GET mail)" ]; then
   2.185  			error=1
   2.186 @@ -224,18 +225,18 @@
   2.187  			gettext "User mail  :"; echo " $(GET mail)"
   2.188  			gettext "MAINTAINER :"; echo " $MAINTAINER"
   2.189  		fi
   2.190 -		
   2.191 -		# Publish and display pkg urls if no error
   2.192 +
   2.193 +		# Publish and display pkg url's if no error
   2.194  		if [ "$error" == "0" ]; then
   2.195  			gettext "Moving package to mirror..."
   2.196  			mv -f ${supfile} ${packages}; status
   2.197  			gettext "Moving receip to public wok..."
   2.198  			mkdir -p ${wok}/${PACKAGE} && mv -f receip ${wok}/${PACKAGE}; status
   2.199  			[ -f "README" ] && mv -f README ${wok}/${PACKAGE}
   2.200 -			
   2.201 +
   2.202  			# Handle packages.md5
   2.203 -			
   2.204 -			
   2.205 +
   2.206 +
   2.207  			# Log activity date|user|mail|pkg|version|short_desc
   2.208  			cat >> ${suplog} << EOT
   2.209  $(date '+%Y-%m-%d %H:%M')|$user|$MAINTAINER|$PACKAGE|$VERSION|$SHORT_DESC
   2.210 @@ -257,32 +258,11 @@
   2.211  </html>"
   2.212  		rm -rf ${cache} $(dirname $tmpfile) && exit 0 ;;
   2.213  
   2.214 -	*\ admin\ *)
   2.215 -		# Admin tools: gen packages.md5 for the mirror, clean any buggy
   2.216 -		# or corrupted packages in wok.
   2.217 -		d="SUP Hub admin"
   2.218 -		header
   2.219 -		html_header
   2.220 -		user_box
   2.221 -		if ! check_auth && admin_user; then
   2.222 -			echo "Only for admins" && html_footer && exit 0
   2.223 -		fi
   2.224 -		cat << EOT
   2.225 -<h2>${d}</h2>
   2.226 -<div id="tools">
   2.227 -	<a href="?sup=upload">Upload package</a>
   2.228 -</div>
   2.229 -EOT
   2.230 -		echo "<pre>"
   2.231 -		echo "Checking: $content/sup/wok"
   2.232 -		for pkg in $(ls $content/sup/wok); do
   2.233 -			echo "$pkg"
   2.234 -		done
   2.235 -		echo "</pre>"
   2.236 -		html_footer && exit 0 ;;
   2.237 -	
   2.238 +	*\ admin\ *|*\ db\ *)
   2.239 +		. ${plugins}/sup/sup-admin.cgi ;;
   2.240 +
   2.241  	*\ sup\ *)
   2.242 -		d="SliTaz User Packages Hub"
   2.243 +		d="SUP - SliTaz User Packages"
   2.244  		header
   2.245  		html_header
   2.246  		user_box
   2.247 @@ -301,17 +281,16 @@
   2.248  		fi
   2.249  		cat << EOT
   2.250  <p>
   2.251 -	SliTaz User Packages services: Beta :-) -
   2.252 -	<a href="http://scn.slitaz.org/index.cgi?d=en/sup">Documentation</a>
   2.253 +	SliTaz User Packages hub: beta testing :-)
   2.254 +	- Packages: $(ls $wok | wc -l)
   2.255 +	- <a href="$packages/">Browse mirror</a>
   2.256 +	- <a href="http://scn.slitaz.org/index.cgi?d=en/sup">Documentation</a>
   2.257  </p>
   2.258  
   2.259 -<pre>
   2.260 -Packages : $(ls $wok | wc -l)
   2.261 -Mirror   : <a href="$packages/">Browse</a>
   2.262 -</pre>
   2.263  <h3>$(gettext "Latest uploads")</h3>
   2.264  <pre>
   2.265  EOT
   2.266 +		# Latest uploads from sup.log
   2.267  		IFS="|"
   2.268  		tac ${suplog} | head -n 6 | while read date user mail pkg version short_desc
   2.269  		do
   2.270 @@ -324,8 +303,8 @@
   2.271  		done
   2.272  		unset IFS
   2.273  		echo "</pre>"
   2.274 -		
   2.275 -		# Packages list: if one day, too many packages, then this should
   2.276 +
   2.277 +		# Packages listing: if one day, too much packages, then this should
   2.278  		# move to ?sup=list
   2.279  		cat << EOT
   2.280  <h3>$(gettext "SUP packages")</h3>
   2.281 @@ -349,5 +328,5 @@
   2.282  		done
   2.283  		echo "</table></div>"
   2.284  		html_footer && exit 0 ;;
   2.285 -		
   2.286 +
   2.287  esac