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

feat: add flox support and show more examples #23

Merged
merged 17 commits into from
Mar 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(superviseur): add stop_command to config
tsirysndr committed Mar 26, 2023
commit 8fd2850385ee59a5c1c8ef8fd0cb86af86f2d74a
5 changes: 4 additions & 1 deletion examples/nginx-golang-mysql/.gitignore
Original file line number Diff line number Diff line change
@@ -49,7 +49,8 @@ go.work
.LSOverride

# Icon must end with two \r
Icon
Icon


# Thumbnails
._*
@@ -74,4 +75,6 @@ Temporary Items
# iCloud generated files
*.icloud

.mysql

# End of https://www.toptal.com/developers/gitignore/api/go,macos,bazel
52 changes: 45 additions & 7 deletions examples/nginx-golang-mysql/Superfile.hcl
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
project = "nginx-golang-mysql"
services = [
{
"name" = "demo"
"name" = "nginx"
"type" = "exec"
"command" = "ping $GITHUB_DOMAIN"
"working_dir" = "/tmp"
"description" = "Ping Service Example"
"command" = "nginx --version"
"working_dir" = "."
"description" = "Nginx Service Example"
"depends_on" = ["go"]
"env" = { }
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
"stdout" = "/tmp/nginx-stdout.log"
"stderr" = "/tmp/nginx-stderr.log"
flox = {
"environment" = ".#nginx-golang-mysql"
}
},
{
"name" = "go"
"type" = "exec"
"command" = "go version"
"working_dir" = "."
"description" = "Go Service Example"
"depends_on" = ["mysql"]
"env" = { }
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
"stdout" = "/tmp/go-stdout.log"
"stderr" = "/tmp/go-stderr.log"
flox = {
"environment" = ".#nginx-golang-mysql"
}
},
{
"name" = "mysql"
"type" = "exec"
"command" = "mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE --socket=$PWD/$MYSQL_UNIX_PORT"
"stop_command" = "mysqladmin -u root --socket=$PWD/$MYSQL_UNIX_PORT shutdown"
"working_dir" = "."
"description" = "MySQL Service Example"
"depends_on" = []
"env" = {
"GITHUB_DOMAIN" = "github.com"
MYSQL_HOME = "./.mysql"
MYSQL_DATADIR = "./.mysql/data"
MYSQL_PID_FILE = "./.mysql/./mysql.pid"
MYSQL_UNIX_PORT = "./.mysql/mysql.sock"
}
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
"stdout" = "/tmp/demo-stdout.log"
"stderr" = "/tmp/demo-stderr.log"
"stdout" = "/tmp/mysql-stdout.log"
"stderr" = "/tmp/mysql-stderr.log"
flox = {
"environment" = ".#nginx-golang-mysql"
}
2 changes: 1 addition & 1 deletion examples/nginx-golang-mysql/proxy/nginx.conf
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend:8000;
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
}
}
36 changes: 32 additions & 4 deletions examples/nginx-nodejs-redis/Superfile.hcl
Original file line number Diff line number Diff line change
@@ -3,13 +3,11 @@ services = [
{
"name" = "nodejs"
"type" = "exec"
"command" = "ping $GITHUB_DOMAIN"
"command" = "npm start"
"working_dir" = "/tmp"
"description" = "Ping Service Example"
"depends_on" = []
"env" = {
"GITHUB_DOMAIN" = "github.com"
}
"env" = {}
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
@@ -18,5 +16,35 @@ services = [
flox = {
"environment" = ".#nginx-nodejs-redis"
}
},
{
"name" = "redis"
"type" = "exec"
"command" = "redis-server"
"description" = "Redis Service Example"
"depends_on" = []
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
"stdout" = "/tmp/redis-stdout.log"
"stderr" = "/tmp/redis-stderr.log"
flox = {
"environment" = ".#nginx-nodejs-redis"
}
},
{
"name" = "nginx"
"type" = "exec"
"command" = "nginx -g 'daemon off;'"
"description" = "Nginx Service Example"
"depends_on" = ["nodejs", "redis"]
"autostart" = true
"autorestart" = false
"namespace" = "demo_namespace"
"stdout" = "/tmp/nginx-stdout.log"
"stderr" = "/tmp/nginx-stderr.log"
flox = {
"environment" = ".#nginx-nodejs-redis"
}
}
]
59 changes: 59 additions & 0 deletions pkgs/nginx-golang-mysql/catalog.json
Original file line number Diff line number Diff line change
@@ -2,6 +2,65 @@
"nixpkgs-flox": {
"x86_64-darwin": {
"stable": {
"figlet": {
"latest": {
"cache": {
"out": {
"https://cache.nixos.org": {}
}
},
"element": {
"attrPath": [
"legacyPackages",
"x86_64-darwin",
"figlet"
],
"originalUrl": "flake:nixpkgs",
"storePaths": [
"/nix/store/y5sfzfnq984sf616ln1rjdkd2q99454p-figlet-2.2.5"
],
"url": "github:flox/nixpkgs/d0d55259081f0b97c828f38559cad899d351cad1"
},
"eval": {
"meta": {
"description": "Program for making large letters out of ordinary text",
"outputsToInstall": [
"out"
],
"position": "/nix/store/32cp7y0cf36h3xp0fqwy2nf432fpcaci-source/pkgs/tools/misc/figlet/default.nix:33",
"unfree": false
},
"name": "figlet-2.2.5",
"outputs": {
"out": "/nix/store/y5sfzfnq984sf616ln1rjdkd2q99454p-figlet-2.2.5"
},
"pname": "figlet",
"system": "x86_64-darwin",
"version": "2.2.5"
},
"publish_element": {
"attrPath": [
"evalCatalog",
"x86_64-darwin",
"stable",
"figlet"
],
"originalUrl": "flake:nixpkgs-flox",
"storePaths": [
"/nix/store/y5sfzfnq984sf616ln1rjdkd2q99454p-figlet-2.2.5"
],
"url": "flake:nixpkgs-flox/34ee8469ab62af53cd25a839de33950b19126dc8"
},
"schema": 1,
"source": {
"locked": {
"lastModified": 1676973346,
"revCount": 456418
}
},
"type": "catalogRender"
}
},
"go": {
"latest": {
"cache": {
43 changes: 43 additions & 0 deletions pkgs/nginx-golang-mysql/flox.nix
Original file line number Diff line number Diff line change
@@ -9,4 +9,47 @@
packages.nixpkgs-flox.nginx = {};
packages.nixpkgs-flox.go = {};
packages.nixpkgs-flox.mysql = {};
packages.nixpkgs-flox.figlet = {};

shell.hook = ''
figlet dev

MYSQL_HOME=$PWD/.mysql
MYSQL_DATADIR=$MYSQL_HOME/data
export MYSQL_UNIX_PORT=$MYSQL_HOME/mysql.sock
MYSQL_PID_FILE=$MYSQL_HOME/mysql.pid
alias mysql='mysql -u root'

# kill any running mysql instance
if [ -f $MYSQL_PID_FILE ]; then
kill $(cat $MYSQL_PID_FILE)
fi

if [ ! -d "$MYSQL_HOME" ]; then
# Make sure to use normal authentication method otherwise we can only
# connect with unix account. But users do not actually exists in nix.
mysql_install_db --auth-root-authentication-method=normal \
--datadir=$MYSQL_DATADIR \
--pid-file=$MYSQL_PID_FILE
fi

# Starts the daemon
#mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE \
# --socket=$MYSQL_UNIX_PORT 2> $MYSQL_HOME/mysql.log &
#MYSQL_PID=$!

#finish()
#{
# mysqladmin -u root --socket=$MYSQL_UNIX_PORT shutdown
# kill $MYSQL_PID
#}
#trap finish EXIT
'';

environmentVariables.MYSQL_DATABASE = "flox";
environmentVariables.MYSQL_ROOT_PASSWORD = "db-q5n2g";
environmentVariables.MYSQL_HOST = "localhost";
environmentVariables.MYSQL_TEST_DATABASE = "test";
environmentVariables.MYSQL_USER = "flox";
environmentVariables.MYSQL_PASSWORD = "db-q5n2g";
}
10 changes: 10 additions & 0 deletions pkgs/nginx-golang-mysql/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"elements": [
{
"active": true,
"attrPath": "evalCatalog.x86_64-darwin.stable.figlet",
"originalUrl": "flake:nixpkgs-flox",
"outputs": null,
"storePaths": [
"/nix/store/y5sfzfnq984sf616ln1rjdkd2q99454p-figlet-2.2.5"
],
"url": "flake:nixpkgs-flox/34ee8469ab62af53cd25a839de33950b19126dc8"
},
{
"active": true,
"attrPath": "evalCatalog.x86_64-darwin.stable.go",
1 change: 1 addition & 0 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ pub fn execute_new(cfg_format: ConfigFormat) {
name: "demo".to_string(),
r#type: "exec".to_string(),
command: "ping $GITHUB_DOMAIN".to_string(),
stop_command: None,
working_dir: "/tmp".to_string(),
watch_dir: None,
description: Some("Ping Service Example".to_string()),
51 changes: 49 additions & 2 deletions src/superviseur.rs
Original file line number Diff line number Diff line change
@@ -209,13 +209,14 @@ impl SuperviseurInternal {
"flox print-dev-env -A {}",
flox.environment.replace(".#", "")
);
std::process::Command::new("sh")
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();

let command = format!(
"flox activate -e {} -- {}",
@@ -257,7 +258,7 @@ impl SuperviseurInternal {
self.childs
.lock()
.unwrap()
.insert(service_key, child.id() as i32);
.insert(service_key, process.pid.unwrap() as i32);

let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
@@ -377,6 +378,52 @@ impl SuperviseurInternal {
}

fn handle_stop(&self, service: Service, project: String, restart: bool) -> Result<(), Error> {
if let Some(stop_command) = service.stop_command.clone() {
let envs = service.env.clone();
let working_dir = service.working_dir.clone();

match service.clone().flox {
Some(flox) => {
let stop_command =
format!("flox activate -e {} -- {}", flox.environment, stop_command);
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(stop_command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();
}
None => {
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(stop_command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();
}
};
let mut childs = self.childs.lock().unwrap();
let service_key = format!("{}-{}", project.clone(), service.name.clone());
childs.remove(&service_key);

self.event_tx
.send(ProcessEvent::Stopped(service.name.clone(), project.clone()))
.unwrap();
if restart {
self.cmd_tx
.send(SuperviseurCommand::Start(service, project))
.unwrap();
}
return Ok(());
}
let mut childs = self.childs.lock().unwrap();
let service_key = format!("{}-{}", project.clone(), service.name.clone());
match childs.get(&service_key) {
1 change: 1 addition & 0 deletions src/types/configuration.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ pub struct Service {
pub name: String,
pub r#type: String, // docker, podman, exec, wasm
pub command: String,
pub stop_command: Option<String>,
pub working_dir: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub watch_dir: Option<String>,