Skip to content

Commit

Permalink
[setup] Write binary checksum to service unit
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 8, 2024
1 parent 5bac16c commit 8e48de1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var configDir = "/etc"
// unitComment is comment for service unit
var unitComment = "# Unit generated by ek.go/setup"

// checksum is current binary checksum
var checksum string

// ////////////////////////////////////////////////////////////////////////////////// //

// Install installs or reinstalls application on the system
Expand Down Expand Up @@ -142,14 +145,25 @@ func (b *binaryInfo) ServiceUnitPath() string {
// IsBinInstalled returns true if current binary already installed
func (b *binaryInfo) IsBinInstalled() bool {
return fsutil.IsExist(b.BinInstallPath()) &&
hash.FileHash(b.File) == hash.FileHash(b.BinInstallPath())
b.Checksum() == hash.FileHash(b.BinInstallPath())
}

// IsServiceInstalled returns true if service unit already installed
func (b *binaryInfo) IsServiceInstalled() bool {
return fsutil.IsExist(b.ServiceUnitPath())
}

// Checksum returns checksum of current binary
func (b *binaryInfo) Checksum() string {
if checksum != "" {
return checksum
}

checksum = hash.FileHash(b.File)

return checksum
}

// ////////////////////////////////////////////////////////////////////////////////// //

// getBinary returns basic info about current binary
Expand Down Expand Up @@ -367,7 +381,12 @@ func createServiceFile(app App, bin binaryInfo) error {
func generateServiceUnit(app App, bin binaryInfo) []byte {
var buf bytes.Buffer

buf.WriteString(unitComment + "\n\n")
buf.WriteString(fmt.Sprintf(
"%s (%s)\n\n",
unitComment,
strutil.Head(bin.Checksum(), 7),
))

buf.WriteString("[Unit]\n")
buf.WriteString(fmt.Sprintf("Description=%s\n", strutil.Q(app.Name, bin.Name)))

Expand Down

0 comments on commit 8e48de1

Please sign in to comment.