Skip to content

armgit / muxin

Public Imported from GitHub
0

All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.

Cloud machines: only regions and sizes DigitalOcean offers

Adib Hanna48a38c2c6ba0
diff --git a/src/cloud/digitalocean.rs b/src/cloud/digitalocean.rs
index 7765c68..a4b1276 100644
--- a/src/cloud/digitalocean.rs
+++ b/src/cloud/digitalocean.rs
@@ -33,7 +33,7 @@ pub struct Region {
pub sizes: Vec<String>,
}
-#[derive(Debug, Clone, Serialize)]
+#[derive(Debug, Clone, Default, Serialize)]
pub struct Size {
pub slug: String,
pub vcpus: u32,
diff --git a/src/cloud/mod.rs b/src/cloud/mod.rs
index ae546c7..a24e60c 100644
--- a/src/cloud/mod.rs
+++ b/src/cloud/mod.rs
@@ -50,10 +50,22 @@ pub struct CloudCreate {
pub install: Option<Vec<String>>,
}
-/// One region per city, as people know them ("New York", not NYC1/NYC2/NYC3):
-/// (city, region slug), the city's newest datacenter, Americas first, then
-/// Europe, then Asia and Oceania.
-pub fn cities(regions: &[Region]) -> Vec<(String, String)> {
+/// Basic droplets on regular CPUs, the ones Muxin offers: the premium AMD
+/// and Intel twins of each size are left out.
+pub fn regular_size(slug: &str) -> bool {
+ slug.starts_with("s-") && !slug.ends_with("-amd") && !slug.ends_with("-intel")
+}
+
+/// The sizes on offer in a region, cheapest first (as DigitalOcean lists them).
+pub fn offered<'a>(sizes: &'a [Size], region: &str) -> impl Iterator<Item = &'a Size> {
+ let region = region.to_string();
+ sizes.iter().filter(move |s| regular_size(&s.slug) && s.regions.contains(&region))
+}
+
+/// One region per city, to pick from: the one with the most sizes on offer
+/// (DigitalOcean stops offering sizes in some datacenters, New York's older
+/// ones among them), in a fixed order of cities. Cities with none are left out.
+pub fn cities(regions: &[Region], sizes: &[Size]) -> Vec<(String, String)> {
const ORDER: &[&str] = &[
"New York", "Toronto", "Atlanta", "Richmond", "San Francisco", "Kansas City", "Memphis",
"London", "Amsterdam", "Frankfurt",
@@ -61,19 +73,27 @@ pub fn cities(regions: &[Region]) -> Vec<(String, String)> {
];
let city_of = |r: &Region| r.name.trim_end_matches(|c: char| c.is_ascii_digit() || c == ' ').trim().to_string();
let number = |slug: &str| slug.trim_start_matches(|c: char| c.is_ascii_alphabetic()).parse::<u32>().unwrap_or(0);
- let mut best: Vec<(String, String)> = Vec::new();
+ // (city, slug, sizes on offer there)
+ let mut best: Vec<(String, String, usize)> = Vec::new();
for r in regions {
+ let count = offered(sizes, &r.slug).count();
+ if count == 0 {
+ continue;
+ }
let city = city_of(r);
let city = if city.is_empty() { r.slug.to_uppercase() } else { city };
- match best.iter_mut().find(|(c, _)| *c == city) {
- Some(entry) if number(&r.slug) > number(&entry.1) => entry.1 = r.slug.clone(),
+ match best.iter_mut().find(|(c, _, _)| *c == city) {
+ Some(entry) if (count, number(&r.slug)) > (entry.2, number(&entry.1)) => {
+ entry.1 = r.slug.clone();
+ entry.2 = count;
+ }
Some(_) => {}
- None => best.push((city, r.slug.clone())),
+ None => best.push((city, r.slug.clone(), count)),
}
}
let rank = |c: &str| ORDER.iter().position(|o| *o == c).unwrap_or(ORDER.len());
- best.sort_by_key(|(c, _)| rank(c));
- best
+ best.sort_by_key(|(c, _, _)| rank(c));
+ best.into_iter().map(|(city, slug, _)| (city, slug)).collect()
}
/// The city a region slug is in, by the same names.
@@ -118,14 +138,15 @@ pub fn size_label(slug: &str) -> String {
/// The region nearest this computer, by its UTC offset, among `available`.
pub fn nearest_region(offset_hours: i32, available: &[String]) -> Option<String> {
+ // Nearest first, then the next nearest: a region can have nothing on offer.
let preferences: &[&str] = match offset_hours {
- ..=-9 => &["sfo3", "sfo2", "nyc3"],
- -8..=-6 => &["sfo3", "tor1", "nyc3"],
- -5..=-3 => &["nyc3", "nyc1", "tor1"],
- -2..=2 => &["lon1", "ams3", "fra1"],
- 3..=4 => &["fra1", "ams3", "blr1"],
- 5..=6 => &["blr1", "sgp1"],
- 7..=9 => &["sgp1", "syd1"],
+ ..=-9 => &["sfo3", "sfo2", "tor1", "nyc3", "nyc1", "syd1"],
+ -8..=-6 => &["sfo3", "sfo2", "tor1", "nyc3", "nyc1", "atl1"],
+ -5..=-3 => &["nyc3", "nyc1", "tor1", "atl1", "ric1", "sfo3", "sfo2", "lon1", "ams3"],
+ -2..=2 => &["lon1", "ams3", "fra1", "nyc3", "tor1"],
+ 3..=4 => &["fra1", "ams3", "lon1", "blr1"],
+ 5..=6 => &["blr1", "sgp1", "fra1"],
+ 7..=9 => &["sgp1", "syd1", "blr1"],
_ => &["syd1", "sgp1"],
};
preferences.iter().map(|s| s.to_string()).find(|s| available.contains(s)).or_else(|| available.first().cloned())
@@ -148,11 +169,22 @@ mod tests {
#[test]
fn one_choice_per_city() {
let r = |slug: &str, name: &str| Region { slug: slug.into(), name: name.into(), sizes: Vec::new() };
- let regions = vec![r("nyc1", "New York 1"), r("sgp1", "Singapore 1"), r("nyc3", "New York 3"), r("lon1", "London 1"), r("nyc2", "New York 2")];
+ let size = |slug: &str, regions: &[&str]| Size { slug: slug.into(), regions: regions.iter().map(|r| r.to_string()).collect(), ..Default::default() };
+ let regions = vec![r("nyc1", "New York 1"), r("sgp1", "Singapore 1"), r("nyc3", "New York 3"), r("lon1", "London 1"), r("nyc2", "New York 2"), r("atl1", "Atlanta 1")];
+ // nyc3 has the most on offer; atl1 has only a premium size, so it isn't offered.
+ let sizes = vec![
+ size("s-1vcpu-1gb", &["nyc1", "nyc3", "sgp1", "lon1"]),
+ size("s-2vcpu-2gb", &["nyc3", "lon1"]),
+ size("s-2vcpu-2gb-intel", &["atl1"]),
+ ];
assert_eq!(
- cities(&regions),
+ cities(&regions, &sizes),
vec![("New York".to_string(), "nyc3".to_string()), ("London".into(), "lon1".into()), ("Singapore".into(), "sgp1".into())]
);
+ // When the newest datacenter has nothing on offer, an older one with sizes is chosen.
+ let sizes = vec![size("s-1vcpu-1gb", &["nyc1", "lon1"]), size("s-2vcpu-2gb", &["lon1"])];
+ assert_eq!(cities(&regions, &sizes), vec![("New York".to_string(), "nyc1".to_string()), ("London".into(), "lon1".into())]);
assert_eq!(city_of_slug(&regions, "nyc1").as_deref(), Some("New York"));
+ assert_eq!(offered(&sizes, "lon1").count(), 2);
}
}
diff --git a/src/ops/cloud.rs b/src/ops/cloud.rs
index c1b07a9..567fdcc 100644
--- a/src/ops/cloud.rs
+++ b/src/ops/cloud.rs
@@ -63,12 +63,18 @@ impl Engine {
match (account, regions, sizes) {
(Ok(a), Ok(r), Ok(s)) => {
out.account = Some(a);
- let slugs: Vec<String> = r.iter().map(|r| r.slug.clone()).collect();
- let offset = chrono::Local::now().offset().local_minus_utc() / 3600;
- out.default_region = Some(settings.region.clone()).filter(|r| slugs.contains(r)).or_else(|| nearest_region(offset, &slugs)).unwrap_or_default();
- out.regions = r;
// The everyday sizes: basic ("s-"), general purpose ("g-") and CPU-optimized ("c-") droplets.
out.sizes = s.into_iter().filter(|s| ["s-", "g-", "c-"].iter().any(|p| s.slug.starts_with(p)) && s.memory_mb >= 1024).collect();
+ // Defaults that work: a region with sizes on offer (DigitalOcean
+ // offers none in some datacenters), and a size on offer there.
+ let on_offer: Vec<String> = r.iter().map(|r| r.slug.clone()).filter(|slug| crate::cloud::offered(&out.sizes, slug).next().is_some()).collect();
+ let offset = chrono::Local::now().offset().local_minus_utc() / 3600;
+ out.default_region = Some(settings.region.clone()).filter(|r| on_offer.contains(r)).or_else(|| nearest_region(offset, &on_offer)).unwrap_or_default();
+ let mut there = crate::cloud::offered(&out.sizes, &out.default_region);
+ if !crate::cloud::offered(&out.sizes, &out.default_region).any(|s| s.slug == out.default_size) {
+ out.default_size = there.next().map(|s| s.slug.clone()).unwrap_or_default();
+ }
+ out.regions = r;
}
(a, r, s) => out.error = a.err().or(r.err()).or(s.err()),
}
diff --git a/src/ui/cloud_dialog.rs b/src/ui/cloud_dialog.rs
index fc2977c..d561b3d 100644
--- a/src/ui/cloud_dialog.rs
+++ b/src/ui/cloud_dialog.rs
@@ -45,10 +45,12 @@ impl CloudDialog {
cx.spawn(async move |this, cx| {
if let Ok(options) = job.await {
let _ = this.update(cx, |this, cx| {
- if this.region.is_empty() {
+ // The saved region and size, while they're on offer; else the defaults, which are.
+ if crate::cloud::offered(&options.sizes, &this.region).next().is_none() {
this.region = options.default_region.clone();
}
this.options = Load::Ready(options);
+ this.fit_size();
cx.notify();
});
}
@@ -66,6 +68,16 @@ impl CloudDialog {
}
}
+ /// Keeps the size one on offer in the chosen region: the cheapest there
+ /// when the chosen one isn't.
+ fn fit_size(&mut self) {
+ let Load::Ready(options) = &self.options else { return };
+ let mut there = crate::cloud::offered(&options.sizes, &self.region);
+ if !crate::cloud::offered(&options.sizes, &self.region).any(|s| s.slug == self.size) {
+ self.size = there.next().map(|s| s.slug.clone()).unwrap_or_default();
+ }
+ }
+
pub fn first_focus(&self, cx: &App) -> FocusHandle {
self.name.focus_handle(cx)
}
@@ -142,20 +154,21 @@ impl Render for CloudDialog {
.child(text)
};
let chosen_city = crate::cloud::city_of_slug(&options.regions, &self.region);
- let regions: Vec<AnyElement> = crate::cloud::cities(&options.regions)
+ let regions: Vec<AnyElement> = crate::cloud::cities(&options.regions, &options.sizes)
.into_iter()
.map(|(city, slug)| {
let selected = chosen_city.as_deref() == Some(city.as_str());
chip(SharedString::from(format!("cr-{slug}")), city, selected)
.on_click(cx.listener(move |this, _, _, cx| {
this.region = slug.clone();
+ this.fit_size();
cx.notify()
}))
.into_any_element()
})
.collect();
// Basic sizes available in the region, cheapest first.
- let sizes: Vec<&crate::cloud::Size> = options.sizes.iter().filter(|s| super::settings::regular_size(&s.slug) && s.regions.contains(&self.region)).take(8).collect();
+ let sizes: Vec<&crate::cloud::Size> = crate::cloud::offered(&options.sizes, &self.region).take(8).collect();
let picked = options.sizes.iter().find(|s| s.slug == self.size && s.regions.contains(&self.region)).cloned();
let size_rows: Vec<AnyElement> = sizes
.iter()
diff --git a/src/ui/settings.rs b/src/ui/settings.rs
index fbd5cf7..c0e061a 100644
--- a/src/ui/settings.rs
+++ b/src/ui/settings.rs
@@ -9,7 +9,6 @@ mod cloud;
mod prompts;
mod shell;
-pub(super) use cloud::regular_size;
use super::input::{InputEvent, TextInput};
use super::kit::{self, Button, ButtonStyle, card, icon, mono, section_title};
diff --git a/src/ui/settings/cloud.rs b/src/ui/settings/cloud.rs
index bb5bdec..2082338 100644
--- a/src/ui/settings/cloud.rs
+++ b/src/ui/settings/cloud.rs
@@ -368,9 +368,10 @@ impl SettingsPage {
// New machines: only with a working sign-in.
let defaults = options.as_ref().filter(|o| o.account.is_some()).map(|o| {
- let region = if settings.region.is_empty() { o.default_region.clone() } else { settings.region.clone() };
+ // The saved region while it has sizes on offer, else the default (which does).
+ let region = Some(settings.region.clone()).filter(|r| crate::cloud::offered(&o.sizes, r).next().is_some()).unwrap_or_else(|| o.default_region.clone());
let chosen_city = crate::cloud::city_of_slug(&o.regions, &region);
- let regions: Vec<AnyElement> = crate::cloud::cities(&o.regions)
+ let regions: Vec<AnyElement> = crate::cloud::cities(&o.regions, &o.sizes)
.into_iter()
.map(|(city, slug)| {
let selected = chosen_city.as_deref() == Some(city.as_str());
@@ -379,10 +380,7 @@ impl SettingsPage {
.into_any_element()
})
.collect();
- let sizes: Vec<AnyElement> = o
- .sizes
- .iter()
- .filter(|s| regular_size(&s.slug))
+ let sizes: Vec<AnyElement> = crate::cloud::offered(&o.sizes, &region)
.take(8)
.enumerate()
.map(|(i, s)| {
@@ -525,10 +523,6 @@ impl SettingsPage {
}
}
-/// Basic droplets on regular CPUs: the premium AMD and Intel twins of each size are left out.
-pub(crate) fn regular_size(slug: &str) -> bool {
- slug.starts_with("s-") && !slug.ends_with("-amd") && !slug.ends_with("-intel")
-}
fn row(theme: &Theme, label: &'static str, control: AnyElement) -> impl IntoElement {
div()