Getting started with ACF development

From Alpine Linux
Revision as of 14:26, 25 October 2007 by Ncopa (talk | contribs)

For the impatient, download http://dev.alpinelinux.org/~ncopa/setup-acf-dev and execute as root.

The script will:

  • Set up a small alpine chroot environment
  • Set up a user in the environment with sudo access
  • Install needed applications like subversion, http server and vim
  • Configure the http server
  • Check out ACF modules from svn repository and install them in the environemt
  • Create a simple .vimrc with useful key binding
  • Create a simple script to enter the chroot


#!/bin/sh

# bootstrap an alpine installation and set up ACF development
# version 1.2

usage() {
        cat << EOF
usage: $(basename $0) TARGETDIR

The following environment variables are used:"

  WGET        path to wget program ($WGET)
  TAR         path to tar program ($TAR)
  MIRROR      alpine mirror ($MIRROR)
  BASE        name of alpine base package ($BASE)
  ACFUSER     non-root username to use in chroot (\$SUDO_USER)
  ACFSVN      svn repository base ($ACFSVN)
  MODULES     svn modules to checkout ($MODULES)
  CHROOT      chroot program. I.E 'linux32 chroot' ($CHROOT)
  PORT        port for mini_httpd ($PORT)
  http_proxy  proxy server ($http_proxy)

EOF
        exit 3
}

die () {
        echo "$@" >&2
        exit 3
}

# read args

# set up vars
: ${WGET:="/usr/bin/wget"}
: ${TAR:="/usr/bin/tar"}
: ${MIRROR:="http://dev.alpinelinux.org/alpine/v1.7"}
: ${ACFSVN:="svn://svn.alpinelinux.org/acf"}
: ${MODULES:="core alpine-baselayout"}
: ${BASE:="base.tar.bz2"}
: ${ACFUSER:="$SUDO_USER"}
: ${CHROOT:="chroot"}
: ${PORT:="80"}

target=$1
CHD="$CHROOT $target"
[ -n "$http_proxy" ] && PROXY="http_proxy=$http_proxy"
APPS="haserl make mini_httpd subversion sudo vim luafilesystem"

# main
[ -z "$target" ] && usage
[ "$target" = "/" ] && die "Bootstrapping Alpine to '/' is probably not a good idea. Aborting..."
[ `whoami` != root ] && die "This script needs to be run as root."

while ! getent passwd "$ACFUSER" >/dev/null 2>&1 && [ "$ACFUSER" != root ] ; do
        echo -n "Enter non-root username: "
        read ACFUSER
done

mkdir -p "$target"

echo ">>> Fetching $MIRROR/$BASE..."
sh -c "$PROXY $WGET -q -O - $MIRROR/$BASE | tar -C \"$target\" -jxv" \
        || die "Failed to fetch or unpack $BASE"

echo ">>> Creating missing dirs..."
for dir in proc sys dev home; do
        mkdir -p "$target/$dir"
done

echo ">>> Installing busybox links..."
# create fake /proc/self/exe
mkdir -p "$target/proc/self"
ln -s /bin/busybox "$target/proc/self/exe"
$CHD /bin/busybox --install -s
rm -r "$target/proc/self"

if [ -f /etc/resolv.conf ]; then
        echo ">>> Copying /etc/resolv.conf..."
        cp /etc/resolv.conf "$target/etc/"
fi

echo ">>> Setting up APK_PATH..."
mkdir -p "$target/etc/apk"
echo "APK_PATH=$MIRROR/apks" >> "$target/etc/apk/apk.conf"

if [ -n "$http_proxy" ]; then
        echo ">>> Setting up http_proxy..."
        sed -i -e '/^http_proxy=/d' "$target/etc/profile"
        echo "http_proxy=$http_proxy" >> "$target/etc/profile"
fi

echo ">>> Settig up user $ACFUSER..."
$CHD adduser -D $ACFUSER

echo ">>> Installing applications..."
$CHD /bin/sh -c "$PROXY apk_add $APPS"

echo ">>> Setting up $ACFUSER as passwordless sudo user..."
sed -i -e "/^$ACFUSER /d" "$target/etc/sudoers"
echo "$ACFUSER ALL=(ALL) NOPASSWD: ALL" >> "$target/etc/sudoers"

echo ">>> Setting up mini_httpd..."
sed -i -e "/^MINI_HTTPD.*/d" "$target/etc/conf.d/mini_httpd"
echo 'MINI_HTTPD_OPTS="-C /etc/mini_httpd.conf"
MINI_HTTPD_DOCROOT=/var/www/localhost/htdocs' \
        >> "$target/etc/conf.d/mini_httpd"
cat << EOF > "$target/etc/mini_httpd.conf"
nochroot
dir=/var/www/localhost/htdocs
user=nobody
logfile=/var/log/mini_httpd.log
cgipat=cgi-bin**
port=$PORT
EOF
$CHD mkdir -p /var/www/localhost
$CHD ln -sf /usr/share/acf/www /var/www/localhost/htdocs

for svnmod in $MODULES ; do
        echo ">>> Checking out ACF module $svnmod"
        $CHD su $ACFUSER -c "svn co $ACFSVN/$svnmod/trunk ~/$svnmod"
        echo ">>> Installing ACF module $svnmod"
        $CHD su $ACFUSER -c "cd ~/$svnmod && sudo make install"
done

echo ">>> Creating .vimrc..."
cat <<EOF > "$target/home/$ACFUSER/.vimrc"
noremap <F9> <C-C>:w<CR> :! sudo make install<CR>
inoremap <F9> <C-C>:w<CR> :! sudo make install<CR>
EOF

echo ">>> Creating enter-chroot script..."
cat <<EOF > "$target/enter-chroot"
dir=\$(dirname \$0)
mount --bind /proc \$dir/proc
echo "Entering ACF develmopment chroot. Type 'exit' or press ctrl-d to leave."
$CHROOT \$(dirname \$0) /bin/sh -c 'su -l $ACFUSER' 
echo "Left ACF development chroot."
umount \$dir/proc
EOF
chmod +x "$target/enter-chroot"

cat <<EOF
>>>
>>> Setup Completed. Enter the chroot environement with (as root):
>>>
>>>     $target/enter-chroot
>>>
>>> Once inside the chroot the privileges will drop to '$ACFUSER'.
>>> You can start and stop mini_httpd within chroot by executing:
>>>
>>>     sudo /etc/init.d/mini_httpd start
>>>     sudo /etc/init.d/mini_httpd stop
>>>
>>> You can specify the port in /etc/mini_httpd.conf
>>>
>>> Remember to install the acf module to activate changes:
>>>
>>>     sudo make install
>>>
EOF