atf.sh 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
#!/bin/bash
#
# atf.sh, an automatic Akabei testing framework Chakra GNU/Linux
# 
# Basen on a-setup.sh by the Chakra Team
# GPL

# version
VER="0.2"

# local root name (safe to change)
BASENAME="buildroot"

# dir vars
CURDIR="${PWD}"
BASEPATH="${CURDIR}/${BASENAME}"

# argument vars
REPO="core"
BRANCH="master"
COMMITMODE="n"
ARCH="x64"
CARCH="x86_64"

# user vars
USERID="$(getent passwd "${USER}" | cut -d: -f3)"

# Remote paths
PKGSOURCE="http://rsync.chakraos.org/packages"

# setup local root dir
PM_BIN="pacman"
PM_CONF="pacman.conf"
mkdir -p "${BASEPATH}"

REPO_NAME="${REPO}"
CHROOT="${BASEPATH}/_chroots/$BRANCH-${ARCH}"
CHAKRAFOLDER="chakra"
CHAKRADIR="${CHROOT}/${CHAKRAFOLDER}"
REPODIR="${CHAKRADIR}/${REPO_NAME}"

# Pkgs to install
INSTALLPKGS=('base' 'base-devel' 'cmake' 'openssh' 'git' 'sudo' 'boost' 'vim'
             'rsync' 'squashfs-tools' 'curl' 'libusb-compat'
             'gnupg' 'cdrkit' 'bash-completion' 'akabei')

#
# formatted output functions
#
title() {
    local mesg="${1}"; shift
    echo " "
    printf "\033[1;33m>>>\033[1;0m\033[1;1m ${mesg}\033[1;0m\n"
    echo " "
}

msg() {
    local mesg="${1}"; shift
    echo -e "\033[1;32m::\033[1;0m\033[1;0m ${mesg}\033[1;0m"
}

question() {
    local mesg="${1}"; shift
    echo -e -n "\033[1;32m::\033[1;0m\033[1;0m ${mesg}\033[1;0m"
}

notice() {
    local mesg="${1}"; shift
    echo -e -n ":: ${mesg}\n"
}

warning() {
    local mesg="${1}"; shift
    printf "\033[1;33m::\033[1;0m\033[1;1m ${mesg}\033[1;0m\n"
}

error() {
    local mesg="${1}"; shift
    printf "\033[1;31m::\033[1;0m\033[1;0m ${mesg}\033[1;0m\n"
}

newline() {
    echo
}

status_start() {
    local mesg="${1}"; shift
    echo -e -n "\033[1;32m::\033[1;0m\033[1;0m ${mesg}\033[1;0m"
}

status_ok() {
    echo -e "\033[1;32m OK \033[1;0m"
}

status_done() {
    echo -e "\033[1;32m DONE \033[1;0m"
}

banner() {
    newline
    printf '\e[1;36m'
    echo "  .,-:::::   ::   .:   :::.      :::  .   :::::::..    :::.     "
    echo ",;;;'\`\`\`\`'  ,;;   ;;,  ;;\`;;     ;;; .;;,.;;;;\`\`;;;;   ;;\`;;    "
    echo "[[[        ,[[[,,,[[[ ,[[ '[[,   [[[[[/'   [[[,/[[['  ,[[ '[[,  "
    echo " \$\$\$       \"\$\$\$\"\"\"\$\$\$c\$\$\$cc\$\$\$c _\$\$\$\$,     $\$\$\$\$\$c   c\$\$\$cc\$\$\$c "
    echo "\`88bo,__,o, 888   \"88o888   888,\"888\"88o,  888b \"88bo,888   888,"
    echo "   YUMMMMMP\"MMM    YMMYMM   \"\"\`  MMM \"MMP\" MMMM   \"W\" YMM   \"\"\` "
    printf '\e[0m'
    newline
}

#
# check distro and needed packages
#
if [ ! -e "/usr/bin/git" ] ; then
    newline
    error "this script needs the package GIT installed, please install it before continuing."
    newline
    exit
fi

#
# {u,}mount chroot's {dev,sys,proc,cache} functions
#
mount_special() {
    sudo mount -v /dev "${CHROOT}/dev" --bind &>/dev/null
    sudo mount -v /sys "${CHROOT}/sys" --bind &>/dev/null
    sudo mount -v /proc "${CHROOT}/proc" --bind &>/dev/null
    sudo mount -v "${BASEPATH}/_cache-${CARCH}" "${CHROOT}/var/cache/pacman/pkg" --bind &>/dev/null
}
umount_special() {
    sudo umount -v "${CHROOT}/dev" &>/dev/null
    sudo umount -v "${CHROOT}/sys" &>/dev/null
    sudo umount -v "${CHROOT}/proc" &>/dev/null
    sudo umount -v "${CHROOT}/var/cache/pacman/pkg" &>/dev/null
}

