slitaz-dev-tools view tazdev/tazdev @ rev 273

tazdev: add setup-cgi (only a proposal, adaptable via config file)
author Christophe Lincoln <pankso@slitaz.org>
date Mon Feb 27 22:08:17 2017 +0100 (2017-02-27)
parents 6f82a9600893
children 66c023d0f82f
line source
1 #!/bin/sh
2 #
3 # Tazdev - SliTaz developers and build host tool. System wide config
4 # file is located at: /etc/slitaz/tazdev.conf. Keep the code clear and
5 # well commented please, also: configuration variables are $UPPERCASE
6 # and variables initialized by tazdev itself are $lowerspace
7 #
8 # (c) 2014-2017 SliTaz GNU/Linux - GNU gpl v3
9 #
10 # AUTHORS
11 # Christophe Lincoln <pankso@slitaz.org>
12 # Pascal Bellard <bellard@slitaz.org>
13 # Eric Joseph-Alexandre <erjo@slitaz.org>
14 #
16 VERSION=2.1
18 . /lib/libtaz.sh
20 [ -f /etc/slitaz/tazdev.conf ] && . /etc/slitaz/tazdev.conf
21 [ -f ~/.config/slitaz/tazdev.conf ] && . ~/.config/slitaz/tazdev.conf
22 [ -f tazdev.conf ] && . ./tazdev.conf
24 if [ ! "$SLITAZ_HOME" ]; then
25 echo -e "\nNo config file found\n" && exit 1
26 fi
28 usage() {
29 cat << EOT
31 $(boldify "Usage:") $(basename $0) [command] [vers|user|tool] [vers|--opts]
33 SliTaz developers and build host tool v$VERSION
35 $(boldify "Commands:")
36 usage Print this short usage and command list
37 help Give help on a SliTaz tool or library.
38 -s stats Display statistics about your projects
39 -c chroot Mount virtual fs and chroot into the build env
40 -gc gen-chroot Generate a chroot using packages from config file
41 -uc unchroot Unmount chroot specified on cmdline
42 -cc clean-chroot Clean a chroot environment (skip root/ and home/)
43 -p push Upload new packages to the main mirror
44 -dp dry-push Show what will be uploaded to the mirror.
45 pull Download new packages from the main mirror
46 dry-pull Show what will be downloaded from the mirror.
47 -ur up-repos Update all your SliTaz projects repos in one command.
48 -rp relpkg Archive and upload new package/project version
49 -sc setup-cgi Setup your CGI environment
51 $(boldify "Options:")
52 --rootfs= Path to the chroot to generate or clean
53 --arch= Specify the architecture type for cross-chroot, push/pull
54 --clean Clean chroot before generation a new one
56 $(boldify "Options:")
57 $(basename $0) gen-chroot undigest --clean
58 $(basename $0) -c --arch=arm
60 EOT
61 }
63 get_version() {
64 # Stable, undigest or custom.
65 if [ "$2" ] && [ ! $(echo $2 | grep "\--*") ]; then
66 version=$2
67 slitaz=$SLITAZ_HOME/$2
68 else
69 version=cooking
70 slitaz=$SLITAZ_HOME/cooking
71 fi
72 if [ "$arch" ]; then
73 rootfs=$slitaz/$arch/chroot
74 else
75 rootfs=$slitaz/chroot
76 fi
77 }
79 check_mirror() {
80 # ping -c 1 $MIRROR
81 if [ -n "$2" ]; then
82 user=$2
83 else
84 user=$user
85 fi
86 if [ "$2" = "stable" ] || [ "$3" = "stable" ]; then
87 remote=$MIRROR_PKGS/stable/
88 local=$SLITAZ_HOME/stable/packages/
89 elif [ "$2" = "backports" ] || [ "$3" = "backports" ]; then
90 remote=$MIRROR_PKGS/backports/
91 local=$SLITAZ_HOME/backports/packages/
92 elif [ "$2" = "undigest" ] || [ "$3" = "undigest" ]; then
93 remote=$MIRROR_PKGS/undigest/
94 local=$SLITAZ_HOME/undigest/packages/
95 elif [ "$2" = "rpi" ] || [ "$3" = "rpi" ]; then
96 remote=$MIRROR_PKGS/cross/rpi/
97 local=$SLITAZ_HOME/cooking/arm/packages/
98 else
99 remote=$MIRROR_PKGS/cooking/
100 local=$SLITAZ_HOME/cooking/packages/
101 fi
102 if [ "$arch" ]; then
103 remote=${remote}$arch/
104 local=${local}$arch/
105 fi
106 }
108 # Bind a directory into the chroot
109 bind_chroot_dir()
110 {
111 mkdir -p $1 $2
112 mount -o bind $1 $2
113 }
115 # Mount virtual Kernel file systems and chroot but check that nobody
116 # else has done mounts
117 mount_chroot()
118 {
119 if [ ! -d $rootfs/proc/1 ]; then
120 echo -ne "\nMounting virtual filesystems..."
121 mount -t proc proc $rootfs/proc
122 mount -t sysfs sysfs $rootfs/sys
123 mount -t devpts devpts $rootfs/dev/pts
124 mount -t tmpfs shm $rootfs/dev/shm
125 status
126 fi
127 # Mount source so they can be shared between cooking/stable/undigest.
128 # But do it only if it's a slitaz development chroot.
129 fs=$rootfs/home/slitaz
130 if [ -d "$slitaz" ]; then
131 bind_chroot_dir $SLITAZ_HOME/src $fs/src
132 # Now mount package dir so they are in /home/slitaz/$version
133 # We may not mount cache wok or others it has no point and if
134 # one wants to use a shared wok he can bind it manually.
135 if [ "$arch" ]; then
136 slitaz=$slitaz/$arch
137 fi
138 [ -d "$fs/packages" ] || mkdir -p $fs/packages
139 [ -d "$slitaz/packages" ] || mkdir -p $fs/packages
140 bind_chroot_dir $slitaz/packages $fs/packages
141 fi
142 }
144 # Unmount virtual Kernel file systems.
145 umount_chroot() {
146 [ "$1" ] && rootfs=$1
147 fs=$rootfs/home/slitaz
148 echo -ne "\nUnmounting virtual filesystems..."
149 umount $rootfs/dev/shm
150 umount $rootfs/dev/pts
151 umount $rootfs/sys
152 umount $rootfs/proc
153 if mount | fgrep -q $fs/src; then
154 umount $fs/src
155 umount $fs/packages
156 fi
157 status && echo ""
158 }
160 # Get the last cooking base rootfs, extract and configure.
161 gen_chroot() {
162 echo -e "\nGenerating new chroot in: $rootfs"
163 separator
164 mkdir -p $rootfs
165 # We my gen cooking chroot from a stable version or invers
166 case "$version" in
167 cooking|undigest) url="http://$MIRROR/packages/cooking/" ;;
168 stable*|4.0*|backports) url="http://$MIRROR/packages/stable/" ;;
169 esac
170 # --mirror=
171 [ "$mirror" ] && url="$mirror"
172 mpath=/var/lib/tazpkg/mirror
173 cp $mpath ${mpath}.tazdev
174 echo "$url" > $mpath
175 mkdir -p $(dirname $rootfs$mpath)
176 echo "$url" > $rootfs$mpath
177 echo -n "Mirror URL: ${url#http://}"
178 tazpkg recharge 2>/dev/null 1>/dev/null
179 status
180 for pkg in $CHROOT_PKGS
181 do
182 echo -n "Installing: $pkg $vers"
183 tazpkg -gi $pkg --root=$rootfs 2>/dev/null 1>/dev/null
184 status
185 done
186 echo -n "Creating resolv.conf..."
187 cat /etc/resolv.conf > $rootfs/etc/resolv.conf
188 status
189 echo -n "Creating TZ..."
190 cat /etc/TZ > $rootfs/etc/TZ
191 status
192 echo -n "Restoring host packages list..."
193 mv -f ${mpath}.tazdev $mpath
194 tazpkg recharge 2>/dev/null 1>/dev/null >/dev/null
195 status
196 separator
197 case "$version" in
198 cooking) version="" ;;
199 esac
200 [ "$arch" ] && version="$version --arch=$arch"
201 echo -n "Ready to chroot with:"; colorize 34 " tazdev -c $version"
202 newline
203 }
205 #
206 # Commands
207 #
209 case "$1" in
210 stats|-s)
211 newline
212 echo "Statistics for: $PROJECTS"
213 newline
214 echo "Project $(indent 24 Revision) $(indent 38 Version) $(indent 54 Files)"
215 separator
216 cd ${PROJECTS}
217 for proj in *
218 do
219 rev=""
220 if [ -d "${PROJECTS}/${proj}/.hg" ]; then
221 cd ${PROJECTS}/${proj}
222 rev=$(hg head --template '{rev}\n')
223 vers=$(hg tags | head -n 2 | tail -n 1 | cut -d " " -f 1)
224 files=$(find . -type f | wc -l)
225 echo "$proj $(indent 24 $rev) $(indent 38 $vers) $(indent 54 $files)"
226 fi
227 done
228 separator
229 echo "" ;;
230 chroot|-c)
231 # Chroot into a build env. Default to cooking configured in tazdev.conf
232 check_root
233 get_version $@
234 [ -d "$2" ] && rootfs=$2
235 mount_chroot
236 echo -e "Chrooting to: $rootfs\n"
237 chroot $rootfs /bin/sh --login
238 umount_chroot ;;
239 umchroot|-uc)
240 check_root
241 get_version $@
242 [ -d "$2" ] && rootfs=$2
243 umount_chroot $rootfs ;;
244 gen-chroot|-gc)
245 check_root
246 # We can use: --rootfs=/path/to/chroot
247 if [ ! "$rootfs" ]; then
248 get_version $@
249 fi
250 if [ "$clean" ] || [ "$forced" ]; then
251 $0 -cc --rootfs=$rootfs --noline
252 fi
253 # Dont break another env.
254 if [ -d $rootfs/bin ]; then
255 echo -e "\nA chroot environment already exists in : $rootfs\n"
256 exit 1
257 fi
258 gen_chroot ;;
259 clean-chroot|-cc)
260 check_root
261 # We can use: --rootfs=/path/to/chroot
262 if [ ! "$rootfs" ]; then
263 get_version $@
264 fi
265 if [ ! -d "$rootfs" ]; then
266 echo -e "\nChroot doesn't exist: $rootfs\n"
267 exit 1
268 fi
269 if [ -d $rootfs/proc/1 ]; then
270 echo -e "\nWARNING: $rootfs/proc mounted!\n"
271 exit 1
272 fi
273 cd $rootfs || exit 1
274 echo ""
275 boldify "Cleaning chroot: $rootfs"
276 separator
277 # Keep root/ and /home they may have a build wok, custom scripts, etc.
278 for i in boot bin dev etc lib media mnt proc sbin sys tmp usr var run
279 do
280 if [ -d "$i" ]; then
281 echo -n "Removing: $i ($(du -sh $i | awk '{ print $1 }'))... "
282 rm -rf $i && status
283 fi
284 done
285 rm -f init
286 separator
287 [ "$noline" ] || newline ;;
288 push|-p)
289 check_mirror $@
290 rsync -r -t -O -l -v -z --delete \
291 $local -e ssh $user@$MIRROR:$remote ;;
292 dry-push|-dp)
293 check_mirror $@
294 rsync -r -t -O -l -v -z --delete --dry-run \
295 $local -e ssh $user@$MIRROR:$remote ;;
296 pull)
297 check_mirror $@
298 rsync -r -t -l -v -z --delete \
299 -e ssh $user@$MIRROR:$remote $local ;;
300 dry-pull)
301 check_mirror $@
302 rsync -r -t -l -v -z --delete --dry-run \
303 -e ssh $user@$MIRROR:$remote $local ;;
304 up-repos|-ur)
305 # Update all at once.
306 newline
307 boldify "Update all SliTaz Hg repos"
308 separator
309 for p in $(ls $PROJECTS)
310 do
311 echo -n $(boldify Project:) && colorize 36 " $p"
312 cd ${PROJECTS}/${p}
313 hg pull -u
314 done
315 separator
316 newline ;;
317 relpkg|-rp)
318 # Release a slitaz sub-project and upload tarball to mirror
319 [ -z "$MIRROR_SOURCES" ] && MIRROR_SOURCES="/var/www/slitaz/mirror/sources"
320 if [ -z $2 ]; then
321 echo -e "\nUsage: $0 relpkg package [version]\n"
322 exit 0
323 fi
324 pkg=$2
326 # We can get the last found version in .hgtags
327 if [ -z $3 ]; then
328 version=$(tail -n 1 $PROJECTS/$pkg/.hgtags | awk '{print $2'})
329 else
330 version=$3
331 fi
333 echo "" && cd $PROJECTS/$pkg
335 # Sanity check
336 if ! grep -q $version$ .hgtags; then
337 echo "Missing Hg tag for version: $version"
338 echo -e "You may want to: hg tag $version && hg push\n"
339 exit 0
340 fi
342 # Archive
343 echo -n "Creating tarball and md5sum for: $pkg-$version... "
344 hg archive -t tgz $SOURCE/$pkg-$version.tar.gz
345 ( cd $SOURCE; md5sum $pkg-$version.tar.gz > $pkg-$version.md5 )
346 echo "Done"
348 # Upload
349 echo -n "Do you wish to upload tarball to the mirror [N/y] ? "
350 read upload
351 if [ "$upload" = "y" ]; then
352 cd ${SOURCE}
353 echo "Uploading to: $MIRROR/sources/${pkg#slitaz-}"
354 scp $pkg-$version.tar.gz $pkg-$version.md5 \
355 $MIRROR:$MIRROR_SOURCES/${pkg#slitaz-}
356 fi
358 # Update pkg in wok
359 echo -n "Do you wish to update $pkg in wok [N/y] ? "
360 read update
361 if [ "$update" = "y" ]; then
362 echo -n "Updating $pkg: $version"
363 cd $PROJECTS/wok
364 sed -i s"/VERSION=.*/VERSION=\"$version\"/" $pkg*/receipt
365 status
366 fi ;;
368 setup-cgi|-sc)
369 . /etc/slitaz/slitaz.conf
370 echo "$(boldify 'Public :') $PUBLIC"
371 mkdir -p ${PUBLIC}/cgi-bin
372 cd ${PUBLIC}/cgi-bin
373 for proj in tazbug tinycm mediabox; do
374 if [ ! -d "$proj" ]; then
375 hg clone ttp://hg.slitaz.org/${proj}
376 fi
377 done
378 if [ ! -d "$INSTALLED/lighttpd" ]; then
379 sudo tazpkg -gi lighttpd
380 fi
381 echo
382 echo "TODO: setup SCN (tinycm install + plugins from slitaz-forge"
383 echo
384 echo "$(boldify 'URL :') http://localhost/~${USER}/cgi-bin/" ;;
386 help)
387 if [ ! "$2" ]; then
388 echo "Missing tool/library name"
389 echo "Example: tazdev help httphelper"; exit 0
390 fi
391 newline
392 boldify "Help for: $2"
393 separator
394 if [ -f "/usr/share/doc/slitaz/$2.txt" ]; then
395 less -M /usr/share/doc/slitaz/$2.txt
396 else
397 echo "No help found"
398 fi
399 separator && newline ;;
401 usage|*) usage ;;
403 esac
405 exit 0