Fetch all EC2 metadata / user data in the initrd
Since we're already fetching one datum, we may as well fetch the others needed by fetch-ec2-data. This also eliminates the dependency on wget.
This commit is contained in:
@@ -57,8 +57,7 @@ let cfg = config.ec2; in
|
|||||||
boot.initrd.postMountCommands =
|
boot.initrd.postMountCommands =
|
||||||
''
|
''
|
||||||
metaDir=$targetRoot/etc/ec2-metadata
|
metaDir=$targetRoot/etc/ec2-metadata
|
||||||
mkdir -m 0755 -p $targetRoot/etc
|
mkdir -m 0755 -p "$metaDir"
|
||||||
mkdir -m 0700 -p "$metaDir"
|
|
||||||
|
|
||||||
echo "getting EC2 instance metadata..."
|
echo "getting EC2 instance metadata..."
|
||||||
|
|
||||||
@@ -66,6 +65,19 @@ let cfg = config.ec2; in
|
|||||||
wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
|
wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
|
||||||
fi
|
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
|
||||||
|
|
||||||
diskNr=0
|
diskNr=0
|
||||||
diskForUnionfs=
|
diskForUnionfs=
|
||||||
for device in /dev/xvd[abcde]*; do
|
for device in /dev/xvd[abcde]*; do
|
||||||
|
|||||||
@@ -8,13 +8,12 @@ let
|
|||||||
|
|
||||||
echo "attempting to fetch configuration from EC2 user data..."
|
echo "attempting to fetch configuration from EC2 user data..."
|
||||||
|
|
||||||
export PATH=${config.nix.package}/bin:${pkgs.wget}/bin:${pkgs.systemd}/bin:${pkgs.gnugrep}/bin:${pkgs.gnused}/bin:${config.system.build.nixos-rebuild}/bin:$PATH
|
export PATH=${config.nix.package}/bin:${pkgs.systemd}/bin:${pkgs.gnugrep}/bin:${pkgs.gnused}/bin:${config.system.build.nixos-rebuild}/bin:$PATH
|
||||||
export NIX_PATH=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
|
export NIX_PATH=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
|
||||||
|
|
||||||
userData="$(mktemp)"
|
userData=/etc/ec2-metadata/user-data
|
||||||
wget -q --wait=1 --tries=0 --retry-connrefused -O - http://169.254.169.254/2011-01-01/user-data > "$userData"
|
|
||||||
|
|
||||||
if [[ $? -eq 0 ]]; then
|
if [ -s "$userData" ]; then
|
||||||
|
|
||||||
# If the user-data looks like it could be a nix expression,
|
# If the user-data looks like it could be a nix expression,
|
||||||
# copy it over. Also, look for a magic three-hash comment and set
|
# copy it over. Also, look for a magic three-hash comment and set
|
||||||
@@ -37,7 +36,7 @@ let
|
|||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "failed to fetch user data"
|
echo "no user data is available"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# This module defines a systemd service that obtains the SSH key and
|
# This module defines a systemd service that sets the SSH host key and
|
||||||
# host name of virtual machines running on Amazon EC2, Eucalyptus and
|
# authorized client key and host name of virtual machines running on
|
||||||
# OpenStack Compute (Nova).
|
# Amazon EC2, Eucalyptus and OpenStack Compute (Nova).
|
||||||
|
|
||||||
{ config, lib, pkgs, ... }:
|
{ config, lib, pkgs, ... }:
|
||||||
|
|
||||||
@@ -9,55 +9,49 @@ with lib;
|
|||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
|
|
||||||
systemd.services.fetch-ec2-data =
|
systemd.services.apply-ec2-data =
|
||||||
{ description = "Fetch EC2 Data";
|
{ description = "Apply EC2 Data";
|
||||||
|
|
||||||
wantedBy = [ "multi-user.target" "sshd.service" ];
|
wantedBy = [ "multi-user.target" "sshd.service" ];
|
||||||
before = [ "sshd.service" ];
|
before = [ "sshd.service" ];
|
||||||
wants = [ "ip-up.target" ];
|
|
||||||
after = [ "ip-up.target" ];
|
|
||||||
|
|
||||||
path = [ pkgs.wget pkgs.iproute ];
|
path = [ pkgs.iproute ];
|
||||||
|
|
||||||
script =
|
script =
|
||||||
''
|
''
|
||||||
wget="wget -q --retry-connrefused -O -"
|
|
||||||
|
|
||||||
${optionalString (config.networking.hostName == "") ''
|
${optionalString (config.networking.hostName == "") ''
|
||||||
echo "setting host name..."
|
echo "setting host name..."
|
||||||
${pkgs.nettools}/bin/hostname $($wget http://169.254.169.254/1.0/meta-data/hostname)
|
if [ -s /etc/ec2-metadata/hostname ]; then
|
||||||
|
${pkgs.nettools}/bin/hostname $(cat /etc/ec2-metadata/hostname)
|
||||||
|
fi
|
||||||
''}
|
''}
|
||||||
|
|
||||||
# Don't download the SSH key if it has already been injected
|
|
||||||
# into the image (a Nova feature).
|
|
||||||
if ! [ -e /root/.ssh/authorized_keys ]; then
|
if ! [ -e /root/.ssh/authorized_keys ]; then
|
||||||
echo "obtaining SSH key..."
|
echo "obtaining SSH key..."
|
||||||
mkdir -m 0700 -p /root/.ssh
|
mkdir -m 0700 -p /root/.ssh
|
||||||
$wget http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key > /root/key.pub
|
if [ -s /etc/ec2-metadata/public-keys-0-openssh-key ]; then
|
||||||
if [ $? -eq 0 -a -e /root/key.pub ]; then
|
cat /etc/ec2-metadata/public-keys-0-openssh-key >> /root/.ssh/authorized_keys
|
||||||
cat /root/key.pub >> /root/.ssh/authorized_keys
|
|
||||||
echo "new key added to authorized_keys"
|
echo "new key added to authorized_keys"
|
||||||
chmod 600 /root/.ssh/authorized_keys
|
chmod 600 /root/.ssh/authorized_keys
|
||||||
rm -f /root/key.pub
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Extract the intended SSH host key for this machine from
|
# Extract the intended SSH host key for this machine from
|
||||||
# the supplied user data, if available. Otherwise sshd will
|
# the supplied user data, if available. Otherwise sshd will
|
||||||
# generate one normally.
|
# generate one normally.
|
||||||
$wget http://169.254.169.254/2011-01-01/user-data > /root/user-data || true
|
userData=/etc/ec2-metadata/user-data
|
||||||
|
|
||||||
mkdir -m 0755 -p /etc/ssh
|
mkdir -m 0755 -p /etc/ssh
|
||||||
|
|
||||||
key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' /root/user-data)"
|
key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' $userData)"
|
||||||
key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' /root/user-data)"
|
key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' $userData)"
|
||||||
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then
|
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_dsa_key ]; then
|
||||||
(umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
|
(umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
|
||||||
echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
|
echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
|
||||||
fi
|
fi
|
||||||
|
|
||||||
key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' /root/user-data)"
|
key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' $userData)"
|
||||||
key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' /root/user-data)"
|
key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' $userData)"
|
||||||
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then
|
if [ -n "$key" -a -n "$key_pub" -a ! -e /etc/ssh/ssh_host_ed25519_key ]; then
|
||||||
(umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
|
(umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
|
||||||
echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
|
echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
|
||||||
|
|||||||
Reference in New Issue
Block a user