aboutsummaryrefslogtreecommitdiff
path: root/nixops/Makefile
blob: a7b24cd98c7cd7dfe7d78daf57eadb0f39612a91 (plain) (blame)
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
export
PASSWORD_STORE_DIR = $(shell pwd)/secrets
NIXOPS_STATE ?= ./state/eldiron.nixops
NIXOPS_DEPLOYMENT = cef694f3-081d-11e9-b31f-0242ec186adf
nixpkgs ?= $(shell cat ../nix/sources.json | jq -r '."nixpkgs-nixops".url')
nixpkgsNext ?= $(shell cat ../nix/sources.json | jq -r '."nixpkgs-nixops-next".url')
nixpkgsPrevious ?= $(shell cat ../nix/sources.json | jq -r '."nixpkgs-nixops-previous".url')
NIX_PATH = nixpkgs=${nixpkgs}:nixpkgsNext=${nixpkgsNext}:nixpkgsPrevious=${nixpkgsPrevious}

NIXOPS := $(shell NIX_PATH=$(NIX_PATH) nix-build --no-out-link -E "with import <nixpkgs> { overlays = builtins.attrValues (import ../overlays); }; nixops")/bin/nixops
NIXOPS_PRIV = ./scripts/with_env $(NIXOPS)

###### Current channel information
nix-info:
	@version=$$(nix eval --raw nixpkgs.lib.version) && \
	  mainversion=$$(echo $$version | cut -d"." -f -2) && \
	  echo "https://releases.nixos.org/nixos/$$mainversion/nixos-$$version/nixexprs.tar.xz" && \
	  nix-instantiate --find-file nixpkgs
.PHONY: nix-info

###### Initial setup
setup:
	./scripts/setup
.PHONY: setup

###### Nixops regular tasks
TARGET ?=
NIXOPS_ARGS ?=
ifdef TARGET
  override NIXOPS_ARGS +=--include=$(TARGET)
endif
SSH_ARGS ?=

edit_env:
	pass edit Nixops/files/environment.nix || true

nixops:
	$(NIXOPS_PRIV) $(NIXOPS_ARGS)

ssh-eldiron:
	$(NIXOPS_PRIV) ssh eldiron -- $(SSH_ARGS)

ssh-dilion:
	$(NIXOPS_PRIV) ssh dilion -- $(SSH_ARGS)

ssh-backup-2:
	$(NIXOPS_PRIV) ssh backup-2 -- $(SSH_ARGS)

ssh-monitoring-1:
	$(NIXOPS_PRIV) ssh monitoring-1 -- $(SSH_ARGS)

info:
	$(NIXOPS_PRIV) list
	$(NIXOPS_PRIV) info

debug:
	$(NIXOPS_PRIV) deploy --build-only --show-trace $(NIXOPS_ARGS)

dry-run:
	$(NIXOPS_PRIV) deploy --dry-run $(NIXOPS_ARGS)

build:
	$(NIXOPS_PRIV) deploy --build-only $(NIXOPS_ARGS)

upload:
	$(NIXOPS_PRIV) deploy --copy-only $(NIXOPS_ARGS)

deploy:
	$(NIXOPS_PRIV) deploy $(NIXOPS_ARGS)

deploy-reboot:
	$(NIXOPS_PRIV) deploy --force-reboot $(NIXOPS_ARGS)

reboot:
	$(NIXOPS_PRIV) reboot --include=$(TARGET)
.PHONY: nixops ssh-eldiron info debug dry-run build upload deploy deploy-reboot reboot

###### Cleanup generations and garbage collection
profile := $$($(NIXOPS_PRIV) info | grep "^Nix profile: " | sed -e "s/^Nix profile: //")
GEN ?= "+3"

list-generations:
	nix-env -p $(profile) --list-generations
	$(NIXOPS_PRIV) ssh eldiron -- nix-env -p /nix/var/nix/profiles/system --list-generations
.PHONY: list-generations

delete-generations:
	nix-env -p $(profile) --delete-generations $(GEN)
	$(NIXOPS_PRIV) ssh eldiron -- nix-env -p /nix/var/nix/profiles/system --delete-generations $(GEN)
	$(NIXOPS_PRIV) ssh dilion -- nix-env -p /nix/var/nix/profiles/system --delete-generations $(GEN)
	$(NIXOPS_PRIV) ssh backup-2 -- nix-env -p /nix/var/nix/profiles/system --delete-generations $(GEN)
	$(NIXOPS_PRIV) ssh monitoring-1 -- nix-env -p /nix/var/nix/profiles/system --delete-generations $(GEN)
.PHONY: delete-generations

cleanup: delete-generations
	nix-store --gc
	$(NIXOPS_PRIV) ssh eldiron -- nix-store --gc
	$(NIXOPS_PRIV) ssh dilion -- nix-store --gc
	$(NIXOPS_PRIV) ssh backup-2 -- nix-store --gc
	$(NIXOPS_PRIV) ssh monitoring-1 -- nix-store --gc
.PHONY: cleanup

###### Pull environment and deployment from remote
pull_deployment:
	@if $(NIXOPS) info -d $(NIXOPS_DEPLOYMENT) 2>/dev/null >/dev/null ; then \
	  echo "This will remove your current deployment file and recreate it!. Continue? [y/N]" && \
	  read y && \
	  [ "$$y" = "y" -o "$$y" = "Y" ] && \
	  $(NIXOPS) delete --force -d $(NIXOPS_DEPLOYMENT); \
	fi
	pass show Nixops/Deployment | $(NIXOPS) import
	$(NIXOPS) modify -d $(NIXOPS_DEPLOYMENT) "$$(pwd)/default.nix"
.PHONY: pull_deployment

deployment_is_set:
	$(NIXOPS) info -d $(NIXOPS_DEPLOYMENT) 2>/dev/null >/dev/null
.PHONY: deployment_is_set

###### Push deployment information to password store
push_deployment:
	$(NIXOPS) export | pass insert -m Nixops/Deployment
.PHONY: push