-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from fluentci-io/feat/support-for-services
feat: add commands for managing services (up, down, status, ps)
- Loading branch information
Showing
13 changed files
with
439 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ on: | |
jobs: | ||
build: | ||
name: release | ||
runs-on: ubuntu-latest | ||
runs-on: macos-latest | ||
strategy: | ||
matrix: | ||
target: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { green, procfile } from "../../deps.ts"; | ||
import { getProcfiles } from "../utils.ts"; | ||
|
||
export default async function down() { | ||
const files = await getProcfiles(); | ||
const services = []; | ||
// deno-lint-ignore no-explicit-any | ||
let infos: Record<string, any> = {}; | ||
|
||
for (const file of files) { | ||
const manifest = procfile.parse(Deno.readTextFileSync(file)); | ||
services.push(...Object.keys(manifest)); | ||
infos = { | ||
...infos, | ||
...manifest, | ||
}; | ||
|
||
for (const service of Object.keys(manifest)) { | ||
const socket = file.replace("Procfile", ".overmind.sock"); | ||
infos[service].socket = socket; | ||
const command = new Deno.Command("sh", { | ||
args: ["-c", `echo stop | nc -U -w 1 ${socket}`], | ||
stdout: "piped", | ||
}); | ||
const process = await command.spawn(); | ||
const { success } = await process.output(); | ||
if (!success) { | ||
console.log(`Failed to stop ${green(service)}`); | ||
continue; | ||
} | ||
console.log(`Successfully stopped ${green(service)}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { green, procfile } from "../../deps.ts"; | ||
import { getProcfiles } from "../utils.ts"; | ||
|
||
export default async function echo(name: string) { | ||
const files = await getProcfiles(); | ||
const services = []; | ||
// deno-lint-ignore no-explicit-any | ||
let infos: Record<string, any> = {}; | ||
|
||
for (const file of files) { | ||
const manifest = procfile.parse(Deno.readTextFileSync(file)); | ||
services.push(...Object.keys(manifest)); | ||
infos = { | ||
...infos, | ||
...manifest, | ||
}; | ||
|
||
for (const service of Object.keys(manifest)) { | ||
const socket = file.replace("Procfile", ".overmind.sock"); | ||
infos[service].socket = socket; | ||
} | ||
} | ||
|
||
if (!infos[name]) { | ||
console.log("Service not found in Procfile"); | ||
Deno.exit(1); | ||
} | ||
|
||
const socket = infos[name].socket; | ||
const command = new Deno.Command("sh", { | ||
args: ["-c", `echo echo | nc -U ${socket}`], | ||
stdout: "inherit", | ||
stderr: "inherit", | ||
}); | ||
const process = await command.spawn(); | ||
const { success } = await process.output(); | ||
if (!success) { | ||
console.log(`Failed to stream logs for ${green(name)}`); | ||
Deno.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { procfile, Table } from "../../deps.ts"; | ||
import { getProcfiles, getServicePid } from "../utils.ts"; | ||
|
||
export default async function listServices() { | ||
const files = await getProcfiles(); | ||
const services = []; | ||
// deno-lint-ignore no-explicit-any | ||
let infos: Record<string, any> = {}; | ||
|
||
for (const file of files) { | ||
const manifest = procfile.parse(Deno.readTextFileSync(file)); | ||
services.push(...Object.keys(manifest)); | ||
infos = { | ||
...infos, | ||
...manifest, | ||
}; | ||
|
||
for (const service of Object.keys(manifest)) { | ||
const socket = file.replace("Procfile", ".overmind.sock"); | ||
infos[service].socket = socket; | ||
const command = new Deno.Command("sh", { | ||
args: ["-c", `echo status | nc -U -w 1 ${socket}`], | ||
stdout: "piped", | ||
}); | ||
const process = await command.spawn(); | ||
const { stdout, success } = await process.output(); | ||
if (!success) { | ||
infos[service].status = "Stopped"; | ||
continue; | ||
} | ||
const decoder = new TextDecoder(); | ||
infos[service].status = decoder.decode(stdout).includes("running") | ||
? "Up" | ||
: "Stopped"; | ||
} | ||
} | ||
|
||
services.sort(); | ||
|
||
const table = new Table(); | ||
table.header(["PROCESS", "PID", "STATUS", "COMMAND"]); | ||
for (const service of services) { | ||
const pid = await getServicePid(service, infos[service].socket); | ||
table.push([ | ||
service, | ||
pid, | ||
infos[service].status, | ||
infos[service].command + " " + infos[service].options.join(" "), | ||
]); | ||
} | ||
table.render(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { green, procfile } from "../../deps.ts"; | ||
import { getProcfiles } from "../utils.ts"; | ||
|
||
export default async function restart(name: string) { | ||
const files = await getProcfiles(); | ||
const services = []; | ||
// deno-lint-ignore no-explicit-any | ||
let infos: Record<string, any> = {}; | ||
|
||
for (const file of files) { | ||
const manifest = procfile.parse(Deno.readTextFileSync(file)); | ||
services.push(...Object.keys(manifest)); | ||
infos = { | ||
...infos, | ||
...manifest, | ||
}; | ||
|
||
for (const service of Object.keys(manifest)) { | ||
const socket = file.replace("Procfile", ".overmind.sock"); | ||
infos[service].socket = socket; | ||
} | ||
} | ||
|
||
if (!infos[name]) { | ||
console.log("Service not found in Procfile"); | ||
Deno.exit(1); | ||
} | ||
|
||
const socket = infos[name].socket; | ||
const command = new Deno.Command("sh", { | ||
args: ["-c", `echo restart | nc -U -w 1 ${socket}`], | ||
stdout: "piped", | ||
}); | ||
const process = await command.spawn(); | ||
const { success } = await process.output(); | ||
if (!success) { | ||
console.log(`Failed to restart ${green(name)}`); | ||
return; | ||
} | ||
console.log(`Successfully restarted ${green(name)}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { brightGreen, gray, bold, procfile, Table, Cell } from "../../deps.ts"; | ||
import { getServicePid } from "../utils.ts"; | ||
|
||
export default async function status(name: string) { | ||
const command = new Deno.Command("bash", { | ||
args: ["-c", "ls .fluentci/*/Procfile"], | ||
stdout: "piped", | ||
}); | ||
const process = await command.spawn(); | ||
const { stdout, success } = await process.output(); | ||
if (!success) { | ||
console.log("No services running"); | ||
Deno.exit(0); | ||
} | ||
const decoder = new TextDecoder(); | ||
const files = decoder.decode(stdout).trim().split("\n"); | ||
const services = []; | ||
// deno-lint-ignore no-explicit-any | ||
let infos: Record<string, any> = {}; | ||
|
||
for (const file of files) { | ||
const manifest = procfile.parse(Deno.readTextFileSync(file)); | ||
services.push(...Object.keys(manifest)); | ||
infos = { | ||
...infos, | ||
...manifest, | ||
}; | ||
|
||
for (const service of Object.keys(manifest)) { | ||
infos[service].procfile = file; | ||
const socket = file.replace("Procfile", ".overmind.sock"); | ||
infos[service].socket = socket; | ||
const command = new Deno.Command("sh", { | ||
args: ["-c", `echo status | nc -U -w 1 ${socket}`], | ||
stdout: "piped", | ||
}); | ||
const process = await command.spawn(); | ||
const { stdout, success } = await process.output(); | ||
if (!success) { | ||
infos[service].status = "Stopped"; | ||
continue; | ||
} | ||
const decoder = new TextDecoder(); | ||
infos[service].status = decoder.decode(stdout).includes("running") | ||
? "Up" | ||
: "Stopped"; | ||
} | ||
} | ||
|
||
if (!infos[name]) { | ||
console.log("Service not found in Procfile"); | ||
Deno.exit(1); | ||
} | ||
|
||
const pid = await getServicePid(name, infos[name].socket); | ||
|
||
console.log( | ||
`${infos[name].status === "Up" ? brightGreen("●") : "○"} ${name}` | ||
); | ||
|
||
const table = new Table().body([ | ||
[ | ||
new Cell("Procfile:").align("right"), | ||
`${infos[name].procfile}\n└─ ${gray( | ||
infos[name].command + " " + infos[name].options.join(" ") | ||
)}`, | ||
], | ||
[ | ||
new Cell("Active:").align("right"), | ||
infos[name].status === "Up" | ||
? bold(brightGreen("active (running)")) | ||
: "inactive (dead)", | ||
], | ||
[new Cell("Socket:").align("right"), infos[name].socket], | ||
[new Cell("Main PID:").align("right"), pid], | ||
[ | ||
new Cell("WorkDir:").align("right"), | ||
infos[name].socket.replace("/.overmind.sock", ""), | ||
], | ||
]); | ||
table.render(); | ||
console.log(""); | ||
} |
Oops, something went wrong.