2022-08-11 11:30:51 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-01-17 09:19:43 +00:00
|
|
|
"io/ioutil" // nolint
|
2022-12-21 16:05:33 +00:00
|
|
|
"os"
|
2022-08-11 11:30:51 +00:00
|
|
|
|
|
|
|
nodepair "github.com/mudler/go-nodepair"
|
|
|
|
qr "github.com/mudler/go-nodepair/qrcode"
|
|
|
|
)
|
|
|
|
|
2022-12-21 16:05:33 +00:00
|
|
|
// isDirectory determines if a file represented
|
|
|
|
// by `path` is a directory or not.
|
|
|
|
func isDirectory(path string) (bool, error) {
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileInfo.IsDir(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func isReadable(fileName string) bool {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:30:51 +00:00
|
|
|
func register(loglevel, arg, configFile, device string, reboot, poweroff bool) error {
|
|
|
|
b, _ := ioutil.ReadFile(configFile)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-12-21 16:05:33 +00:00
|
|
|
if arg != "" {
|
|
|
|
isDir, err := isDirectory(arg)
|
|
|
|
if err == nil && isDir {
|
|
|
|
return fmt.Errorf("Cannot register with a directory, please pass a file.") //nolint:revive // This is a message printed to the user.
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isReadable(arg) {
|
|
|
|
return fmt.Errorf("Cannot register with a file that is not readable.") //nolint:revive // This is a message printed to the user.
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 11:30:51 +00:00
|
|
|
// dmesg -D to suppress tty ev
|
|
|
|
fmt.Println("Sending registration payload, please wait")
|
|
|
|
|
|
|
|
config := map[string]string{
|
|
|
|
"device": device,
|
|
|
|
"cc": string(b),
|
|
|
|
}
|
|
|
|
|
|
|
|
if reboot {
|
|
|
|
config["reboot"] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if poweroff {
|
|
|
|
config["poweroff"] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
err := nodepair.Send(
|
|
|
|
ctx,
|
|
|
|
config,
|
|
|
|
nodepair.WithReader(qr.Reader),
|
|
|
|
nodepair.WithToken(arg),
|
|
|
|
nodepair.WithLogLevel(loglevel),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Payload sent, installation will start on the machine briefly")
|
|
|
|
return nil
|
|
|
|
}
|