#
# generate a pacman manager conf
#
create_pacmanconf() {
    newline
    msg "creating ${PM_CONF}"
    echo >> "${BASEPATH}/${PM_CONF}"
    echo "[options]" >> "${BASEPATH}/${PM_CONF}"
    echo "HoldPkg=pacman glibc" >> "${BASEPATH}/${PM_CONF}"
    echo "SigLevel=Never" >> "${BASEPATH}/${PM_CONF}"
    echo >> "${BASEPATH}/${PM_CONF}"

    local PACREPO
    for PACREPO in testing core platform desktop; do
        echo "[${PACREPO}]" >> ${BASENAME}/${PM_CONF}
        echo "Server=${PKGSOURCE}/${PACREPO}/${CARCH}/" >> ${BASENAME}/${PM_CONF}
        echo >> ${BASENAME}/${PM_CONF}
    done
}

#
# chroot creation functions
#
check_chroot() {
    if [ -d "${CHROOT}" ] ; then
	newline
	error "The chroot ${BRANCH}-${ARCH} already exists. Do you want to"
	newline
	msg "(k)eep chroot ${BRANCH}-${ARCH} and update it?"
	msg "(d)elete/reinstall chroot ${BRANCH}-${ARCH}?"
	msg "(u)ninstall chroot ${BRANCH}-${ARCH}?"
	question "(q)uit this script? "
	read OPTION
	case "${OPTION}" in
	    d* )
		newline
		status_start "chroot ${BRANCH}-${ARCH}  "
		    cd "${BASEPATH}"
		    sudo -v
		    sudo rm -rf -v "${CHROOT}" &>/dev/null
		    sudo rm -rf -v "${BASEPATH}/${REPO_NAME}-${CARCH}" &>/dev/null
		    sudo -v
		status_done
		;;
	    u* )
		newline
		status_start "uninstalling ${REPO_NAME}-${CARCH}  "
		    cd "${BASEPATH}"
		    sudo -v
		    sudo rm -rf -v "${CHROOT}" &>/dev/null
		    sudo rm -rf -v "${BASEPATH}/${REPO_NAME}-${CARCH}" &>/dev/null
		    sudo -v
		status_done
		newline
		exit 1
		;;
	    k* )
		newline
		msg "going on ..."
		newline
		;;
	    q* )
		newline
		msg "bye!"
		newline
		exit 1
		;;
	esac
    fi

    if [ -d "${BASEPATH}/${REPO_NAME}-${CARCH}" ] ; then
	newline
	error "The ${REPO_NAME}-${CARCH} already exists. Do you want to"
	newline
	msg "(d)elete and reinstall ${REPO_NAME}-${CARCH}?"
	msg "(u)ninstall ${REPO_NAME}-${CARCH}?"
	question "(q)uit this script? "
	read OPTION
	case "${OPTION}" in
	    d* )
		newline
		status_start "deleting ${REPO_NAME}-${CARCH}  "
		    uninstall_chroot
		status_done
		;;
	    u* )
		newline
		status_start "uninstalling ${REPO_NAME}-${CARCH}  "
		    uninstall_chroot
		status_done
		newline
		exit 1
		;;
	    q* )
		newline
		msg "bye!"
		newline
		exit 1
		;;
	esac
    fi
}

uninstall_chroot() {
    cd "${BASEPATH}"

    # Umount stuff.
    sudo umount -v "${CHROOT}/dev" &>/dev/null
    sudo umount -v "${CHROOT}/sys" &>/dev/null
    sudo umount -v "${CHROOT}/proc" &>/dev/null
    sudo umount -v "${CHROOT}/var/cache/pacman/pkg" &>/dev/null

    sudo rm -rf -v "${BASEPATH}/${REPO_NAME}-${CARCH}" &>/dev/null
    sudo -v

}

pre_install_packages() {
    newline
    title "Creating chroot: ${REPO_NAME}-${CARCH}"
    newline
    status_start "creating special dirs"
	mkdir -p "${CHROOT}"/{dev,sys,proc,var/cache/pacman/pkg}
    status_done
    status_start "mounting special dirs"
	mount_special
    status_done
    status_start "creating pacman dirs"
	mkdir -p "${CHROOT}/var/lib/pacman" &>/dev/null
    status_done
}

