Skip to content

Commit

Permalink
Merge pull request #95 from whywaita/feat/94
Browse files Browse the repository at this point in the history
Add "strict" parameter
  • Loading branch information
whywaita authored Aug 26, 2021
2 parents 08917d6 + cfa1453 commit 4d7c105
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/jmoiron/sqlx v1.2.0
github.com/lestrrat-go/jwx v1.2.0 // indirect
github.com/ory/dockertest/v3 v3.6.2
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/prometheus/client_golang v1.10.0 // indirect
github.com/satori/go.uuid v1.2.0
goji.io v2.0.2+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ github.com/pact-foundation/pact-go v1.0.4 h1:OYkFijGHoZAYbOIb1LWXrwKQbMMRUv1oQ89
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/performancecopilot/speed v3.0.0+incompatible h1:2WnRzIquHa5QxaJKShDkLM+sc0JPuwhXzK8OYOyt3Vg=
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type conf struct {
MySQLDSN string
Port int
ShoesPluginPath string
RunnerUser string

RunnerUser string
Debug bool
Debug bool
Strict bool // check to registered runner before delete job
}

// Config Environment keys
Expand All @@ -30,4 +31,5 @@ const (
EnvPort = "PORT"
EnvShoesPluginPath = "PLUGIN"
EnvDebug = "DEBUG"
EnvStrict = "STRICT"
)
5 changes: 5 additions & 0 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func Load() {
} else {
Config.Debug = false
}

Config.Strict = true
if os.Getenv(EnvStrict) == "false" {
Config.Strict = false
}
}

func checkBinary(p string) (string, error) {
Expand Down
21 changes: 20 additions & 1 deletion pkg/gh/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"net/url"
"path"
"strings"
"time"

"github.com/patrickmn/go-cache"

"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v35/github"
Expand All @@ -19,8 +22,16 @@ import (
var (
// ErrNotFound is error for not found
ErrNotFound = fmt.Errorf("not found")

// ResponseCache is cache variable
responseCache *cache.Cache
)

func init() {
c := cache.New(5*time.Minute, 10*time.Minute)
responseCache = c
}

// NewClient create a client of GitHub
func NewClient(ctx context.Context, personalToken, gheDomain string) (*github.Client, error) {
ts := oauth2.StaticTokenSource(
Expand Down Expand Up @@ -96,13 +107,16 @@ func ExistGitHubRunner(ctx context.Context, client *github.Client, owner, repo,

// ListRunners get runners that registered repository or org
func ListRunners(ctx context.Context, client *github.Client, owner, repo string) ([]*github.Runner, error) {
var rs []*github.Runner
if cachedRs, found := responseCache.Get(getCacheKey(owner, repo)); found {
return cachedRs.([]*github.Runner), nil
}

var opts = &github.ListOptions{
Page: 0,
PerPage: 10,
}

var rs []*github.Runner
for {
logger.Logf(true, "get runners from GitHub, page: %d, now all runners: %d", opts.Page, len(rs))
runners, resp, err := listRunners(ctx, client, owner, repo, opts)
Expand All @@ -117,11 +131,16 @@ func ListRunners(ctx context.Context, client *github.Client, owner, repo string)
opts.Page = resp.NextPage
}

responseCache.Set(getCacheKey(owner, repo), rs, 1*time.Second)
logger.Logf(true, "found %d runners", len(rs))

return rs, nil
}

func getCacheKey(owner, repo string) string {
return fmt.Sprintf("owner-%s-repo-%s", owner, repo)
}

func listRunners(ctx context.Context, client *github.Client, owner, repo string, opts *github.ListOptions) (*github.Runners, *github.Response, error) {
if repo == "" {
runners, resp, err := client.Actions.ListOrganizationRunners(ctx, owner, opts)
Expand Down
24 changes: 14 additions & 10 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/whywaita/myshoes/internal/config"

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

"github.com/whywaita/myshoes/pkg/datastore"
Expand Down Expand Up @@ -107,20 +109,22 @@ func (s *Starter) do(ctx context.Context) error {
return
}

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 config.Config.Strict {
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 {
logger.Logf(false, "failed to delete an instance that not registered instance (target ID: %s, cloud ID: %s): %+v\n", job.TargetID, cloudID, err)
// not return, need to update target status if err.
}
if err := deleteInstance(ctx, cloudID); err != nil {
logger.Logf(false, "failed to delete an instance that not registered instance (target ID: %s, cloud ID: %s): %+v\n", job.TargetID, cloudID, err)
// not return, need to update target status if err.
}

if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("cannot register runner to GitHub (job ID: %s)", job.UUID)); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}

if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("cannot register runner to GitHub (job ID: %s)", job.UUID)); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}

return
}

r := datastore.Runner{
Expand Down
23 changes: 21 additions & 2 deletions pkg/web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"github.com/whywaita/myshoes/pkg/logger"
)

type inputConfig struct {
type inputConfigDebug struct {
Debug bool `json:"debug"`
}

type inputConfigStrict struct {
Strict bool `json:"strict"`
}

func handleConfigDebug(w http.ResponseWriter, r *http.Request) {
i := inputConfig{}
i := inputConfigDebug{}

if err := json.NewDecoder(r.Body).Decode(&i); err != nil {
logger.Logf(false, "failed to decode request body: %+v", err)
Expand All @@ -26,3 +30,18 @@ func handleConfigDebug(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
return
}

func handleConfigStrict(w http.ResponseWriter, r *http.Request) {
i := inputConfigStrict{}

if err := json.NewDecoder(r.Body).Decode(&i); err != nil {
logger.Logf(false, "failed to decode request body: %+v", err)
outputErrorMsg(w, http.StatusBadRequest, "json decode error")
return
}

config.Config.Strict = i.Strict
logger.Logf(false, "switch strict mode to %t", i.Strict)
w.WriteHeader(http.StatusNoContent)
return
}
4 changes: 4 additions & 0 deletions pkg/web/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func NewMux(ds datastore.Datastore) *goji.Mux {
apacheLogging(r)
handleConfigDebug(w, r)
})
mux.HandleFunc(pat.Post("/config/strict"), func(w http.ResponseWriter, r *http.Request) {
apacheLogging(r)
handleConfigStrict(w, r)
})

return mux
}
Expand Down

0 comments on commit 4d7c105

Please sign in to comment.