aboutsummaryrefslogtreecommitdiff
path: root/overlays/nixops/hetzner_cloud.patch
blob: b75c1168d0dce51b73b130cf1cfa4384d3fc60bb (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
From 272e50d0b0262e49cdcaad42cdab57aad183d1c2 Mon Sep 17 00:00:00 2001
From: goodraven
 <employee-pseudonym-7f597def-7eeb-47f8-b10a-0724f2ba59a9@google.com>
Date: Thu, 3 May 2018 22:24:58 -0700
Subject: [PATCH] Initial commit adding support for hetzner cloud

This is based on the digital ocean backend. It also uses nixos-infect. I extended nixos-infect to be generic
for both backends.

Fixes #855
---
 examples/trivial-hetzner-cloud.nix |  12 ++
 nix/eval-machine-info.nix          |   1 +
 nix/hetzner-cloud.nix              |  56 +++++++
 nix/options.nix                    |   1 +
 nixops/backends/hetzner_cloud.py   | 230 +++++++++++++++++++++++++++++
 nixops/data/nixos-infect           |  77 +++++++---
 6 files changed, 354 insertions(+), 23 deletions(-)
 create mode 100644 examples/trivial-hetzner-cloud.nix
 create mode 100644 nix/hetzner-cloud.nix
 create mode 100644 nixops/backends/hetzner_cloud.py

diff --git a/examples/trivial-hetzner-cloud.nix b/examples/trivial-hetzner-cloud.nix
new file mode 100644
index 000000000..c61add6bb
--- /dev/null
+++ b/examples/trivial-hetzner-cloud.nix
@@ -0,0 +1,12 @@
+{
+  resources.sshKeyPairs.ssh-key = {};
+
+  machine = { config, pkgs, ... }: {
+    services.openssh.enable = true;
+
+    deployment.targetEnv = "hetznerCloud";
+    deployment.hetznerCloud.serverType = "cx11";
+
+    networking.firewall.allowedTCPPorts = [ 22 ];
+  };
+}
diff --git a/nix/eval-machine-info.nix b/nix/eval-machine-info.nix
index 2884b4b47..6a7205786 100644
--- a/nix/eval-machine-info.nix
+++ b/nix/eval-machine-info.nix
@@ -309,6 +309,7 @@ rec {
           digitalOcean = optionalAttrs (v.config.deployment.targetEnv == "digitalOcean") v.config.deployment.digitalOcean;
           gce = optionalAttrs (v.config.deployment.targetEnv == "gce") v.config.deployment.gce;
           hetzner = optionalAttrs (v.config.deployment.targetEnv == "hetzner") v.config.deployment.hetzner;
+          hetznerCloud = optionalAttrs (v.config.deployment.targetEnv == "hetznerCloud") v.config.deployment.hetznerCloud;
           container = optionalAttrs (v.config.deployment.targetEnv == "container") v.config.deployment.container;
           route53 = v.config.deployment.route53;
           virtualbox =
diff --git a/nix/hetzner-cloud.nix b/nix/hetzner-cloud.nix
new file mode 100644
index 000000000..21d148c1a
--- /dev/null
+++ b/nix/hetzner-cloud.nix
@@ -0,0 +1,56 @@
+{ config, pkgs, lib, utils, ... }:
+
+with utils;
+with lib;
+with import ./lib.nix lib;
+
+let
+  cfg = config.deployment.hetznerCloud;
+in
+{
+  ###### interface
+  options = {
+
+    deployment.hetznerCloud.authToken = mkOption {
+      default = "";
+      example = "8b2f4e96af3997853bfd4cd8998958eab871d9614e35d63fab45a5ddf981c4da";
+      type = types.str;
+      description = ''
+        The API auth token. We're checking the environment for
+        <envar>HETZNER_CLOUD_AUTH_TOKEN</envar> first and if that is
+        not set we try this auth token.
+      '';
+    };
+
+    deployment.hetznerCloud.datacenter = mkOption {
+      example = "fsn1-dc8";
+      default = null;
+      type = types.nullOr types.str;
+      description = ''
+        The datacenter.
+      '';
+    };
+
+    deployment.hetznerCloud.location = mkOption {
+      example = "fsn1";
+      default = null;
+      type = types.nullOr types.str;
+      description = ''
+        The location.
+      '';
+    };
+
+    deployment.hetznerCloud.serverType = mkOption {
+      example = "cx11";
+      type = types.str;
+      description = ''
+        Name or id of server types.
+      '';
+    };
+  };
+
+  config = mkIf (config.deployment.targetEnv == "hetznerCloud") {
+    nixpkgs.system = mkOverride 900 "x86_64-linux";
+    services.openssh.enable = true;
+  };
+}
diff --git a/nix/options.nix b/nix/options.nix
index 0866c3ab8..db021f74d 100644
--- a/nix/options.nix
+++ b/nix/options.nix
@@ -22,6 +22,7 @@ in
       ./keys.nix
       ./gce.nix
       ./hetzner.nix
+      ./hetzner-cloud.nix
       ./container.nix
       ./libvirtd.nix
     ];
diff --git a/nixops/backends/hetzner_cloud.py b/nixops/backends/hetzner_cloud.py
new file mode 100644
index 000000000..a2cb176b9
--- /dev/null
+++ b/nixops/backends/hetzner_cloud.py
@@ -0,0 +1,230 @@
+# -*- coding: utf-8 -*-
+"""
+A backend for hetzner cloud.
+
+This backend uses nixos-infect (which uses nixos LUSTRATE) to infect a
+hetzner cloud instance. The setup requires two reboots, one for
+the infect itself, another after we pushed the nixos image.
+"""
+import os
+import os.path
+import time
+import socket
+
+import requests
+
+import nixops.resources
+from nixops.backends import MachineDefinition, MachineState
+from nixops.nix_expr import Function, RawValue
+import nixops.util
+import nixops.known_hosts
+
+infect_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'nixos-infect'))
+
+API_HOST = 'api.hetzner.cloud'
+
+class ApiError(Exception):
+    pass
+
+class ApiNotFoundError(ApiError):
+    pass
+
+class HetznerCloudDefinition(MachineDefinition):
+    @classmethod
+    def get_type(cls):
+        return "hetznerCloud"
+
+    def __init__(self, xml, config):
+        MachineDefinition.__init__(self, xml, config)
+        self.auth_token = config["hetznerCloud"]["authToken"]
+        self.location = config["hetznerCloud"]["location"]
+        self.datacenter = config["hetznerCloud"]["datacenter"]
+        self.server_type = config["hetznerCloud"]["serverType"]
+
+    def show_type(self):
+        return "{0} [{1}]".format(self.get_type(), self.location or self.datacenter or 'any location')
+
+
+class HetznerCloudState(MachineState):
+    @classmethod
+    def get_type(cls):
+        return "hetznerCloud"
+
+    state = nixops.util.attr_property("state", MachineState.MISSING, int)  # override
+    public_ipv4 = nixops.util.attr_property("publicIpv4", None)
+    public_ipv6 = nixops.util.attr_property("publicIpv6", None)
+    location = nixops.util.attr_property("hetznerCloud.location", None)
+    datacenter = nixops.util.attr_property("hetznerCloud.datacenter", None)
+    server_type = nixops.util.attr_property("hetznerCloud.serverType", None)
+    auth_token = nixops.util.attr_property("hetznerCloud.authToken", None)
+    server_id = nixops.util.attr_property("hetznerCloud.serverId", None, int)
+
+    def __init__(self, depl, name, id):
+        MachineState.__init__(self, depl, name, id)
+        self.name = name
+
+    def get_ssh_name(self):
+        return self.public_ipv4
+
+    def get_ssh_flags(self, *args, **kwargs):
+        super_flags = super(HetznerCloudState, self).get_ssh_flags(*args, **kwargs)
+        return super_flags + [
+            '-o', 'UserKnownHostsFile=/dev/null',
+            '-o', 'StrictHostKeyChecking=no',
+            '-i', self.get_ssh_private_key_file(),
+        ]
+
+    def get_physical_spec(self):
+        return Function("{ ... }", {
+            'imports': [ RawValue('<nixpkgs/nixos/modules/profiles/qemu-guest.nix>') ],
+            ('boot', 'loader', 'grub', 'device'): 'nodev',
+            ('fileSystems', '/'): { 'device': '/dev/sda1', 'fsType': 'ext4'},
+            ('users', 'extraUsers', 'root', 'openssh', 'authorizedKeys', 'keys'): [self.depl.active_resources.get('ssh-key').public_key],
+        })
+
+    def get_ssh_private_key_file(self):
+        return self.write_ssh_private_key(self.depl.active_resources.get('ssh-key').private_key)
+
+    def create_after(self, resources, defn):
+        # make sure the ssh key exists before we do anything else
+        return {
+            r for r in resources if
+            isinstance(r, nixops.resources.ssh_keypair.SSHKeyPairState)
+        }
+
+    def get_auth_token(self):
+        return os.environ.get('HETZNER_CLOUD_AUTH_TOKEN', self.auth_token)
+
+    def _api(self, path, method=None, data=None, json=True):
+        """Basic wrapper around requests that handles auth and serialization."""
+        assert path[0] == '/'
+        url = 'https://%s%s' % (API_HOST, path)
+        token = self.get_auth_token()
+        if not token:
+            raise Exception('No hetzner cloud auth token set')
+        headers = {
+            'Authorization': 'Bearer '+self.get_auth_token(),
+        }
+        res = requests.request(
+            method=method,
+            url=url,
+            json=data,
+            headers=headers)
+
+        if res.status_code == 404:
+            raise ApiNotFoundError('Not Found: %r' % path)
+        elif not res.ok:
+            raise ApiError('Response for %s %s has status code %d: %s' % (method, path, res.status_code, res.content))
+        if not json:
+            return
+        try:
+            res_data = res.json()
+        except ValueError as e:
+            raise ApiError('Response for %s %s has invalid JSON (%s): %r' % (method, path, e, res.content))
+        return res_data
+
+
+    def destroy(self, wipe=False):
+        if not self.server_id:
+            self.log('server {} was never made'.format(self.name))
+            return
+        self.log('destroying server {} with id {}'.format(self.name, self.server_id))
+        try:
+            res = self._api('/v1/servers/%s' % (self.server_id), method='DELETE')
+        except ApiNotFoundError:
+            self.log("server not found - assuming it's been destroyed already")
+
+        self.public_ipv4 = None
+        self.server_id = None
+
+        return True
+
+    def _create_ssh_key(self, public_key):
+        """Create or get an ssh key and return an id."""
+        public_key = public_key.strip()
+        res = self._api('/v1/ssh_keys', method='GET')
+        name = 'nixops-%s-%s' % (self.depl.uuid, self.name)
+        deletes = []
+        for key in res['ssh_keys']:
+            if key['public_key'].strip() == public_key:
+                return key['id']
+            if key['name'] == name:
+                deletes.append(key['id'])
+        for d in deletes:
+            # This reply is empty, so don't decode json.
+            self._api('/v1/ssh_keys/%d' % d, method='DELETE', json=False)
+        res = self._api('/v1/ssh_keys', method='POST', data={
+            'name': name,
+            'public_key': public_key,
+        })
+        return res['ssh_key']['id']
+
+    def create(self, defn, check, allow_reboot, allow_recreate):
+        ssh_key = self.depl.active_resources.get('ssh-key')
+        if ssh_key is None:
+            raise Exception('Please specify a ssh-key resource (resources.sshKeyPairs.ssh-key = {}).')
+
+        self.set_common_state(defn)
+
+        if self.server_id is not None:
+            return
+
+        ssh_key_id = self._create_ssh_key(ssh_key.public_key)
+
+        req = {
+                'name': self.name,
+                'server_type': defn.server_type,
+                'start_after_create': True,
+                'image': 'debian-9',
+                'ssh_keys': [
+                    ssh_key_id,
+                ],
+        }
+
+        if defn.datacenter:
+            req['datacenter'] = defn.datacenter
+        elif defn.location:
+            req['location'] = defn.location
+
+        self.log_start("creating server ...")
+        create_res = self._api('/v1/servers', method='POST', data=req)
+        self.server_id = create_res['server']['id']
+        self.public_ipv4 = create_res['server']['public_net']['ipv4']['ip']
+        self.public_ipv6 = create_res['server']['public_net']['ipv6']['ip']
+        self.datacenter = create_res['server']['datacenter']['name']
+        self.location = create_res['server']['datacenter']['location']['name']
+
+        action = create_res['action']
+        action_path = '/v1/servers/%d/actions/%d' % (self.server_id, action['id'])
+
+        while action['status'] == 'running':
+            time.sleep(1)
+            res = self._api(action_path, method='GET')
+            action = res['action']
+
+        if action['status'] != 'success':
+            raise Exception('unexpected status: %s' % action['status'])
+
+        self.log_end("{}".format(self.public_ipv4))
+
+        self.wait_for_ssh()
+        self.log_start("running nixos-infect")
+        self.run_command('bash </dev/stdin 2>&1', stdin=open(infect_path))
+        self.reboot_sync()
+
+    def reboot(self, hard=False):
+        if hard:
+            self.log("sending hard reset to server...")
+            res = self._api('/v1/servers/%d/actions/reset' % self.server_id, method='POST')
+            action = res['action']
+            action_path = '/v1/servers/%d/actions/%d' % (self.server_id, action['id'])
+            while action['status'] == 'running':
+                time.sleep(1)
+                res = self._api(action_path, method='GET')
+                action = res['action']
+            if action['status'] != 'success':
+                raise Exception('unexpected status: %s' % action['status'])
+            self.wait_for_ssh()
+            self.state = self.STARTING
+        else:
+            MachineState.reboot(self, hard=hard)
diff --git a/nixops/data/nixos-infect b/nixops/data/nixos-infect
index 66634357b..437a2ec61 100644
--- a/nixops/data/nixos-infect
+++ b/nixops/data/nixos-infect
@@ -68,26 +68,49 @@ makeConf() {
 }
 EOF
   # (nixos-generate-config will add qemu-user and bind-mounts, so avoid)
+  local disk
+  if [ -e /dev/sda ]; then
+    disk=/dev/sda
+  else
+    disk=/dev/vda
+  fi
   cat > /etc/nixos/hardware-configuration.nix << EOF
 { ... }:
 {
   imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
-  boot.loader.grub.device = "/dev/vda";
-  fileSystems."/" = { device = "/dev/vda1"; fsType = "ext4"; };
+  boot.loader.grub.device = "${disk}";
+  fileSystems."/" = { device = "${disk}1"; fsType = "ext4"; };
 }
 EOF
 
   local IFS=$'\n'
-  ens3_ip4s=($(ip address show dev eth0 | grep 'inet '          | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
-  ens3_ip6s=($(ip address show dev eth0 | grep 'inet6 .*global' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
-  ens4_ip4s=($(ip address show dev eth1 | grep 'inet '          | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
-  ens4_ip6s=($(ip address show dev eth1 | grep 'inet6 .*global' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
-  gateway=($(ip route show dev eth0 | grep default | sed -r 's|default via ([0-9.]+).*|\1|'))
-  gateway6=($(ip -6 route show dev eth0 | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|'))
-  ether0=($(ip address show dev eth0 | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|'))
-  ether1=($(ip address show dev eth1 | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|'))
+  gateway=($(ip route show | grep default | sed -r 's|default via ([0-9.]+).*|\1|'))
+  gateway6=($(ip -6 route show | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|'))
+  interfaces=($(ip link | awk -F ': ' '/^[0-9]*: / {if ($2 != "lo") {print $2}}'))
   nameservers=($(grep ^nameserver /etc/resolv.conf | cut -f2 -d' '))
 
+  # Predict the predictable name for each interface since that is enabled in
+  # the nixos system.
+  declare -A predictable_names
+  for interface in ${interfaces[@]}; do
+    # udevadm prints out the candidate names which will be selected if
+    # available in this order.
+    local name=$(udevadm info /sys/class/net/$interface | awk -F = '
+    /^E: ID_NET_NAME_FROM_DATABASE=/ {arr[1]=$2}
+    /^E: ID_NET_NAME_ONBOARD=/ {arr[2]=$2}
+    /^E: ID_NET_NAME_SLOT=/ {arr[3]=$2}
+    /^E: ID_NET_NAME_PATH=/ {arr[4]=$2}
+    /^E: ID_NET_NAME_MAC=/ {arr[5]=$2}
+    END {for (i=1;i<6;i++) {if (length(arr[i]) > 0) { print arr[i]; break}}}')
+    if [ -z "$name" ]; then
+      echo Could not determine predictable name for interface $interface
+    fi
+    predictable_names[$interface]=$name
+  done
+
+  # Take a gamble on the first interface being able to reach the gateway.
+  local default_interface=${predictable_names[${interfaces[0]}]}
+
   cat > /etc/nixos/networking.nix << EOF
 { ... }: {
   # This file was populated at runtime with the networking
@@ -96,25 +119,27 @@ EOF
     nameservers = [$(for a in ${nameservers[@]}; do echo -n "
       \"$a\""; done)
     ];
-    defaultGateway = "${gateway}";
-    defaultGateway6 = "${gateway6}";
+    defaultGateway = {address = "${gateway}"; interface = "${default_interface}";};
+    defaultGateway6 = {address = "${gateway6}"; interface = "${default_interface}";};
     interfaces = {
-      ens3 = {
-        ip4 = [$(for a in ${ens3_ip4s[@]}; do echo -n "
-          $a"; done)
-        ];
-        ip6 = [$(for a in ${ens3_ip6s[@]}; do echo -n "
-          $a"; done)
-        ];
-      };
-      ens4 = {
-        ip4 = [$(for a in ${ens4_ip4s[@]}; do echo -n "
+EOF
+
+  for interface in ${interfaces[@]}; do
+    ip4s=($(ip address show dev $interface | grep 'inet '          | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
+    ip6s=($(ip address show dev $interface | grep 'inet6 .*global' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|'))
+    cat >> /etc/nixos/networking.nix << EOF
+      ${predictable_names[$interface]} = {
+        ip4 = [$(for a in ${ip4s[@]}; do echo -n "
           $a"; done)
         ];
-        ip6 = [$(for a in ${ens4_ip6s[@]}; do echo -n "
+        ip6 = [$(for a in ${ip6s[@]}; do echo -n "
           $a"; done)
         ];
       };
+EOF
+  done
+
+  cat >> /etc/nixos/networking.nix << EOF
     };
   };
 }
@@ -154,6 +179,12 @@ export HOME="/root"
 groupadd -r nixbld -g 30000
 seq 1 10 | xargs -I{} useradd -c "Nix build user {}" -d /var/empty -g nixbld -G nixbld -M -N -r -s `which nologin` nixbld{}
 
+if ! which curl >/dev/null 2>/dev/null; then
+  if which apt-get >/dev/null 2>/dev/null; then
+    apt-get update && apt-get install -y curl
+  fi
+fi
+
 curl https://nixos.org/nix/install | sh
 
 source ~/.nix-profile/etc/profile.d/nix.sh