From 2858b35100c8cc350b8f162227fbb767c8c2173e Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Mon, 28 Jan 2019 11:59:18 +0100 Subject: [PATCH 1/6] nova-image: use wget instead of cloud-init (via EC2 API) The Openstack metadata service exposes the EC2 API. We use the existing `ec2.nix` module to configure the hostname and ssh keys of an Openstack Instance. A test checks the ssh server is well configured. This is mainly to reduce the size of the image (700MB). Also, declarative features provided by cloud-init are not really useful since we would prefer to use our `configuration.nix` file instead. --- nixos/modules/virtualisation/nova-config.nix | 62 +++++++++++--------- nixos/tests/common/ec2.nix | 61 +++++++++++++++++++ nixos/tests/ec2.nix | 60 ++----------------- nixos/tests/nova-image.nix | 60 +++++++++++++++++++ 4 files changed, 160 insertions(+), 83 deletions(-) create mode 100644 nixos/tests/common/ec2.nix create mode 100644 nixos/tests/nova-image.nix diff --git a/nixos/modules/virtualisation/nova-config.nix b/nixos/modules/virtualisation/nova-config.nix index cecf2a3f144c..fef20d498e97 100644 --- a/nixos/modules/virtualisation/nova-config.nix +++ b/nixos/modules/virtualisation/nova-config.nix @@ -1,4 +1,4 @@ -{ lib, ... }: +{ pkgs, lib, ... }: with lib; @@ -6,6 +6,8 @@ with lib; imports = [ ../profiles/qemu-guest.nix ../profiles/headless.nix + # The Openstack Metadata service exposes data on an EC2 API also. + ./ec2-data.nix ]; config = { @@ -26,35 +28,41 @@ with lib; passwordAuthentication = mkDefault false; }; - services.cloud-init.enable = true; + systemd.services.nova-init = { + path = [ pkgs.wget ]; + description = "Fetch Metadata on startup"; + wantedBy = [ "multi-user.target" ]; + before = [ "apply-ec2-data.service" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + script = + '' + metaDir=/etc/ec2-metadata + mkdir -m 0755 -p "$metaDir" - # Put /tmp and /var on /ephemeral0, which has a lot more space. - # Unfortunately we can't do this with the `fileSystems' option - # because it has no support for creating the source of a bind - # mount. Also, "move" /nix to /ephemeral0 by layering a unionfs-fuse - # mount on top of it so we have a lot more space for Nix operations. + echo "getting Openstack instance metadata (via EC2 API)..." + if ! [ -e "$metaDir/ami-manifest-path" ]; then + wget --retry-connrefused -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path + fi - /* - boot.initrd.postMountCommands = - '' - mkdir -m 1777 -p $targetRoot/ephemeral0/tmp - mkdir -m 1777 -p $targetRoot/tmp - mount --bind $targetRoot/ephemeral0/tmp $targetRoot/tmp + if ! [ -e "$metaDir/user-data" ]; then + wget --retry-connrefused -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data" + fi - mkdir -m 755 -p $targetRoot/ephemeral0/var - mkdir -m 755 -p $targetRoot/var - mount --bind $targetRoot/ephemeral0/var $targetRoot/var + if ! [ -e "$metaDir/hostname" ]; then + wget --retry-connrefused -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname + fi - mkdir -p /unionfs-chroot/ro-nix - mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix - - mkdir -p /unionfs-chroot/rw-nix - mkdir -m 755 -p $targetRoot/ephemeral0/nix - mount --rbind $targetRoot/ephemeral0/nix /unionfs-chroot/rw-nix - unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix - ''; - - boot.initrd.supportedFilesystems = [ "unionfs-fuse" ]; - */ + if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then + wget --retry-connrefused -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key + fi + ''; + restartIfChanged = false; + unitConfig.X-StopOnRemoval = false; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + }; }; } diff --git a/nixos/tests/common/ec2.nix b/nixos/tests/common/ec2.nix new file mode 100644 index 000000000000..99a39473b617 --- /dev/null +++ b/nixos/tests/common/ec2.nix @@ -0,0 +1,61 @@ +{ pkgs, makeTest }: + +with pkgs.lib; + +{ + makeEc2Test = { name, image, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }: + let + metaData = pkgs.stdenv.mkDerivation { + name = "metadata"; + buildCommand = '' + mkdir -p $out/1.0/meta-data + ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data + echo "${hostname}" > $out/1.0/meta-data/hostname + echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path + '' + optionalString (sshPublicKey != null) '' + mkdir -p $out/1.0/meta-data/public-keys/0 + ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key + ''; + }; + in makeTest { + name = "ec2-" + name; + nodes = {}; + testScript = + '' + my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine"; + mkdir $imageDir, 0700; + my $diskImage = "$imageDir/machine.qcow2"; + system("qemu-img create -f qcow2 -o backing_file=${image}/nixos.qcow2 $diskImage") == 0 or die; + system("qemu-img resize $diskImage 10G") == 0 or die; + + # Note: we use net=169.0.0.0/8 rather than + # net=169.254.0.0/16 to prevent dhcpcd from getting horribly + # confused. (It would get a DHCP lease in the 169.254.* + # range, which it would then configure and prompty delete + # again when it deletes link-local addresses.) Ideally we'd + # turn off the DHCP server, but qemu does not have an option + # to do that. + my $startCommand = "qemu-kvm -m 768"; + $startCommand .= " -device virtio-net-pci,netdev=vlan0"; + $startCommand .= " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"; + $startCommand .= " -drive file=$diskImage,if=virtio,werror=report"; + $startCommand .= " \$QEMU_OPTS"; + + my $machine = createMachine({ startCommand => $startCommand }); + + ${script} + ''; + }; + + snakeOilPrivateKey = '' + -----BEGIN OPENSSH PRIVATE KEY----- + b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW + QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye + EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q + AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4 + FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE= + -----END OPENSSH PRIVATE KEY----- + ''; + + snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key"; +} diff --git a/nixos/tests/ec2.nix b/nixos/tests/ec2.nix index ed6bf7da988c..db959a63e40f 100644 --- a/nixos/tests/ec2.nix +++ b/nixos/tests/ec2.nix @@ -6,6 +6,8 @@ with import ../lib/testing.nix { inherit system pkgs; }; with pkgs.lib; +with import common/ec2.nix { inherit makeTest pkgs; }; + let image = (import ../lib/eval-config.nix { @@ -39,65 +41,10 @@ let ]; }).config.system.build.amazonImage; - makeEc2Test = { name, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }: - let - metaData = pkgs.stdenv.mkDerivation { - name = "metadata"; - buildCommand = '' - mkdir -p $out/1.0/meta-data - ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data - echo "${hostname}" > $out/1.0/meta-data/hostname - echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path - '' + optionalString (sshPublicKey != null) '' - mkdir -p $out/1.0/meta-data/public-keys/0 - ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key - ''; - }; - in makeTest { - name = "ec2-" + name; - nodes = {}; - testScript = - '' - my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine"; - mkdir $imageDir, 0700; - my $diskImage = "$imageDir/machine.qcow2"; - system("qemu-img create -f qcow2 -o backing_file=${image}/nixos.qcow2 $diskImage") == 0 or die; - system("qemu-img resize $diskImage 10G") == 0 or die; - - # Note: we use net=169.0.0.0/8 rather than - # net=169.254.0.0/16 to prevent dhcpcd from getting horribly - # confused. (It would get a DHCP lease in the 169.254.* - # range, which it would then configure and prompty delete - # again when it deletes link-local addresses.) Ideally we'd - # turn off the DHCP server, but qemu does not have an option - # to do that. - my $startCommand = "qemu-kvm -m 768"; - $startCommand .= " -device virtio-net-pci,netdev=vlan0"; - $startCommand .= " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"; - $startCommand .= " -drive file=$diskImage,if=virtio,werror=report"; - $startCommand .= " \$QEMU_OPTS"; - - my $machine = createMachine({ startCommand => $startCommand }); - - ${script} - ''; - }; - - snakeOilPrivateKey = '' - -----BEGIN OPENSSH PRIVATE KEY----- - b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW - QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye - EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q - AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4 - FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE= - -----END OPENSSH PRIVATE KEY----- - ''; - - snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key"; - in { boot-ec2-nixops = makeEc2Test { name = "nixops-userdata"; + inherit image; sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! userData = '' @@ -142,6 +89,7 @@ in { boot-ec2-config = makeEc2Test { name = "config-userdata"; + inherit image; sshPublicKey = snakeOilPublicKey; # ### http://nixos.org/channels/nixos-unstable nixos diff --git a/nixos/tests/nova-image.nix b/nixos/tests/nova-image.nix new file mode 100644 index 000000000000..488649b70e6b --- /dev/null +++ b/nixos/tests/nova-image.nix @@ -0,0 +1,60 @@ +{ system ? builtins.currentSystem, + config ? {}, + pkgs ? import ../.. { inherit system config; } +}: + +with import ../lib/testing.nix { inherit system pkgs; }; +with pkgs.lib; + +with import common/ec2.nix { inherit makeTest pkgs; }; + +let + image = + (import ../lib/eval-config.nix { + inherit system; + modules = [ + ../maintainers/scripts/openstack/nova-image.nix + ../modules/testing/test-instrumentation.nix + ../modules/profiles/qemu-guest.nix + ]; + }).config.system.build.novaImage; + +in + makeEc2Test { + name = "nova-ec2-metadata"; + inherit image; + sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! + + userData = '' + SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey} + SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} + ''; + script = '' + $machine->start; + $machine->waitForFile("/etc/ec2-metadata/user-data"); + $machine->waitForUnit("sshd.service"); + + $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); + + # We have no keys configured on the client side yet, so this should fail + $machine->fail("ssh -o BatchMode=yes localhost exit"); + + # Let's install our client private key + $machine->succeed("mkdir -p ~/.ssh"); + + $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519"); + $machine->succeed("chmod 600 ~/.ssh/id_ed25519"); + + # We haven't configured the host key yet, so this should still fail + $machine->fail("ssh -o BatchMode=yes localhost exit"); + + # Add the host key; ssh should finally succeed + $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"); + $machine->succeed("ssh -o BatchMode=yes localhost exit"); + + # Just to make sure resizing is idempotent. + $machine->shutdown; + $machine->start; + $machine->waitForFile("/etc/ec2-metadata/user-data"); + ''; + } From 849460f8789943b9758c6e782d1cc0bb8a8bd950 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Mon, 28 Jan 2019 14:44:41 +0100 Subject: [PATCH 2/6] nova-image: add amazon-init module to the nova image This allows the VM to provide a `configuration.nix` file to the VM. The test doesn't work in sandbox because it needs Internet (however it works interactively). --- nixos/modules/virtualisation/nova-config.nix | 3 +- nixos/tests/nova-image.nix | 36 ++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/nixos/modules/virtualisation/nova-config.nix b/nixos/modules/virtualisation/nova-config.nix index fef20d498e97..c944fff96a85 100644 --- a/nixos/modules/virtualisation/nova-config.nix +++ b/nixos/modules/virtualisation/nova-config.nix @@ -8,6 +8,7 @@ with lib; ../profiles/headless.nix # The Openstack Metadata service exposes data on an EC2 API also. ./ec2-data.nix + ./amazon-init.nix ]; config = { @@ -32,7 +33,7 @@ with lib; path = [ pkgs.wget ]; description = "Fetch Metadata on startup"; wantedBy = [ "multi-user.target" ]; - before = [ "apply-ec2-data.service" ]; + before = [ "apply-ec2-data.service" "amazon-init.service"]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; script = diff --git a/nixos/tests/nova-image.nix b/nixos/tests/nova-image.nix index 488649b70e6b..7934ab31c705 100644 --- a/nixos/tests/nova-image.nix +++ b/nixos/tests/nova-image.nix @@ -19,12 +19,11 @@ let ]; }).config.system.build.novaImage; -in - makeEc2Test { - name = "nova-ec2-metadata"; +in { + metadata = makeEc2Test { + name = "nova-ec2-metadata"; inherit image; - sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! - + sshPublicKey = snakeOilPublicKey; userData = '' SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey} SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} @@ -57,4 +56,29 @@ in $machine->start; $machine->waitForFile("/etc/ec2-metadata/user-data"); ''; - } + }; + + userdata = makeEc2Test { + name = "nova-ec2-metadata"; + inherit image; + sshPublicKey = snakeOilPublicKey; + userData = '' + { pkgs, ... }: + { + imports = [ + + + + ]; + environment.etc.testFile = { + text = "whoa"; + }; + } + ''; + script = '' + $machine->start; + $machine->waitForFile("/etc/testFile"); + $machine->succeed("cat /etc/testFile | grep -q 'whoa'"); + ''; + }; +} From d190b204f001d1446807f56eed99a73f8b89e244 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Mon, 28 Jan 2019 15:09:48 +0100 Subject: [PATCH 3/6] Rename `novaImage` to `openstackImage` People don't necessary know `nova` is related to Openstack (it is a component of Openstack). So, it is more explicit to call it `openstackImage`. --- .../scripts/openstack/nova-image.nix | 26 ------------------- .../scripts/openstack/openstack-image.nix | 26 +++++++++++++++++++ .../{nova-config.nix => openstack-config.nix} | 2 +- nixos/tests/all-tests.nix | 3 +++ .../{nova-image.nix => openstack-image.nix} | 10 +++---- 5 files changed, 35 insertions(+), 32 deletions(-) delete mode 100644 nixos/maintainers/scripts/openstack/nova-image.nix create mode 100644 nixos/maintainers/scripts/openstack/openstack-image.nix rename nixos/modules/virtualisation/{nova-config.nix => openstack-config.nix} (98%) rename nixos/tests/{nova-image.nix => openstack-image.nix} (90%) diff --git a/nixos/maintainers/scripts/openstack/nova-image.nix b/nixos/maintainers/scripts/openstack/nova-image.nix deleted file mode 100644 index b6f3a5b15200..000000000000 --- a/nixos/maintainers/scripts/openstack/nova-image.nix +++ /dev/null @@ -1,26 +0,0 @@ -# nix-build '' -A config.system.build.novaImage --arg configuration "{ imports = [ ./nixos/maintainers/scripts/openstack/nova-image.nix ]; }" - -{ config, lib, pkgs, ... }: - -with lib; - -{ - imports = - [ ../../../modules/installer/cd-dvd/channel.nix - ../../../modules/virtualisation/nova-config.nix - ]; - - system.build.novaImage = import ../../../lib/make-disk-image.nix { - inherit lib config; - pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package - diskSize = 8192; - format = "qcow2"; - configFile = pkgs.writeText "configuration.nix" - '' - { - imports = [ ]; - } - ''; - }; - -} diff --git a/nixos/maintainers/scripts/openstack/openstack-image.nix b/nixos/maintainers/scripts/openstack/openstack-image.nix new file mode 100644 index 000000000000..4c464f43f61d --- /dev/null +++ b/nixos/maintainers/scripts/openstack/openstack-image.nix @@ -0,0 +1,26 @@ +# nix-build '' -A config.system.build.openstackImage --arg configuration "{ imports = [ ./nixos/maintainers/scripts/openstack/openstack-image.nix ]; }" + +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = + [ ../../../modules/installer/cd-dvd/channel.nix + ../../../modules/virtualisation/openstack-config.nix + ]; + + system.build.openstackImage = import ../../../lib/make-disk-image.nix { + inherit lib config; + pkgs = import ../../../.. { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package + diskSize = 8192; + format = "qcow2"; + configFile = pkgs.writeText "configuration.nix" + '' + { + imports = [ ]; + } + ''; + }; + +} diff --git a/nixos/modules/virtualisation/nova-config.nix b/nixos/modules/virtualisation/openstack-config.nix similarity index 98% rename from nixos/modules/virtualisation/nova-config.nix rename to nixos/modules/virtualisation/openstack-config.nix index c944fff96a85..7f4799d1719a 100644 --- a/nixos/modules/virtualisation/nova-config.nix +++ b/nixos/modules/virtualisation/openstack-config.nix @@ -29,7 +29,7 @@ with lib; passwordAuthentication = mkDefault false; }; - systemd.services.nova-init = { + systemd.services.openstack-init = { path = [ pkgs.wget ]; description = "Fetch Metadata on startup"; wantedBy = [ "multi-user.target" ]; diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 7bc2f3076f1e..e7040a52e304 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -159,6 +159,9 @@ in openldap = handleTest ./openldap.nix {}; opensmtpd = handleTest ./opensmtpd.nix {}; openssh = handleTest ./openssh.nix {}; + # openstack-image-userdata doesn't work in a sandbox as the simulated openstack instance needs network access + #openstack-image-userdata = (handleTestOn ["x86_64-linux"] ./openstack-image.nix {}).userdata or {}; + openstack-image-metadata = (handleTestOn ["x86_64-linux"] ./openstack-image.nix {}).metadata or {}; osquery = handleTest ./osquery.nix {}; ostree = handleTest ./ostree.nix {}; pam-oath-login = handleTest ./pam-oath-login.nix {}; diff --git a/nixos/tests/nova-image.nix b/nixos/tests/openstack-image.nix similarity index 90% rename from nixos/tests/nova-image.nix rename to nixos/tests/openstack-image.nix index 7934ab31c705..c7b28126e508 100644 --- a/nixos/tests/nova-image.nix +++ b/nixos/tests/openstack-image.nix @@ -13,15 +13,15 @@ let (import ../lib/eval-config.nix { inherit system; modules = [ - ../maintainers/scripts/openstack/nova-image.nix + ../maintainers/scripts/openstack/openstack-image.nix ../modules/testing/test-instrumentation.nix ../modules/profiles/qemu-guest.nix ]; - }).config.system.build.novaImage; + }).config.system.build.openstackImage; in { metadata = makeEc2Test { - name = "nova-ec2-metadata"; + name = "openstack-ec2-metadata"; inherit image; sshPublicKey = snakeOilPublicKey; userData = '' @@ -59,14 +59,14 @@ in { }; userdata = makeEc2Test { - name = "nova-ec2-metadata"; + name = "openstack-ec2-metadata"; inherit image; sshPublicKey = snakeOilPublicKey; userData = '' { pkgs, ... }: { imports = [ - + ]; From 78acac050ff27eb45332095935ab112de31d065d Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Mon, 28 Jan 2019 21:06:24 +0100 Subject: [PATCH 4/6] nixos/openstackImage: default hostname is empty string This is to let the `ec2-data.nix` module sets the hostname from the metadata API value. --- nixos/modules/virtualisation/openstack-config.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nixos/modules/virtualisation/openstack-config.nix b/nixos/modules/virtualisation/openstack-config.nix index 7f4799d1719a..9d8f2ac245d6 100644 --- a/nixos/modules/virtualisation/openstack-config.nix +++ b/nixos/modules/virtualisation/openstack-config.nix @@ -29,6 +29,9 @@ with lib; passwordAuthentication = mkDefault false; }; + # Force getting the hostname from Openstack metadata. + networking.hostName = mkDefault ""; + systemd.services.openstack-init = { path = [ pkgs.wget ]; description = "Fetch Metadata on startup"; From 933da6de9110ca208a82fb31a6730cbe9d971600 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Wed, 6 Feb 2019 12:16:22 +0100 Subject: [PATCH 5/6] nixos: Add ec2-metadata-fetcher.nix file To share the metadata fetcher script between ec2 and Openstack images. --- nixos/modules/virtualisation/amazon-image.nix | 29 +++++-------------- .../virtualisation/ec2-metadata-fetcher.nix | 23 +++++++++++++++ .../virtualisation/openstack-config.nix | 29 +++++-------------- 3 files changed, 38 insertions(+), 43 deletions(-) create mode 100644 nixos/modules/virtualisation/ec2-metadata-fetcher.nix diff --git a/nixos/modules/virtualisation/amazon-image.nix b/nixos/modules/virtualisation/amazon-image.nix index 9015200beead..6f4f99caa6f1 100644 --- a/nixos/modules/virtualisation/amazon-image.nix +++ b/nixos/modules/virtualisation/amazon-image.nix @@ -8,7 +8,13 @@ with lib; -let cfg = config.ec2; in +let + cfg = config.ec2; + metadataFetcher = import ./ec2-metadata-fetcher.nix { + targetRoot = "$targetRoot/"; + wgetExtraOptions = "-q"; + }; +in { imports = [ ../profiles/headless.nix ./ec2-data.nix ./amazon-init.nix ]; @@ -61,26 +67,7 @@ let cfg = config.ec2; in # Nix operations. boot.initrd.postMountCommands = '' - metaDir=$targetRoot/etc/ec2-metadata - mkdir -m 0755 -p "$metaDir" - - echo "getting EC2 instance metadata..." - - if ! [ -e "$metaDir/ami-manifest-path" ]; then - wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path - fi - - if ! [ -e "$metaDir/user-data" ]; then - wget -q -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data" - fi - - if ! [ -e "$metaDir/hostname" ]; then - wget -q -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname - fi - - if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then - wget -q -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key - fi + ${metadataFetcher} diskNr=0 diskForUnionfs= diff --git a/nixos/modules/virtualisation/ec2-metadata-fetcher.nix b/nixos/modules/virtualisation/ec2-metadata-fetcher.nix new file mode 100644 index 000000000000..b531787c31a2 --- /dev/null +++ b/nixos/modules/virtualisation/ec2-metadata-fetcher.nix @@ -0,0 +1,23 @@ +{ targetRoot, wgetExtraOptions }: +'' + metaDir=${targetRoot}etc/ec2-metadata + mkdir -m 0755 -p "$metaDir" + + echo "getting EC2 instance metadata..." + + if ! [ -e "$metaDir/ami-manifest-path" ]; then + wget ${wgetExtraOptions} -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path + fi + + if ! [ -e "$metaDir/user-data" ]; then + wget ${wgetExtraOptions} -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data" + fi + + if ! [ -e "$metaDir/hostname" ]; then + wget ${wgetExtraOptions} -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname + fi + + if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then + wget ${wgetExtraOptions} -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key + fi +'' diff --git a/nixos/modules/virtualisation/openstack-config.nix b/nixos/modules/virtualisation/openstack-config.nix index 9d8f2ac245d6..d5e862da0ea4 100644 --- a/nixos/modules/virtualisation/openstack-config.nix +++ b/nixos/modules/virtualisation/openstack-config.nix @@ -2,6 +2,12 @@ with lib; +let + metadataFetcher = import ./ec2-metadata-fetcher.nix { + targetRoot = "/"; + wgetExtraOptions = "--retry-connrefused"; + }; +in { imports = [ ../profiles/qemu-guest.nix @@ -39,28 +45,7 @@ with lib; before = [ "apply-ec2-data.service" "amazon-init.service"]; wants = [ "network-online.target" ]; after = [ "network-online.target" ]; - script = - '' - metaDir=/etc/ec2-metadata - mkdir -m 0755 -p "$metaDir" - - echo "getting Openstack instance metadata (via EC2 API)..." - if ! [ -e "$metaDir/ami-manifest-path" ]; then - wget --retry-connrefused -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path - fi - - if ! [ -e "$metaDir/user-data" ]; then - wget --retry-connrefused -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data" - fi - - if ! [ -e "$metaDir/hostname" ]; then - wget --retry-connrefused -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname - fi - - if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then - wget --retry-connrefused -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key - fi - ''; + script = metadataFetcher; restartIfChanged = false; unitConfig.X-StopOnRemoval = false; serviceConfig = { From ff310146870a130477a4fe47e5f53bfe2a77bc74 Mon Sep 17 00:00:00 2001 From: Antoine Eiche Date: Fri, 8 Feb 2019 09:20:40 +0100 Subject: [PATCH 6/6] nixos/tests/ec2: reuse ssh keys from ssh-keys.nix --- nixos/tests/common/ec2.nix | 12 ------------ nixos/tests/ec2.nix | 4 ++++ nixos/tests/openstack-image.nix | 4 ++++ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/nixos/tests/common/ec2.nix b/nixos/tests/common/ec2.nix index 99a39473b617..1e69b63191a7 100644 --- a/nixos/tests/common/ec2.nix +++ b/nixos/tests/common/ec2.nix @@ -46,16 +46,4 @@ with pkgs.lib; ${script} ''; }; - - snakeOilPrivateKey = '' - -----BEGIN OPENSSH PRIVATE KEY----- - b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW - QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye - EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q - AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4 - FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE= - -----END OPENSSH PRIVATE KEY----- - ''; - - snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key"; } diff --git a/nixos/tests/ec2.nix b/nixos/tests/ec2.nix index db959a63e40f..384fce67c227 100644 --- a/nixos/tests/ec2.nix +++ b/nixos/tests/ec2.nix @@ -41,6 +41,10 @@ let ]; }).config.system.build.amazonImage; + sshKeys = import ./ssh-keys.nix pkgs; + snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; + snakeOilPublicKey = sshKeys.snakeOilPublicKey; + in { boot-ec2-nixops = makeEc2Test { name = "nixops-userdata"; diff --git a/nixos/tests/openstack-image.nix b/nixos/tests/openstack-image.nix index c7b28126e508..d0225016ab76 100644 --- a/nixos/tests/openstack-image.nix +++ b/nixos/tests/openstack-image.nix @@ -19,6 +19,10 @@ let ]; }).config.system.build.openstackImage; + sshKeys = import ./ssh-keys.nix pkgs; + snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; + snakeOilPublicKey = sshKeys.snakeOilPublicKey; + in { metadata = makeEc2Test { name = "openstack-ec2-metadata";