Skip to content

Commit

Permalink
Merge pull request #169 from site0801/fix/add-instance-responce
Browse files Browse the repository at this point in the history
Add resource_type in AddInstanceResponse
  • Loading branch information
whywaita authored Mar 20, 2023
2 parents 8d9faf2 + 5a64181 commit 3b4ee0b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 71 deletions.
117 changes: 65 additions & 52 deletions api/proto.go/myshoes.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions api/proto.go/myshoes_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/proto/myshoes.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ message AddInstanceResponse {
string cloud_id = 1;
string shoes_type = 2;
string ip_address = 3;
ResourceType resource_type = 4;
}

message DeleteInstanceRequest {
Expand Down
8 changes: 4 additions & 4 deletions pkg/shoes/shoes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *g

// Client is plugin client interface
type Client interface {
AddInstance(ctx context.Context, runnerID, setupScript string, resourceType datastore.ResourceType, labels []string) (string, string, string, error)
AddInstance(ctx context.Context, runnerID, setupScript string, resourceType datastore.ResourceType, labels []string) (string, string, string, datastore.ResourceType, error)
DeleteInstance(ctx context.Context, cloudID string, labels []string) error
}

Expand All @@ -79,7 +79,7 @@ type GRPCClient struct {
}

// AddInstance create instance for runner
func (c *GRPCClient) AddInstance(ctx context.Context, runnerName, setupScript string, resourceType datastore.ResourceType, labels []string) (string, string, string, error) {
func (c *GRPCClient) AddInstance(ctx context.Context, runnerName, setupScript string, resourceType datastore.ResourceType, labels []string) (string, string, string, datastore.ResourceType, error) {
req := &pb.AddInstanceRequest{
RunnerName: runnerName,
SetupScript: setupScript,
Expand All @@ -88,10 +88,10 @@ func (c *GRPCClient) AddInstance(ctx context.Context, runnerName, setupScript st
}
resp, err := c.client.AddInstance(ctx, req)
if err != nil {
return "", "", "", fmt.Errorf("failed to AddInstance: %w", err)
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to AddInstance: %w", err)
}

return resp.CloudId, resp.IpAddress, resp.ShoesType, nil
return resp.CloudId, resp.IpAddress, resp.ShoesType, datastore.UnmarshalResourceType(resp.ResourceType), nil
}

// DeleteInstance delete instance for runner
Expand Down
21 changes: 12 additions & 9 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {

cctx, cancel := context.WithTimeout(ctx, runner.MustRunningTime)
defer cancel()
cloudID, ipAddress, shoesType, err := s.bung(cctx, job, *target)
cloudID, ipAddress, shoesType, resourceType, err := s.bung(cctx, job, *target)
if err != nil {
logger.Logf(false, "failed to bung (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)

Expand All @@ -170,6 +170,9 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {

return fmt.Errorf("failed to bung (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err)
}
if resourceType == datastore.ResourceTypeUnknown {
resourceType = target.ResourceType
}

runnerName := runner.ToName(job.UUID.String())
if config.Config.Strict {
Expand All @@ -195,7 +198,7 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {
IPAddress: ipAddress,
TargetID: job.TargetID,
CloudID: cloudID,
ResourceType: target.ResourceType,
ResourceType: resourceType,
RunnerUser: sql.NullString{
String: config.Config.RunnerUser,
Valid: true,
Expand Down Expand Up @@ -228,34 +231,34 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {
}

// bung is start runner, like a pistol! :)
func (s *Starter) bung(ctx context.Context, job datastore.Job, target datastore.Target) (string, string, string, error) {
func (s *Starter) bung(ctx context.Context, job datastore.Job, target datastore.Target) (string, string, string, datastore.ResourceType, error) {
logger.Logf(false, "start create instance (job: %s)", job.UUID)
runnerName := runner.ToName(job.UUID.String())

script, err := s.getSetupScript(ctx, target, runnerName)
if err != nil {
return "", "", "", fmt.Errorf("failed to get setup scripts: %w", err)
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to get setup scripts: %w", err)
}

client, teardown, err := shoes.GetClient()
if err != nil {
return "", "", "", fmt.Errorf("failed to get plugin client: %w", err)
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to get plugin client: %w", err)
}
defer teardown()

labels, err := gh.ExtractRunsOnLabels([]byte(job.CheckEventJSON))
if err != nil {
return "", "", "", fmt.Errorf("failed to extract labels: %w", err)
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to extract labels: %w", err)
}

cloudID, ipAddress, shoesType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType, labels)
cloudID, ipAddress, shoesType, resourceType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType, labels)
if err != nil {
return "", "", "", fmt.Errorf("failed to add instance: %w", err)
return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to add instance: %w", err)
}

logger.Logf(false, "instance create successfully! (job: %s, cloud ID: %s)", job.UUID, cloudID)

return cloudID, ipAddress, shoesType, nil
return cloudID, ipAddress, shoesType, resourceType, nil
}

func deleteInstance(ctx context.Context, cloudID, checkEventJSON string) error {
Expand Down

0 comments on commit 3b4ee0b

Please sign in to comment.