Commit 7fab1b43 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Enhance error handling

parent 3a02f970
......@@ -98,17 +98,12 @@ func restCall(method string, path string, body io.Reader) (responseBytes []byte,
return
}
func fetchDepState(id int) (state api.DepState) {
func fetchDepState(id int) (state api.DepState, err error) {
response, err := restCall("GET", fmt.Sprintf("state/%d", id), nil)
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(
response,
&state,
); err != nil {
panic(err)
return
}
err = json.Unmarshal(response, &state)
return
}
......@@ -153,16 +148,25 @@ func findService(sid int) (service api.Service, err error) {
}
// pollDepState polls the api until the state reaches "deployed", "failed", or "questionnare"
func pollDepState(state api.DepState) api.DepState {
func pollDepState(initialState api.DepState) (state api.DepState, err error) {
interval := time.Second
for pollCount := 1; state.State != "deployed" && state.State != "questionnaire" && state.State != "failed"; pollCount++ {
state = initialState
continuePoll := func(s api.DepState) bool {
return s.State != "deployed" && s.State != "questionnaire" && s.State != "failed"
}
for pollCount := 1; continuePoll(state); pollCount++ {
// log old
log.Println("Deployment has state:", state.State)
// poll new
time.Sleep(interval)
state = fetchDepState(state.ID)
state, err = fetchDepState(state.ID)
if err != nil {
log.Printf("Error: Fetching state: %s", err)
}
if pollCount%5 == 0 {
interval *= 2
......@@ -170,7 +174,7 @@ func pollDepState(state api.DepState) api.DepState {
}
}
log.Println("Deployment has state:", state.State)
return state
return
}
func sshConnect(key api.SSHKey, creds api.Credentials) {
......@@ -336,7 +340,10 @@ func main() {
// wait for the deployment to be executed
var state = deployment.States[0]
state = pollDepState(state)
state, err = pollDepState(state)
if err != nil {
fatal(err)
}
switch state.State {
case "deployed":
......
......@@ -17,13 +17,13 @@ import (
// selectLocalKey selects a key from the user .ssh dir
//
// This is meant for the case that the user did not provided a key argument
func selectLocalKey() (selected string) {
func selectLocalKey() (selected string, err error) {
var sshDir = filepath.Join(os.ExpandEnv("$HOME"), ".ssh")
// pick a local key
localKeys, err := ioutil.ReadDir(sshDir)
if err != nil {
log.Fatalf("Reading SSH dir: %s", err)
return
}
if len(localKeys) > 0 {
......@@ -43,7 +43,8 @@ func selectLocalKey() (selected string) {
// check if key the pair exists
if _, ok := keyMap[key]; ok {
if _, ok := keyMap[key+".pub"]; ok {
return filepath.Join(sshDir, key+".pub")
selected = filepath.Join(sshDir, key+".pub")
return
}
}
}
......@@ -53,14 +54,15 @@ func selectLocalKey() (selected string) {
// does the corresponding private key exist?
privateKeyName := strings.TrimSuffix(keyName, ".pub")
if _, ok := keyMap[privateKeyName]; ok {
return filepath.Join(sshDir, keyName)
selected = filepath.Join(sshDir, keyName)
return
}
}
}
}
// the ssh dir is empty!?
log.Fatalf("Please specify an SSH public key")
err = fmt.Errorf("Unable to find SSH public keys. Please specify an SSH public key")
return
}
......@@ -85,7 +87,10 @@ func readUserKey(userArg string) (key api.SSHKey, err error) {
}
func readSomePubKey() (key api.SSHKey, err error) {
selected := selectLocalKey()
selected, err := selectLocalKey()
if err != nil {
return
}
var keyBytes []byte
keyBytes, err = ioutil.ReadFile(selected)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment