reorganise runtime
This commit is contained in:
34
runtime/local/build/build.go
Normal file
34
runtime/local/build/build.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Package build builds a micro runtime package
|
||||
package build
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/runtime/local/source"
|
||||
)
|
||||
|
||||
// Builder builds binaries
|
||||
type Builder interface {
|
||||
// Build builds a package
|
||||
Build(*Source) (*Package, error)
|
||||
// Clean deletes the package
|
||||
Clean(*Package) error
|
||||
}
|
||||
|
||||
// Source is the source of a build
|
||||
type Source struct {
|
||||
// Language is the language of code
|
||||
Language string
|
||||
// Location of the source
|
||||
Repository *source.Repository
|
||||
}
|
||||
|
||||
// Package is micro service package
|
||||
type Package struct {
|
||||
// Name of the binary
|
||||
Name string
|
||||
// Location of the binary
|
||||
Path string
|
||||
// Type of binary
|
||||
Type string
|
||||
// Source of the binary
|
||||
Source *Source
|
||||
}
|
93
runtime/local/build/docker/docker.go
Normal file
93
runtime/local/build/docker/docker.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Package docker builds docker images
|
||||
package docker
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
docker "github.com/fsouza/go-dockerclient"
|
||||
"github.com/micro/go-micro/runtime/local/build"
|
||||
"github.com/micro/go-micro/util/log"
|
||||
)
|
||||
|
||||
type Builder struct {
|
||||
Options build.Options
|
||||
Client *docker.Client
|
||||
}
|
||||
|
||||
func (d *Builder) Build(s *build.Source) (*build.Package, error) {
|
||||
image := filepath.Join(s.Repository.Path, s.Repository.Name)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
tw := tar.NewWriter(buf)
|
||||
defer tw.Close()
|
||||
|
||||
dockerFile := "Dockerfile"
|
||||
|
||||
// open docker file
|
||||
f, err := os.Open(filepath.Join(s.Repository.Path, s.Repository.Name, dockerFile))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// read docker file
|
||||
by, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tarHeader := &tar.Header{
|
||||
Name: dockerFile,
|
||||
Size: int64(len(by)),
|
||||
}
|
||||
err = tw.WriteHeader(tarHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = tw.Write(by)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tr := bytes.NewReader(buf.Bytes())
|
||||
|
||||
err = d.Client.BuildImage(docker.BuildImageOptions{
|
||||
Name: image,
|
||||
Dockerfile: dockerFile,
|
||||
InputStream: tr,
|
||||
OutputStream: ioutil.Discard,
|
||||
RmTmpContainer: true,
|
||||
SuppressOutput: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &build.Package{
|
||||
Name: image,
|
||||
Path: image,
|
||||
Type: "docker",
|
||||
Source: s,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *Builder) Clean(b *build.Package) error {
|
||||
image := filepath.Join(b.Path, b.Name)
|
||||
return d.Client.RemoveImage(image)
|
||||
}
|
||||
|
||||
func NewBuilder(opts ...build.Option) build.Builder {
|
||||
options := build.Options{}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
endpoint := "unix:///var/run/docker.sock"
|
||||
client, err := docker.NewClient(endpoint)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &Builder{
|
||||
Options: options,
|
||||
Client: client,
|
||||
}
|
||||
}
|
70
runtime/local/build/go/golang.go
Normal file
70
runtime/local/build/go/golang.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Package golang is a go package manager
|
||||
package golang
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/micro/go-micro/runtime/local/build"
|
||||
)
|
||||
|
||||
type Builder struct {
|
||||
Options build.Options
|
||||
Cmd string
|
||||
Path string
|
||||
}
|
||||
|
||||
// whichGo locates the go command
|
||||
func whichGo() string {
|
||||
// check GOROOT
|
||||
if gr := os.Getenv("GOROOT"); len(gr) > 0 {
|
||||
return filepath.Join(gr, "bin", "go")
|
||||
}
|
||||
|
||||
// check path
|
||||
for _, p := range filepath.SplitList(os.Getenv("PATH")) {
|
||||
bin := filepath.Join(p, "go")
|
||||
if _, err := os.Stat(bin); err == nil {
|
||||
return bin
|
||||
}
|
||||
}
|
||||
|
||||
// best effort
|
||||
return "go"
|
||||
}
|
||||
|
||||
func (g *Builder) Build(s *build.Source) (*build.Package, error) {
|
||||
binary := filepath.Join(g.Path, s.Repository.Name)
|
||||
source := filepath.Join(s.Repository.Path, s.Repository.Name)
|
||||
|
||||
cmd := exec.Command(g.Cmd, "build", "-o", binary, source)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &build.Package{
|
||||
Name: s.Repository.Name,
|
||||
Path: binary,
|
||||
Type: "go",
|
||||
Source: s,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Builder) Clean(b *build.Package) error {
|
||||
binary := filepath.Join(b.Path, b.Name)
|
||||
return os.Remove(binary)
|
||||
}
|
||||
|
||||
func NewBuild(opts ...build.Option) build.Builder {
|
||||
options := build.Options{
|
||||
Path: os.TempDir(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &Builder{
|
||||
Options: options,
|
||||
Cmd: whichGo(),
|
||||
Path: options.Path,
|
||||
}
|
||||
}
|
15
runtime/local/build/options.go
Normal file
15
runtime/local/build/options.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package build
|
||||
|
||||
type Options struct {
|
||||
// local path to download source
|
||||
Path string
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
|
||||
// Local path for repository
|
||||
func Path(p string) Option {
|
||||
return func(o *Options) {
|
||||
o.Path = p
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user