stockholm/Makefile

122 lines
3.7 KiB
Makefile
Raw Normal View History

stockholm ?= .
2016-03-10 11:32:29 +00:00
export HOSTNAME ?= $(shell cat /proc/sys/kernel/hostname)
2016-03-05 11:40:20 +00:00
export STOCKHOLM_VERSION ?= $(shell \
version=git.$$(git describe --always --dirty); \
2016-03-10 11:32:29 +00:00
case $$version in (*-dirty) version=$$version@$$HOSTNAME; esac; \
2016-03-05 11:40:20 +00:00
date=$$(date +%y.%m); \
printf '%s' "$$date.$$version"; \
)
2016-05-20 01:21:56 +00:00
system ?= $(HOSTNAME)
$(if $(system),,$(error unbound variable: system))
2016-03-10 11:41:24 +00:00
nixos-config ?= $(stockholm)/$(LOGNAME)/1systems/$(system).nix
2016-02-16 05:20:40 +00:00
# target = [target_user@]target_host[:target_port][/target_path]
ifdef target
_target_user != echo $(target) | sed -n 's/@.*//p'
_target_path != echo $(target) | sed -n 's/^[^/]*//p'
_target_port != echo $(target) | sed -En 's|^.*:([^/]*)(/.*)?$$|\1|p'
_target_host != echo $(target) | sed -En 's/^(.*@)?([^:/]*).*/\2/p'
ifneq ($(_target_host),)
$(if $(target_host),$(error cannot define both, target_host and host in target))
target_host ?= $(_target_host)
endif
ifneq ($(_target_user),)
$(if $(target_user),$(error cannot define both, target_user and user in target))
target_user ?= $(_target_user)
endif
ifneq ($(_target_port),)
$(if $(target_port),$(error cannot define both, target_port and port in target))
target_port ?= $(_target_port)
endif
ifneq ($(_target_path),)
$(if $(target_path),$(error cannot define both, target_path and path in target))
target_path ?= $(_target_path)
endif
endif
export target_host ?= $(system)
export target_user ?= root
export target_port ?= 22
export target_path ?= /var/src
2016-02-02 02:19:20 +00:00
$(if $(target_host),,$(error unbound variable: target_host))
$(if $(target_user),,$(error unbound variable: target_user))
$(if $(target_port),,$(error unbound variable: target_port))
$(if $(target_path),,$(error unbound variable: target_path))
2016-06-04 22:25:47 +00:00
build = \
nix-build \
--no-out-link \
--show-trace \
-I nixos-config=$(nixos-config) \
-I stockholm=$(stockholm) \
-E "let build = import <stockholm>; in $(1)"
2016-02-04 10:32:13 +00:00
evaluate = \
nix-instantiate \
--eval \
--readonly-mode \
--show-trace \
-I nixos-config=$(nixos-config) \
-I stockholm=$(stockholm) \
-E "let eval = import <stockholm>; in with eval; $(1)"
2016-02-04 10:32:13 +00:00
execute = \
result=$$($(call evaluate,config.krebs.build.$(1))) && \
script=$$(echo "$$result" | jq -r .) && \
echo "$$script" | PS5=% sh
2016-02-04 10:32:13 +00:00
2016-05-20 00:52:23 +00:00
ifeq ($(MAKECMDGOALS),)
$(error No goals specified)
endif
# usage: make deploy system=foo [target_host=bar]
deploy: ssh ?= ssh
2016-02-04 10:32:13 +00:00
deploy:
$(call execute,populate)
$(ssh) $(target_user)@$(target_host) -p $(target_port) \
2016-03-05 11:40:20 +00:00
env STOCKHOLM_VERSION="$$STOCKHOLM_VERSION" \
nixos-rebuild switch --show-trace -I $(target_path)
2016-06-04 22:25:47 +00:00
# usage: make build.pkgs.get
build build.:;@$(call build,$${expr-eval})
build.%:;@$(call build,$@)
2016-02-04 10:32:13 +00:00
# usage: make LOGNAME=shared system=wolf eval.config.krebs.build.host.name
eval eval.:;@$(call evaluate,$${expr-eval})
eval.%:;@$(call evaluate,$@)
2016-02-12 18:56:04 +00:00
# usage: make install system=foo [target_host=bar]
2016-02-12 22:20:50 +00:00
install: ssh ?= ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
2016-02-12 18:56:04 +00:00
install:
$(ssh) $(target_user)@$(target_host) -p $(target_port) \
2016-02-12 18:56:04 +00:00
env target_path=$(target_path) \
sh -s prepare < krebs/4lib/infest/prepare.sh
target_path=/mnt$(target_path) $(call execute,populate)
$(ssh) $(target_user)@$(target_host) -p $(target_port) \
2016-02-12 18:56:04 +00:00
env NIXOS_CONFIG=$(target_path)/nixos-config \
2016-03-05 11:40:20 +00:00
STOCKHOLM_VERSION="$$STOCKHOLM_VERSION" \
2016-02-12 18:56:04 +00:00
nixos-install
2016-02-16 06:41:26 +00:00
# usage: make test system=foo [target=bar] [method={eval,build}]
method ?= eval
ifeq ($(method),build)
test: command = nix-build --no-out-link
else
ifeq ($(method),eval)
test: command ?= nix-instantiate --eval --json --readonly-mode --strict
else
$(error bad method: $(method))
endif
endif
test: ssh ?= ssh
2016-02-16 06:41:26 +00:00
test:
$(call execute,populate)
$(ssh) $(target_user)@$(target_host) -p $(target_port) \
$(command) --show-trace -I $(target_path) \
2016-02-16 06:41:26 +00:00
-A config.system.build.toplevel $(target_path)/stockholm