From 919a8ceabacd9fedaaa5f067fe4a676820f87c9c Mon Sep 17 00:00:00 2001
From: Hideki Yamane <henrich@debian.org>
Date: Tue, 20 Feb 2018 11:10:55 +0900
Subject: [PATCH] support docker environment

For CI environment on salsa.debian.org, it uses docker for GitLab Runner.
I've tried to run piuparts but it couldn't since debootstrap fails to
mount /proc. Let's try to add docker support for debootstrap, first
(similar to fakechroot support).

Specifying "--work-on=docker", it works.

Maybe we can add other environment such as proot in the future.
---
 debootstrap | 29 ++++++++++++++++++++++++++++-
 functions   | 19 ++++++++++++++++++-
 scripts/sid |  5 ++++-
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/debootstrap b/debootstrap
index fcdb20f..8341313 100755
--- a/debootstrap
+++ b/debootstrap
@@ -27,6 +27,7 @@ KEYRING=""
 DISABLE_KEYRING=""
 FORCE_KEYRING=""
 VARIANT=""
+WORK_ENVIRONMENT=""
 MERGED_USR="no"
 ARCH=""
 HOST_ARCH=""
@@ -101,6 +102,8 @@ usage()
       --variant=X            use variant X of the bootstrap scripts
                              (currently supported variants: buildd, fakechroot,
                               minbase)
+      --work-on=X            consider X as host environment
+                             (currently supported environment: docker)
       --merged-usr           make /{bin,sbin,lib}/ symlinks to /usr/
       --keyring=K            check Release files against keyring K
       --no-check-gpg         avoid checking Release file signatures
@@ -304,6 +307,17 @@ if [ $# != 0 ] ; then
 			error 1 NEEDARG "option requires an argument %s" "$1"
 		fi
 		;;
+	    --work-on|--work-on=?*)
+		if [ "$1" = "--work-on" -a -n "$2" ]; then
+			WORK_ENVIRONMENT="$2"
+			shift 2
+		elif [ "$1" != "${1#--work-on=}" ]; then
+			WORK_ENVIRONMENT="${1#--work-on=}"
+			shift 1
+		else
+			error 1 NEEDARG "option requires an argument %s" "$1"
+		fi
+		;;
             --merged-usr)
 		MERGED_USR=yes
 		shift
@@ -385,6 +399,10 @@ if [ "$SECOND_STAGE_ONLY" = "true" ]; then
 		VARIANT=$(cat $DEBOOTSTRAP_DIR/variant)
 		SUPPORTED_VARIANTS="$VARIANT"
 	fi
+	if [ -e $DEBOOTSTRAP_DIR/work-on ]; then
+		WORK_ON=$(cat $DEBOOTSTRAP_DIR/work-on)
+		SUPPORTED_ENVIRONMENT="$WORK_ON"
+	fi
 	if [ -z "$CHROOTDIR" ]; then
 		TARGET=/
 	else
@@ -453,7 +471,6 @@ fi
 
 if [ -z "$ARCH" ] || [ -z "$HOST_OS" ]; then
         error 1 WHATARCH "Couldn't work out current architecture"
-
 fi
 
 if [ "$HOST_OS" = "kfreebsd" ] || [ "$HOST_OS" = "freebsd" ]; then
@@ -563,6 +580,14 @@ if ! $ok; then
 	error 1 UNSUPPVARIANT "unsupported variant"
 fi
 
+ok=false
+for v in $SUPPORTED_ENVIRONMENT; do
+	if work_on $v; then ok=true; fi
+done
+if ! $ok; then
+	error 1 UNSUPPENV "unsupported work-on environment"
+fi
+
 ###########################################################################
 
 if am_doing_phase finddebs; then
@@ -638,6 +663,8 @@ if am_doing_phase first_stage; then
 		echo "$SUITE"			>"$TARGET/debootstrap/suite"
 		[ "" = "$VARIANT" ] ||
 		echo "$VARIANT"			>"$TARGET/debootstrap/variant"
+		[ "" = "$WORK_ON" ] ||
+		echo "$WORK_ON"			>"$TARGET/debootstrap/work-on"
 		echo "$required"		>"$TARGET/debootstrap/required"
 		echo "$base"			>"$TARGET/debootstrap/base"
 
diff --git a/functions b/functions
index 3cfa0d4..863e8cc 100644
--- a/functions
+++ b/functions
@@ -230,6 +230,23 @@ variants () {
 	error 1 UNSUPPVARIANT "unsupported variant"
 }
 
+########################################################## working environment handling
+
+work_on () {
+	if [ "$1" = "$WORK_ENVIRONMENT" ]; then return 0; fi
+	if [ "$1" = "-" ] && [ "$WORK_ENVIRONMENT" = "" ]; then return 0; fi
+	return 1
+}
+
+SUPPORTED_ENVIRONMENT="-"
+host_environment () {
+	SUPPORTED_ENVIRONMENT="$*"
+	for v in $*; do
+		if work_on "$v"; then return 0; fi
+	done
+	error 1 UNSUPPENV "unsupported environment"
+}
+
 ################################################# work out names for things
 
 mirror_style () {
@@ -1119,7 +1136,7 @@ setup_proc () {
 	umount_on_exit /lib/init/rw
 }
 
-setup_proc_fakechroot () {
+setup_proc_symlink () {
 	rm -rf "$TARGET/proc"
 	ln -s /proc "$TARGET"
 }
diff --git a/scripts/sid b/scripts/sid
index 1d96477..43fb2db 100644
--- a/scripts/sid
+++ b/scripts/sid
@@ -2,6 +2,7 @@ mirror_style release
 download_style apt
 finddebs_style from-indices
 variants - buildd fakechroot minbase
+host_environment - docker
 keyring /usr/share/keyrings/debian-archive-keyring.gpg
 
 if doing_variant fakechroot; then
@@ -94,7 +95,9 @@ Status: install ok installed" >> "$TARGET/var/lib/dpkg/status"
 	}
 
 	if doing_variant fakechroot; then
-		setup_proc_fakechroot
+		setup_proc_symlink
+	elif work_on docker; then
+		setup_proc_symlink
 	else
 		setup_proc
 		in_target /sbin/ldconfig
-- 
2.16.1

