CoreOS.ps1

<#
 
 
 
#>


function Get-CoreOSImage {

    $cache = Get-BoxPath -Path "cache"

    $ProgressPreference = "SilentlyContinue"

    ##### Get metadata

    Write-Host "Checking new CoreOS image ..." -NoNewline
    $response = Invoke-WebRequest "https://builds.coreos.fedoraproject.org/streams/stable.json"
    Write-Host -ForegroundColor Green " Done."
    
    $data = $response.Content | ConvertFrom-Json
    $virtualbox = $data.architectures.x86_64.artifacts.virtualbox
    
    #### Get cached image

    $imageFile = Join-Path $cache "fedora-coreos-$($virtualbox.release)-virtualbox.x86_64.ova"
    if (Test-Path $imageFile -PathType Leaf) {
        return $imageFile
    }

    #### Download new image

    $disk = $virtualbox.formats.ova.disk

    $tmpImageFile = "${imageFile}.tmp"
    if (Test-Path $tmpImageFile -PathType Leaf) {
        Remove-Item $tmpImageFile
    }

    $response = Invoke-WebRequest $disk.location -UseBasicParsing -Method Head
    $downloadSize = [int]$response.Headers["Content-Length"]

    Write-Host "Downloading new CoreOS image '$($virtualbox.release)' ($([int]($downloadSize / 1024 / 1024)) MB) ..." -NoNewline
    Invoke-WebRequest $disk.location -OutFile $tmpImageFile
    Write-Host -ForegroundColor Green " Done."

    $hash = (Get-FileHash $tmpImageFile -Algorithm SHA256).Hash
    if (-Not($hash -eq $disk.sha256)) {
        # Remove-Item $tmpImageFile
        Write-Host -ForegroundColor Red "Downloaded CoreOs image '$($virtualbox.release)' hash check failed." 
        exit
    }
    
    Move-Item $tmpImageFile $imageFile

    return $imageFile
}

function Get-CoreOSIgnitionConfig {
    param(
        [Parameter(mandatory = $true)]
        [string] $Hostname
    )
    
    $config = @'
{
  "ignition": { "version": "3.4.0" },
  "passwd": {
    "users": [
      {
        "name": "box",
        "passwordHash": "$y$j9T$BAlET20ZhfuQ.YzttOAaA.$8O8Fb/0UMSq5TPyufNVGffUrUYiazipQglTTo4VN.iB",
        "sshAuthorizedKeys": [
          "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJdMddarXNcDnTCO2TFoF5uqrD3sicDofldtedxhlDdU box"
        ]
      }
    ]
  }
  "storage": {
    "files": [{
      "path": "/etc/hostname",
      "mode": 420,
      "overwrite": true,
      "contents": { "source": "data:,${Hostname}" }
    }]
  }
}
'@


$config = '{"ignition": { "version": "3.4.0" }}'

$config = @'
{"ignition": { "version": "3.4.0" }, "storage": {
    "files": [{
      "path": "/etc/hostname",
      "mode": 420,
      "overwrite": true,
      "contents": { "source": "data:,xtec" }
    }]
  }}
'@


  return $config
}