Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove cloud build helper #572

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 8 additions & 71 deletions tools/sgcloudspanner/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package sgcloudspanner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"strings"
"time"

Expand All @@ -18,10 +16,9 @@ import (
// Cloud Spanner Emulator versions can be found here,
// https://console.cloud.google.com/gcr/images/cloud-spanner-emulator/global/emulator
const (
cloudbuildNetwork = "cloudbuild"
url = "gcr.io/cloud-spanner-emulator/emulator"
version = "1.5.17"
image = url + ":" + version
url = "gcr.io/cloud-spanner-emulator/emulator"
version = "1.5.17"
image = url + ":" + version
)

// RunEmulator runs the Cloud Spanner emulator in Docker.
Expand All @@ -39,12 +36,7 @@ func RunEmulator(ctx context.Context) (_ func(), err error) {
if !isDockerDaemonRunning(ctx) {
return nil, fmt.Errorf("the Docker daemon does not seem to be running")
}
var dockerRunCmd *exec.Cmd
if isRunningOnCloudBuild(ctx) {
dockerRunCmd = sgdocker.Command(ctx, "run", "-d", "--network", cloudbuildNetwork, "--publish-all", image)
} else {
dockerRunCmd = sgdocker.Command(ctx, "run", "-d", "--publish-all", image)
}
dockerRunCmd := sgdocker.Command(ctx, "run", "-d", "--publish-all", image)
var dockerRunStdout strings.Builder
dockerRunCmd.Stdout = &dockerRunStdout
if err := dockerRunCmd.Run(); err != nil {
Expand All @@ -67,19 +59,10 @@ func RunEmulator(ctx context.Context) (_ func(), err error) {
sg.Logger(ctx).Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err)
}
}
var emulatorHost string
if isRunningOnCloudBuild(ctx) {
emulatorHost, err = inspectPortAddressCloudbuild(ctx, containerID, "9010/tcp", image)
if err != nil {
cleanup()
return nil, err
}
} else {
emulatorHost, err = inspectPortAddress(ctx, containerID, "9010/tcp")
if err != nil {
cleanup()
return nil, err
}
emulatorHost, err := inspectPortAddress(ctx, containerID, "9010/tcp")
if err != nil {
cleanup()
return nil, err
}
if err := os.Setenv("SPANNER_EMULATOR_HOST", emulatorHost); err != nil {
cleanup()
Expand Down Expand Up @@ -118,45 +101,6 @@ func inspectPortAddress(ctx context.Context, containerID, containerPort string)
return "", fmt.Errorf("no mapping found for %s in container %s", containerPort, containerID)
}

func inspectPortAddressCloudbuild(ctx context.Context, containerID, containerPort, image string) (string, error) {
var stdout bytes.Buffer
cmd := sgdocker.Command(ctx, "inspect", containerID)
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
var containers []struct {
Config struct {
Image string
}
NetworkSettings struct {
Ports map[string][]struct {
HostPort string
}
Networks map[string]struct {
Gateway string
}
}
}
if err := json.NewDecoder(&stdout).Decode(&containers); err != nil {
return "", err
}
var host, port string

for _, container := range containers {
if container.Config.Image == image {
port = container.NetworkSettings.Ports[containerPort][0].HostPort // prefer first option
}
if network, ok := container.NetworkSettings.Networks[cloudbuildNetwork]; ok {
host = network.Gateway
}
}
if host == "" || port == "" {
return "", fmt.Errorf("failed to inspect container %s for port %s", containerID, containerPort)
}
return fmt.Sprintf("%s:%s", host, port), nil
}

func awaitReachable(ctx context.Context, addr string, wait, maxWait time.Duration) error {
deadline := time.Now().Add(maxWait)
for time.Now().Before(deadline) {
Expand All @@ -173,10 +117,3 @@ func awaitReachable(ctx context.Context, addr string, wait, maxWait time.Duratio
}
return fmt.Errorf("%s was unreachable for %v", addr, maxWait)
}

func isRunningOnCloudBuild(ctx context.Context) bool {
cmd := sgdocker.Command(ctx, "network", "inspect", cloudbuildNetwork)
var stdout bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, nil
return cmd.Run() == nil
}
18 changes: 0 additions & 18 deletions tools/sggosemanticrelease/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,6 @@ func Command(ctx context.Context, args ...string) *exec.Cmd {
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

// ReleaseFromCloudBuildCommand creates a semantic release with optional extra arguments to semantic-release.
// Deprecated: This helper will be removed in a coming release. Copy it if you still need it.
func ReleaseFromCloudBuildCommand(ctx context.Context, ci bool, repo string, extraArgs ...string) *exec.Cmd {
args := []string{
"--allow-initial-development-versions",
"--allow-no-changes",
"--ci-condition",
"default",
"--provider-opt",
}
args = append(args, "slug="+repo)
args = append(args, extraArgs...)
if !ci {
args = append(args, "--dry")
}
return Command(ctx, args...)
}

func PrepareCommand(ctx context.Context) error {
binDir := sg.FromToolsDir(name, version)
binary := filepath.Join(binDir, name)
Expand Down