Unverified Commit 45d8ed8f authored by Philip Taron's avatar Philip Taron
Browse files

nixos/ec2-metadata-fetcher: add bzip2 user-data decompression

Refactor try_decompress to use a decompress_cmd variable, making it
straightforward to add new compression formats. Add bzip2 support
and a corresponding NixOS test.
parent ea11e1de
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@ in
      wants = [ "network-online.target" ];
      after = [ "network-online.target" ];
      path = with pkgs; [
        bzip2
        curl
        file
        gzip
+12 −10
Original line number Diff line number Diff line
@@ -62,22 +62,24 @@ get_imds() {
}

try_decompress() {
  local temp ftype
  local temp ftype decompress_cmd
  if [ ! -s "$1" ]; then
    return
  fi
  ftype=$(file --brief "$1")
  case $ftype in
    gzip*)
    gzip*)  decompress_cmd=zcat ;;
    bzip2*) decompress_cmd=bzcat ;;
    *)      return ;;
  esac
  echo "decompressing: $1"
  temp=$(mktemp)
      if zcat "$1" > "$temp"; then
  if $decompress_cmd "$1" > "$temp"; then
    mv "$temp" "$1"
  else
    echo "failed to decompress: $1"
    rm -f "$temp"
  fi
  esac
}

get_imds -o "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
+5 −0
Original line number Diff line number Diff line
@@ -273,6 +273,11 @@ in
            test_data = b"#!/bin/bash\necho gzip-decompression-test\n"
            test_userdata_decompression(machine, user_data_path, gzip_mod.compress(test_data), "gzip")

        with subtest("Decompression of bzip2-compressed user-data"):
            import bz2
            test_data = b"#!/bin/bash\necho bzip2-decompression-test\n"
            test_userdata_decompression(machine, user_data_path, bz2.compress(test_data), "bzip2")

    finally:
        machine.shutdown()
        temp_dir.cleanup()