- Rust 99.3%
- Just 0.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| examples | ||
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| justfile | ||
| README.md | ||
Deployers
An infrastructure-as-code solution where your roles & playbooks are literally made with code.
The advantages of doing so is that you get an extra layer of validation thanks to the compiler. You also get access to programming primitives such as if/else or loops with a familiar syntax.
Additionally everything being code this means that you can use the built-in commands, or you can develop your own with the same code that makes your playbooks and roles. A role can consist of calls to built-in commands, but it can also offer a set of specific commands that other roles can build upon. As commands are functions, you can create new commands that compose other commands.
Reference
Complete examples can be found in the examples folder.
File manipulations (commands::files)
Built-in file manipulation commands are available via the deployers::commands::files
module.
Setting a text file's content
The write command can be used to set a file's content. It accepts a raw string
rather than a path to a file on the deployment machine so the content can be
manipulated before sending it to the remote server (with templating for example).
let content = include_str!("main.rs");
deployers::commands::files::write(&mut remote_server, content, "~/main.rs")
.await
.expect("set file's content");
Reading a file from the remote
The read command can be used to read the content of a file from the remote server
let content = include_str!("main.rs");
let result = deployers::commands::files::read(&mut remote_server, "~/main.rs")
.await
.expect("read main.rs");
// we're trimming results to be sure
if result.trim() != content.trim() {
panic!("remote read did not yield expect result");
}
Checking if a file exists
The exists command returns true if the target file exists on the remote server,
or false if it does not.
if deployers::commands::files::exists(&mut remove_server, "~/main.rs").await {
println!("the file exists");
}
Checking if a directory exists
The exists_dir command returns true if the target file exists on the remote server,
or false if it does not.
if deployers::commands::files::exists_dir(&mut remove_server, "~/deployers").await {
println!("the directory exists");
}
Deploying larger or binary files
The deploy command can be used to upload a file to the remote path, it uses scp
under the hood and requires the scp on the remote target (but not on the current
machine).
deployers::commands::files::deploy(&mut remove_server, "./README.md", "~/README.md")
.await;
if deployers::commands::files::exists(&mut remove_server, "~/README.md").await {
println!("the file was successfully sent via scp");
}
Moving a file on the remote target
The mv command can be used to move a file (on the remote target) from its old
location to a new one.
deployers::commands::files::mv(remove_server, "~/README.md", "~/LISEZMOI.md")
.await
.expect("move README.md");
if deployers::commands::files::exists(remove_server, "~/LISEZMOI.md").await {
println!("the file was successfully moved");
}
Creating a directory
The mkdir command can be used to create a directory on the remote target. The
It does not throw any error if the directory already exists or if a parent directory
does not exist yet (it will create them recursively).
deployers::commands::files::mkdir(remove_server, "~/deployers")
.await
.expect("mkdir ~/deployers");
if deployers::commands::files::exists_dir(remove_server, "~/deployers").await {
println!("directory was created");
}
Removing files or directories
The rm command can be used to recursively delete files or directories.
deployers::commands::files::rm(remove_server, "~/LISEZMOI.md", false)
.await
.expect("rm LISEZMOI.md");
if deployers::commands::files::exists(remove_server, "~/LISEZMOI.md").await {
panic!("file was not deleted");
}
deployers::commands::files::rm(remove_server, "~/deployers", true)
.await
.expect("rm ~/deployers");
if deployers::commands::files::exists_dir(remove_server, "~/deployers").await {
panic!("directory was not deleted");
}
Packages (commands::packages)
Installing packages (apt-get)
Packages can be installed via apt-get install with the install command.
deployers::commands::packages::install(&mut remove_server, &["nginx", "podman"]).await;
Updating the packages list (apt-get)
The package list can be updated via apt-get update with the update command.
deployers::commands::packages::update(&mut remote_server).await;