Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
[hcl] make some fields optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed May 20, 2023
1 parent 5069676 commit 1f8f8cf
Show file tree
Hide file tree
Showing 30 changed files with 249 additions and 158 deletions.
6 changes: 1 addition & 5 deletions examples/angular/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ service "angular" {
description = "Angular example"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "demo_namespace"
stdout = "/tmp/angular-stdout.log"
stderr = "/tmp/angular-stderr.log"

port = 4200

build = {
Expand Down
5 changes: 0 additions & 5 deletions examples/deno-fresh/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "deno" {
description = "Deno example app"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "deno_namespace"
stdout = "/tmp/deno-stdout.log"
stderr = "/tmp/deno-stderr.log"
port = 8000

#use "flox" {
Expand Down
5 changes: 0 additions & 5 deletions examples/devbox-examples/deno-fresh/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "deno" {
description = "Deno example app"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "deno_namespace"
stdout = "/tmp/deno-stdout.log"
stderr = "/tmp/deno-stderr.log"
port = 8000

use "devbox" {
Expand Down
5 changes: 0 additions & 5 deletions examples/devenv-examples/deno-fresh/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "deno" {
description = "Deno example app"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "deno_namespace"
stdout = "/tmp/deno-stdout.log"
stderr = "/tmp/deno-stderr.log"
port = 8000

use "devenv" { }
Expand Down
5 changes: 0 additions & 5 deletions examples/nginx-golang-mysql/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ service "mysql" {
MYSQL_PID_FILE = "./.mysql/./mysql.pid"
MYSQL_UNIX_PORT = "./.mysql/mysql.sock"
}
autostart = true
autorestart = false
namespace = "demo_namespace"
stdout = "/tmp/mysql-stdout.log"
stderr = "/tmp/mysql-stderr.log"
flox = {
environment = ".#nginx-golang-mysql"
}
Expand Down
10 changes: 0 additions & 10 deletions examples/nginx-nodejs-redis/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ service "nodejs" {
env = {
REDIS_HOST = "redis"
}
autostart = true
autorestart = false
namespace = "demo_namespace"
stdout = "/tmp/nodejs-stdout.log"
stderr = "/tmp/nodejs-stderr.log"
port = 5005
build = {
command = "npm install"
Expand All @@ -35,11 +30,6 @@ service "redis" {
description = "Redis Service Example"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "demo_namespace"
stdout = "/tmp/redis-stdout.log"
stderr = "/tmp/redis-stderr.log"
port = 6379

#use "flox" {
Expand Down
5 changes: 0 additions & 5 deletions examples/nix-examples/deno-fresh/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "deno" {
description = "Deno example app"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "deno_namespace"
stdout = "/tmp/deno-stdout.log"
stderr = "/tmp/deno-stderr.log"
port = 8000

use "nix" { }
Expand Down
7 changes: 1 addition & 6 deletions examples/react-nginx/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ service "react" {
working_dir = "."
description = "React app"
depends_on = []
env = { }
autostart = true
autorestart = false
namespace = "demo_namespace"
stdout = "/tmp/react-stdout.log"
stderr = "/tmp/react-stderr.log"
env = {}
flox = {
environment = ".#react-nginx"
}
Expand Down
5 changes: 0 additions & 5 deletions examples/wasm-examples/lunatic/tcp-echo-server/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "tcp_echo_server" {
description = "TCP echo server demo"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "tcp_echo_namespace"
stdout = "/tmp/tcp_echo_server-stdout.log"
stderr = "/tmp/tcp_echo_server-stderr.log"
port = 4000

use "wasm" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "http_server" {
description = "HTTP server demo"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "http_namespace"
stdout = "/tmp/http_server-stdout.log"
stderr = "/tmp/demo_server-stderr.log"
port = 3000

use "wasm" {
Expand Down
9 changes: 1 addition & 8 deletions examples/wasm-examples/spin/http-server/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ service "http" {
working_dir = "."
description = "HTTP server demo"
depends_on = []
env = {
"GITHUB_DOMAIN" = "github.com"
}
autostart = true
autorestart = false
namespace = "http_namespace"
stdout = "/tmp/http-stdout.log"
stderr = "/tmp/http-stderr.log"
env = { }
port = 3000

use "wasm" {
Expand Down
5 changes: 0 additions & 5 deletions examples/wasm-examples/wasmedge/server-warp/Superfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ service "http_server" {
description = "HTTP server demo"
depends_on = []
env = {}
autostart = true
autorestart = false
namespace = "http_namespace"
stdout = "/tmp/http_server-stdout.log"
stderr = "/tmp/demo_server-stderr.log"
port = 8080

use "wasm" {
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ pub fn execute_new(cfg_format: ConfigFormat) {
depends_on: vec![],
dependencies: vec![],
env,
autostart: true,
autorestart: false,
namespace: Some("demo_namespace".to_string()),
autostart: None,
autorestart: None,
namespace: None,
port: None,
stdout: "/tmp/demo-stdout.log".to_string(),
stderr: "/tmp/demo-stderr.log".to_string(),
stdout: None,
stderr: None,
wait_for: None,
build: None,
r#use: None,
Expand Down
33 changes: 28 additions & 5 deletions src/graphql/schema/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use futures_util::Stream;
use tokio_stream::StreamExt;

use crate::{
default_stdout,
graphql::{schema::objects::subscriptions::TailLogStream, simple_broker::SimpleBroker},
types::configuration::ConfigurationData,
util::read_lines,
Expand Down Expand Up @@ -51,8 +52,13 @@ impl LoggingQuery {
.iter()
.find(|(_, s)| s.id == Some(id.to_string()))
.ok_or_else(|| Error::new("Service not found"))?;

let log_file = File::open(&service.stdout).map_err(|e| Error::new(e.to_string()))?;
let log_file = File::open(
&service
.stdout
.clone()
.unwrap_or(default_stdout!(config.project, service.name)),
)
.map_err(|e| Error::new(e.to_string()))?;

let reader = BufReader::new(log_file);

Expand Down Expand Up @@ -88,7 +94,12 @@ impl LoggingQuery {
.find(|(_, s)| s.id == Some(id.to_string()))
.ok_or_else(|| Error::new("Service not found"))?;

let lines = read_lines(&service.stdout)?;
let lines = read_lines(
&service
.stdout
.clone()
.unwrap_or(default_stdout!(config.project, service.name)),
)?;

Ok(Log { lines })
}
Expand Down Expand Up @@ -124,7 +135,13 @@ impl LoggingSubscription {
.find(|(_, s)| s.id == Some(id.to_string()))
.ok_or_else(|| Error::new("Service not found"))?;

let log_file = File::open(&service.stdout).map_err(|e| Error::new(e.to_string()))?;
let log_file = File::open(
&service
.stdout
.clone()
.unwrap_or(default_stdout!(config.project, service.name)),
)
.map_err(|e| Error::new(e.to_string()))?;

let reader = BufReader::new(log_file);

Expand Down Expand Up @@ -183,7 +200,13 @@ impl LoggingSubscription {
.find(|(_, s)| s.id == Some(id.to_string()))
.ok_or_else(|| Error::new("Service not found"))?;

let log_file = File::open(&service.stdout).map_err(|e| Error::new(e.to_string()))?;
let log_file = File::open(
&service
.stdout
.clone()
.unwrap_or(default_stdout!(config.project, service.name)),
)
.map_err(|e| Error::new(e.to_string()))?;

let cloned_id = id.clone();
thread::spawn(move || {
Expand Down
21 changes: 11 additions & 10 deletions src/graphql/schema/objects/project_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use names::Generator;
use tokio::sync::mpsc;

use crate::{
default_stdout,
graphql::simple_broker::SimpleBroker,
superviseur::core::SuperviseurCommand,
types::configuration::{ConfigurationData, Service},
Expand Down Expand Up @@ -86,14 +87,10 @@ impl ProjectConfiguration {
r#type: service.r#type.unwrap_or("exec".to_string()),
command: service.command,
working_dir: working_directory,
stdout: service
.log_file
.unwrap_or(format!("/tmp/stdout-{}.log", service_id.clone())),
stderr: service
.stderr_file
.unwrap_or(format!("/tmp/stderr-{}.log", service_id)),
autorestart: service.auto_restart.unwrap_or(false),
autostart: service.auto_start.unwrap_or(false),
stdout: service.log_file,
stderr: service.stderr_file,
autorestart: service.auto_restart,
autostart: service.auto_start,
env,
depends_on: service.depends_on.unwrap_or(vec![]),
port: service.port,
Expand Down Expand Up @@ -155,10 +152,14 @@ impl ProjectConfiguration {
let mut stdout = vec![];
for (_, service) in &config.services {
// loop while the file does not exist
while !Path::new(&service.stdout).exists() {
let stdout_file = &service
.clone()
.stdout
.unwrap_or(default_stdout!(config.project, service.name));
while !Path::new(stdout_file).exists() {
sleep(Duration::from_secs(2));
}
let lines = read_lines(&service.stdout).unwrap();
let lines = read_lines(&stdout_file).unwrap();
stdout.extend(lines);
}
Ok(stdout)
Expand Down
12 changes: 6 additions & 6 deletions src/graphql/schema/objects/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl From<&mut types::configuration::Service> for Service {
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect(),
auto_restart: service.autorestart,
auto_restart: service.autorestart.unwrap_or_default(),
depends_on: service.dependencies.clone(),
working_directory: service.working_dir.clone(),
log_file: service.stdout.clone(),
stderr_file: service.stderr.clone(),
log_file: service.stdout.clone().unwrap_or_default(),
stderr_file: service.stderr.clone().unwrap_or_default(),
port: service.port.map(|x| x as i32),
..Default::default()
}
Expand Down Expand Up @@ -134,11 +134,11 @@ impl From<&types::configuration::Service> for Service {
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect(),
auto_restart: service.autorestart,
auto_restart: service.autorestart.unwrap_or_default(),
depends_on: service.dependencies.clone(),
working_directory: service.working_dir.clone(),
log_file: service.stdout.clone(),
stderr_file: service.stderr.clone(),
log_file: service.stdout.clone().unwrap_or_default(),
stderr_file: service.stderr.clone().unwrap_or_default(),
port: service.port.map(|x| x as i32),
..Default::default()
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ pub mod api {
}
}
}

macro_rules! default_stderr {
($project: expr, $service: expr) => {
format!("/tmp/{}-{}.err", $project, $service)
};
}

macro_rules! default_stdout {
($project: expr, $service: expr) => {
format!("/tmp/{}-{}.log", $project, $service)
};
}

pub(crate) use default_stderr;
pub(crate) use default_stdout;
13 changes: 10 additions & 3 deletions src/server/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
StartResponse, StatusRequest, StatusResponse, StopRequest, StopResponse,
},
},
default_stderr, default_stdout,
superviseur::{
core::{ProcessEvent, Superviseur, SuperviseurCommand},
drivers::setup_drivers,
Expand Down Expand Up @@ -429,9 +430,15 @@ impl ControlService for Control {
description: service.description.clone(),
working_dir: service.working_dir.clone(),
env: service.env.clone(),
auto_restart: service.autorestart,
stdout: service.stdout.clone(),
stderr: service.stderr.clone(),
auto_restart: service.autorestart.unwrap_or(false),
stdout: service
.stdout
.clone()
.unwrap_or(default_stdout!(config.project, service.name)),
stderr: service
.stderr
.clone()
.unwrap_or(default_stderr!(config.project, service.name)),
port: service.port,
..Default::default()
});
Expand Down
Loading

0 comments on commit 1f8f8cf

Please sign in to comment.