nixos: Add ec2-metadata-fetcher.nix file

To share the metadata fetcher script between ec2 and Openstack images.
This commit is contained in:
Antoine Eiche 2019-02-06 12:16:22 +01:00
parent 78acac050f
commit 933da6de91
3 changed files with 38 additions and 43 deletions

View File

@ -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=

View File

@ -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
''

View File

@ -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 = {