2016-02-16 06:31:37 +00:00
|
|
|
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)
|
2016-02-16 06:31:37 +00:00
|
|
|
$(if $(system),,$(error unbound variable: system))
|
2016-03-10 11:41:24 +00:00
|
|
|
|
|
|
|
nixos-config ?= $(stockholm)/$(LOGNAME)/1systems/$(system).nix
|
2016-06-13 15:27:46 +00:00
|
|
|
ifneq ($(words $(wildcard $(nixos-config))),1)
|
|
|
|
$(error bad nixos-config: $(nixos-config))
|
|
|
|
endif
|
2015-12-22 19:31:21 +00:00
|
|
|
|
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
|
|
|
|
|
2016-07-17 00:58:20 +00:00
|
|
|
target_host ?= $(system)
|
|
|
|
target_user ?= root
|
|
|
|
target_port ?= 22
|
|
|
|
target_path ?= /var/src
|
2016-02-02 02:19:20 +00:00
|
|
|
|
2016-02-16 06:31:37 +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))
|
|
|
|
|
2017-06-27 21:28:41 +00:00
|
|
|
whatsupnix = \
|
|
|
|
if type whatsupnix >/dev/null 2>&1; then \
|
|
|
|
whatsupnix $(1); \
|
|
|
|
else \
|
|
|
|
cat; \
|
|
|
|
fi
|
|
|
|
|
2016-06-04 22:25:47 +00:00
|
|
|
build = \
|
|
|
|
nix-build \
|
2017-06-27 21:28:41 +00:00
|
|
|
-Q \
|
2016-06-04 22:25:47 +00:00
|
|
|
--no-out-link \
|
|
|
|
--show-trace \
|
|
|
|
-I nixos-config=$(nixos-config) \
|
|
|
|
-I stockholm=$(stockholm) \
|
2017-06-27 21:28:41 +00:00
|
|
|
-E "with import <stockholm>; $(1)" \
|
|
|
|
$(2) \
|
|
|
|
|& $(call whatsupnix)
|
2016-06-04 22:25:47 +00:00
|
|
|
|
2016-02-04 10:32:13 +00:00
|
|
|
evaluate = \
|
|
|
|
nix-instantiate \
|
|
|
|
--eval \
|
|
|
|
--readonly-mode \
|
|
|
|
--show-trace \
|
2016-02-16 06:31:37 +00:00
|
|
|
-I nixos-config=$(nixos-config) \
|
|
|
|
-I stockholm=$(stockholm) \
|
2017-06-27 21:29:43 +00:00
|
|
|
-E "let eval = import <stockholm>; in with eval; $(1)" \
|
|
|
|
$(2)
|
2016-02-04 10:32:13 +00:00
|
|
|
|
2016-05-20 00:52:23 +00:00
|
|
|
ifeq ($(MAKECMDGOALS),)
|
|
|
|
$(error No goals specified)
|
|
|
|
endif
|
|
|
|
|
2016-02-04 04:40:26 +00:00
|
|
|
# usage: make deploy system=foo [target_host=bar]
|
2016-07-17 00:58:20 +00:00
|
|
|
ifeq ($(debug),true)
|
|
|
|
deploy: rebuild-command = dry-activate
|
|
|
|
else
|
|
|
|
deploy: rebuild-command = switch
|
|
|
|
endif
|
2016-02-16 07:24:08 +00:00
|
|
|
deploy: ssh ?= ssh
|
2016-07-17 00:58:20 +00:00
|
|
|
deploy:
|
|
|
|
$(MAKE) populate debug=false
|
2016-02-16 07:24:08 +00:00
|
|
|
$(ssh) $(target_user)@$(target_host) -p $(target_port) \
|
2016-03-05 11:40:20 +00:00
|
|
|
env STOCKHOLM_VERSION="$$STOCKHOLM_VERSION" \
|
2017-06-15 17:57:54 +00:00
|
|
|
nixos-rebuild -Q $(rebuild-command) --show-trace -I $(target_path) \
|
2017-06-27 21:28:41 +00:00
|
|
|
|& $(call whatsupnix,$(target_user)@$(target_host):$(target_port))
|
2016-02-04 04:40:26 +00:00
|
|
|
|
2016-07-16 19:43:38 +00:00
|
|
|
# usage: make populate system=foo
|
2016-10-03 13:21:31 +00:00
|
|
|
populate: populate-target = \
|
|
|
|
$(target_user)@$(target_host):$(target_port)$(target_path)
|
2016-07-16 19:43:38 +00:00
|
|
|
ifeq ($(debug),true)
|
2016-07-17 00:37:46 +00:00
|
|
|
populate: populate-flags += --debug
|
2016-07-16 19:43:38 +00:00
|
|
|
endif
|
2016-07-16 23:30:11 +00:00
|
|
|
ifneq ($(ssh),)
|
|
|
|
populate: populate-flags += --ssh=$(ssh)
|
|
|
|
endif
|
2016-07-16 19:43:38 +00:00
|
|
|
populate:
|
2016-07-16 22:31:53 +00:00
|
|
|
$(call evaluate,config.krebs.build.source) --json --strict | \
|
2016-10-03 13:21:31 +00:00
|
|
|
populate $(populate-target) $(populate-flags)
|
2016-07-16 19:43:38 +00:00
|
|
|
|
2016-07-16 22:52:21 +00:00
|
|
|
# usage: make pkgs.populate
|
|
|
|
pkgs:;@$(error no package selected)
|
|
|
|
pkgs.%:;@$(call build,$@)
|
2016-06-04 22:25:47 +00:00
|
|
|
|
2016-02-04 10:32:13 +00:00
|
|
|
# usage: make LOGNAME=shared system=wolf eval.config.krebs.build.host.name
|
2016-02-19 15:15:14 +00:00
|
|
|
eval eval.:;@$(call evaluate,$${expr-eval})
|
2016-05-20 01:02:35 +00:00
|
|
|
eval.%:;@$(call evaluate,$@)
|
2016-02-04 04:40:26 +00:00
|
|
|
|
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:
|
2016-02-13 15:03:40 +00:00
|
|
|
$(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
|
2016-07-16 19:43:38 +00:00
|
|
|
$(MAKE) populate target_path=/mnt$(target_path)
|
2016-02-13 15:03:40 +00:00
|
|
|
$(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
|
|
|
|
2016-02-16 11:49:04 +00:00
|
|
|
# usage: make test system=foo [target=bar] [method={eval,build}]
|
|
|
|
method ?= eval
|
|
|
|
ifeq ($(method),build)
|
2017-06-27 21:29:43 +00:00
|
|
|
test: test = $(call build,$(1),$(2))
|
2016-02-16 11:49:04 +00:00
|
|
|
else
|
|
|
|
ifeq ($(method),eval)
|
2017-06-27 21:29:43 +00:00
|
|
|
test: test ?= $(call evaluate,$(1),$(2)) --json --strict | jq -r .
|
2016-02-16 11:49:04 +00:00
|
|
|
else
|
|
|
|
$(error bad method: $(method))
|
|
|
|
endif
|
|
|
|
endif
|
2016-02-16 07:24:08 +00:00
|
|
|
test: ssh ?= ssh
|
2016-08-04 19:36:44 +00:00
|
|
|
ifeq ($(target_user)@$(target_host),$(LOGNAME)@$(HOSTNAME))
|
|
|
|
test: wrapper = exec
|
|
|
|
else
|
|
|
|
test: wrapper = $(ssh) $(target_user)@$(target_host) -p $(target_port)
|
|
|
|
endif
|
2016-07-16 19:43:38 +00:00
|
|
|
test: populate
|
2017-06-27 21:29:43 +00:00
|
|
|
$(wrapper) $(call test,config.system.build.toplevel,-I $(target_path))
|