-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6272e39
commit 5495681
Showing
14 changed files
with
321 additions
and
136 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
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,106 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
awssdk "github.com/aws/aws-sdk-go-v2/aws" | ||
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/flashbots/vpnham/aws" | ||
"github.com/flashbots/vpnham/metrics" | ||
"github.com/flashbots/vpnham/utils" | ||
"go.opentelemetry.io/otel/attribute" | ||
otelapi "go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
type updateAWSRouteTables struct { | ||
name string | ||
timeout time.Duration | ||
|
||
cidr string | ||
networkInterfaceID string | ||
routeTables []string | ||
} | ||
|
||
func UpdateAWSRouteTables( | ||
name string, | ||
timeout time.Duration, | ||
cidr string, | ||
networkInterfaceID string, | ||
routeTables []string, | ||
) Job { | ||
return &updateAWSRouteTables{ | ||
name: name, | ||
timeout: timeout, | ||
cidr: cidr, | ||
networkInterfaceID: networkInterfaceID, | ||
routeTables: routeTables, | ||
} | ||
} | ||
|
||
func (j *updateAWSRouteTables) Name() string { | ||
return j.name | ||
} | ||
|
||
func (j *updateAWSRouteTables) Execute(ctx context.Context) error { | ||
errs := []error{} | ||
for _, rt := range j.routeTables { | ||
err := j.updateRouteTable(ctx, rt, j.cidr, j.networkInterfaceID) | ||
if err != nil { | ||
metrics.Errors.Add(ctx, 1, otelapi.WithAttributes( | ||
attribute.String(metrics.LabelErrorScope, "job_"+j.name), | ||
)) | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
switch len(errs) { | ||
default: | ||
return errors.Join(errs...) | ||
case 1: | ||
return errs[0] | ||
case 0: | ||
return nil | ||
} | ||
} | ||
|
||
func (j *updateAWSRouteTables) updateRouteTable( | ||
ctx context.Context, | ||
routeTable string, | ||
cidr string, | ||
networkInterfaceID string, | ||
) error { | ||
cli, err := aws.NewClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var route *awstypes.Route | ||
|
||
// check if the route is already set | ||
err = utils.WithTimeout(ctx, j.timeout, func(ctx context.Context) error { | ||
route, err = cli.FindRoute(ctx, routeTable, cidr) | ||
return err | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if route != nil && awssdk.ToString(route.NetworkInterfaceId) == networkInterfaceID { | ||
// route is already up to date | ||
return nil | ||
} | ||
|
||
if route != nil { | ||
// route exists but with different next hop | ||
return utils.WithTimeout(ctx, j.timeout, func(ctx context.Context) error { | ||
return cli.UpdateRoute(ctx, routeTable, cidr, networkInterfaceID) | ||
}) | ||
} | ||
|
||
// no route yet | ||
return utils.WithTimeout(ctx, j.timeout, func(ctx context.Context) error { | ||
return cli.CreateRoute(ctx, routeTable, cidr, networkInterfaceID) | ||
}) | ||
} |
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,8 @@ | ||
package job | ||
|
||
import "context" | ||
|
||
type Job interface { | ||
Execute(context.Context) error | ||
Name() string | ||
} |
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,105 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
|
||
"github.com/flashbots/vpnham/logutils" | ||
"github.com/flashbots/vpnham/metrics" | ||
"github.com/flashbots/vpnham/types" | ||
"github.com/flashbots/vpnham/utils" | ||
"go.opentelemetry.io/otel/attribute" | ||
otelapi "go.opentelemetry.io/otel/metric" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type runScript struct { | ||
name string | ||
timeout time.Duration | ||
|
||
script types.Script | ||
} | ||
|
||
func RunScript( | ||
name string, | ||
timeout time.Duration, | ||
script types.Script, | ||
) Job { | ||
return &runScript{ | ||
name: name, | ||
timeout: timeout, | ||
script: script, | ||
} | ||
} | ||
|
||
func (j *runScript) Name() string { | ||
return j.name | ||
} | ||
|
||
func (j *runScript) Execute(ctx context.Context) error { | ||
l := logutils.LoggerFromContext(ctx) | ||
|
||
errs := []error{} | ||
for step, _cmd := range j.script { | ||
if len(_cmd) == 0 { | ||
continue | ||
} | ||
|
||
strCmd := strings.Join(_cmd, " ") | ||
|
||
l.Debug("Executing command", | ||
zap.String("command", strCmd), | ||
) | ||
|
||
ctx, cancel := context.WithTimeout(ctx, j.timeout) | ||
defer cancel() | ||
|
||
cmd := exec.CommandContext(ctx, _cmd[0], _cmd[1:]...) | ||
|
||
stdout := &strings.Builder{} | ||
cmd.Stdout = stdout | ||
|
||
stderr := &strings.Builder{} | ||
cmd.Stderr = stderr | ||
|
||
cmd.Env = os.Environ() | ||
|
||
start := time.Now() | ||
err := utils.WithTimeout(ctx, j.timeout, func(ctx context.Context) error { | ||
return cmd.Run() | ||
}) | ||
duration := time.Since(start) | ||
|
||
if err != nil { | ||
metrics.Errors.Add(ctx, 1, otelapi.WithAttributes( | ||
attribute.String(metrics.LabelErrorScope, "job_"+j.name), | ||
)) | ||
errs = append(errs, err) | ||
} | ||
|
||
l.Info("Executed command", | ||
zap.String("script", j.name), | ||
zap.Int("step", step), | ||
zap.String("command", strCmd), | ||
zap.Int64("duration_us", duration.Microseconds()), | ||
|
||
zap.String("stderr", strings.TrimSpace(stderr.String())), | ||
zap.String("stdout", strings.TrimSpace(stdout.String())), | ||
|
||
zap.Error(err), | ||
) | ||
} | ||
|
||
switch len(errs) { | ||
default: | ||
return errors.Join(errs...) | ||
case 1: | ||
return errs[0] | ||
case 0: | ||
return nil | ||
} | ||
} |
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.