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.
This commit is contained in:
parent
bf041c3f1d
commit
2858b35100
|
@ -1,4 +1,4 @@
|
||||||
{ lib, ... }:
|
{ pkgs, lib, ... }:
|
||||||
|
|
||||||
with lib;
|
with lib;
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ with lib;
|
||||||
imports = [
|
imports = [
|
||||||
../profiles/qemu-guest.nix
|
../profiles/qemu-guest.nix
|
||||||
../profiles/headless.nix
|
../profiles/headless.nix
|
||||||
|
# The Openstack Metadata service exposes data on an EC2 API also.
|
||||||
|
./ec2-data.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
@ -26,35 +28,41 @@ with lib;
|
||||||
passwordAuthentication = mkDefault false;
|
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.
|
echo "getting Openstack instance metadata (via EC2 API)..."
|
||||||
# Unfortunately we can't do this with the `fileSystems' option
|
if ! [ -e "$metaDir/ami-manifest-path" ]; then
|
||||||
# because it has no support for creating the source of a bind
|
wget --retry-connrefused -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
|
||||||
# mount. Also, "move" /nix to /ephemeral0 by layering a unionfs-fuse
|
fi
|
||||||
# mount on top of it so we have a lot more space for Nix operations.
|
|
||||||
|
|
||||||
/*
|
if ! [ -e "$metaDir/user-data" ]; then
|
||||||
boot.initrd.postMountCommands =
|
wget --retry-connrefused -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data"
|
||||||
''
|
fi
|
||||||
mkdir -m 1777 -p $targetRoot/ephemeral0/tmp
|
|
||||||
mkdir -m 1777 -p $targetRoot/tmp
|
|
||||||
mount --bind $targetRoot/ephemeral0/tmp $targetRoot/tmp
|
|
||||||
|
|
||||||
mkdir -m 755 -p $targetRoot/ephemeral0/var
|
if ! [ -e "$metaDir/hostname" ]; then
|
||||||
mkdir -m 755 -p $targetRoot/var
|
wget --retry-connrefused -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname
|
||||||
mount --bind $targetRoot/ephemeral0/var $targetRoot/var
|
fi
|
||||||
|
|
||||||
mkdir -p /unionfs-chroot/ro-nix
|
if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then
|
||||||
mount --rbind $targetRoot/nix /unionfs-chroot/ro-nix
|
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
|
||||||
mkdir -p /unionfs-chroot/rw-nix
|
'';
|
||||||
mkdir -m 755 -p $targetRoot/ephemeral0/nix
|
restartIfChanged = false;
|
||||||
mount --rbind $targetRoot/ephemeral0/nix /unionfs-chroot/rw-nix
|
unitConfig.X-StopOnRemoval = false;
|
||||||
unionfs -o allow_other,cow,nonempty,chroot=/unionfs-chroot,max_files=32768 /rw-nix=RW:/ro-nix=RO $targetRoot/nix
|
serviceConfig = {
|
||||||
'';
|
Type = "oneshot";
|
||||||
|
RemainAfterExit = true;
|
||||||
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
|
};
|
||||||
*/
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
61
nixos/tests/common/ec2.nix
Normal file
61
nixos/tests/common/ec2.nix
Normal file
|
@ -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";
|
||||||
|
}
|
|
@ -6,6 +6,8 @@
|
||||||
with import ../lib/testing.nix { inherit system pkgs; };
|
with import ../lib/testing.nix { inherit system pkgs; };
|
||||||
with pkgs.lib;
|
with pkgs.lib;
|
||||||
|
|
||||||
|
with import common/ec2.nix { inherit makeTest pkgs; };
|
||||||
|
|
||||||
let
|
let
|
||||||
image =
|
image =
|
||||||
(import ../lib/eval-config.nix {
|
(import ../lib/eval-config.nix {
|
||||||
|
@ -39,65 +41,10 @@ let
|
||||||
];
|
];
|
||||||
}).config.system.build.amazonImage;
|
}).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 {
|
in {
|
||||||
boot-ec2-nixops = makeEc2Test {
|
boot-ec2-nixops = makeEc2Test {
|
||||||
name = "nixops-userdata";
|
name = "nixops-userdata";
|
||||||
|
inherit image;
|
||||||
sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
|
sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
|
||||||
|
|
||||||
userData = ''
|
userData = ''
|
||||||
|
@ -142,6 +89,7 @@ in {
|
||||||
|
|
||||||
boot-ec2-config = makeEc2Test {
|
boot-ec2-config = makeEc2Test {
|
||||||
name = "config-userdata";
|
name = "config-userdata";
|
||||||
|
inherit image;
|
||||||
sshPublicKey = snakeOilPublicKey;
|
sshPublicKey = snakeOilPublicKey;
|
||||||
|
|
||||||
# ### http://nixos.org/channels/nixos-unstable nixos
|
# ### http://nixos.org/channels/nixos-unstable nixos
|
||||||
|
|
60
nixos/tests/nova-image.nix
Normal file
60
nixos/tests/nova-image.nix
Normal file
|
@ -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");
|
||||||
|
'';
|
||||||
|
}
|
Loading…
Reference in a new issue