Skip to content

Commit

Permalink
Merge pull request #112 from whywaita/fix/109
Browse files Browse the repository at this point in the history
Support --ephemeral
  • Loading branch information
whywaita authored Oct 15, 2021
2 parents 25fb1d7 + f64f4cf commit ab232b1
Show file tree
Hide file tree
Showing 19 changed files with 939 additions and 432 deletions.
82 changes: 68 additions & 14 deletions pkg/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/hashicorp/go-version"

uuid "github.com/satori/go.uuid"

"github.com/whywaita/myshoes/pkg/gh"
Expand Down Expand Up @@ -38,7 +40,7 @@ type Datastore interface {
UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, newStatus TargetStatus, description string) error
UpdateToken(ctx context.Context, targetID uuid.UUID, newToken string, newExpiredAt time.Time) error

UpdateResourceType(ctx context.Context, targetID uuid.UUID, newResourceType ResourceType) error
UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType ResourceType, newRunnerVersion, newRunnerUser, newProviderURL sql.NullString) error

EnqueueJob(ctx context.Context, job Job) error
ListJobs(ctx context.Context) ([]Job, error)
Expand Down Expand Up @@ -183,19 +185,22 @@ func (j *Job) RepoURL() string {

// Runner is a runner
type Runner struct {
UUID uuid.UUID `db:"runner_id"`
ShoesType string `db:"shoes_type"`
IPAddress string `db:"ip_address"`
TargetID uuid.UUID `db:"target_id"`
CloudID string `db:"cloud_id"`
Deleted bool `db:"deleted"`
Status RunnerStatus `db:"status"`
ResourceType ResourceType `db:"resource_type"`
RepositoryURL string `db:"repository_url"`
RequestWebhook string `db:"request_webhook"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt sql.NullTime `db:"deleted_at"`
UUID uuid.UUID `db:"runner_id"`
ShoesType string `db:"shoes_type"`
IPAddress string `db:"ip_address"`
TargetID uuid.UUID `db:"target_id"`
CloudID string `db:"cloud_id"`
Deleted bool `db:"deleted"`
Status RunnerStatus `db:"status"`
ResourceType ResourceType `db:"resource_type"`
RunnerUser sql.NullString `db:"runner_user" json:"runner_user"`
RunnerVersion sql.NullString `db:"runner_version" json:"runner_version"`
ProviderURL sql.NullString `db:"provider_url" json:"provider_url"`
RepositoryURL string `db:"repository_url"`
RequestWebhook string `db:"request_webhook"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt sql.NullTime `db:"deleted_at"`
}

// RunnerStatus is status for runner
Expand All @@ -207,3 +212,52 @@ const (
RunnerStatusCompleted = "completed"
RunnerStatusReachHardLimit = "reach_hard_limit"
)

const (
// DefaultRunnerVersion is default value of actions/runner
DefaultRunnerVersion = "v2.275.1"
)

// RunnerTemporaryMode is mode of temporary runner
type RunnerTemporaryMode int

// RunnerEphemeralModes variable
const (
RunnerTemporaryUnknown RunnerTemporaryMode = iota
RunnerTemporaryOnce
RunnerTemporaryEphemeral
)

// StringFlag return flag
func (rtm RunnerTemporaryMode) StringFlag() string {
switch rtm {
case RunnerTemporaryOnce:
return "--once"
case RunnerTemporaryEphemeral:
return "--ephemeral"
}
return "unknown"
}

// GetRunnerTemporaryMode get runner version and RunnerTemporaryMode
func GetRunnerTemporaryMode(runnerVersion sql.NullString) (string, RunnerTemporaryMode, error) {
if !runnerVersion.Valid {
// not set, return default
return DefaultRunnerVersion, RunnerTemporaryOnce, nil
}

ephemeralSupportVersion, err := version.NewVersion("v2.282.0")
if err != nil {
return "", RunnerTemporaryUnknown, fmt.Errorf("failed to parse ephemeral runner version: %w", err)
}

inputVersion, err := version.NewVersion(runnerVersion.String)
if err != nil {
return "", RunnerTemporaryUnknown, fmt.Errorf("failed to parse input runner version: %w", err)
}

if ephemeralSupportVersion.GreaterThan(inputVersion) {
return runnerVersion.String, RunnerTemporaryOnce, nil
}
return runnerVersion.String, RunnerTemporaryEphemeral, nil
}
17 changes: 15 additions & 2 deletions pkg/datastore/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memory

import (
"context"
"database/sql"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -141,8 +142,8 @@ func (m *Memory) UpdateToken(ctx context.Context, targetID uuid.UUID, newToken s
return nil
}

// UpdateResourceType update resource_type in target
func (m *Memory) UpdateResourceType(ctx context.Context, targetID uuid.UUID, newResourceType datastore.ResourceType) error {
// UpdateTargetParam update parameter of target
func (m *Memory) UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType datastore.ResourceType, newRunnerVersion, newRunnerUser, newProviderURL string) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -151,6 +152,18 @@ func (m *Memory) UpdateResourceType(ctx context.Context, targetID uuid.UUID, new
return fmt.Errorf("not found")
}
t.ResourceType = newResourceType
t.RunnerVersion = sql.NullString{
String: newRunnerVersion,
Valid: true,
}
t.RunnerUser = sql.NullString{
String: newRunnerUser,
Valid: true,
}
t.ProviderURL = sql.NullString{
String: newProviderURL,
Valid: true,
}

m.targets[targetID] = t
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/datastore/mysql/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (m *MySQL) CreateRunner(ctx context.Context, runner datastore.Runner) error
return fmt.Errorf("failed to execute INSERT query runners: %w", err)
}

queryDetail := `INSERT INTO runner_detail(runner_id, shoes_type, ip_address, target_id, cloud_id, resource_type, repository_url, request_webhook) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
if _, err := tx.ExecContext(ctx, queryDetail, runner.UUID.String(), runner.ShoesType, runner.IPAddress, runner.TargetID.String(), runner.CloudID, runner.ResourceType, runner.RepositoryURL, runner.RequestWebhook); err != nil {
queryDetail := `INSERT INTO runner_detail(runner_id, shoes_type, ip_address, target_id, cloud_id, resource_type, repository_url, request_webhook, runner_user, runner_version, provider_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
if _, err := tx.ExecContext(ctx, queryDetail, runner.UUID.String(), runner.ShoesType, runner.IPAddress, runner.TargetID.String(), runner.CloudID, runner.ResourceType, runner.RepositoryURL, runner.RequestWebhook, runner.RunnerUser, runner.RunnerVersion, runner.ProviderURL); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute INSERT query runner_detail: %w", err)
}
Expand All @@ -43,7 +43,7 @@ func (m *MySQL) CreateRunner(ctx context.Context, runner datastore.Runner) error
// ListRunners get a not deleted runners
func (m *MySQL) ListRunners(ctx context.Context) ([]datastore.Runner, error) {
var runners []datastore.Runner
query := `SELECT runner.runner_id, detail.shoes_type, detail.ip_address, detail.target_id, detail.cloud_id, detail.created_at, detail.updated_at, detail.resource_type, detail.repository_url, detail.request_webhook
query := `SELECT runner.runner_id, detail.shoes_type, detail.ip_address, detail.target_id, detail.cloud_id, detail.created_at, detail.updated_at, detail.resource_type, detail.repository_url, detail.request_webhook, detail.runner_user, detail.runner_version, detail.provider_url
FROM runners_running AS runner JOIN runner_detail AS detail ON runner.runner_id = detail.runner_id`
err := m.Conn.SelectContext(ctx, &runners, query)
if err != nil {
Expand All @@ -61,7 +61,7 @@ func (m *MySQL) ListRunners(ctx context.Context) ([]datastore.Runner, error) {
func (m *MySQL) GetRunner(ctx context.Context, id uuid.UUID) (*datastore.Runner, error) {
var r datastore.Runner

query := `SELECT runner_id, shoes_type, ip_address, target_id, cloud_id, created_at, updated_at, resource_type, repository_url, request_webhook FROM runner_detail WHERE runner_id = ?`
query := `SELECT runner_id, shoes_type, ip_address, target_id, cloud_id, created_at, updated_at, resource_type, repository_url, request_webhook, runner_user, runner_version, provider_url FROM runner_detail WHERE runner_id = ?`
if err := m.Conn.GetContext(ctx, &r, query, id.String()); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, datastore.ErrNotFound
Expand Down
Loading

0 comments on commit ab232b1

Please sign in to comment.