Modules - MaynardMiner/SWARM GitHub Wiki
Incorporating Your Own Modules Into SWARM
.psm1 modules
Powershell uses a system of modules to add/remove functions and features to its codebase. SWARM takes full advantage of modules, and allows users to create their own, and add them to SWARM.
Use/Case Demonstration.
Lets say you wish to document NVIDIA GPU activity, over time. You would like to create a script that will occasionally record nvidia-smi
and write it to file. Instead of writing a separate script that runs independently of SWARM, SWARM gives you the ability to add this codebase directly to SWARM via the modules
command.
Here is the code you have made:
invoke-expression "nvidia-smi" | Tee-Object -Variable NVSMI | Out-Null
$NVSMI | Out-Host | Out-File ".\nvsmi_data.txt" -Append
You would like this to run in a loop.
Incorporating Your Code Into SWARM.
The above code can be stored in a .psm1 file. For our example, we will save the above code to "nvsmi.psm1". It is important to note that you should not use "_" or "-" in the name of the file, or this may cause issues in linux.
Adding the code to SWARM will involve using the modules
command. There is two method to use the modules command:
1.) "looping" - Code will run at the start of each new loop (Database begins to load). 2.) "single" - Code will run once at the start of SWARM.
Because you wish your code to run periodically, we will use the "looping" command.
modules "C:\nvsmi.ps1" looping
You code is now loaded into SWARM, and SWARM will restart itself with your module now loaded. A copy of your model will be placed in the ".\build\global" folder.
Bash scripting and powershell
Powershell Core can run commands you would run in linux. Here is an example:
.> pwsh
.> netstat | grep "TIME_WAIT"
tcp 0 0 localhost:36217 localhost:50003 TIME_WAIT
tcp 0 0 localhost:10001 localhost:46752 TIME_WAIT
tcp 0 0 localhost:38461 localhost:46002 TIME_WAIT
tcp 0 0 localhost:6099 localhost:46004 TIME_WAIT
.>
As you can see, you can creates scripts very similar to bash scripts. The other unique feature of Powershell, is that the commands can be converted to variables that Powershell can use:
pwsh
.> Invoke-Expression "netstat | grep `"TIME_WAIT`"" | Tee-Object -Variable netstat | Out-Null
.> ### Now we test the variable:
.> $netstat | Select-String "50003"
tcp 0 0 localhost:36217 localhost:50003 TIME_WAIT
.>
As you can see in the above- The object can now be manipulated with Powershell.
$Global variables
SWARM has been designed using as many $Global variables as possible. Global variables extend to all modules, meaning you are able to manipulate actual running variables in SWARM while it is running.
if($global:miners.count -gt 0) {
$MinerNames = $global:Miners.Name
}
In the above, you are pulling the active miner list in SWARM, and selecting the name from each miner.
RunSpace
Since the module is loaded either at the start of SWARM, or during the start of the loop- If you wish to have your script run simultaneously with SWARM, you will have to establish a separate thread by creating a runspace for it. Unfortunately $Global variables do not extend to a runspace in most cases. However, you can make use of synchronized hashtables to have your script manipulate values of SWARM's script while it is running. But this will take modification of SWARM codebase itself. This is an advanced process, but possible.