mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-09-03 15:47:27 -04:00
Ensure old device ID remains active on the client if new device ID registration fails
This commit is contained in:
+1
-1
@@ -74,7 +74,7 @@ func LibmuttonInit(inputCB func(prompt string) string, clientSpecificIniData [][
|
|||||||
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
return errors.New("unable to initialize libmutton directories: " + err.Error())
|
||||||
}
|
}
|
||||||
// write config file
|
// write config file
|
||||||
if len(clientSpecificIniData) > 0 { // TODO test passing empty clientSpecificIniData
|
if len(clientSpecificIniData) > 0 {
|
||||||
err = cfg.WriteConfig(append(clientSpecificIniData, [][3]string{{"LIBMUTTON", "offlineMode", "true"}}...), nil, false)
|
err = cfg.WriteConfig(append(clientSpecificIniData, [][3]string{{"LIBMUTTON", "offlineMode", "true"}}...), nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to write config file: " + err.Error())
|
return errors.New("unable to write config file: " + err.Error())
|
||||||
|
|||||||
+21
-8
@@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rwinkhart/go-boilerplate/back"
|
||||||
"github.com/rwinkhart/libmutton/global"
|
"github.com/rwinkhart/libmutton/global"
|
||||||
"github.com/rwinkhart/libmutton/syncclient"
|
"github.com/rwinkhart/libmutton/syncclient"
|
||||||
)
|
)
|
||||||
@@ -23,32 +24,44 @@ func DeviceIDGen(oldDeviceID string) (string, string, error) {
|
|||||||
newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
newDeviceID := deviceIDPrefix + "-" + deviceIDSuffix
|
||||||
|
|
||||||
// create new device ID file (locally)
|
// create new device ID file (locally)
|
||||||
fileToClose, err := os.OpenFile(global.ConfigDir+global.PathSeparator+"devices"+global.PathSeparator+newDeviceID, os.O_CREATE|os.O_WRONLY, 0600)
|
newDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + newDeviceID
|
||||||
|
oldDeviceIDPath := global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID
|
||||||
|
f, err := os.OpenFile(newDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.New("unable to create local device ID file: " + err.Error())
|
return "", "", errors.New("unable to create local device ID file: " + err.Error())
|
||||||
}
|
}
|
||||||
_ = fileToClose.Close() // error ignored; if the file could be created, it can probably be closed
|
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||||
|
|
||||||
// remove old device ID file (locally; may not exist)
|
cleanupOnFail := func() {
|
||||||
err = os.RemoveAll(global.ConfigDir + global.PathSeparator + "devices" + global.PathSeparator + oldDeviceID)
|
// remove new device ID file
|
||||||
if err != nil {
|
os.RemoveAll(newDeviceIDPath)
|
||||||
return "", "", errors.New("unable to remove old device ID file (locally): " + err.Error())
|
// restore old device ID file (if it has already been removed due to DirInit)
|
||||||
|
if isAccessible, _ := back.TargetIsFile(oldDeviceIDPath, true); !isAccessible {
|
||||||
|
f, _ := os.OpenFile(oldDeviceIDPath, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
|
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// register new device ID with server and fetch remote EntryRoot and OS type
|
// register new device ID with server and fetch remote EntryRoot and OS type
|
||||||
// also removes the old device ID file (remotely)
|
// also removes the old device ID file (remotely)
|
||||||
|
// if registration fails, remove the new device ID file locally and return before removing the old one
|
||||||
sshClient, _, _, _, err := syncclient.GetSSHClient()
|
sshClient, _, _, _, err := syncclient.GetSSHClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cleanupOnFail()
|
||||||
return "", "", errors.New("unable to connect to SSH client: " + err.Error())
|
return "", "", errors.New("unable to connect to SSH client: " + err.Error())
|
||||||
}
|
}
|
||||||
output, err := syncclient.GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID)
|
output, err := syncclient.GetSSHOutput(sshClient, "libmuttonserver register", newDeviceID+"\n"+oldDeviceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cleanupOnFail()
|
||||||
return "", "", errors.New("unable to register device ID with server: " + err.Error())
|
return "", "", errors.New("unable to register device ID with server: " + err.Error())
|
||||||
}
|
}
|
||||||
sshEntryRootSSHIsWindows := strings.Split(output, global.FSSpace)
|
sshEntryRootSSHIsWindows := strings.Split(output, global.FSSpace)
|
||||||
err = sshClient.Close()
|
_ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it
|
||||||
|
|
||||||
|
// remove old device ID file (locally; may not exist)
|
||||||
|
err = os.RemoveAll(oldDeviceIDPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.New("unable to close SSH client: " + err.Error())
|
return "", "", errors.New("unable to remove old device ID file (locally): " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1], nil
|
return sshEntryRootSSHIsWindows[0], sshEntryRootSSHIsWindows[1], nil
|
||||||
|
|||||||
Reference in New Issue
Block a user