Skip to content

Commit

Permalink
Merge pull request #93 from whywaita/fix/92
Browse files Browse the repository at this point in the history
Add columns to runner_detail
  • Loading branch information
whywaita authored Aug 26, 2021
2 parents dcafa8c + 6b29fd3 commit 08917d6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 47 deletions.
40 changes: 30 additions & 10 deletions pkg/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,38 @@ type Job struct {
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
}

// RepoURL return repository URL that send webhook.
func (j *Job) RepoURL() string {
serverURL := "https://github.com"
if j.GHEDomain.Valid {
serverURL = j.GHEDomain.String
}

s := strings.Split(serverURL, "://")

var u url.URL
u.Scheme = s[0]
u.Host = s[1]
u.Path = j.Repository

return u.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"`
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"`
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 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) VALUES (?, ?, ?, ?, ?)`
if _, err := tx.ExecContext(ctx, queryDetail, runner.UUID.String(), runner.ShoesType, runner.IPAddress, runner.TargetID.String(), runner.CloudID); err != nil {
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 {
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
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
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 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 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
18 changes: 10 additions & 8 deletions pkg/datastore/mysql/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (

var testRunnerID = uuid.FromStringOrNil("7943e412-c0ae-4068-ab24-3e71a13fbe53")
var testRunner = datastore.Runner{
UUID: testRunnerID,
ShoesType: "shoes-test",
IPAddress: "",
TargetID: testTargetID,
CloudID: "mycloud-uuid",
UUID: testRunnerID,
ShoesType: "shoes-test",
TargetID: testTargetID,
CloudID: "mycloud-uuid",
ResourceType: datastore.ResourceTypeNano,
RepositoryURL: "https://github.com/octocat/Hello-World",
RequestWebhook: "{}",
}

func TestMySQL_CreateRunner(t *testing.T) {
Expand Down Expand Up @@ -289,7 +291,7 @@ func TestMySQL_DeleteRunner(t *testing.T) {

func getRunnerFromSQL(testDB *sqlx.DB, 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 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 FROM runner_detail WHERE runner_id = ?`
stmt, err := testDB.Preparex(query)
if err != nil {
return nil, fmt.Errorf("failed to prepare: %w", err)
Expand All @@ -303,7 +305,7 @@ func getRunnerFromSQL(testDB *sqlx.DB, id uuid.UUID) (*datastore.Runner, error)

func getRunningRunnerFromSQL(testDB *sqlx.DB, id uuid.UUID) (*datastore.Runner, error) {
var r datastore.Runner
query := `SELECT detail.runner_id, shoes_type, ip_address, target_id, cloud_id, detail.created_at, updated_at
query := `SELECT detail.runner_id, shoes_type, ip_address, target_id, cloud_id, detail.created_at, updated_at, detail.resource_type, detail.repository_url, detail.request_webhook
FROM runner_detail AS detail JOIN runnesr_running AS running ON detail.runner_id = running.runner_id WHERE detail.runner_id = ?`
stmt, err := testDB.Preparex(query)
if err != nil {
Expand All @@ -318,7 +320,7 @@ FROM runner_detail AS detail JOIN runnesr_running AS running ON detail.runner_id

func getDeletedRunnerFromSQL(testDB *sqlx.DB, id uuid.UUID) (*datastore.Runner, error) {
var r datastore.Runner
query := `SELECT detail.runner_id, shoes_type, ip_address, target_id, cloud_id, detail.created_at, updated_at
query := `SELECT detail.runner_id, shoes_type, ip_address, target_id, cloud_id, detail.created_at, updated_at, detail.resource_type, detail.repository_url, detail.request_webhook
FROM runner_detail AS detail JOIN runners_deleted AS deleted ON detail.runner_id = deleted.runner_id WHERE detail.runner_id = ?`
stmt, err := testDB.Preparex(query)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/datastore/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ CREATE TABLE `runner_detail` (
`ip_address` VARCHAR(255) NOT NULL,
`target_id` VARCHAR(36) NOT NULL,
`cloud_id` TEXT NOT NULL,
`resource_type` ENUM('nano', 'micro', 'small', 'medium', 'large', 'xlarge', '2xlarge', '3xlarge', '4xlarge') NOT NULL,
`repository_url` VARCHAR(255) NOT NULL,
`request_webhook` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT current_timestamp,
`updated_at` TIMESTAMP NOT NULL DEFAULT current_timestamp ON UPDATE current_timestamp,
KEY `fk_runner_target_id` (`target_id`),
Expand Down
47 changes: 24 additions & 23 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

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

"github.com/whywaita/myshoes/pkg/datastore"
"github.com/whywaita/myshoes/pkg/gh"
"github.com/whywaita/myshoes/pkg/logger"
Expand Down Expand Up @@ -87,7 +89,13 @@ func (s *Starter) do(ctx context.Context) error {
return
}

cloudID, ipAddress, shoesType, err := s.bung(ctx, job)
target, err := s.ds.GetTarget(ctx, job.TargetID)
if err != nil {
logger.Logf(false, "failed to retrieve relational target: (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}

cloudID, ipAddress, shoesType, err := s.bung(ctx, job.UUID, *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 @@ -99,7 +107,7 @@ func (s *Starter) do(ctx context.Context) error {
return
}

if err := s.checkRegisteredRunner(ctx, job, cloudID); err != nil {
if err := s.checkRegisteredRunner(ctx, cloudID, *target); err != nil {
logger.Logf(false, "failed to check to register runner (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)

if err := deleteInstance(ctx, cloudID); err != nil {
Expand All @@ -116,11 +124,14 @@ func (s *Starter) do(ctx context.Context) error {
}

r := datastore.Runner{
UUID: job.UUID,
ShoesType: shoesType,
IPAddress: ipAddress,
TargetID: job.TargetID,
CloudID: cloudID,
UUID: job.UUID,
ShoesType: shoesType,
IPAddress: ipAddress,
TargetID: job.TargetID,
CloudID: cloudID,
ResourceType: target.ResourceType,
RepositoryURL: job.RepoURL(),
RequestWebhook: job.CheckEventJSON,
}
if err := s.ds.CreateRunner(ctx, r); err != nil {
logger.Logf(false, "failed to save runner to datastore (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
Expand Down Expand Up @@ -152,31 +163,26 @@ func (s *Starter) do(ctx context.Context) error {
}

// bung is start runner, like a pistol! :)
func (s *Starter) bung(ctx context.Context, job datastore.Job) (string, string, string, error) {
logger.Logf(false, "start create instance (job: %s)", job.UUID)
func (s *Starter) bung(ctx context.Context, jobUUID uuid.UUID, target datastore.Target) (string, string, string, error) {
logger.Logf(false, "start create instance (job: %s)", jobUUID)
client, teardown, err := shoes.GetClient()
if err != nil {
return "", "", "", fmt.Errorf("failed to get plugin client: %w", err)
}
defer teardown()

target, err := s.ds.GetTarget(ctx, job.TargetID)
if err != nil {
return "", "", "", fmt.Errorf("failed to retrieve relational target: %w", err)
}

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

runnerName := runner.ToName(job.UUID.String())
runnerName := runner.ToName(jobUUID.String())
cloudID, ipAddress, shoesType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType)
if err != nil {
return "", "", "", fmt.Errorf("failed to add instance: %w", err)
}

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

return cloudID, ipAddress, shoesType, nil
}
Expand All @@ -197,12 +203,7 @@ func deleteInstance(ctx context.Context, cloudID string) error {
}

// checkRegisteredRunner check to register runner to GitHub
func (s *Starter) checkRegisteredRunner(ctx context.Context, job datastore.Job, cloudID string) error {
target, err := s.ds.GetTarget(ctx, job.TargetID)
if err != nil {
return fmt.Errorf("failed to retrieve relational target: %w", err)
}

func (s *Starter) checkRegisteredRunner(ctx context.Context, cloudID string, target datastore.Target) error {
client, err := gh.NewClient(ctx, target.GitHubToken, target.GHEDomain.String)
if err != nil {
return fmt.Errorf("failed to create github client: %w", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func validateUpdateTarget(old, new *datastore.Target) error {

// generated
t.Status = ""
t.StatusDescription = sql.NullString{}
t.GitHubToken = ""
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/web/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func receiveCheckRunWebhook(ctx context.Context, event *github.CheckRunEvent, ds
action := event.GetAction()
installationID := event.GetInstallation().GetID()

repoName := *(event.Repo.FullName)
repoURL := *(event.Repo.HTMLURL)
repo := event.GetRepo()
repoName := repo.GetFullName()
repoURL := repo.GetHTMLURL()

jb, err := json.Marshal(event)
if err != nil {
Expand Down

0 comments on commit 08917d6

Please sign in to comment.