Skip to content

Commit

Permalink
Add more info when stopping/starting instances (#46)
Browse files Browse the repository at this point in the history
* Add more info when stopping/starting instances

* Print instance tags too

* Print only the first 10 tags

* Pass ctx to print requestID and service UUID

Do not print instance tags as they can be altered by the users and
cannot be trusted.

* string casting not necessary, already a string

* go fmt
  • Loading branch information
fridim authored Nov 14, 2023
1 parent b062cad commit e7a1a3b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
32 changes: 29 additions & 3 deletions cmd/sandbox-api/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,42 @@ func (w Worker) Execute(j *models.LifecycleResourceJob) error {
}

log.Logger.Debug("assume successful")

ctx := context.TODO()

// Add RequestID to context
ctx = context.WithValue(ctx, "RequestID", j.RequestID)
// If job has a parent, add serviceUUID to context
if j.ParentID != 0 {
// Load parent job from DB
parentJob, err := models.GetLifecyclePlacementJob(w.Dbpool, j.ParentID)

if err != nil {
log.Logger.Error("Error getting parent job", "error", err)
return err
}

// Load placement from DB
placement, err := models.GetPlacement(w.Dbpool, parentJob.PlacementID)
if err != nil {
log.Logger.Error("Error getting placement", "error", err)
return err
}

// Add service UUID to context
ctx = context.WithValue(ctx, "ServiceUUID", placement.ServiceUuid)
}

switch j.Action {
case "start":
j.SetStatus("running")
return sandbox.Start(assume.Credentials)
return sandbox.Start(ctx, assume.Credentials)
case "stop":
j.SetStatus("running")
return sandbox.Stop(assume.Credentials)
return sandbox.Stop(ctx, assume.Credentials)
case "status":
j.SetStatus("running")
status, err := sandbox.Status(assume.Credentials, j)
status, err := sandbox.Status(ctx, assume.Credentials, j)
if err != nil {
j.SetStatus("error")
log.Logger.Error("Error getting status", "error", err)
Expand Down
2 changes: 0 additions & 2 deletions internal/dynamodb/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dynamodb

import (
"errors"
"log/slog"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -246,7 +245,6 @@ func GetAccount(svc *dynamodb.DynamoDB, name string) (AwsAccountDynamoDB, error)
log.Logger.Error("Unmarshalling dynamodb item", "error", err)
return AwsAccountDynamoDB{}, err
}
log.Logger.Info("GetItem succeeded", slog.String("sandbox", sandbox.Name))

return sandbox, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func InitLoggers(debugFlag bool, attrs []slog.Attr) {
}

opts := slog.HandlerOptions{
Level: slog.LevelInfo,
Level: slog.LevelInfo,
ReplaceAttr: replaceAttrs,
}
if debugFlag {
Expand Down
26 changes: 21 additions & 5 deletions internal/models/aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Sort[T Sortable](accounts []T, by string) []T {
}

// Start method starts all the stopped instances in the account
func (a AwsAccount) Start(creds *ststypes.Credentials) error {
func (a AwsAccount) Start(ctx context.Context, creds *ststypes.Credentials) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Logger.Error("Error loading config", "error", err)
Expand Down Expand Up @@ -201,7 +201,15 @@ func (a AwsAccount) Start(creds *ststypes.Credentials) error {
errR = err
continue
}
log.Logger.Info("Start instance", "account", a.Name, "instance_id", *instance.InstanceId)
log.Logger.Info("Start instance",
"account", a.Name,
"account_id", a.AccountID,
"instance_id", *instance.InstanceId,
"instance_type", instance.InstanceType,
"region", *region.RegionName,
"request_id", ctx.Value("RequestID"),
"service_uuid", ctx.Value("ServiceUUID"),
)
}
}
}
Expand All @@ -210,7 +218,7 @@ func (a AwsAccount) Start(creds *ststypes.Credentials) error {
}

// Stop method stops all the running instances in the account
func (a AwsAccount) Stop(creds *ststypes.Credentials) error {
func (a AwsAccount) Stop(ctx context.Context, creds *ststypes.Credentials) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Logger.Error("Error loading config", "error", err)
Expand Down Expand Up @@ -276,7 +284,15 @@ func (a AwsAccount) Stop(creds *ststypes.Credentials) error {
errR = err
continue
}
log.Logger.Info("Stop instance", "account", a.Name, "instance_id", *instance.InstanceId)
log.Logger.Info("Stop instance",
"account", a.Name,
"account_id", a.AccountID,
"instance_id", *instance.InstanceId,
"instance_type", instance.InstanceType,
"region", *region.RegionName,
"request_id", ctx.Value("RequestID"),
"service_uuid", ctx.Value("ServiceUUID"),
)
}
}
}
Expand Down Expand Up @@ -319,7 +335,7 @@ func MakeStatus(job *LifecycleResourceJob) Status {
}

