Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 309 lines (255 sloc) 6.48 KB
#!/usr/bin/env bash
# shellcheck disable=SC1117
JOBSET=./jobset.nix
GET_ATTRS=./scripts/get-attrs.nix
BASH_COMPL_SCRIPT=./scripts/please.complete.bash.sh
NIX_INSTALL_URL="${NIX_INSTALL_URL:-https://nixos.org/nix/install}"
log() {
local args="$*"
local PLEASE="\e[32m[please]:\e[0m"
echo -e "$PLEASE $args"
}
log-error() {
local args="$*"
local PLEASE="\e[31m[please]\e[0m"
echo -e "$PLEASE $args"
}
log-run() {
local cmd="$1"
log "Running \"$cmd\"\n"
eval $cmd
}
_list_attrs() {
nix-instantiate --strict --eval --expr "(import $GET_ATTRS {}).all" | tr -d "[]\""
}
_force_nixpkgs() {
# without evaluating this all our queries will fail
nix-build --quiet ./nixpkgs-fetch.nix >/dev/null
}
_get_name() {
local pkg="$1"
nix-instantiate --strict --eval --expr "(import ./jobset.nix {}).$1.name" | tr -d '"'
}
#
# sanity check functions
#
_isRegularUser() {
test $(id -u) -ne 0
}
_hasKvmSupport() {
test -c /dev/kvm && test -w /dev/kvm && test -r /dev/kvm
}
_isChannelInstalled() {
nix-channel --list | grep contrail >/dev/null
}
_isSubstituterConfigured() {
nix show-config | grep "cache.nix.corp.cloudwatt.com" >/dev/null
}
_isNixInstalled() {
nix --version >/dev/null 2>&1
}
_sourceNix() {
NIX_SH="$HOME/.nix-profile/etc/profile.d/nix.sh"
test -f "$NIX_SH" && source "$NIX_SH"
}
_addCacheConfig() {
if test -f ~/.config/nix/nix.conf
then
log "$HOME/.config/nix/nix.conf exists. Please follow the instructions from the README"
else
mkdir -p "$HOME"/.config/nix/
cat << EOF > "$HOME"/.config/nix/nix.conf
substituters = https://cache.nixos.org https://cache.nix.corp.cloudwatt.com
trusted-substituters = https://cache.nix.corp.cloudwatt.com
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cache.nix.cloudwatt.com:ApPt6XtZeOQ3hNRTSBQx+m6rd8p04G0DQwz0bZfVPL8=
EOF
fi
}
check_args() {
local actual="$1"
local expected="$2"
local cmd="$3"
if [ "$actual" -ne "$expected" ]; then
log-error "'$cmd' requires $expected arguments but $actual were given"
exit 1
fi
}
#
# subcommands
#
register-completions() {
cat "$BASH_COMPL_SCRIPT"
}
build() {
local pkg="$1"
log-run "nix-build default.nix -A $pkg"
if [ $? -eq 0 ]; then
echo ""
log "Your build result is symlinked in ./result"
fi
}
install() {
local pkg="$1"
log-run "nix-env -f default.nix -iA $pkg"
}
uninstall() {
local pkg="$1"
log-run "nix-env -e $pkg"
}
shell() {
local pkg="$1"
log-run "nix-shell default.nix -A $pkg"
}
init() {
log "Initializing setup ..."
if _isNixInstalled ; then
log "Looks like nix is already installed"
else
log "Looks like nix is not installed yet"
log "Running 'curl https://nixos.org/nix/install | sh'"
curl "$NIX_INSTALL_URL" | sh
_sourceNix
fi
if _isChannelInstalled; then
log "channel is already configured"
else
log "adding contrail channel"
log-run "nix-channel --add https://hydra.nix.corp.cloudwatt.com/jobset/nixpkgs-tungsten/trunk/channel/latest contrail"
fi
if _isSubstituterConfigured; then
log "cache is already configured"
else
log "adding contrail binary cache"
_addCacheConfig
fi
}
doctor() {
OK="\e[32mOK\e[0m"
X="\e[31mX\e[0m"
FAIL=""
log "Running sanity checks:\n"
if _isNixInstalled
then
echo -e "- Nix installed : $OK"
else
echo -e "- Nix installed : $X"
FAIL="."
fi
if _isChannelInstalled
then
echo -e "- contrail channel: $OK"
else
echo -e "- contrail channel: $X"
FAIL="."
fi
if _isSubstituterConfigured
then
echo -e "- contrail cache: $OK"
else
echo -e "- contrail cache: $X"
FAIL="."
fi
if _hasKvmSupport
then
echo -e "- kvm support: $OK"
else
echo -e "- kvm support: '/dev/kvm' either not found or you don't have r/w permissions. You probably won't be able to start VMs"
fi
if [[ $FAIL = "" ]]; then
echo -e "\nAll essential tests passed."
else
echo -e "\nSome tests failed. Try running the init command:\n"
echo -e " ./please init\n"
echo -e "Check the 'Manual Configuration' section of the README.md if you continue to experience problems."
exit 1
fi
}
list() {
local args="$*"
if [ -z "$args" ]; then
_list_attrs | tr " " "\n"
else
_list_attrs | tr " " "\n" | grep "$args"
fi
}
run-test(){
local t="$1"
log-run "nix-build default.nix -A $t"
}
run-vm(){
local t="$1"
log-run "nix-build default.nix -A $t.driver"
QEMU_NET_OPTS="hostfwd=tcp::8080-:8080,hostfwd=tcp::8143-:8143,hostfwd=tcp::2222-:22" result/bin/nixos-run-vms
}
usage() {
cat <<EOM
Usage: please <command> [args]
build [artifact] -- build an artifact
completions -- output completion script
doctor -- perform sanity checks
init -- configure initial setup
install [artifact] -- install an artifact
list -- list artifacts and tests
run-test [test] -- run a test
run-vm [test] -- run an interactive vm of a test
shell [artifact] -- enter a dev shell for an artifact
uninstall [artifact] -- uninstall a previously installed artifact
In order to enable context-sensitive completions (bash only!) run:
$ source <(./please completions)
You should add this to your init scripts.
EOM
}
if ! _isRegularUser; then
log-error "root user detected. Run ./please as non-root user!"
exit 1
fi
if [ -z "$1" ]; then
usage
exit 1
fi
_sourceNix
_isNixInstalled && _force_nixpkgs
subcommand="$1"
shift
case "$subcommand" in
build)
check_args $# 1 "build"
build "$@"
;;
install)
check_args $# 1 "install"
install "$@"
;;
uninstall)
check_args $# 1 "uninstall"
uninstall $(_get_name "$@")
;;
shell)
check_args $# 1 "shell"
shell "$@"
;;
init)
init "$@"
;;
list)
list "$@"
;;
run-test)
check_args $# 1 "run-test"
run-test "$@"
;;
run-vm)
check_args $# 1 "run-vm"
run-vm "$@"
;;
doctor)
doctor
;;
completions)
register-completions
;;
*)
echo "Error: unknown command: $subcommand"
usage
exit 1
esac
You can’t perform that action at this time.