Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Command] Add spire:occulus-update command #116

Merged
merged 2 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [3.1.1] 4/29/2023

* Add command `spire:occulus-update` to update Occulus to the latest version or install it if it doesn't exist [#116](https://github.com/Akkadius/spire/pull/116)

## [3.1.0] 4/29/2023

* Improvements to how Spire acquires static assets
Expand Down
3 changes: 3 additions & 0 deletions boot/inject_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var commandSet = wire.NewSet(
cmd.NewTestFilesystemCommand,
cmd.NewSpireInitCommand,
cmd.NewUserChangePasswordCommand,
cmd.NewSpireOcculusUpdateCommand,
eqemuchangelog.NewChangelogCommand,
ProvideCommands,
)
Expand All @@ -47,6 +48,7 @@ func ProvideCommands(
testFilesystemCmd *cmd.TestFilesystemCommand,
spireInstallCmd *cmd.SpireInitCommand,
userChangePasswordCmd *cmd.UserChangePasswordCommand,
spireOcculusUpdateCmd *cmd.SpireOcculusUpdateCommand,
) []*cobra.Command {
return []*cobra.Command{
adminPingOcculus.Command(),
Expand All @@ -65,5 +67,6 @@ func ProvideCommands(
testFilesystemCmd.Command(),
spireInstallCmd.Command(),
userChangePasswordCmd.Command(),
spireOcculusUpdateCmd.Command(),
}
}
3 changes: 2 additions & 1 deletion boot/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions internal/console/cmd/spire_occulus_update_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"github.com/Akkadius/spire/internal/occulus"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// SpireOcculusUpdateCommand is the command to update Occulus
type SpireOcculusUpdateCommand struct {
logger *logrus.Logger
command *cobra.Command
occulus *occulus.ProcessManagement
}

// Command implementation of the Command interface
func (c *SpireOcculusUpdateCommand) Command() *cobra.Command {
return c.command
}

// NewSpireOcculusUpdateCommand creates a new spire:init command
func NewSpireOcculusUpdateCommand(
logger *logrus.Logger,
occulus *occulus.ProcessManagement,
) *SpireOcculusUpdateCommand {
i := &SpireOcculusUpdateCommand{
logger: logger,
occulus: occulus,
command: &cobra.Command{
Use: "spire:occulus-update",
Short: "Updates or fetches Occulus",
},
}

i.command.Run = i.Handle

return i
}

// Handle implementation of the Command interface
func (c *SpireOcculusUpdateCommand) Handle(_ *cobra.Command, args []string) {
path, err := c.occulus.FetchOcculusAndGetBinaryPath()
if err != nil {
c.logger.Error(err)
return
}

c.logger.Infof("Occulus updated to [%v]", path)
}
180 changes: 95 additions & 85 deletions internal/occulus/process_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,92 +70,11 @@ func checkIfPortAvailable(port int) (status bool, err error) {
}

func (m *ProcessManagement) Run() error {
client := github.NewClient(nil)
if len(os.Getenv("GITHUB_TOKEN")) > 0 {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := &http.Client{
Timeout: 5 * time.Second,
Transport: &oauth2.Transport{
Source: ts,
},
}
client = github.NewClient(tc)
}

release, _, err := client.Repositories.GetLatestRelease(
context.Background(),
"Akkadius",
"Occulus",
)

var downloadPath string

// check if we have a binary
// if not, download it
downloadPath, err := m.FetchOcculusAndGetBinaryPath()
if err != nil {
m.logger.Error(err)
downloadPath, err = m.GetCurrentOcculusBinaryPath()
m.logger.Infof("[Occulus.ProcessManagement] Using existing download path @ [%v]\n", downloadPath)
if err != nil {
return err
}
}

isWindows := runtime.GOOS == "windows"

if err == nil && len(downloadPath) == 0 {
// build binary target name from asset name
// eg. occulus-v2-1-0
tagName := *release.TagName
binaryName := fmt.Sprintf("%v-%v", "occulus", tagName)
binaryName = strings.ReplaceAll(binaryName, ".", "-")

downloadPath = filepath.Join(m.pathmgmt.GetEQEmuServerPath(), "bin", binaryName)

// kill existing
err = m.KillExistingRunningProcesses()
if err != nil {
return err
}

// cleanup
err = m.CleanupOldVersions(tagName)
if err != nil {
return err
}

if isWindows {
downloadPath += ".exe"
}

// check if binary exists before we try to download it
if _, err := os.Stat(downloadPath); errors.Is(err, os.ErrNotExist) {
// loop through latest release assets
for _, asset := range release.Assets {
releaseAssetName := *asset.Name
releaseDownloadUrl := *asset.BrowserDownloadURL

// Occulus assets use `-win` suffix
assetMatch := runtime.GOOS
if isWindows {
assetMatch = "win"
}

// find asset / release matching the operating system
if strings.Contains(releaseAssetName, assetMatch) {
m.logger.Infof("[Occulus.ProcessManagement] Downloading new binary @ [%v]\n", downloadPath)
err := download.WithProgress(downloadPath, releaseDownloadUrl)
if err != nil {
return err
}
}
}
}
}

// windows is a strange beast
if isWindows {
downloadPath = strings.ReplaceAll(downloadPath, ".exe", "")
return err
}

// run binary
Expand Down Expand Up @@ -301,3 +220,94 @@ func (m *ProcessManagement) KillExistingRunningProcesses() error {

return nil
}

func (m *ProcessManagement) FetchOcculusAndGetBinaryPath() (string, error) {
client := github.NewClient(nil)
if len(os.Getenv("GITHUB_TOKEN")) > 0 {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := &http.Client{
Timeout: 5 * time.Second,
Transport: &oauth2.Transport{
Source: ts,
},
}
client = github.NewClient(tc)
}

release, _, err := client.Repositories.GetLatestRelease(
context.Background(),
"Akkadius",
"Occulus",
)

var downloadPath string

if err != nil {
m.logger.Info(err)
downloadPath, err = m.GetCurrentOcculusBinaryPath()
m.logger.Infof("[Occulus.ProcessManagement] Using existing download path @ [%v]\n", downloadPath)
if err != nil {
return "", err
}
}

isWindows := runtime.GOOS == "windows"
if err == nil && len(downloadPath) == 0 {
// build binary target name from asset name
// eg. occulus-v2-1-0
tagName := *release.TagName
binaryName := fmt.Sprintf("%v-%v", "occulus", tagName)
binaryName = strings.ReplaceAll(binaryName, ".", "-")

downloadPath = filepath.Join(m.pathmgmt.GetEQEmuServerPath(), "bin", binaryName)

// kill existing
err = m.KillExistingRunningProcesses()
if err != nil {
return "", err
}

// cleanup
err = m.CleanupOldVersions(tagName)
if err != nil {
return "", err
}

if isWindows {
downloadPath += ".exe"
}

// check if binary exists before we try to download it
if _, err := os.Stat(downloadPath); errors.Is(err, os.ErrNotExist) {
// loop through latest release assets
for _, asset := range release.Assets {
releaseAssetName := *asset.Name
releaseDownloadUrl := *asset.BrowserDownloadURL

// Occulus assets use `-win` suffix
assetMatch := runtime.GOOS
if isWindows {
assetMatch = "win"
}

// find asset / release matching the operating system
if strings.Contains(releaseAssetName, assetMatch) {
m.logger.Infof("[Occulus.ProcessManagement] Downloading new binary @ [%v]\n", downloadPath)
err := download.WithProgress(downloadPath, releaseDownloadUrl)
if err != nil {
return "", err
}
}
}
}
}

// windows is a strange beast
if isWindows {
downloadPath = strings.ReplaceAll(downloadPath, ".exe", "")
}

return downloadPath, nil
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spire",
"version": "3.1.0",
"version": "3.1.1",
"repository": {
"type": "git",
"url": "https://github.com/Akkadius/spire.git"
Expand Down