// Status method returns the status of all the instances in the account
func (a AwsAccount) Status(creds *ststypes.Credentials, job *LifecycleResourceJob) (Status, error) {
func (a AwsAccount) Status(ctx context.Context, creds *ststypes.Credentials, job *LifecycleResourceJob) (Status, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Logger.Error("Error loading config", "error", err)
Expand Down
12 changes: 6 additions & 6 deletions internal/models/lifecycle_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func GetLifecycleResourceJob(dbpool *pgxpool.Pool, id int) (*LifecycleResourceJo

err := dbpool.QueryRow(
context.Background(),
"SELECT id, COALESCE(parent_id, 0), resource_name, resource_type, status, request, lifecycle_result, lifecycle_action, updated_at, locality FROM lifecycle_resource_jobs WHERE id = $1",
"SELECT id, COALESCE(parent_id, 0), resource_name, resource_type, status, request_id, request, lifecycle_result, lifecycle_action, updated_at, locality FROM lifecycle_resource_jobs WHERE id = $1",
id,
).Scan(&j.ID, &j.ParentID, &j.ResourceName, &j.ResourceType, &j.Status, &j.Request, &j.Result, &j.Action, &j.UpdatedAt, &j.Locality)
).Scan(&j.ID, &j.ParentID, &j.ResourceName, &j.ResourceType, &j.Status, &j.RequestID, &j.Request, &j.Result, &j.Action, &j.UpdatedAt, &j.Locality)

if err != nil {
return nil, err
Expand Down Expand Up @@ -80,9 +80,9 @@ func GetLifecyclePlacementJob(dbpool *pgxpool.Pool, id int) (*LifecyclePlacement

err := dbpool.QueryRow(
context.Background(),
"SELECT id, placement_id, status, request, lifecycle_action, locality FROM lifecycle_placement_jobs WHERE id = $1",
"SELECT id, placement_id, status, request_id, request, lifecycle_action, locality FROM lifecycle_placement_jobs WHERE id = $1",
id,
).Scan(&j.ID, &j.PlacementID, &j.Status, &j.Request, &j.Action, &j.Locality)
).Scan(&j.ID, &j.PlacementID, &j.Status, &j.RequestID, &j.Request, &j.Action, &j.Locality)
if err != nil {
return nil, err
}
Expand All @@ -98,9 +98,9 @@ func GetLifecyclePlacementJobByRequestID(dbpool *pgxpool.Pool, requestID string)

err := dbpool.QueryRow(
context.Background(),
"SELECT id, placement_id, status, request, lifecycle_action, locality FROM lifecycle_placement_jobs WHERE request_id = $1",
"SELECT id, placement_id, status, request_id, request, lifecycle_action, locality FROM lifecycle_placement_jobs WHERE request_id = $1",
requestID,
).Scan(&j.ID, &j.PlacementID, &j.Status, &j.Request, &j.Action, &j.Locality)
).Scan(&j.ID, &j.PlacementID, &j.Status, &j.RequestID, &j.Request, &j.Action, &j.Locality)
if err != nil {
return nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions tools/placement_action.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#################################################################################
# Get an access token using the login token
#################################################################################

GET http://{{host}}/api/v1/login
Authorization: Bearer {{login_token}}
HTTP 200
[Captures]
access_token: jsonpath "$.access_token"
[Asserts]
jsonpath "$.access_token" isString
jsonpath "$.access_token_exp" isString


#################################################################################
# Stop a placement
#################################################################################

PUT http://{{host}}/api/v1/placements/{{uuid}}/{{action}}
Authorization: Bearer {{access_token}}
HTTP 202
21 changes: 21 additions & 0 deletions tools/sandbox_action.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#################################################################################
# Get an access token using the login token
#################################################################################

GET http://{{host}}/api/v1/login
Authorization: Bearer {{login_token}}
HTTP 200
[Captures]
access_token: jsonpath "$.access_token"
[Asserts]
jsonpath "$.access_token" isString
jsonpath "$.access_token_exp" isString


#################################################################################
# Stop a placement
#################################################################################

PUT http://{{host}}/api/v1/accounts/{{type}}/{{name}}/{{action}}
Authorization: Bearer {{access_token}}
HTTP 202

0 comments on commit e7a1a3b

Please sign in to comment.