This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): support cloud build for preparation in geospatialjpv3
- Loading branch information
Showing
9 changed files
with
264 additions
and
133 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
98 changes: 98 additions & 0 deletions
98
server/cmsintegration/cmsintegrationv3/geospatialjpv3/prepare_cloudbuild.go
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,98 @@ | ||
package geospatialjpv3 | ||
|
||
import ( | ||
"context" | ||
"path" | ||
|
||
"github.com/reearth/reearthx/log" | ||
"github.com/reearth/reearthx/rerror" | ||
"google.golang.org/api/cloudbuild/v1" | ||
) | ||
|
||
type prepareOnCloudBuildConfig struct { | ||
City string | ||
Project string | ||
CMSURL string | ||
CMSToken string | ||
CloudBuildImage string | ||
CloudBuildMachineType string | ||
CloudBuildProject string | ||
CloudBuildRegion string | ||
} | ||
|
||
const defaultDockerImage = "eukarya/plateauview2-sidecar-worker:latest" | ||
|
||
func prepareOnCloudBuild(ctx context.Context, conf prepareOnCloudBuildConfig) error { | ||
if conf.CloudBuildImage == "" { | ||
conf.CloudBuildImage = defaultDockerImage | ||
} | ||
|
||
log.Debugfc(ctx, "geospatialjp webhook: prepare (cloud build): %s", ppp.Sprint(conf)) | ||
|
||
return runCloudBuild(ctx, CloudBuildConfig{ | ||
Image: conf.CloudBuildImage, | ||
Args: []string{ | ||
"--city=" + conf.City, | ||
"--project=" + conf.Project, | ||
"--wetrun", | ||
}, | ||
Env: []string{ | ||
"REEARTH_CMS_URL=" + conf.CMSURL, | ||
"REEARTH_CMS_TOKEN=" + conf.CMSToken, | ||
}, | ||
MachineType: conf.CloudBuildMachineType, | ||
Project: conf.CloudBuildProject, | ||
Region: conf.CloudBuildRegion, | ||
}) | ||
} | ||
|
||
type CloudBuildConfig struct { | ||
Image string | ||
Args []string | ||
Env []string | ||
MachineType string | ||
Region string | ||
Project string | ||
} | ||
|
||
func runCloudBuild(ctx context.Context, conf CloudBuildConfig) error { | ||
cb, err := cloudbuild.NewService(ctx) | ||
if err != nil { | ||
return rerror.ErrInternalBy(err) | ||
} | ||
|
||
machineType := "" | ||
if v := conf.MachineType; v != "default" { | ||
machineType = v | ||
} | ||
|
||
build := &cloudbuild.Build{ | ||
Timeout: "86400s", // 1 day | ||
QueueTtl: "86400s", // 1 day | ||
Steps: []*cloudbuild.BuildStep{ | ||
{ | ||
Name: conf.Image, | ||
Args: conf.Args, | ||
Env: conf.Env, | ||
}, | ||
}, | ||
Options: &cloudbuild.BuildOptions{ | ||
MachineType: machineType, | ||
}, | ||
} | ||
|
||
if conf.Region != "" { | ||
call := cb.Projects.Locations.Builds.Create( | ||
path.Join("projects", conf.Project, "locations", conf.Region), | ||
build, | ||
) | ||
_, err = call.Do() | ||
} else { | ||
call := cb.Projects.Builds.Create(conf.Project, build) | ||
_, err = call.Do() | ||
} | ||
if err != nil { | ||
return rerror.ErrInternalBy(err) | ||
} | ||
return nil | ||
} |
50 changes: 50 additions & 0 deletions
50
server/cmsintegration/cmsintegrationv3/geospatialjpv3/prepare_cloudrunjobs.go
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,50 @@ | ||
package geospatialjpv3 | ||
|
||
import ( | ||
"context" | ||
|
||
run "cloud.google.com/go/run/apiv2" | ||
runpb "cloud.google.com/go/run/apiv2/runpb" | ||
"github.com/reearth/reearthx/log" | ||
) | ||
|
||
// jobName (Cloud Run Jobs): "projects/" + gcpProjectID + "/locations/" + gcpLocation + "/jobs/plateauview-api-worker" | ||
|
||
func prepareWithCloudRunJobs(ctx context.Context, itemID, projectID, jobName string) error { | ||
if jobName == "" { | ||
log.Debugfc(ctx, "geospatialjp webhook: no job name") | ||
return nil | ||
} | ||
|
||
log.Debugfc(ctx, "geospatialjp webhook: prepare (cloud run jobs): %s", jobName) | ||
|
||
client, err := run.NewJobsClient(ctx) | ||
if err != nil { | ||
log.Debugfc(ctx, "geospatialjp webhook: failed to create run client: %v", err) | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
overrides := runpb.RunJobRequest_Overrides{ | ||
ContainerOverrides: []*runpb.RunJobRequest_Overrides_ContainerOverride{ | ||
{Args: []string{ | ||
"prepare-gspatialjp", | ||
"--city=" + itemID, | ||
"--project=" + projectID, | ||
"--wetrun", | ||
}}, | ||
}} | ||
|
||
req := &runpb.RunJobRequest{ | ||
Name: jobName, | ||
Overrides: &overrides, | ||
} | ||
|
||
if _, err = client.RunJob(ctx, req); err != nil { | ||
log.Debugfc(ctx, "geospatialjp webhook: failed to run job: %v", err) | ||
return err | ||
} | ||
|
||
log.Debugfc(ctx, "geospatialjp webhook: run job: %v", req) | ||
return nil | ||
} |
20 changes: 0 additions & 20 deletions
20
server/cmsintegration/cmsintegrationv3/geospatialjpv3/prepare_test.go
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.