on main help sign in
refactor: changes
commit1cb669be8832ad2ad83ff37cfb6547ec193b5ce5 authorrobcholz <84130577+robcholz@users.noreply.github.com> dateFri 6 Feb 2026, 20:49 (8 months ago) parentcd76a4f
3 files changed+86−40
src/instance.rs+15−34 src/ssh.sh+46 src/vm.rs+25−6
src/instance.rs+15−34
@@ -33,6 +33,7 @@ const SSH_GUEST_DIR: &str = "/root/.vibebox";
3333const DEFAULT_SSH_USER: &str = "vibebox";
3434const SSH_CONNECT_RETRIES: usize = 20;
3535const SSH_CONNECT_DELAY_MS: u64 = 500;
36+const SSH_SETUP_SCRIPT: &str = include_str!("ssh.sh");
3637
3738#[derive(Debug, Clone, Serialize, Deserialize)]
3839struct InstanceConfig {
@@ -61,7 +62,12 @@ pub fn run_with_ssh(
6162 let instance_dir = ensure_instance_dir(&project_root)?;
6263 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
6364
64− let config = load_or_create_instance_config(&instance_dir)?;
65+ let mut config = load_or_create_instance_config(&instance_dir)?;
66+ // Clear cached IP to avoid reusing a stale address on startup.
67+ if config.vm_ipv4.is_some() {
68+ config.vm_ipv4 = None;
69+ write_instance_config(&instance_dir.join(INSTANCE_TOML), &config)?;
70+ }
6571 let config = Arc::new(Mutex::new(config));
6672
6773 let extra_shares = vec![DirectoryShare::new(
@@ -244,40 +250,15 @@ fn build_ssh_login_actions(
244250
245251 let key_path = format!("{guest_dir}/{key_name}.pub");
246252
247− let mask_cache = format!(
248− "if [ -d /root/{project_name}/.vibebox ]; then mount -t tmpfs tmpfs /root/{project_name}/.vibebox; fi"
249− );
253+ let setup_script = SSH_SETUP_SCRIPT
254+ .replace("__SSH_USER__", &ssh_user)
255+ .replace("__SUDO_PASSWORD__", &sudo_password)
256+ .replace("__PROJECT_NAME__", project_name)
257+ .replace("__KEY_PATH__", &key_path);
258+ let setup = vm::script_command_from_content("ssh_setup", &setup_script)
259+ .expect("ssh setup script contained invalid marker");
250260
251− let setup_lines = [
252− "if ! command -v sshd >/dev/null 2>&1; then apt-get update && apt-get install -y openssh-server sudo; fi",
253− "systemctl enable ssh >/dev/null 2>&1 || true",
254− &format!("id -u {ssh_user} >/dev/null 2>&1 || useradd -m -s /bin/bash {ssh_user}"),
255− &format!("echo \"{ssh_user}:{sudo_password}\" | chpasswd"),
256− &format!("usermod -aG sudo {ssh_user}"),
257− &format!("install -d -m 700 /home/{ssh_user}/.ssh"),
258− &format!("install -m 600 {key_path} /home/{ssh_user}/.ssh/authorized_keys"),
259− &format!("chown -R {ssh_user}:{ssh_user} /home/{ssh_user}/.ssh"),
260− "mkdir -p /etc/ssh/sshd_config.d",
261− "cat >/etc/ssh/sshd_config.d/vibebox.conf <<'VIBEBOX_SSHD'",
262− "PasswordAuthentication no",
263− "KbdInteractiveAuthentication no",
264− "ChallengeResponseAuthentication no",
265− "PubkeyAuthentication yes",
266− "PermitRootLogin no",
267− &format!("AllowUsers {ssh_user}"),
268− "VIBEBOX_SSHD",
269− "systemctl restart ssh",
270− "echo VIBEBOX_SSH_READY",
271− ];
272− let setup = setup_lines.join("\n");
273−
274− let ip_probe = "while true; do ip=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n 1); if [ -n \"$ip\" ]; then echo VIBEBOX_IPV4=$ip; break; fi; sleep 1; done";
275−
276− vec![
277− LoginAction::Send(mask_cache),
278− LoginAction::Send(setup),
279− LoginAction::Send(ip_probe.to_string()),
280− ]
261+ vec![LoginAction::Send(setup)]
281262}
282263
283264fn spawn_ssh_io(
src/ssh.shnew file+46
@@ -0,0 +1,46 @@
1+#!/bin/sh
2+
3+SSH_USER="__SSH_USER__"
4+SUDO_PASSWORD="__SUDO_PASSWORD__"
5+PROJECT_NAME="__PROJECT_NAME__"
6+KEY_PATH="__KEY_PATH__"
7+
8+if [ -d /root/${PROJECT_NAME}/.vibebox ]; then
9+ mount -t tmpfs tmpfs /root/${PROJECT_NAME}/.vibebox
10+fi
11+
12+if ! command -v sshd >/dev/null 2>&1; then
13+ apt-get update && apt-get install -y openssh-server sudo
14+fi
15+
16+systemctl enable ssh >/dev/null 2>&1 || true
17+id -u ${SSH_USER} >/dev/null 2>&1 || useradd -m -s /bin/bash ${SSH_USER}
18+echo "${SSH_USER}:${SUDO_PASSWORD}" | chpasswd
19+usermod -aG sudo ${SSH_USER}
20+install -d -m 700 /home/${SSH_USER}/.ssh
21+install -m 600 ${KEY_PATH} /home/${SSH_USER}/.ssh/authorized_keys
22+chown -R ${SSH_USER}:${SSH_USER} /home/${SSH_USER}/.ssh
23+rm -f /home/${SSH_USER}/.bash_logout
24+mkdir -p /etc/ssh/sshd_config.d
25+cat >/etc/ssh/sshd_config.d/vibebox.conf <<'VIBEBOX_SSHD'
26+PasswordAuthentication no
27+KbdInteractiveAuthentication no
28+ChallengeResponseAuthentication no
29+PubkeyAuthentication yes
30+PermitRootLogin no
31+AllowUsers __SSH_USER__
32+VIBEBOX_SSHD
33+systemctl restart ssh
34+echo VIBEBOX_SSH_READY
35+
36+echo "=== generated network file ==="
37+sed -n '1,200p' /run/systemd/network/10-netplan-all-en.network || true
38+
39+while true; do
40+ ip=$(ip -4 -o addr show scope global | awk '{print $4}' | cut -d/ -f1 | head -n 1)
41+ if [ -n "$ip" ]; then
42+ echo VIBEBOX_IPV4=$ip
43+ break
44+ fi
45+ sleep 1
46+done
src/vm.rs+25−6
@@ -415,7 +415,7 @@ fn script_command_from_path(
415415 script_command_from_content(&label, &script)
416416}
417417
418−fn script_command_from_content(
418+pub(crate) fn script_command_from_content(
419419 label: &str,
420420 script: &str,
421421) -> Result<String, Box<dyn std::error::Error>> {
@@ -725,7 +725,12 @@ where
725725 Error,
726726 }
727727
728− fn poll_with_wakeup<'a>(main_fd: RawFd, wakeup_fd: RawFd, buf: &'a mut [u8]) -> PollResult<'a> {
728+ fn poll_with_wakeup<'a>(
729+ main_fd: RawFd,
730+ wakeup_fd: RawFd,
731+ buf: &'a mut [u8],
732+ timeout_ms: i32,
733+ ) -> PollResult<'a> {
729734 let mut fds = [
730735 libc::pollfd {
731736 fd: main_fd,
@@ -739,8 +744,12 @@ where
739744 },
740745 ];
741746
742− let ret = unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) };
743− if ret <= 0 || fds[1].revents & libc::POLLIN != 0 {
747+ let ret = unsafe { libc::poll(fds.as_mut_ptr(), 2, timeout_ms) };
748+ if ret == 0 {
749+ PollResult::Spurious
750+ } else if ret < 0 {
751+ PollResult::Error
752+ } else if fds[1].revents & libc::POLLIN != 0 {
744753 PollResult::Shutdown
745754 } else if fds[0].revents & libc::POLLIN != 0 {
746755 let n = unsafe { libc::read(main_fd, buf.as_mut_ptr() as *mut _, buf.len()) };
@@ -786,7 +795,12 @@ where
786795 continue;
787796 }
788797
789− match poll_with_wakeup(libc::STDIN_FILENO, wakeup_read.as_raw_fd(), &mut buf) {
798+ match poll_with_wakeup(
799+ libc::STDIN_FILENO,
800+ wakeup_read.as_raw_fd(),
801+ &mut buf,
802+ -1,
803+ ) {
790804 PollResult::Shutdown | PollResult::Error => break,
791805 PollResult::Spurious => continue,
792806 PollResult::Ready(bytes) => {
@@ -839,7 +853,12 @@ where
839853 let mut guard = raw_guard.lock().unwrap();
840854 *guard = None;
841855 }
842− match poll_with_wakeup(vm_output_fd.as_raw_fd(), wakeup_read.as_raw_fd(), &mut buf)
856+ match poll_with_wakeup(
857+ vm_output_fd.as_raw_fd(),
858+ wakeup_read.as_raw_fd(),
859+ &mut buf,
860+ 100,
861+ )
843862 {
844863 PollResult::Shutdown | PollResult::Error => break,
845864 PollResult::Spurious => continue,