Skip to content

Commit

Permalink
Merge pull request #2295 from jarhodes314/feat/2281/multicall-binary
Browse files Browse the repository at this point in the history
Make tedge a multi-call binary
  • Loading branch information
reubenmiller authored Oct 5, 2023
2 parents 548a6a4 + 49607e8 commit 5a52630
Show file tree
Hide file tree
Showing 33 changed files with 1,616 additions and 1,339 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ homepage = "https://thin-edge.io"
repository = "https://github.com/thin-edge/thin-edge.io"

[workspace.dependencies]
anstyle = "1.0"
anyhow = "1.0"
assert-json-diff = "2.0"
assert_cmd = "2.0"
Expand All @@ -49,7 +50,10 @@ c8y_firmware_manager = { path = "crates/extensions/c8y_firmware_manager" }
c8y_http_proxy = { path = "crates/extensions/c8y_http_proxy" }
c8y_auth_proxy = { path = "crates/extensions/c8y_auth_proxy" }
c8y_log_manager = { path = "crates/extensions/c8y_log_manager" }
c8y-configuration-plugin = { path = "plugins/c8y_configuration_plugin" }
c8y-firmware-plugin = { path = "plugins/c8y_firmware_plugin" }
c8y_mapper_ext = { path = "crates/extensions/c8y_mapper_ext" }
c8y-remote-access-plugin = { path = "plugins/c8y_remote_access_plugin" }
camino = "1.1"
certificate = { path = "crates/common/certificate" }
clap = { version = "4.4", features = ["cargo", "derive"] }
Expand Down Expand Up @@ -127,6 +131,8 @@ strum_macros = "0.24"
syn = { version = "2", features = ["full", "extra-traits"] }
tedge_actors = { path = "crates/core/tedge_actors" }
tedge_api = { path = "crates/core/tedge_api" }
tedge-apt-plugin = { path = "plugins/tedge_apt_plugin" }
tedge-agent = { path = "crates/core/tedge_agent" }
tedge_config = { path = "crates/common/tedge_config" }
tedge_config_macros = { path = "crates/common/tedge_config_macros" }
tedge_config_macros-impl = { path = "crates/common/tedge_config_macros/impl" }
Expand All @@ -136,11 +142,14 @@ tedge_file_system_ext = { path = "crates/extensions/tedge_file_system_ext" }
tedge_health_ext = { path = "crates/extensions/tedge_health_ext" }
tedge_http_ext = { path = "crates/extensions/tedge_http_ext" }
tedge_log_manager = { path = "crates/extensions/tedge_log_manager" }
tedge-log-plugin = { path = "plugins/tedge_log_plugin" }
tedge-mapper = { path = "crates/core/tedge_mapper" }
tedge_mqtt_ext = { path = "crates/extensions/tedge_mqtt_ext" }
tedge_signal_ext = { path = "crates/extensions/tedge_signal_ext" }
tedge_test_utils = { path = "crates/tests/tedge_test_utils" }
tedge_timer_ext = { path = "crates/extensions/tedge_timer_ext" }
tedge_utils = { path = "crates/common/tedge_utils" }
tedge-watchdog = { path = "crates/core/tedge_watchdog" }
tempfile = "3.5"
test-case = "2.2"
thiserror = "1.0"
Expand Down
5 changes: 3 additions & 2 deletions ci/build_scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,16 @@ build_tarball() {
tar_type="bsdtar"
fi

# Only include the tedge binary as it is a multi-call binary
case "$tar_type" in
bsdtar)
# bsd tar requires different options to prevent adding extra "AppleDouble" files, e.g. `._` files, to the archive
echo "Using bsdtar, but please consider using gnu-tar instead. Install via: brew install gnu-tar"
COPYFILE_DISABLE=1 tar cfz "$TAR_FILE" --no-xattrs --no-mac-metadata -C "$source_dir" --files-from <(printf "%s\n" "${PACKAGES[@]}")
COPYFILE_DISABLE=1 tar cfz "$TAR_FILE" --no-xattrs --no-mac-metadata -C "$source_dir" --files-from <(printf "%s\n" "tedge")
;;
*)
# Default to gnu tar (as this is generally the default)
"$tar_cmd" cfz "$TAR_FILE" --no-xattrs --owner=0 --group=0 --mode='0755' -C "$source_dir" --files-from <(printf "%s\n" "${PACKAGES[@]}")
"$tar_cmd" cfz "$TAR_FILE" --no-xattrs --owner=0 --group=0 --mode='0755' -C "$source_dir" --files-from <(printf "%s\n" "tedge")
;;
esac

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ deb:
- c8y_configuration_plugin (<= 0.8.1)

