Skip to content

Commit

Permalink
added system upgrade for build <= 6. bug fixed with system upgrade is…
Browse files Browse the repository at this point in the history
…sues, added config state
  • Loading branch information
jhoe123 committed Jun 29, 2022
1 parent a1ce3f3 commit 4cae2c2
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 98 deletions.
2 changes: 0 additions & 2 deletions foundation/constants/systemActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package constants
//const ACTION_APP_SLEEP = "ela.action.APP_SLEEP" // called when app stop running
const APP_CONFIG_NAME = "info.json" // default name for app config json
const ACTION_APP_LAUNCH = "ela.action.APP_LAUNCH" // called when app will be launched
const ACTION_OPEN_CONTENT = "ela.action.CONTENT_OPEN" // called when needs to open a content
const ACTION_GET_PENDING = "ela.action.GET_PENDING" // called to retrieve pending actions for specific package
const ACTION_APP_INSTALL = "ela.action.INSTALL" // called to launch app installer
const ACTION_APP_UNINSTALL = "ela.action.UNINSTALL" // call to uninstall app via ela.installer
const ACTION_APP_SYSTEM_INSTALL = "ela.action.SYSTEM_INSTALL" // called to initiate system installation
3 changes: 3 additions & 0 deletions foundation/constants/systemService.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const SYSTEM_ACTIVITY_RESULT = "ela.system.ACTIVITY_RESULT"
const SYSTEM_TERMINATE = "ela.system.TERMINATE"
const SYSTEM_TERMINATE_NOW = "ela.system.TERMINATE_NOW"

// system configure success
const SYSTEM_CONFIGURED = "ela.system.CONFIGURED"

/*
service state was changed. usually contains the state integer value.
Check Service Center for references.
Expand Down
26 changes: 13 additions & 13 deletions internal/cwd/system/config/envvar.go → internal/cwd/env/envvar.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package config
package env

import (
"encoding/json"
"log"
"os"

"github.com/cansulting/elabox-system-tools/foundation/constants"
Expand All @@ -28,11 +27,12 @@ func getSourceLoc() string {
return path.GetSystemAppDirData(constants.SYSTEM_SERVICE_ID) + "/" + FILENAME
}

func initialize() error {
func initEnv() error {
if singleton == nil {
singleton = &Env{}
envsrc := getSourceLoc()
if _, err := os.Stat(envsrc); err == nil {
// load from file
bytes, err := os.ReadFile(envsrc)
if err != nil {
return errors.SystemNew("Failed loading environment file @ "+envsrc+" ", err)
Expand All @@ -41,6 +41,10 @@ func initialize() error {
if err := json.Unmarshal(bytes, &singleton); err != nil {
return errors.SystemNew("Failed loading environment file @ "+envsrc+" ", err)
}
// env loaded
for key, val := range singleton.Vars {
os.Setenv(key, val)
}
}
}
if singleton.Vars == nil {
Expand All @@ -51,26 +55,22 @@ func initialize() error {
}

func SetEnv(key string, value string) error {
if err := initialize(); err != nil {
return err
if singleton.Vars[key] != value {
singleton.Vars[key] = value
os.Setenv(key, value)
return saveEnv()
}
singleton.Vars[key] = value
os.Setenv(key, value)
return SaveEnv()
return nil
}

func GetEnv(key string) string {
if err := initialize(); err != nil {
log.Panic(err)
return ""
}
if key == "" {
return ""
}
return singleton.Vars[key]
}

func SaveEnv() error {
func saveEnv() error {
marshaled, err := json.Marshal(singleton)
if err != nil {
return errors.SystemNew("Failed saving environment config file", err)
Expand Down
14 changes: 10 additions & 4 deletions internal/cwd/system/config/init.go → internal/cwd/env/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config
package env

import (
"github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/errors"
"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/foundation/system"
"github.com/cansulting/elabox-system-tools/registry/app"
)

Expand All @@ -12,14 +13,19 @@ const ELAVERSION = "ELAVERSION" // current version of system

// intialize system configuration
func Init() error {
if GetBuildMode() != DEBUG {
if system.BuildMode != system.DEBUG {
logger.ConsoleOut = false
}

if err := SetEnv(ELAENV, string(GetBuildMode())); err != nil {
if err := initEnv(); err != nil {
return err
}
if err := SetEnv(ELAENV, string(system.BuildMode)); err != nil {
return errors.SystemNew("System Config Environment error", err)
}
pkg, err := app.RetrievePackage(constants.SYSTEM_SERVICE_ID)
if pkg == nil {
err = errors.SystemNew("unable to load package", nil)
}
if err != nil {
return errors.SystemNew("System config environment error ", err)
}
Expand Down
39 changes: 21 additions & 18 deletions internal/cwd/packageinstaller/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"time"

"github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/internal/cwd/packageinstaller/broadcast"
pkconst "github.com/cansulting/elabox-system-tools/internal/cwd/packageinstaller/constants"
Expand Down Expand Up @@ -43,23 +44,33 @@ func startCommandline() {
return
}
pk := os.Args[1]
processInstallCommand(pk, IsArgExist("-r"), IsArgExist("-s"), IsArgExist("-l"), !IsArgExist("-i"))
processInstallCommand(pk, IsArgExist("-r"), IsArgExist("-l"), !IsArgExist("-i"))
}

func processInstallCommand(targetPk string, restart bool, systemUpdate bool, logging bool, customInstaller bool) {
func processInstallCommand(targetPk string, restart bool, logging bool, runCustomInstaller bool) {
// step: load package
content, err := pkg.LoadFromSource(targetPk)
if err != nil {
pkconst.Logger.Fatal().Err(err).Caller().Msg("Failed running commandline")
return
}
systemUpdate := false
if content.Config.PackageId == constants.SYSTEM_SERVICE_ID {
systemUpdate = true
}
// step: request for server broadcast
if logging || systemUpdate {
logger.SetHook(loggerHook{})
pkconst.Logger = logger.GetInstance()
}
// step: we need clients to system update via ports
if systemUpdate {
// true if this is parent system update. parent system update means this will execute custom installer if there is
parentSystemUpdate := false
if systemUpdate && runCustomInstaller {
parentSystemUpdate = true
}

// step: we need clients to system update
if parentSystemUpdate {
// step: terminate system
// if err := utils.TerminateSystem(pkconst.TERMINATE_TIMEOUT); err != nil {
// pkconst.Logger.Debug().Err(err).Caller().Msg("failed terminating system")
Expand All @@ -68,17 +79,6 @@ func processInstallCommand(targetPk string, restart bool, systemUpdate bool, log
// step: check if theres a last failed installation
lastState := sysinstall.GetLastState()
if lastState == sysinstall.SUCCESS {
// upgrades
oldpk := sysinstall.GetInstalledPackage()
oldbuildnum := -1
newBuildNum := int(content.Config.Build)
if oldpk != nil {
oldbuildnum = int(oldpk.Build)
}
if err := sysupgrade.Start(oldbuildnum, newBuildNum); err != nil {
pkconst.Logger.Error().Err(err).Stack().Caller().Msg("Failed system upgrade.")
return
}
if err := sysinstall.MarkInprogress(); err != nil {
pkconst.Logger.Error().Err(err).Stack().Caller().Msg("Failed to mark installation as inprogress. install aborted.")
return
Expand All @@ -88,9 +88,12 @@ func processInstallCommand(targetPk string, restart bool, systemUpdate bool, log
}
} else {
logger.ConsoleOut = false
if systemUpdate {
sysupgrade.CheckAndUpgrade(int(content.Config.Build))
}
}
// step: use custom installer or not?
if !customInstaller || !content.HasCustomInstaller() {
if !runCustomInstaller || !content.HasCustomInstaller() {
normalInstall(content)
} else {
if err := content.RunCustomInstaller(targetPk, true, "-i"); err != nil {
Expand All @@ -99,7 +102,7 @@ func processInstallCommand(targetPk string, restart bool, systemUpdate bool, log
}
}
// step: and stop listeners
if systemUpdate {
if parentSystemUpdate {
// mark as success
if err := sysinstall.MarkSuccess(); err != nil {
pkconst.Logger.Error().Err(err).Caller().Msg("Failed installation.")
Expand All @@ -112,7 +115,7 @@ func processInstallCommand(targetPk string, restart bool, systemUpdate bool, log
}
pkconst.Logger.Info().Msg("Installed success.")
// step: restart system
if systemUpdate {
if parentSystemUpdate {
if restart {
if err := utils.Reboot(5); err != nil {
pkconst.Logger.Fatal().Err(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/cwd/packageinstaller/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestSystemUpdateCommandline(test *testing.T) {
return
}*/

processInstallCommand(pkpath, false, true, false, true)
processInstallCommand(pkpath, false, true, false)
//log.Println(string(bytes))
}

Expand Down
9 changes: 9 additions & 0 deletions internal/cwd/packageinstaller/sysinstall.go/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"os"

spath "path"

