Skip to content

Commit 4709930

Browse files
authored
Merge pull request #37 from rushmorem/nixos-target
Add NixOS target
2 parents d6afd2a + e8591e2 commit 4709930

File tree

19 files changed

+345
-243
lines changed

19 files changed

+345
-243
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ zdaemon = "0.0.2"
3232
zfilexfer = "0.0.2"
3333
hostname = "0.1"
3434
czmq = { version = "0.1", optional = true }
35+
pnet = "0.16"
3536

3637
[lib]
3738
name = "inapi"

src/host/data/macros.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ want_macro!("Object", wantobj, is_object, as_object);
5151
// ($v:expr) => (if $v.$isf() {
5252
// Ok($v.$asf().unwrap())
5353
// } else {
54-
// Err($crate::error::Error::Generic(format!("Value is not $t")))
54+
// Err($crate::Error::Generic(format!("Value is not $t")))
5555
// });
5656
//
5757
// ($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
5858
// $n!(v)
5959
// } else {
60-
// Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
60+
// Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
6161
// });
6262
// }
6363
// }
@@ -79,13 +79,13 @@ macro_rules! neednull {
7979
($v:expr) => (if $v.is_null() {
8080
Ok($v.as_null().unwrap())
8181
} else {
82-
Err($crate::error::Error::Generic(format!("Value is not null")))
82+
Err($crate::Error::Generic(format!("Value is not null")))
8383
});
8484

8585
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
8686
neednull!(v)
8787
} else {
88-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
88+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
8989
});
9090
}
9191

@@ -96,13 +96,13 @@ macro_rules! needbool {
9696
($v:expr) => (if $v.is_boolean() {
9797
Ok($v.as_bool().unwrap())
9898
} else {
99-
Err($crate::error::Error::Generic(format!("Value is not boolean")))
99+
Err($crate::Error::Generic(format!("Value is not boolean")))
100100
});
101101

102102
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
103103
needbool!(v)
104104
} else {
105-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
105+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
106106
});
107107
}
108108

@@ -113,13 +113,13 @@ macro_rules! needi64 {
113113
($v:expr) => (if $v.is_i64() {
114114
Ok($v.as_i64().unwrap())
115115
} else {
116-
Err($crate::error::Error::Generic(format!("Value is not i64")))
116+
Err($crate::Error::Generic(format!("Value is not i64")))
117117
});
118118

119119
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
120120
needi64!(v)
121121
} else {
122-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
122+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
123123
});
124124
}
125125

@@ -130,13 +130,13 @@ macro_rules! needu64 {
130130
($v:expr) => (if $v.is_u64() {
131131
Ok($v.as_u64().unwrap())
132132
} else {
133-
Err($crate::error::Error::Generic(format!("Value is not u64")))
133+
Err($crate::Error::Generic(format!("Value is not u64")))
134134
});
135135

136136
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
137137
needu64!(v)
138138
} else {
139-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
139+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
140140
});
141141
}
142142

@@ -147,13 +147,13 @@ macro_rules! needf64 {
147147
($v:expr) => (if $v.is_f64() {
148148
Ok($v.as_f64().unwrap())
149149
} else {
150-
Err($crate::error::Error::Generic(format!("Value is not f64")))
150+
Err($crate::Error::Generic(format!("Value is not f64")))
151151
});
152152

153153
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
154154
needf64!(v)
155155
} else {
156-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
156+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
157157
});
158158
}
159159

@@ -164,13 +164,13 @@ macro_rules! needstr {
164164
($v:expr) => (if $v.is_string() {
165165
Ok($v.as_str().unwrap())
166166
} else {
167-
Err($crate::error::Error::Generic(format!("Value is not string")))
167+
Err($crate::Error::Generic(format!("Value is not string")))
168168
});
169169

170170
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
171171
needstr!(v)
172172
} else {
173-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
173+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
174174
});
175175
}
176176

@@ -181,13 +181,13 @@ macro_rules! needarray {
181181
($v:expr) => (if $v.is_array() {
182182
Ok($v.as_array().unwrap())
183183
} else {
184-
Err($crate::error::Error::Generic(format!("Value is not array")))
184+
Err($crate::Error::Generic(format!("Value is not array")))
185185
});
186186

187187
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
188188
needarray!(v)
189189
} else {
190-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
190+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
191191
});
192192
}
193193

