How I Use Plang for System Administration
Plang: A Surprisingly Versatile Tool for System Administration
This post dives into Plang, an intent-based programming language that interprets natural language. For more, visit plang.is or get started here.
Plang is a powerful tool, especially when it comes to system administration. Its ability to operate at the programming layer while leveraging various tools gives developers—and sysadmins —an impressive level of control and efficiency.
In system administration, scripts simplify repetitive tasks, and I use Plang specifically to deploy apps to servers. My Plang apps are pretty straightforward, so they don’t need the heavy-duty CI/CD pipeline, which would just add unnecessary complexity.
So, how does it work?
The Plan
My goal is to take the app
folder from my development machine and set it up on a Linux server. Here’s what needs to happen:
Zip the
app
folder toapp.zip
, excluding any.db
and*.sqlite
files.Connect to my Linux server via SFTP.
Upload
app.zip
.Clear out any old
.build
files to avoid using outdated code.Unzip
app.zip
on the server.Set the correct file permissions.
Optionally update the Plang runtime.
Confirm the Plang runtime is working.
When you’re doing this setup for the first time, it's common to miss a step or two—don’t worry. The flexibility of Plang makes it easy to adjust on the fly.
The Deployment
Let’s walk through the deployment by setting up a Deploy.goal
file on my computer, and the first line is simply Deploy
:
Deploy
Zipping the App
We’ll start by creating a zip file of the app directory, which is in the same location as Deploy.goal
:
- zip "./" to "deploy/app.zip", exclude ".db", "*.sqlite", overwrite file
Let's stop here. If you are familiar with programming, you will probably be thinking what is the syntax here? How do I know how to structure this zip command, but here is the thing. Plang does not have strict syntax, it's all about what would you like to happen. Instead of `zip "./" to "deploy/app.zip"`, you say `compress the folder I am in and save it to app.zip`. Because `plang` takes your intent and converts it to code. Let's continue.
Connecting via SFTP
Next, I connect to the server using SFTP. I’ve set up a private key with a passphrase for secure access:
- read file '/events/key', into %sshPrivateKey%
- connect to sftp,
host: myhost.server.com, port: 22, username: root,
private key: %sshPrivateKey%, private key passphrase: %Settings.PrivateKeyPassphrase%
Now that we’re logged into the server, we can start deploying.
Uploading and Setting Up
First, let’s upload the zip file:
- upload 'deploy/app.zip' through sftp to '/home/plang/app.zip'
After that, we’ll run a few Linux commands to clean up any previous .build
folder and unzip the new code:
- run ssh command, "rm -rf /home/plang/app/.build"
- run ssh command, 'unzip -o /home/plang/app.zip -d /home/plang/app'
Updating the Plang Runtime
For flexibility, I’ve added an option to update the Plang runtime. If I run the script with plang=1
, it triggers the update:
- if %plang% = 1 then SetupPlang
Now, let’s create the SetupPlang
goal.
The SetupPlang Goal
The SetupPlang
goal takes care of a few critical steps:
Downloads the latest version of Plang for Linux.
Unzips
plang.zip
.Sets execution permissions for
plang
andselenium-manager
(I use a browser in my app).Runs
plang --version
to validate the installation and manages any errors (like creating a symlink or installing i18n).Restarts the service if it’s running or notifies me if it isn’t installed.
SetupPlang
- run ssh command, 'wget -O /home/plang/plang.zip https://github.com/PLangHQ/plang/releases/latest/download/plang-linux-x64.zip'
- run ssh command, 'unzip -o /home/plang/plang.zip -d /home/plang/plang'
- run ssh command, 'chmod +x /home/plang/plang/plang'
- run ssh command, 'chmod +x /home/plang/plang/selenium-manager/linux/selenium-manager'
- run ssh command, 'plang --version'
on error message 'No such file or', call CreateSymbolicLink then retry
on error message 'command not found', call CreateSymbolicLink then retry
on error message contains 'Couldn't find a valid ICU package', call InstallICU then retry
- run ssh command, 'sudo systemctl restart plang', write to %serviceRestart%
on error message 'plang.service not found', call ServiceNotInstalled ignore error
- write out %serviceRestart%
In the case of errors, I have additional commands to handle them:
CreateSymbolicLink
- write out 'Creating symbolic link'
- run ssh command, 'sudo ln -s /home/plang/plang/plang /usr/local/bin/plang'
on error message contains 'File exists', ignore the error
InstallICU
- write out 'Installing ICU'
- run ssh command 'sudo dnf install -y libicu'
ServiceNotInstalled
- write out 'Plang service not installed'
Finally, to confirm the Plang runtime is installed, I run:
- run ssh command, 'plang --version', write to %plangVersion%
- write out "App updated at %Now% - %plangVersion%"
Running and Updating
Once the code is ready, I build it:
plang build
And then, to deploy both the app and Plang runtime, I run:
plang Deploy plang=1
If I only need to update the app, I just execute:
plang Deploy
Here’s an example of the output:
App updated at 29.10.2024 10:57:05 - plang version: 0.15.3.0
Full Code for Reference
Deploy
- zip "./" to "deploy/app.zip", exclude ".db", "*.sqlite", overwrite file
- read file '/events/key', into %sshPrivateKey%
- connect to sftp,
host: myhost.server.com, port: 22, username: root,
private key: %sshPrivateKey%, private key passphrase: %Settings.PrivateKeyPassphrase%
- upload 'deploy/app.zip' through sftp to '/home/plang/app.zip'
- run ssh command, "rm -rf /home/plang/app/.build"
- run ssh command, 'unzip -o /home/plang/app.zip -d /home/plang/app'
- if %plang% = 1 then SetupPlang
- run ssh command, 'plang --version', write to %plangVersion%
- write out "App updated at %Now% - %plangVersion%"
SetupPlang
- run ssh command, 'wget -O /home/plang/plang.zip https://github.com/PLangHQ/plang/releases/latest/download/plang-linux-x64.zip'
- run ssh command, 'unzip -o /home/plang/plang.zip -d /home/plang/plang'
- run ssh command, 'chmod +x /home/plang/plang/plang'
- run ssh command, 'chmod +x /home/plang/plang/selenium-manager/linux/selenium-manager'
- run ssh command, 'plang --version'
on error message 'No such file or', call CreateSymbolicLink then retry
on error message 'command not found', call CreateSymbolicLink then retry
on error message contains 'Couldn't find a valid ICU package', call InstallICU then retry
- run ssh command, 'sudo systemctl restart plang', write to %serviceRestart%
on error message 'plang.service not found', call ServiceNotInstalled ignore error
- write out %serviceRestart%
CreateSymbolicLink
- write out 'Creating symbolic link'
- run ssh command, 'sudo ln -s /home/plang/plang/plang /usr/local/bin/plang'
on error message contains 'File exists', ignore the error
InstallICU
- write out 'Installing ICU'
- run ssh command 'sudo dnf install -y libicu'
ServiceNotInstalled
- write out 'Plang service not installed'
Using Plang in system administration is a real timesaver, making deployment as efficient and straightforward as possible.
Benefits
Using Plang for deployment is a game-changer. It's simple, readable, and just works. No messing around with clunky syntax—each step is laid out in plain language, making it dead easy to follow and tweak whenever I need. Plang's modular setup breaks down tasks into bite-sized pieces, so fixing, adjusting, or handling errors is a breeze. And the built-in retry logic? Absolutely essential—it catches issues on the fly, keeping things from blowing up mid-deploy.
Plang’s variable management is just as smooth. I can stash command outputs and use them wherever I need, keeping everything flowing without extra steps. Need to log deployment records? Plang’s got you covered with built-in database support, no extra setup or fuss. In short, Plang slashes the usual code bloat and complexity, leaving me with a clean, powerful setup that actually saves time.
More Information
Interested in learning more about Plang? Here are some useful resources to get started:
Todo Example to build a simple web service
Explore the GitHub repo for source code
Join our Discord Community for discussions and support
Or chat with the Plang Assistant to get help with code generation