install_packages() {
    if [ "${_creationLoop}" == "1" ] ; then
	unset _creationLoop 
	warning "Seems some went wrong. Check the pacman install log!"
	newline
	if [ "$_updateRet" == "1" ] ; then
	    msg "(r)etry package update?"
	    msg "(s)kip package update at your own risk?"
	else
	    msg "(r)etry package installation?"
	    msg "(s)kip package installation at your own risk?"
	fi
	question "(q)uit this script? "
	read OPTION
	case "${OPTION}" in
	    r* )
		newline 
		if [ "${_updateRet}" == "1" ] ; then
		    msg "retrying package update" 
		else
		    msg "retrying package installation" 
		fi
		sleep 4 
		install_packages 
		;;
            s* )
		newline 
		if [ "${_updateRet}" == "1" ] ; then
		    msg "skipping package update" 
		else
		    msg "skipping package installation" 
		fi
		return 0 
		;;
            q* )
		umount_special
		rm -f "${BASEPATH}/${PM_CONF}"
		newline 
		msg "bye!" 
		newline
		exit 1
		;;	
	esac
    fi
    sudo -v
    newline

    # Theorically if the user exists the chroot as been created
    if [ "${_installDone}" != "1" ] ; then
	if [ -d "${CHROOT}/home/${USER}" ] ; then
	    msg "updating chroot ${BRANCH}-${ARCH}"
	    unset _installDone
	    unset _updateRet
	    unset _creationLoop
	    sudo LC_ALL=C "${PM_BIN}" --noconfirm --needed --config "${BASEPATH}/${PM_CONF}" -r "${CHROOT}" --cachedir "${BASEPATH}/_cache-${ARCH}" -Syyu || _updateRet="1"
	    if [ "${_updateRet}" == "1" ] ; then
		error "!! failed to update the chroot (not good)..."
		_creationLoop="1"
		install_packages
	    else
		_installDone="1"
	    fi
	else
	    msg "installing chroot ${BRANCH}-${ARCH}"
	    unset _installDone
	    unset _installRet
	    unset _creationLoop
	    sudo LC_ALL=C "${PM_BIN}" --noconfirm --needed --config "${BASEPATH}/${PM_CONF}" -r "${CHROOT}" --cachedir "${BASEPATH}/_cache-${ARCH}" -Syy "${INSTALLPKGS[@]}" || _installRet="1"
	    if [ "${_installRet}" == "1" ] ; then
		error "!! failed to install the needed packages..."
		_creationLoop="1"
		install_packages
	    else
		_installDone="1"
	    fi
	fi
    fi
}

create_chroot() {
    sudo -v
    newline

    status_start "configuring system"
        mkdir -p "${CHROOT}/etc" &>/dev/null
        sudo cp /etc/resolv.conf "${CHROOT}/etc" &>/dev/null
        if [[ ! -f "${CHROOT}/etc/${PM_CONF}.bak" ]]; then
            sudo mv "${CHROOT}/etc/${PM_CONF}"{,.bak}
            sudo cp "${BASEPATH}/${PM_CONF}" "${CHROOT}/etc" &>/dev/null
        fi
    status_done

    status_start "setting up locale.conf"
	sudo touch "${CHROOT}/etc/locale.conf"
	sudo chmod 777 "${CHROOT}/etc/locale.conf"
	sudo echo "LANG=C" >> "${CHROOT}/etc/locale.conf"
	sudo echo "LC_MESSAGES=C" >> "${CHROOT}/etc/locale.conf"
    status_done

    # search for a user already existing or create it
    newline
    if [ "$(grep "^${USER}:" "${CHROOT}/etc/passwd" | cut -d ":" -f1)" != "${USER}" ] ; then
	title "User setup"

	status_start "adding user: ${USER}"
	    sudo chroot "${CHROOT}" useradd -g users -u "${USERID}" -G audio,video,optical,storage,log -m "${USER}" &>/dev/null
	status_done

	warning "you will be asked to enter a password for the chroot's user account"

	sudo -v
	newline

	sudo chroot "${CHROOT}" passwd "${USER}"

	sudo -v
	newline

	status_start "setting up /etc/sudoers"
	    sudo chmod 777 "${CHROOT}/etc/sudoers"
	    sudo echo >> "${CHROOT}/etc/sudoers"
	    sudo echo "${USER}     ALL=(ALL) NOPASSWD: ALL" >> "${CHROOT}/etc/sudoers"
	    sudo chmod 0440 "${CHROOT}/etc/sudoers"
	status_done
    else
	msg "found user: ${USER}"
    fi

    status_start "setting up device permissions"
        sudo chroot "${CHROOT}" chmod 777 /dev/console &>/dev/null
        sudo chroot "${CHROOT}" chmod 777 /dev/null &>/dev/null
        sudo chroot "${CHROOT}" chmod 777 /dev/zero &>/dev/null
    status_done

    status_start "unmounting special dirs"
        umount_special
    status_done
}

