Adapt pmctl to new update package in portbase

This commit is contained in:
Daniel 2019-10-25 13:29:16 +02:00
parent bfde6cb044
commit 90c89eb012
7 changed files with 101 additions and 76 deletions

View file

@ -1,61 +0,0 @@
package main
import (
"errors"
"log"
"time"
"github.com/safing/portmaster/updates"
)
func getFile(opts *Options) (*updates.File, error) {
// get newest local file
updates.LoadLatest()
file, err := updates.GetLocalPlatformFile(opts.Identifier)
if err == nil {
return file, nil
}
if err != updates.ErrNotFound {
return nil, err
}
// download
if opts.AllowDownload {
log.Printf("downloading %s...\n", opts.Identifier)
// download indexes
err = updates.UpdateIndexes()
if err != nil {
return nil, err
}
// download file
file, err := updates.GetPlatformFile(opts.Identifier)
if err != nil {
return nil, err
}
return file, nil
}
// wait for 30 seconds
log.Printf("waiting for download of %s (by Portmaster Core) to complete...\n", opts.Identifier)
// try every 0.5 secs
for tries := 0; tries < 60; tries++ {
time.Sleep(500 * time.Millisecond)
// reload local files
updates.LoadLatest()
// get file
file, err := updates.GetLocalPlatformFile(opts.Identifier)
if err == nil {
return file, nil
}
if err != updates.ErrNotFound {
return nil, err
}
}
return nil, errors.New("please try again later or check the Portmaster logs")
}

View file

@ -87,7 +87,7 @@ func getServiceExecCommand(exePath string, escape bool) []string {
maybeEscape(exePath, escape),
"run",
"core-service",
"--db",
"--data",
maybeEscape(dataRoot.Path, escape),
"--input-signals",
}

View file

