Skip to content

Commit 0604cea

Browse files
committed
Add deployment commands
1 parent 5bf97ca commit 0604cea

File tree

5 files changed

+171
-12
lines changed

5 files changed

+171
-12
lines changed

cmd/gghelper/deploy.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
// CreateDeployment - command execution for createdeployment subcommand
9+
func CreateDeployment(args []string) {
10+
11+
var region, profile string
12+
13+
cmdFlags := flag.NewFlagSet("ask", flag.ExitOnError)
14+
15+
cmdFlags.StringVar(&region, "r", "", "region")
16+
cmdFlags.StringVar(&profile, "p", "", "profile")
17+
cmdFlags.Parse(args)
18+
19+
ggSession := CreateSession(profile, region)
20+
21+
ggSession.LoadGroupConfig("config.json")
22+
23+
err := ggSession.CreateDeployment()
24+
if err != nil {
25+
fmt.Printf("Error: %v\n", err)
26+
}
27+
}
28+
29+
// ListDeployment - command execution for listdeployment subcommand
30+
func ListDeployment(args []string) {
31+
32+
var region, profile string
33+
34+
cmdFlags := flag.NewFlagSet("ask", flag.ExitOnError)
35+
36+
cmdFlags.StringVar(&region, "r", "", "region")
37+
cmdFlags.StringVar(&profile, "p", "", "profile")
38+
cmdFlags.Parse(args)
39+
40+
ggSession := CreateSession(profile, region)
41+
42+
ggSession.LoadGroupConfig("config.json")
43+
44+
err := ggSession.ListDeployment()
45+
if err != nil {
46+
fmt.Printf("Error: %v\n", err)
47+
}
48+
}
49+
50+
// ResetDeployment - command execution for resetdeployment subcommand
51+
func ResetDeployment(args []string) {
52+
53+
var region, profile string
54+
var force bool
55+
56+
cmdFlags := flag.NewFlagSet("ask", flag.ExitOnError)
57+
58+
cmdFlags.BoolVar(&force, "f", false, "force reset")
59+
cmdFlags.StringVar(&region, "r", "", "region")
60+
cmdFlags.StringVar(&profile, "p", "", "profile")
61+
cmdFlags.Parse(args)
62+
63+
ggSession := CreateSession(profile, region)
64+
65+
ggSession.LoadGroupConfig("config.json")
66+
67+
err := ggSession.ResetDeployment(force)
68+
if err != nil {
69+
fmt.Printf("Error: %v\n", err)
70+
}
71+
}

cmd/gghelper/gghelper.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,33 @@ import (
99
"github.com/cloudtools/gghelper"
1010
)
1111

