Skip to content

Commit

Permalink
Merge pull request #54 from nlewo/detect-reboot
Browse files Browse the repository at this point in the history
Detect if the host needs to be rebooted
  • Loading branch information
nlewo authored Aug 20, 2024
2 parents aab5ac5 + 5bb76fd commit 84ef9af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ var statusCmd = &cobra.Command{
logrus.Fatal(err)
}
fmt.Printf("Status of the machine %s\n", status.Hostname)
needToReboot := "no"
if status.NeedToReboot {
needToReboot = "yes"
}
fmt.Printf(" Need to reboot: %s\n", needToReboot)
for _, r := range status.RepositoryStatus.Remotes {
fmt.Printf(" Remote %s fetched %s\n",
r.Url, humanize.Time(r.FetchedAt),
Expand Down
8 changes: 8 additions & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type State struct {
IsRunning bool `json:"is_running"`
Deployment deployment.Deployment `json:"deployment"`
Hostname string `json:"hostname"`
NeedToReboot bool `json:"need_to_reboot"`
}

type Manager struct {
Expand All @@ -44,6 +45,7 @@ type Manager struct {
// for a first iteration: this needs to be removed
isRunning bool
needToBeRestarted bool
needToReboot bool
cominServiceRestartFunc func() error

evalFunc generation.EvalFunc
Expand Down Expand Up @@ -107,6 +109,7 @@ func (m Manager) toState() State {
IsRunning: m.isRunning,
Deployment: m.deployment,
Hostname: m.hostname,
NeedToReboot: m.needToReboot,
}
}

Expand Down Expand Up @@ -153,6 +156,8 @@ func (m Manager) onDeployment(ctx context.Context, deploymentResult deployment.D
if getsEvicted && evicted.ProfilePath != "" {
profile.RemoveProfilePath(evicted.ProfilePath)
}
m.needToReboot = utils.NeedToReboot()
m.prometheus.SetHostInfo(m.needToReboot)
return m
}

Expand Down Expand Up @@ -211,6 +216,9 @@ func (m Manager) Run() {
logrus.Infof(" machineId = %s", m.machineId)
logrus.Infof(" repositoryPath = %s", m.repositoryPath)

m.needToReboot = utils.NeedToReboot()
m.prometheus.SetHostInfo(m.needToReboot)

for {
select {
case <-m.stateRequestCh:
Expand Down
20 changes: 19 additions & 1 deletion internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ type Prometheus struct {
buildInfo *prometheus.GaugeVec
deploymentInfo *prometheus.GaugeVec
fetchCounter *prometheus.CounterVec
hostInfo *prometheus.GaugeVec
}

func New() Prometheus {
promReg := prometheus.NewRegistry()
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "comin_build_info",
Help: "Build info for comin.",
}, []string{"version"})
}, []string{"version"})
deploymentInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "comin_deployment_info",
Help: "Info of the last deployment.",
Expand All @@ -28,14 +29,20 @@ func New() Prometheus {
Name: "comin_fetch_count",
Help: "Number of fetches per status",
}, []string{"remote_name", "status"})
hostInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "comin_host_info",
Help: "Info of the host.",
}, []string{"need_to_reboot"})
promReg.MustRegister(buildInfo)
promReg.MustRegister(deploymentInfo)
promReg.MustRegister(fetchCounter)
promReg.MustRegister(hostInfo)
return Prometheus{
promRegistry: promReg,
buildInfo: buildInfo,
deploymentInfo: deploymentInfo,
fetchCounter: fetchCounter,
hostInfo: hostInfo,
}
}

Expand All @@ -60,3 +67,14 @@ func (m Prometheus) SetDeploymentInfo(commitId, status string) {
m.deploymentInfo.Reset()
m.deploymentInfo.With(prometheus.Labels{"commit_id": commitId, "status": status}).Set(1)
}

func (m Prometheus) SetHostInfo(needToReboot bool) {
m.hostInfo.Reset()
var value string
if needToReboot {
value = "1"
} else {
value = "0"
}
m.hostInfo.With(prometheus.Labels{"need_to_reboot": value}).Set(1)
}
28 changes: 28 additions & 0 deletions internal/utils/reboot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"os"

"github.com/sirupsen/logrus"
)

// NeedToReboot return true when the current deployed kernel is not
// the booted kernel. Note we should implement something smarter such
// as described in
// https://discourse.nixos.org/t/nixos-needsreboot-determine-if-you-need-to-reboot-your-nixos-machine/40790
func NeedToReboot() (reboot bool) {
current, err := os.Readlink("/run/current-system/kernel")
if err != nil {
logrus.Errorf("Failed to read the symlink /run/current-system/kernel: %s", err)
return
}
booted, err := os.Readlink("/run/booted-system/kernel")
if err != nil {
logrus.Errorf("Failed to read the symlink /run/booted-system/kernel: %s", err)
return
}
if current != booted {
reboot = true
}
return
}

0 comments on commit 84ef9af

Please sign in to comment.