contents:
# binary
- src: .build/c8y-configuration-plugin
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/c8y-configuration-plugin.service
dst: /lib/systemd/system/
Expand Down
7 changes: 3 additions & 4 deletions configuration/package_manifests/nfpm.c8y-firmware-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ vendor: "thin-edge.io"
homepage: "https://thin-edge.io"
license: "Apache-2.0"

depends:
- tedge

deb:
fields:
Vcs-Browser: ${CI_PROJECT_URL}
Vcs-Git: ${CI_PROJECT_URL}
compression: xz

contents:
# binary
- src: .build/c8y-firmware-plugin
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/c8y-firmware-plugin.service
dst: /lib/systemd/system/
Expand Down
10 changes: 0 additions & 10 deletions configuration/package_manifests/nfpm.c8y-remote-access-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ deb:
compression: xz

depends:
# Requires tedge user to create the supported operation file.
# The tedge user is created by the tedge package
# so it must be run before this package's postinst.
# Debian seems to work without this depends, however the rpm package
# seems to use another sorting order if the dependency is not defined.
- tedge

contents:
# binary
- src: .build/c8y-remote-access-plugin
dst: /usr/bin/

overrides:
apk:
scripts:
Expand Down
6 changes: 2 additions & 4 deletions configuration/package_manifests/nfpm.tedge-agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ vendor: "thin-edge.io"
homepage: "https://thin-edge.io"
license: "Apache-2.0"

depends:
- tedge
replaces:
- tedge_agent
provides:
Expand All @@ -30,10 +32,6 @@ deb:
- tedge_agent (<= 0.8.1)

contents:
# binary
- src: .build/tedge-agent
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/tedge-agent.service
dst: /lib/systemd/system/tedge-agent.service
Expand Down
6 changes: 2 additions & 4 deletions configuration/package_manifests/nfpm.tedge-apt-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ vendor: "thin-edge.io"
homepage: "https://thin-edge.io"
license: "Apache-2.0"

depends:
- tedge
replaces:
- tedge_apt_plugin
conflicts:
Expand All @@ -28,10 +30,6 @@ deb:
- tedge_apt_plugin (<= 0.8.1)

contents:
# binary
- src: .build/tedge-apt-plugin
dst: /usr/bin/

# Symlink to sm plugin dir
- src: /usr/bin/tedge-apt-plugin
dst: /etc/tedge/sm-plugins/apt
Expand Down
6 changes: 2 additions & 4 deletions configuration/package_manifests/nfpm.tedge-log-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ vendor: "thin-edge.io"
homepage: "https://thin-edge.io"
license: "Apache-2.0"

depends:
- tedge
replaces:
- c8y_log_plugin
- c8y-log-plugin
Expand All @@ -31,10 +33,6 @@ deb:
- c8y-log-plugin

contents:
# binary
- src: .build/tedge-log-plugin
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/tedge-log-plugin.service
dst: /lib/systemd/system/
Expand Down
4 changes: 0 additions & 4 deletions configuration/package_manifests/nfpm.tedge-mapper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ deb:
- tedge_mapper (<= 0.8.1)

contents:
# binary
- src: .build/tedge-mapper
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/tedge-mapper-aws.service
dst: /lib/systemd/system/tedge-mapper-aws.service
Expand Down
6 changes: 2 additions & 4 deletions configuration/package_manifests/nfpm.tedge-watchdog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ vendor: "thin-edge.io"
homepage: "https://thin-edge.io"
license: "Apache-2.0"

depends:
- tedge
replaces:
- tedge_watchdog
conflicts:
Expand All @@ -28,10 +30,6 @@ deb:
- tedge_watchdog (<= 0.8.1)

contents:
# binary
- src: .build/tedge-watchdog
dst: /usr/bin/