@@ -198,12 +198,12 @@ macro_rules! needobj {
198198
($v:expr) => (if $v.is_object() {
199199
Ok($v.as_object().unwrap())
200200
} else {
201-
Err($crate::error::Error::Generic(format!("Value is not object")))
201+
Err($crate::Error::Generic(format!("Value is not object")))
202202
});
203203

204204
($v:expr => $p:expr) => (if let Some(v) = $v.pointer($p) {
205205
needobj!(v)
206206
} else {
207-
Err($crate::error::Error::Generic(format!("Could not find {} in data", $p)))
207+
Err($crate::Error::Generic(format!("Could not find {} in data", $p)))
208208
});
209209
}

src/host/telemetry.rs

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use host::Host;
1212
use serde_json::Map;
1313
use serde_json::Value;
1414
use target::Target;
15+
#[cfg(feature = "local-run")]
1516

1617
#[cfg(feature = "local-run")]
1718
#[derive(Debug, RustcEncodable)]
@@ -80,25 +81,15 @@ impl Telemetry {
8081
let mut net = Vec::new();
8182
for netif in self.net {
8283
let mut map: Map<String, Value> = Map::new();
83-
map.insert("interface".into(), json!(netif.interface));
84-
map.insert("mac".into(), json!(netif.mac));
85-
if let Some(inet) = netif.inet {
86-
let mut map1: Map<String, Value> = Map::new();
87-
map1.insert("address".into(), json!(inet.address));
88-
map1.insert("netmask".into(), json!(inet.netmask));
89-
map.insert("inet".into(), json!(map1));
90-
}
91-
if let Some(inet6) = netif.inet6 {
92-
let mut map1: Map<String, Value> = Map::new();
93-
map1.insert("address".into(), json!(inet6.address));
94-
map1.insert("prefixlen".into(), json!(inet6.prefixlen));
95-
map1.insert("scopeid".into(), json!(inet6.scopeid));
96-
map.insert("inet6".into(), json!(map1));
84+
map.insert("name".into(), json!(netif.name));
85+
map.insert("index".into(), json!(netif.index));
86+
if let Some(mac) = netif.mac {
87+
map.insert("mac".into(), json!(mac));
9788
}
98-
if let Some(status) = netif.status {
99-
let status = if status == NetifStatus::Active { "Active".to_string() } else { "Inactive".to_string() };
100-
map.insert("status".into(), json!(status));
89+
if let Some(ips) = netif.ips {
90+
map.insert("ips".into(), json!(ips));
10191
}
92+
map.insert("flags".into(), json!(netif.flags));
10293
net.push(map);
10394
}
10495

@@ -159,33 +150,11 @@ pub struct FsMount {
159150
#[cfg(feature = "local-run")]
160151
#[derive(Debug, RustcEncodable)]
161152
pub struct Netif {
162-
pub interface: String,
153+
pub name: String,
154+
pub index: u32,
163155
pub mac: Option<String>,
164-
pub inet: Option<NetifIPv4>,
165-
pub inet6: Option<NetifIPv6>,
166-
pub status: Option<NetifStatus>,
167-
}
168-
169-
#[cfg(feature = "local-run")]
170-
#[derive(Debug, RustcEncodable, PartialEq)]
171-
pub enum NetifStatus {
172-
Active,
173-
Inactive,
174-
}
175-
176-
#[cfg(feature = "local-run")]
177-
#[derive(Debug, RustcEncodable)]
178-
pub struct NetifIPv4 {
179-
pub address: String,
180-
pub netmask: String,
181-
}
182-
183-
#[cfg(feature = "local-run")]
184-
#[derive(Debug, RustcEncodable)]
185-
pub struct NetifIPv6 {
186-
pub address: String,
187-
pub prefixlen: u8,
188-
pub scopeid: Option<String>,
156+
pub ips: Option<Vec<String>>,
157+
pub flags: u32,
189158
}
190159

191160
#[cfg(feature = "local-run")]

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
#[cfg(feature = "remote-run")]
2828
extern crate czmq;
29+
#[cfg(not(feature = "local-run"))]
2930
#[macro_use]
3031
extern crate lazy_static;
3132
extern crate libc;
@@ -41,6 +42,7 @@ extern crate tempfile;
4142
extern crate zdaemon;
4243
extern crate zfilexfer;
4344
extern crate hostname;
45+
extern crate pnet;
4446

4547
#[macro_use]
4648
mod ffi_helpers;
@@ -68,7 +70,7 @@ pub use host::{Host, ffi as host_ffi};
6870
pub use host::data::open as data_open;
6971
pub use mustache::{MapBuilder, VecBuilder};
7072
pub use package::{Package, ffi as package_ffi};
71-
pub use package::providers::Providers;
73+
pub use package::providers::{Providers, ProviderFactory};
7274
#[cfg(feature = "remote-run")]
7375
pub use payload::{Payload, ffi as payload_ffi};
7476
#[cfg(feature = "remote-run")]

src/package/providers/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod macports;
1313
pub mod pkg;
1414
pub mod ports;
1515
pub mod yum;
16+
pub mod nix;
1617

1718
use command::CommandResult;
1819
use error::{Error, Result};
@@ -32,6 +33,7 @@ pub enum Providers {
3233
Pkg,
3334
Ports,
3435
Yum,
36+
Nix,
3537
}
3638

3739
impl ToString for Providers {
@@ -44,6 +46,7 @@ impl ToString for Providers {
4446
&Providers::Pkg => "Pkg".to_string(),
4547
&Providers::Ports => "Ports".to_string(),
4648
&Providers::Yum => "Yum".to_string(),
49+
&Providers::Nix => "Nix".to_string(),
4750
}
4851
}
4952
}
@@ -58,6 +61,7 @@ impl convert::From<String> for Providers {
5861
"Pkg" => Providers::Pkg,
5962
"Ports" => Providers::Ports,
6063
"Yum" => Providers::Yum,
64+
"Nix" => Providers::Nix,
6165
_ => panic!("Invalid provider"),
6266
}
6367
}
@@ -91,6 +95,7 @@ impl ProviderFactory {
9195
Providers::Pkg => Box::new(pkg::Pkg),
9296
Providers::Ports => Box::new(ports::Ports),
9397
Providers::Yum => Box::new(yum::Yum),
98+
Providers::Nix => Box::new(nix::Nix),
9499
}
95100
}
96101
}

