Skip to content

Commit

Permalink
Fix mode reg and install
Browse files Browse the repository at this point in the history
  • Loading branch information
the2pizza committed Jan 7, 2025
1 parent 5c2e095 commit f5eff9e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ jobs:
files: |
target/release/api-${{ env.ARCH }}
target/release/agent-${{ env.ARCH }}
deploy/install
docs/
README.md
config-agent-example.toml
config-api-example.toml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ users.json
xray-config-mk2nl.json
connection-lines.txt
dev/data
deploy/env
30 changes: 15 additions & 15 deletions deploy/install
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ PG_USERNAME="${PG_USERNAME:-postgres}"
PG_PASSWORD="${PG_PASSWORD:-password}"
API_ENDPOINT=${API_ENDPOINT:-http://localhost:3005}

sudo mkdir -p "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

echo "Installing xray-core version $XRAY_VERSION..."
curl -L -o xray.zip "$XRAY_URL"
sudo unzip -o xray.zip -d "$INSTALL_DIR"
sudo rm xray.zip
unzip -o xray.zip -d "$INSTALL_DIR"
rm xray.zip

echo "XRAY Core Version"
$INSTALL_DIR/xray --version

cat <<EOF | sudo tee /etc/systemd/system/xray.service
cat <<EOF | tee /etc/systemd/system/xray.service
[Unit]
Description=Xray Core Service
After=network.target
Expand All @@ -74,14 +74,14 @@ Restart=on-failure
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable xray
systemctl daemon-reload
systemctl enable xray

echo "Installing agent version $PONY_VERSION..."
curl -L -o agent "$AGENT_URL"
sudo chmod +x agent
chmod +x agent

cat <<EOF | sudo tee /etc/systemd/system/agent.service
cat <<EOF | tee /etc/systemd/system/agent.service
[Unit]
Description=Pony Agent Service
After=xray.service
Expand All @@ -97,8 +97,8 @@ RestartSec=5
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable agent
systemctl daemon-reload
systemctl enable agent

echo "Generating configurations..."

Expand All @@ -124,7 +124,7 @@ fi
if [[ -f "$XRAY_CONFIG_PATH" ]]; then
echo "File $XRAY_CONFIG_PATH already exist. skip."
else
cat <<EOF | sudo tee "$XRAY_CONFIG_PATH"
cat <<EOF | tee "$XRAY_CONFIG_PATH"
{
"log": {
"loglevel": "debug"
Expand Down Expand Up @@ -314,7 +314,7 @@ fi
if [[ -f "$PONY_CONFIG_PATH" ]]; then
echo "File $PONY_CONFIG_PATH already exist. skip."
else
cat <<EOF | sudo tee "$PONY_CONFIG_PATH"
cat <<EOF | tee "$PONY_CONFIG_PATH"
[debug]
enabled = true
web_server = "127.0.0.1"
Expand Down Expand Up @@ -344,17 +344,17 @@ sub_endpoint = "$ZMQ_ENDPOINT"
[node]
env = "$ENV"
uuid = $(./xray uuid)
uuid = "$(./xray uuid)"
hostname = "$HOSTNAME"
default_interface = "$default_interface"
ipv4 = "$default_ip"
[pg]
host = "${PG_HOST}"
port = "${PG_PORT}"
port = ${PG_PORT}
db = "${PG_DB}"
username = "${PG_USERNAME}"
password = "${PG_PASSWORD}
password = "${PG_PASSWORD}"
[api]
endpoint = "$API_ENDPOINT"
Expand Down
7 changes: 5 additions & 2 deletions src/bin/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = {
let settings = settings.clone();
debug!("----->>>>> Register node");
let _ =
agent::register_node(state.clone(), settings.clone(), settings.node.env.clone()).await;
if let Err(e) =
agent::register_node(state.clone(), settings.clone(), settings.node.env.clone()).await
{
panic!("Cannot register node {:?}", e);
}
};

if debug {
Expand Down
36 changes: 25 additions & 11 deletions src/jobs/agent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::{Duration, Utc};
use log::{debug, error};
use reqwest::Client;
use reqwest::Url;
use reqwest::{Client, StatusCode};
use std::{error::Error, sync::Arc};
use tokio::sync::Mutex;
use uuid::Uuid;
Expand Down Expand Up @@ -168,28 +169,41 @@ pub async fn register_node(
let node_state = state.lock().await;
let node = node_state.nodes.get(&env).clone();

let client = Client::new();
let mut endpoint = Url::parse(&settings.api.endpoint)?;
endpoint
.path_segments_mut()
.map_err(|_| "Invalid API endpoint")?
.push("node")
.push("register");
let endpoint = endpoint.to_string();

let endpoint = format!("{}/node/register", settings.api.endpoint);
debug!("ENDPOINT: {}", endpoint);

match serde_json::to_string_pretty(&node) {
Ok(json) => debug!("NODE {}", json),
Err(e) => error!("Error serializing to JSON: {}", e),
Ok(json) => debug!("Serialized node for environment '{}': {}", env, json),
Err(e) => error!("Error serializing node for environment '{}': {}", env, e),
}

if let Some(env) = node {
if let Some(node) = env.first() {
let res = client.post(endpoint).json(&node).send().await?;
if res.status().is_success() {
debug!("Req success!");
let res = Client::new()
.post(&endpoint)
.header("Content-Type", "application/json")
.json(&node)
.send()
.await?;

if res.status().is_success() || res.status() == StatusCode::NOT_MODIFIED {
return Ok(());
} else {
error!("Req error: {} {:?}", res.status(), res);
return Err(format!("Req error: {} {:?}", res.status(), res).into());
}
} else {
return Err("No nodes found in environment".into());
}
} else {
return Err("No data found for the given environment".into());
}

Ok(())
}

pub async fn init_state(
Expand Down

0 comments on commit f5eff9e

Please sign in to comment.