12+
func printUsage() {
13+
fmt.Println("usage: gghelper <command> [<args>]")
14+
fmt.Println("Commands: ")
15+
fmt.Println(" createcore - create a new Greengrass core")
16+
fmt.Println(" createdeployment - create a new Greengrass core")
17+
fmt.Println(" creategroup - create a new Greengrass group")
18+
fmt.Println(" createsub - create a new Greengrass subscription")
19+
fmt.Println(" deleteregion - delete all greengrass/iot resources in a region")
20+
fmt.Println(" lambda - submit a lambda function")
21+
fmt.Println(" listdeployment - list greengrass deployment")
22+
fmt.Println(" listgroup - list greengrass group")
23+
fmt.Println(" listlambda - list greengrass view of lambda functions")
24+
fmt.Println(" listsub - list greengrass subscriptions")
25+
fmt.Println(" resetdeployment - reset greengrass deployments")
26+
}
27+
1228
func main() {
1329
if len(os.Args) == 1 {
14-
fmt.Println("usage: gghelper <command> [<args>]")
15-
fmt.Println("Commands: ")
16-
fmt.Println(" createcore create a new Greengrass core")
17-
fmt.Println(" creategroup create a new Greengrass group")
18-
fmt.Println(" createsub create a new Greengrass subscription")
19-
fmt.Println(" deleteregion delete all greengrass/iot resources in a region")
20-
fmt.Println(" lambda submit a lambda function")
21-
fmt.Println(" listgroup list greengrass group")
22-
fmt.Println(" listlambda list greengrass view of lambda functions")
23-
fmt.Println(" listsub list greengrass subscriptions")
30+
printUsage()
2431
return
2532
}
2633

2734
switch os.Args[1] {
2835
case "createcore":
2936
CreateCore(os.Args[2:])
37+
case "createdeployment", "createdeploy", "deploy":
38+
CreateDeployment(os.Args[2:])
3039
case "creategroup":
3140
CreateGroup(os.Args[2:])
3241
case "createsub":
@@ -35,14 +44,19 @@ func main() {
3544
DeleteRegion(os.Args[2:])
3645
case "lambda":
3746
Lambda(os.Args[2:])
47+
case "listdeployment", "listdeploy":
48+
ListDeployment(os.Args[2:])
3849
case "listgroup":
3950
ListGroup(os.Args[2:])
4051
case "listlambda":
4152
ListLambda(os.Args[2:])
4253
case "listsub":
4354
ListSub(os.Args[2:])
55+
case "resetdeployment", "resetdeploy":
56+
ResetDeployment(os.Args[2:])
4457
default:
4558
fmt.Printf("%q is not valid command.\n", os.Args[1])
59+
printUsage()
4660
os.Exit(2)
4761
}
4862
}

deploy.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package gghelper
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/service/greengrass"
7+
)
8+
9+
// CreateDeployment - Create a deployment
10+
func (ggSession *GreengrassSession) CreateDeployment() error {
11+
deployments, err := ggSession.greengrass.ListDeployments(&greengrass.ListDeploymentsInput{
12+
GroupId: &ggSession.config.Group.ID,
13+
})
14+
if err != nil {
15+
return err
16+
}
17+
fmt.Printf("deployments: %v\n", deployments)
18+
19+
newDeployment := "NewDeployment"
20+
create, err := ggSession.greengrass.CreateDeployment(&greengrass.CreateDeploymentInput{
21+
DeploymentType: &newDeployment,
22+
GroupId: &ggSession.config.Group.ID,
23+
GroupVersionId: &ggSession.config.Group.Version,
24+
})
25+
if err != nil {
26+
return err
27+
}
28+
fmt.Printf("%v\n", create)
29+
30+
return nil
31+
}
32+
33+
// ListDeployment - lists the current deployment status
34+
func (ggSession *GreengrassSession) ListDeployment() error {
35+
deployments, err := ggSession.greengrass.ListDeployments(&greengrass.ListDeploymentsInput{
36+
GroupId: &ggSession.config.Group.ID,
37+
})
38+
if err != nil {
39+
return err
40+
}
41+
fmt.Printf("deployments: %v\n", deployments)
42+
43+
for _, d := range deployments.Deployments {
44+
deployment, err := ggSession.greengrass.GetDeploymentStatus(&greengrass.GetDeploymentStatusInput{
45+
DeploymentId: d.DeploymentId,
46+
GroupId: &ggSession.config.Group.ID,
47+
})
48+
if err != nil {
49+
return err
50+
}
51+
fmt.Printf("deployment: %v\n", deployment)
52+
}
53+
54+
return nil
55+
}
56+
57+
// ResetDeployment - reset deployments status
58+
func (ggSession *GreengrassSession) ResetDeployment(force bool) error {
59+
reset, err := ggSession.greengrass.ResetDeployments(&greengrass.ResetDeploymentsInput{
60+
GroupId: &ggSession.config.Group.ID,
61+
})
62+
if err != nil {
63+
return err
64+
}
65+
fmt.Printf("deployment reset: %v\n", reset)
66+
67+
return nil
68+
}

group.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ func (ggSession *GreengrassSession) updateGroup() error {
8080
input.SubscriptionDefinitionVersionArn = &ggSession.config.SubscriptionDefinition.VersionArn
8181
}
8282

83-
_, err := ggSession.greengrass.CreateGroupVersion(input)
83+
groupVersion, err := ggSession.greengrass.CreateGroupVersion(input)
8484
if err != nil {
8585
fmt.Printf("updateGroup error: %v\n", err)
8686
return err
8787
}
88+
89+
ggSession.config.Group.Arn = *groupVersion.Arn
90+
ggSession.config.Group.Version = *groupVersion.Version
91+
8892
fmt.Printf("Updated group version\n")
8993

9094
return nil

groupconfig.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ type function struct {
1616
}
1717

1818
type group struct {
19-
ID string `json:"id"`
19+
Arn string `json:"arn"`
20+
ID string `json:"id"`
21+
Version string `json:"version"`
2022
}
2123

2224
type thing struct {

0 commit comments

Comments
 (0)