Help:Toolforge/Building container images/My first Buildpack Rust tool
This stub webservice is designed to get a sample Rust application installed onto Toolforge using the new build service, as quickly as possible. The application is written using the Rocket framework.
Note that you can do a similar thing even if you are not building a webservice (ex. jobs, etc.), the only difference would be the entrypoints setup in the Procfile
(see Help:Toolforge/Building container images#Procfile).
The guide will teach you how to:
- Create a new tool
- Run a Rust WSGI webservice on Kubernetes
Getting started
Prerequisites
Skills
- Basic knowledge of Rust
- Basic knowledge of SSH
- Basic knowledge of the Unix command line
- Basic knowledge of Git
Accounts
Step-by-step guide
Step 1: Create a new tool account
- Follow the Toolforge quickstart guide to create a Toolforge tool and SSH into Toolforge.
- For the examples in this tutorial,
sample-rust-buildpack-app
is used to indicate places where your unique tool name is used in another command.
- For the examples in this tutorial,
- Make sure to create a git repository for the tool, you can get one like this:
- Log into the toolforge admin page
- Select your tool
- On the left side panel, under
Git repositories
clickcreate repository
- Copy the url in the
Clone
section- There's a private url, that we will use to clone it locally, starting with "git":
git@gitlab.wikimedia.org:toolforge-repos/sample-rust-buildpack-app.git
- And a public one, that we will use to build the app in toolforge, starting with "https":
https://gitlab.wikimedia.org/toolforge-repos/sample-rust-buildpack-app.git
- There's a private url, that we will use to clone it locally, starting with "git":
Step 2: Create a basic Rocket webservice
- What is Rocket?
Rocket is a popular web development framework for Rust.
How to create a basic Rocket webservice
- Clone your tool git repository
You will have to clone the tool repository to be able to add code to it, on your local computer (with git installed) you can run:
laptop:~$ git clone git@gitlab.wikimedia.org:toolforge-repos/sample-rust-buildpack-app.git
laptop:~$ cd sample-rust-buildpack-app
That will create a folder called sample-rust-buildpack-app
. We are going to put the code in that folder.
- Create the default hello world application code
cargo
to manage your project.laptop:~sample-rust-buildpack-app$ cargo init --name sample-rust-buildpack-app --bin
Created binary (application) package
- Overwrite the default rust application with our Rocket hello world
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
- Add the rocket dependency to
Cargo.toml
laptop:~sample-rust-buildpack-app$ cargo add rocket
Updating crates.io index
Adding rocket v0.5.0 to dependencies.
Features:
+ http2
- json
- msgpack
- mtls
- rmp-serde
- secrets
- serde_json
- tls
- uuid
- uuid_
Updating crates.io index
Code on Toolforge must always be licensed under an Open Source Initiative (OSI) approved license. See the Right to fork policy for more information on this Toolforge policy.
- Create the Procfile
The Procfile is based on heroku's procfile, though we don't support all it's features, for now we only use the web
entry point to get the command your server will be start with:
laptop:~sample-rust-buildpack-app$ cat > Procfile << EOF
web: ROCKET_ADDRESS=0.0.0.0 ROCKET_PORT=$PORT ROCKET_KEEP_ALIVE=0 ./target/release/sample-rust-buildpack-app
EOF
- Commit your changes and push
laptop:~sample-rust-buildpack-app$ git add .
laptop:~sample-rust-buildpack-app$ git commit -m "First commit"
laptop:~sample-rust-buildpack-app$ git push origin main
EOF
- Build the image
Now we have to ssh to login.toolforge.org
and start the build for the image:
laptop:~sample-rust-buildpack-app$ ssh login.toolforge.org # or the equivalent with PuTTY
dcaro@tools-sgebastion-10$ become sample-rust-buildpack-app
tools.sample-rust-buildpack-app@tools-sgebastion-10$ toolforge build start https://gitlab.wikimedia.org/toolforge-repos/sample-rust-buildpack-app.git
- Wait for the build to finish
You can check the status of the build like this:
tools.sample-rust-buildpack-app@tools-sgebastion-10:~$ toolforge build show
You have to wait for the status to be ok(Succeeded)
.
- Start the webservice
tools.sample-rust-buildpack-app@tools-sgebastion-10$ toolforge webservice buildservice start --mount=none
Starting webservice.
Once the webservice is started, navigate to https://sample-rust-buildpack-app.toolforge.org
in your web browser, and see a 'Hello World!' message. It might take a few minutes until it is reachable.
Notes
You can see the code used in this example here: https://gitlab.wikimedia.org/toolforge-repos/sample-rust-buildpack-app
Troubleshooting
See Help:Toolforge/Build_Service#Troubleshooting.
See also
Communication and support
Support and administration of the WMCS resources is provided by the Wikimedia Foundation Cloud Services team and Wikimedia movement volunteers. Please reach out with questions and join the conversation:
- Chat in real time in the IRC channel #wikimedia-cloud connect or the bridged Telegram group
- Discuss via email after you have subscribed to the cloud@ mailing list
- Subscribe to the cloud-announce@ mailing list (all messages are also mirrored to the cloud@ list)
- Read the News wiki page
Use a subproject of the #Cloud-Services Phabricator project to track confirmed bug reports and feature requests about the Cloud Services infrastructure itself
Read the Cloud Services Blog (for the broader Wikimedia movement, see the Wikimedia Technical Blog)