Skip to content

Commit

Permalink
Merge pull request #75 from mysteriumnetwork/fix/multiarch-2
Browse files Browse the repository at this point in the history
Support native runner on MacOS
  • Loading branch information
Zensey committed Nov 3, 2022
2 parents 2a19f0e + e004412 commit 512b51f
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 165 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
run: |
go run cmd/resource/resource.go
go build -v -trimpath -ldflags "-s -w -H windowsgui" -o bin/myst-launcher-amd64.exe github.com/mysteriumnetwork/myst-launcher/cmd/app
go build -v -trimpath -ldflags "-s -w -X 'main.debugMode=1'" -o bin/myst-launcher-dbg-amd64.exe github.com/mysteriumnetwork/myst-launcher/cmd/app
env:
GOARCH: amd64
GOOS: windows
Expand All @@ -46,7 +45,6 @@ jobs:
prerelease: true
files: |
bin/myst-launcher-amd64.exe
bin/myst-launcher-dbg-amd64.exe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 1 addition & 3 deletions app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func NewApp() *AppState {
}

func (s *AppState) StopAppController() {
if !s.ctrApp.GetFinished() {
s.TriggerAction(model.ActionStop)
}
s.ctrApp.Shutdown()
}

func (s *AppState) StartAppController() {
Expand Down
29 changes: 21 additions & 8 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ import (
"github.com/mysteriumnetwork/myst-launcher/utils"
)

var debugMode = ""

func main() {
ap := app.NewApp()
ipc := ipc_.NewHandler()

if len(os.Args) > 1 {
switch os.Args[1] {
cmd := ""
debugMode := false
for _, v := range os.Args {
switch v {
case _const.FlagInstall,
_const.FlagUninstall,
_const.FlagInstallFirewall:
cmd = v
case _const.FlagDebug:
debugMode = true
}
}
if cmd != "" {
switch cmd {
case _const.FlagInstall:
utils.InstallExe()
return
Expand All @@ -47,6 +57,9 @@ func main() {
return
}
}
if debugMode {
utils.AllocConsole(false)
}

mod := model.NewUIModel()
mod.SetApp(ap)
Expand All @@ -59,12 +72,12 @@ func main() {
return
}
}
utils.EnableAutorun(mod.Config.AutoStart)

err := utils.EnableAutorun(mod.Config.AutoStart)
fmt.Println("utils.EnableAutorun", err)

prodVersion, _ := utils.GetProductVersion()
mod.SetProductVersion(prodVersion)
if debugMode != "" {
if debugMode {
log.Println("Product version:", prodVersion)
}

Expand All @@ -90,7 +103,7 @@ func main() {
gui_win32.ShutdownGDIPlus()
ap.StopAppController()

if debugMode != "" {
if debugMode {
fmt.Println("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
Expand Down
1 change: 1 addition & 0 deletions const/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
FlagInstall = "-install-binary"
FlagUninstall = "-uninstall"
FlagInstallFirewall = "-install-fw"
FlagDebug = "-debug"
)
27 changes: 17 additions & 10 deletions controller/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package docker

import (
"log"
"sync"
"time"

model_ "github.com/mysteriumnetwork/myst-launcher/model"
Expand All @@ -20,7 +21,9 @@ import (
type Controller struct {
a model_.AppState

finished bool
finished bool
wg sync.WaitGroup

mgr model_.PlatformManager
mystManager *myst.Manager
lg *log.Logger
Expand All @@ -39,12 +42,16 @@ func (c *Controller) SetApp(a model_.AppState) {
c.a = a
}

func (c *Controller) GetFinished() bool {
return c.finished
func (c *Controller) setFinished() {
c.finished = true
c.wg.Done()
}

func (c *Controller) SetFinished() {
c.finished = true
func (c *Controller) Shutdown() {
if !c.finished {
c.a.GetAction() <- model_.ActionStop
c.wg.Wait()
}
}

func (c *Controller) Start() {
Expand All @@ -70,12 +77,13 @@ func (c *Controller) Start() {
c.mystManager = mystManager
docker := NewDockerRunner(mystManager.GetDockerClient())

defer c.SetFinished()
c.wg.Add(1)
defer c.setFinished()

t1 := time.NewTicker(15 * time.Second)
for {
if wantExit := c.tryStartOrInstallDocker(docker); wantExit {
c.SetFinished()
c.setFinished()
ui.CloseUI()
return
}
Expand All @@ -87,7 +95,6 @@ func (c *Controller) Start() {
c.upgradeContainer(false)
}

// c.lg.Println("wait action >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
select {
case act := <-action:
c.lg.Println("action:", act)
Expand Down Expand Up @@ -121,6 +128,8 @@ func (c *Controller) Start() {

case model_.ActionStop:
c.lg.Println("[docker] stop")
mdl.SetStateContainer(model_.RunnableStateUnknown)
mystManager.Stop()
return
}

Expand Down Expand Up @@ -241,14 +250,12 @@ func (c *Controller) startContainer() {
if !mdl.Config.Enabled {
return
}

containerAlreadyRunning, err := c.mystManager.Start()
if err != nil {
mdl.SetStateContainer(model_.RunnableStateUnknown)
c.lg.Println("startContainer", err)
return
}

mdl.SetStateContainer(model_.RunnableStateRunning)

if !containerAlreadyRunning && mdl.Config.InitialState == model_.InitialStateFirstRunAfterInstall {
Expand Down
20 changes: 7 additions & 13 deletions controller/native/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"sync"

"github.com/codingsince1985/checksum"
wapi "github.com/iamacarpet/go-win64api"

_const "github.com/mysteriumnetwork/myst-launcher/const"
"github.com/mysteriumnetwork/myst-launcher/model"
"github.com/mysteriumnetwork/myst-launcher/updates"
"github.com/mysteriumnetwork/myst-launcher/utils"
_const "github.com/mysteriumnetwork/myst-launcher/const"

)

const (
Expand Down Expand Up @@ -63,6 +63,7 @@ func (c *Controller) CheckAndUpgradeNodeExe(forceUpgrade bool) bool {
mdl.ImageInfo.VersionLatest = tagLatest
mdl.ImageInfo.VersionCurrent = cfg.NodeExeVersion
mdl.ImageInfo.HasUpdate = tagLatest != cfg.NodeExeVersion

defer func() {
cfg.NodeLatestTag = tagLatest
cfg.NodeExeVersion = tagLatest
Expand All @@ -82,7 +83,7 @@ func (c *Controller) CheckAndUpgradeNodeExe(forceUpgrade bool) bool {
utils.TerminateProcess(p, 0)
}

if c.a.GetModel().Config.AutoUpgrade || sha256 == "" {
if cfg.AutoUpgrade || sha256 == "" {
c.tryInstall()

sha256, _ := checksum.SHA256sum(fullpath)
Expand Down Expand Up @@ -136,17 +137,10 @@ var once sync.Once

func tryInstallFirewallRules(ui model.Gui_) {
once.Do(func() {
// check firewall rules
needFirewallSetup := false
rule, err := wapi.FirewallRuleGet(fwRuleNameUDP)
if err != nil || rule.Name == "" {
needFirewallSetup = true
}

rule, err = wapi.FirewallRuleGet(fwRuleNameTCP)
if err != nil || rule.Name == "" {
needFirewallSetup = true
}
// check firewall rules
needFirewallSetup := checkFirewallRules()

if needFirewallSetup {
ret := ui.YesNoModal("Installation", "Firewall rule missing, addition is required. Press Yes to approve.")
if ret == model.IDYES {
Expand Down
35 changes: 25 additions & 10 deletions controller/native/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package native

import (
"log"
"sync"
"time"

model_ "github.com/mysteriumnetwork/myst-launcher/model"
Expand All @@ -19,16 +20,15 @@ type Controller struct {
a model_.AppState

finished bool
runner *NodeRunner
lg *log.Logger
}
wg sync.WaitGroup

func (c *Controller) GetFinished() bool {
return c.finished
runner *NodeRunner
lg *log.Logger
}

func (c *Controller) SetFinished() {
func (c *Controller) setFinished() {
c.finished = true
c.wg.Done()
}

func NewController() *Controller {
Expand All @@ -45,7 +45,12 @@ func (c *Controller) SetApp(a model_.AppState) {
c.runner = NewRunner(a.GetModel())
}

func (c *Controller) Shutdown() {}
func (c *Controller) Shutdown() {
if !c.finished {
c.a.GetAction() <- model_.ActionStop
c.wg.Wait()
}
}

// Supervise the node
func (c *Controller) Start() {
Expand All @@ -61,7 +66,8 @@ func (c *Controller) Start() {
model.ImageInfo.VersionLatest = cfg.NodeLatestTag
model.Update()

defer c.SetFinished()
c.wg.Add(1)
defer c.setFinished()

t1 := time.NewTicker(15 * time.Second)
for {
Expand All @@ -73,7 +79,6 @@ func (c *Controller) Start() {
// c.upgradeContainer(false)
// }

// c.lg.Println("wait action >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
select {
case act := <-action:
c.lg.Println("action:", act)
Expand Down Expand Up @@ -107,6 +112,8 @@ func (c *Controller) Start() {

case model_.ActionStop:
c.lg.Println("[native] stop")
model.SetStateContainer(model_.RunnableStateUnknown)
c.stop()
return
}

Expand Down Expand Up @@ -151,10 +158,18 @@ func (c *Controller) startContainer() {

ui := c.a.GetUI()
tryInstallFirewallRules(ui)

running := c.runner.IsRunningOrTryStart()
if running {
model.SetStateContainer(model_.RunnableStateRunning)

cfg := &model.Config
switch cfg.InitialState {
case model_.InitialStateFirstRunAfterInstall, model_.InitialStateUndefined:
cfg.InitialState = model_.InitialStateNormalRun
cfg.Save()
ui.ShowNotificationInstalled()
}
}
}
}
20 changes: 7 additions & 13 deletions controller/native/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (r *NodeRunner) isRunning() uint32 {
// return values: isRunning
func (r *NodeRunner) IsRunningOrTryStart() bool {

p := r.isRunning()
if p == 0 {
if r.isRunning() == 0 {
return r.startNode() == nil
}

Expand Down Expand Up @@ -110,21 +109,16 @@ func (r *NodeRunner) startNode() error {
dataDirArg := fmt.Sprintf("--data-dir=%s", r.configpath)
userspaceArg := "--userspace"

switch runtime.GOOS {
case "windows":
args := []string{userspaceArg, versionArg, configDirArg, dataDirArg, "service", "--agreed-terms-and-conditions"}
if err := utils.CmdStart(fullExePath, args...); err != nil {
log.Println("run node failed:", err)
return err
}
args := []string{userspaceArg, versionArg, configDirArg, dataDirArg, "service", "--agreed-terms-and-conditions"}

case "darwin":
// cmd = exec.Command("open", "/Applications/Docker.app/")
r.cmd = exec.Command(fullExePath, versionArg, configDirArg, dataDirArg, "service", "--agreed-terms-and-conditions")
if err := r.cmd.Start(); err != nil {
switch runtime.GOOS {
case "windows", "darwin":
cmd, err := utils.CmdStart(fullExePath, args...)
if err != nil {
log.Println("run node failed:", err)
return err
}
r.cmd = cmd

default:
return errors.New("unsupported OS: " + runtime.GOOS)
Expand Down
5 changes: 4 additions & 1 deletion controller/native/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ func extractNodeBinary(src, dest string) error {
return extractor.NewTgz().Extract(src, dest)
}

func CheckAndInstallFirewall() {
func CheckAndInstallFirewall() {}

func checkFirewallRules() bool {
return false
}
14 changes: 14 additions & 0 deletions controller/native/utils_win32.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ func CheckAndInstallFirewallRules() {
log.Println(err)
}
}

// returns true if some firewall rules to be setup
func checkFirewallRules() bool {
rule, err := wapi.FirewallRuleGet(fwRuleNameUDP)
if err != nil || rule.Name == "" {
return true
}

rule, err = wapi.FirewallRuleGet(fwRuleNameTCP)
if err != nil || rule.Name == "" {
return true
}
return false
}

0 comments on commit 512b51f

Please sign in to comment.