42 lines
1.5 KiB
FSharp
42 lines
1.5 KiB
FSharp
module Install
|
|
|
|
#r "/opt/fsi/packages/AWSSDK.Core.dll"
|
|
#r "/opt/fsi/packages/AWSSDK.S3.dll"
|
|
|
|
open System
|
|
open System.IO
|
|
open System.IO.Compression
|
|
open System.Security.Cryptography
|
|
open Amazon.Runtime
|
|
open Amazon.S3
|
|
open Amazon.S3.Model
|
|
|
|
[<CLIMutable>]
|
|
type Settings =
|
|
{ Endpoint: string
|
|
Bucket: string
|
|
Key: string
|
|
Sha256: string
|
|
AccessKey: string }
|
|
|
|
let unpack expected (input: Stream) destination =
|
|
use archive = new MemoryStream()
|
|
input.CopyTo archive
|
|
archive.Position <- 0L
|
|
let actual = SHA256.HashData archive |> Convert.ToHexString |> _.ToLowerInvariant()
|
|
if actual <> expected then
|
|
invalidOp "plugin archive checksum differs"
|
|
archive.Position <- 0L
|
|
use zip = new ZipArchive(archive, ZipArchiveMode.Read)
|
|
zip.ExtractToDirectory(destination, true)
|
|
for entry in zip.Entries do
|
|
if entry.Name <> "" then
|
|
File.SetUnixFileMode(Path.Combine(destination, entry.FullName), enum<UnixFileMode> ((entry.ExternalAttributes >>> 16) &&& 0o777))
|
|
|
|
let run settings password destination =
|
|
let config = AmazonS3Config(ServiceURL = settings.Endpoint, AuthenticationRegion = "us-east-1", ForcePathStyle = true)
|
|
use client = new AmazonS3Client(BasicAWSCredentials(settings.AccessKey, password), config)
|
|
let request = GetObjectRequest(BucketName = settings.Bucket, Key = settings.Key)
|
|
use reply = client.GetObjectAsync(request).GetAwaiter().GetResult()
|
|
unpack settings.Sha256 reply.ResponseStream destination
|
|
printfn "Installed plugin archive sha256:%s" settings.Sha256
|