package provider import ( "context" "fmt" "os" "runtime" "strings" "sync" "git.starwarslab.us/ATOM_IT/Deployr/internal/catalog" "git.starwarslab.us/ATOM_IT/Deployr/internal/run" ) // ------------------------------------------------------------------- apt --- type apt struct { refreshOnce sync.Once } func newApt() *apt { return &apt{} } func (a *apt) Name() string { return "apt" } func (a *apt) OSes() []string { return []string{"linux"} } func (a *apt) Label() string { return "APT (Debian/Ubuntu)" } func (a *apt) Available(context.Context, Opts) bool { return runtime.GOOS == "linux" && run.Available("apt-get") } // aptEnv keeps dpkg from opening a curses prompt and from asking about changed // config files -- either one hangs an unattended run forever. var aptEnv = []string{"DEBIAN_FRONTEND=noninteractive", "APT_LISTCHANGES_FRONTEND=none"} var aptConfArgs = []string{ "-o", "Dpkg::Options::=--force-confdef", "-o", "Dpkg::Options::=--force-confold", } func (a *apt) exec(ctx context.Context, o Opts, args ...string) run.Result { return run.Exec(ctx, run.Options{ Name: "apt-get", Args: args, Env: aptEnv, NeedRoot: true, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } // refresh updates the package index once per process. A stale index is the most // common reason an install fails on a machine that has been off for a while. func (a *apt) refresh(ctx context.Context, o Opts) { a.refreshOnce.Do(func() { if o.DryRun { return } if res := a.exec(ctx, o, "update", "-qq"); res.Failed() && o.Log != nil { o.Log.Debugf("apt-get update failed (continuing): %s", res.Error()) } }) } func (a *apt) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { a.refresh(ctx, o) args := append([]string{"install", "-y"}, aptConfArgs...) args = append(args, src.ID) args = append(args, src.Args...) res := a.exec(ctx, o, args...) return simpleOutcome(res, o, "apt-get install", src.ID) } func (a *apt) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { a.refresh(ctx, o) args := append([]string{"install", "--only-upgrade", "-y"}, aptConfArgs...) args = append(args, src.ID) res := a.exec(ctx, o, args...) return simpleOutcome(res, o, "apt-get upgrade", src.ID) } func (a *apt) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := a.exec(ctx, o, "remove", "-y", src.ID) return simpleOutcome(res, o, "apt-get remove", src.ID) } func (a *apt) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := run.Exec(ctx, run.Options{ Name: "dpkg-query", Args: []string{"-W", "-f=${Version}", src.ID}, Timeout: o.Timeout, Log: o.Log, }) if res.Failed() || strings.TrimSpace(res.Stdout) == "" { return "", false } return strings.TrimSpace(res.Stdout), true } // ------------------------------------------------------------ dnf/zypper --- // rpmMgr covers the RPM-family managers, which differ only in their // non-interactive switch and subcommand names. type rpmMgr struct { name string // provider name: dnf or zypper bins []string // candidate executables, in preference order once sync.Once bin string } func (r *rpmMgr) Name() string { return r.name } func (r *rpmMgr) OSes() []string { return []string{"linux"} } func (r *rpmMgr) Label() string { if r.name == "zypper" { return "Zypper (openSUSE)" } return "DNF/YUM (Fedora/RHEL)" } func (r *rpmMgr) resolve() string { r.once.Do(func() { if runtime.GOOS != "linux" { return } for _, b := range r.bins { if run.Available(b) { r.bin = b return } } }) return r.bin } func (r *rpmMgr) Available(context.Context, Opts) bool { return r.resolve() != "" } func (r *rpmMgr) exec(ctx context.Context, o Opts, args ...string) run.Result { if r.name == "zypper" { args = append([]string{"--non-interactive"}, args...) } else { args = append(args, "-y") } return run.Exec(ctx, run.Options{ Name: r.resolve(), Args: args, NeedRoot: true, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (r *rpmMgr) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := r.exec(ctx, o, append([]string{"install", src.ID}, src.Args...)...) return simpleOutcome(res, o, r.name+" install", src.ID) } func (r *rpmMgr) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { verb := "upgrade" if r.name == "zypper" { verb = "update" } res := r.exec(ctx, o, verb, src.ID) return simpleOutcome(res, o, r.name+" "+verb, src.ID) } func (r *rpmMgr) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := r.exec(ctx, o, "remove", src.ID) return simpleOutcome(res, o, r.name+" remove", src.ID) } func (r *rpmMgr) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := run.Exec(ctx, run.Options{ Name: "rpm", Args: []string{"-q", "--qf", "%{VERSION}-%{RELEASE}", src.ID}, Timeout: o.Timeout, Log: o.Log, }) if res.Failed() || strings.TrimSpace(res.Stdout) == "" { return "", false } return strings.TrimSpace(res.Stdout), true } // ---------------------------------------------------------------- pacman --- type pacman struct{} func (p *pacman) Name() string { return "pacman" } func (p *pacman) OSes() []string { return []string{"linux"} } func (p *pacman) Label() string { return "pacman (Arch)" } func (p *pacman) Available(context.Context, Opts) bool { return runtime.GOOS == "linux" && run.Available("pacman") } func (p *pacman) exec(ctx context.Context, o Opts, args ...string) run.Result { return run.Exec(ctx, run.Options{ Name: "pacman", Args: args, NeedRoot: true, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (p *pacman) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := p.exec(ctx, o, append([]string{"-S", "--noconfirm", "--needed", src.ID}, src.Args...)...) return simpleOutcome(res, o, "pacman -S", src.ID) } func (p *pacman) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { // Arch does not support upgrading a single package against a stale index; // -Syu with a target is the supported way to avoid a partial upgrade. res := p.exec(ctx, o, "-Syu", "--noconfirm", src.ID) return simpleOutcome(res, o, "pacman -Syu", src.ID) } func (p *pacman) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := p.exec(ctx, o, "-Rns", "--noconfirm", src.ID) return simpleOutcome(res, o, "pacman -Rns", src.ID) } func (p *pacman) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := run.Exec(ctx, run.Options{Name: "pacman", Args: []string{"-Q", src.ID}, Timeout: o.Timeout, Log: o.Log}) if res.Failed() { return "", false } fields := strings.Fields(res.Stdout) if len(fields) >= 2 { return fields[1], true } return "", true } // --------------------------------------------------------------- flatpak --- type flatpak struct{} func (f *flatpak) Name() string { return "flatpak" } func (f *flatpak) OSes() []string { return []string{"linux"} } func (f *flatpak) Label() string { return "Flatpak" } func (f *flatpak) Available(context.Context, Opts) bool { return runtime.GOOS == "linux" && run.Available("flatpak") } // scope installs system-wide when we have root and per-user otherwise, so an // unprivileged run still succeeds instead of erroring out. func (f *flatpak) scope() string { if os.Geteuid() == 0 { return "--system" } return "--user" } func (f *flatpak) exec(ctx context.Context, o Opts, args ...string) run.Result { return run.Exec(ctx, run.Options{ Name: "flatpak", Args: args, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (f *flatpak) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { repo := src.Repo if repo == "" { repo = "flathub" } args := []string{"install", f.scope(), "-y", "--noninteractive", repo, src.ID} res := f.exec(ctx, o, append(args, src.Args...)...) return simpleOutcome(res, o, "flatpak install", src.ID) } func (f *flatpak) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := f.exec(ctx, o, "update", f.scope(), "-y", "--noninteractive", src.ID) return simpleOutcome(res, o, "flatpak update", src.ID) } func (f *flatpak) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := f.exec(ctx, o, "uninstall", f.scope(), "-y", "--noninteractive", src.ID) return simpleOutcome(res, o, "flatpak uninstall", src.ID) } func (f *flatpak) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := f.exec(ctx, o, "info", src.ID) if res.Failed() { return "", false } for _, line := range strings.Split(res.Stdout, "\n") { if strings.HasPrefix(strings.TrimSpace(line), "Version:") { return strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "Version:")), true } } return "", true } // ------------------------------------------------------------------ snap --- type snap struct{} func (s *snap) Name() string { return "snap" } func (s *snap) OSes() []string { return []string{"linux"} } func (s *snap) Label() string { return "Snap" } func (s *snap) Available(context.Context, Opts) bool { return runtime.GOOS == "linux" && run.Available("snap") } func (s *snap) exec(ctx context.Context, o Opts, args ...string) run.Result { return run.Exec(ctx, run.Options{ Name: "snap", Args: args, NeedRoot: true, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (s *snap) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := s.exec(ctx, o, append([]string{"install", src.ID}, src.Args...)...) return simpleOutcome(res, o, "snap install", src.ID) } func (s *snap) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := s.exec(ctx, o, "refresh", src.ID) return simpleOutcome(res, o, "snap refresh", src.ID) } func (s *snap) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := s.exec(ctx, o, "remove", src.ID) return simpleOutcome(res, o, "snap remove", src.ID) } func (s *snap) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := run.Exec(ctx, run.Options{Name: "snap", Args: []string{"list", src.ID}, Timeout: o.Timeout, Log: o.Log}) if res.Failed() { return "", false } lines := strings.Split(strings.TrimSpace(res.Stdout), "\n") if len(lines) < 2 { return "", true } fields := strings.Fields(lines[1]) if len(fields) >= 2 { return fields[1], true } return "", true } // --------------------------------------------------------------- shared --- // noOpPhrases are the ways the Linux managers say "there was nothing to do". var noOpPhrases = []string{ "is already the newest version", "nothing to do", "no packages marked for update", "nothing to upgrade", "is already installed", "reinstalling", "no updates available", "already installed", } // simpleOutcome maps a package manager result onto an Outcome, treating // "nothing to do" as success rather than an error. func simpleOutcome(res run.Result, o Opts, what, id string) (Outcome, error) { if o.DryRun { return Outcome{Message: "dry run"}, nil } noop := false for _, phrase := range noOpPhrases { if res.Contains(phrase) { noop = true break } } switch { case !res.Failed(): if noop { return Outcome{NoOp: true, Message: "already up to date"}, nil } return Outcome{}, nil case noop: return Outcome{NoOp: true, Message: "already up to date"}, nil default: return Outcome{}, fmt.Errorf("%s %s: %s", what, id, res.Error()) } }