"github.com/cansulting/elabox-system-tools/foundation/app/data"
"github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/path"
Expand Down Expand Up @@ -34,6 +36,13 @@ func HasOldPackage() bool {

// use to create old package
func CreateOldPackageInfo() error {
// not yet installed?
if _, err := os.Stat(CUR_PK); err != nil {
return nil
}
if err := os.MkdirAll(spath.Dir(OLD_PK), perm.PUBLIC_WRITE); err != nil {
return err
}
wfile, err := os.OpenFile(OLD_PK, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perm.PUBLIC_VIEW)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package sysupgrade

import (
"strconv"

"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/registry/util"
)
Expand All @@ -14,14 +12,15 @@ type build3upgrade struct {

func (instance build3upgrade) onUpgrade(oldBuild int) error {
if oldBuild <= 3 {
logger.GetInstance().Debug().Msg("Starts upgrading build lower or equal to 3")
// we need to delete registry. registry was updated
logger.GetInstance().Debug().Msg("Deleting app registry...")
if err := util.DeleteDB(); err != nil {
logger.GetInstance().Warn().Err(err).Msg("Failed deleting db.")
}
return nil
}
logger.GetInstance().Debug().Msg(
"No upgrade found for old build " + strconv.Itoa(oldBuild) + ". skipped.")
//logger.GetInstance().Debug().Msg(
// "No upgrade found for old build " + strconv.Itoa(oldBuild) + ". skipped.")
return nil
}
24 changes: 24 additions & 0 deletions internal/cwd/packageinstaller/sysupgrade/build6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// system upgrade for build 6 and lower
package sysupgrade

import (
"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/internal/cwd/env"
)

type build6 struct {
}

func (instance *build6) onUpgrade(oldbuild int) error {
// 6 and lower build should mark as already configured.
// update the system variable name "config" to 1
if oldbuild <= 6 {
logger.GetInstance().Debug().Msg("Starts upgrading build lower or equal to 6")
if err := env.Init(); err != nil {
logger.GetInstance().Error().Err(err).Msg("failed to initialize environment, upgrade skip for 6")
return nil
}
env.SetEnv("config", "1")
}
return nil
}
24 changes: 22 additions & 2 deletions internal/cwd/packageinstaller/sysupgrade/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package sysupgrade

import "github.com/cansulting/elabox-system-tools/foundation/logger"
import (
"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/internal/cwd/packageinstaller/constants"
"github.com/cansulting/elabox-system-tools/internal/cwd/packageinstaller/sysinstall.go"
)

// starts upgrading the system.
// Components can be outdated apps, registry, libraries etc
Expand All @@ -11,11 +15,27 @@ func Start(oldBuild int, newBuild int) error {
logger.GetInstance().Debug().Msg("Fresh OS. System upgrade skipped.")
return nil
}
logger.GetInstance().Debug().Msg("Starts upgrading system components...")
upgrader := build3upgrade{}
if err := upgrader.onUpgrade(oldBuild); err != nil {
return err
}
upgrader2 := build6{}
if err := upgrader2.onUpgrade(oldBuild); err != nil {
return err
}
logger.GetInstance().Debug().Msg("Component upgrades finished.")
return nil
}

func CheckAndUpgrade(newBuildNum int) {
// upgrades
oldpk := sysinstall.GetInstalledPackage()
oldbuildnum := -1
if oldpk != nil {
oldbuildnum = int(oldpk.Build)
}
if err := Start(oldbuildnum, newBuildNum); err != nil {
constants.Logger.Error().Err(err).Stack().Caller().Msg("Failed system upgrade.")
return
}
}
2 changes: 1 addition & 1 deletion internal/cwd/system/appman/appConnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type AppConnect struct {
RPC *RPCBridge
nodejs *Nodejs
server *http.Server
initialized bool
initialized bool // true if this specific app/package was already initialized
terminatedIntently bool // true if this app was terminated intentionally
}

Expand Down
18 changes: 11 additions & 7 deletions internal/cwd/system/appman/appConnectManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ func SendAppPendingAction(
// run all start up apps
func InitializeAllPackages() {
global.Logger.Info().Msg("Services are starting up...")
// pkgs, err := registry.RetrieveStartupPackages()
// if err != nil {
// global.Logger.Error().Err(err).Caller().Msg("Failed retrieving startup packages.")
// }
// for _, pkg := range pkgs {
// InitializePackage(pkg)
// }
pkgs, err := registry.RetrieveAllPackages()
if err != nil {
global.Logger.Error().Err(err).Caller().Msg("Failed retrieving startup packages.")
Expand All @@ -184,6 +177,17 @@ func InitializeAllPackages() {
}
}

// run packages that are necessary to configure the elabox
func initializeConfigPackages() error {
global.Logger.Info().Msg("Services for config state are starting up...")
for _, pkg := range global.CONFIG_PKGS {
if err := InitializePackage(pkg); err != nil {
return err
}
}
return nil
}

// initialize specific package
func InitializePackage(pki string) error {
app := GetAppConnect(pki, nil)
Expand Down
Loading

0 comments on commit 4cae2c2

Please sign in to comment.