Skip to content
Snippets Groups Projects
Unverified Commit bb35cbe0 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Store last known status of machine in file in machine's storage path

parent 0ca54ffb
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,14 @@ before_script: ...@@ -7,6 +7,14 @@ before_script:
- apt-get update -yqqq - apt-get update -yqqq
- apt-get install -yqqq make - apt-get install -yqqq make
test:
stage: test
script:
- make fmt lint vet test-long
tags:
- docker
- privileged
build: build:
stage: build stage: build
script: script:
......
...@@ -38,6 +38,7 @@ func SetSSHClientCreator(creator SSHClientCreator) { ...@@ -38,6 +38,7 @@ func SetSSHClientCreator(creator SSHClientCreator) {
type Host struct { type Host struct {
ConfigVersion int ConfigVersion int
LastState state.State `json:"-"`
Driver drivers.Driver Driver drivers.Driver
DriverName string DriverName string
HostOptions *Options HostOptions *Options
...@@ -103,7 +104,12 @@ func (h *Host) runActionForState(action func() error, desiredState state.State) ...@@ -103,7 +104,12 @@ func (h *Host) runActionForState(action func() error, desiredState state.State)
return err return err
} }
return mcnutils.WaitFor(drivers.MachineInState(h.Driver, desiredState)) if err := mcnutils.WaitFor(drivers.MachineInState(h.Driver, desiredState)); err != nil {
return err
}
h.LastState = desiredState
return nil
} }
func (h *Host) WaitForDocker() error { func (h *Host) WaitForDocker() error {
...@@ -161,6 +167,8 @@ func (h *Host) Restart() error { ...@@ -161,6 +167,8 @@ func (h *Host) Restart() error {
} }
} }
h.LastState = state.Running
return h.WaitForDocker() return h.WaitForDocker()
} }
...@@ -221,3 +229,10 @@ func (h *Host) Provision() error { ...@@ -221,3 +229,10 @@ func (h *Host) Provision() error {
return provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions) return provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions)
} }
func (h *Host) UpdateLastState() {
lastState, err := h.Driver.GetState()
if err == nil {
h.LastState = lastState
}
}
...@@ -61,6 +61,7 @@ func (api *Client) NewHost(driverName string, rawDriver []byte) (*host.Host, err ...@@ -61,6 +61,7 @@ func (api *Client) NewHost(driverName string, rawDriver []byte) (*host.Host, err
return &host.Host{ return &host.Host{
ConfigVersion: version.ConfigVersion, ConfigVersion: version.ConfigVersion,
LastState: state.None,
Name: driver.GetMachineName(), Name: driver.GetMachineName(),
Driver: driver, Driver: driver,
DriverName: driver.DriverName(), DriverName: driver.DriverName(),
...@@ -135,7 +136,10 @@ func (api *Client) Create(h *host.Host) error { ...@@ -135,7 +136,10 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Creating machine...") log.Info("Creating machine...")
if err := api.performCreate(h); err != nil { if err := api.performCreate(h); err != nil {
h.UpdateLastState()
// Try to save machine when Create fails, it can store some critical information like DropletID // Try to save machine when Create fails, it can store some critical information like DropletID
// (which in some/most DO failures cases is equal 0 :P)
api.Save(h) api.Save(h)
return fmt.Errorf("Error creating machine: %s", err) return fmt.Errorf("Error creating machine: %s", err)
...@@ -143,6 +147,7 @@ func (api *Client) Create(h *host.Host) error { ...@@ -143,6 +147,7 @@ func (api *Client) Create(h *host.Host) error {
log.Debug("Reticulating splines...") log.Debug("Reticulating splines...")
h.UpdateLastState()
return nil return nil
} }
......
...@@ -72,6 +72,8 @@ func (s Filestore) Save(host *host.Host) error { ...@@ -72,6 +72,8 @@ func (s Filestore) Save(host *host.Host) error {
return err return err
} }
s.saveToFile([]byte(host.LastState.String()), filepath.Join(hostPath, "last_state"))
return s.saveToFile(data, filepath.Join(hostPath, "config.json")) return s.saveToFile(data, filepath.Join(hostPath, "config.json"))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment