Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.direnv
.envrc
fly.toml
target-sandbox
7 changes: 7 additions & 0 deletions fly/activity-fly-http/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ fn main() -> Result<()> {
let contents = re
.replace_all(&contents, "#[serde(rename_all = \"kebab-case\")]\n$1")
.into_owned();
let re_env = regex::Regex::new(r"(pub env: Option<_rt::Vec)").unwrap();
let contents = re_env
.replace(
&contents,
"#[serde(with = \"crate::machine::env_serde\", default)]\n $1",
)
.into_owned();
std::fs::write(&path, contents)?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion fly/activity-fly-http/fly-http-machine-config.json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cat <<EOF
"protocol": "tcp"
}
],
"env": [ ["key", "val"], ["other", "otherval"] ],
"env": [ ["key", "val"], ["other", null] ],
"files": [
{"guest-path":"/etc/info.txt", "raw-value":"$(echo -n "hello" | base64)"}
],
Expand Down
244 changes: 150 additions & 94 deletions fly/activity-fly-http/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::generated::exports::obelisk_flyio::activity_fly_http::machines::{
ExecConfig, ExecResponse, Guest, Machine, MachineConfig,
};
use crate::generated::obelisk_flyio::activity_fly_http::regions::Region;
use crate::machine::ser::MachineConfigSer;
use crate::wstd_util::JsonRequest as _;
use crate::{API_BASE_URL, AppName, Component, MachineId, request_with_api_token};
use anyhow::{Context, anyhow, bail, ensure};
Expand All @@ -14,62 +13,54 @@ use serde::Serialize;
use wstd::http::{Body, Client, Method, StatusCode};
use wstd::runtime::block_on;

