Skip to content

Commit 07950fb

Browse files
Script to convert legacy config files to values (NR-183332) (#333)
* Script to convert legacy config files to values. * Go releaser adding both binaries (#339)
1 parent e995fb0 commit 07950fb

37 files changed

+976
-133
lines changed

.goreleaser.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project_name: newrelic-super-agent
22
builds:
3-
-
3+
- id: newrelic-super-agent
44
main: ./build/dummy.go
55
goarch:
66
- arm64
@@ -10,21 +10,43 @@ builds:
1010
binary: newrelic-super-agent
1111
hooks:
1212
post:
13-
- cmd: sh ./build/scripts/build_super_agent.sh
13+
- cmd: sh ./build/scripts/build_binary.sh
1414
env:
1515
- ARCH={{ .Arch }}
1616
- BUILD_FEATURE=onhost
17+
- BIN=newrelic-super-agent
1718
- cmd: sh ./build/scripts/replace_go_with_rust.sh
1819
env:
1920
- ARCH={{ .Arch }}
21+
- BIN=newrelic-super-agent
2022
- cmd: sh ./build/scripts/download_embedded.sh
2123
env:
2224
- ARCH={{ .Arch }}
25+
- id: newrelic-config-migrate
26+
main: ./build/dummy.go
27+
goarch:
28+
- arm64
29+
- amd64
30+
goos:
31+
- linux
32+
binary: newrelic-config-migrate
33+
hooks:
34+
post:
35+
- cmd: sh ./build/scripts/build_binary.sh
36+
env:
37+
- ARCH={{ .Arch }}
38+
- BUILD_FEATURE=onhost
39+
- BIN=newrelic-config-migrate
40+
- cmd: sh ./build/scripts/replace_go_with_rust.sh
41+
env:
42+
- ARCH={{ .Arch }}
43+
- BIN=newrelic-config-migrate
2344
archives:
2445
- format: tar.gz
2546
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
2647
builds:
2748
- newrelic-super-agent
49+
- newrelic-config-migrate
2850

2951
nfpms:
3052
- package_name: newrelic-super-agent

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[package]
22
name = "newrelic_super_agent"
33
description = "New Relic Super Agent Limited Preview"
4-
version = "0.5.3"
4+
version = "0.6.1"
55
edition = "2021"
66
authors = ["The New Relic Core Agent & Open Standards (CAOS) Team"]
77
publish = false
8+
default-run = "newrelic-super-agent"
89

910
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1011

@@ -73,7 +74,11 @@ required-features = ["onhost"]
7374

7475
[[bin]]
7576
name = "newrelic-super-agent"
76-
path = "src/bin/main.rs"
77+
path = "src/bin/newrelic-super-agent.rs"
78+
79+
[[bin]]
80+
name = "newrelic-config-migrate"
81+
path = "src/bin/newrelic-config-migrate.rs"
7782

7883

7984
[features]

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ BUILD_MODE ?= release
3737
# Cross-compilation only works from amd64 host.
3838
build-super-agent:
3939
@echo "Building with mode: $(BUILD_MODE) and arch: $(ARCH)"
40-
ARCH=$(ARCH) BUILD_MODE=$(BUILD_MODE) ./build/scripts/build_super_agent.sh
40+
ARCH=$(ARCH) BUILD_MODE=$(BUILD_MODE) BIN="newrelic-super-agent" ./build/scripts/build_binary.sh
41+
42+
# Cross-compilation only works from amd64 host.
43+
build-config-migrate:
44+
@echo "Building with mode: $(BUILD_MODE) and arch: $(ARCH)"
45+
ARCH=$(ARCH) BUILD_MODE=$(BUILD_MODE) BIN="newrelic-config-migrate" ./build/scripts/build_binary.sh
4146

4247
.PHONY: build-dev-image
4348
build-dev-image:

build/rust.Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ ARG BUILD_MODE
2929
ENV BUILD_MODE_ENV=${BUILD_MODE}
3030
ARG BUILD_FEATURE
3131
ENV BUILD_FEATURE_ENV=${BUILD_FEATURE}
32+
ARG BUILD_BIN
33+
ENV BUILD_BIN_ENV=${BUILD_BIN}
3234

3335
# Execute the command dynamically at runtime
3436
CMD [ "sh", "-c", "\
3537
CMD_STRING='cargo build'; \
3638
[ \"$BUILD_MODE_ENV\" != 'debug' ] && CMD_STRING='cargo build --release'; \
3739
CMD_STRING=\"$CMD_STRING --features $BUILD_FEATURE_ENV\"; \
3840
CMD_STRING=\"$CMD_STRING --target $ARCH_NAME-unknown-linux-gnu\"; \
41+
CMD_STRING=\"$CMD_STRING --bin $BUILD_BIN_ENV\"; \
42+
CMD_STRING=\"$CMD_STRING --target-dir target-$BUILD_BIN_ENV\"; \
3943
$CMD_STRING \
4044
"]

build/scripts/build_binary.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
RUST_VERSION="1.71.1"
5+
6+
# compile production version of rust agent
7+
8+
if [ "$ARCH" = "arm64" ];then
9+
ARCH_NAME="aarch64"
10+
fi
11+
12+
if [ "$ARCH" = "amd64" ];then
13+
ARCH_NAME="x86_64"
14+
fi
15+
16+
: "${BUILD_MODE:=release}" # Default to release if not specified
17+
18+
if [ -z "${BIN}" ]; then
19+
BIN="newrelic-super-agent"
20+
echo "BIN not provided; defaulting to 'newrelic-super-agent'."
21+
fi
22+
23+
24+
if [ -z "${BUILD_FEATURE}" ]; then
25+
BUILD_FEATURE="onhost"
26+
echo "BUILD_FEATURE not provided; defaulting to 'onhost'."
27+
fi
28+
29+
docker build -t "rust-cross-${ARCH_NAME}-${BIN}" -f ./build/rust.Dockerfile --build-arg ARCH_NAME="${ARCH_NAME}" --build-arg BUILD_MODE="${BUILD_MODE}" --build-arg BUILD_FEATURE="${BUILD_FEATURE}" --build-arg BUILD_BIN="${BIN}" .
30+
31+
CARGO_HOME=/tmp/.cargo cargo fetch
32+
docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/app -v /tmp/.cargo:/usr/src/app/.cargo "rust-cross-${ARCH_NAME}-${BIN}"
33+
34+
mkdir -p "bin"
35+
36+
cp "./target-${BIN}/${ARCH_NAME}-unknown-linux-gnu/${BUILD_MODE}/${BIN}" "./bin/${BIN}-${ARCH}"

build/scripts/build_super_agent.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

build/scripts/replace_go_with_rust.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
set -e
33

44
if [ "$ARCH" = "arm64" ];then
5-
BINARY_PATH="./dist/newrelic-super-agent_linux_${ARCH}/newrelic-super-agent"
5+
BINARY_PATH="./dist/${BIN}_linux_${ARCH}/${BIN}"
66
fi
77

88
if [ "$ARCH" = "amd64" ];then
9-
BINARY_PATH="./dist/newrelic-super-agent_linux_${ARCH}_v1/newrelic-super-agent"
9+
BINARY_PATH="./dist/${BIN}_linux_${ARCH}_v1/${BIN}"
1010
fi
1111

1212
# move rust compiled files into goreleaser generated locations
13-
cp "./bin/newrelic-super-agent-${ARCH}" "${BINARY_PATH}"
13+
cp "./bin/${BIN}-${ARCH}" "${BINARY_PATH}"
1414

1515
# validate
1616
docker run --rm -v "$PWD":/usr/src/app -w /usr/src/app ubuntu /bin/bash -c "apt-get update && apt-get install tree -y && tree ./"
17-

src/bin/newrelic-config-migrate.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use log::{error, info};
2+
use newrelic_super_agent::config::store::{SuperAgentConfigLoader, SuperAgentConfigStoreFile};
3+
use newrelic_super_agent::config::super_agent_configs::{AgentID, AgentTypeFQN};
4+
use newrelic_super_agent::config_migrate::config::MigrationConfig;
5+
use newrelic_super_agent::config_migrate::converter::ConfigConverter;
6+
use newrelic_super_agent::config_migrate::defaults::{
7+
DEFAULT_AGENT_ID, NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING,
8+
};
9+
use newrelic_super_agent::config_migrate::legacy_config_renamer::LegacyConfigRenamer;
10+
use newrelic_super_agent::config_migrate::values_persister_file::ValuesPersisterFile;
11+
use newrelic_super_agent::logging::Logging;
12+
use std::error::Error;
13+
use std::path::PathBuf;
14+
const DEFAULT_CFG_PATH: &str = "/etc/newrelic-super-agent/config.yaml";
15+
16+
fn main() -> Result<(), Box<dyn Error>> {
17+
// init logging singleton
18+
Logging::try_init()?;
19+
20+
info!("Starting conversion tool...");
21+
22+
let config: MigrationConfig = serde_yaml::from_str(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING)?;
23+
24+
let config_converter = ConfigConverter::default();
25+
let values_persister = ValuesPersisterFile::default();
26+
let legacy_config_renamer = LegacyConfigRenamer::default();
27+
28+
for cfg in config.configs {
29+
let Ok(agent_type) = get_current_agent_type().map_err(|e| {
30+
error!("Error finding newrelic-super-agent config: {}", e);
31+
}) else {
32+
return Ok(());
33+
};
34+
35+
if cfg.agent_type_fqn != agent_type {
36+
error!("This script requires a config file and nr_infra_agent agent_type 0.0.2");
37+
return Ok(());
38+
}
39+
40+
match config_converter.convert(&cfg) {
41+
Ok(agent_variables) => {
42+
let values_content = serde_yaml::to_string(&agent_variables)?;
43+
values_persister.persist_values_file(
44+
&AgentID::new(DEFAULT_AGENT_ID)?,
45+
values_content.as_str(),
46+
)?;
47+
for (_, dir_path) in cfg.dirs_map {
48+
legacy_config_renamer.rename_path(dir_path.as_str())?;
49+
}
50+
for (_, file_path) in cfg.files_map {
51+
legacy_config_renamer.rename_path(file_path.as_str())?;
52+
}
53+
info!("Config files successfully converted");
54+
}
55+
Err(e) => {
56+
error!(
57+
"Conversion failed, old files or paths are renamed or not present: {}",
58+
e
59+
);
60+
}
61+
};
62+
}
63+
64+
Ok(())
65+
}
66+
67+
// TODO : Add tests for getting this agent_type and check the agent_type by namespace instead of name DEFAULT_AGENT_ID
68+
fn get_current_agent_type() -> Result<AgentTypeFQN, Box<dyn Error>> {
69+
let local_config_path = PathBuf::from(DEFAULT_CFG_PATH.to_string());
70+
let super_agent_config_storer =
71+
SuperAgentConfigStoreFile::new(&local_config_path).with_remote()?;
72+
let agents = super_agent_config_storer.load()?.agents;
73+
Ok(agents
74+
.get(&AgentID::new(DEFAULT_AGENT_ID)?)
75+
.cloned()?
76+
.agent_type)
77+
}

0 commit comments

Comments
 (0)