From 5f3de290f343cdaa64834357248077b64f9e7ac1 Mon Sep 17 00:00:00 2001 From: Kieron Morris Date: Sun, 9 Mar 2025 13:01:10 +0000 Subject: [PATCH] DevOps Workflow Improvements - `VirtualBox-Wrapper.ps1` now takes 'up' or 'down' as opposed to a machine name. This allows start/stop of a virtualmachine. - `VirtualBox-Wrapper.ps1` now relies on a gitignored `localenv.json` to work. - `VirtualBox-Wrapper.ps1` can also optionally monitor the log file generated from the serial adapter in VirtualBox. - `readme.md` updated to provide instructions on how to populate the `localenv.json` file. - `tasks.json` updated to have a "Clean" task to --remove-orphans, the Build task depends on this. - `tasks.json` updated to have a "Close VirtualBox" task, this runs the `virtualbox-wrapper.ps1` in 'down' mode. The Build task depends on this. - `launch.json` updated to run the `VirtualBox-Wrapper.ps1` with the "-Command up" argument, instead of machine name. - .gitignore updated to ignore any instances of `localenv.json`. --- .gitignore | 3 +- .vscode/launch.json | 2 +- .vscode/tasks.json | 26 ++++++++++++++++-- docker-compose.yml | 1 - readme.md | 27 ++++++++++-------- virtualbox-wrapper.ps1 | 62 +++++++++++++++++++++++++++++++++++------- 6 files changed, 93 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 99ad6b47..25406f93 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,4 @@ /*.sh~ /*.img src/include/asuro.pas -.vscode/launch.json -.vscode +localenv.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 56f57f0f..5178fc41 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "PowerShell", "preLaunchTask": "Build", "script": "${workspaceFolder}/virtualbox-wrapper.ps1", - "args": ["-MachineName", "7d395c96-891c-4139-b77d-9b6b144b0b93"], + "args": ["-Command", "up"], "cwd": "${workspaceFolder}", } ] diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 3f74eb24..9cb0382a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,14 +9,18 @@ "command": "docker-compose", "args": [ "run", - "builder" + "builder", ], "type": "shell", "problemMatcher": [], "group": { "kind": "build", "isDefault": true - } + }, + "dependsOn": [ + "Close VirtualBox", + "Clean" + ] }, { "label": "Build (Builder)", @@ -26,6 +30,24 @@ "builder" ], "type": "shell" + }, + { + "label": "Clean", + "command": "docker-compose", + "args": [ + "down", + "--remove-orphans" + ], + "type": "shell" + }, + { + "label": "Close VirtualBox", + "command": "./virtualbox-wrapper.ps1", + "args": [ + "-Command", + "down" + ], + "type": "shell" } ] } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b5c19202..845189bd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,3 @@ -version: "3.9" services: builder: build: . diff --git a/readme.md b/readme.md index afb9b984..1ab8b627 100644 --- a/readme.md +++ b/readme.md @@ -50,23 +50,26 @@ We welcome everyone to give building/breaking/fixing/shooting Asuro a go, feel f ```xml ``` - Copy the uuid, in our case `7d395c96-891c-4139-b77d-9b6b144b0b93` and replace the uuid found in `.vscode\launch.json` under `args`, so that it looks something like this: + Copy the uuid, in our case `7d395c96-891c-4139-b77d-9b6b144b0b93` & create a `localenv.json` file in the project root with the following content: ```json { - "configurations": [ - { - "name":"Run", - "request": "launch", - "type": "PowerShell", - "preLaunchTask": "Build", - "script": "${workspaceFolder}/virtualbox-wrapper.ps1", - "args": ["-MachineName", "7d395c96-891c-4139-b77d-9b6b144b0b93"], - "cwd": "${workspaceFolder}", - } - ] + "VirtualBox":{ + "MachineName":"" + } } ``` This will allow VSCode to automatically launch VirtualBox once Asuro has been compiled. + + You can also enable the serial adapter "COM1" in mode "Raw File", give it a path, and provide this path in the `localenv.json` as follows: + ```json + { + "VirtualBox" : { + "MachineName": "", + "LogLocation": "Fully\\Qualified\\Path\\To\\Your\\Log\\File" + } + } + ``` + This will allow you to see the console output from Asuro in your host terminal. 13. Open your project folder in VSCode, use CTRL+SHIFT+B to build & F5 to build + run in VBox. 14. Congratulations! You can now play with Asuro! diff --git a/virtualbox-wrapper.ps1 b/virtualbox-wrapper.ps1 index fdd460f2..7fd2bf04 100644 --- a/virtualbox-wrapper.ps1 +++ b/virtualbox-wrapper.ps1 @@ -1,16 +1,58 @@ +<# +You need a local git-ignored localenv.json file with the following content: +{ + "virtualbox": { + "MachineName": "your-machine-name or guid" + } +} +#> + param ( - $MachineName + [Parameter(Mandatory=$true)] + [ValidateSet('up', 'down')] + [String]$Command ) -VBoxManage.exe startvm $MachineName +$Config = Get-Content .\localenv.json | ConvertFrom-Json +$MachineName = $Config.virtualbox.MachineName +$LogLocation = $Config.virtualbox.LogLocation +$LogOutputEnabled = $LogLocation -ne $null -$running=$true -while($running) { - Start-Sleep -Seconds 1 - $status=(VBoxManage.exe list runningvms) - if($status) { - $running=$status.contains($MachineName) - } else { - $running=$false +if ($Command -eq 'up') { + + if($LogOutputEnabled) { + Clear-Content $LogLocation } + + $MonitorJob = Start-Job -ArgumentList $MachineName -ScriptBlock { + param($MachineName) + Write-Output "Starting $MachineName" + VBoxManage.exe startvm $MachineName + $running=$true + while($running) { + $status=(VBoxManage.exe list runningvms) + if($status) { + $running=$status.contains($MachineName) + } else { + $running=$false + } + } + } + + if($LogOutputEnabled) { + $LogJob = Start-Job -ArgumentList $LogLocation -ScriptBlock { + param($LogLocation) + Get-Content -Path $LogLocation -Wait + } + } + + while($MonitorJob.State -eq 'Running') { + if($LogOutputEnabled) { + Receive-Job $LogJob + } + Receive-Job $MonitorJob + } +} elseif ($Command -eq 'down') { + Write-Output "Stopping $MachineName" + VBoxManage.exe controlvm $MachineName poweroff } \ No newline at end of file