pub(crate) mod ser {
use std::collections::HashMap;
pub(crate) mod env_serde {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::BTreeMap;

use crate::generated::exports::obelisk_flyio::activity_fly_http::machines::{
ExecResponse, FileConfig, GuestConfig, InitConfig, MachineConfig, MachineRestart, Mount,
ServiceConfig, StopConfig,
};
use crate::generated::obelisk_flyio::activity_fly_http::regions::Region;
use serde::{Deserialize, Serialize};
type EnvVarsVec = Vec<(String, Option<String>)>;

// Fix env var serialization.
// WIT: `option<list<tuple<string, string>>>`
// Expected `{"key1":"val1",...}`
// TODO: Remove when `map` type is implemented.
#[derive(Serialize, Debug)]
pub(crate) struct MachineConfigSer {
pub image: String,
pub guest: Option<GuestConfig>,
/// Destroy the VM after first exec
pub auto_destroy: Option<bool>,
pub init: Option<InitConfig>,
pub env: Option<HashMap<String, String>>,
pub restart: Option<MachineRestart>,
pub stop_config: Option<StopConfig>,
pub mounts: Option<Vec<Mount>>,
pub services: Option<Vec<ServiceConfig>>,
pub files: Option<Vec<FileConfig>>,
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<EnvVarsVec>, D::Error>
where
D: Deserializer<'de>,
{
let map: Option<BTreeMap<String, Option<String>>> = Option::deserialize(deserializer)?;
Ok(map.map(|m| m.into_iter().collect()))
}

impl From<MachineConfig> for MachineConfigSer {
fn from(value: MachineConfig) -> Self {
MachineConfigSer {
image: value.image,
guest: value.guest,
auto_destroy: value.auto_destroy,
init: value.init,
env: value.env.map(HashMap::from_iter),
restart: value.restart,
stop_config: value.stop_config,
mounts: value.mounts,
services: value.services,
files: value.files,
pub fn serialize<S>(value: &Option<EnvVarsVec>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
None => serializer.serialize_none(),
Some(pairs) => {
let map: BTreeMap<&str, Option<&str>> = pairs
.iter()
.map(|(k, v)| (k.as_str(), v.as_deref()))
.collect();
map.serialize(serializer)
}
}
}
}

pub(crate) mod ser {
use crate::generated::exports::obelisk_flyio::activity_fly_http::machines::{
ExecResponse, MachineConfig,
};
use crate::generated::obelisk_flyio::activity_fly_http::regions::Region;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Debug)]
pub(crate) struct MachineCreateRequestSer {
pub(crate) name: String,
pub(crate) config: MachineConfigSer,
pub(crate) config: MachineConfig,
pub(crate) region: Option<Region>,
}

#[derive(Serialize, Debug)]
pub(crate) struct MachineUpdateRequestSer {
pub(crate) config: MachineConfigSer,
pub(crate) config: MachineConfig,
pub(crate) region: Option<Region>,
}

Expand Down Expand Up @@ -169,7 +160,7 @@ async fn create(
{
let request_payload = MachineCreateRequestSer {
name: machine_name,
config: MachineConfigSer::from(machine_config),
config: machine_config,
region,
};
let url = format!("{API_BASE_URL}/apps/{app_name}/machines");
Expand Down Expand Up @@ -210,7 +201,7 @@ async fn update(
) -> Result<(), anyhow::Error> {
{
let request_payload = MachineUpdateRequestSer {
config: MachineConfigSer::from(machine_config),
config: machine_config,
region,
};
let url = format!("{API_BASE_URL}/apps/{app_name}/machines/{machine_id}");
Expand Down Expand Up @@ -469,59 +460,124 @@ mod tests {
#[test]
fn machine_deserialization() {
let json = r#"
{
"id": "080155df097248",
"name": "machine",
"state": "started",
"region": "ams",
"instance_id": "01K4SR42ZPDHHCN70QNZKVPK48",
"private_ip": "fdaa:0:fcc8:a7b:32c:3a59:29d5:2",
"config": {
"init": {
"swap_size_mb": 256
},
"guest": {
"cpu_kind": "shared",
"cpus": 1,
"memory_mb": 256
},
"image": "getobelisk/obelisk:0.24.1-ubuntu",
"restart": {
"policy": "on-failure"
}
{
"id": "d892d51f691378",
"name": "obelisk",
"state": "created",
"region": "ams",
"instance_id": "01KN183BZYXG5QCDBEPMFHE84Y",
"private_ip": "fdaa:0:fcc8:a7b:624:3e5c:9e8d:2",
"config": {
"env": {
"OBELISK__API__LISTENING_ADDR": "",
"OBELISK__EXTERNAL__LISTENING_ADDR": "",
"OBELISK__WEBUI__LISTENING_ADDR": null
},
"init": {
"entrypoint": [
"/usr/bin/env",
"bash",
"/usr/local/bin/litestream-entrypoint.sh"
],
"swap_size_mb": 256
},
"guest": {
"cpu_kind": "shared",
"cpus": 1,
"memory_mb": 256
},
"mounts": [
{
"encrypted": true,
"path": "/volume",
"size_gb": 1,
"volume": "vol_rk19ee7jnp5dmpk4",
"name": "db"
}
],
"services": [
{
"protocol": "tcp",
"internal_port": 9091,
"ports": [
{
"port": 444,
"handlers": [
"tls"
]
}
],
"force_instance_key": null
},
"incomplete_config": null,
"image_ref": {
"registry": "docker-hub-mirror.fly.io",
"repository": "getobelisk/obelisk",
"tag": "0.24.1-ubuntu",
"digest": "sha256:041f936be0d2494aca338e43efe052ee087c1e2520385c6f4640efa9e92ab06a",
"labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "24.04"
}
{
"protocol": "tcp",
"internal_port": 9090,
"ports": [
{
"port": 443,
"handlers": [
"tls"
]
}
],
"force_instance_key": null
}
],
"image": "getobelisk/obelisk:0.36.1-ubuntu-litestream",
"files": [
{
"guest_path": "/etc/obelisk/obelisk.toml",
"raw_value": "W1thY3Rpdml0eV93YXNtXV0KZW52X3ZhcnMgPSBbIkZMWV9BUElfVE9LRU4iXQpsb2NhdGlvbiA9ICJvY2k6Ly9kb2NrZXIuaW8vZ2V0b2JlbGlzay9jb21wb25lbnRzX2ZseV9hY3Rpdml0eV9mbHlfaHR0cDoyMDI2LTAzLTMwQHNoYTI1NjpmZWYwNzUzMTEyMGUxMGE4ZTU5ZWRiYjQ5MjdkY2ZmZmFlNjhhZWQ2OTI1ZDc2ZWE0M2M4ZTAyZTMwNTg0MTYxIgptYXhfcmV0cmllcyA9IDYKbmFtZSA9ICJhY3Rpdml0eV9mbHlfaHR0cCIKCltbYWN0aXZpdHlfd2FzbS5hbGxvd2VkX2hvc3RdXQptZXRob2RzID0gIioiCnBhdHRlcm4gPSAiKjovLyo6KiIKClthY3Rpdml0eV93YXNtLmV4ZWMubG9ja19leHBpcnldCnNlY29uZHMgPSAxNQoKW1thY3Rpdml0eV93YXNtXV0KbG9jYXRpb24gPSAib2NpO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IndlYmhvb2tfaGVhbHRoY2hlY2siCnJvdXRlcyA9IFsiIl0KCltbd29ya2Zsb3dfd2FzbV1dCmxvY2F0aW9uID0gIm9jaTovL2RvY2tlci5pby9nZXRvYmVsaXNrL2NvbXBvbmVudHNfZmx5aW9fb2JlbGlza19kZXBsb3llcl9mbHlpbzoyMDI2LTAzLTMwQHNoYTI1NjowN2I1ZjI0OTJhM2Y0MWEzNmZiZTc0Y2RhODAxYTdhMGMyOTFhOTg2Zjk5ZDBjMWQwODY2YzQzYmY4OWQ3YWEyIgpuYW1lID0gIm9iZWxpc2tfZGVwbG95ZXJfZmx5aW8iCg=="
},
"created_at": "2025-09-10T12:03:04Z",
"updated_at": "2025-09-10T12:03:07Z",
"events": [
{
"id": "01K4SR45V7PBDQ7HBHEAJ6C9YA",
"type": "start",
"status": "started",
"request": {},
"source": "flyd",
"timestamp": 1757505787751
},
{
"id": "01K4SR432JJXA85KC2RB63ANTA",
"type": "launch",
"status": "created",
"source": "user",
"timestamp": 1757505784914
}
],
"host_status": "ok"
}
{
"guest_path": "/etc/obelisk/server.toml",
"raw_value": "W2FwaV0KbGlzdGVuaW5nX2FkZHIgPSAiWzo6XTo1MDA1IgoKW2RhdGFiYXNlLnNxbGl0ZV0KZGlyZWN0b3J5ID0gIi92b2x1bWUvb2JlbGlzay1zcWxpdGUiCgpbZGF0YWJhc2Uuc3FsaXRlLnByYWdtYV0KY2FjaGVfc2l6ZSA9ICIzMDAwIgoKW1todHRwX3NlcnZlcl1dCmxpc3RlbmluZ19hZGRyID0gIjAuMC4wLjA6OTA5MSIKbmFtZSA9ICJoZWFsdGhjaGVja19zZXJ2ZXIiCgpbbG9nLmZpbGVdCmRpcmVjdG9yeSA9ICIvdmFyL2xvZyIKZW5hYmxlZCA9IHRydWUKcHJlZml4ID0gIm9iZWxpc2subG9nIgp0YXJnZXQgPSB0cnVlCgpbd2FzbV0KY2FjaGVfZGlyZWN0b3J5ID0gIi92b2x1bWUvd2FzbSIKcGFyYWxsZWxfY29tcGlsYXRpb24gPSBmYWxzZQoKW3dhc20uY29kZWdlbl9jYWNoZV0KZGlyZWN0b3J5ID0gIi92b2x1bWUvY29kZWdlbiIKClt3ZWJ1aV0KbGlzdGVuaW5nX2FkZHIgPSAiWzo6XTo4MDgwIgo="
},
{
"guest_path": "/usr/local/bin/litestream-entrypoint.sh",
"raw_value": "CiMhL3Vzci9iaW4vZW52IGJhc2gKCnNldCAtZXVvIHBpcGVmYWlsCgpsaXRlc3RyZWFtIHJlc3RvcmUgLWlmLXJlcGxpY2EtZXhpc3RzIC0tY29uZmlnIC9ldGMvbGl0ZXN0cmVhbS55bWwgL3ZvbHVtZS9vYmVsaXNrLXNxbGl0ZS9vYmVsaXNrLnNxbGl0ZQpleGVjIGxpdGVzdHJlYW0gcmVwbGljYXRlIC0tY29uZmlnIC9ldGMvbGl0ZXN0cmVhbS55bWwgLS1leGVjICdvYmVsaXNrIHNlcnZlciBydW4gLS1zZXJ2ZXItY29uZmlnIC9ldGMvb2JlbGlzay9zZXJ2ZXIudG9tbCAtLWRlcGxveW1lbnQgL2V0Yy9vYmVsaXNrL29iZWxpc2sudG9tbCcKICAgICAgICA="
},
{
"guest_path": "/etc/litestream.yml",
"raw_value": "CmRiczoKICAtIHBhdGg6ICIvdm9sdW1lL29iZWxpc2stc3FsaXRlL29iZWxpc2suc3FsaXRlIgogICAgcmVwbGljYToKICAgICAgdXJsOiAiczM6Ly9saXRlc3RyZWFtLWJ1Y2tldC40OGU2NWVkZjc1OTQzOC52bS5pdHNlbGYuaW50ZXJuYWw6OTAwMC9saXRlc3RyZWFtL29iZWxpc2siCiAgICAgIGFjY2Vzcy1rZXktaWQ6ICJtaW5pb2FkbWluIgogICAgICBzZWNyZXQtYWNjZXNzLWtleTogIm1pbmlvYWRtaW4iCg=="
}
],
"restart": {
"policy": "on-failure",
"max_retries": 5
}
},
"incomplete_config": null,
"image_ref": {
"registry": "docker-hub-mirror.fly.io",
"repository": "getobelisk/obelisk",
"tag": "0.36.1-ubuntu-litestream",
"digest": "sha256:4c78dcb79c7435c460a1f988f8beb4787e595dbb10456642deff70c613c31894",
"labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "24.04"
}
},
"created_at": "2026-03-31T06:08:08Z",
"updated_at": "2026-03-31T06:08:08Z",
"events": [
{
"id": "01KN183C327XW9SZYH8P8E3D16",
"type": "launch",
"status": "created",
"source": "user",
"timestamp": 1774937288802
},
{
"id": "01KN183BZZKK58758ZWX292X93",
"type": "launch",
"status": "pending",
"source": "flyd",
"timestamp": 1774937288703
}
],
"host_status": "ok"
}
"#;
let machine: Machine = serde_json::from_str(json).unwrap();
assert_debug_snapshot!(machine)
Expand Down
Loading
Loading