Commit 792784d0 authored by Kamil Trzciński's avatar Kamil Trzciński 🔴

WIP

parent 0a7cbc09
package main
type Build struct {
GetBuildResponse
}
func (b *Build) Run() {
}
package main
import (
"os"
"path"
"fmt"
"github.com/codegangsta/cli"
log "github.com/Sirupsen/logrus"
"github.com/ayufan/gitlab-ci-multi-runner/src"
)
func main() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a GitLab-CI Multi Runner"
app.Version = "0.1.0"
app.Author = ""
app.Email = ""
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: fmt.Sprintf("Log level (options: debug, info, warn, error, fatal, panic)"),
},
}
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "run",
ShortName: "r",
Usage: "start single runner",
Flags: []cli.Flag{flToken, flURL},
Action: run,
},
{
Name: "setup",
ShortName: "s",
Usage: "setup a new runner",
Flags: []cli.Flag{flRegistrationToken, flURL, flHostname},
Action: setup,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
src.Main()
}
package main
import (
"time"
"github.com/codegangsta/cli"
)
func run(c *cli.Context) {
runner_config := RunnerConfig{
URL: flURL.Value,
Token: flToken.Value,
}
for {
new_build := GetBuild(&runner_config)
if new_build != nil {
time.Sleep(3 * time.Second)
continue
}
build := Build{new_build}
build.Run()
}
}
package src
import (
"bufio"
"os"
"io/ioutil"
)
type Build struct {
GetBuildResponse
}
func (b *Build) Generate() *string {
file, err := ioutil.TempFile("", "build_script")
if err != nil {
return nil
}
os.Chmod(file.Name(), os.ModePerm & 0700)
w := bufio.NewWriter(file)
defer w.Flush()
w.WriteString("#!/usr/bin/env bash\n")
w.WriteString("\n")
w.WriteString("echo Using $(hostname)\n")
w.WriteString("\n")
w.WriteString("set -e\n")
w.WriteString("trap 'kill -s INT 0' EXIT\n")
w.WriteString("\n")
w.WriteString(b.Commands)
name := file.Name()
return &name
}
package src
import (
"os"
"path"
"github.com/codegangsta/cli"
log "github.com/Sirupsen/logrus"
)
func Main() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Usage = "a GitLab-CI Multi Runner"
app.Version = "0.1.0"
app.Author = "Kamil Trzciński"
app.Email = "ayufan@ayufan.eu"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "debug mode",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "log-level, l",
Value: "info",
Usage: "Log level (options: debug, info, warn, error, fatal, panic)",
},
}
// logs
app.Before = func(c *cli.Context) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatalf(err.Error())
}
log.SetLevel(level)
// If a log level wasn't specified and we are running in debug mode,
// enforce log-level=debug.
if !c.IsSet("log-level") && !c.IsSet("l") && c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "run",
ShortName: "r",
Usage: "start single runner",
Flags: []cli.Flag{flToken, flURL},
Action: run,
},
{
Name: "setup",
ShortName: "s",
Usage: "setup a new runner",
Flags: []cli.Flag{flRegistrationToken, flURL, flHostname},
Action: setup,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
package main
package src
type RunnerConfig struct {
Name string `json:"name"`
URL string `json:"url"`
Token string `json:"token"`
Limit int `json:"limit"`
Executor string `json:"executor"`
}
type Config struct {
......
package src
import (
)
type Executor interface {
Run(config RunnerConfig, build Build) error
}
func GetExecutor(config RunnerConfig) Executor {
switch config.Executor {
case "shell":
return &ShellExecutor{}
case "":
return &ShellExecutor{}
default:
return nil
}
}
package main
package src
import (
"github.com/codegangsta/cli"
......
package main
package src
import (
"encoding/json"
......@@ -133,7 +133,7 @@ func getUrl(baseURL string, request string, a ...interface{}) string {
return fmt.Sprintf("%s/api/v1/%s", baseURL, fmt.Sprintf(request, a...));
}
func GetBuild(config *RunnerConfig) *GetBuildResponse {
func GetBuild(config RunnerConfig) *GetBuildResponse {
request := GetBuildRequest{
Token: config.Token,
}
......@@ -148,7 +148,7 @@ func GetBuild(config *RunnerConfig) *GetBuildResponse {
}
}
func RegisterRunner(config *RunnerConfig) *RegisterRunnerResponse {
func RegisterRunner(config RunnerConfig) *RegisterRunnerResponse {
request := RegisterRunnerRequest{
Token: config.Token,
Hostname: config.Name,
......@@ -163,7 +163,7 @@ func RegisterRunner(config *RunnerConfig) *RegisterRunnerResponse {
}
}
func UpdateBuild(config *RunnerConfig, id int, state BuildState, trace io.Reader) UpdateState {
func UpdateBuild(config RunnerConfig, id int, state BuildState, trace io.Reader) UpdateState {
data, err := readPayload(trace)
if err != nil {
return UpdateFailed
......
package src
import (
"errors"
"time"
"bytes"
"github.com/codegangsta/cli"
)
func failBuild(config RunnerConfig, build Build, err error) {
for {
error_buffer := bytes.NewBufferString(err.Error())
result := UpdateBuild(config, build.Id, Failed, error_buffer)
switch result {
case UpdateSucceeded:
return
case UpdateAbort:
return
case UpdateFailed:
time.Sleep(3 * time.Second)
continue
}
}
}
func run(c *cli.Context) {
runner_config := RunnerConfig{
URL: flURL.Value,
Token: flToken.Value,
}
for {
new_build := GetBuild(runner_config)
if new_build == nil {
time.Sleep(3 * time.Second)
continue
}
build := Build{*new_build}
executor := GetExecutor(runner_config)
if executor == nil {
go failBuild(runner_config, build, errors.New("couldn't get executor"))
continue
}
err := executor.Run(runner_config, build)
if err != nil {
go failBuild(runner_config, build, err)
continue
}
}
}
package main
package src
import (
"log"
......@@ -62,7 +62,7 @@ func setup(c *cli.Context) {
runner_config.Token = strings.TrimSpace(runner_config.Token)
}
result := RegisterRunner(&runner_config)
result := RegisterRunner(runner_config)
if result == nil {
log.Fatalf("Failed to register this runner. Perhaps your SSH key is invalid or you are having network problems");
}
......
package src
import (
)
type ShellExecutor struct {
}
func (s *ShellExecutor) Run(config RunnerConfig, build Build) error {
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment