Skip to content

Commit

Permalink
updated eid and esc package id, added custom link and custom port for…
Browse files Browse the repository at this point in the history
… www/activities, added running web server for custom activity port
  • Loading branch information
jhoe123 committed Apr 13, 2022
1 parent ca618c7 commit 46a6c74
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 38 deletions.
31 changes: 19 additions & 12 deletions foundation/app/data/packageConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,28 @@ const SYSTEM = "system" // identifies the package is installed on system lo
const EXTERNAL = "external" // identifies the package is installed on external location
const NODE_JS_DIR = "nodejs" // sub directory of binary dir, this is where node js scripts reside

type ActivityGroupConfig struct {
CustomLink string `json:"customLink"`
CustomPort int `json:"customPort"`
Activities []string `json:"activities"` // if app has activity. this contains definition of actions that will triggerr activity
}

// This structure represents package json file along with the binary.
// this contains information about the application behaviour, permission and services.
type PackageConfig struct {
Name string `json:"name"` // package name
Description string `json:"description"` // description of package
PackageId string `json:"packageId"` // identifies the package/application. this should be unique. format = company.package
Build int16 `json:"build"` // this should be incremental starting from 1
Version string `json:"version"` // major.minor.patch
Program string `json:"program"` // the main program file to execute
ProgramArgs []string `json:"programArgs"` // arguments to pass to program
Name string `json:"name"` // package name
Description string `json:"description"` // description of package
PackageId string `json:"packageId"` // identifies the package/application. this should be unique. format = company.package
Build int16 `json:"build"` // this should be incremental starting from 1
Version string `json:"version"` // major.minor.patch
Program string `json:"program"` // the main program file to execute
ProgramArgs []string `json:"programArgs"` // arguments to pass to program
ActivityGroup ActivityGroupConfig `json:"activityGroup,omitempty"` // www configuration
// request permission for specific action/feature
// if the specific action was called and was not defined. the process will be void
Permissions []string `json:"permissions"`
ExportServices bool `json:"exportService"` // true if the package contains services
Activities []string `json:"activities"` // if app has activity. this contains definition of actions that will triggerr activity
Permissions []string `json:"permissions"`
ExportServices bool `json:"exportService"` // true if the package contains services

BroacastListener []string `json:"actionListener"` // defined actions which action listener will listen to
InstallLocation string `json:"location"` // either system or external
Source string `json:"-"` // the source location
Expand Down Expand Up @@ -130,10 +137,10 @@ func (c *PackageConfig) HasServices() bool {

// use to check if contains activity that has action of
func (c *PackageConfig) HasActivity(actionId string) bool {
if c.Activities == nil || len(c.Activities) == 0 {
if c.ActivityGroup.Activities == nil || len(c.ActivityGroup.Activities) == 0 {
return false
}
for _, act := range c.Activities {
for _, act := range c.ActivityGroup.Activities {
if act == actionId {
return true
}
Expand Down
4 changes: 4 additions & 0 deletions foundation/constants/systemService.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const APP_TERMINATE = "ela.system.APP_TERMINATE"
const ACTION_APP_RESTART = "ela.system.APP_RESTART"
const ACTION_APP_CLEAR_DATA = "ela.system.APP_CLEAR_DATA"

// initialize package. called after a package was installed
const ACTION_APP_INSTALLED = "ela.system.APP_INSTALLED"
const ACTION_APP_UNINSTALLED = "ela.system.APP_UNINSTALLED"

type AppRunningState int

const (
Expand Down
9 changes: 9 additions & 0 deletions internal/cwd/packageinstaller/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ func (a *activity) OnPendingAction(action *data.Action) error {
return a.startNormalInstall(pkgData)
}
return a.runCustomInstaller(sourcePkg, pkgData)
// uninstall package
default:
pkid := action.DataToString()
if pkid == "" {
return errors.SystemNew("failed to uninstall, no package id was provided as parameter", nil)
}
global.Logger.Info().Msg("start uninstall package " + pkid)
a.terminatePackage(pkid)
return utils.UninstallPackage(pkid, global.DELETE_DATA_ONUNINSTALL, true, true)
}
}
Expand Down Expand Up @@ -116,11 +118,18 @@ func (a *activity) finish(err string) {
} else {
global.Logger.Info().Msg("Install success")
broadcast.UpdateSystem(a.currentPkg, broadcast.INSTALLED)
broadcast.OnPackageInstalled(a.currentPkg)
}
// comment this line. system will terminate this activity automatically
//a.running = false
}

func (a *activity) terminatePackage(pki string) {
if _, err := global.AppController.RPC.CallSystem(data.NewAction(constants.APP_TERMINATE, pki, nil)); err != nil {
global.Logger.Error().Err(err).Msg("failed to terminate package " + pki)
}
}

// callback from installer on progress
func (a *activity) onInstallProgress(progress int, pkg string) {
broadcast.UpdateProgress(pkg, progress)
Expand Down
12 changes: 12 additions & 0 deletions internal/cwd/packageinstaller/broadcast/normalInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package broadcast
import (
"strconv"

appc "github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/event/data"
"github.com/cansulting/elabox-system-tools/internal/cwd/packageinstaller/constants"
)
Expand Down Expand Up @@ -37,6 +38,17 @@ func UpdateSystem(pkgId string, status InstallState) {
}
}

// notify system that the installation is complete for specific package
func OnPackageInstalled(pki string) error {
_, err := constants.AppController.RPC.CallSystem(data.NewAction(
appc.ACTION_APP_INSTALLED, pki, nil,
))
if err != nil {
return err
}
return nil
}

// broadcast error
// @param pkgId - the package id currently installing
func Error(pkgId string, code int, val string) {
Expand Down
71 changes: 71 additions & 0 deletions internal/cwd/system/appman/appConnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package appman

import (
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -43,6 +44,8 @@ type AppConnect struct {
launched bool // true if this app was launched
RPC *RPCBridge
nodejs *Nodejs
server *http.Server
initialized bool
}

// create new app connect
Expand All @@ -65,11 +68,33 @@ func newAppConnect(
nodejs: node,
process: nil,
Client: client,
initialized: false,
}
res.RPC = NewRPCBridge(pk.PackageId, res, global.Server.EventServer)
return res
}

// initialize web serving and services related to this app
func (app *AppConnect) init() error {
if app.initialized {
return nil
}
app.initialized = true
// www serving
if app.Config.ActivityGroup.CustomPort > 0 {
if err := app.startServeWww(); err != nil {
return err
}
}
// start services
if app.Config.ExportServices || app.Config.Nodejs {
ac := eventd.NewActionById(constants.ACTION_START_SERVICE)
app.PendingActions.AddPendingService(&ac)
return app.Launch()
}
return nil
}

// use to check if this app is currently running
func (app *AppConnect) IsRunning() bool {
if app.nodejs != nil {
Expand Down Expand Up @@ -118,6 +143,7 @@ func (app *AppConnect) Launch() error {

go asyncRun(app, cmd)
}

app.launched = true
return nil
}
Expand Down Expand Up @@ -187,10 +213,16 @@ func (app *AppConnect) Terminate() error {
}
}()

// stopping www
if app.Config.ActivityGroup.CustomPort > 0 {
app.stopServeWww()
}

time.Sleep(time.Second * time.Duration(global.APP_TERMINATE_COUNTDOWN))
if app.IsRunning() {
return app.forceTerminate()
}

return nil
}

Expand All @@ -216,3 +248,42 @@ func (n *AppConnect) Write(data []byte) (int, error) {
print(string(data))
return len(data), nil
}

// start serving the www for app with custom port
func (n *AppConnect) startServeWww() error {

// starts listening to port
go func(port int, pkg string, wwwPath string) {
global.Logger.Debug().Str("category", "web").Msg("Starting www custom port " + strconv.Itoa(port) + " for package " + pkg + ".")
serverMux := http.NewServeMux()
n.server = &http.Server{Addr: ":" + strconv.Itoa(port), Handler: serverMux}
// create file server for package
//serverMux.Handle("/", http.FileServer(http.Dir(wwwPath)))
path := ""
serverMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path = wwwPath + r.URL.Path
if _, err := os.Stat(path); err == nil {
http.ServeFile(w, r, path)
return
}
http.ServeFile(w, r, wwwPath+"/index.html")
})
if err := n.server.ListenAndServe(); err != nil {
global.Logger.Warn().Err(err).Str("category", "web").Caller().Msg("Failed to start listening to port for " + pkg + ".")
}
}(n.Config.ActivityGroup.CustomPort, n.Config.PackageId, n.Config.GetWWWDir()+"/"+n.Config.PackageId)
return nil
}

// stop serving to specific port
func (n *AppConnect) stopServeWww() error {
if n.server == nil {
return nil
}
global.Logger.Debug().Str("category", "web").Msg("Stopping www custom port " + strconv.Itoa(n.Config.ActivityGroup.CustomPort) + " for package " + n.Config.PackageId + ".")
if err := n.server.Close(); err != nil {
return err
}
n.server = nil
return nil
}
58 changes: 40 additions & 18 deletions internal/cwd/system/appman/appConnectManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"errors"

appd "github.com/cansulting/elabox-system-tools/foundation/app/data"
"github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/event/data"
"github.com/cansulting/elabox-system-tools/foundation/event/protocol"
"github.com/cansulting/elabox-system-tools/foundation/logger"
"github.com/cansulting/elabox-system-tools/internal/cwd/system/global"
"github.com/cansulting/elabox-system-tools/registry/app"
registry "github.com/cansulting/elabox-system-tools/registry/app"
)

Expand Down Expand Up @@ -135,19 +135,6 @@ func LaunchAppActivity(
return err
}

func LaunchAppService(pkgid string) (*AppConnect, error) {
app := GetAppConnect(pkgid, nil)
if app == nil {
return nil, errors.New("Package " + pkgid + " was not found.")
}
if app.launched {
return app, nil
}
ac := data.NewActionById(constants.ACTION_START_SERVICE)
app.PendingActions.AddPendingService(&ac)
return app, app.Launch()
}

// use to launch app
func SendAppPendingAction(
app *AppConnect,
Expand All @@ -165,15 +152,50 @@ func SendAppPendingAction(
}

// run all start up apps
func InitializeStartups() {
func InitializeAllPackages() {
global.Logger.Info().Msg("Services are starting up...")
pkgs, err := registry.RetrieveStartupPackages()
// 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.")
return
}
for _, pkg := range pkgs {
if _, err := LaunchAppService(pkg); err != nil {
global.Logger.Error().Err(err).Caller().Msg("Failed launching app.")
config, err := app.RetrievePackage(pkg)
if err != nil {
global.Logger.Error().Err(err).Caller().Msg("Failed retrieving package " + pkg)
continue
}
// should we initialize the package?
if !config.ExportServices &&
!config.Nodejs &&
config.ActivityGroup.CustomPort == 0 {
continue
}
if err := InitializePackage(pkg); err != nil {
global.Logger.Error().Err(err).Caller().Msg("Failed initializing package " + pkg)
}
}
}

// initialize specific package
func InitializePackage(pki string) error {
app := GetAppConnect(pki, nil)
if app == nil {
return errors.New("Package " + pki + " was not found.")
}
if app.launched {
return nil
}
if err := app.init(); err != nil {
global.Logger.Error().Err(err).Caller().Msg("Failed launching app.")
return err
}
return nil
}
2 changes: 1 addition & 1 deletion internal/cwd/system/appman/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/cansulting/elabox-system-tools/internal/cwd/system/global"
func Initialize(commandline bool) error {
if !commandline {
if global.RUN_STARTUPAPPS {
InitializeStartups()
InitializeAllPackages()
} else {
global.Logger.Debug().Msg("Startup apps was disabled.")
}
Expand Down
15 changes: 14 additions & 1 deletion internal/cwd/system/servicecenter/serviceRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func OnRecievedRequest(
return onAppRestart(client, action)
case constants.ACTION_APP_CLEAR_DATA:
return onAppClearData(client, action)
case constants.ACTION_APP_INSTALLED:
return initPackage(action)
case constants.SYSTEM_UPDATE_MODE:
return activateUpdateMode(client, action)
case constants.SYSTEM_TERMINATE:
Expand Down Expand Up @@ -201,7 +203,7 @@ func startService(action data.Action) string {
if pkgid == "" {
return rpc.CreateResponse(rpc.INVALID_CODE, "package id shouldnt be empty")
}
_, err := appman.LaunchAppService(pkgid)
err := appman.InitializePackage(pkgid)
if err != nil {
return rpc.CreateResponse(rpc.INVALID_CODE, err.Error())
}
Expand Down Expand Up @@ -285,3 +287,14 @@ func terminate(seconds uint) string {
}()
return rpc.CreateSuccessResponse("Terminated")
}

// initialize and start newly instlled package
func initPackage(action data.Action) string {
global.Logger.Debug().Msg("Initializing package " + action.PackageId)
pki := action.PackageId
if err := appman.InitializePackage(pki); err != nil {
global.Logger.Error().Err(err).Msg("Failed to initialize package " + pki)
return rpc.CreateResponse(rpc.SYSTEMERR_CODE, err.Error())
}
return rpc.CreateSuccessResponse("Initialized")
}
3 changes: 2 additions & 1 deletion internal/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ if [ "$answerEla" == "y" ]; then
mkdir -p $eidbin
cp ${EID_SRC}/build/bin/geth $eidbin
chmod +x $eidbin/geth
mv $escbin/geth $eidbin/ela.eid
# esc
escbin=$buildpath/esc/bin
mkdir -p $escbin
cp ${ESC_SRC}/build/bin/geth $escbin
chmod +x $escbin/geth
mv $escbin/geth $escbin/esc
mv $escbin/geth $escbin/ela.esc
# carrier
carrierlib=$buildpath/carrier/bin
mkdir -p $carrierlib
Expand Down
2 changes: 1 addition & 1 deletion internal/test/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestRetrieveAllPackages(test *testing.T) {
test.Error(err)
}
for _, pk := range pks {
log.Println(pk.ToJson())
log.Println(pk)
}

}
Loading

0 comments on commit 46a6c74

Please sign in to comment.