Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix(server): support cloud build for preparation in geospatialjpv3
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 20, 2024
1 parent 8839f26 commit 93d31a3
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 133 deletions.
7 changes: 6 additions & 1 deletion server/cmsintegration/cmsintegrationcommon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ type Config struct {
// FME v3
FMEURLV3 string
// geospatial.jp v3
GeospatialjpJobName string
GeospatialjpBuildType string
GeospatialjpCloudRunJobsJobName string
GeospatialjpCloudBuildImage string
GeospatialjpCloudBuildMachineType string
GeospatialjpCloudBuildProject string
GeospatialjpCloudBuildRegion string

// compat
// geospatial.jp v2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ type Config struct {
CMSBase string
CMSToken string
CMSIntegration string
JobName string
BuildType string
// cloud run jobs
CloudRunJobsJobName string
// cloud build image
CloudBuildImage string
CloudBuildMachineType string
CloudBuildProject string
CloudBuildRegion string
}

var ppp *pp.PrettyPrinter
Expand Down
52 changes: 14 additions & 38 deletions server/cmsintegration/cmsintegrationv3/geospatialjpv3/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,21 @@ 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 Prepare(ctx context.Context, itemID, projectID, jobName string) error {
log.Debugfc(ctx, "geospatialjp webhook: Prepare: %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,
func Prepare(ctx context.Context, itemID, projectID string, conf Config) error {
if conf.BuildType == "cloudbuild" {
return prepareOnCloudBuild(ctx, prepareOnCloudBuildConfig{
City: itemID,
Project: projectID,
CMSURL: conf.CMSBase,
CMSToken: conf.CMSToken,
CloudBuildImage: conf.CloudBuildImage,
CloudBuildMachineType: conf.CloudBuildMachineType,
CloudBuildProject: conf.CloudBuildProject,
CloudBuildRegion: conf.CloudBuildRegion,
})
} else {
return prepareWithCloudRunJobs(ctx, itemID, projectID, conf.CloudRunJobsJobName)
}

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
}
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
}
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
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *handler) Webhook(conf Config) (cmswebhook.Handler, error) {
log.Debugfc(ctx, "geospatialjpv3 webhook: %s", ppp.Sprint(cityItem))

if b := getChangedBool(ctx, w, prepareFieldKey); b != nil && *b {
if err := Prepare(ctx, cityItem.ID, w.ProjectID(), conf.JobName); err != nil {
if err := Prepare(ctx, cityItem.ID, w.ProjectID(), conf); err != nil {
log.Errorfc(ctx, "geospatialjpv3 webhook: failed to prepare: %v", err)
}
} else {
Expand Down
19 changes: 12 additions & 7 deletions server/cmsintegration/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ func geospatialjpv2Config(conf Config) geospatialjpv2.Config {

func geospatialjpv3Config(conf Config) geospatialjpv3.Config {
return geospatialjpv3.Config{
CMSBase: conf.CMSBaseURL,
CMSToken: conf.CMSToken,
CMSIntegration: conf.CMSIntegration,
CkanBase: conf.CkanBaseURL,
CkanOrg: conf.CkanOrg,
CkanToken: conf.CkanToken,
JobName: conf.GeospatialjpJobName,
CMSBase: conf.CMSBaseURL,
CMSToken: conf.CMSToken,
CMSIntegration: conf.CMSIntegration,
CkanBase: conf.CkanBaseURL,
CkanOrg: conf.CkanOrg,
CkanToken: conf.CkanToken,
BuildType: conf.GeospatialjpBuildType,
CloudRunJobsJobName: conf.GeospatialjpCloudRunJobsJobName,
CloudBuildImage: conf.GeospatialjpCloudBuildImage,
CloudBuildMachineType: conf.GeospatialjpCloudBuildMachineType,
CloudBuildProject: conf.GeospatialjpCloudBuildProject,
CloudBuildRegion: conf.GeospatialjpCloudBuildRegion,
}
}

Expand Down
Loading

0 comments on commit 93d31a3

Please sign in to comment.