-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert all of the launcher crates to use prost. #6105
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,17 +12,13 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::io; | ||
use std::path::Path; | ||
|
||
use crate::core::os::process::Pid; | ||
use crate::protocol; | ||
use crate::{ | ||
core::os::process::Pid, | ||
error::{Error, Result}, | ||
protocol::{self, Error as ProtocolError}, | ||
}; | ||
use ipc_channel::ipc::{IpcOneShotServer, IpcReceiver, IpcSender}; | ||
use protobuf; | ||
|
||
use crate::error::{Error, Result}; | ||
use std::{collections::HashMap, fs, io, path::Path}; | ||
|
||
type Env = HashMap<String, String>; | ||
type IpcServer = IpcOneShotServer<Vec<u8>>; | ||
|
@@ -49,38 +45,36 @@ impl LauncherCli { | |
let tx = IpcSender::connect(pipe_to_launcher).map_err(Error::Connect)?; | ||
let (ipc_srv, pipe_to_sup) = IpcServer::new().map_err(Error::BadPipe)?; | ||
debug!("IpcServer::new() returned pipe_to_sup: {}", pipe_to_sup); | ||
let mut cmd = protocol::Register::new(); | ||
cmd.set_pipe(pipe_to_sup); | ||
let mut cmd = protocol::Register::default(); | ||
cmd.pipe = pipe_to_sup.clone(); | ||
Self::send(&tx, &cmd)?; | ||
let (rx, raw) = ipc_srv.accept().map_err(|_| Error::AcceptConn)?; | ||
Self::read::<protocol::NetOk>(&raw)?; | ||
Ok(LauncherCli { | ||
tx: tx, | ||
rx: rx, | ||
pipe: cmd.take_pipe(), | ||
pipe: pipe_to_sup, | ||
}) | ||
} | ||
|
||
/// Read a launcher protocol message from a byte array | ||
fn read<T>(bytes: &[u8]) -> Result<T> | ||
where | ||
T: protobuf::MessageStatic, | ||
T: protocol::LauncherMessage, | ||
{ | ||
let txn = protocol::NetTxn::from_bytes(bytes).map_err(Error::Deserialize)?; | ||
let txn = protocol::NetTxn::from_bytes(bytes)?; | ||
if txn.message_id() == "NetErr" { | ||
let err = txn | ||
.decode::<protocol::NetErr>() | ||
.map_err(Error::Deserialize)?; | ||
return Err(Error::Protocol(err)); | ||
let err = txn.decode::<protocol::NetErr>()?; | ||
return Err(Error::Protocol(ProtocolError::NetErr(err))); | ||
} | ||
let msg = txn.decode::<T>().map_err(Error::Deserialize)?; | ||
let msg = txn.decode::<T>()?; | ||
Ok(msg) | ||
} | ||
|
||
/// Receive and read protocol message from an IpcReceiver | ||
fn recv<T>(rx: &IpcReceiver<Vec<u8>>) -> Result<T> | ||
where | ||
T: protobuf::MessageStatic, | ||
T: protocol::LauncherMessage, | ||
{ | ||
match rx.recv() { | ||
Ok(bytes) => Self::read(&bytes), | ||
|
@@ -91,18 +85,18 @@ impl LauncherCli { | |
/// Send a command to a Launcher | ||
fn send<T>(tx: &IpcSender<Vec<u8>>, message: &T) -> Result<()> | ||
where | ||
T: protobuf::MessageStatic, | ||
T: protocol::LauncherMessage, | ||
{ | ||
let txn = protocol::NetTxn::build(message).map_err(Error::Serialize)?; | ||
let bytes = txn.to_bytes().map_err(Error::Serialize)?; | ||
let txn = protocol::NetTxn::build(message)?; | ||
let bytes = txn.to_bytes()?; | ||
tx.send(bytes).map_err(Error::Send)?; | ||
Ok(()) | ||
} | ||
|
||
/// Receive and read protocol message from an IpcReceiver | ||
fn try_recv<T>(rx: &IpcReceiver<Vec<u8>>) -> Result<Option<T>> | ||
where | ||
T: protobuf::MessageStatic, | ||
T: protocol::LauncherMessage, | ||
{ | ||
match rx.try_recv().map_err(|err| Error::from(*err)) { | ||
Ok(bytes) => { | ||
|
@@ -124,11 +118,11 @@ impl LauncherCli { | |
|
||
/// Restart a running process with the same arguments | ||
pub fn restart(&self, pid: Pid) -> Result<Pid> { | ||
let mut msg = protocol::Restart::new(); | ||
msg.set_pid(pid.into()); | ||
let mut msg = protocol::Restart::default(); | ||
msg.pid = pid.into(); | ||
Self::send(&self.tx, &msg)?; | ||
let reply = Self::recv::<protocol::SpawnOk>(&self.rx)?; | ||
Ok(reply.get_pid() as Pid) | ||
Ok(reply.pid as Pid) | ||
} | ||
|
||
/// Send a process spawn command to the connected Launcher | ||
|
@@ -154,8 +148,8 @@ impl LauncherCli { | |
G: ToString, | ||
P: ToString, | ||
{ | ||
let mut msg = protocol::Spawn::new(); | ||
msg.set_binary(bin.as_ref().to_path_buf().to_string_lossy().into_owned()); | ||
let mut msg = protocol::Spawn::default(); | ||
msg.binary = bin.as_ref().to_path_buf().to_string_lossy().into_owned(); | ||
|
||
// On Windows, we only expect user to be Some. | ||
// | ||
|
@@ -164,34 +158,35 @@ impl LauncherCli { | |
// used; names are only for backward compatibility with older | ||
// Launchers. | ||
if let Some(name) = user { | ||
msg.set_svc_user(name.to_string()); | ||
msg.svc_user = Some(name.to_string()); | ||
} | ||
if let Some(name) = group { | ||
msg.set_svc_group(name.to_string()); | ||
msg.svc_group = Some(name.to_string()); | ||
} | ||
if let Some(uid) = user_id { | ||
msg.set_svc_user_id(uid); | ||
msg.svc_user_id = Some(uid); | ||
} | ||
if let Some(gid) = group_id { | ||
msg.set_svc_group_id(gid); | ||
msg.svc_group_id = Some(gid); | ||
} | ||
|
||
// This is only for Windows | ||
if let Some(password) = password { | ||
msg.set_svc_password(password.to_string()); | ||
msg.svc_password = Some(password.to_string()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could streamline these a bit with this: msg.svc_user = user.map(|u| u.to_string());
msg.svc_group = group.map(|g| g.to_string());
msg.svc_user_id = user_id;
msg.svc_group_id = group_id;
msg.svc_password = password.map(|p| p.to_string()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's awesome, will do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raskchanky Actually, if you wanna get really wild, try this: let msg = protocol::Spawn {
binary: bin.as_ref().to_path_buf().to_string_lossy().into_owned(),
svc_user: user.map(|u| u.to_string()),
svc_group: group.map(|g| g.to_string()),
svc_user_id: user_id,
svc_group_id: group_id,
svc_password: password.map(|p| p.to_string()),
env: env,
id: id.to_string(),
..Default::default()
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😲 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern could be used on all these messages, actually. Kinda nice to reduce the mutability of everything to the bare minimum. |
||
} | ||
msg.set_env(env); | ||
msg.set_id(id.to_string()); | ||
|
||
msg.env = env; | ||
msg.id = id.to_string(); | ||
Self::send(&self.tx, &msg)?; | ||
let reply = Self::recv::<protocol::SpawnOk>(&self.rx)?; | ||
Ok(reply.get_pid() as Pid) | ||
Ok(reply.pid as Pid) | ||
} | ||
|
||
pub fn terminate(&self, pid: Pid) -> Result<i32> { | ||
let mut msg = protocol::Terminate::new(); | ||
msg.set_pid(pid.into()); | ||
let mut msg = protocol::Terminate::default(); | ||
msg.pid = pid.into(); | ||
Self::send(&self.tx, &msg)?; | ||
let reply = Self::recv::<protocol::TerminateOk>(&self.rx)?; | ||
Ok(reply.get_exit_code()) | ||
Ok(reply.exit_code) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
// Inline common build protocols behavior | ||
include!("../libbuild-protocols.rs"); | ||
use prost_build; | ||
|
||
/// Automatically generate Rust code from our protobuf definitions at | ||
/// compile time. | ||
/// | ||
/// Generated code is deposited in `OUT_DIR` and automatically | ||
/// `include!`-ed in our Rust modules, per standard Prost practice. | ||
|
||
fn main() { | ||
protocols::generate_if_feature_enabled(); | ||
let mut config = prost_build::Config::new(); | ||
config.type_attribute(".", "#[derive(Serialize, Deserialize)]"); | ||
config | ||
.compile_protos( | ||
&[ | ||
"protocols/error.proto", | ||
"protocols/launcher.proto", | ||
"protocols/net.proto", | ||
"protocols/supervisor.proto", | ||
], | ||
&["protocols/"], | ||
) | ||
.unwrap() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
syntax = "proto2"; | ||
|
||
package error; | ||
package launcher.error; | ||
|
||
enum ErrCode { | ||
Unknown = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
syntax = "proto2"; | ||
|
||
package launcher; | ||
package launcher.launcher; | ||
|
||
message Register { | ||
optional string pipe = 1; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
syntax = "proto2"; | ||
|
||
package net; | ||
package launcher.net; | ||
|
||
message Envelope { | ||
optional string message_id = 1; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
syntax = "proto2"; | ||
|
||
package supervisor; | ||
package launcher.supervisor; | ||
|
||
message Shutdown {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's minor, but I've found it useful to have things under
crate::
to actually be defined in the current crate. We end up doing this because we've got this over inlib.rs
habitat/components/launcher-client/src/lib.rs
Lines 15 to 16 in 6784490
Thus,
core
andprotocol
are actually symbols "in our crate".For something like this, I like to do:
(and often, that
self as protocol
can be refactored away with a bit more typing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, will update