Skip to content

Commit

Permalink
feat: add wasm demo using podman
Browse files Browse the repository at this point in the history
Use ubi image, podman and ascii art from podman

Include CI using podman to build the image as well
  • Loading branch information
benoitf committed Oct 25, 2023
1 parent 3914f34 commit 7239a91
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/build-publish-wasm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
#
# Copyright (C) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

name: Build and publish the wasm examples

on:
push:
paths:
- "wasm/**"
- ".github/workflows/build-publish-wasm.yaml"
workflow_dispatch:

jobs:
build:
name: Build all wasm examples
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4

- name: Install qemu dependency
run: |
sudo apt-get update
sudo apt-get install -y qemu-user-static
- name: Update podman
run: |
sudo sh -c "echo 'deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/unstable/xUbuntu_22.04/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list"
curl -L https://build.opensuse.org/projects/devel:kubic:libcontainers:unstable/signing_keys/download?kind=gpg | sudo apt-key add -
sudo apt-get update -qq
sudo apt-get install -y podman
podman version
- name: Build Rust Hello World Wasm Image
id: build-image
run: |
SHORT_SHA1=$(echo ${GITHUB_SHA} | cut -c1-7)
podman build --platform wasi/wasm -t wasm-rust-hello-world -f ./wasm/rust-hello-world/Containerfile ./wasm/rust-hello-world
podman tag wasm-rust-hello-world wasm-rust-hello-world:${SHORT_SHA1}
echo "tags=latest ${SHORT_SHA1}" >> $GITHUB_OUTPUT
- name: Log in to Red Hat Registry
uses: redhat-actions/podman-login@v1
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_PASSWORD }}

- name: list images
run: |
podman images
echo "the tags are ${{ steps.build-image.outputs.tags }}"
- name: Push To quay.io
id: push-to-quay
uses: redhat-actions/push-to-registry@v2
with:
image: wasm-rust-hello-world
tags: ${{ steps.build-image.outputs.tags }}
registry: quay.io/podman-desktop-demo
25 changes: 25 additions & 0 deletions wasm/rust-hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright (C) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

[package]
name = "rust-hello-world"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "rust-hello"
path = "src/main.rs"
38 changes: 38 additions & 0 deletions wasm/rust-hello-world/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Copyright (C) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

# Build using the host platform (and not target platform wasi/wasm)
FROM --platform=$BUILDPLATFORM docker.io/redhat/ubi9-minimal as builder

# install rust and wasm/wasi target
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
&& source "$HOME/.cargo/env" && rustup target add wasm32-wasi

# copy source code
COPY Cargo.toml /app/
COPY src /app/src

# change working directory
WORKDIR /app

# Build
RUN source "$HOME/.cargo/env" && cd /app && cargo build --target wasm32-wasi --release

# now copy the wasm binary and flag it as the entrypoint
FROM scratch
ENTRYPOINT [ "/rust-hello-world.wasm" ]
COPY --from=builder /app/target/wasm32-wasi/release/rust-hello.wasm /rust-hello-world.wasm
41 changes: 41 additions & 0 deletions wasm/rust-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Hello world in Rust

Inspired from https://doc.rust-lang.org/rust-by-example/hello.html
and the C podman hello https://github.com/containers/PodmanHello/blob/main/podman_hello_world.c

It's displaying a Hello World with some ascii art from Máirín Duffy @mairin

In the Containerfile, we build using wasm32-wasi and the target platform of the image is `wasi/wasm`
Then the .wasm binary is copied in a scratch image with architecture `wasi/wasm`



## Build command:

### prerequisites

- A recent podman version (not the v3.x for example on Ubuntu LTS)


### instructions

podman build --platform=wasi/wasm -t wasm-rust-hello-world .


## Run command

### prerequisites

crun-wasm/wasmedge-rt packages need to be installed in the podman machine/host

# on Windows, this is the default now
# on macOS: need to initialize a podman machine using next channel


### running from the previous build step

podman run wasm-rust-hello-world

### running from quay.io image

podman run --platform wasi/wasm quay.io/podman-desktop-demo/wasm-rust-hello-world
46 changes: 46 additions & 0 deletions wasm/rust-hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

fn main() {

// use of strings literal for multi-line string
// https://doc.rust-lang.org/reference/tokens.html#raw-string-literals

// ascii art from Máirín Duffy @mairin
let hello = r#"
!... Hello Podman wasm World ...!
.--"--.
/ - - \
/ (O) (O) \
~~~| -=(,Y,)=- |
.---. /` \ |~~
~/ o o \~~~~.----. ~~
| =(X)= |~ / (O (O) \
~~~~~~~ ~| =(Y_)=- |
~~~~ ~~~| U |~~
Project: https://github.com/containers/podman
Website: https://podman.io
Documents: https://docs.podman.io
Twitter: @Podman_io
"#;
println!("{}", hello);

}

0 comments on commit 7239a91

Please sign in to comment.