@ -13,11 +13,10 @@ import (
"github.com/safing/portbase/database/record"
"github.com/safing/portbase/formats/dsd"
"github.com/safing/portbase/info"
"github.com/safing/portmaster/updates"
"github.com/spf13/cobra"
)
func initializeLogFile(logFilePath string, identifier string, updateFile *updates.File) *os.File {
func initializeLogFile(logFilePath string, identifier string, version string) *os.File {
logFile, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0444)
if err != nil {
log.Printf("failed to create log file %s: %s\n", logFilePath, err)
@ -42,7 +41,7 @@ func initializeLogFile(logFilePath string, identifier string, updateFile *update
c.AppendAsBlock(metaSection)
// log file data type (string) and newline for better manual viewing
c.Append([]byte("S\n"))
c.Append([]byte(fmt.Sprintf("executing %s version %s on %s %s\n", identifier, updateFile.Version(), runtime.GOOS, runtime.GOARCH)))
c.Append([]byte(fmt.Sprintf("executing %s version %s on %s %s\n", identifier, version, runtime.GOOS, runtime.GOARCH)))
_, err = logFile.Write(c.CompileData())
if err != nil {
@ -83,7 +82,7 @@ func initControlLogFile() *os.File {
// open log file
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
return initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
return initializeLogFile(logFilePath, "control/portmaster-control", info.Version())
}
func logControlError(cErr error) {
@ -101,7 +100,7 @@ func logControlError(cErr error) {
// open log file
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.error.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", info.Version())
if errorFile == nil {
return
}
@ -121,7 +120,7 @@ func logControlStack() {
// open log file
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.stack.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", updates.NewFile("", info.Version(), false))
errorFile := initializeLogFile(logFilePath, "control/portmaster-control", info.Version())
if errorFile == nil {
return
}

View file

@ -9,8 +9,9 @@ import (
"strings"
"syscall"
"github.com/safing/portbase/updater"
"github.com/safing/portmaster/core/structure"
"github.com/safing/portmaster/updates"
"github.com/safing/portbase/utils"
@ -28,6 +29,17 @@ var (
showShortVersion bool
showFullVersion bool
// create registry
registry = &updater.ResourceRegistry{
Name: "updates",
UpdateURLs: []string{
"https://updates.safing.io",
},
Beta: false,
DevMode: false,
Online: false,
}
rootCmd = &cobra.Command{
Use: "portmaster-control",
Short: "Controller for all portmaster components",
@ -153,8 +165,24 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
return fmt.Errorf("failed to initialize data root: %s", err)
}
dataRoot = structure.Root()
// manually set updates root (no modules)
updates.SetDataRoot(structure.Root())
// initialize registry
err := registry.Initialize(structure.Root().ChildDir("updates", 0755))
if err != nil {
return err
}
err = registry.LoadIndexes()
if err != nil {
return err
}
err = registry.ScanStorage("")
if err != nil {
log.Printf("WARNING: error during storage scan: %s\n", err)
}
registry.SelectVersions()
}
// logs and warning

View file

@ -102,6 +102,11 @@ func handleRun(cmd *cobra.Command, opts *Options) (err error) {
func run(cmd *cobra.Command, opts *Options) (err error) {
// set download option
if opts.AllowDownload {
registry.Online = true
}
// parse identifier
opts.ShortIdentifier = path.Dir(opts.Identifier)
@ -124,7 +129,7 @@ func run(cmd *cobra.Command, opts *Options) (err error) {
// notify service after some time
go func() {
// assume that after 5 seconds service has finished starting
// assume that after 3 seconds service has finished starting
time.Sleep(3 * time.Second)
startupComplete <- struct{}{}
}()
@ -188,7 +193,7 @@ func run(cmd *cobra.Command, opts *Options) (err error) {
}
func execute(opts *Options, args []string) (cont bool, err error) {
file, err := getFile(opts)
file, err := registry.GetFile(platform(opts.Identifier))
if err != nil {
return true, fmt.Errorf("could not get component: %s", err)
}
@ -218,13 +223,13 @@ func execute(opts *Options, args []string) (cont bool, err error) {
} else {
// open log file
logFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
logFile = initializeLogFile(logFilePath, opts.Identifier, file)
logFile = initializeLogFile(logFilePath, opts.Identifier, file.Version())
if logFile != nil {
defer finalizeLogFile(logFile, logFilePath)
}
// open error log file
errorFilePath := filepath.Join(logFileBasePath, fmt.Sprintf("%s.error.log", time.Now().UTC().Format("2006-02-01-15-04-05")))
errorFile = initializeLogFile(errorFilePath, opts.Identifier, file)
errorFile = initializeLogFile(errorFilePath, opts.Identifier, file.Version())
if errorFile != nil {
defer finalizeLogFile(errorFile, errorFilePath)
}

View file

@ -79,7 +79,7 @@ func show(cmd *cobra.Command, opts *Options) error {
opts.Identifier += ".exe"
}
file, err := getFile(opts)
file, err := registry.GetFile(platform(opts.Identifier))
if err != nil {
return fmt.Errorf("could not get component: %s", err)
}

54
pmctl/update.go Normal file
View file

@ -0,0 +1,54 @@
package main
import (
"context"
"fmt"
"runtime"
"github.com/safing/portbase/log"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(updatesCmd)
}
var updatesCmd = &cobra.Command{
Use: "update",
Short: "Run a manual update process",
RunE: func(cmd *cobra.Command, args []string) error {
return downloadUpdates()
},
}
func downloadUpdates() error {
// mark required updates
if onWindows {
registry.MandatoryUpdates = []string{
platform("core/portmaster-core.exe"),
platform("control/portmaster-control.exe"),
platform("app/portmaster-app.exe"),
platform("notifier/portmaster-notifier.exe"),
platform("notifier/portmaster-snoretoast.exe"),
}
} else {
registry.MandatoryUpdates = []string{
platform("core/portmaster-core"),
platform("control/portmaster-control"),
platform("app/portmaster-app"),
platform("notifier/portmaster-notifier"),
}
}
// ok, now we want logging.
err := log.Start()
if err != nil {
fmt.Printf("failed to start logging: %s\n", err)
}
return registry.DownloadUpdates(context.TODO())
}
func platform(identifier string) string {
return fmt.Sprintf("%s_%s/%s", runtime.GOOS, runtime.GOARCH, identifier)
}