Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(156)

Unified Diff: cmd/plugins/juju-restore/restore.go

Issue 70770043: all: use errgo instead of fmt.Errorf
Patch Set: all: use errgo instead of fmt.Errorf Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cmd/plugins/juju-metadata/validatetoolsmetadata.go ('k') | cmd/supercommand.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cmd/plugins/juju-restore/restore.go
=== modified file 'cmd/plugins/juju-restore/restore.go'
--- cmd/plugins/juju-restore/restore.go 2014-02-20 08:23:40 +0000
+++ cmd/plugins/juju-restore/restore.go 2014-03-03 11:56:45 +0000
@@ -15,6 +15,7 @@
"path"
"text/template"
+ "github.com/juju/errgo/errors"
"github.com/loggo/loggo"
"launchpad.net/gnuflag"
"launchpad.net/goyaml"
@@ -87,7 +88,7 @@
return cmd.CheckEmpty(args)
}
if len(args) == 0 {
- return fmt.Errorf("no backup file specified")
+ return errors.Newf("no backup file specified")
}
c.backupFile = args[0]
return cmd.CheckEmpty(args[1:])
@@ -142,7 +143,7 @@
}
creds, err := extractCreds(c.backupFile)
if err != nil {
- return fmt.Errorf("cannot extract credentials from backup file: %v", err)
+ return errors.Notef(err, "cannot extract credentials from backup file")
}
progress("extracted credentials from backup file")
store, err := configstore.Default()
@@ -155,31 +156,31 @@
}
env, err := rebootstrap(cfg, ctx, c.Constraints)
if err != nil {
- return fmt.Errorf("cannot re-bootstrap environment: %v", err)
+ return errors.Notef(err, "cannot re-bootstrap environment")
}
progress("connecting to newly bootstrapped instance")
conn, err := juju.NewAPIConn(env, api.DefaultDialOpts())
if err != nil {
- return fmt.Errorf("cannot connect to bootstrap instance: %v", err)
+ return errors.Notef(err, "cannot connect to bootstrap instance")
}
progress("restoring bootstrap machine")
newInstId, machine0Addr, err := restoreBootstrapMachine(conn, c.backupFile, creds)
if err != nil {
- return fmt.Errorf("cannot restore bootstrap machine: %v", err)
+ return errors.Notef(err, "cannot restore bootstrap machine")
}
progress("restored bootstrap machine")
// Update the environ state to point to the new instance.
if err := bootstrap.SaveState(env.Storage(), &bootstrap.BootstrapState{
StateInstances: []instance.Id{newInstId},
}); err != nil {
- return fmt.Errorf("cannot update environ bootstrap state storage: %v", err)
+ return errors.Notef(err, "cannot update environ bootstrap state storage")
}
// Construct our own state info rather than using juju.NewConn so
// that we can avoid storage eventual-consistency issues
// (and it's faster too).
caCert, ok := cfg.CACert()
if !ok {
- return fmt.Errorf("configuration has no CA certificate")
+ return errors.Newf("configuration has no CA certificate")
}
progress("opening state")
st, err := state.Open(&state.Info{
@@ -189,11 +190,11 @@
Password: creds.Password,
}, state.DefaultDialOpts(), environs.NewStatePolicy())
if err != nil {
- return fmt.Errorf("cannot open state: %v", err)
+ return errors.Notef(err, "cannot open state")
}
progress("updating all machines")
if err := updateAllMachines(st, machine0Addr); err != nil {
- return fmt.Errorf("cannot update machines: %v", err)
+ return errors.Notef(err, "cannot update machines")
}
return nil
}
@@ -210,7 +211,7 @@
"provisioner-safe-mode": true,
})
if err != nil {
- return nil, fmt.Errorf("cannot enable provisioner-safe-mode: %v", err)
+ return nil, errors.Notef(err, "cannot enable provisioner-safe-mode")
}
env, err := environs.New(cfg)
if err != nil {
@@ -218,24 +219,24 @@
}
state, err := bootstrap.LoadState(env.Storage())
if err != nil {
- return nil, fmt.Errorf("cannot retrieve environment storage; perhaps the environment was not bootstrapped: %v", err)
+ return nil, errors.Notef(err, "cannot retrieve environment storage; perhaps the environment was not bootstrapped")
}
if len(state.StateInstances) == 0 {
- return nil, fmt.Errorf("no instances found on bootstrap state; perhaps the environment was not bootstrapped")
+ return nil, errors.Newf("no instances found on bootstrap state; perhaps the environment was not bootstrapped")
}
if len(state.StateInstances) > 1 {
- return nil, fmt.Errorf("restore does not support HA juju configurations yet")
+ return nil, errors.Newf("restore does not support HA juju configurations yet")
}
inst, err := env.Instances(state.StateInstances)
if err == nil {
- return nil, fmt.Errorf("old bootstrap instance %q still seems to exist; will not replace", inst)
+ return nil, errors.Newf("old bootstrap instance %q still seems to exist; will not replace", inst)
}
if err != environs.ErrNoInstances {
- return nil, fmt.Errorf("cannot detect whether old instance is still running: %v", err)
+ return nil, errors.Notef(err, "cannot detect whether old instance is still running")
}
// Remove the storage so that we can bootstrap without the provider complaining.
if err := env.Storage().Remove(bootstrap.StateFile); err != nil {
- return nil, fmt.Errorf("cannot remove %q from storage: %v", bootstrap.StateFile, err)
+ return nil, errors.Notef(err, "cannot remove %q from storage", bootstrap.StateFile)
}
// TODO If we fail beyond here, then we won't have a state file and
@@ -245,7 +246,7 @@
// it go ahead anyway without the check.
if err := bootstrap.Bootstrap(ctx, env, cons); err != nil {
- return nil, fmt.Errorf("cannot bootstrap new instance: %v", err)
+ return nil, errors.Notef(err, "cannot bootstrap new instance")
}
return env, nil
}
@@ -253,25 +254,25 @@
func restoreBootstrapMachine(conn *juju.APIConn, backupFile string, creds credentials) (newInstId instance.Id, addr string, err error) {
addr, err = conn.State.Client().PublicAddress("0")
if err != nil {
- return "", "", fmt.Errorf("cannot get public address of bootstrap machine: %v", err)
+ return "", "", errors.Notef(err, "cannot get public address of bootstrap machine")
}
status, err := conn.State.Client().Status(nil)
if err != nil {
- return "", "", fmt.Errorf("cannot get environment status: %v", err)
+ return "", "", errors.Notef(err, "cannot get environment status")
}
info, ok := status.Machines["0"]
if !ok {
- return "", "", fmt.Errorf("cannot find bootstrap machine in status")
+ return "", "", errors.Newf("cannot find bootstrap machine in status")
}
newInstId = instance.Id(info.InstanceId)
progress("copying backup file to bootstrap host")
if err := scp(backupFile, addr, "~/juju-backup.tgz"); err != nil {
- return "", "", fmt.Errorf("cannot copy backup file to bootstrap instance: %v", err)
+ return "", "", errors.Notef(err, "cannot copy backup file to bootstrap instance")
}
progress("updating bootstrap machine")
if err := ssh(addr, updateBootstrapMachineScript(newInstId, creds)); err != nil {
- return "", "", fmt.Errorf("update script failed: %v", err)
+ return "", "", errors.Notef(err, "update script failed")
}
return newInstId, addr, nil
}
@@ -289,7 +290,7 @@
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
- return credentials{}, fmt.Errorf("cannot unzip %q: %v", backupFile, err)
+ return credentials{}, errors.Notef(err, "cannot unzip %q", backupFile)
}
defer gzr.Close()
outerTar, err := findFileInTar(gzr, "juju-backup/root.tar")
@@ -302,19 +303,19 @@
}
data, err := ioutil.ReadAll(agentConf)
if err != nil {
- return credentials{}, fmt.Errorf("failed to read agent config file: %v", err)
+ return credentials{}, errors.Notef(err, "failed to read agent config file")
}
var conf interface{}
if err := goyaml.Unmarshal(data, &conf); err != nil {
- return credentials{}, fmt.Errorf("cannot unmarshal agent config file: %v", err)
+ return credentials{}, errors.Notef(err, "cannot unmarshal agent config file")
}
m, ok := conf.(map[interface{}]interface{})
if !ok {
- return credentials{}, fmt.Errorf("config file unmarshalled to %T not %T", conf, m)
+ return credentials{}, errors.Newf("config file unmarshalled to %T not %T", conf, m)
}
password, ok := m["statepassword"].(string)
if !ok || password == "" {
- return credentials{}, fmt.Errorf("agent password not found in configuration")
+ return credentials{}, errors.Newf("agent password not found in configuration")
}
return credentials{
Tag: "machine-0",
@@ -327,7 +328,7 @@
for {
hdr, err := tarr.Next()
if err != nil {
- return nil, fmt.Errorf("%q not found: %v", name, err)
+ return nil, errors.Notef(err, "%q not found", name)
}
if path.Clean(hdr.Name) == name {
return tarr, nil
@@ -391,7 +392,7 @@
err = nil
for ; pendingMachineCount > 0; pendingMachineCount-- {
if updateErr := <-done; updateErr != nil && err == nil {
- err = fmt.Errorf("machine update failed")
+ err = errors.Newf("machine update failed")
}
}
return err
@@ -402,7 +403,7 @@
progress("updating machine: %v\n", m)
addr := instance.SelectPublicAddress(m.Addresses())
if addr == "" {
- return fmt.Errorf("no appropriate public address found")
+ return errors.Newf("no appropriate public address found")
}
return ssh(addr, sshArg)
}
@@ -420,7 +421,7 @@
logger.Debugf("ssh command: %s %q", cmd.Path, cmd.Args)
data, err := cmd.CombinedOutput()
if err != nil {
- return fmt.Errorf("ssh command failed: %v (%q)", err, data)
+ return errors.Newf("ssh command failed: %v (%q)", err, data)
}
progress("ssh command succeeded: %q", data)
return nil
@@ -441,7 +442,7 @@
return nil
}
if _, ok := err.(*exec.ExitError); ok {
- return fmt.Errorf("scp failed: %s", out)
+ return errors.Newf("scp failed: %s", out)
}
return err
}
@@ -457,7 +458,7 @@
var buf bytes.Buffer
err := tmpl.Execute(&buf, data)
if err != nil {
- panic(fmt.Errorf("template error: %v", err))
+ panic(errors.Notef(err, "template error"))
}
return buf.String()
}
« no previous file with comments | « cmd/plugins/juju-metadata/validatetoolsmetadata.go ('k') | cmd/supercommand.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b