on main help sign in
refactor: changes
1cb669brobcholz
src/instance.rs+15−34
@@ -33,6 +33,7 @@ const SSH_GUEST_DIR: &str = "/root/.vibebox";
33const DEFAULT_SSH_USER: &str = "vibebox";
33const DEFAULT_SSH_USER: &str = "vibebox";
34const SSH_CONNECT_RETRIES: usize = 20;
34const SSH_CONNECT_RETRIES: usize = 20;
35const SSH_CONNECT_DELAY_MS: u64 = 500;
35const SSH_CONNECT_DELAY_MS: u64 = 500;
36+const SSH_SETUP_SCRIPT: &str = include_str!("ssh.sh");
36
37
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[derive(Debug, Clone, Serialize, Deserialize)]
38struct InstanceConfig {
39struct InstanceConfig {
@@ -61,7 +62,12 @@ pub fn run_with_ssh(
61 let instance_dir = ensure_instance_dir(&project_root)?;
62 let instance_dir = ensure_instance_dir(&project_root)?;
62 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
63 let (ssh_key, _ssh_pub) = ensure_ssh_keypair(&instance_dir)?;
63
64
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+ }
65 let config = Arc::new(Mutex::new(config));
71 let config = Arc::new(Mutex::new(config));
66
72
67 let extra_shares = vec![DirectoryShare::new(
73 let extra_shares = vec![DirectoryShare::new(
@@ -244,40 +250,15 @@ fn build_ssh_login_actions(
244
250
245 let key_path = format!("{guest_dir}/{key_name}.pub");
251 let key_path = format!("{guest_dir}/{key_name}.pub");
246
252
247− let mask_cache = format!(
253+ let setup_script = SSH_SETUP_SCRIPT
248− "if [ -d /root/{project_name}/.vibebox ]; then mount -t tmpfs tmpfs /root/{project_name}/.vibebox; fi"
254+ .replace("__SSH_USER__", &ssh_user)
249− );
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");
250
260
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),
261+ vec![LoginAction::Send(setup)]
279− LoginAction::Send(ip_probe.to_string()),
280− ]
281}
262}
282
263
283fn spawn_ssh_io(
264fn 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(
415 script_command_from_content(&label, &script)
415 script_command_from_content(&label, &script)
416}
416}
417
417
418−fn script_command_from_content(
418+pub(crate) fn script_command_from_content(
419 label: &str,
419 label: &str,
420 script: &str,
420 script: &str,
421) -> Result<String, Box<dyn std::error::Error>> {
421) -> Result<String, Box<dyn std::error::Error>> {
@@ -725,7 +725,12 @@ where
725 Error,
725 Error,
726 }
726 }
727
727
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> {
729 let mut fds = [
734 let mut fds = [
730 libc::pollfd {
735 libc::pollfd {
731 fd: main_fd,
736 fd: main_fd,
@@ -739,8 +744,12 @@ where
739 },
744 },
740 ];
745 ];
741
746
742− let ret = unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) };
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
743− if ret <= 0 || fds[1].revents & libc::POLLIN != 0 {
752+ } else if fds[1].revents & libc::POLLIN != 0 {
744 PollResult::Shutdown
753 PollResult::Shutdown
745 } else if fds[0].revents & libc::POLLIN != 0 {
754 } else if fds[0].revents & libc::POLLIN != 0 {
746 let n = unsafe { libc::read(main_fd, buf.as_mut_ptr() as *mut _, buf.len()) };
755 let n = unsafe { libc::read(main_fd, buf.as_mut_ptr() as *mut _, buf.len()) };
@@ -786,7 +795,12 @@ where
786 continue;
795 continue;
787 }
796 }
788
797
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+ ) {
790 PollResult::Shutdown | PollResult::Error => break,
804 PollResult::Shutdown | PollResult::Error => break,
791 PollResult::Spurious => continue,
805 PollResult::Spurious => continue,
792 PollResult::Ready(bytes) => {
806 PollResult::Ready(bytes) => {
@@ -839,7 +853,12 @@ where
839 let mut guard = raw_guard.lock().unwrap();
853 let mut guard = raw_guard.lock().unwrap();
840 *guard = None;
854 *guard = None;
841 }
855 }
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+ )
843 {
862 {
844 PollResult::Shutdown | PollResult::Error => break,
863 PollResult::Shutdown | PollResult::Error => break,
845 PollResult::Spurious => continue,
864 PollResult::Spurious => continue,