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
|
||||
}
|
||||
}
|
5
runtime/local/process/options.go
Normal file
5
runtime/local/process/options.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package process
|
||||
|
||||
type Options struct{}
|
||||
|
||||
type Option func(o *Options)
|
100
runtime/local/process/os/os.go
Normal file
100
runtime/local/process/os/os.go
Normal file
@@ -0,0 +1,100 @@
|
||||
// +build !windows
|
||||
|
||||
// Package os runs processes locally
|
||||
package os
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/micro/go-micro/runtime/local/process"
|
||||
)
|
||||
|
||||
func (p *Process) Exec(exe *process.Executable) error {
|
||||
cmd := exec.Command(exe.Package.Path)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (p *Process) Fork(exe *process.Executable) (*process.PID, error) {
|
||||
// create command
|
||||
cmd := exec.Command(exe.Package.Path, exe.Args...)
|
||||
// set env vars
|
||||
cmd.Env = append(cmd.Env, exe.Env...)
|
||||
|
||||
// create process group
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
|
||||
in, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
er, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// start the process
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &process.PID{
|
||||
ID: fmt.Sprintf("%d", cmd.Process.Pid),
|
||||
Input: in,
|
||||
Output: out,
|
||||
Error: er,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Process) Kill(pid *process.PID) error {
|
||||
id, err := strconv.Atoi(pid.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pr, err := os.FindProcess(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// now kill it
|
||||
err = pr.Kill()
|
||||
|
||||
// kill the group
|
||||
if pgid, err := syscall.Getpgid(id); err == nil {
|
||||
syscall.Kill(-pgid, syscall.SIGKILL)
|
||||
}
|
||||
|
||||
// return the kill error
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) Wait(pid *process.PID) error {
|
||||
id, err := strconv.Atoi(pid.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pr, err := os.FindProcess(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ps, err := pr.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ps.Success() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(ps.String())
|
||||
}
|
89
runtime/local/process/os/os_windows.go
Normal file
89
runtime/local/process/os/os_windows.go
Normal file
@@ -0,0 +1,89 @@
|
||||
// Package os runs processes locally
|
||||
package os
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
"github.com/micro/go-micro/runtime/process"
|
||||
)
|
||||
|
||||
func (p *Process) Exec(exe *process.Executable) error {
|
||||
cmd := exec.Command(exe.Package.Path)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (p *Process) Fork(exe *process.Executable) (*process.PID, error) {
|
||||
// create command
|
||||
cmd := exec.Command(exe.Package.Path, exe.Args...)
|
||||
// set env vars
|
||||
cmd.Env = append(cmd.Env, exe.Env...)
|
||||
|
||||
in, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
er, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// start the process
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &process.PID{
|
||||
ID: fmt.Sprintf("%d", cmd.Process.Pid),
|
||||
Input: in,
|
||||
Output: out,
|
||||
Error: er,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Process) Kill(pid *process.PID) error {
|
||||
id, err := strconv.Atoi(pid.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pr, err := os.FindProcess(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// now kill it
|
||||
err = pr.Kill()
|
||||
|
||||
// return the kill error
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) Wait(pid *process.PID) error {
|
||||
id, err := strconv.Atoi(pid.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pr, err := os.FindProcess(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ps, err := pr.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ps.Success() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(ps.String())
|
||||
}
|
12
runtime/local/process/os/process.go
Normal file
12
runtime/local/process/os/process.go
Normal file
@@ -0,0 +1,12 @@
|
||||
// Package os runs processes locally
|
||||
package os
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/runtime/local/process"
|
||||
)
|
||||
|
||||
type Process struct{}
|
||||
|
||||
func NewProcess(opts ...process.Option) process.Process {
|
||||
return &Process{}
|
||||
}
|
41
runtime/local/process/process.go
Normal file
41
runtime/local/process/process.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Package process executes a binary
|
||||
package process
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/micro/go-micro/runtime/local/build"
|
||||
)
|
||||
|
||||
// Process manages a running process
|
||||
type Process interface {
|
||||
// Executes a process to completion
|
||||
Exec(*Executable) error
|
||||
// Creates a new process
|
||||
Fork(*Executable) (*PID, error)
|
||||
// Kills the process
|
||||
Kill(*PID) error
|
||||
// Waits for a process to exit
|
||||
Wait(*PID) error
|
||||
}
|
||||
|
||||
type Executable struct {
|
||||
// Package containing executable
|
||||
Package *build.Package
|
||||
// The env variables
|
||||
Env []string
|
||||
// Args to pass
|
||||
Args []string
|
||||
}
|
||||
|
||||
// PID is the running process
|
||||
type PID struct {
|
||||
// ID of the process
|
||||
ID string
|
||||
// Stdin
|
||||
Input io.Writer
|
||||
// Stdout
|
||||
Output io.Reader
|
||||
// Stderr
|
||||
Error io.Reader
|
||||
}
|
87
runtime/local/source/git/git.go
Normal file
87
runtime/local/source/git/git.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Package git provides a git source
|
||||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-micro/runtime/local/source"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
)
|
||||
|
||||
// Source retrieves source code
|
||||
// An empty struct can be used
|
||||
type Source struct {
|
||||
Options source.Options
|
||||
}
|
||||
|
||||
func (g *Source) Fetch(url string) (*source.Repository, error) {
|
||||
purl := url
|
||||
|
||||
if parts := strings.Split(url, "://"); len(parts) > 1 {
|
||||
purl = parts[len(parts)-1]
|
||||
}
|
||||
|
||||
name := filepath.Base(url)
|
||||
path := filepath.Join(g.Options.Path, purl)
|
||||
|
||||
_, err := git.PlainClone(path, false, &git.CloneOptions{
|
||||
URL: url,
|
||||
})
|
||||
if err == nil {
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// repo already exists
|
||||
if err != git.ErrRepositoryAlreadyExists {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// open repo
|
||||
re, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// update it
|
||||
if err := re.Fetch(nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Source) Commit(r *source.Repository) error {
|
||||
repo := filepath.Join(r.Path)
|
||||
re, err := git.PlainOpen(repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return re.Push(nil)
|
||||
}
|
||||
|
||||
func (g *Source) String() string {
|
||||
return "git"
|
||||
}
|
||||
|
||||
func NewSource(opts ...source.Option) *Source {
|
||||
options := source.Options{
|
||||
Path: os.TempDir(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &Source{
|
||||
Options: options,
|
||||
}
|
||||
}
|
93
runtime/local/source/go/golang.go
Normal file
93
runtime/local/source/go/golang.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Package golang is a source for Go
|
||||
package golang
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-micro/runtime/local/source"
|
||||
)
|
||||
|
||||
type Source struct {
|
||||
Options source.Options
|
||||
// Go Command
|
||||
Cmd string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (g *Source) Fetch(url string) (*source.Repository, error) {
|
||||
purl := url
|
||||
|
||||
if parts := strings.Split(url, "://"); len(parts) > 1 {
|
||||
purl = parts[len(parts)-1]
|
||||
}
|
||||
|
||||
// name of repo
|
||||
name := filepath.Base(url)
|
||||
// local path of repo
|
||||
path := filepath.Join(g.Path, purl)
|
||||
args := []string{"get", "-d", url, path}
|
||||
|
||||
cmd := exec.Command(g.Cmd, args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &source.Repository{
|
||||
Name: name,
|
||||
Path: path,
|
||||
URL: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Commit is not yet supported
|
||||
func (g *Source) Commit(r *source.Repository) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Source) String() string {
|
||||
return "golang"
|
||||
}
|
||||
|
||||
// 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 NewSource(opts ...source.Option) source.Source {
|
||||
options := source.Options{
|
||||
Path: os.TempDir(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
cmd := whichGo()
|
||||
path := options.Path
|
||||
|
||||
// point of no return
|
||||
if len(cmd) == 0 {
|
||||
panic("Could not find Go executable")
|
||||
}
|
||||
|
||||
return &Source{
|
||||
Options: options,
|
||||
Cmd: cmd,
|
||||
Path: path,
|
||||
}
|
||||
}
|
15
runtime/local/source/options.go
Normal file
15
runtime/local/source/options.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package source
|
||||
|
||||
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
|
||||
}
|
||||
}
|
22
runtime/local/source/source.go
Normal file
22
runtime/local/source/source.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Package source retrieves source code
|
||||
package source
|
||||
|
||||
// Source retrieves source code
|
||||
type Source interface {
|
||||
// Fetch repo from a url
|
||||
Fetch(url string) (*Repository, error)
|
||||
// Commit and upload repo
|
||||
Commit(*Repository) error
|
||||
// The sourcerer
|
||||
String() string
|
||||
}
|
||||
|
||||
// Repository is the source repository
|
||||
type Repository struct {
|
||||
// Name or repo
|
||||
Name string
|
||||
// Local path where repo is stored
|
||||
Path string
|
||||
// URL from which repo was retrieved
|
||||
URL string
|
||||
}
|
Reference in New Issue
Block a user