forked from ungleich-public/cdist
		
	[type/__uci] Apply all commands in a single batch
This commit is contained in:
		
					parent
					
						
							
								ec984f81b5
							
						
					
				
			
			
				commit
				
					
						3e5f18d409
					
				
			
		
					 3 changed files with 51 additions and 0 deletions
				
			
		
							
								
								
									
										43
									
								
								cdist/conf/type/__uci/files/uci_apply.sh
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								cdist/conf/type/__uci/files/uci_apply.sh
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
changes=$(uci changes)
 | 
			
		||||
 | 
			
		||||
if test -n "${changes}"
 | 
			
		||||
then
 | 
			
		||||
	echo 'Uncommited UCI changes were found on the target:'
 | 
			
		||||
	printf '%s\n\n' "${changes}"
 | 
			
		||||
	echo 'This can be caused by manual changes or due to a previous failed run.'
 | 
			
		||||
	echo 'Please investigate the situation, revert or commit the changes, and try again.'
 | 
			
		||||
	exit 1
 | 
			
		||||
fi >&2
 | 
			
		||||
 | 
			
		||||
check_errors() {
 | 
			
		||||
	# reads stdin and forwards non-empty lines to stderr.
 | 
			
		||||
	# returns 0 if stdin is empty, else 1.
 | 
			
		||||
	! grep -e . >&2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
commit() {
 | 
			
		||||
	uci commit
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
rollback() {
 | 
			
		||||
	printf '\nAn error occurred when trying to commit UCI transaction!\n' >&2
 | 
			
		||||
 | 
			
		||||
	uci changes \
 | 
			
		||||
	| sed -e 's/^-//' -e 's/\..*\$//' \
 | 
			
		||||
	| sort -u \
 | 
			
		||||
	| while read -r _package
 | 
			
		||||
	  do
 | 
			
		||||
		  uci revert "${_package}"
 | 
			
		||||
		  echo "${_package}"  # for logging
 | 
			
		||||
	  done \
 | 
			
		||||
	| awk '
 | 
			
		||||
	  BEGIN { printf "Reverted changes in: " }
 | 
			
		||||
	  { printf "%s%s", (FNR > 1 ? ", " : ""), $0 }
 | 
			
		||||
	  END { printf "\n" }' >&2
 | 
			
		||||
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uci_apply() {
 | 
			
		||||
	uci batch 2>&1 | check_errors && commit || rollback
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -91,3 +91,11 @@ in
 | 
			
		|||
		exit 1
 | 
			
		||||
		;;
 | 
			
		||||
esac
 | 
			
		||||
 | 
			
		||||
if test -s "${__object:?}/files/uci_batch.txt"
 | 
			
		||||
then
 | 
			
		||||
	cat "${__type:?}/files/uci_apply.sh"
 | 
			
		||||
	printf "uci_apply <<'EOF'\n"
 | 
			
		||||
	cat "${__object:?}/files/uci_batch.txt"
 | 
			
		||||
	printf '\nEOF\n'
 | 
			
		||||
fi
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										0
									
								
								cdist/conf/type/__uci/nonparallel
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								cdist/conf/type/__uci/nonparallel
									
										
									
									
									
										Normal file
									
								
							
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue