blob: d0e6ae3c15db6431a68c266bc9c0a9991003c808 [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
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020055 # Stopgap measure to import //cluster/nix machine definitions into new
Serge Bazanskib3c67702021-09-10 22:27:24 +000056 # //ops/ infrastructure.
57 #
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020058 # TODO(q3k): inject defs-cluster-k0.nix / defs-machines.nix content via
59 # nixos options instead of having module definitions loading it themselves,
60 # deduplicate list of machines below with defs-machines.nix somehow.
Serge Bazanskib3c67702021-09-10 22:27:24 +000061 clusterMachineConfig = name: [({ config, pkgs, ...}: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020062 # The hostname is used by //cluster/nix machinery to load the appropriate
63 # config from defs-machines into defs-cluster-k0.
64 networking.hostName = name;
65 imports = [
66 ../cluster/nix/modules/base.nix
67 ../cluster/nix/modules/kubernetes.nix
Serge Bazanski9848e7e2021-09-10 22:30:56 +000068 ../cluster/nix/modules/ceph.nix
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020069 ];
Serge Bazanskib3c67702021-09-10 22:27:24 +000070 })];
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020071
Serge Bazanskia16af2d2021-10-16 19:14:05 +000072 # mkMachine builds NixOS modules into a NixOS derivation.
73 # It:
74 # 1) injects passthru.hscloud.provision which deploys that configuration
75 # over SSH to a production machine.
76 # 2) injects 'workspace' as a nixos module argument which points to the root
77 # of the hscloud readTree object. It will contain whatever nixpkgs
78 # checkout this file has been invoked with, ie. will not be 'mixed in'
79 # with the pkgs argument.
Serge Bazanskia0332a72021-03-17 22:12:43 +010080 mkMachine = pkgs: paths: pkgs.nixos ({ config, pkgs, ... }: {
Serge Bazanski6abe4fa2020-10-03 00:18:34 +020081 imports = paths;
Serge Bazanskib3c67702021-09-10 22:27:24 +000082
83 config = let
84 name = config.networking.hostName;
85 domain = if (config.networking ? domain) && config.networking.domain != null then config.networking.domain else "hswaw.net";
86 fqdn = name + "." + domain;
87 toplevel = config.system.build.toplevel;
88
89 runProvision = ''
90 #!/bin/sh
91 set -eu
92 remote=root@${fqdn}
93 echo "Configuration for ${fqdn} is ${toplevel}"
94 nix copy -s --to ssh://$remote ${toplevel}
95
96 running="$(ssh $remote readlink -f /nix/var/nix/profiles/system)"
97 if [ "$running" == "${toplevel}" ]; then
98 echo "${fqdn} already running ${toplevel}."
99 else
100 echo "/etc/systemd/system diff:"
101 ssh $remote diff -ur /var/run/current-system/etc/systemd/system ${toplevel}/etc/systemd/system || true
102 echo ""
103 echo ""
104 echo "dry-activate diff:"
105 ssh $remote ${toplevel}/bin/switch-to-configuration dry-activate
106 read -p "Do you want to switch to this configuration? " -n 1 -r
107 echo
108 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
109 exit 1
110 fi
111
112 echo -ne "\n\nswitch-to-configuration test...\n"
113 ssh $remote ${toplevel}/bin/switch-to-configuration test
114 fi
115
116 echo -ne "\n\n"
117 read -p "Do you want to set this configuration as boot? " -n 1 -r
118 echo
119 if ! [[ $REPLY =~ ^[Yy]$ ]]; then
120 exit 1
121 fi
122
123 echo -ne "\n\nsetting system profile...\n"
124 ssh $remote nix-env -p /nix/var/nix/profiles/system --set ${toplevel}
125
126 echo -ne "\n\nswitch-to-configuration boot...\n"
127 ssh $remote ${toplevel}/bin/switch-to-configuration boot
128 '';
129 in {
130 passthru.hscloud.provision = pkgs.writeScript "provision-${fqdn}" runProvision;
Serge Bazanskia16af2d2021-10-16 19:14:05 +0000131
132 # TODO(q3k): this should be named hscloud, but that seems to not work. Debug and rename.
133 _module.args.workspace = hscloud;
Serge Bazanskib3c67702021-09-10 22:27:24 +0000134 };
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200135 });
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200136in {
Serge Bazanskib3c67702021-09-10 22:27:24 +0000137 "bc01n01.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "bc01n01");
138 "bc01n02.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "bc01n02");
139 "dcr01s22.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "dcr01s22");
140 "dcr01s24.hswaw.net" = mkMachine nixpkgsCluster (clusterMachineConfig "dcr01s24");
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200141
Serge Bazanskib3c67702021-09-10 22:27:24 +0000142 "edge01.waw.bgp.wtf" = mkMachine nixpkgsBgpwtf [
Serge Bazanski6abe4fa2020-10-03 00:18:34 +0200143 ../bgpwtf/machines/edge01.waw.bgp.wtf.nix
144 ../bgpwtf/machines/edge01.waw.bgp.wtf-hardware.nix
145 ];
146}