blob: 0a97bcd7a4357d54893ae80f5d01cf3cb025a79b [file] [log] [blame]
Serge Bazanski6abe4fa2020-10-03 00:18:34 +02001# Top-level file aggregating all machines managed from hscloud.
2#
3# This allows to have a common attrset of machines that can be deployed
4# in the same way.
5#
Serge Bazanskib3c67702021-09-10 22:27:24 +00006# For information about building/deploying machines see //ops/README.md.
Serge Bazanski6abe4fa2020-10-03 00:18:34 +02007
8{ hscloud, pkgs, ... }:
9
10let
Serge Bazanskib3c67702021-09-10 22:27:24 +000011 # nixpkgs for cluster machines (.hswaw.net). Currently pinned to an old
12 # nixpkgs because NixOS modules for kubernetes changed enough that it's not
13 # super easy to use them as is.
14 #
15 # TODO(q3k): fix this: use an old nixpkgs for Kube modules while using
16 # hscloud nixpkgs for everything else.
17 nixpkgsCluster = import (pkgs.fetchFromGitHub {
18 owner = "nixos";
19 repo = "nixpkgs-channels";
20 rev = "44ad80ab1036c5cc83ada4bfa451dac9939f2a10";
21 sha256 = "1b61nzvy0d46cspy07szkc0rggacxiqg9v1py27pkqpj7rvawfsk";
Serge Bazanski9848e7e2021-09-10 22:30:56 +000022 }) {
23 overlays = [
24 (self: super: rec {
25 # Use a newer version of Ceph (16, Pacific, EOL 2023-06-01) than in
26 # this nixpkgs (15, Octopus, EOL 2022-06-01).
27 #
28 # This is to:
29 # 1. Fix a bug in which ceph-volume lvm create fails due to a rocksdb
30 # mismatch (https://tracker.ceph.com/issues/49815)
31 # 2. At the time of deployment not start out with an ancient version
32 # of Ceph.
33 #
34 # Once we unpin nixpkgsCluster past a version that contains this Ceph,
35 # this can be unoverlayed.
36 inherit (super.callPackages ./ceph {
37 boost = super.boost17x.override { enablePython = true; python = super.python3; };
38 lua = super.lua5_4;
39 }) ceph ceph-client;
40 ceph-lib = ceph.lib;
41 })
42 ];
43 };
Serge Bazanskib3c67702021-09-10 22:27:24 +000044
45 # edge01 still lives on an old nixpkgs checkout.
46 #
47 # TODO(b/3): unpin and deploy.
48 nixpkgsBgpwtf = import (pkgs.fetchFromGitHub {
49 owner = "nixos";
50 repo = "nixpkgs-channels";
51 rev = "c59ea8b8a0e7f927e7291c14ea6cd1bd3a16ff38";
52 sha256 = "1ak7jqx94fjhc68xh1lh35kh3w3ndbadprrb762qgvcfb8351x8v";
53 }) {};
54
Piotr Dobrowolskia01905a2021-10-16 18:22:46 +020055 # customs.hackerspace.pl migration temporary checkout
56 nixpkgsCustoms = import (pkgs.fetchFromGitHub {
57 owner = "nixos";
58 repo = "nixpkgs";
59 rev = "d12178b1c4a6ef1232c8c677573ba9db204e66ff";
60 sha256 = "0p7df7yzi35kblxr5ks0rxxp9cfh269g88xpj60sdhdjvfnn6cp7";
61 }) {};
62
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020063 # Stopgap measure to import //cluster/nix machine definitions into new
Serge Bazanskib3c67702021-09-10 22:27:24 +000064 # //ops/ infrastructure.
65 #
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020066 # TODO(q3k): inject defs-cluster-k0.nix / defs-machines.nix content via
67 # nixos options instead of having module definitions loading it themselves,
68 # deduplicate list of machines below with defs-machines.nix somehow.
Serge Bazanskib3c67702021-09-10 22:27:24 +000069 clusterMachineConfig = name: [({ config, pkgs, ...}: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020070 # The hostname is used by //cluster/nix machinery to load the appropriate
71 # config from defs-machines into defs-cluster-k0.
72 networking.hostName = name;
73 imports = [
74 ../cluster/nix/modules/base.nix
75 ../cluster/nix/modules/kubernetes.nix
Serge Bazanski9848e7e2021-09-10 22:30:56 +000076 ../cluster/nix/modules/ceph.nix
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020077 ];
Serge Bazanskib3c67702021-09-10 22:27:24 +000078 })];
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020079
Serge Bazanskia16af2d2021-10-16 19:14:05 +000080 # mkMachine builds NixOS modules into a NixOS derivation.
81 # It:
82 # 1) injects passthru.hscloud.provision which deploys that configuration
83 # over SSH to a production machine.
84 # 2) injects 'workspace' as a nixos module argument which points to the root
85 # of the hscloud readTree object. It will contain whatever nixpkgs
86 # checkout this file has been invoked with, ie. will not be 'mixed in'
87 # with the pkgs argument.
Serge Bazanskia0332a72021-03-17 22:12:43 +010088 mkMachine = pkgs: paths: pkgs.nixos ({ config, pkgs, ... }: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020089 imports = paths;
Serge Bazanskib3c67702021-09-10 22:27:24 +000090
91 config = let
92 name = config.networking.hostName;
93 domain = if (config.networking ? domain) && config.networking.domain != null then config.networking.domain else "hswaw.net";
94 fqdn = name + "." + domain;
95 toplevel = config.system.build.toplevel;
96
97 runProvision = ''
98 #!/bin/sh
99 set -eu
100 remote=root@${fqdn}
101 echo "Configuration for ${fqdn} is ${toplevel}"
102 nix copy -s --to ssh://$remote ${toplevel}
103
104 running="$(ssh $remote readlink -f /nix/var/nix/profiles/system)"
105 if [ "$running" == "${toplevel}" ]; then
106 echo "${fqdn} already running ${toplevel}."
107 else
108 echo "/etc/systemd/system diff:"
109 ssh $remote diff -ur /var/run/current-system/etc/systemd/system ${toplevel}/etc/systemd/system || true
110 echo ""
111 echo ""
112 echo "dry-activate diff:"
113 ssh $remote ${toplevel}/bin/switch-to-configuration dry-activate
114 read -p "Do you want to switch to this configuration? " -n 1 -r
115 echo
116 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
117 exit 1
118 fi
119
120 echo -ne "\n\nswitch-to-configuration test...\n"
121 ssh $remote ${toplevel}/bin/switch-to-configuration test
122 fi
123
124 echo -ne "\n\n"
125 read -p "Do you want to set this configuration as boot? " -n 1 -r
126 echo
127 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
128 exit 1
129 fi
130
131 echo -ne "\n\nsetting system profile...\n"
132 ssh $remote nix-env -p /nix/var/nix/profiles/system --set ${toplevel}
133
134 echo -ne "\n\nswitch-to-configuration boot...\n"
135 ssh $remote ${toplevel}/bin/switch-to-configuration boot
136 '';
137 in {
138 passthru.hscloud.provision = pkgs.writeScript "provision-${fqdn}" runProvision;
Serge Bazanskia16af2d2021-10-16 19:14:05 +0000139
140 # TODO(q3k): this should be named hscloud, but that seems to not work. Debug and rename.
141 _module.args.workspace = hscloud;
Serge Bazanskib3c67702021-09-10 22:27:24 +0000142 };
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200143 });
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200144in {
Serge Bazanskib3c67702021-09-10 22:27:24 +0000145 "bc01n01.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "bc01n01");
146 "bc01n02.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "bc01n02");
147 "dcr01s22.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "dcr01s22");
148 "dcr01s24.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "dcr01s24");
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200149
Serge Bazanskib3c67702021-09-10 22:27:24 +0000150 "edge01.waw.bgp.wtf" = mkMachine nixpkgsBgpwtf [
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200151 ../bgpwtf/machines/edge01.waw.bgp.wtf.nix
152 ../bgpwtf/machines/edge01.waw.bgp.wtf-hardware.nix
153 ];
Piotr Dobrowolskia01905a2021-10-16 18:22:46 +0200154
155 "customs.hackerspace.pl" = mkMachine nixpkgsCustoms [
156 ../hswaw/machines/customs.hackerspace.pl/configuration.nix
157 ];
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200158}