Skip to content

Commit

Permalink
Merge pull request #15 from cansulting/release/v0.6.7
Browse files Browse the repository at this point in the history
Release/v0.6.7
  • Loading branch information
jhoe123 authored Jul 1, 2022
2 parents fdda759 + 7300ea5 commit 02b655c
Show file tree
Hide file tree
Showing 87 changed files with 1,699 additions and 493 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"type": "go",
"request": "launch",
"mode": "debug",
"cwd": "${workspaceFolder}/internal/builds/linux/packageinstaller",
"program": "${workspaceFolder}/internal/cwd/packageinstaller",
"buildFlags": "-tags=IDE"
},
Expand All @@ -35,6 +36,15 @@
"mode": "debug",
"program": "${workspaceFolder}/sample/simple",
"cwd": "${workspaceFolder}/sample/simple/build"
},
{
"name": "Launch Account Manager",
"type": "go",
"request": "launch",
"mode": "debug",
"cwd": "${workspaceFolder}/internal/builds/linux/account_manager",
"program": "${workspaceFolder}/internal/cwd/account_manager",
"buildFlags": "-tags=IDE"
}
]
}
31 changes: 18 additions & 13 deletions foundation/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package app
// To initialize call NewController, for debugging use NewControllerWithDebug
// please see the documentation for more info.
import (
"os"
"strconv"
"time"

Expand All @@ -35,6 +36,7 @@ func RunApp(app *Controller) error {

// start the app
if err := app.onStart(); err != nil {
logger.GetInstance().Error().Str("category", "appcontroller").Err(err).Msg("Failed to start app")
return err
}

Expand Down Expand Up @@ -65,11 +67,17 @@ func NewController(
if logger.GetInstance() == nil {
logger.Init(config.PackageId)
}
// step: create RPC
rpc, err := rpc.NewRPCHandlerDefault()
if err != nil {
return nil, errors.SystemNew("Controller: Failed to start. Couldnt create client connector.", err)
}
return &Controller{
Debugging: system.IDE,
AppService: service,
Activity: activity,
Config: config,
RPC: rpc,
}, nil
}

Expand Down Expand Up @@ -103,24 +111,21 @@ func (m *Controller) onStart() error {
Str("category", "appcontroller").
Msg("Starting App Ide = " + strconv.FormatBool(system.IDE))

// step: create RPC
if m.RPC == nil {
rpc, err := rpc.NewRPCHandlerDefault()
if err != nil {
return errors.SystemNew("Controller: Failed to start. Couldnt create client connector.", err)
}
m.RPC = rpc
}
m.initRPCRequests()
// step: send running state
awake := constants.APP_AWAKE
appState := appd.AppState{State: constants.APP_AWAKE}
if m.Debugging {
awake = constants.APP_AWAKE_DEBUG
appState.State = constants.APP_AWAKE_DEBUG
wd, err := os.Getwd()
if err != nil {
logger.GetInstance().Error().Err(err).Caller().Msg("failed to retrieve debug working dir")
}
appState.Data = wd
}
res, err := m.RPC.CallSystem(
data.NewAction(constants.APP_CHANGE_STATE, m.Config.PackageId, awake))
data.NewAction(constants.APP_CHANGE_STATE, m.Config.PackageId, appState))
if err != nil {
logger.GetInstance().Error().Str("category", "appcontroller").Err(err).Msg("Failed to send running state")
logger.GetInstance().Error().Str("category", "appcontroller").Err(err).Msg("Failed to send awake state")
return err
}
logger.GetInstance().Debug().Msg("Pending actions =" + res.ToString())
Expand Down Expand Up @@ -160,7 +165,7 @@ func (m *Controller) onEnd() error {
data.NewAction(
constants.APP_CHANGE_STATE,
m.Config.PackageId,
constants.APP_SLEEP))
appd.AppState{State: constants.APP_SLEEP}))
if err != nil {
logger.GetInstance().Error().Err(err).Caller().Str("category", "appcontroller").Msg("Controller.onEnd Change state failed.")
}
Expand Down
1 change: 1 addition & 0 deletions foundation/app/controllerRpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (instance *Controller) onPendingActions(client protocol.ClientInterface, ac
if actionG.Activity != nil {
// forward to activity
if err := instance.Activity.OnPendingAction(actionG.Activity); err != nil {
logger.GetInstance().Error().Err(err).Caller().Msg("failed processing pending action")
return rpc.CreateResponse(rpc.SYSTEMERR_CODE, err.Error())
}
}
Expand Down
10 changes: 10 additions & 0 deletions foundation/app/data/appState.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// handles information for app state

package data

import "github.com/cansulting/elabox-system-tools/foundation/constants"

type AppState struct {
State constants.AppRunningState `json:"state"`
Data interface{} `json:"data"`
}
62 changes: 45 additions & 17 deletions foundation/app/data/packageConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,36 @@ 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
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
BroacastListener []string `json:"actionListener"` // defined actions which action listener will listen to
InstallLocation string `json:"location"` // which location the package will be installed
Source string `json:"-"` // the source location
Nodejs bool `json:"nodejs"` // true if this package includes node js
PackagerVersion string `json:"packagerVersion"` // version of packager of this package
Ext map[string]interface{} `json:"ext"` // extra values
Permissions []string `json:"permissions"`
ExportServices bool `json:"exportService"` // true if the package contains services

BroacastListener []string `json:"actionListener,omitempty"` // defined actions which action listener will listen to
InstallLocation string `json:"location,omitempty"` // either system or external
Source string `json:"-"` // the source location
Nodejs bool `json:"nodejs"` // true if this package includes node js
PackagerVersion string `json:"packagerVersion,omitempty"` // version of packager of this package
Ext map[string]interface{} `json:"ext,omitempty"` // extra values
MinRuntime string `json:"minRuntime,omitempty"` // minimum system runtime version this package will run on
ExposePorts []int `json:"exposePorts,omitempty"` // this package requires port to be expose to make package usable.
//Services map[string]string `json:"services"` // if app has a service. this contains definition of commands available to service
}

