-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFirmwareUpdate.fs
67 lines (60 loc) · 2.44 KB
/
FirmwareUpdate.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module FirmwareUpdate
open System.Diagnostics
open System
open FSharp.Control.Tasks.ContextInsensitive
open Thoth.Json.Net
open ServerCore.Domain
open System.Threading.Tasks
open Paket
open System.IO
let firmwareTarget = Path.GetFullPath "/home/pi/firmware"
let runFirmwareUpdate() =
let p = new Process()
let startInfo = ProcessStartInfo()
startInfo.WorkingDirectory <- "/home/pi/firmware/"
startInfo.FileName <- "sudo"
startInfo.Arguments <- "sh update.sh"
startInfo.RedirectStandardOutput <- true
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
p.StartInfo <- startInfo
p.Start() |> ignore
let checkFirmware (log:log4net.ILog,tagServer) = task {
use webClient = new Net.WebClient()
Net.ServicePointManager.SecurityProtocol <-
Net.ServicePointManager.SecurityProtocol |||
Net.SecurityProtocolType.Tls11 |||
Net.SecurityProtocolType.Tls12
let url = sprintf @"%s/api/firmware" tagServer
let! result = webClient.DownloadStringTaskAsync(Uri url)
match Decode.fromString Firmware.Decoder result with
| Error msg ->
log.ErrorFormat("Decoder error: {0}", msg)
return failwith msg
| Ok firmware ->
try
log.InfoFormat("Latest firmware on server: {0}", firmware.Version)
let serverVersion = SemVer.Parse firmware.Version
let localVersion = SemVer.Parse ReleaseNotes.Version
if serverVersion > localVersion then
let localFileName = Path.GetTempFileName().Replace(".tmp", ".zip")
log.InfoFormat("Starting download of {0}", firmware.Url)
do! webClient.DownloadFileTaskAsync(firmware.Url,localFileName)
log.Info "Download done."
if Directory.Exists firmwareTarget then
Directory.Delete(firmwareTarget,true)
Directory.CreateDirectory(firmwareTarget) |> ignore
Compression.ZipFile.ExtractToDirectory(localFileName, firmwareTarget)
File.Delete localFileName
runFirmwareUpdate()
while true do
log.Info "Running firmware update."
do! Task.Delay 3000
()
else
if Directory.Exists firmwareTarget then
Directory.Delete(firmwareTarget,true)
with
| exn ->
log.ErrorFormat("Upgrade error: {0}", exn.Message)
}