Files

275 lines
8.8 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package cmd
import (
"context"
"fmt"
"maps"
"slices"
"strings"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
rt "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/runtime"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
)
const sharedProjectName = "fluxer"
func (t *moveBase) projectName() string {
if t.project != "" {
return t.project
}
return t.inst.Project()
}
func (t *moveBase) shareNetwork(ctx context.Context) error {
s := t.inst.Shared()
if s == nil {
return nil
}
if t.runtime != instance.RuntimeContainer && s.Network != "" {
return ui.Refused(fmt.Sprintf("%s in the compose project %s %s to Fluxer on the network %s, and the systemd service can't join it.", ui.JoinAnd(s.Joined), s.Project, oneOrMany(len(s.Joined), "connects", "connect"), s.Network), "Run "+migrateCommand+" --to compose or --to docker instead, which keep Fluxer's container on that network.")
}
e, err := t.app.Engine(ctx, t.inst)
if err != nil {
return err
}
if t.project, err = freeProject(ctx, e, s.Project); err != nil {
return rt.EngineError(e.Kind, err)
}
if s.Network != "" {
t.chosen[settings.NetworkAttach] = s.Network
t.chosen[settings.NetworkDefault] = "true"
if len(s.Aliases) > 0 {
t.chosen[settings.NetworkAliases] = strings.Join(s.Aliases, ",")
}
}
return nil
}
func freeProject(ctx context.Context, e *engine.Engine, shared string) (string, error) {
projects, err := e.Projects(ctx)
if err != nil {
return "", err
}
taken := func(name string) (bool, error) {
if slices.ContainsFunc(projects, func(p engine.ProjectInfo) bool { return p.Name == name }) {
return true, nil
}
if slices.ContainsFunc(instance.Others(""), func(o *instance.Instance) bool { return o.Project() == name }) {
return true, nil
}
if vols, err := e.Volumes(ctx, name); err != nil || len(vols) > 0 {
return true, err
}
if _, ok, err := e.Inspect(ctx, name); err != nil || ok {
return true, err
}
return e.VolumeExists(ctx, name+"-data")
}
for n := 0; n < 100; n++ {
name := sharedProjectName
switch {
case n == 1:
name = shared + "-" + sharedProjectName
case n > 1:
name = fmt.Sprintf("%s-%s-%d", shared, sharedProjectName, n)
}
busy, err := taken(name)
if err != nil {
return "", err
}
if !busy {
return name, nil
}
}
return "", fmt.Errorf("no free compose project name next to %s", shared)
}
func sharedPlan(inst *instance.Instance, project string) []ui.PlanItem {
s := inst.Shared()
if s == nil {
return nil
}
items := []ui.PlanItem{{Verb: ui.PlanKeep, Text: fmt.Sprintf("Leave %s running in the compose project %s", ui.JoinAnd(s.Others), s.Project), Detail: "rexctl stops and removes only Fluxer's containers and volumes"}}
where := fmt.Sprintf("Run Fluxer as its own compose project %s", project)
detail := ""
if s.Network != "" {
detail = fmt.Sprintf("on the network %s", s.Network)
if len(s.Aliases) > 0 {
detail += fmt.Sprintf(" as %s, so your proxy keeps sending to %s:8080", ui.JoinAnd(s.Aliases), s.Aliases[0])
}
}
items = append(items, ui.PlanItem{Verb: ui.PlanCreate, Text: where, Detail: detail})
if s.Included {
items = append(items, ui.PlanItem{Verb: ui.PlanChange, Text: fmt.Sprintf("Empty %s once Fluxer runs again", ui.JoinAnd(s.Fluxer)), Detail: fmt.Sprintf("so %s in %s starts no old Fluxer containers. rexctl keeps each file as a .pre-move copy and puts them back if you undo", s.RootCommand(), s.Dir)})
}
return items
}
func targetProject(inst *instance.Instance, target any) string {
if t, ok := target.(interface{ projectName() string }); ok {
return t.projectName()
}
return inst.Project()
}
func finishShared(ctx context.Context, app *App, old, moved *instance.Instance) {
if old.Shared() == nil {
return
}
if fresh, err := instance.Find(old.Dir); err == nil {
old = fresh
}
s := old.Shared()
if s == nil {
return
}
pinOld(ctx, app, old)
if !s.Included {
app.UI.Warn(fmt.Sprintf("%s in %s still lists Fluxer's compose files, so %s there would start the old containers again.", relPathOr(s.Dir, s.Files), s.Dir, s.RootCommand()), fmt.Sprintf("Take the files in %s out of it. Fluxer now runs from %s.", old.Dir, moved.Dir))
return
}
stubbed, err := old.StubFiles(ctx)
if err != nil {
app.UI.Detail("emptying the compose files in %s: %v", old.Dir, err)
app.UI.Warn(fmt.Sprintf("Couldn't empty %s, so %s in %s would start the old containers again.", ui.JoinAnd(s.Fluxer), s.RootCommand(), s.Dir), fmt.Sprintf("Take %s out of the include in your compose files there. Fluxer now runs from %s.", old.Dir, moved.Dir))
return
}
if len(stubbed) > 0 {
keep := "nothing"
if s.Network != "" {
keep = "only the network " + s.Network
}
app.UI.OK("Emptied %s in %s, which now define %s. The earlier files are next to them as %s copies.", ui.JoinAnd(stubbed), old.Dir, keep, instance.PreMoveSuffix)
}
line := fmt.Sprintf("%s %s running in the compose project %s, and %s in %s no longer starts Fluxer.", ui.JoinAnd(s.Others), oneOrMany(len(s.Others), "keeps", "keep"), s.Project, s.RootCommand(), s.Dir)
if s.Network != "" && len(s.Aliases) > 0 {
line += fmt.Sprintf(" Fluxer's container is on %s as %s, so your proxy needs no change.", s.Network, ui.JoinAnd(s.Aliases))
}
app.UI.Info("%s", line)
}
func relPathOr(dir string, files []string) string {
if len(files) == 0 {
return dir
}
return ui.JoinAnd(files)
}
func pinOld(ctx context.Context, app *App, old *instance.Instance) {
s := old.Shared()
e, err := app.Engine(ctx, old)
if err != nil {
app.UI.Detail("reading the old containers: %v", err)
return
}
containers, err := e.Named(s.Project, "").PS(ctx)
if err != nil {
app.UI.Detail("reading the old containers: %v", err)
return
}
pinned := map[string]string{}
for _, c := range containers {
if !slices.Contains(s.Services, c.Service) {
continue
}
info, ok, err := e.Inspect(ctx, c.ID)
if err != nil || !ok || info.Restart != "always" {
continue
}
if err := e.SetRestart(ctx, c.Name, "no"); err != nil {
app.UI.Detail("keeping %s stopped after a restart: %v", c.Name, err)
continue
}
pinned[c.Name] = info.Restart
}
if len(pinned) == 0 {
return
}
s.Restart = pinned
if err := old.Save(); err != nil {
app.UI.Detail("saving %s: %v", old.Dir, err)
}
}
func unpinOld(ctx context.Context, app *App, old *instance.Instance) {
s := old.Shared()
if s == nil || len(s.Restart) == 0 {
return
}
e, err := app.Engine(ctx, old)
if err != nil {
app.UI.Detail("reading the old containers: %v", err)
return
}
for _, name := range slices.Sorted(maps.Keys(s.Restart)) {
if _, ok, err := e.Inspect(ctx, name); err != nil || !ok {
continue
}
if err := e.SetRestart(ctx, name, s.Restart[name]); err != nil {
app.UI.Detail("putting back the restart policy of %s: %v", name, err)
}
}
s.Restart = nil
}
func undoShared(ctx context.Context, app *App, old *instance.Instance) error {
unpinOld(ctx, app, old)
back, err := old.RestoreStubs()
if err != nil {
return ui.Wrap(err, fmt.Sprintf("Couldn't put back the compose files in %s.", old.Dir), fmt.Sprintf("Rename each %s copy there back to its own name, then run %s --undo again.", instance.PreMoveSuffix, migrateCommand))
}
if len(back) > 0 {
app.UI.OK("Put back %s in %s.", ui.JoinAnd(back), old.Dir)
}
return nil
}
func removeSharedOld(ctx context.Context, e *engine.Engine, old *instance.Instance) error {
s := old.Shared()
p, err := deploy.Load(ctx, old, e)
if err != nil {
return err
}
containers, err := e.Named(s.Project, "").PS(ctx)
if err != nil {
return err
}
var services []string
for _, c := range containers {
if (slices.Contains(s.Services, c.Service) || slices.Contains(p.Services(), c.Service)) && !slices.Contains(s.Others, c.Service) && !slices.Contains(services, c.Service) {
services = append(services, c.Service)
}
}
if len(services) > 0 {
if err := p.Remove(ctx, services...); err != nil {
return err
}
}
return rt.RemoveVolumes(ctx, e, s.Volumes)
}
func sharedCleanUpLine(old *instance.Instance) string {
s := old.Shared()
if s == nil || !s.Included {
return ""
}
line := fmt.Sprintf("Your compose files in %s still include %s, which define %s now, and the %s copies there hold the old files.", s.Dir, ui.JoinAnd(s.Fluxer), sharedKeeps(s), instance.PreMoveSuffix)
if s.Network != "" {
return line + fmt.Sprintf(" To take the include out, first give %s the network %s with external: true, so Fluxer's container stays reachable.", ui.JoinAnd(s.Joined), s.Network)
}
return line + " You can take that include out."
}
func sharedKeeps(s *instance.SharedRoot) string {
if s.Network == "" {
return "nothing"
}
return "only the network " + s.Network
}