Expand All @@ -56,6 +66,16 @@ func DefaultPackage() *PackageConfig {
return &PackageConfig{InstallLocation: EXTERNAL /*, Restart: false*/}
}

// load config given the source location( system or external)
func (c *PackageConfig) LoadFromLocation(pkid string, location string) error {
src := path.GetSystemAppDir()
if location != SYSTEM {
src = path.GetExternalAppDir()
}
src += "/" + pkid + "/" + constants.APP_CONFIG_NAME
return c.LoadFromSrc(src)
}

// local package data given the source location
func (c *PackageConfig) LoadFromSrc(src string) error {
bytes, err := os.ReadFile(src)
Expand Down Expand Up @@ -119,10 +139,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 Expand Up @@ -194,6 +214,14 @@ func (c *PackageConfig) GetLibraryDir() string {
return path.GetLibPath() + "/" + c.PackageId
}

func (c *PackageConfig) GetWWWDir() string {
if c.InstallLocation == SYSTEM || !path.HasExternal() {
return path.GetSystemWWW() + "/" + c.PackageId
} else {
return path.GetExternalWWW() + "/" + c.PackageId
}
}

// return true if has main binary
func (c *PackageConfig) HasMainProgram() bool {
if c.Program == "" {
Expand Down
3 changes: 2 additions & 1 deletion foundation/app/rpc/responseUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
const SUCCESS_CODE = 200
const SYSTEMERR_CODE = 400 // theres something wrong with the system
const INVALID_CODE = 401
const NOT_IMPLEMENTED = 300 // code was not implemented
const NOT_IMPLEMENTED = 300 // code was not implemented
const INVALID_PARAMETER_PROVIDED = 402 // parameters was invalid

// return json string for response
func CreateResponse(code int16, msg string) string {
Expand Down
3 changes: 1 addition & 2 deletions foundation/constants/systemActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +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
10 changes: 10 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 All @@ -61,6 +64,13 @@ const SERVICE_PENDING_ACTIONS = "ela.system.PENDING_ACTIONS"
// sends terminate action to app
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
53 changes: 51 additions & 2 deletions foundation/event/data/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ package data

import (
"encoding/json"
"fmt"
"log"
"reflect"

"github.com/cansulting/elabox-system-tools/foundation/app/data"
"github.com/cansulting/elabox-system-tools/foundation/constants"
"github.com/cansulting/elabox-system-tools/foundation/errors"

"github.com/mitchellh/mapstructure"
Expand All @@ -28,9 +31,9 @@ type Action struct {
Id string `json:"id"`
// optional. which specific package will handle this action.
// if nothing was specified then look for any valid package that can carry out the action
PackageId string `json:"packageId"`
PackageId string `json:"packageId,omitempty"`
// optional. data which will be use to execute the action
Data interface{} `json:"data"`
Data interface{} `json:"data,omitempty"`
//valueAction *Action `json:"-"`
}

Expand Down Expand Up @@ -111,6 +114,36 @@ func (a *Action) DataToMap() (map[string]interface{}, error) {
return nil, nil
}

func (a *Action) DataToObj(obj interface{}) error {
str := a.DataToString()
if str != "" {
return json.Unmarshal([]byte(str), obj)
}
return errors.SystemNew("data is empty", nil)
}

func (a *Action) DataToAppState() (*data.AppState, error) {
if a.Data != nil {
switch a.Data.(type) {
case string:
appState := data.AppState{}
if err := a.DataToObj(&appState); err != nil {
return nil, err
}
return &appState, nil
case map[string]interface{}:
datm := a.Data.(map[string]interface{})
state := datm["state"].(float64)
appState := data.AppState{
State: constants.AppRunningState(state),
Data: datm["data"],
}
return &appState, nil
}
}
return nil, nil
}

func convertData(data interface{}) interface{} {
if data != nil {
switch data.(type) {
Expand All @@ -133,3 +166,19 @@ func (a *Action) ToJson() string {
}
return string(res)
}

func (a *Action) ToString() string {
data := ""
if a.Data != nil {
switch a.Data.(type) {
case string:
data = a.Data.(string)
case map[string]interface{}:
content, _ := json.Marshal(a.Data)
data = string(content)
case float64:
data = fmt.Sprintf("%f", a.Data)
}
}
return "id=" + a.Id + " package=" + a.PackageId + " data=" + data
}
4 changes: 2 additions & 2 deletions foundation/event/data/actionGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

type ActionGroup struct {
Activity *Action `json:"activity"`
Service *Action `json:"service"`
Activity *Action `json:"activity,omitempty"`
Service *Action `json:"service,omitempty"`
//Broadcasts []Action
}

Expand Down
2 changes: 1 addition & 1 deletion foundation/event/protocol/connectorServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ConnectorServer interface {
GetState() data.ConnectionType
Open() error
SetStatus(status system.Status, data interface{}) error
GetStatus() string
GetStatus() system.Status
// send data to all room
Broadcast(room string, event string, data interface{}) error
// send service response to client
Expand Down
9 changes: 9 additions & 0 deletions foundation/system/buildmode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package system

type BuildModeType string

const (
DEBUG BuildModeType = "DEBUG"
STAGING BuildModeType = "STAGING"
RELEASE BuildModeType = "RELEASE"
)
6 changes: 6 additions & 0 deletions foundation/system/buildmode_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !RELEASE && !STAGING
// +build !RELEASE,!STAGING

package system

const BuildMode = DEBUG
6 changes: 6 additions & 0 deletions foundation/system/buildmode_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build RELEASE
// +build RELEASE

package system

const BuildMode = RELEASE
6 changes: 6 additions & 0 deletions foundation/system/buildmode_staging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build STAGING
// +build STAGING

package system

const BuildMode = STAGING
Loading

0 comments on commit 02b655c

Please sign in to comment.