Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closes #400 #415

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sztp-agent/cmd/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureagent"
"github.com/spf13/cobra"
)

// NewSystemdCommand NewEnableCommand returns the enable command
func NewSystemdCommand() *cobra.Command {
var (
path string
options string
)

cmd := &cobra.Command{
Use: "systemd",
Short: "Create a templated systemd unit file for sztp-agent",
RunE: func(c *cobra.Command, _ []string) error {
err := secureagent.CreateUnitFile(options, path)
return err
},
}

flags := cmd.Flags()
flags.StringVarP(&path, "path", "p", "/etc/systemd/system",
"Path for unit file to be created")
flags.StringVarP(&options, "options", "o", "",
"sztp-agent args/flags to add into the unit file")
return cmd
}
35 changes: 35 additions & 0 deletions sztp-agent/cmd/systemd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"github.com/spf13/cobra"
"reflect"
"testing"
)

func TestSystemdCommand(t *testing.T) {
tests := []struct {
name string
want *cobra.Command
}{
{
name: "TestSystemdCommand",
want: &cobra.Command{
Use: "systemd",
Short: "Create a template systemd unit file",
RunE: func(c *cobra.Command, _ []string) error {
err := c.Help()
cobra.CheckErr(err)
return nil
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewSystemdCommand(""); !reflect.DeepEqual(got.Commands(), tt.want.Commands()) {
t.Errorf("NewStatusCommand() = %v, want %v", got, tt.want)
}
})
}
}
36 changes: 36 additions & 0 deletions sztp-agent/pkg/secureagent/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package secureagent

import (
"fmt"
"log"
"os"
"strings"
)

const unitFile = "sztp-agent.service"

func CreateUnitFile(execOptions string, path string) error {
path = strings.TrimSuffix(path, "/") + "/" + unitFile // ensures no double trailing slashes
contents := fmt.Sprintf(`[Unit]
Description=SZTP Agent
After=network.target

[Service]
ExecStart=opi-sztp-agent %[1]s
ExecReload=opi-sztp-agent %[1]s
Type=notify
Restart=always

[Install]
WantedBy=default.target
RequiredBy=network.target
`, execOptions)

err := os.WriteFile(path, []byte(contents), 0644)
if err != nil {
return fmt.Errorf("creating unit file %s: %v", path, err)
}

log.Printf("Unit file %s created successfully. Ensure sztp-agent binary is installed on your system", path+"")
return nil
}
44 changes: 44 additions & 0 deletions sztp-agent/pkg/secureagent/systemd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package secureagent

import (
"bytes"
"fmt"
"os"
"testing"
)

func TestCreateUnitFile(t *testing.T) {
const testOptions = "-bogus -options"
const unitFilePath = "/tmp"

contents := fmt.Sprintf(`[Unit]
Description=SZTP Agent
After=network.target

[Service]
ExecStart=opi-sztp-agent %[1]s
ExecReload=opi-sztp-agent %[1]s
Type=notify
Restart=always

[Install]
WantedBy=default.target
RequiredBy=network.target
`, testOptions)

CreateUnitFile(testOptions, unitFilePath)

b, err := os.ReadFile(unitFilePath + "/" + unitFile)
if err != nil {
t.Errorf("Error reading unit file %s: %v", unitFilePath, err)
}

if !bytes.Equal(b, []byte(contents)) {
t.Errorf("Bytes do not match for contents and written unit file %s", unitFilePath)
}

err = os.Remove(unitFilePath + "/" + unitFile)
if err != nil {
t.Errorf("Error deleting unit file %s: %v", unitFilePath, err)
}
}