# initialize sudo and ssh
setup_ssh() {
    if [ -e "/usr/bin/sudo" ] ; then
	newline
	warning "initializing sudo, you may be asked for your password"
	newline
	sudo /bin/true &>/dev/null
    else
	newline
	error "please install and configure sudo"
	newline
	exit 1
    fi
}

create_databasedir() {
    mkdir -p $BASEPATH/databases
    akabei-create-db generate $BASEPATH/databases/local.db $BASEPATH/_cache-x64/*
    sudo chown root:root $BASEPATH/databases/local.db
    sudo chmod g+r,o+r $BASEPATH/databases/local.db
    sudo chown root:root $CHROOT
}


prepare_akabei_conf() {
    title "Preparing akabei.conf ..."
435
    wget -O akabei.conf https://gitorious.org/chakra/akabei/raw/master:scripts/akabei-test-chroot/akabei.conf
436 437 438 439 440 441 442 443 444 445 446 447 448
    sed -i "s|YOURPATH|$(pwd)|g" ./akabei.conf
    sudo mv ./akabei.conf /etc/akabei.conf
}

import_signatures() {
    title "Importing Chakra signatures to akabei-keyring ..."
    newline
    sudo mkdir ./buildroot/_chroots/master-x64/etc/akabei.d/
    sudo mkdir ./buildroot/_chroots/master-x64/etc/akabei.d/keyring/
    sudo chmod go-r-x buildroot/_chroots/master-x64/etc/akabei.d/keyring/
    
    sudo gpg --homedir ./buildroot/_chroots/master-x64/etc/akabei.d/keyring/ --trust-model always --import /usr/share/chakra/signatures/*.asc

AlmAck's avatar
AlmAck committed
449
    for key in 3DB6614F EBC85A93 26C56140 BF2412F9 02238B03 B6611E8A C51B9BC2 D417608D F906E3F4 4CA5A1FF 82AC496A; do
450
        sudo gpg --homedir ./buildroot/_chroots/master-x64/etc/akabei.d/keyring --recv-keys --keyserver keys.gnupg.net $key
451 452 453
        printf 'trust\n5\ny\quit\n' | sudo gpg --homedir ../buildroot/_chroots/master-x64/etc/akabei.d/keyring --no-permission-warning --command-fd 0 --edit-key $key
    done

454
    sudo gpg --homedir ./buildroot/_chroots/master-x64/etc/akabei.d/keyring --list-keys
AlmAck's avatar
AlmAck committed
455
    # show the current imported keys
456

AlmAck's avatar
AlmAck committed
457
    # set one more permission to make polkit work correctly
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    sudo chown root:root ./buildroot/_chroots/master-x64/etc/akabei.d/
}

# all done! :) print some information
all_done() {
    newline
    title "All done!"
    newline
    msg "Now the setup is complete and you can start"
    msg "with updating the databases of akabei:"
    newline
    msg "akabei sync"
    newline
    msg "For forther information follow the wiki:"
    msg "http://chakraos.org/wiki/index.php?title=Test_Akabei"
    newline
}

#
# startup
#
clear
banner
title "Akabei Testing Framework Setup Script v${VER}"

# ensure we are not running as root
if [ ${UID} -ne 0 ]; then
    msg "running on user: ${USER}"
else
    newline
    error "do not run me on your root account, thanks."
    newline
    exit 1
fi

newline
warning "Root                 : ${BASEPATH}"
warning "Chroot Installation  : ${BASEPATH}/${REPO_NAME}-${ARCH}"
sleep 1
newline
question "Do you want to continue (y/n/q)? "

while true ; do
    read YN
    case $YN in
        [yY]* )
            setup_ssh 
            create_pacmanconf 
            check_chroot 
            pre_install_packages 
            install_packages
            create_databasedir
            prepare_akabei_conf
	    import_signatures
            all_done
	    exit 
	    ;;
        [nN]* )
	    newline 
	    msg "bye!" 
	    newline 
	    exit 
	    ;;

        [qQ]* )
	    exit
	    ;;

	* )
	    echo "Enter (y)es, (n)o or (q)uit"
	    ;;
    esac
done