func (r *Remote) createNewMachineFromKite(c *KodingClient, log logging.Logger) error { var host string host, err := r.hostFromClient(c.Client) if err != nil { log.Error("Unable to extract host from *kite.Client. err:%s", err) return err } // For backwards compatibility, check if the host already has a name // in the cache. // // If the name does not exist in the host the string will be empty, and // Machines.Add() will create a new unique name. // // If the string *does* exist then we use that, remove it from the map, // and save the map to avoid dealing with this next time. name, ok := r.machineNamesCache[host] if ok { log.Info( "Using legacy name, and removing it from database. name:%s, host:%s", name, host, ) delete(r.machineNamesCache, host) // Should't bother exiting here, not a terrible error.. but not good, either. // Log it for knowledge, and move on. if err := r.saveMachinesNames(); err != nil { log.Error("Failed to save machine names. err:%s", err) } } // Name can be empty here, since Machines.Add() will handle creation // of the name. machineMeta := machine.MachineMeta{ URL: c.URL, IP: host, Hostname: c.Hostname, Username: c.Username, Name: name, MachineLabel: c.MachineLabel, Teams: c.Teams, } newMachine, err := machine.NewMachine(machineMeta, r.log, c.Client) if err != nil { return err } if err = r.machines.Add(newMachine); err != nil { log.Error("Unable to Add new machine to *machine.Machines. err:%s", err) return err } return nil }
// restoreLoadedMachineFromKite takes an old loaded machine and applies meta // changes to it, while also creating a valid machine instance with a proper // transport/etc. func (r *Remote) restoreLoadedMachineFromKite(c *KodingClient, loadedMachine *machine.Machine, log logging.Logger) (bool, error) { // metaChanged is our return value, which lets the caller know if any of the meta // for this machine changed from the given kite. If it does, the caller will // want to save the machines to the local db. var metaChanged bool host, err := r.hostFromClient(c.Client) if err != nil { log.Error("Unable to extract host from *kite.Client. err:%s", err) return false, err } // Get our old machine meta, loaded from the DB machineMeta := loadedMachine.MachineMeta if machineMeta.MachineLabel != c.MachineLabel { machineMeta.MachineLabel = c.MachineLabel metaChanged = true } if machineMeta.URL == "" && c.URL != "" { log.Warning("Machine missing MachineMeta.URL, updating it to %q", c.URL) machineMeta.URL = c.URL metaChanged = true } if machineMeta.IP == "" && host != "" { log.Warning("Machine missing MachineMeta.IP, updating it to %q", host) machineMeta.IP = host metaChanged = true } if machineMeta.Hostname != c.Hostname { machineMeta.Hostname = c.Hostname metaChanged = true } if machineMeta.Username != c.Username { machineMeta.Username = c.Username metaChanged = true } // Remove the machine, because we're going to be creating a new machine instance // below. err = r.machines.Remove(loadedMachine) if err != nil && err != machine.ErrMachineNotFound { log.Error("Unable to Remove old machine from *machine.Machines. err:%s", err) return false, err } restoredMachine, err := machine.NewMachine(machineMeta, r.log, c.Client) if err != nil { return false, err } // Add our newly created machine instance. if err = r.machines.Add(restoredMachine); err != nil { log.Error("Unable to Add new machine to *machine.Machines. err:%s", err) return false, err } return metaChanged, nil }