@@ -2,8 +2,11 @@ package serv
22
33import (
44 "context"
5+ "fmt"
56 "os"
67 "os/signal"
8+ "path/filepath"
9+ "runtime"
710 "strconv"
811 "syscall"
912
@@ -46,6 +49,8 @@ func (p *program) run() {
4649 utils .Logger .ErrorF ("error migrating logs table: %v" , err )
4750 }
4851
52+ ensureUpdaterServiceInstalled ()
53+
4954 ctx , cancel := context .WithCancel (context .Background ())
5055 defer cancel ()
5156 ctx = metadata .AppendToOutgoingContext (ctx , "key" , cnf .AgentKey )
@@ -68,3 +73,52 @@ func (p *program) run() {
6873 signal .Notify (signals , syscall .SIGINT , syscall .SIGTERM )
6974 <- signals
7075}
76+
77+ func ensureUpdaterServiceInstalled () {
78+ isInstalled , err := utils .CheckIfServiceIsInstalled ("UTMStackUpdater" )
79+ if err != nil {
80+ utils .Logger .ErrorF ("error checking if updater service is installed: %v" , err )
81+ return
82+ }
83+
84+ if isInstalled {
85+ utils .Logger .Info ("updater service is already installed" )
86+ return
87+ }
88+
89+ utils .Logger .Info ("updater service not found, installing..." )
90+
91+ updaterPath := filepath .Join (utils .GetMyPath (), fmt .Sprintf (config .UpdaterFile , "" ))
92+
93+ if ! utils .CheckIfPathExist (updaterPath ) {
94+
95+ cnf , err := config .GetCurrentConfig ()
96+ if err != nil {
97+ utils .Logger .ErrorF ("error getting config to download updater: %v" , err )
98+ return
99+ }
100+
101+ updaterBinary := fmt .Sprintf (config .UpdaterFile , "" )
102+ if err := utils .DownloadFile (fmt .Sprintf (config .DependUrl , cnf .Server , config .DependenciesPort , updaterBinary ), map [string ]string {}, updaterBinary , utils .GetMyPath (), cnf .SkipCertValidation ); err != nil {
103+ utils .Logger .ErrorF ("error downloading updater binary: %v" , err )
104+ return
105+ }
106+
107+ if runtime .GOOS == "linux" || runtime .GOOS == "darwin" {
108+ if err := utils .Execute ("chmod" , utils .GetMyPath (), "755" , updaterBinary ); err != nil {
109+ utils .Logger .ErrorF ("error setting permissions on updater: %v" , err )
110+ return
111+ }
112+ }
113+
114+ utils .Logger .Info ("updater binary downloaded successfully" )
115+ }
116+
117+ err = utils .Execute (updaterPath , utils .GetMyPath (), "install" )
118+ if err != nil {
119+ utils .Logger .ErrorF ("error installing updater service: %v" , err )
120+ return
121+ }
122+
123+ utils .Logger .Info ("updater service installed successfully" )
124+ }
0 commit comments