Skip to content

Commit

Permalink
Runners (#1439)
Browse files Browse the repository at this point in the history
* refactor(runners): extract jobs to separate entity
  • Loading branch information
fiftin authored Aug 27, 2023
1 parent bb4a9d9 commit b5a99eb
Show file tree
Hide file tree
Showing 16 changed files with 447 additions and 10 deletions.
54 changes: 54 additions & 0 deletions api-docs-runners.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
swagger: '3.0'
info:
title: SEMAPHORE
description: Semaphore Runner API
version: "2.2.0"

host: localhost:3000

consumes:
- application/json
produces:
- application/json
- text/plain; charset=utf-8

tags:
- name: authentication
description: Authentication, Logout & API Tokens
- name: project
description: Everything related to a project
- name: user
description: User-related API

schemes:
- http
- https

basePath: /api/runners

definitions:

paths:
/register:
post:
requestBody:
content:
application/json:
schema:
type: object
required:
- registrationToken
properties:
registrationToken: { type: string }
responses:
200:
description: API Token

/unregister:
post:

/status:
put:

/jobs:
get:
52 changes: 52 additions & 0 deletions api/runners/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package runners

import (
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/mux"
"net/http"
"strings"
)

func RunnerRoute() *mux.Router {
r := mux.NewRouter()

webPath := "/"
if util.WebHostURL != nil {
webPath = util.WebHostURL.Path
if !strings.HasSuffix(webPath, "/") {
webPath += "/"
}
}

pingRouter := r.Path(webPath + "api/runners/register").Subrouter()

pingRouter.Methods("POST", "HEAD").HandlerFunc(registerRunner)

return r
}

func registerRunner(w http.ResponseWriter, r *http.Request) {
var register struct {
RegistrationToken string `json:"registration_token" binding:"required"`
}

if !helpers.Bind(w, r, &register) {
return
}

if register.RegistrationToken != util.Config.RegistrationToken {
return
}

runner, err := helpers.Store(r).CreateRunner(db.Runner{
State: db.RunnerActive,
})

if err != nil {
return
}

helpers.WriteJSON(w, http.StatusOK, runner)
}
25 changes: 25 additions & 0 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/ansible-semaphore/semaphore/services/runners"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(runnerCmd)
}

func runRunner() {

taskPool := runners.JobPool{}

go taskPool.Run()
}

var runnerCmd = &cobra.Command{
Use: "runner",
Short: "Run in runner mode",
Run: func(cmd *cobra.Command, args []string) {
runRunner()
},
}
15 changes: 15 additions & 0 deletions db/Runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package db

type RunnerState string

const (
RunnerOffline RunnerState = "offline"
RunnerActive RunnerState = "active"
)

type Runner struct {
ID int `db:"id" json:"-"`
Token string `db:"token" json:"token"`
ProjectID *int `db:"project_id" json:"project_id"`
State RunnerState `db:"state" json:"state"`
}
16 changes: 16 additions & 0 deletions db/Store.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ type Store interface {
CreateView(view View) (View, error)
DeleteView(projectID int, viewID int) error
SetViewPositions(projectID int, viewPositions map[int]int) error

GetRunner(projectID int, runnerID int) (Runner, error)
GetRunners(projectID int) ([]Runner, error)
DeleteRunner(projectID int, runnerID int) error
GetGlobalRunner(runnerID int) (Runner, error)
GetGlobalRunners() ([]Runner, error)
DeleteGlobalRunner(runnerID int) error
UpdateRunner(runner Runner) error
CreateRunner(runner Runner) (Runner, error)
}

var AccessKeyProps = ObjectProps{
Expand Down Expand Up @@ -304,6 +313,13 @@ var ViewProps = ObjectProps{
DefaultSortingColumn: "position",
}

var RunnerProps = ObjectProps{
TableName: "runner",
Type: reflect.TypeOf(Runner{}),
PrimaryColumnName: "id",
IsGlobal: true,
}

func (p ObjectProps) GetReferringFieldsFrom(t reflect.Type) (fields []string, err error) {
n := t.NumField()
for i := 0; i < n; i++ {
Expand Down
35 changes: 35 additions & 0 deletions db/bolt/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package bolt

import "github.com/ansible-semaphore/semaphore/db"

func (d *BoltDb) GetRunner(projectID int, runnerID int) (runner db.Runner, err error) {
return
}

func (d *BoltDb) GetRunners(projectID int) (runners []db.Runner, err error) {
return
}

func (d *BoltDb) DeleteRunner(projectID int, runnerID int) (err error) {
return
}

func (d *BoltDb) GetGlobalRunner(runnerID int) (runner db.Runner, err error) {
return
}

func (d *BoltDb) GetGlobalRunners() (runners []db.Runner, err error) {
return
}

func (d *BoltDb) DeleteGlobalRunner(runnerID int) (err error) {
return
}

func (d *BoltDb) UpdateRunner(runner db.Runner) (err error) {
return
}

func (d *BoltDb) CreateRunner(runner db.Runner) (newRunner db.Runner, err error) {
return
}
35 changes: 35 additions & 0 deletions db/sql/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sql

import "github.com/ansible-semaphore/semaphore/db"

func (d *SqlDb) GetRunner(projectID int, runnerID int) (runner db.Runner, err error) {
return
}

func (d *SqlDb) GetRunners(projectID int) (runners []db.Runner, err error) {
return
}

func (d *SqlDb) DeleteRunner(projectID int, runnerID int) (err error) {
return
}

func (d *SqlDb) GetGlobalRunner(runnerID int) (runner db.Runner, err error) {
return
}

func (d *SqlDb) GetGlobalRunners() (runners []db.Runner, err error) {
return
}

func (d *SqlDb) DeleteGlobalRunner(runnerID int) (err error) {
return
}

func (d *SqlDb) UpdateRunner(runner db.Runner) (err error) {
return
}

func (d *SqlDb) CreateRunner(runner db.Runner) (newRunner db.Runner, err error) {
return
}
Loading

0 comments on commit b5a99eb

Please sign in to comment.