From 1a11cf393396ae0e892f610633fc0a716f49481b Mon Sep 17 00:00:00 2001 From: Shaun Taheri Date: Thu, 11 Aug 2016 14:57:52 +0200 Subject: Fix CORE_SERVER variable in provisioning --- Makefile | 14 +++++++------- run/Dockerfile | 2 +- run/run.sh | 11 ++++++----- run/sota-system-info | 11 ----------- run/sota.toml.env | 2 +- run/sota.toml.template | 2 +- run/system_info.sh | 11 +++++++++++ src/datatype/command.rs | 11 +++++------ src/datatype/config.rs | 2 +- src/datatype/error.rs | 2 +- src/datatype/system_info.rs | 28 ++++++++++++---------------- src/interpreter.rs | 16 ++++++++-------- src/main.rs | 4 +++- src/ota_plus.rs | 14 ++++++-------- tests/ota_plus_client_tests.rs | 2 ++ tests/sota.toml | 2 +- 16 files changed, 66 insertions(+), 68 deletions(-) delete mode 100755 run/sota-system-info create mode 100755 run/system_info.sh diff --git a/Makefile b/Makefile index 0147a82..d13c4e0 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,8 @@ CARGO := $(RUST_IN_DOCKER) cargo define make-pkg @docker run --rm \ --env-file run/sota.toml.env \ - --env OTA_AUTH_URL=$(OTA_AUTH_URL) \ - --env OTA_CORE_URL=$(OTA_CORE_URL) \ + --env OTA_AUTH_URL=$(AUTH_SERVER) \ + --env OTA_CORE_URL=$(CORE_SERVER) \ --env PACKAGE_VERSION=$(PACKAGE_VERSION) \ --env CARGO_HOME=/cargo \ --volume ~/.cargo:/cargo \ @@ -39,16 +39,16 @@ help: run: image ## Run the client inside a Docker container. @docker run --rm -it --net=host \ --env-file run/sota.toml.env \ - --env AUTH_PLUS_URL=$(AUTH_PLUS_URL) \ - --env CORE_SERVER=$(CORE_SERVER) \ - --env DEVICE_REGISTRY_URL=$(DEVICE_REGISTRY_URL) \ --env AUTH_SECTION=$(AUTH_SECTION) \ + --env AUTH_SERVER=$(AUTH_SERVER) \ + --env CORE_SERVER=$(CORE_SERVER) \ --env CONFIG_ONLY=$(CONFIG_ONLY) \ - --env DEVICE_VIN=$(DEVICE_VIN) \ --env DEVICE_UUID=$(DEVICE_UUID) \ - --env TEMPLATE_PATH=$(TEMPLATE_PATH) \ + --env DEVICE_VIN=$(DEVICE_VIN) \ --env OUTPUT_PATH=$(OUTPUT_PATH) \ + --env REGISTRY_SERVER=$(REGISTRY_SERVER) \ --env RUST_LOG=$(RUST_LOG) \ + --env TEMPLATE_PATH=$(TEMPLATE_PATH) \ advancedtelematic/sota-client:latest clean: ## Remove all compiled libraries, builds and temporary files. diff --git a/run/Dockerfile b/run/Dockerfile index d516382..f8b98aa 100644 --- a/run/Dockerfile +++ b/run/Dockerfile @@ -17,7 +17,7 @@ COPY sota_client /usr/bin/ COPY sota.toml.template /etc/ COPY sota_certificates /etc/ COPY run.sh /usr/bin/ -COPY sota-system-info /usr/bin/ +COPY system_info.sh /usr/bin/ EXPOSE 8888 9080 CMD ["/usr/bin/run.sh"] diff --git a/run/run.sh b/run/run.sh index c60c746..751c721 100755 --- a/run/run.sh +++ b/run/run.sh @@ -4,10 +4,11 @@ set -eo pipefail # set default environment variables AUTH_SECTION="${AUTH_SECTION:-false}" -AUTH_PLUS_URL="${AUTH_PLUS_URL:-http://localhost:9001}" -DEVICE_REGISTRY_URL="${DEVICE_REGISTRY_URL:-http://localhost:8083}" -TEMPLATE_PATH="${TEMPLATE_PATH:-/etc/sota.toml.template}" +AUTH_SERVER="${AUTH_SERVER:-http://localhost:9001}" +CORE_SERVER="${CORE_SERVER:-http://localhost:8080}" OUTPUT_PATH="${OUTPUT_PATH:-/etc/sota.toml}" +REGISTRY_SERVER="${REGISTRY_SERVER:-http://localhost:8083}" +TEMPLATE_PATH="${TEMPLATE_PATH:-/etc/sota.toml.template}" # generate or use existing device vin RAND=$(< /dev/urandom tr -dc A-HJ-NPR-Z0-9 | head -c 13 || [[ $? -eq 141 ]]) @@ -15,7 +16,7 @@ export DEVICE_VIN=${DEVICE_VIN:-"TEST${RAND}"} # create or use existing device uuid if [[ -z "${DEVICE_UUID}" ]]; then - DEVICE_UUID=$(http post "${DEVICE_REGISTRY_URL}/api/v1/devices" \ + DEVICE_UUID=$(http post "${REGISTRY_SERVER}/api/v1/devices" \ deviceName="${DEVICE_VIN}" \ deviceId="${DEVICE_VIN}" \ deviceType=Vehicle \ @@ -26,7 +27,7 @@ export DEVICE_UUID # create or use existing device credentials if [[ -z "${AUTH_CLIENT_ID}" ]]; then - CREDENTIALS=$(http post "${AUTH_PLUS_URL}/clients" \ + CREDENTIALS=$(http post "${AUTH_SERVER}/clients" \ client_name="${DEVICE_VIN}" \ grant_types:='["client_credentials"]' \ --check-status --print=b) diff --git a/run/sota-system-info b/run/sota-system-info deleted file mode 100755 index 3f24dfe..0000000 --- a/run/sota-system-info +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -INFO=$(lshw -json -sanitize) - -MANIFEST_PATH=/etc/manifest.xml -if [ -f $MANIFEST_PATH ]; then - MANIFEST={\"manifest_file\":\"$( cat $MANIFEST_PATH | sed 's/"/\\"/'g | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' )\"} - echo $MANIFEST $INFO | jq -s add | jq -r . -else - echo $INFO | jq -r . -fi diff --git a/run/sota.toml.env b/run/sota.toml.env index e1c579d..ea50b6f 100644 --- a/run/sota.toml.env +++ b/run/sota.toml.env @@ -13,8 +13,8 @@ DBUS_TIMEOUT=60 DEVICE_PACKAGES_DIR=/tmp/ DEVICE_PACKAGE_MANAGER=dpkg DEVICE_POLLING_INTERVAL=10 -DEVICE_SYSTEM_INFO=sota-system-info DEVICE_CERTIFICATES_PATH=/etc/sota_certificates +DEVICE_SYSTEM_INFO=system_info.sh GATEWAY_CONSOLE=false GATEWAY_DBUS=false diff --git a/run/sota.toml.template b/run/sota.toml.template index 745913b..900d042 100644 --- a/run/sota.toml.template +++ b/run/sota.toml.template @@ -20,7 +20,7 @@ uuid = "${DEVICE_UUID}" vin = "${DEVICE_VIN}" packages_dir = "${DEVICE_PACKAGES_DIR}" package_manager = "${DEVICE_PACKAGE_MANAGER}" -system_info = "${DEVICE_SYSTEM_INFO}" +system_info = "${DEVICE_SYSTEM_INFO}" polling_interval = ${DEVICE_POLLING_INTERVAL} certificates_path = "${DEVICE_CERTIFICATES_PATH}" diff --git a/run/system_info.sh b/run/system_info.sh new file mode 100755 index 0000000..3f24dfe --- /dev/null +++ b/run/system_info.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +INFO=$(lshw -json -sanitize) + +MANIFEST_PATH=/etc/manifest.xml +if [ -f $MANIFEST_PATH ]; then + MANIFEST={\"manifest_file\":\"$( cat $MANIFEST_PATH | sed 's/"/\\"/'g | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' )\"} + echo $MANIFEST $INFO | jq -s add | jq -r . +else + echo $INFO | jq -r . +fi diff --git a/src/datatype/command.rs b/src/datatype/command.rs index 91d57f8..ac61774 100644 --- a/src/datatype/command.rs +++ b/src/datatype/command.rs @@ -19,9 +19,8 @@ pub enum Command { ListInstalledPackages, UpdateInstalledPackages, - SendSystemInfo, - SendInstalledSoftware(Option), + SendSystemInfo, SendUpdateReport(Option), } @@ -58,12 +57,12 @@ named!(command <(Command, Vec<&str>)>, chain!( => { |_| Command::ListInstalledPackages } | alt_complete!(tag!("SendInstalledSoftware") | tag!("sendinst")) => { |_| Command::SendInstalledSoftware(None) } + | alt_complete!(tag!("SendSystemInfo") | tag!("info")) + => { |_| Command::SendSystemInfo } | alt_complete!(tag!("SendUpdateReport") | tag!("sendup")) => { |_| Command::SendUpdateReport(None) } | alt_complete!(tag!("Shutdown") | tag!("shutdown")) => { |_| Command::Shutdown } - | alt_complete!(tag!("SendSystemInfo") | tag!("sendsysteminfo")) - => { |_| Command::SendSystemInfo } | alt_complete!(tag!("UpdateInstalledPackages") | tag!("upinst")) => { |_| Command::UpdateInstalledPackages } ) @@ -239,9 +238,9 @@ mod tests { #[test] fn sendsysteminfo_test() { - assert_eq!("sendsysteminfo".parse::().unwrap(), Command::SendSystemInfo); + assert_eq!("info".parse::().unwrap(), Command::SendSystemInfo); assert_eq!("SendSystemInfo".parse::().unwrap(), Command::SendSystemInfo); - assert!("sendsysteminfo now".parse::().is_err()); + assert!("info please".parse::().is_err()); assert!("SendSystemInfo 1 2".parse::().is_err()); } diff --git a/src/datatype/config.rs b/src/datatype/config.rs index 210a11e..f52fea0 100644 --- a/src/datatype/config.rs +++ b/src/datatype/config.rs @@ -273,10 +273,10 @@ mod tests { [device] uuid = "123e4567-e89b-12d3-a456-426655440000" vin = "V1234567890123456" + system_info = "system_info.sh" polling_interval = 10 packages_dir = "/tmp/" package_manager = "dpkg" - system_info = "sota-system-info" certificates_path = "/tmp/sota_certificates" "#; diff --git a/src/datatype/error.rs b/src/datatype/error.rs index bd819d4..09edd90 100644 --- a/src/datatype/error.rs +++ b/src/datatype/error.rs @@ -105,7 +105,7 @@ impl Display for Error { Error::Recv(ref s) => format!("Recv error: {}", s.clone()), Error::SendEvent(ref s) => format!("Send error for Event: {}", s.clone()), Error::SendInterpret(ref s) => format!("Send error for Interpret: {}", s.clone()), - Error::SystemInfo(ref s) => s.clone(), + Error::SystemInfo(ref s) => format!("Couldn't generate system info: {}", s.clone()), Error::TomlDecode(ref e) => format!("Toml decode error: {}", e.clone()), Error::TomlParser(ref e) => format!("Toml parser errors: {:?}", e.clone()), Error::UrlParse(ref s) => format!("Url parse error: {}", s.clone()), diff --git a/src/datatype/system_info.rs b/src/datatype/system_info.rs index 85d1de6..de776aa 100644 --- a/src/datatype/system_info.rs +++ b/src/datatype/system_info.rs @@ -1,35 +1,32 @@ use rustc_serialize::{Decoder, Decodable}; use rustc_serialize::json::Json; - use std::process::Command; use std::str::FromStr; use datatype::Error; -#[derive(Debug, PartialEq, Eq, Clone)] + +#[derive(PartialEq, Eq, Debug, Clone)] pub struct SystemInfo { - script : String + command: String } impl SystemInfo { - - pub fn new(script: String) -> SystemInfo { - SystemInfo { script: script} + pub fn new(command: String) -> SystemInfo { + SystemInfo { command: command } } - pub fn get_json(&self) -> Result { - Command::new(&self.script) - .output().map_err(|e| Error::SystemInfo(format!("Error getting system info: {}", e))) - .and_then(|c| String::from_utf8(c.stdout) - .map_err(|e| Error::FromUtf8(e))) - .and_then(|s| Json::from_str(&s) - .map_err(|e| Error::JsonParser(e))) + pub fn report(&self) -> Result { + Command::new(&self.command) + .output().map_err(|err| Error::SystemInfo(err.to_string())) + .and_then(|info| String::from_utf8(info.stdout).map_err(Error::FromUtf8)) + .and_then(|text| Json::from_str(&text).map_err(Error::JsonParser)) } } impl Default for SystemInfo { fn default() -> SystemInfo { - SystemInfo::new("sota-system-info".to_string()) + SystemInfo::new("system_info.sh".to_string()) } } @@ -43,7 +40,6 @@ impl FromStr for SystemInfo { impl Decodable for SystemInfo { fn decode(d: &mut D) -> Result { - d.read_str().and_then(|s| s.parse::() - .map_err(|e|d.error(&format!("Error couldn't parse SystemInfo: {}",e)))) + d.read_str().and_then(|s| Ok(s.parse::().unwrap())) } } diff --git a/src/interpreter.rs b/src/interpreter.rs index fb8464e..9db8947 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -149,6 +149,13 @@ impl<'t> GlobalInterpreter<'t> { }); } + Command::SendSystemInfo => { + let info = try!(self.config.device.system_info.report()); + try!(ota.send_system_info(&info)); + etx.send(Event::Ok); + info!("Posted system info to the server.") + }, + Command::SendUpdateReport(report) => { report.map(|rep| { info!("Sending Update Report: {:?}", rep); @@ -158,13 +165,6 @@ impl<'t> GlobalInterpreter<'t> { Command::Shutdown => std::process::exit(0), - Command::SendSystemInfo => { - let body = try!(self.config.device.system_info.get_json()); - try!(ota.send_system_info(&body)); - etx.send(Event::Ok); - info!("Posted system info to the server") - }, - Command::UpdateInstalledPackages => { try!(ota.update_installed_packages()); etx.send(Event::Ok); @@ -192,8 +192,8 @@ impl<'t> GlobalInterpreter<'t> { Command::GetPendingUpdates | Command::ListInstalledPackages | Command::SendInstalledSoftware(_) | - Command::SendUpdateReport(_) | Command::SendSystemInfo | + Command::SendUpdateReport(_) | Command::UpdateInstalledPackages => etx.send(Event::NotAuthenticated), Command::Shutdown => std::process::exit(0), diff --git a/src/main.rs b/src/main.rs index e727b95..dfca8a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use std::path::Path; use std::sync::{Arc, Mutex}; use std::time::Duration; -use libotaplus::datatype::{config, Command, Config, Event}; +use libotaplus::datatype::{config, Command, Config, Event, SystemInfo}; use libotaplus::gateway::{Console, DBus, Gateway, Interpret, Http, Websocket}; use libotaplus::gateway::broadcast::Broadcast; use libotaplus::http::{AuthClient, set_ca_certificates}; @@ -173,6 +173,7 @@ fn build_config() -> Config { opts.optopt("", "device-package-manager", "change the package manager", "MANAGER"); opts.optopt("", "device-polling-interval", "change the package polling interval", "INTERVAL"); opts.optopt("", "device-certificates-path", "change the OpenSSL CA certificates file", "PATH"); + opts.optopt("", "device-system-info", "change the system information command", "PATH"); opts.optopt("", "gateway-console", "toggle the console gateway", "BOOL"); opts.optopt("", "gateway-dbus", "toggle the dbus gateway", "BOOL"); @@ -225,6 +226,7 @@ fn build_config() -> Config { config.device.polling_interval = interval.parse().unwrap_or_else(|err| exit!("Invalid device polling interval: {}", err)); }); matches.opt_str("device-certificates-path").map(|certs| config.device.certificates_path = certs); + matches.opt_str("device-system-info").map(|cmd| config.device.system_info = SystemInfo::new(cmd)); matches.opt_str("gateway-console").map(|console| { config.gateway.console = console.parse().unwrap_or_else(|err| exit!("Invalid console gateway boolean: {}", err)); diff --git a/src/ota_plus.rs b/src/ota_plus.rs index c2690bc..9c47e26 100644 --- a/src/ota_plus.rs +++ b/src/ota_plus.rs @@ -1,6 +1,6 @@ use chan::Sender; use rustc_serialize::json; -use rustc_serialize::Encodable; +use rustc_serialize::json::Json; use std::fs::File; use std::io; use std::path::PathBuf; @@ -99,14 +99,12 @@ impl<'c, 'h> OTA<'c, 'h> { Ok(()) } - pub fn send_system_info(&mut self, json: &T) -> Result<(), Error> { + pub fn send_system_info(&mut self, info: &Json) -> Result<(), Error> { debug!("sending system info"); - let endpoint = format!("/api/v1/vehicle_updates/{}/{}", self.config.device.uuid, "system_info"); - let url = self.config.core.server.join(&endpoint).expect("couldn't build endpoint url"); - let body = try!(json::encode(&json)); - let resp_rx = self.client.put(url, Some(body.into_bytes())); - let resp = resp_rx.recv().expect("no send_system_info response received"); - let _ = try!(resp); + let body = try!(json::encode(info)); + let resp_rx = self.client.put(self.endpoint("system_info"), Some(body.into_bytes())); + let resp = resp_rx.recv().expect("no send_system_info response received"); + let _ = try!(resp); Ok(()) } } diff --git a/tests/ota_plus_client_tests.rs b/tests/ota_plus_client_tests.rs index ae7e4bb..fd25f77 100644 --- a/tests/ota_plus_client_tests.rs +++ b/tests/ota_plus_client_tests.rs @@ -76,6 +76,8 @@ Options: change the package polling interval --device-certificates-path PATH change the OpenSSL CA certificates file + --device-system-info PATH + change the system information command --gateway-console BOOL toggle the console gateway --gateway-dbus BOOL diff --git a/tests/sota.toml b/tests/sota.toml index 08c05b7..487887d 100644 --- a/tests/sota.toml +++ b/tests/sota.toml @@ -20,7 +20,7 @@ uuid = "123e4567-e89b-12d3-a456-426655440000" vin = "V1234567890123456" packages_dir = "/tmp/" package_manager = "dpkg" -system_info = "sota-system-info" +system_info = "system_info.sh" polling_interval = 10 certificates_path = "/tmp/sota_certificates" -- cgit v1.2.1