-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rename-power-commands' into 'develop'
Rename power commands Closes #253 See merge request open-source/bytemark-client!249
- Loading branch information
Showing
15 changed files
with
417 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package wait | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" | ||
"github.com/BytemarkHosting/bytemark-client/lib" | ||
"github.com/BytemarkHosting/bytemark-client/lib/brain" | ||
) | ||
|
||
// VMPowerOff waits for the named virtual machine to power off before returning | ||
// a nil error. This is done by frequently polling the brain for info about the | ||
// VM. If any calls fail, the error is returned. | ||
func VMPowerOff(c *app.Context, name lib.VirtualMachineName) (err error) { | ||
vm := brain.VirtualMachine{PowerOn: true} | ||
|
||
for vm.PowerOn { | ||
if !c.IsTest() { | ||
time.Sleep(5 * time.Second) | ||
} | ||
fmt.Fprint(c.App().Writer, ".") | ||
|
||
vm, err = c.Client().GetVirtualMachine(name) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func init() { | ||
serverFlag := cli.GenericFlag{ | ||
Name: "server", | ||
Usage: "the server to reset", | ||
Value: new(app.VirtualMachineNameFlag), | ||
} | ||
|
||
Commands = append(Commands, cli.Command{ | ||
Name: "reset", | ||
Usage: "restart a server as though the reset button had been pushed FISMO", | ||
UsageText: "reset server <server>", | ||
Description: "For cloud servers, this does not cause the qemu process to be restarted. This means that the server will remain on the same head and will not notice hardware changes.", | ||
Action: cli.ShowSubcommandHelp, | ||
Flags: []cli.Flag{ | ||
serverFlag, | ||
}, | ||
Subcommands: []cli.Command{{ | ||
Name: "server", | ||
Usage: "restart a server as though the reset button had been pushed", | ||
UsageText: "reset server <server>", | ||
Description: "For cloud servers, this does not cause the qemu process to be restarted. This means that the server will remain on the same head and will not notice hardware changes.", | ||
Flags: []cli.Flag{ | ||
serverFlag, | ||
}, | ||
Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { | ||
vmName := c.VirtualMachineName("server") | ||
c.LogErr("Attempting to reset %v...\r\n", vmName) | ||
err = c.Client().ResetVirtualMachine(vmName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.LogErr("%v reset successfully.\r\n", vmName) | ||
return | ||
}), | ||
}}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package commands_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/commands" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/testutil" | ||
"github.com/BytemarkHosting/bytemark-client/lib" | ||
"github.com/cheekybits/is" | ||
) | ||
|
||
func TestResetCommand(t *testing.T) { | ||
is := is.New(t) | ||
config, c, app := testutil.BaseTestAuthSetup(t, false, commands.Commands) | ||
vmn := lib.VirtualMachineName{VirtualMachine: "test-server", Group: "test-group", Account: "test-account"} | ||
|
||
config.When("GetVirtualMachine").Return(lib.VirtualMachineName{VirtualMachine: "smog-daniels", Group: "creation", Account: "science"}) | ||
|
||
c.When("ResetVirtualMachine", vmn).Times(1) | ||
|
||
err := app.Run(strings.Split("bytemark reset server test-server.test-group.test-account", " ")) | ||
is.Nil(err) | ||
if ok, err := c.Verify(); !ok { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/args" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/wait" | ||
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/app/with" | ||
brainRequests "github.com/BytemarkHosting/bytemark-client/lib/requests/brain" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func init() { | ||
Commands = append(Commands, cli.Command{ | ||
Name: "restart", | ||
Usage: "power off a server and start it again", | ||
UsageText: "restart server [--rescue | --appliance <appliance>] <server>", | ||
Description: "This command will power down a server and then start it back up again.", | ||
Action: cli.ShowSubcommandHelp, | ||
Subcommands: []cli.Command{{ | ||
Name: "server", | ||
Usage: "power off a server and start it again", | ||
UsageText: "restart server [--rescue | --appliance <appliance>] <server>", | ||
Flags: []cli.Flag{ | ||
cli.GenericFlag{ | ||
Name: "server", | ||
Usage: "the server to restart", | ||
Value: new(app.VirtualMachineNameFlag), | ||
}, | ||
cli.BoolFlag{ | ||
Name: "rescue", | ||
Usage: "boots the server using the rescue appliance", | ||
}, | ||
cli.StringFlag{ | ||
Name: "appliance", | ||
Usage: "the appliance to boot into when the server starts", | ||
}, | ||
}, | ||
Action: app.Action(args.Optional("server"), with.RequiredFlags("server"), with.Auth, func(c *app.Context) (err error) { | ||
vmName := c.VirtualMachineName("server") | ||
appliance := c.String("appliance") | ||
|
||
if appliance != "" && c.Bool("rescue") { | ||
return fmt.Errorf("--appliance and --rescue have both been set when only one is allowed") | ||
} | ||
|
||
if c.Bool("rescue") { | ||
appliance = "rescue" | ||
} | ||
|
||
fmt.Fprintf(c.App().Writer, "Shutting down %v...", vmName) | ||
err = c.Client().ShutdownVirtualMachine(vmName, true) | ||
if err != nil { | ||
return | ||
} | ||
err = wait.VMPowerOff(c, vmName) | ||
if err != nil { | ||
return | ||
} | ||
|
||
c.Log("Done!\n\nStarting %s back up.", vmName) | ||
if appliance != "" { | ||
err = brainRequests.StartVirtualMachineWithAppliance(c.Client(), vmName, appliance) | ||
c.Log("Server has now started. Use `bytemark console %v` or visit %v to connect.", c.String("server"), c.Config().PanelURL()) | ||
} else { | ||
err = c.Client().StartVirtualMachine(vmName) | ||
} | ||
|
||
return | ||
}), | ||
}}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.