@@ -40,115 +40,32 @@ func UpdateDependencies(cnf *config.Config) {
4040 continue
4141 }
4242
43- if newVersion .UpdaterVersion != currentVersion .UpdaterVersion {
44- utils .Logger .Info ("New version of updater found: %s" , newVersion .UpdaterVersion )
45- if err := utils .DownloadFile (fmt .Sprintf (config .DependUrl , cnf .Server , config .DependenciesPort , fmt .Sprintf (config .UpdaterFile , "" )), map [string ]string {}, fmt .Sprintf (config .UpdaterFile , "_new" ), utils .GetMyPath (), cnf .SkipCertValidation ); err != nil {
46- utils .Logger .ErrorF ("error downloading updater: %v" , err )
43+ if newVersion .Version != currentVersion .Version {
44+ utils .Logger .Info ("New version of agent found: %s" , newVersion .Version )
45+ if err := utils .DownloadFile (fmt .Sprintf (config .DependUrl , cnf .Server , config .DependenciesPort , fmt .Sprintf (config .ServiceFile , "" )), map [string ]string {}, fmt .Sprintf (config .ServiceFile , "_new" ), utils .GetMyPath (), cnf .SkipCertValidation ); err != nil {
46+ utils .Logger .ErrorF ("error downloading agent: %v" , err )
47+ continue
48+ }
49+
50+ currentVersion = newVersion
51+ err = utils .WriteJSON (config .VersionPath , & currentVersion )
52+ if err != nil {
53+ utils .Logger .ErrorF ("error writing version file: %v" , err )
4754 continue
4855 }
4956
5057 if runtime .GOOS == "linux" || runtime .GOOS == "darwin" {
51- if err = utils .Execute ("chmod" , utils .GetMyPath (), "-R" , "755" , filepath .Join (utils .GetMyPath (), fmt .Sprintf (config .UpdaterFile , "_new" ))); err != nil {
58+ if err = utils .Execute ("chmod" , utils .GetMyPath (), "-R" , "755" , filepath .Join (utils .GetMyPath (), fmt .Sprintf (config .ServiceFile , "_new" ))); err != nil {
5259 utils .Logger .ErrorF ("error executing chmod: %v" , err )
5360 }
5461 }
5562
56- utils .Logger .Info ("Starting updater update process..." )
57- err = runUpdateProcess ()
63+ err = utils .Execute (filepath .Join (utils .GetMyPath (), fmt .Sprintf (config .UpdaterSelf , "" )), utils .GetMyPath ())
5864 if err != nil {
59- utils .Logger .ErrorF ("error updating updater: %v" , err )
60- os .Remove (filepath .Join (utils .GetMyPath (), "version_new.json" ))
61- os .Remove (filepath .Join (utils .GetMyPath (), fmt .Sprintf (config .UpdaterFile , "_new" )))
62- } else {
63- utils .Logger .Info ("Updater update completed successfully" )
64- if utils .CheckIfPathExist (config .VersionPath ) {
65- err := utils .ReadJson (config .VersionPath , & currentVersion )
66- if err != nil {
67- utils .Logger .ErrorF ("error reading updated version file: %v" , err )
68- }
69- }
65+ utils .Logger .ErrorF ("error executing updater: %v" , err )
7066 }
71- } else {
72- os .Remove (filepath .Join (utils .GetMyPath (), "version_new.json" ))
73- }
74- }
75- }
76-
77- func runUpdateProcess () error {
78- path := utils .GetMyPath ()
79-
80- newBin := fmt .Sprintf (config .UpdaterFile , "_new" )
81- oldBin := fmt .Sprintf (config .UpdaterFile , "" )
82- backupBin := fmt .Sprintf (config .UpdaterFile , ".old" )
83-
84- updaterNew := filepath .Join (path , newBin )
85- if _ , err := os .Stat (updaterNew ); err != nil {
86- return fmt .Errorf ("no _new binary found to update" )
87- }
88-
89- if err := utils .StopService (config .SERVICE_UPDATER_NAME ); err != nil {
90- return fmt .Errorf ("error stopping updater: %v" , err )
91- }
92-
93- time .Sleep (10 * time .Second )
94-
95- backupPath := filepath .Join (path , backupBin )
96- if utils .CheckIfPathExist (backupPath ) {
97- utils .Logger .Info ("Removing previous backup: %s" , backupPath )
98- if err := os .Remove (backupPath ); err != nil {
99- utils .Logger .ErrorF ("could not remove old backup: %v" , err )
10067 }
101- }
10268
103- if err := os .Rename (filepath .Join (path , oldBin ), backupPath ); err != nil {
104- return fmt .Errorf ("error backing up old binary: %v" , err )
69+ os .Remove (filepath .Join (utils .GetMyPath (), "version_new.json" ))
10570 }
106-
107- if err := os .Rename (filepath .Join (path , newBin ), filepath .Join (path , oldBin )); err != nil {
108- os .Rename (backupPath , filepath .Join (path , oldBin ))
109- return fmt .Errorf ("error renaming new binary: %v" , err )
110- }
111-
112- if err := utils .StartService (config .SERVICE_UPDATER_NAME ); err != nil {
113- rollbackUpdater (oldBin , backupBin , path )
114- return fmt .Errorf ("error starting updater: %v" , err )
115- }
116-
117- time .Sleep (30 * time .Second )
118-
119- isHealthy , err := utils .CheckIfServiceIsActive (config .SERVICE_UPDATER_NAME )
120- if err != nil || ! isHealthy {
121- utils .Logger .Info ("New version failed health check, rolling back..." )
122- rollbackUpdater (oldBin , backupBin , path )
123- return fmt .Errorf ("rollback completed: new version failed health check" )
124- }
125-
126- utils .Logger .Info ("Health check passed for updater" )
127-
128- versionNewPath := filepath .Join (path , "version_new.json" )
129- versionPath := filepath .Join (path , "version.json" )
130- if utils .CheckIfPathExist (versionNewPath ) {
131- if err := os .Rename (versionNewPath , versionPath ); err != nil {
132- utils .Logger .ErrorF ("error updating version file: %v" , err )
133- } else {
134- utils .Logger .Info ("Version file updated successfully" )
135- }
136- }
137-
138- return nil
139- }
140-
141- func rollbackUpdater (currentBin , backupBin , path string ) {
142- utils .Logger .Info ("Rolling back updater to previous version..." )
143-
144- utils .StopService (config .SERVICE_UPDATER_NAME )
145- time .Sleep (5 * time .Second )
146-
147- os .Remove (filepath .Join (path , currentBin ))
148- os .Rename (filepath .Join (path , backupBin ), filepath .Join (path , currentBin ))
149-
150- utils .StartService (config .SERVICE_UPDATER_NAME )
151- os .Remove (filepath .Join (path , "version_new.json" ))
152-
153- utils .Logger .Info ("Rollback completed for updater" )
15471}
0 commit comments