2024-06-12 02:42:10 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-08-09 03:24:54 +02:00
|
|
|
"runtime/debug"
|
2024-06-15 23:59:14 +02:00
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
"runtime"
|
2024-06-13 03:17:56 +02:00
|
|
|
"sync"
|
2024-06-12 02:42:10 +02:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/authkey"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/filestore"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/service"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/telemetry"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/shellutil"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wavebase"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wcloud"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wconfig"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wcore"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/web"
|
2024-09-12 02:39:08 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/wlayout"
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshremote"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc/wshserver"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshutil"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
2024-06-12 02:42:10 +02:00
|
|
|
)
|
|
|
|
|
2024-08-01 08:47:33 +02:00
|
|
|
// these are set at build time
|
|
|
|
var WaveVersion = "0.0.0"
|
|
|
|
var BuildTime = "0"
|
|
|
|
|
2024-09-23 22:33:39 +02:00
|
|
|
const InitialTelemetryWait = 10 * time.Second
|
|
|
|
const TelemetryTick = 2 * time.Minute
|
2024-08-09 03:24:54 +02:00
|
|
|
const TelemetryInterval = 4 * time.Hour
|
|
|
|
|
2024-06-13 03:17:56 +02:00
|
|
|
var shutdownOnce sync.Once
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func doShutdown(reason string) {
|
2024-06-13 03:17:56 +02:00
|
|
|
shutdownOnce.Do(func() {
|
|
|
|
log.Printf("shutting down: %s\n", reason)
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
2024-09-05 09:21:08 +02:00
|
|
|
go blockcontroller.StopAllBlockControllers()
|
2024-08-09 03:24:54 +02:00
|
|
|
shutdownActivityUpdate()
|
|
|
|
sendTelemetryWrapper()
|
2024-06-13 03:17:56 +02:00
|
|
|
// TODO deal with flush in progress
|
|
|
|
filestore.WFS.FlushCache(ctx)
|
2024-06-20 08:59:41 +02:00
|
|
|
watcher := wconfig.GetWatcher()
|
|
|
|
if watcher != nil {
|
|
|
|
watcher.Close()
|
|
|
|
}
|
2024-09-05 09:21:08 +02:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2024-10-06 22:40:02 +02:00
|
|
|
log.Printf("shutdown complete\n")
|
2024-06-13 03:17:56 +02:00
|
|
|
os.Exit(0)
|
|
|
|
})
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func installShutdownSignalHandlers() {
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
go func() {
|
|
|
|
for sig := range sigCh {
|
|
|
|
doShutdown(fmt.Sprintf("got signal %v", sig))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-06-13 03:17:56 +02:00
|
|
|
// watch stdin, kill server if stdin is closed
|
|
|
|
func stdinReadWatch() {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
_, err := os.Stdin.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
doShutdown(fmt.Sprintf("stdin closed/error (%v)", err))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 08:59:41 +02:00
|
|
|
func configWatcher() {
|
|
|
|
watcher := wconfig.GetWatcher()
|
|
|
|
if watcher != nil {
|
|
|
|
watcher.Start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 03:24:54 +02:00
|
|
|
func telemetryLoop() {
|
|
|
|
var nextSend int64
|
|
|
|
time.Sleep(InitialTelemetryWait)
|
|
|
|
for {
|
|
|
|
if time.Now().Unix() > nextSend {
|
|
|
|
nextSend = time.Now().Add(TelemetryInterval).Unix()
|
|
|
|
sendTelemetryWrapper()
|
|
|
|
}
|
|
|
|
time.Sleep(TelemetryTick)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTelemetryWrapper() {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("[error] in sendTelemetryWrapper: %v\n", r)
|
|
|
|
debug.PrintStack()
|
|
|
|
}()
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
2024-08-09 03:24:54 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[error] getting client data for telemetry: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wcloud.SendTelemetry(ctx, client.OID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[error] sending telemetry: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startupActivityUpdate() {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
activity := telemetry.ActivityUpdate{
|
|
|
|
Startup: 1,
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
activity.NumTabs, _ = wstore.DBGetCount[*waveobj.Tab](ctx)
|
2024-08-09 03:24:54 +02:00
|
|
|
err := telemetry.UpdateActivity(ctx, activity) // set at least one record into activity (don't use go routine wrap here)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error updating startup activity: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shutdownActivityUpdate() {
|
|
|
|
activity := telemetry.ActivityUpdate{Shutdown: 1}
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 1*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
err := telemetry.UpdateActivity(ctx, activity) // do NOT use the go routine wrap here (this needs to be synchronous)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error updating shutdown activity: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:52:35 +02:00
|
|
|
func createMainWshClient() {
|
|
|
|
rpc := wshserver.GetMainRpcClient()
|
2024-10-24 07:43:17 +02:00
|
|
|
wshutil.DefaultRouter.RegisterRoute(wshutil.DefaultRoute, rpc, true)
|
2024-08-14 03:19:29 +02:00
|
|
|
wps.Broker.SetClient(wshutil.DefaultRouter)
|
2024-08-30 20:33:04 +02:00
|
|
|
localConnWsh := wshutil.MakeWshRpc(nil, nil, wshrpc.RpcContext{Conn: wshrpc.LocalConnName}, &wshremote.ServerImpl{})
|
|
|
|
go wshremote.RunSysInfoLoop(localConnWsh, wshrpc.LocalConnName)
|
2024-10-24 07:43:17 +02:00
|
|
|
wshutil.DefaultRouter.RegisterRoute(wshutil.MakeConnectionRouteId(wshrpc.LocalConnName), localConnWsh, true)
|
2024-08-14 01:52:35 +02:00
|
|
|
}
|
|
|
|
|
2024-10-27 21:12:41 +01:00
|
|
|
func grabAndRemoveEnvVars() error {
|
|
|
|
err := authkey.SetAuthKeyFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting auth key: %v", err)
|
|
|
|
}
|
|
|
|
err = wavebase.CacheAndRemoveEnvVars()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = wcloud.CacheAndRemoveEnvVars()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 02:42:10 +02:00
|
|
|
func main() {
|
2024-06-13 04:33:44 +02:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
|
|
|
log.SetPrefix("[wavesrv] ")
|
2024-08-01 08:47:33 +02:00
|
|
|
wavebase.WaveVersion = WaveVersion
|
2024-08-09 03:24:54 +02:00
|
|
|
wavebase.BuildTime = BuildTime
|
2024-06-13 04:33:44 +02:00
|
|
|
|
2024-10-27 21:12:41 +01:00
|
|
|
err := grabAndRemoveEnvVars()
|
2024-08-22 00:04:39 +02:00
|
|
|
if err != nil {
|
2024-10-27 21:12:41 +01:00
|
|
|
log.Printf("[error] %v\n", err)
|
2024-08-22 00:04:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = service.ValidateServiceMap()
|
2024-06-12 02:42:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error validating service map: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
err = wavebase.EnsureWaveDataDir()
|
2024-06-12 02:42:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave home dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-09-05 23:05:42 +02:00
|
|
|
err = wavebase.EnsureWaveDBDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave db dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
err = wavebase.EnsureWaveConfigDir()
|
2024-09-11 18:26:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave config dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-22 01:51:18 +02:00
|
|
|
|
|
|
|
// TODO: rather than ensure this dir exists, we should let the editor recursively create parent dirs on save
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
err = wavebase.EnsureWavePresetsDir()
|
2024-10-22 01:51:18 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave presets dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
waveLock, err := wavebase.AcquireWaveLock()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error acquiring wave lock (another instance of Wave is likely running): %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-15 23:59:14 +02:00
|
|
|
defer func() {
|
2024-10-04 21:20:52 +02:00
|
|
|
err = waveLock.Close()
|
2024-06-15 23:59:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error releasing wave lock: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-08-01 08:47:33 +02:00
|
|
|
log.Printf("wave version: %s (%s)\n", WaveVersion, BuildTime)
|
Update data and config paths to match platform defaults (#1047)
Going forward for new installations, config and data files will be
stored at the platform default paths, as defined by
[env-paths](https://www.npmjs.com/package/env-paths).
For backwards compatibility, if the `~/.waveterm` or `WAVETERM_HOME`
directory exists and contains valid data, it will be used. If this check
fails, then `WAVETERM_DATA_HOME` and `WAVETERM_CONFIG_HOME` will be
used. If these are not defined, then `XDG_DATA_HOME` and
`XDG_CONFIG_HOME` will be used. Finally, if none of these are defined,
the [env-paths](https://www.npmjs.com/package/env-paths) defaults will
be used.
As with the existing app, dev instances will write to `waveterm-dev`
directories, while all others will write to `waveterm`.
2024-10-22 18:26:58 +02:00
|
|
|
log.Printf("wave data dir: %s\n", wavebase.GetWaveDataDir())
|
|
|
|
log.Printf("wave config dir: %s\n", wavebase.GetWaveConfigDir())
|
2024-06-12 02:42:10 +02:00
|
|
|
err = filestore.InitFilestore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing filestore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = wstore.InitWStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing wstore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-08-01 08:47:33 +02:00
|
|
|
go func() {
|
|
|
|
err := shellutil.InitCustomShellStartupFiles()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing wsh and shell-integration files: %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-09-17 09:00:20 +02:00
|
|
|
window, firstRun, err := wcore.EnsureInitialData()
|
2024-06-12 02:42:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring initial data: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-09-26 00:52:12 +02:00
|
|
|
if firstRun {
|
|
|
|
migrateErr := wstore.TryMigrateOldHistory()
|
|
|
|
if migrateErr != nil {
|
|
|
|
log.Printf("error migrating old history: %v\n", migrateErr)
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 02:39:08 +02:00
|
|
|
if window != nil {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
2024-09-17 09:00:20 +02:00
|
|
|
if !firstRun {
|
|
|
|
err = wlayout.BootstrapNewWindowLayout(ctx, window)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("error applying new window layout: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-09-12 02:39:08 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-14 01:52:35 +02:00
|
|
|
createMainWshClient()
|
2024-06-12 02:42:10 +02:00
|
|
|
installShutdownSignalHandlers()
|
2024-08-09 03:24:54 +02:00
|
|
|
startupActivityUpdate()
|
2024-06-13 03:17:56 +02:00
|
|
|
go stdinReadWatch()
|
2024-08-09 03:24:54 +02:00
|
|
|
go telemetryLoop()
|
2024-06-20 08:59:41 +02:00
|
|
|
configWatcher()
|
2024-07-18 03:42:49 +02:00
|
|
|
webListener, err := web.MakeTCPListener("web")
|
2024-07-09 22:47:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating web listener: %v\n", err)
|
2024-07-09 23:31:16 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-18 03:42:49 +02:00
|
|
|
wsListener, err := web.MakeTCPListener("websocket")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating websocket listener: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go web.RunWebSocketServer(wsListener)
|
2024-07-18 00:24:43 +02:00
|
|
|
unixListener, err := web.MakeUnixListener()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating unix listener: %v\n", err)
|
|
|
|
return
|
2024-07-09 22:47:39 +02:00
|
|
|
}
|
2024-06-12 02:42:10 +02:00
|
|
|
go func() {
|
2024-10-27 21:12:41 +01:00
|
|
|
if BuildTime == "" {
|
|
|
|
BuildTime = "0"
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
2024-10-27 21:12:41 +01:00
|
|
|
// use fmt instead of log here to make sure it goes directly to stderr
|
|
|
|
fmt.Fprintf(os.Stderr, "WAVESRV-ESTART ws:%s web:%s version:%s buildtime:%s\n", wsListener.Addr(), webListener.Addr(), WaveVersion, BuildTime)
|
2024-06-12 02:42:10 +02:00
|
|
|
}()
|
2024-08-17 20:21:25 +02:00
|
|
|
go wshutil.RunWshRpcOverListener(unixListener)
|
2024-07-09 22:47:39 +02:00
|
|
|
web.RunWebServer(webListener) // blocking
|
2024-06-12 02:42:10 +02:00
|
|
|
runtime.KeepAlive(waveLock)
|
|
|
|
}
|