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 message_section_title "Update the files affecting package installation of the host environment"
122 message_echo "INFO: targets are packages, distfiles, port options and ports/packages management tools configurations"
123 for dirpath in "${DISTDIR}" "${PACKAGES}" "${PORT_DBDIR}"
127 cd $dirpath && find . -type f
130 [ -e "$opt_basedir/builder$dirpath/$file" ] || rm "$dirpath/$file"
133 cd "$opt_basedir"/builder$dirpath && find . -type f
136 [ ! -e "$dirpath/$file" -o "$opt_basedir/builder$dirpath/$file" -nt "$dirpath/$file" ] || continue
137 nodedir=`dirname "$dirpath/$file"`
139 cp -p "$opt_basedir/builder$dirpath/$file" "$dirpath/$file"
141 find -d $dirpath -type d -empty -delete
143 for filepath in "${LOCALBASE}"/etc/portsreinstall.conf "${LOCALBASE}"/etc/pkgtools.conf "${LOCALBASE}"/etc/pkg.conf /etc/make.conf "${DBDIR}"
145 if [ -e "$opt_basedir"/builder$filepath ]
147 [ ! -e $filepath -o "$opt_basedir"/builder$filepath -nt $filepath ] && cp -p "$opt_basedir"/builder$filepath $filepath
155 # ============= Ending process =============
156 command_do_ending_process ()
158 temp_terminate_process () { :; }
159 if [ $opt_no_opening_message = yes ]
161 message_echo "Done as ${APPNAME}"
164 message_section_title "COMPLETELY DONE"
165 message_echo "- E N D -"
168 # ============= Initial clean up =============
169 command_do_chroot_cleanup ()
171 local PROGRAM_DEPENDS
173 _program_exec_and_record_completion__operation ()
178 program_exec_and_record_completion CLEANUP_GUEST
181 # ============= Common process after exiting from the chroot environment =============
182 command_do_chroot_postprocess ()
184 if ! command_do_chk_build_completion
186 message_echo "INFO: Exited from the builder chroot environment without completing the build." >&2
189 message_echo "INFO: The package build completed."
190 message_echo "Is it OK to apply the upgrade to the host environment? ([y]/n)"
191 if ! message_query_yn_default_yes
193 message_echo "INFO: Terminated because the continuation is stopped by the user."
198 # ============= Starter process in the chroot environment: Update the ports tree =============
199 command_do_starter_portsnap ()
201 local PROGRAM_DEPENDS
202 PROGRAM_DEPENDS='CLEANUP_GUEST'
203 _program_exec_and_record_completion__operation ()
206 [ $opt_fullcourse = no ] && return
207 message_section_title "Full course starter: Update the ports tree"
208 if [ -e "$opt_basedir/builder$PORTSNAP_WORKDIR/INDEX" ]
214 if [ $opt_batch_mode = no ]
220 chroot "$opt_basedir"/builder /usr/bin/env portsnap fetch $mode > $stdout
223 program_exec_and_record_completion STARTER_GUEST_PORTS_TREE
226 # ============= Starter process in the chroot environment: Update the package repository =============
227 command_do_starter_pkg ()
229 local PROGRAM_DEPENDS
230 PROGRAM_DEPENDS='STARTER_GUEST_PORTS_TREE'
231 _program_exec_and_record_completion__operation ()
234 [ $opt_fullcourse = no ] && return
235 message_section_title "Full course starter: Update the package repository"
236 if [ $opt_batch_mode = no ]
242 chroot "$opt_basedir"/builder /usr/bin/env pkg update $opts
245 program_exec_and_record_completion STARTER_GUEST_PKG_REPO
248 # ============= Starter process in the chroot environment: Clean the temporary database =============
249 command_do_starter_clean ()
251 local PROGRAM_DEPENDS
252 PROGRAM_DEPENDS='STARTER_GUEST_PKG_REPO'
253 _program_exec_and_record_completion__operation ()
256 [ $opt_fullcourse = no ] && return
257 message_section_title "Full course starter: Clean the temporary database"
258 if [ $opt_batch_mode = no ]
264 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} $opts clean force
267 program_exec_and_record_completion STARTER_GUEST_CLEAN
270 # ============= Main process in the chroot environment: Initiate the reinstallation process =============
271 command_do_main_init_resinst ()
273 local PROGRAM_DEPENDS
274 PROGRAM_DEPENDS='STARTER_GUEST_CLEAN'
275 _program_exec_and_record_completion__operation ()
278 [ $opt_fullcourse = no ] && return
279 message_section_title "Full course main: Initiate the reinstallation process"
281 [ $opt_batch_mode = yes ] && opts='-Ya'
282 chroot "$opt_basedir"/builder /usr/bin/env ${PROGRAM} -SJCGjqx $opts
285 program_exec_and_record_completion STARTER_GUEST_INIT_REINST
288 # ============= Enter the chroot environment =============
289 command_do_chroot_enter_if_incomplete ()
291 local PROGRAM_DEPENDS
292 command_do_chk_build_completion || program_deregister_stage_complete UPGRADE_GUEST
293 PROGRAM_DEPENDS='STARTER_GUEST_INIT_REINST'
294 _program_exec_and_record_completion__operation ()
297 command_do_chroot_enter "$@"
298 exitstatus=`command_do_get_recent_chroot_exit_status`
299 [ "$exitstatus" -gt 0 ] && exit $exitstatus
300 command_do_chroot_postprocess
303 program_exec_and_record_completion UPGRADE_GUEST "$@"
306 # ============= Pack (re)installed packages at the chroot environment =============
307 command_do_chroot_pack_update ()
309 local PROGRAM_DEPENDS
310 if [ -e "$opt_basedir"/store/complete ]
312 if ! command_do_chk_build_completion
314 program_deregister_stage_complete PACK_PKGS
315 rm -rf "$opt_basedir"/store/complete
318 program_deregister_stage_complete PACK_PKGS
320 PROGRAM_DEPENDS='UPGRADE_GUEST'
321 _program_exec_and_record_completion__operation ()
323 message_section_title "Pack the upgrades"
324 [ $opt_batch_mode = yes ] && opt_quiet='-a '
325 chroot "$opt_basedir"/builder /bin/csh -c "${PROGRAM} -S $opt_quiet packupgrade create && ${PROGRAM} -S $opt_quiet packupgrade crop /.${PROGRAM}"
327 program_exec_and_record_completion PACK_PKGS
330 # ============= Update of the files affecting package installation of the host environment =============
331 command_do_chroot_update_host_files ()
333 local PROGRAM_DEPENDS
334 PROGRAM_DEPENDS='PACK_PKGS'
335 _program_exec_and_record_completion__operation ()
337 command_do_update_host_files
339 program_exec_and_record_completion UPDATE_HOST
342 # ============= Update of packages at the host environment =============
343 command_do_chroot_update_host_pkgs ()
345 local PROGRAM_DEPENDS
346 PROGRAM_DEPENDS='UPDATE_HOST'
347 _program_exec_and_record_completion__operation ()
349 message_section_title "Extract the upgrade archive"
351 cd "$opt_basedir"/store
354 tar xzf portsreinstall-upgrade.tar.gz -C work
357 program_exec_and_record_completion EXTRACT_SCRIPT_ARCHIVE
358 PROGRAM_DEPENDS='EXTRACT_SCRIPT_ARCHIVE'
359 _program_exec_and_record_completion__operation ()
361 message_section_title "Update the packages at the host environment"
363 cd "$opt_basedir"/store
365 [ $opt_batch_mode = yes ] && opt_quiet='-a '
366 work/portsreinstall-upgrade -S clean
367 work/portsreinstall-upgrade -S $opt_quiet -P "${PACKAGES}/${PKGREPOSITORYSUBDIR}"
370 program_exec_and_record_completion UPGRADE_HOST
373 # ============= Main pre-operation of do/auto =============
377 command_do_chroot_cleanup
379 # Build and mount of the chroot environment
384 # ============= Main post-operation of do/auto =============
387 # Pack (re)installed packages at the guest
388 command_do_chroot_pack_update
390 # Update of the files affecting package installation of the host environment
391 command_do_chroot_update_host_files
393 # Update of packages at the host environment
394 command_do_chroot_update_host_pkgs