Skip to content

Commit 08d3460

Browse files
[POA-1415] Re-enable support for EC2 commands (#20)
In this PR, we are re-enabling support for EC2 in our agent. Changes done: * Replaced `--collection` flag with `--project` in EC2 command * Added a base`ec2` command since the `setup` command can be confusing. * So `postman-insights-agent setup ...` -> `postman-insights-agent ec2 setup ...` * Only `ec2` command also defaults to `ec2 setup` * Moved the `CheckAPIKeyAndInsightsProjectID` function out from `ecs.go` to `check.go` * Other small syntactic or typo changes
1 parent 6937e53 commit 08d3460

File tree

7 files changed

+85
-85
lines changed

7 files changed

+85
-85
lines changed

cmd/internal/cmderr/checks.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package cmderr
22

33
import (
4-
"errors"
5-
4+
"github.com/akitasoftware/akita-libs/akid"
5+
"github.com/pkg/errors"
66
"github.com/postmanlabs/postman-insights-agent/cfg"
77
"github.com/postmanlabs/postman-insights-agent/env"
88
"github.com/postmanlabs/postman-insights-agent/printer"
9+
"github.com/postmanlabs/postman-insights-agent/rest"
10+
"github.com/postmanlabs/postman-insights-agent/telemetry"
11+
"github.com/postmanlabs/postman-insights-agent/util"
912
)
1013

1114
// Checks that a user has configured their Postman API key and returned them.
@@ -19,9 +22,37 @@ func RequirePostmanAPICredentials(explanation string) (string, error) {
1922
} else {
2023
printer.Infof("Please set the POSTMAN_API_KEY environment variable, either in your shell session or prepend it to postman-insights-agent command.\n")
2124
}
22-
//lint:ignore ST1005 This is a user-facing error message
2325
return "", AkitaErr{Err: errors.New("Could not find a Postman API key to use")}
2426
}
2527

2628
return key, nil
2729
}
30+
31+
// Checks that an API key and a project ID are provided, and that the API key is
32+
// valid for the project ID.
33+
func CheckAPIKeyAndInsightsProjectID(projectID string) error {
34+
// Check for API key.
35+
_, err := RequirePostmanAPICredentials("The Postman Insights Agent must have an API key in order to capture traces.")
36+
if err != nil {
37+
return err
38+
}
39+
40+
// Check that project ID is provided.
41+
if projectID == "" {
42+
return errors.New("project ID is missing, it must be specified")
43+
}
44+
45+
frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID())
46+
var serviceID akid.ServiceID
47+
err = akid.ParseIDAs(projectID, &serviceID)
48+
if err != nil {
49+
return errors.Wrap(err, "failed to parse project ID")
50+
}
51+
52+
_, err = util.GetServiceNameByServiceID(frontClient, serviceID)
53+
if err != nil {
54+
return err
55+
}
56+
57+
return nil
58+
}

cmd/internal/ec2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
- Log in as root user, or use `sudo su` to enable root before running the below command
1616
```
17-
POSTMAN_API_KEY=<postman-api-key> postman-insights-agent setup --collection <postman-collectionID>
17+
POSTMAN_API_KEY=<postman-api-key> postman-insights-agent ec2 --project <postman-insights-project-id>
1818
```
1919

2020
To check the status or logs please use

cmd/internal/ec2/add.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func reportStep(stepName string) {
4747
telemetry.WorkflowStep("Starting systemd conguration", stepName)
4848
}
4949

