53
pkg/speedrun/cloud/google.go
Normal file
53
pkg/speedrun/cloud/google.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package cloud
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/api/compute/v1"
|
||||
)
|
||||
|
||||
type GoogleClient struct {
|
||||
*compute.Service
|
||||
}
|
||||
|
||||
func NewGCPClient() (*GoogleClient, error) {
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
|
||||
gce, err := compute.NewService(ctx)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("couldn't initialize GCP client: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &GoogleClient{gce}, nil
|
||||
}
|
||||
|
||||
// GetInstances returns a list of external IP addresses used for the SHH connection
|
||||
func (c *GoogleClient) GetInstances(project string) ([]Instance, error) {
|
||||
instances := []Instance{}
|
||||
listCall := c.Instances.AggregatedList(project).Fields("nextPageToken", "items(Name,NetworkInterfaces,Labels)")
|
||||
var ctx context.Context
|
||||
|
||||
listCall.Pages(ctx, func(list *compute.InstanceAggregatedList) error {
|
||||
for _, item := range list.Items {
|
||||
for _, instance := range item.Instances {
|
||||
i := Instance{
|
||||
Name: instance.Name,
|
||||
PrivateAddress: instance.NetworkInterfaces[0].NetworkIP,
|
||||
PublicAddress: instance.NetworkInterfaces[0].AccessConfigs[0].NatIP,
|
||||
Labels: instance.Labels,
|
||||
}
|
||||
instances = append(instances, i)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_, err := listCall.Do()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return instances, nil
|
||||
}
|
||||
93
pkg/speedrun/cloud/instance.go
Normal file
93
pkg/speedrun/cloud/instance.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package cloud
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
|
||||
"github.com/antonmedv/expr"
|
||||
"github.com/apex/log"
|
||||
"github.com/speedrunsh/speedrun/pkg/common/cryptoutil"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
PublicAddress string
|
||||
PrivateAddress string
|
||||
Name string
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
func (i Instance) GetAddress(private bool) string {
|
||||
if private {
|
||||
return i.PrivateAddress
|
||||
}
|
||||
|
||||
return i.PublicAddress
|
||||
}
|
||||
|
||||
func GetInstances(target string) ([]Instance, error) {
|
||||
project := viper.GetString("gcp.projectid")
|
||||
|
||||
gcpClient, err := NewGCPClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info("Fetching instance list")
|
||||
instances, err := gcpClient.GetInstances(project)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
subset, err := filter(instances, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(subset) == 0 {
|
||||
return nil, fmt.Errorf("no instances found")
|
||||
}
|
||||
|
||||
return subset, nil
|
||||
}
|
||||
|
||||
func SetupTLS() (*tls.Config, error) {
|
||||
insecure := viper.GetBool("tls.insecure")
|
||||
caPath := viper.GetString("tls.ca")
|
||||
certPath := viper.GetString("tls.cert")
|
||||
keyPath := viper.GetString("tls.key")
|
||||
|
||||
if insecure {
|
||||
log.Warn("Using insecure TLS configuration, this should be avoided in production environments")
|
||||
return cryptoutil.InsecureTLSConfig()
|
||||
} else {
|
||||
return cryptoutil.ClientTLSConfig(caPath, certPath, keyPath)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func filter(instnces []Instance, target string) ([]Instance, error) {
|
||||
var subset []Instance
|
||||
|
||||
program, err := expr.Compile(target, expr.Env(Instance{}), expr.AsBool())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, instance := range instnces {
|
||||
output, err := expr.Run(program, instance)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
match, ok := output.(bool)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if match {
|
||||
subset = append(subset, instance)
|
||||
}
|
||||
}
|
||||
return subset, nil
|
||||
}
|
||||
Reference in New Issue
Block a user