1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00
Files
BakaShrot/OldSource/Build.ps1
T
aziascreations e044fcf359 Moved the PureBasic version to OldSource/, Started the Rust rewrite in Source/
The rewrite's plan and decisions are in revamp.md. Steps 0 to 10 are done:
the workspace and checks, bakashrot-proto, bakashrot-pki and
bakashrot-cert, bakashrot-config, bakashrot-storage (schema v9), the
server and the client library (every request but replication's,
attachments and retention included), the C DLL with its C, PureBasic and
Python bindings, and the bakashrot tool. Step 11 (replication) is next.

OldSource/ is the old implementation, kept building and passing as the
reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVTGs2SsufaH6zquzcb
2026-11-14 12:47:40 +01:00

334 lines
12 KiB
PowerShell

# ============================================================================
# BakaShrot build & run script
#
# 0. Extracts libsodium's dynamic build from Libs/libsodium-*-msvc.zip, and
# sqlite3.dll from the latest Libs/sqlite-dll-win-*.zip, if not done yet.
# Delete Libs/libsodium/x64 or Libs/sqlite/x64 to extract them again.
# 1. Compiles BakaShrotGenerateKeyPairs, the server, the client DLL, its
# example app and the tools (BakaShrotPushMessage,
# BakaShrotGetLatestMessage, BakaShrotGetMessages, BakaShrotGetAttachment,
# BakaShrotQuery, BakaShrotResolveIds, BakaShrotList) into ./Build, next to copies of
# libsodium.dll and sqlite3.dll
# 2. Generates a keypair for two servers and one for a client
# 3. Writes the servers' configs and the client's keys. Topics and nodes
# have names, and each server its own ids for them, to show names being
# translated (logs is 1 on server, 200 on server2):
# server.ini logs:server1 on port 12345, registers the client, peer
# of server2, database ./server.sqlite
# server2.ini logs:server2 on port 12346, peer of server, database
# ./server2.sqlite
# client.ini logs:local through server, for the tools and the example
# app, may push to logs and sensors, read every topic, and
# run raw queries
# client2.ini logs:local2 through server2, same client key, may not query
# Both servers replicate logs and sensors to each other, e.g.:
# .\BakaShrotPushMessage.exe -t sensors @msg.json client.ini
# .\BakaShrotGetLatestMessage.exe sensors local client2.ini
# .\BakaShrotGetMessages.exe logs local --follow client2.ini
# .\BakaShrotResolveIds.exe topic sensors client2.ini (201 there, 2 on server)
# .\BakaShrotQuery.exe "SELECT topic, node, message_id FROM messages" client.ini
# 4. Runs the first server, type "quit" or an empty line to stop it, or
# "restart" / "vacuum" / "help". Run the second one in
# another terminal to see them link: .\BakaShrot_Server.exe server2.ini
#
# Usage: powershell -ExecutionPolicy Bypass -File Build.ps1 [-Compiler <path>]
#
# Keys are regenerated on every run, ./Build is git-ignored since it holds
# private keys.
# ============================================================================
param(
[string]$Compiler = "C:\Program Files\PureBasic_573\Compilers\pbcompiler.exe"
)
$ErrorActionPreference = "Stop"
$Root = $PSScriptRoot
$Output = Join-Path $Root "Build"
# Where Commons/Cryptography/Libsodium.pbi imports it from, x64 since the
# compiler above is the x64 one.
$SodiumDir = Join-Path $Root "Libs\libsodium\x64"
# Where sqlite3.dll is extracted to, the server loads it instead of the
# SQLite bundled with PureBasic 5.73, which lacks the JSON functions.
$SqliteDir = Join-Path $Root "Libs\sqlite\x64"
$ServerHost = "127.0.0.1"
$ServerPort = 12345
$Server2Port = 12346
$PeerRetryDelay = 10
$PeerBatchSize = 100
# The same names on both servers, different ids, see the top of this file.
# Same client, through server2 as its own node (local2), so no topic/node is
# ever written to on both servers.
$Topics = @{ logs = 1; sensors = 2 }
$Nodes = @{ server1 = 1; local = 2; server2 = 3; local2 = 4 }
$Topics2 = @{ logs = 200; sensors = 201 }
$Nodes2 = @{ server1 = 11; local = 12; server2 = 13; local2 = 14 }
function Format-Names($Names) {
($Names.GetEnumerator() | Sort-Object Value | ForEach-Object { "$($_.Key) = $($_.Value)" }) -join "`r`n"
}
function Expand-Libsodium {
$Zip = Get-ChildItem (Join-Path $Root "Libs") -Filter "libsodium-*-msvc.zip" | Select-Object -First 1
if (-not $Zip) {
throw "Missing Libs\libsodium-*-msvc.zip, get it from https://download.libsodium.org/libsodium/releases/"
}
Write-Host "Extracting libsodium from $($Zip.Name)"
Add-Type -AssemblyName System.IO.Compression.FileSystem
$Archive = [IO.Compression.ZipFile]::OpenRead($Zip.FullName)
try {
# Zip folder -> Libs\libsodium folder
$Targets = @{ "x64" = "x64"; "Win32" = "x86" }
foreach ($Arch in $Targets.Keys) {
$Destination = Join-Path $Root "Libs\libsodium\$($Targets[$Arch])"
New-Item -ItemType Directory -Force $Destination | Out-Null
foreach ($Name in @("libsodium.dll", "libsodium.lib")) {
$Entry = $Archive.GetEntry("libsodium/$Arch/Release/v143/dynamic/$Name")
if (-not $Entry) {
throw "$($Zip.Name) has no $Arch dynamic $Name"
}
[IO.Compression.ZipFileExtensions]::ExtractToFile($Entry, (Join-Path $Destination $Name), $true)
}
}
}
finally {
$Archive.Dispose()
}
}
function Expand-Sqlite {
Add-Type -AssemblyName System.IO.Compression.FileSystem
foreach ($Arch in @("x64", "x86")) {
# The version in the name is zero-padded, so the last one by name is the latest.
$Zip = Get-ChildItem (Join-Path $Root "Libs") -Filter "sqlite-dll-win-$Arch-*.zip" | Sort-Object Name | Select-Object -Last 1
if (-not $Zip) {
throw "Missing Libs\sqlite-dll-win-$Arch-*.zip, get it from https://www.sqlite.org/download.html"
}
Write-Host "Extracting SQLite from $($Zip.Name)"
$Destination = Join-Path $Root "Libs\sqlite\$Arch"
New-Item -ItemType Directory -Force $Destination | Out-Null
$Archive = [IO.Compression.ZipFile]::OpenRead($Zip.FullName)
try {
$Entry = $Archive.GetEntry("sqlite3.dll")
if (-not $Entry) {
throw "$($Zip.Name) has no sqlite3.dll"
}
[IO.Compression.ZipFileExtensions]::ExtractToFile($Entry, (Join-Path $Destination "sqlite3.dll"), $true)
}
finally {
$Archive.Dispose()
}
}
}
function Invoke-Compiler($Source, $Executable, $Flags) {
Write-Host "Compiling $Source"
& $Compiler (Join-Path $Root $Source) @Flags /QUIET /EXE (Join-Path $Output $Executable)
if ($LASTEXITCODE -ne 0) {
throw "Failed to compile $Source"
}
}
function New-KeyPair {
# BakaShrotGenerateKeyPairs prints "PrivateKey = ..." then "PublicKey = ...".
$Lines = & (Join-Path $Output "BakaShrotGenerateKeyPairs.exe")
if ($LASTEXITCODE -ne 0) {
throw "BakaShrotGenerateKeyPairs failed: $Lines"
}
return @{
PrivateKey = $Lines[0] -replace "^PrivateKey = ", ""
PublicKey = $Lines[1] -replace "^PublicKey = ", ""
}
}
Push-Location $Root
try {
New-Item -ItemType Directory -Force $Output | Out-Null
# 0. libsodium
if (-not (Test-Path (Join-Path $SodiumDir "libsodium.lib"))) {
Expand-Libsodium
}
Copy-Item (Join-Path $SodiumDir "libsodium.dll") $Output -Force
# 0. SQLite
if (-not (Test-Path (Join-Path $SqliteDir "sqlite3.dll"))) {
Expand-Sqlite
}
Copy-Item (Join-Path $SqliteDir "sqlite3.dll") $Output -Force
# 1. Compile
Invoke-Compiler "Tools\BakaShrotGenerateKeyPairs\BakaShrotGenerateKeyPairs.pb" "BakaShrotGenerateKeyPairs.exe" @("/CONSOLE")
Invoke-Compiler "Server\BakaShrot_Server.pb" "BakaShrot_Server.exe" @("/CONSOLE", "/THREAD")
Invoke-Compiler "Client\BakaShrot_Client.pbi" "BakaShrot_Client.dll" @("/DLL", "/THREAD")
# Example app using the DLL, run it from ./Build while the server runs.
Invoke-Compiler "Client\BakaShrot_Client_Prototypes.pbi" "ClientExample.exe" @("/CONSOLE")
# Tools using the DLL, same as the example:
# BakaShrotPushMessage [-t <Topic>] [-a <File>] <Json|@JsonFile> [ClientIni]
Invoke-Compiler "Tools\BakaShrotPushMessage\BakaShrotPushMessage.pb" "BakaShrotPushMessage.exe" @("/CONSOLE")
# BakaShrotGetLatestMessage <Topic> <Node> [ClientIni]
Invoke-Compiler "Tools\BakaShrotGetLatestMessage\BakaShrotGetLatestMessage.pb" "BakaShrotGetLatestMessage.exe" @("/CONSOLE")
# BakaShrotGetMessages <Topic> <Node> [--after <Id|latest>] [--limit <N>] [--follow] [ClientIni]
Invoke-Compiler "Tools\BakaShrotGetMessages\BakaShrotGetMessages.pb" "BakaShrotGetMessages.exe" @("/CONSOLE")
# BakaShrotGetAttachment <Topic> <Node> <MessageId> <OutFile> [ClientIni]
Invoke-Compiler "Tools\BakaShrotGetAttachment\BakaShrotGetAttachment.pb" "BakaShrotGetAttachment.exe" @("/CONSOLE")
# BakaShrotQuery <Sql|@SqlFile> [ClientIni]
Invoke-Compiler "Tools\BakaShrotQuery\BakaShrotQuery.pb" "BakaShrotQuery.exe" @("/CONSOLE")
# BakaShrotResolveIds <topic|node> <NameOrId> [ClientIni]
Invoke-Compiler "Tools\BakaShrotResolveIds\BakaShrotResolveIds.pb" "BakaShrotResolveIds.exe" @("/CONSOLE")
# BakaShrotList [topics|nodes|all] [ClientIni] [--json]
Invoke-Compiler "Tools\BakaShrotList\BakaShrotList.pb" "BakaShrotList.exe" @("/CONSOLE")
# 2. Keys
Write-Host "Generating keys"
$ServerKeys = New-KeyPair
$Server2Keys = New-KeyPair
$ClientKeys = New-KeyPair
# 3. Configs
# Both servers are each other's peer, and register each other as a client
# so the links can authenticate.
$ServerConfig = @"
[BakaShrot]
PrivateKey = $($ServerKeys.PrivateKey)
Node = server1
; This server's ids for the names every server shares.
[BakaShrot.Topics]
$(Format-Names $Topics)
[BakaShrot.Nodes]
$(Format-Names $Nodes)
[BakaShrot.Server]
Host = $ServerHost
Port = $ServerPort
ReplicatingTopics = logs;sensors
NetThreadPoolSize = 4
DatabasePath = ./server.sqlite
PeerRetryDelay = $PeerRetryDelay
PeerBatchSize = $PeerBatchSize
[BakaShrot.Peer.server2]
Host = $ServerHost
Port = $Server2Port
Node = server2
PublicKey = $($Server2Keys.PublicKey)
[BakaShrot.Client.local]
PublicKeys = $($ClientKeys.PublicKey)
Node = local
DefaultTopic = logs
PushTopics = logs;sensors
ReadTopics = *
AllowRead = 1
AllowWrite = 1
AllowQuery = 1
; The peer only replicates, it has no business reading or writing as a client.
[BakaShrot.Client.server2]
PublicKeys = $($Server2Keys.PublicKey)
Node = server2
AllowRead = 0
AllowWrite = 0
AllowQuery = 0
"@
Set-Content -Path (Join-Path $Output "server.ini") -Value $ServerConfig -Encoding Ascii
$Server2Config = @"
[BakaShrot]
PrivateKey = $($Server2Keys.PrivateKey)
Node = server2
; Other ids than server's, same names: replication goes by name.
[BakaShrot.Topics]
$(Format-Names $Topics2)
[BakaShrot.Nodes]
$(Format-Names $Nodes2)
[BakaShrot.Server]
Host = $ServerHost
Port = $Server2Port
ReplicatingTopics = logs;sensors
NetThreadPoolSize = 4
DatabasePath = ./server2.sqlite
PeerRetryDelay = $PeerRetryDelay
PeerBatchSize = $PeerBatchSize
[BakaShrot.Peer.server]
Host = $ServerHost
Port = $ServerPort
Node = server1
PublicKey = $($ServerKeys.PublicKey)
; The peer only replicates, it has no business reading or writing as a client.
[BakaShrot.Client.server]
PublicKeys = $($ServerKeys.PublicKey)
Node = server1
AllowRead = 0
AllowWrite = 0
AllowQuery = 0
; No raw queries through this server.
[BakaShrot.Client.local]
PublicKeys = $($ClientKeys.PublicKey)
Node = local2
DefaultTopic = logs
PushTopics = logs;sensors
ReadTopics = *
AllowRead = 1
AllowWrite = 1
AllowQuery = 0
"@
Set-Content -Path (Join-Path $Output "server2.ini") -Value $Server2Config -Encoding Ascii
$Client2Config = @"
[BakaShrot]
PrivateKey = $($ClientKeys.PrivateKey)
PublicKey = $($ClientKeys.PublicKey)
Node = local2
ServerHost = $ServerHost
ServerPort = $Server2Port
ServerPublicKey = $($Server2Keys.PublicKey)
"@
Set-Content -Path (Join-Path $Output "client2.ini") -Value $Client2Config -Encoding Ascii
# Read by ClientExample.exe.
$ClientConfig = @"
[BakaShrot]
PrivateKey = $($ClientKeys.PrivateKey)
PublicKey = $($ClientKeys.PublicKey)
Node = local
ServerHost = $ServerHost
ServerPort = $ServerPort
ServerPublicKey = $($ServerKeys.PublicKey)
"@
Set-Content -Path (Join-Path $Output "client.ini") -Value $ClientConfig -Encoding Ascii
Write-Host "Wrote Build\server.ini, Build\server2.ini, Build\client.ini and Build\client2.ini"
# 4. Run, from ./Build so ./test.sqlite ends up there too.
Write-Host "Start the second server in another terminal to see them link:"
Write-Host " cd Build; .\BakaShrot_Server.exe server2.ini"
Push-Location $Output
try {
& ".\BakaShrot_Server.exe" "server.ini"
}
finally {
Pop-Location
}
}
finally {
Pop-Location
}