# service definitions
- src: ./configuration/init/systemd/tedge-watchdog.service
dst: /lib/systemd/system/
Expand Down
13 changes: 12 additions & 1 deletion crates/core/tedge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ repository = { workspace = true }
readme = "README.md"

[dependencies]
anstyle = { workspace = true }
anyhow = { workspace = true }
base64 = { workspace = true }
c8y-configuration-plugin = { workspace = true }
c8y-firmware-plugin = { workspace = true }
c8y-remote-access-plugin = { workspace = true }
camino = { workspace = true }
certificate = { workspace = true }
clap = { workspace = true, features = ["cargo", "derive"] }
clap = { workspace = true, features = ["cargo", "derive", "string", "unstable-styles"] }
doku = { workspace = true }
hyper = { workspace = true, default-features = false }
nix = { workspace = true }
pad = { workspace = true }
reqwest = { workspace = true, features = [
"blocking",
Expand All @@ -30,9 +35,15 @@ rumqttc = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
strum_macros = { workspace = true }
tedge-agent = { workspace = true }
tedge-apt-plugin = { workspace = true }
tedge-log-plugin = { workspace = true }
tedge-mapper = { workspace = true }
tedge-watchdog = { workspace = true }
tedge_config = { workspace = true }
tedge_utils = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
toml = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }
Expand Down
82 changes: 81 additions & 1 deletion crates/core/tedge/src/cli/init.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,106 @@
use crate::command::BuildContext;
use crate::command::Command;
use crate::Component;
use anyhow::bail;
use anyhow::Context;
use clap::Subcommand;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use tedge_utils::file::create_directory;
use tedge_utils::file::PermissionEntry;

#[derive(Debug)]
pub struct TEdgeInitCmd {
user: String,
group: String,
relative_links: bool,
context: BuildContext,
}

impl TEdgeInitCmd {
pub fn new(user: String, group: String, context: BuildContext) -> Self {
pub fn new(user: String, group: String, relative_links: bool, context: BuildContext) -> Self {
Self {
user,
group,
relative_links,
context,
}
}

fn initialize_tedge(&self) -> anyhow::Result<()> {
let executable_name =
std::env::current_exe().context("retrieving the current executable name")?;
let stat = std::fs::metadata(&executable_name).with_context(|| {
format!(
"reading metadata for the current executable ({})",
executable_name.display()
)
})?;
let Some(executable_dir) = executable_name.parent() else {
bail!(
"current executable ({}) does not have a parent directory",
executable_name.display()
)
};
let Some(executable_file_name) = executable_name.file_name() else {
bail!(
"current executable ({}) does not have a file name",
executable_name.display()
)
};

let component_subcommands: Vec<String> =
Component::augment_subcommands(clap::Command::new("tedge"))
.get_subcommands()
.map(|c| c.get_name().to_owned())
.chain(["tedge-apt-plugin".to_owned()])
.collect();

for component in &component_subcommands {
let link = executable_dir.join(component);
match std::fs::symlink_metadata(&link) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => bail!(
"couldn't read metadata for {}. do you need to run with sudo?",
link.display()
),
meta => {
let file_exists = meta.is_ok();
if file_exists {
nix::unistd::unlink(&link).with_context(|| {
format!("removing old version of {component} at {}", link.display())
})?;
}

let tedge = if self.relative_links {
Path::new(executable_file_name)
} else {
&*executable_name
};
std::os::unix::fs::symlink(tedge, &link).with_context(|| {
format!("creating symlink for {component} to {}", tedge.display())
})?;

let res = std::process::Command::new("chown")
.arg("--no-dereference")
.arg(&format!("{}:{}", stat.uid(), stat.gid()))
.arg(&link)
.output()
.with_context(|| {
format!(
"executing chown to change ownership of symlink at {}",
link.display()
)
})?;
anyhow::ensure!(
res.status.success(),
"failed to change ownership of symlink at {}\n\nSTDERR: {}",
link.display(),
String::from_utf8_lossy(&res.stderr),
)
}
}
}

let config_dir = self.context.config_location.tedge_config_root_path.clone();
create_directory(
&config_dir,
Expand Down
Loading

1 comment on commit 5a52630

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
301 0 3 301 100 54m36.198999999s

Please sign in to comment.