Skip to content

Commit

Permalink
Make "machine ID" easier to supply on import
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Sep 8, 2023
1 parent ec73137 commit f234e49
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/gdpr_import/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"flag"
"os"
"path"
Expand All @@ -10,6 +9,7 @@ import (
"github.com/turt2live/matrix-media-repo/archival/v2archive"
"github.com/turt2live/matrix-media-repo/common/assets"
"github.com/turt2live/matrix-media-repo/common/config"
"github.com/turt2live/matrix-media-repo/common/import_cmdline"
"github.com/turt2live/matrix-media-repo/common/logging"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/common/runtime"
Expand Down Expand Up @@ -37,7 +37,7 @@ func main() {
assets.SetupMigrations(*migrationsPath)

if ids.GetMachineId() == 0 {
panic(errors.New("expected custom machine ID for import process (unsafe to import as Machine 0)"))
import_cmdline.AskMachineId()
}

var err error
Expand Down
3 changes: 2 additions & 1 deletion cmd/import_synapse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/common/assets"
"github.com/turt2live/matrix-media-repo/common/config"
"github.com/turt2live/matrix-media-repo/common/import_cmdline"
"github.com/turt2live/matrix-media-repo/common/logging"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/common/runtime"
Expand Down Expand Up @@ -52,7 +53,7 @@ func main() {
assets.SetupMigrations(*migrationsPath)

if ids.GetMachineId() == 0 {
panic(errors.New("expected custom machine ID for import process (unsafe to import as Machine 0)"))
import_cmdline.AskMachineId()
}

var realPsqlPassword string
Expand Down
27 changes: 27 additions & 0 deletions common/import_cmdline/ask_machine_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package import_cmdline

import (
"fmt"
"os"

"github.com/turt2live/matrix-media-repo/util/ids"
"golang.org/x/term"
)

func AskMachineId() {
fmt.Println("The importer runs as a MMR worker and needs to have a dedicated MACHINE_ID. See https://docs.t2bot.io/matrix-media-repo/deployment/horizontal_scaling.html for details on what a MACHINE_ID is.")
if !term.IsTerminal(int(os.Stdin.Fd())) {
fmt.Println("Please specify a MACHINE_ID environment variable.")
os.Exit(2)
return // for good measure
}
fmt.Println("If you don't use horizontal scaling, you can use '1' as the machine ID. Otherwise, please enter an unused machine ID in your environment.")
fmt.Printf("Machine ID: ")
var machineId int64
if _, err := fmt.Scanf("%d", &machineId); err != nil {
panic(err)
}
if err := ids.SetMachineId(machineId); err != nil {
panic(err)
}
}
15 changes: 15 additions & 0 deletions util/ids/snowflake.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ids

import (
"errors"
"os"
"strconv"

Expand Down Expand Up @@ -30,3 +31,17 @@ func makeSnowflake() (*snowflake.Node, error) {
sfnode = node
return sfnode, nil
}

func SetMachineId(id int64) error {
if err := os.Setenv("MACHINE_ID", strconv.FormatInt(id, 10)); err != nil {
return err
}
sfnode = nil
if GetMachineId() != id {
return errors.New("unexpected error setting machine ID")
}
if _, err := makeSnowflake(); err != nil {
return err
}
return nil
}

0 comments on commit f234e49

Please sign in to comment.