Skip to content

Commit

Permalink
feat: explicitly start and stop canonical-facts
Browse files Browse the repository at this point in the history
Running 'rhc connect' will explicitly activate and start the
rhc-canonical-facts.timer unit. Likewise, running 'rhc disconnect' will
explicitly deactivate and stop the rhc-canonical-facts.timer unit.
  • Loading branch information
subpop committed Dec 2, 2024
1 parent 5719f7d commit 8ca6042
Showing 1 changed file with 27 additions and 40 deletions.
67 changes: 27 additions & 40 deletions activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,60 @@ import (
"context"
"fmt"

systemd "github.com/coreos/go-systemd/v22/dbus"
systemd "github.com/redhatinsights/rhc/internal/systemd"
)

// activateService tries to enable and start yggdrasil service.
// The service can be branded to rhcd on RHEL
// activateService tries to enable and start the rhc-canonical-facts.timer and
// yggdrasil.service.
func activateService() error {
ctx := context.Background()
conn, err := systemd.NewSystemConnectionContext(ctx)
conn, err := systemd.NewConnectionContext(context.Background(), systemd.ConnectionTypeSystem)
if err != nil {
return err
return fmt.Errorf("cannot connect to systemd: %v", err)
}
defer conn.Close()

unitName := ServiceName + ".service"

if _, _, err := conn.EnableUnitFilesContext(ctx, []string{unitName}, false, true); err != nil {
return err
err = conn.EnableUnit("rhc-canonical-facts.timer", true, false)
if err != nil {
return fmt.Errorf("cannot enable rhc-canonical-facts.timer: %v", err)
}

done := make(chan string)
if _, err := conn.StartUnitContext(ctx, unitName, "replace", done); err != nil {
return err
}
<-done
properties, err := conn.GetUnitPropertiesContext(ctx, unitName)
// Start the canonical-facts service immediately, so the facts get generated
// and written out before yggdrasil.service starts.
err = conn.StartUnit("rhc-canonical-facts.service", false)
if err != nil {
return err
return fmt.Errorf("cannot start rhc-canonical-facts.service: %v", err)
}
activeState := properties["ActiveState"]
if activeState.(string) != "active" {
return fmt.Errorf("error: The unit %v failed to start. Run 'systemctl status %v' for more information", unitName, unitName)

err = conn.EnableUnit("yggdrasil.service", true, false)
if err != nil {
return fmt.Errorf("cannot enable yggdrasil.service: %v", err)
}

err = conn.ReloadContext(ctx)
err = conn.Reload()
if err != nil {
return err
return fmt.Errorf("cannot reload systemd: %v", err)
}

return nil
}

// deactivateService tries to stop and disable yggdrasil service.
// The service can be branded to rhcd on RHEL
// deactivateService tries to stop and disable the rhc-canonical-facts.timer and
// yggdrasil.service.
func deactivateService() error {
// Use simple background context without anything extra
ctx := context.Background()
conn, err := systemd.NewSystemConnectionContext(ctx)
conn, err := systemd.NewConnectionContext(context.Background(), systemd.ConnectionTypeSystem)
if err != nil {
return err
return fmt.Errorf("cannot connect to systemd: %v", err)
}
defer conn.Close()

unitName := ServiceName + ".service"

done := make(chan string)
if _, err := conn.StopUnitContext(ctx, unitName, "replace", done); err != nil {
return err
}
<-done

if _, err := conn.DisableUnitFilesContext(ctx, []string{unitName}, false); err != nil {
return err
err = conn.DisableUnit("rhc-canonical-facts.timer", true, false)
if err != nil {
return fmt.Errorf("cannot disable rhc-canonical-facts.timer: %v", err)
}

err = conn.ReloadContext(ctx)
err = conn.DisableUnit("yggdrasil.service", true, false)
if err != nil {
return err
return fmt.Errorf("cannot disable yggdrasil.service: %v", err)
}

return nil
Expand Down

0 comments on commit 8ca6042

Please sign in to comment.