remove binaries from hypervisor (#1960)

This commit is contained in:
Nikolay Volf 2016-08-19 16:49:58 +04:00 committed by Gav Wood
parent 070a2157e6
commit 34de330ed9
2 changed files with 16 additions and 21 deletions

View File

@ -41,8 +41,8 @@ pub struct Hypervisor {
ipc_addr: String, ipc_addr: String,
service: Arc<HypervisorService>, service: Arc<HypervisorService>,
ipc_worker: RwLock<nanoipc::Worker<HypervisorService>>, ipc_worker: RwLock<nanoipc::Worker<HypervisorService>>,
processes: RwLock<HashMap<BinaryId, Child>>, processes: RwLock<HashMap<IpcModuleId, Child>>,
modules: HashMap<IpcModuleId, (BinaryId, BootArgs)>, modules: HashMap<IpcModuleId, BootArgs>,
} }
/// Boot arguments for binary /// Boot arguments for binary
@ -79,8 +79,8 @@ impl Hypervisor {
Hypervisor::with_url(HYPERVISOR_IPC_URL) Hypervisor::with_url(HYPERVISOR_IPC_URL)
} }
pub fn module(mut self, module_id: IpcModuleId, binary_id: BinaryId, args: BootArgs) -> Hypervisor { pub fn module(mut self, module_id: IpcModuleId, args: BootArgs) -> Hypervisor {
self.modules.insert(module_id, (binary_id, args)); self.modules.insert(module_id, args);
self.service.add_module(module_id); self.service.add_module(module_id);
self self
} }
@ -106,7 +106,7 @@ impl Hypervisor {
/// Since one binary can host multiple modules /// Since one binary can host multiple modules
/// we match binaries /// we match binaries
fn match_module(&self, module_id: &IpcModuleId) -> Option<&(BinaryId, BootArgs)> { fn match_module(&self, module_id: &IpcModuleId) -> Option<&BootArgs> {
self.modules.get(module_id) self.modules.get(module_id)
} }
@ -126,24 +126,19 @@ impl Hypervisor {
fn start_module(&self, module_id: IpcModuleId) { fn start_module(&self, module_id: IpcModuleId) {
use std::io::Write; use std::io::Write;
self.match_module(&module_id).map(|&(ref binary_id, ref binary_args)| { self.match_module(&module_id).map(|boot_args| {
let mut processes = self.processes.write().unwrap(); let mut processes = self.processes.write().unwrap();
{ {
if processes.get(binary_id).is_some() { if processes.get(&module_id).is_some() {
// already started for another module // already started for another module
return; return;
} }
} }
let mut executable_path = std::env::current_exe().unwrap(); let mut command = Command::new(&std::env::current_exe().unwrap());
executable_path.pop();
executable_path.push(binary_id);
let executable_path = executable_path.to_str().unwrap();
let mut command = Command::new(&executable_path);
command.stderr(std::process::Stdio::inherit()); command.stderr(std::process::Stdio::inherit());
if let Some(ref cli_args) = binary_args.cli { if let Some(ref cli_args) = boot_args.cli {
for arg in cli_args { command.arg(arg); } for arg in cli_args { command.arg(arg); }
} }
@ -152,18 +147,18 @@ impl Hypervisor {
trace!(target: "hypervisor", "Spawn executable: {:?}", command); trace!(target: "hypervisor", "Spawn executable: {:?}", command);
let mut child = command.spawn().unwrap_or_else( let mut child = command.spawn().unwrap_or_else(
|e| panic!("Hypervisor cannot start binary ({:?}): {}", executable_path, e)); |e| panic!("Hypervisor cannot execute command ({:?}): {}", command, e));
if let Some(ref std_in) = binary_args.stdin { if let Some(ref std_in) = boot_args.stdin {
trace!(target: "hypervisor", "Pushing std-in payload..."); trace!(target: "hypervisor", "Pushing std-in payload...");
child.stdin.as_mut() child.stdin.as_mut()
.expect("std-in should be piped above") .expect("std-in should be piped above")
.write(std_in) .write(std_in)
.unwrap_or_else(|e| panic!(format!("Error trying to pipe stdin for {}: {:?}", &executable_path, e))); .unwrap_or_else(|e| panic!(format!("Error trying to pipe stdin for {:?}: {:?}", &command, e)));
drop(child.stdin.take()); drop(child.stdin.take());
} }
processes.insert(binary_id, child); processes.insert(module_id, child);
}); });
} }
@ -185,8 +180,8 @@ impl Hypervisor {
if wait_time.is_some() { std::thread::sleep(wait_time.unwrap()) } if wait_time.is_some() { std::thread::sleep(wait_time.unwrap()) }
let mut childs = self.processes.write().unwrap(); let mut childs = self.processes.write().unwrap();
for (ref mut binary, ref mut child) in childs.iter_mut() { for (ref mut module, ref mut child) in childs.iter_mut() {
trace!(target: "hypervisor", "Stopping process module: {}", binary); trace!(target: "hypervisor", "Stopping process module: {}", module);
child.kill().unwrap(); child.kill().unwrap();
} }
} }

View File

@ -105,7 +105,7 @@ pub fn sync
-> Result<SyncModules, NetworkError> -> Result<SyncModules, NetworkError>
{ {
let mut hypervisor = hypervisor_ref.take().expect("There should be hypervisor for ipc configuration"); let mut hypervisor = hypervisor_ref.take().expect("There should be hypervisor for ipc configuration");
hypervisor = hypervisor.module(SYNC_MODULE_ID, "parity", sync_arguments(sync_cfg, net_cfg, log_settings)); hypervisor = hypervisor.module(SYNC_MODULE_ID, sync_arguments(sync_cfg, net_cfg, log_settings));
hypervisor.start(); hypervisor.start();
hypervisor.wait_for_startup(); hypervisor.wait_for_startup();