Skip to content
This repository has been archived by the owner on Feb 27, 2020. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petemoore committed Feb 16, 2016
1 parent 118d279 commit 7bef7ba
Show file tree
Hide file tree
Showing 23 changed files with 148 additions and 256 deletions.
30 changes: 14 additions & 16 deletions codegen/go-composite-schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main
import (
"fmt"
"go/build"
"go/format"
"io/ioutil"
"log"
"os"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/kr/text"
"github.com/taskcluster/jsonschema2go"
"github.com/taskcluster/taskcluster-base-go/jsontest"
"golang.org/x/tools/imports"
)

var (
Expand Down Expand Up @@ -105,7 +105,7 @@ func main() {
log.Fatalf("ERROR: Problem assembling content for file '%v': %s", goFile, err)
}
generatedCode = append(generatedCode, []byte("\n"+generateFunctions(ymlFile, j.SubSchema(url).TypeName, schemaProperty, req))...)
sourceCode, err := format.Source([]byte(generatedCode))
sourceCode, err := imports.Process(goFile, []byte(generatedCode), &imports.Options{AllErrors: true})
if err != nil {
log.Fatalf("ERROR: Could not format generated source code for file '%v': %s\nCode:\n%v", goFile, err, string(generatedCode))
}
Expand All @@ -129,21 +129,19 @@ func generateFunctions(ymlFile, goType, schemaProperty string, req bool) string
if err != nil {
log.Fatalf("ERROR: Problem pretty printing json in '%v' - %s", ymlFile, err)
}
result := "func " + goType + `Schema() runtime.CompositeSchema {
schema, err := runtime.NewCompositeSchema(
"` + schemaProperty + `",
[]byte(` + "`\n" + text.Indent(fmt.Sprintf("%v", string(rawJson)), "\t\t") + "\n\t\t`" + `),
`
result := "func " + goType + "Schema() runtime.CompositeSchema {\n"
result += "schema, err := runtime.NewCompositeSchema(\n"
result += "\"" + schemaProperty + "\",\n"
result += "`\n" + text.Indent(fmt.Sprintf("%v", string(rawJson)), "\t\t") + "\n\t\t`" + ",\n"
if req {
result += `true,
`
result += "true,\n"
}
result += `func() interface{} { return &` + goType + `{} },
)
if err != nil {
panic(err)
}
return schema;
}`
result += "func() interface{} { return &" + goType + "{} },\n"
result += ")\n"
result += "if err != nil {\n"
result += "panic(err)\n"
result += "}\n"
result += "return schema\n"
result += "}"
return result
}
4 changes: 2 additions & 2 deletions engines/docker/config-schema.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$schema: http://json-schema.org/draft-04/schema#
title: Config
description: |
description: |-
Config applicable to docker engine
type: object
properties:
rootVolume:
title: Root Volume
description: |
description: |-
Root Volume blah blah
type: string
additionalProperties: false
Expand Down
2 changes: 1 addition & 1 deletion engines/docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate schema-gen
//go:generate go-composite-schema --required config config-schema.yml generated_configschema.go

// comments
package docker
40 changes: 40 additions & 0 deletions engines/docker/generated_configschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package docker

import "github.com/taskcluster/taskcluster-worker/runtime"

type (
Config struct {
RootVolume string `json:"rootVolume"`
}
)

func ConfigSchema() runtime.CompositeSchema {
schema, err := runtime.NewCompositeSchema(
"config",
`
{
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"description": "Config applicable to docker engine",
"properties": {
"rootVolume": {
"description": "Root Volume blah blah",
"title": "Root Volume",
"type": "string"
}
},
"required": [
"rootVolume"
],
"title": "Config",
"type": "object"
}
`,
true,
func() interface{} { return &Config{} },
)
if err != nil {
panic(err)
}
return schema
}
29 changes: 0 additions & 29 deletions engines/docker/generated_functions.go

This file was deleted.

12 changes: 0 additions & 12 deletions engines/docker/generated_types.go

This file was deleted.

