2024-05-11 00:37:40 +00:00
package main
import (
"flag"
"fmt"
2024-05-13 16:49:53 +00:00
"log/slog"
2024-05-11 00:37:40 +00:00
"os"
2024-05-13 16:49:53 +00:00
"github.com/gookit/config/v2"
"github.com/kardianos/service"
2024-05-11 00:37:40 +00:00
)
type program struct {
exit chan struct { }
2024-05-13 20:12:45 +00:00
agent * Agent
config * ConfigFile
2024-05-11 00:37:40 +00:00
}
func ( p * program ) Start ( s service . Service ) error {
if service . Interactive ( ) {
slog . Info ( "Running in terminal." )
} else {
slog . Info ( "Running under service manager." )
}
p . exit = make ( chan struct { } )
// Start should not block. Do the actual work async.
go p . run ( )
return nil
}
func ( p * program ) run ( ) {
2024-05-13 20:12:45 +00:00
p . agent = NewAgent ( p . config . SecretKey , p . config . OneUptimeURL )
2024-05-11 00:37:40 +00:00
p . agent . Start ( )
if service . Interactive ( ) {
slog . Info ( "Running in terminal." )
2024-05-13 20:12:45 +00:00
NewShutdownHook ( ) . Close ( func ( ) {
2024-05-11 00:37:40 +00:00
slog . Info ( "Service Exiting..." )
p . agent . Close ( )
} )
} else {
slog . Info ( "Running under service manager." )
for {
select {
case _ , ok := <- p . exit :
if ! ok {
slog . Info ( "Service Exiting..." )
p . agent . Close ( )
return
}
}
}
}
}
func ( p * program ) Stop ( s service . Service ) error {
close ( p . exit )
return nil
}
func main ( ) {
// Set up the configuration
config . WithOptions ( config . WithTagName ( "json" ) )
cfg := newConfigFile ( )
// Set up the service
svcConfig := & service . Config {
Name : "oneuptime-infrastructure-agent" ,
DisplayName : "OneUptime Infrastructure Agent" ,
2024-05-14 11:23:15 +00:00
Description : "The OneUptime Infrastructure Agent is a lightweight, open-source agent that collects system metrics and sends them to the OneUptime platform. It is designed to be easy to configure and use, and to be extensible." ,
2024-05-11 00:37:40 +00:00
Arguments : [ ] string { "run" } ,
}
// Set up the program
prg := & program {
config : cfg ,
}
// Create the service
s , err := service . New ( prg , svcConfig )
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if len ( os . Args ) > 1 {
cmd := os . Args [ 1 ]
switch cmd {
2024-05-14 11:23:15 +00:00
case "configure" :
installFlags := flag . NewFlagSet ( "configure" , flag . ExitOnError )
2024-05-11 00:37:40 +00:00
secretKey := installFlags . String ( "secret-key" , "" , "Secret key (required)" )
oneuptimeURL := installFlags . String ( "oneuptime-url" , "" , "Oneuptime endpoint root URL (required)" )
err := installFlags . Parse ( os . Args [ 2 : ] )
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
prg . config . SecretKey = * secretKey
prg . config . OneUptimeURL = * oneuptimeURL
if prg . config . SecretKey == "" || prg . config . OneUptimeURL == "" {
2024-05-14 11:23:15 +00:00
slog . Error ( "The --secret-key and --oneuptime-url flags are required for the 'configure' command" )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
// save configuration
err = prg . config . save ( prg . config . SecretKey , prg . config . OneUptimeURL )
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
// Install the service
if err := s . Install ( ) ; err != nil {
2024-05-14 11:23:15 +00:00
slog . Error ( "Failed to configure service: " , err )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
2024-05-14 09:02:48 +00:00
fmt . Println ( "Service installed. Run the service using 'oneuptime-infrastructure-agent start'" )
2024-05-11 00:37:40 +00:00
case "start" :
err := prg . config . loadConfig ( )
if os . IsNotExist ( err ) {
2024-05-14 11:23:15 +00:00
slog . Error ( "Service configuration not found. Please run 'oneuptime-infrastructure-agent configure' to configure the service." )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if err != nil || prg . config . SecretKey == "" || prg . config . OneUptimeURL == "" {
2024-05-14 11:23:15 +00:00
slog . Error ( "Service configuration not found or is incomplete. Please run 'oneuptime-infrastructure-agent configure' to configure the service." )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
err = s . Start ( )
if err != nil {
2024-05-14 09:02:48 +00:00
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-14 10:15:15 +00:00
os . Exit ( 1 )
2024-05-11 00:37:40 +00:00
}
2024-05-14 09:02:48 +00:00
slog . Info ( "OneUptime Infrastructure Agent Started" )
2024-05-11 00:37:40 +00:00
case "run" :
err := prg . config . loadConfig ( )
if os . IsNotExist ( err ) {
2024-05-14 11:23:15 +00:00
slog . Error ( "Service configuration not found. Please run 'oneuptime-infrastructure-agent configure' to configure the service." )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
if err != nil || prg . config . SecretKey == "" || prg . config . OneUptimeURL == "" {
2024-05-14 11:23:15 +00:00
slog . Error ( "Service configuration not found or is incomplete. Please run 'oneuptime-infrastructure-agent configure' to configure the service." )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
err = s . Run ( )
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-14 10:15:15 +00:00
os . Exit ( 1 )
2024-05-11 00:37:40 +00:00
}
2024-05-14 09:02:48 +00:00
2024-05-11 00:37:40 +00:00
case "uninstall" , "stop" , "restart" :
err := service . Control ( s , cmd )
2024-05-14 10:51:36 +00:00
2024-05-11 00:37:40 +00:00
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
2024-05-14 10:51:36 +00:00
2024-05-11 00:37:40 +00:00
if cmd == "uninstall" {
// remove configuration file
err := prg . config . removeConfigFile ( )
if err != nil {
2024-05-13 19:28:21 +00:00
slog . Error ( err . Error ( ) )
2024-05-11 00:37:40 +00:00
os . Exit ( 2 )
}
slog . Info ( "Service Uninstalled" )
}
2024-05-14 10:51:36 +00:00
2024-05-11 00:37:40 +00:00
if cmd == "stop" {
slog . Info ( "Service Stopped" )
}
2024-05-14 09:02:48 +00:00
2024-05-14 10:51:36 +00:00
if cmd == "restart" {
slog . Info ( "Service Restarted" )
}
2024-05-13 20:22:49 +00:00
// add help command
case "help" :
2024-05-14 11:23:15 +00:00
fmt . Println ( "Usage: oneuptime-infrastructure-agent configure | uninstall | start | stop | restart" )
2024-05-14 09:02:48 +00:00
2024-05-11 00:37:40 +00:00
default :
slog . Error ( "Invalid command" )
os . Exit ( 2 )
}
} else {
2024-05-14 11:23:15 +00:00
fmt . Println ( "Usage: oneuptime-infrastructure-agent configure | uninstall | start | stop | restart" )
2024-05-11 00:37:40 +00:00
}
}