Skip to content

Commit

Permalink
WSL: store first install bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
htngr committed Oct 22, 2024
1 parent 817005b commit f27ac90
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ jobs:
id: winver
with:
string: '${{ github.ref_name }}'
pattern: '^v(\d+).(\d+).(\d+)-?.*$'
replace-with: '$1.$2.$3.0'
pattern: '^v(\d+).(\d+).(\d+)(\.(\d+))?$'
replace-with: '$1.$2.$3$4'
- name: Insert version ${{ github.ref_name }} / ${{ steps.winver.outputs.replaced }}
env:
VERSION: ${{ github.ref_name }}
Expand Down
31 changes: 17 additions & 14 deletions codchi/src/platform/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use anyhow::{bail, Context, Result};
use itertools::Itertools;
use log::Level;
use std::{collections::HashMap, fs, sync::mpsc::channel, thread};

pub trait MachineDriver: Sized {
Expand Down Expand Up @@ -297,20 +298,22 @@ git add flake.*
if status == PlatformStatus::NotInstalled {
set_progress_status(format!("Installing {}...", self.config.name));
self.install().inspect_err(|_err| {
log::error!(
"Removing leftovers of machine files for {}...",
self.config.name
);
log::trace!(
"Deleting config data for {}: {:?}",
self.config.name,
fs::remove_dir_all(host::DIR_CONFIG.join_machine(&self.config.name))
);
log::trace!(
"Deleting data for {}: {:?}",
self.config.name,
fs::remove_dir_all(host::DIR_DATA.join_machine(&self.config.name))
);
if !log::log_enabled!(Level::Debug) {
log::error!(
"Removing leftovers of machine files for {}...",
self.config.name
);
log::trace!(
"Deleting config data for {}: {:?}",
self.config.name,
fs::remove_dir_all(host::DIR_CONFIG.join_machine(&self.config.name))
);
log::trace!(
"Deleting data for {}: {:?}",
self.config.name,
fs::remove_dir_all(host::DIR_DATA.join_machine(&self.config.name))
);
}
})?;
} else {
if status == PlatformStatus::Stopped {
Expand Down
121 changes: 56 additions & 65 deletions codchi/src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,76 +41,67 @@ impl Store for StoreImpl {
fn start_or_init_container() -> Result<Self> {
wsl::check_wsl()?;

fn start_or_init(after_install: bool) -> Result<StoreImpl> {
let status = wsl::get_platform_status(consts::CONTAINER_STORE_NAME)?;
log::trace!("WSL store container status: {status:#?}");

let store = StoreImpl {};
match status {
PlatformStatus::NotInstalled => wsl::import(
files::STORE_ROOTFS_NAME,
consts::CONTAINER_STORE_NAME,
consts::host::DIR_DATA
.join_store()
.get_or_create()?
.to_path_buf(),
|| {
wsl::set_sparse(consts::CONTAINER_STORE_NAME)?;
start_or_init(true)
},
)
.inspect_err(|_| {
if !log::log_enabled!(Level::Debug) {
log::error!("Removing leftovers of store files...");
let _ = fs::remove_dir_all(consts::host::DIR_CONFIG.join_store());
let _ = fs::remove_dir_all(consts::host::DIR_DATA.join_store());
}
}),
PlatformStatus::Running => {
while store
.cmd()
.run("ps", &[])
.output_utf8_ok()?
.contains("/sbin/init")
{
set_progress_status(
"The store is currently initializing. Please wait a moment...",
);
thread::sleep(Duration::from_millis(500));
}
if store.cmd().ping_store() {
Ok(store)
} else {
let _ = wsl::wsl_command()
.arg("--terminate")
.arg(consts::CONTAINER_STORE_NAME)
.wait_ok()
.trace_err("Failed stopping incorrectly started store container");
anyhow::bail!(
"The store container was started incorrectly. Please try again!"
);
}
let status = wsl::get_platform_status(consts::CONTAINER_STORE_NAME)?;
log::trace!("WSL store container status: {status:#?}");

let store = StoreImpl {};
match status {
PlatformStatus::NotInstalled => wsl::import(
files::STORE_ROOTFS_NAME,
consts::CONTAINER_STORE_NAME,
consts::host::DIR_DATA
.join_store()
.get_or_create()?
.to_path_buf(),
|| {
wsl::set_sparse(consts::CONTAINER_STORE_NAME)?;
set_progress_status(
"Initializing store container. This takes a while the first time...",
);
Store::init()
},
)
.inspect_err(|_| {
if !log::log_enabled!(Level::Debug) {
log::error!("Removing leftovers of store files...");
let _ = fs::remove_dir_all(consts::host::DIR_CONFIG.join_store());
let _ = fs::remove_dir_all(consts::host::DIR_DATA.join_store());
}
PlatformStatus::Stopped => {
if after_install {
set_progress_status(
"Initializing store container. This takes a while the first time...",
);
}

store
.cmd()
.run("/sbin/init", &[])
.output_ok_streaming(channel().1, |line| {
log_progress("store_init", Level::Debug, &line)
})?;

}),
PlatformStatus::Running => {
while store
.cmd()
.run("ps", &[])
.output_utf8_ok()?
.contains("/sbin/init")
{
set_progress_status(
"The store is currently initializing. Please wait a moment...",
);
thread::sleep(Duration::from_millis(500));
}
if store.cmd().ping_store() {
Ok(store)
} else {
let _ = wsl::wsl_command()
.arg("--terminate")
.arg(consts::CONTAINER_STORE_NAME)
.wait_ok()
.trace_err("Failed stopping incorrectly started store container");
anyhow::bail!("The store container was started incorrectly. Please try again!");
}
}
PlatformStatus::Stopped => {
store
.cmd()
.run("/sbin/init", &[])
.output_ok_streaming(channel().1, |line| {
log_progress("store_init", Level::Debug, &line)
})?;

Ok(store)
}
}

start_or_init(false)
}

fn cmd(&self) -> impl NixDriver {
Expand Down
2 changes: 1 addition & 1 deletion nix/container/store/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ in
export PATH="/bin:${consts.store.PROFILE_STORE}/bin:/root/.nix-profile/bin:$PATH"
# Ensure a consistent umask.
umask 0022
umask 022
# Make nixs' https work
export NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
Expand Down
2 changes: 1 addition & 1 deletion nix/container/store/wsl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ in
echo "Linking $src to $target" >&2
[ -d "$(dirname $target)" ] || mkdir -p "$(dirname $target)"
[ -L "$target" ] && rm "$target"
ln -s "$src" "$target"
ln -fs "$src" "$target"
}
mkLink "$WSL_CODCHI_DIR_CONFIG" "${consts.store.DIR_CONFIG}"
Expand Down

0 comments on commit f27ac90

Please sign in to comment.