10 changes: 5 additions & 5 deletions engines/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type Engine interface {
// from this method.
PayloadSchema() runtime.CompositeSchema

// ConfigSchema returns the json schema that defines the structure of the
// config used by the engine
ConfigSchema() []byte
// ConfigSchema returns the CompositeSchema that represents the engine
// configuration
ConfigSchema() runtime.CompositeSchema

// Capabilities returns a structure declaring which features are supported,
// this is used for up-front feature checking. Unsupport methods must also
Expand Down Expand Up @@ -128,8 +128,8 @@ func (EngineBase) PayloadSchema() runtime.CompositeSchema {

// ConfigSchema returns an empty jsonschema indicating that no custom config is
// required.
func (EngineBase) ConfigSchema() []byte {
return []byte("{}")
func (EngineBase) ConfigSchema() runtime.CompositeSchema {
return runtime.NewEmptyCompositeSchema()
}

// Capabilities returns an zero value Capabilities struct indicating that
Expand Down
43 changes: 0 additions & 43 deletions engines/mock/generated_functions.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
// This source code file is AUTO-GENERATED by github.com/taskcluster/jsonschema2go

package mockengine

import "github.com/taskcluster/taskcluster-worker/runtime"

type (
Payload struct {
Argument string `json:"argument"`

Delay int `json:"delay"`

// Possible values:
// * "true"
// * "false"
// * "set-volume"
// * "get-volume"
// * "ping-proxy"
// * "write-log"
// * "write-error-log"
Function string `json:"function"`
}
)

func PayloadSchema() runtime.CompositeSchema {
schema, err := runtime.NewCompositeSchema(
"p",
[]byte(`
"start",
`
{
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
Expand Down Expand Up @@ -55,7 +47,7 @@ func PayloadSchema() runtime.CompositeSchema {
"title": "Payload",
"type": "object"
}
`),
`,
true,
func() interface{} { return &Payload{} },
)
Expand Down
21 changes: 0 additions & 21 deletions engines/mock/generated_types.go

This file was deleted.

43 changes: 5 additions & 38 deletions engines/mock/mockengine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go-composite-schema --required p payload-schema.yml generatedcode.go
//go:generate go-composite-schema --required start payload-schema.yml generated_payloadschema.go

// Package mockengine implements a MockEngine that doesn't really do anything,
// but allows us to test plugins without having to run a real engine.
Expand Down Expand Up @@ -29,54 +29,21 @@ func init() {
}, "mock")
}

// task.payload.start when engine is "mock"
type payload struct {
Function string `json:"function"`
Argument string `json:"argument"`
Delay int64 `json:"delay"`
}

// mock config contains no fields
func (e engine) ConfigSchema() []byte {
return []byte("{}")
func (e engine) ConfigSchema() runtime.CompositeSchema {
return runtime.NewEmptyCompositeSchema()
}

func (e engine) PayloadSchema() runtime.CompositeSchema {
// Declare the schema for the "task.payload.start" property
schema, err := runtime.NewCompositeSchema("start", `{
"type": "object",
"properties": {
"delay": {"type": "integer"},
"function": {
"type": "string",
"enum": [
"true",
"false",
"set-volume",
"get-volume",
"ping-proxy",
"write-log",
"write-error-log"
]
},
"argument": {"type": "string"}
},
"required": ["delay", "function", "argument"],
"additionalProperties": false
}`, true, func() interface{} { return &payload{} })
if err != nil {
// Any errors here are supposed to be static
panic(err)
}
return schema
return PayloadSchema()
}

func (e engine) NewSandboxBuilder(options engines.SandboxOptions) (engines.SandboxBuilder, error) {
// We know that payload was created with CompositeSchema.Parse() from the
// schema returned by PayloadSchema(), so here we type assert that it is
// indeed a pointer to such a thing.
e.Log.Debug("Building Sandbox")
p, valid := options.Payload.(*payload)
p, valid := options.Payload.(*Payload)
if !valid {
// TODO: Write to some sort of log if the type assertion fails
return nil, engines.ErrContractViolation
Expand Down
2 changes: 1 addition & 1 deletion engines/mock/mocksandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type sandbox struct {
engines.SandboxBuilderBase
engines.SandboxBase
engines.ResultSetBase
payload *payload
payload *Payload
context *runtime.TaskContext
mounts map[string]*mount
proxies map[string]http.Handler
Expand Down
4 changes: 2 additions & 2 deletions engines/winnative/config-schema.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$schema: http://json-schema.org/draft-04/schema#
title: Config
description: |
description: |-
Config applicable to windows native engine
type: object
properties:
usePsExec:
title: Use PSExec
description: |
description: |-
Whether to use PSExec for executing processes
type: boolean
additionalProperties: false
Expand Down
Loading

0 comments on commit 7bef7ba

Please sign in to comment.