-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
antlir/vm: add sidecar on-host service support
Summary: This is worse than zeroxoneb's proposed systemd container runtime environment, but until that is ready this can serve as a stopgap measure for tests that require some supporting service to run on the host before the VM is booted (for example services like DHCP) Reviewed By: zeroxoneb Differential Revision: D28098482 fbshipit-source-id: 0f187d4be997a55dd1ad4953246437f92feafe19
- Loading branch information
1 parent
74883e2
commit b99ddf9
Showing
6 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
use tokio::net::TcpListener; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let listener = TcpListener::bind("[::]:8080").await?; | ||
|
||
loop { | ||
let (mut socket, _) = listener.accept().await?; | ||
|
||
tokio::spawn(async move { | ||
let mut buf = [0; 1024]; | ||
|
||
// In a loop, read data from the socket and write the data back. | ||
loop { | ||
let n = match socket.read(&mut buf).await { | ||
// socket closed | ||
Ok(n) if n == 0 => return, | ||
Ok(n) => n, | ||
Err(e) => { | ||
eprintln!("failed to read from socket; err = {:?}", e); | ||
return; | ||
} | ||
}; | ||
|
||
// Write the data back | ||
if let Err(e) = socket.write_all(&buf[0..n]).await { | ||
eprintln!("failed to write to socket; err = {:?}", e); | ||
return; | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use std::net::SocketAddrV6; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
use tokio::net::TcpStream; | ||
|
||
#[tokio::test] | ||
async fn sidecar() { | ||
let sock = SocketAddrV6::new("fe80::200:0ff:fe00:2".parse().unwrap(), 8080, 0, 2); | ||
let mut stream = TcpStream::connect(sock).await.unwrap(); | ||
|
||
// Write some data. | ||
stream.write_all(b"hello world!").await.unwrap(); | ||
let mut buf = [0; 1024]; | ||
stream.read(&mut buf).await.unwrap(); | ||
assert_eq!( | ||
std::str::from_utf8(&buf) | ||
.unwrap() | ||
.trim_end_matches(char::from(0)), | ||
"hello world!" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters