Skip to content

Commit

Permalink
interface HostIDStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
motemen committed Nov 26, 2015
1 parent 2dc5489 commit 2174bf1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
5 changes: 4 additions & 1 deletion command/command.go
Expand Up @@ -78,7 +78,10 @@ func prepareHost(conf *config.Config, api *mackerel.API) (*mackerel.Host, error)
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to find this host on mackerel (You may want to delete file \"%s\" to register this host to an another organization): %s", conf.HostIDFile(), lastErr.Error())
if fsStorage, ok := conf.HostIDStorage.(*config.FileSystemHostIDStorage); ok {
return nil, fmt.Errorf("Failed to find this host on mackerel (You may want to delete file \"%s\" to register this host to an another organization): %s", fsStorage.HostIDFile(), lastErr.Error())
}
return nil, fmt.Errorf("Failed to find this host on mackerel: %s", lastErr.Error())
}
}

Expand Down
65 changes: 54 additions & 11 deletions config/config.go
Expand Up @@ -53,6 +53,9 @@ type Config struct {

DeprecatedSensu map[string]PluginConfigs `toml:"sensu"` // DEPRECATED this is for backward compatibility
Include string

// Cannot exist in configuration files
HostIDStorage HostIDStorage
}

// PluginConfigs represents a set of [plugin.<kind>.<name>] sections in the configuration file
Expand Down Expand Up @@ -213,29 +216,68 @@ func includeConfigFile(config *Config, include string) error {
return nil
}

func (conf *Config) hostIDStorage() HostIDStorage {
if conf.HostIDStorage == nil {
conf.HostIDStorage = &FileSystemHostIDStorage{Root: conf.Root}
}
return conf.HostIDStorage
}

// LoadHostID loads the previously saved host id.
func (conf *Config) LoadHostID() (string, error) {
return conf.hostIDStorage().LoadHostID()
}

// SaveHostID saves the host id, which may be restored by LoadHostID.
func (conf *Config) SaveHostID(id string) error {
return conf.hostIDStorage().SaveHostID(id)
}

// DeleteSavedHostID deletes the host id saved by SaveHostID.
func (conf *Config) DeleteSavedHostID() error {
return conf.hostIDStorage().DeleteSavedHostID()
}

// HostIDStorage is an interface which maintains persistency
// of the "Host ID" for the current host where the agent is running on.
// The ID is always generated and given by Mackerel (mackerel.io).
type HostIDStorage interface {
LoadHostID() (string, error)
SaveHostID(id string) error
DeleteSavedHostID() error
}

// FileSystemHostIDStorage is the default HostIDStorage
// which saves/loads the host id using an id file on the local filesystem.
// The file will be located at /var/lib/mackerel-agent/id by default on linux.
type FileSystemHostIDStorage struct {
Root string
}

const idFileName = "id"

func (c Config) HostIDFile() string {
return filepath.Join(c.Root, idFileName)
// HostIDFile is the location of the host id file.
func (s FileSystemHostIDStorage) HostIDFile() string {
return filepath.Join(s.Root, idFileName)
}

// LoadHostID loads the HostID of the host where the agent is running on.
// The HostID is chosen and given by Mackerel on host registration.
func (c Config) LoadHostID() (string, error) {
content, err := ioutil.ReadFile(c.HostIDFile())
// LoadHostID loads the current host ID from the mackerel-agent's id file.
func (s FileSystemHostIDStorage) LoadHostID() (string, error) {
content, err := ioutil.ReadFile(s.HostIDFile())
if err != nil {
return "", err
}
return strings.TrimRight(string(content), "\r\n"), nil
}

func (c Config) SaveHostID(id string) error {
err := os.MkdirAll(c.Root, 0755)
// SaveHostID saves the host ID to the mackerel-agent's id file.
func (s FileSystemHostIDStorage) SaveHostID(id string) error {
err := os.MkdirAll(s.Root, 0755)
if err != nil {
return err
}

file, err := os.Create(c.HostIDFile())
file, err := os.Create(s.HostIDFile())
if err != nil {
return err
}
Expand All @@ -249,6 +291,7 @@ func (c Config) SaveHostID(id string) error {
return nil
}

func (c Config) DeleteSavedHostID() error {
return os.Remove(c.HostIDFile())
// DeleteSavedHostID deletes the mackerel-agent's id file.
func (s FileSystemHostIDStorage) DeleteSavedHostID() error {
return os.Remove(s.HostIDFile())
}

0 comments on commit 2174bf1

Please sign in to comment.