1212 * - agent config: /home/baudbot_agent/.config/.env
1313 */
1414
15+ import { execFileSync } from "node:child_process" ;
1516import fs from "node:fs" ;
1617import os from "node:os" ;
1718import path from "node:path" ;
@@ -48,6 +49,7 @@ export function usageText() {
4849 " --broker-url URL Broker base URL (e.g. https://broker.example.com)" ,
4950 " --workspace-id ID Slack workspace ID (e.g. T0123ABCD)" ,
5051 " --registration-token TOKEN Registration token from dashboard callback (required)" ,
52+ " --no-restart Skip automatic agent restart after registration" ,
5153 " -v, --verbose Show detailed registration progress" ,
5254 " -h, --help Show this help" ,
5355 "" ,
@@ -62,6 +64,7 @@ export function parseArgs(argv) {
6264 registrationToken : "" ,
6365 verbose : false ,
6466 help : false ,
67+ noRestart : false ,
6568 } ;
6669
6770 for ( let i = 0 ; i < argv . length ; i ++ ) {
@@ -77,6 +80,11 @@ export function parseArgs(argv) {
7780 continue ;
7881 }
7982
83+ if ( arg === "--no-restart" ) {
84+ out . noRestart = true ;
85+ continue ;
86+ }
87+
8088 if ( arg . startsWith ( "--broker-url=" ) ) {
8189 out . brokerUrl = arg . slice ( "--broker-url=" . length ) ;
8290 continue ;
@@ -558,6 +566,32 @@ export function isMainModule(moduleUrl = import.meta.url, argv1 = process.argv[1
558566 return moduleUrl === argvUrl || ( argvRealUrl !== "" && moduleUrl === argvRealUrl ) ;
559567}
560568
569+ export function hasSystemd ( ) {
570+ try {
571+ fs . accessSync ( "/run/systemd/system" , fs . constants . F_OK ) ;
572+ return true ;
573+ } catch {
574+ return false ;
575+ }
576+ }
577+
578+ export function restartAgent ( { logger = ( ) => { } , execFileSyncImpl = execFileSync } = { } ) {
579+ if ( ! hasSystemd ( ) ) {
580+ console . log ( "⚠️ systemd not available — restart the agent manually." ) ;
581+ return false ;
582+ }
583+
584+ logger ( "Restarting agent via systemctl..." ) ;
585+ try {
586+ execFileSyncImpl ( "systemctl" , [ "restart" , "baudbot" ] , { stdio : "inherit" } ) ;
587+ console . log ( "✅ Agent restarted." ) ;
588+ return true ;
589+ } catch {
590+ console . error ( "⚠️ Agent restart failed — run: sudo baudbot restart" ) ;
591+ return false ;
592+ }
593+ }
594+
561595export async function main ( argv = process . argv . slice ( 2 ) ) {
562596 const parsed = parseArgs ( argv ) ;
563597
@@ -597,7 +631,11 @@ export async function main(argv = process.argv.slice(2)) {
597631 console . log ( ` - ${ target . path } ` ) ;
598632 }
599633
600- console . log ( "Next step: sudo baudbot restart" ) ;
634+ if ( parsed . noRestart ) {
635+ console . log ( "Next step: sudo baudbot restart" ) ;
636+ } else {
637+ restartAgent ( { logger } ) ;
638+ }
601639
602640 return 0 ;
603641}
0 commit comments