src/package/providers/nix.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
2+
// top-level directory of this distribution and at
3+
// https://intecture.io/COPYRIGHT.
4+
//
5+
// Licensed under the Mozilla Public License 2.0 <LICENSE or
6+
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
7+
// modified, or distributed except according to those terms.
8+
9+
//! Nix package provider
10+
11+
use command::{Command, CommandResult};
12+
use error::Result;
13+
use host::Host;
14+
use super::*;
15+
16+
pub struct Nix;
17+
18+
impl Provider for Nix {
19+
fn get_providers(&self) -> Providers {
20+
Providers::Nix
21+
}
22+
23+
fn is_active(&self, host: &mut Host) -> Result<bool> {
24+
let cmd = Command::new("which nix-env");
25+
let result = try!(cmd.exec(host));
26+
27+
Ok(result.exit_code == 0)
28+
}
29+
30+
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
31+
let cmd = Command::new(&format!("nix-env --install --dry-run {}", name));
32+
let result = try!(cmd.exec(host));
33+
34+
if result.exit_code != 0 {
35+
return Err(Error::Agent(result.stderr));
36+
}
37+
38+
Ok(!result.stderr.contains("these paths will be fetched"))
39+
}
40+
41+
fn install(&self, host: &mut Host, name: &str) -> Result<CommandResult> {
42+
let cmd = Command::new(&format!("nix-env --install {}", name));
43+
cmd.exec(host)
44+
}
45+
46+
fn uninstall(&self, host: &mut Host, name: &str) -> Result<CommandResult> {
47+
let cmd = Command::new(&format!("nix-env --uninstall {}", name));
48+
cmd.exec(host)
49+
}
50+
}

src/target/centos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl TelemetryTarget for CentosTarget {
181181
try!(default::fs()),
182182
&hostname,
183183
try!(linux::memory()),
184-
try!(linux::net()),
184+
default::net(),
185185
Os::new(
186186
env::consts::ARCH,
187187
"redhat",

src/target/debian.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl TelemetryTarget for DebianTarget {
182182
try!(default::fs()),
183183
&hostname,
184184
try!(linux::memory()),
185-
try!(linux::net()),
185+
default::net(),
186186
Os::new(env::consts::ARCH, "debian", "debian", &version_str, version_maj, version_min, 0), // No known patch version
187187
);
188188

0 commit comments

Comments
 (0)