Skip to content

Commit

Permalink
flyctl app pause/resume implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed May 27, 2020
1 parent cefe033 commit 0c299ba
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 15 deletions.
3 changes: 3 additions & 0 deletions api/casting.go
@@ -1,13 +1,16 @@
package api

// IntPointer - Returns a pointer to an int
func IntPointer(val int) *int {
return &val
}

// BoolPointer - Returns a pointer to a bool
func BoolPointer(val bool) *bool {
return &val
}

// StringPointer - Returns a pointer to a string
func StringPointer(val string) *string {
return &val
}
76 changes: 64 additions & 12 deletions api/resource_apps.go
@@ -1,6 +1,6 @@
package api

func (c *Client) GetApps() ([]App, error) {
func (client *Client) GetApps() ([]App, error) {
query := `
query {
apps(type: "container", first: 200) {
Expand All @@ -21,17 +21,17 @@ func (c *Client) GetApps() ([]App, error) {
}
`

req := c.NewRequest(query)
req := client.NewRequest(query)

data, err := c.Run(req)
data, err := client.Run(req)
if err != nil {
return nil, err
}

return data.Apps.Nodes, nil
}

func (c *Client) GetAppID(appName string) (string, error) {
func (client *Client) GetAppID(appName string) (string, error) {
query := `
query ($appName: String!) {
app(name: $appName) {
Expand All @@ -40,18 +40,18 @@ func (c *Client) GetAppID(appName string) (string, error) {
}
`

req := c.NewRequest(query)
req := client.NewRequest(query)
req.Var("appName", appName)

data, err := c.Run(req)
data, err := client.Run(req)
if err != nil {
return "", err
}

return data.App.ID, nil
}

func (c *Client) GetApp(appName string) (*App, error) {
func (client *Client) GetApp(appName string) (*App, error) {
query := `
query ($appName: String!) {
app(name: $appName) {
Expand Down Expand Up @@ -86,18 +86,18 @@ func (c *Client) GetApp(appName string) (*App, error) {
}
`

req := c.NewRequest(query)
req := client.NewRequest(query)
req.Var("appName", appName)

data, err := c.Run(req)
data, err := client.Run(req)
if err != nil {
return nil, err
}

return &data.App, nil
}

func (c *Client) CreateApp(name string, orgId string) (*App, error) {
func (client *Client) CreateApp(name string, orgId string) (*App, error) {
query := `
mutation($input: CreateAppInput!) {
createApp(input: $input) {
Expand All @@ -115,15 +115,15 @@ func (c *Client) CreateApp(name string, orgId string) (*App, error) {
}
`

req := c.NewRequest(query)
req := client.NewRequest(query)

req.Var("input", CreateAppInput{
Name: name,
Runtime: "FIRECRACKER",
OrganizationID: orgId,
})

data, err := c.Run(req)
data, err := client.Run(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -171,3 +171,55 @@ func (client *Client) MoveApp(appName string, orgID string) (*App, error) {
data, err := client.Run(req)
return &data.App, err
}

// PauseApp - Send GQL mutation to pause app
func (client *Client) PauseApp(appName string) (*App, error) {
query := `
mutation ($input: SuspendAppInput!) {
suspendApp(input: $input) {
app{
id
name
status
version
hostname
}
}
}
`

req := client.NewRequest(query)

req.Var("input", map[string]string{
"appId": appName,
})

data, err := client.Run(req)
return &data.SuspendApp.App, err
}

// ResumeApp - Send GQL mutation to pause app
func (client *Client) ResumeApp(appName string) (*App, error) {
query := `
mutation ($input: ResumeAppInput!) {
resumeApp(input: $input) {
app{
id
name
status
version
hostname
}
}
}
`

req := client.NewRequest(query)

req.Var("input", map[string]string{
"appId": appName,
})

data, err := client.Run(req)
return &data.ResumeApp.App, err
}
8 changes: 8 additions & 0 deletions api/types.go
Expand Up @@ -78,6 +78,14 @@ type Query struct {
App App
Regions []Region
}

ResumeApp struct {
App App
}

SuspendApp struct {
App App
}
}

type Definition map[string]interface{}
Expand Down
26 changes: 26 additions & 0 deletions cmd/apps.go
Expand Up @@ -74,6 +74,12 @@ func newAppListCommand() *Command {
Description: `The organization to move the app to`,
})

appsPauseStrings := docstrings.Get("apps.pause")
BuildCommand(cmd, runAppsPause, appsPauseStrings.Usage, appsPauseStrings.Short, appsPauseStrings.Long, os.Stdout, requireSession, requireAppName)

appsResumeStrings := docstrings.Get("apps.resume")
BuildCommand(cmd, runAppsResume, appsResumeStrings.Usage, appsResumeStrings.Short, appsResumeStrings.Long, os.Stdout, requireSession, requireAppName)

return cmd
}

Expand All @@ -86,6 +92,26 @@ func runAppsList(ctx *CmdContext) error {
return ctx.Render(&presenters.Apps{Apps: apps})
}

func runAppsPause(ctx *CmdContext) error {
app, err := ctx.Client.API().PauseApp(ctx.AppName)
if err != nil {
return err
}
fmt.Println(app)

fmt.Println(aurora.Bold("App"))
return ctx.RenderEx(&presenters.AppInfo{App: *app}, presenters.Options{HideHeader: true, Vertical: true})
}

func runAppsResume(ctx *CmdContext) error {
app, err := ctx.Client.API().ResumeApp(ctx.AppName)
if err != nil {
return err
}
fmt.Println(aurora.Bold("App"))
return ctx.RenderEx(&presenters.AppInfo{App: *app}, presenters.Options{HideHeader: true, Vertical: true})
}

func runDestroyApp(ctx *CmdContext) error {
appName := ctx.Args[0]

Expand Down
2 changes: 1 addition & 1 deletion cmd/logs.go
Expand Up @@ -73,7 +73,7 @@ func runLogs(ctx *CmdContext) error {
}
}

return nil
// This should not be reached
}

var maxBackoff float64 = 5000
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/superfly/flyctl/internal/client"
)

// ErrAbort - Error generated when application aborts
var ErrAbort = errors.New("abort")
var flyctlClient *client.Client

Expand Down
18 changes: 16 additions & 2 deletions helpgen/flyctlhelp.toml
Expand Up @@ -61,8 +61,22 @@ shortHelp="Move an App to another organization"
longHelp="""The APPS MOVE command will move an application to another
organization the current user belongs to.
"""


[apps.pause]
usage="pause [APPNAME]"
shortHelp="Pause an application"
longHelp="""The APPS PAUSE command will pause an application.
All allocations will be deallocated leaving the application running nowhere.
It will continue to consume networking resources (IP address). See APPS RESUME
for details on restarting it.
"""
[apps.resume]
usage="resume [APPNAME]"
shortHelp="Resume an application"
longHelp="""The APPS Resume command will restart a previously paused application.
The application will resume with its original region pool and a min count of one
meaning there will be one running instance once restarted. Use SCALE SET MIN= to raise
the number of configured instances.
"""

[auth]
usage="auth"
Expand Down

0 comments on commit 0c299ba

Please sign in to comment.