50-
func setupAgentForServer(collectionId string) error {
50+
func setupAgentForServer(projectID string) error {
5151

5252
err := checkUserPermissions()
5353
if err != nil {
@@ -58,12 +58,12 @@ func setupAgentForServer(collectionId string) error {
5858
return err
5959
}
6060

61-
err = configureSystemdFiles(collectionId)
61+
err = configureSystemdFiles(projectID)
6262
if err != nil {
6363
return err
6464
}
6565

66-
err = enablePostmanAgent()
66+
err = enablePostmanInsightsAgent()
6767
if err != nil {
6868
return err
6969
}
@@ -75,11 +75,17 @@ func askToReconfigure() error {
7575
var isReconfigure bool
7676

7777
printer.Infof("postman-insights-agent is already present as a systemd service\n")
78-
printer.Infof("Helpful commands \n Check status: systemctl status postman-insights-agent \n Disable agent: systemctl disable --now postman-insights-agent \n Check Logs: journalctl -fu postman-insights-agent\n Check env file: cat %s \n Check systemd service file: cat %s \n", envFilePath, serviceFilePath)
78+
printer.Infof("Helpful commands\n"+
79+
"Check status: systemctl status postman-insights-agent\n"+
80+
"Disable agent: systemctl disable --now postman-insights-agent\n"+
81+
"Check Logs: journalctl -fu postman-insights-agent\n"+
82+
"Check env file: cat %s\n"+
83+
"Check systemd service file: cat %s\n",
84+
envFilePath, serviceFilePath)
7985

8086
err := survey.AskOne(
8187
&survey.Confirm{
82-
Message: "Overwrite old API key and Collection ID values in systemd configuration file with current values?",
88+
Message: "Overwrite old API key and Project ID values in systemd configuration file with current values?",
8389
Default: true,
8490
Help: "Any edits made to systemd configuration files will be over-written.",
8591
},
@@ -119,7 +125,7 @@ func checkReconfiguration() error {
119125
if strings.Contains(string(out), enabled) {
120126
return askToReconfigure()
121127
}
122-
return errors.Errorf("The systemctl is-enabled command produced output this tool doesn't recognize: %q.\nPlease send this log message to %s for assistance\n", string(out), consts.SupportEmail)
128+
return errors.Errorf("The systemctl is-enabled command produced output the agent doesn't recognize: %q.\nPlease send this log message to %s for assistance\n", string(out), consts.SupportEmail)
123129

124130
}
125131

@@ -155,7 +161,7 @@ func checkSystemdExists() error {
155161
return nil
156162
}
157163

158-
func configureSystemdFiles(collectionId string) error {
164+
func configureSystemdFiles(projectID string) error {
159165
message := "Configuring systemd files"
160166
printer.Infof(message + "\n")
161167
reportStep(message)
@@ -165,19 +171,18 @@ func configureSystemdFiles(collectionId string) error {
165171
return err
166172
}
167173

168-
// Write collectionId and postman-api-key to go template file
169-
174+
// Write projectID and Postman API Key to go template file
170175
tmpl, err := template.ParseFS(envFileFS, envFileTemplateName)
171176
if err != nil {
172177
return errors.Wrapf(err, "systemd env file parsing failed")
173178
}
174179

175180
data := struct {
176181
PostmanAPIKey string
177-
CollectionId string
182+
ProjectID string
178183
}{
179184
PostmanAPIKey: os.Getenv("POSTMAN_API_KEY"),
180-
CollectionId: collectionId,
185+
ProjectID: projectID,
181186
}
182187

183188
// Ensure /etc/default exists
@@ -216,7 +221,7 @@ func configureSystemdFiles(collectionId string) error {
216221
}
217222

218223
// Starts the Postman Insights Agent as a systemd service
219-
func enablePostmanAgent() error {
224+
func enablePostmanInsightsAgent() error {
220225
message := "Enabling postman-insights-agent as a service"
221226
reportStep(message)
222227
printer.Infof(message + "\n")
@@ -230,7 +235,7 @@ func enablePostmanAgent() error {
230235
cmd = exec.Command("systemctl", []string{"enable", "--now", serviceFileName}...)
231236
_, err = cmd.CombinedOutput()
232237
if err != nil {
233-
return errors.Wrapf(err, "faild to run systemctl enable --now")
238+
return errors.Wrapf(err, "failed to run systemctl enable --now")
234239
}
235240
printer.Infof("Postman Insights Agent enabled as a systemd service. Please check logs using the below command \n")
236241
printer.Infof("journalctl -fu postman-insights-agent \n")

cmd/internal/ec2/ec2.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ package ec2
33
import (
44
"fmt"
55

6-
"github.com/pkg/errors"
76
"github.com/postmanlabs/postman-insights-agent/cmd/internal/cmderr"
8-
"github.com/postmanlabs/postman-insights-agent/rest"
9-
"github.com/postmanlabs/postman-insights-agent/telemetry"
10-
"github.com/postmanlabs/postman-insights-agent/util"
117
"github.com/spf13/cobra"
128
)
139

1410
var (
15-
// Mandatory flag: Postman collection id
16-
collectionId string
11+
// Postman Insights project id
12+
projectID string
1713
)
1814

1915
var Cmd = &cobra.Command{
20-
Deprecated: "This is no longer supported and might be removed in a future release.",
16+
Use: "ec2",
17+
Short: "Add the Postman Insights Agent to the EC2 server.",
18+
Long: "The CLI will add the Postman Insights Agent as a systemd service to your current EC2 server.",
19+
SilenceUsage: true,
20+
RunE: addAgentToEC2,
21+
}
22+
23+
// 'postman-insights-agent ec2' should default to 'postman-insights-agent ec2 setup'
24+
var SetupInEC2Cmd = &cobra.Command{
2125
Use: "setup",
22-
Short: "Add the Postman Insights Agent to the current server.",
23-
Long: "The CLI will add the Postman Insights Agent as a systemd service to your current server.",
26+
Short: Cmd.Short,
27+
Long: Cmd.Long,
2428
SilenceUsage: true,
2529
RunE: addAgentToEC2,
2630
}
@@ -37,30 +41,21 @@ var RemoveFromEC2Cmd = &cobra.Command{
3741
}
3842

3943
func init() {
40-
Cmd.PersistentFlags().StringVar(&collectionId, "collection", "", "Your Postman collection ID")
41-
Cmd.MarkPersistentFlagRequired("collection")
44+
Cmd.PersistentFlags().StringVar(&projectID, "project", "", "Your Insights Project ID")
45+
Cmd.MarkPersistentFlagRequired("project")
4246

47+
Cmd.AddCommand(SetupInEC2Cmd)
4348
Cmd.AddCommand(RemoveFromEC2Cmd)
4449
}
4550

4651
func addAgentToEC2(cmd *cobra.Command, args []string) error {
47-
// Check for API key
48-
_, err := cmderr.RequirePostmanAPICredentials("The Postman Insights Agent must have an API key in order to capture traces.")
49-
if err != nil {
50-
return err
51-
}
52-
53-
// Check collecton Id's existence
54-
if collectionId == "" {
55-
return errors.New("Must specify the ID of your collection with the --collection flag.")
56-
}
57-
frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID())
58-
_, err = util.GetOrCreateServiceIDByPostmanCollectionID(frontClient, collectionId)
52+
// Check if the API key and Insights project ID are valid
53+
err := cmderr.CheckAPIKeyAndInsightsProjectID(projectID)
5954
if err != nil {
6055
return err
6156
}
6257

63-
return setupAgentForServer(collectionId)
58+
return setupAgentForServer(projectID)
6459
}
6560

6661
func removeAgentFromEC2(cmd *cobra.Command, args []string) error {

cmd/internal/ec2/postman-insights-agent.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ EnvironmentFile=/etc/default/postman-insights-agent
88
# DO NOT CHANGE
99
# "${FOO}" uses the arguement as is, while "$FOO" splits the string on white space
1010
# Reference: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines
11-
ExecStart=/usr/bin/postman-insights-agent apidump --collection "${COLLECTION_ID}" --interfaces "${INTERFACES}" --filter "${FILTER}" "$EXTRA_APIDUMP_ARGS"
11+
ExecStart=/usr/bin/postman-insights-agent apidump --project "${PROJECT_ID}" --interfaces "${INTERFACES}" --filter "${FILTER}" "$EXTRA_APIDUMP_ARGS"
1212

1313
[Install]
1414
WantedBy=multi-user.target

cmd/internal/ec2/postman-insights-agent.tmpl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
# Add your Postman API key below. For example:
22
#
3-
# POSTMAN_API_KEY=PMAC-XXXXXXX
3+
# POSTMAN_API_KEY=PMAK-XXXXXXX
44
#
55
# This is required.
66

77
POSTMAN_API_KEY={{.PostmanAPIKey}}
88

99

10-
# Add your your Postman Collection ID.
11-
#This is required.
12-
13-
COLLECTION_ID={{.CollectionId}}
10+
# Add your your Postman Project ID.
11+
#
12+
# PROJECT_ID=svc_XXXXXXX
13+
#
14+
# This is required.
1415

15-
# For example,
16-
# COLLECTION_ID=1234567-890abcde-f123-4567-890a-bcdef1234567
16+
PROJECT_ID={{.ProjectID}}
1717

1818

1919
# INTERFACES is optional. If left blank, the agent will listen on all available
2020
# network interfaces.
2121
#
2222
# FILTER is optional. If left blank, no packet-capture filter will be applied.
23+
#
2324
# For example
2425
# INTERFACES=lo,eth0,eth1
2526
# FILTER="port 80 or port 8080"

cmd/internal/ecs/ecs.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ecs
33
import (
44
"fmt"
55

6-
"github.com/akitasoftware/akita-libs/akid"
76
"github.com/akitasoftware/go-utils/optionals"
87
"github.com/aws/aws-sdk-go-v2/aws"
98
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
@@ -12,9 +11,6 @@ import (
1211
ecs_cloudformation_utils "github.com/postmanlabs/postman-insights-agent/aws_utils/cloudformation/ecs"
1312
ecs_console_utils "github.com/postmanlabs/postman-insights-agent/aws_utils/console/ecs"
1413
"github.com/postmanlabs/postman-insights-agent/cmd/internal/cmderr"
15-
"github.com/postmanlabs/postman-insights-agent/rest"
16-
"github.com/postmanlabs/postman-insights-agent/telemetry"
17-
"github.com/postmanlabs/postman-insights-agent/util"
1814
"github.com/spf13/cobra"
1915
)
2016

@@ -141,37 +137,9 @@ func init() {
141137
Cmd.AddCommand(RemoveFromECSCmd)
142138
}
143139

144-
// Checks that an API key and a project ID are provided, and that the API key is
145-
// valid for the project ID.
146-
func checkAPIKeyAndProjectID() error {
147-
// Check for API key.
148-
_, err := cmderr.RequirePostmanAPICredentials("The Postman Insights Agent must have an API key in order to capture traces.")
149-
if err != nil {
150-
return err
151-
}
152-
153-
// Check that project ID is provided.
154-
if projectId == "" {
155-
return errors.New("--project must be specified")
156-
}
157-
158-
frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID())
159-
var serviceID akid.ServiceID
160-
err = akid.ParseIDAs(projectId, &serviceID)
161-
if err != nil {
162-
return errors.Wrap(err, "failed to parse service ID")
163-
}
164-
165-
_, err = util.GetServiceNameByServiceID(frontClient, serviceID)
166-
if err != nil {
167-
return err
168-
}
169-
170-
return nil
171-
}
172-
173140
func addAgentToECS(cmd *cobra.Command, args []string) error {
174-
err := checkAPIKeyAndProjectID()
141+
// Check if the API key and Insights project ID are valid
142+
err := cmderr.CheckAPIKeyAndInsightsProjectID(projectId)
175143
if err != nil {
176144
return err
177145
}
@@ -184,7 +152,7 @@ func removeAgentFromECS(cmd *cobra.Command, args []string) error {
184152
}
185153

186154
func printCloudFormationFragment(cmd *cobra.Command, args []string) error {
187-
err := checkAPIKeyAndProjectID()
155+
err := cmderr.CheckAPIKeyAndInsightsProjectID(projectId)
188156
if err != nil {
189157
return err
190158
}
@@ -211,7 +179,7 @@ func printCloudFormationFragment(cmd *cobra.Command, args []string) error {
211179
}
212180

213181
func printECSTaskDefinition(cmd *cobra.Command, args []string) error {
214-
err := checkAPIKeyAndProjectID()
182+
err := cmderr.CheckAPIKeyAndInsightsProjectID(projectId)
215183
if err != nil {
216184
return err
217185
}

0 commit comments

Comments
 (0)