2 # ==============================================================================
3 # portsreinstall library script
4 # Overlay onto lib/libcommand_do.sh for portsreinstall-chroot
5 # - "do" command operation -
6 # Copyright (C) 2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
7 # This software is distributed under the 2-Clause BSD License.
8 # ==============================================================================
10 # ============= Check the completion of build at the chroot environment =============
11 command_do_chk_build_completion ()
13 [ -e "$opt_basedir/builder/${DBDIR_PROG}"/stage.complete/ALL_COMPLETE ]
16 # ============= Check whether the termination of portsreinstall at the chroot environment was a unionfs error =============
17 command_do_chk_unionfs_error_at_chroot ()
19 [ -e "$opt_basedir/builder/${DBDIR_PROG}"/execflag/unionfs_error ]
22 # ============= Get the recent chroot exit status of the builder chroot environment =============
23 command_do_get_recent_chroot_exit_status ()
25 cat "${DBDIR}/recent_exit_status"
28 # ============= Enter the shell of the guest =============
29 command_do_chroot_enter_shell ()
31 local shellcmd exitstatus
33 =========================================================
34 You entered the builder chroot environment.
35 Complete the update of packages by portsreinstall(8),
36 Then exit to the target environment.
37 =========================================================
40 shellcmd=`id -P | sed -E 's|.*:([^:]*)$|\1|'`
41 case `basename "$shellcmd"` in
43 shellcmd="env PS1='\\\$@[builder]\h\\$ ' ${shellcmd:-/bin/sh}"
46 shellcmd="env PS1='\u@[builder]\h\\$ ' $shellcmd"
49 chroot "$opt_basedir"/builder "$shellcmd"
51 echo "$exitstatus" > ${DBDIR}/recent_exit_status
55 # ============= Execute in the chroot environment automatically repeating by fixing the file systems =============
56 _command_do_auto_repeat_by_fixing_fs ()
58 until "$@" || ! command_do_chk_unionfs_error_at_chroot
60 message_echo "INFO: Retrying by re-mounting the file systems for the builder chroot environment." >&2
66 # ============= Continue reinstallation in the guest =============
67 command_do_chroot_enter_fullcourse ()
69 local exitstatus opt_quiet
70 message_section_title "Full course main: Reinstallation process"
71 [ $opt_batch_mode = yes ] && opt_quiet='-a '
72 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} -S $opt_quiet
74 echo "$exitstatus" > ${DBDIR}/recent_exit_status
78 # ============= Enter the guest and execute portsreinstall(8) automatically =============
79 command_do_chroot_auto ()
81 local exitstatus opt_quiet
82 [ $opt_batch_mode = yes ] && opt_quiet='-a '
84 =========================================================
85 Automatic execution inside the builder chroot environment:
87 =========================================================
90 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} -S $opt_quiet "$@"
92 echo "$exitstatus" > ${DBDIR}/recent_exit_status
96 # ============= Enter the guest =============
97 command_do_chroot_enter ()
99 if [ $COMMAND_MODE = auto ]
101 [ $opt_fullcourse = yes ] && message_section_title "Full course main"
102 _command_do_auto_repeat_by_fixing_fs command_do_chroot_auto "$@"
103 elif [ $opt_fullcourse = no ]
105 if ! command_do_chroot_enter_shell
107 message_echo "INFO: The last exit status in the builder chroot environment was non-zero." >&2
108 command_do_chk_unionfs_error_at_chroot && \
109 message_echo "INFO: Executing ${APPNAME} again may be sufficient for the troubleshooting." >&2
112 _command_do_auto_repeat_by_fixing_fs command_do_chroot_enter_fullcourse
114 return `command_do_get_recent_chroot_exit_status`
117 # ============= Update of the files affecting package installation of the host environment =============
118 command_do_update_host_files ()
120 local dirpath filepath
121 [ "x$opt_share_port_pkgs_dirs" = xyes ] && return
122 message_section_title "Update the files affecting package installation of the host environment"
123 message_echo "INFO: targets are packages, distfiles, port options and ports/packages management tools configurations"
124 for dirpath in "${DISTDIR}" "${PACKAGES}" "${PORT_DBDIR}"
128 cd $dirpath && find . -type f
131 [ -e "$opt_basedir/builder$dirpath/$file" ] || rm "$dirpath/$file"
134 cd "$opt_basedir"/builder$dirpath && find . -type f
137 [ ! -e "$dirpath/$file" -o "$opt_basedir/builder$dirpath/$file" -nt "$dirpath/$file" ] || continue
138 nodedir=`dirname "$dirpath/$file"`
140 cp -p "$opt_basedir/builder$dirpath/$file" "$dirpath/$file"
142 find -d $dirpath -mindepth 1 -type d -empty -delete
144 for filepath in "${LOCALBASE}"/etc/portsreinstall.conf "${LOCALBASE}"/etc/pkgtools.conf "${LOCALBASE}"/etc/pkg.conf /etc/make.conf "${DBDIR}"
146 if [ -e "$opt_basedir"/builder$filepath ]
148 [ ! -e $filepath -o "$opt_basedir"/builder$filepath -nt $filepath ] && cp -p "$opt_basedir"/builder$filepath $filepath
156 # ============= Ending process =============
157 command_do_ending_process ()
159 temp_terminate_process () { :; }
160 if [ $opt_no_opening_message = yes ]
162 message_echo "Done as ${APPNAME}"
165 message_section_title "COMPLETELY DONE"
166 message_echo "- E N D -"
169 # ============= Initial clean up =============
170 command_do_chroot_cleanup ()
172 local PROGRAM_DEPENDS
174 _program_exec_and_record_completion__operation ()
179 program_exec_and_record_completion CLEANUP_GUEST
182 # ============= Common process after exiting from the chroot environment =============
183 command_do_chroot_postprocess ()
185 if ! command_do_chk_build_completion
187 message_echo "INFO: Exited from the builder chroot environment without completing the build." >&2
190 message_echo "INFO: The package build completed."
191 message_echo "Is it OK to apply the upgrade to the host environment? ([y]/n)"
192 if ! message_query_yn_default_yes
194 message_echo "INFO: Terminated because the continuation is stopped by the user."
199 # ============= Starter process in the chroot environment: Update the ports tree =============
200 command_do_starter_portsnap ()
202 local PROGRAM_DEPENDS
203 PROGRAM_DEPENDS='CLEANUP_GUEST'
204 _program_exec_and_record_completion__operation ()
207 [ $opt_fullcourse = no ] && return
208 message_section_title "Full course starter: Update the ports tree"
209 pkgsys_update_portstree
212 program_exec_and_record_completion STARTER_GUEST_PORTS_TREE
215 # ============= Starter process of the full course: Update the package repository =============
216 command_do_starter_pkg ()
218 local PROGRAM_DEPENDS
219 PROGRAM_DEPENDS='STARTER_GUEST_PORTS_TREE'
220 _program_exec_and_record_completion__operation ()
223 [ $opt_fullcourse = no ] && return
224 message_section_title "Full course starter: Update the package repository"
225 pkg_update_pkgrepository
228 program_exec_and_record_completion STARTER_GUEST_PKG_REPO
231 # ============= Starter process of the full course: Clean the temporary database =============
232 command_do_starter_clean ()
234 local PROGRAM_DEPENDS
235 PROGRAM_DEPENDS='STARTER_GUEST_PKG_REPO'
236 _program_exec_and_record_completion__operation ()
239 [ $opt_fullcourse = no ] && return
240 message_section_title "Full course starter: Clean the temporary database"
241 if [ $opt_batch_mode = no ]
247 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} $opts clean force
250 program_exec_and_record_completion STARTER_GUEST_CLEAN
253 # ============= Main process in the chroot environment: Initiate the reinstallation process =============
254 command_do_main_init_resinst ()
256 local PROGRAM_DEPENDS
257 PROGRAM_DEPENDS='STARTER_GUEST_CLEAN'
258 _program_exec_and_record_completion__operation ()
261 [ $opt_fullcourse = no ] && return
262 message_section_title "Full course main: Initiate the reinstallation process"
264 [ $opt_batch_mode = yes ] && opts='-Ya'
265 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} -SJCGjqx $opts
268 program_exec_and_record_completion STARTER_GUEST_INIT_REINST
271 # ============= Enter the chroot environment =============
272 command_do_chroot_enter_if_incomplete ()
274 local PROGRAM_DEPENDS
275 command_do_chk_build_completion || program_deregister_stage_complete UPGRADE_GUEST
276 PROGRAM_DEPENDS='STARTER_GUEST_INIT_REINST'
277 _program_exec_and_record_completion__operation ()
280 command_do_chroot_enter "$@"
281 exitstatus=`command_do_get_recent_chroot_exit_status`
282 [ "$exitstatus" -gt 0 ] && exit $exitstatus
283 command_do_chroot_postprocess
286 program_exec_and_record_completion UPGRADE_GUEST "$@"
289 # ============= Pack (re)installed packages at the chroot environment =============
290 command_do_chroot_pack_update ()
292 local PROGRAM_DEPENDS
293 if [ -e "$opt_basedir"/store/complete ]
295 if ! command_do_chk_build_completion
297 program_deregister_stage_complete PACK_PKGS
298 rm -rf "$opt_basedir"/store/complete
301 program_deregister_stage_complete PACK_PKGS
303 PROGRAM_DEPENDS='UPGRADE_GUEST'
304 _program_exec_and_record_completion__operation ()
306 message_section_title "Pack the upgrades"
307 [ $opt_batch_mode = yes ] && opt_quiet='-a '
308 chroot "$opt_basedir"/builder /bin/csh -c "${PROGRAM} -S $opt_quiet packupgrade create && ${PROGRAM} -S $opt_quiet packupgrade crop /.${PROGRAM}"
310 program_exec_and_record_completion PACK_PKGS
313 # ============= Update of the files affecting package installation of the host environment =============
314 command_do_chroot_update_host_files ()
316 local PROGRAM_DEPENDS
317 PROGRAM_DEPENDS='PACK_PKGS'
318 _program_exec_and_record_completion__operation ()
320 command_do_update_host_files
322 program_exec_and_record_completion UPDATE_HOST
325 # ============= Update of packages at the host environment =============
326 command_do_chroot_update_host_pkgs ()
328 local PROGRAM_DEPENDS
329 PROGRAM_DEPENDS='UPDATE_HOST'
330 _program_exec_and_record_completion__operation ()
333 message_section_title "Extract the upgrade archive"
334 storedir=$opt_basedir/store
335 rm -rf "$storedir/work"
336 mkdir "$storedir/work"
337 tar xzf "$storedir/portsreinstall-upgrade.tar.gz" -C "$storedir/work"
340 program_exec_and_record_completion EXTRACT_SCRIPT_ARCHIVE
341 PROGRAM_DEPENDS='EXTRACT_SCRIPT_ARCHIVE'
342 _program_exec_and_record_completion__operation ()
345 message_section_title "Update the packages at the host environment"
347 [ $opt_batch_mode = yes ] && opt_quiet='-a '
348 $opt_basedir/store/work/portsreinstall-upgrade -S clean
349 $opt_basedir/store/work/portsreinstall-upgrade -S $opt_quiet -P "${PACKAGES}/${PKGREPOSITORYSUBDIR}"
351 program_exec_and_record_completion UPGRADE_HOST
354 # ============= Main pre-operation of do/auto =============
357 # Build and mount of the chroot environment
362 # ============= Main post-operation of do/auto =============
365 # Pack (re)installed packages at the guest
366 command_do_chroot_pack_update
368 # Update of the files affecting package installation of the host environment
369 command_do_chroot_update_host_files
371 # Update of packages at the host environment
372 command_do_chroot_update_host_pkgs