luisf 8 gadi atpakaļ
vecāks
revīzija
3adb7fba41

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+deps
 .deps
 bin
 pkg

+ 4 - 6
Makefile

@@ -1,17 +1,15 @@
 
 
-GOPATH=${CURDIR}/.deps:/home/stdio/go:${CURDIR}
+GOPATH=${CURDIR}/deps:${CURDIR}
 
 
-all: .deps
+all: deps
 	go build -o DIST/builder buildme/cmd/builder
 
-deps: .deps
-
 dist: 
 	tar -c DIST |gzip >dist.tar.gz
 
-.deps:
+deps:
 	go get -v buildme/cmd/builder
 
 clean:
@@ -24,4 +22,4 @@ distclean: clean
 	rm -rf pkg
 
 
-.PHONY: all deps clean distclean
+.PHONY: all clean distclean

+ 1 - 1
env.sh

@@ -1 +1 @@
-export GOPATH=$(pwd)/.deps:$(pwd)
+export GOPATH=$(pwd)/deps:$(pwd)

+ 3 - 6
src/buildme/buildme.go

@@ -65,7 +65,7 @@ func Prepare(p *Project) {
 		log.Println("Error parsing yaml:", err)
 	}
 	p.SetConf(projConf)
-	p.Name = projConf.Meta.Project
+	//p.Name = projConf.Meta.Project
 	log.Println("ProjConf:", projConf)
 }
 
@@ -82,7 +82,7 @@ func Fetch(ftype string, fpath string) (*Project, error) {
 
 // Build Fetch url/path and run docker build
 ///////////////////////////////////////////////////////////////// BUILD
-func Build(ftype string, fpath string, projName string) error {
+/*func Build(ftype string, fpath string, projName string) error {
 	var err error
 	// Fetch proj from tar
 	proj, err := Fetch(ftype, fpath)
@@ -91,12 +91,9 @@ func Build(ftype string, fpath string, projName string) error {
 	}
 	defer proj.Close()
 	Prepare(proj)
-	if projName != "" {
-		proj.SetName(projName)
-	}
 
 	return DockerBuild(proj)
-}
+}*/
 
 func mexec(cmdname string, args ...string) {
 	cmd := exec.Command(cmdname, args...)

+ 2 - 1
src/buildme/cmd/builder/main.go

@@ -7,6 +7,7 @@ import (
 
 	_ "buildme/fetcher/git"
 	_ "buildme/fetcher/path"
+	"buildme/utils"
 
 	"dev.hexasoftware.com/hxs/prettylog"
 )
@@ -17,7 +18,7 @@ var (
 
 func main() {
 
-	strings.Fields("make \"myself and I \"")
+	log.Println("Test:", strings.Join(utils.ParseField("make \"myself and I \""), "\n"))
 	var fetchType string
 	var fetchPath string
 	var fetchName string

+ 3 - 17
src/buildme/docker.go

@@ -1,16 +1,6 @@
 package buildme
 
-import (
-	"context"
-	"fmt"
-	"io"
-	"os"
-
-	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/api/types/container"
-	docker "github.com/docker/docker/client"
-)
-
+/*
 func DockerBuild(p *Project) error {
 	log.Println("New docker environ")
 	cli, err := docker.NewEnvClient()
@@ -19,7 +9,7 @@ func DockerBuild(p *Project) error {
 		return err
 	}
 
-	dockertag := fmt.Sprintf("%s-builder", p.Name)
+	dockertag := fmt.Sprintf("%s-builder", p.conf.Meta.Project)
 	log.Println("Building and tagging:", dockertag)
 	///////////////
 	/// DOCKER OPERATION / BUILDER operation
@@ -66,10 +56,6 @@ func DockerBuild(p *Project) error {
 		io.Copy(f, cr)
 		f.Sync()
 
-		/*err = cli.ContainerStart(ctx, res.ID, types.ContainerStartOptions{AttachStdout: true})
-		if err != nil {
-			return err
-		}*/
 
 		log.Println("Please execute docker run", containerName, "> dist.tar.gz")
 		log.Println("Trying to remove container", res)
@@ -79,4 +65,4 @@ func DockerBuild(p *Project) error {
 		}
 	}
 	return nil
-}
+}*/

+ 41 - 23
src/buildme/project.go

@@ -2,6 +2,7 @@ package buildme
 
 import (
 	"bufio"
+	"buildme/utils"
 	"context"
 	"encoding/json"
 	"fmt"
@@ -16,12 +17,12 @@ import (
 
 // Project type
 type Project struct {
-	tarFile string
-	isTemp  bool
+	tarFilename string
+	isTemp      bool
 	//utils.Filer
 	//io.ReadCloser // Tar format? tar reader perhaps
-	Name       string
-	Dockerfile string
+	//Name       string
+	//Dockerfile string
 
 	conf ProjectConf
 	// Meta YML data
@@ -29,26 +30,27 @@ type Project struct {
 
 func NewProject(name string) *Project {
 	return &Project{
-		tarFile:    "",
-		isTemp:     false,
-		Name:       name,
-		Dockerfile: "docker/Dockerfile.build",
-		conf:       ProjectConf{},
+		tarFilename: "",
+		isTemp:      false,
+		//	Name:       name,
+		//Dockerfile: "docker/Dockerfile.build",
+		conf: ProjectConf{},
 	}
 }
 func (p *Project) SetTarFile(tarFile string, isTemp bool) *Project {
-	p.tarFile = tarFile
+	p.tarFilename = tarFile
 	p.isTemp = isTemp
 	return p
 }
-func (p *Project) SetName(name string) *Project {
+
+/*func (p *Project) SetName(name string) *Project {
 	p.Name = name
 	return p
 }
 func (p *Project) SetDockerfile(dockerfile string) *Project {
 	p.Dockerfile = dockerfile
 	return p
-}
+}*/
 
 func (p *Project) SetConf(projConf ProjectConf) *Project {
 	p.conf = projConf
@@ -56,21 +58,21 @@ func (p *Project) SetConf(projConf ProjectConf) *Project {
 }
 
 func (p *Project) Reader() io.ReadCloser {
-	f, _ := os.Open(p.tarFile)
+	f, _ := os.Open(p.tarFilename)
 	return f
 }
 
 func (p *Project) Close() {
 	if p.isTemp {
 		log.Println("Closing temporary file")
-		os.Remove(p.tarFile)
+		os.Remove(p.tarFilename)
 	}
 	//os.Remove(tarFile) // Should remove??
 }
 
 func (p *Project) Task(name string) error {
 
-	log.Printf("Proj: %s Running task: %s", p.Name, name)
+	log.Printf("Proj: %s Running task: %s", p.conf.Meta.Project, name)
 
 	taskconf, ok := p.conf.Tasks[name]
 	if !ok {
@@ -88,7 +90,7 @@ func (p *Project) Task(name string) error {
 	// Task kind docker
 	// Stupid docker cli
 	dockerhost := os.Getenv("DOCKER_HOST")
-	if !strings.Contains(dockerhost, "://") {
+	if dockerhost != "" && !strings.Contains(dockerhost, "://") {
 		os.Setenv("DOCKER_HOST", "http://"+dockerhost)
 	}
 
@@ -97,7 +99,7 @@ func (p *Project) Task(name string) error {
 		return err
 	}
 	ctx := context.Background()
-	dockertag := p.Name + "-" + name
+	dockertag := p.conf.Meta.Project + "-" + taskconf.Use
 	{
 		// Check if container exists
 
@@ -143,15 +145,31 @@ func (p *Project) Task(name string) error {
 	{
 		log.Println("Testing running the builder")
 		containerName := dockertag
-		containerCfg := &container.Config{AttachStdout: true, Cmd: []string{taskconf.Command}}
-		res, err := cli.ContainerCreate(ctx, containerCfg, nil, nil, containerName)
+		containerCfg := &container.Config{AttachStdout: true, Cmd: utils.ParseField(taskconf.Command)}
+		containerRes, err := cli.ContainerCreate(ctx, containerCfg, nil, nil, containerName)
+		if err != nil {
+			return err
+		}
+
+		// Remove container after finish
+		defer cli.ContainerRemove(ctx, containerRes.ID, types.ContainerRemoveOptions{})
+		log.Println("Created container ID:", containerRes.ID, "with image: ", dockertag)
+
+		log.Println("Starting container")
+		err = cli.ContainerStart(ctx, containerRes.ID, types.ContainerStartOptions{})
+		if err != nil {
+			return err
+		}
+
+		log.Println("Commit container")
+
+		_, err = cli.ContainerCommit(ctx, containerRes.ID, types.ContainerCommitOptions{Config: containerCfg})
 		if err != nil {
 			return err
 		}
-		log.Println("Created container ID:", res)
-		log.Println("Starting container test")
 
-		cr, _, err := cli.CopyFromContainer(ctx, res.ID, "/buildme/dist.tar.gz")
+		// Artifact??
+		/*cr, _, err := cli.CopyFromContainer(ctx, res.ID, "/buildme/dist.tar.gz")
 		if err != nil {
 			log.Println("Copy error", err)
 			return err
@@ -167,7 +185,7 @@ func (p *Project) Task(name string) error {
 		defer f.Close()
 
 		io.Copy(f, cr)
-		f.Sync()
+		f.Sync()*/
 	}
 
 	return nil

+ 29 - 0
src/buildme/utils/string.go

@@ -0,0 +1,29 @@
+package utils
+
+import (
+	"log"
+	"regexp"
+	"strings"
+)
+
+// Smart splitter
+
+func ParseField(s string) []string {
+	res := []string{}
+
+	re := regexp.MustCompile("[^\\\\]\"") // Non escaped '"'
+
+	fparts := re.Split(s, -1)
+	log.Println("Field parts", fparts)
+
+	for i, v := range fparts {
+		if i&0x1 == 1 { // Even
+			res = append(res, v)
+			continue
+		}
+		res = append(res, strings.Split(v, " ")...)
+	}
+
+	return res
+
+}

+ 16 - 7
src/buildme/utils/tardir.go

@@ -11,13 +11,11 @@ import (
 )
 
 //Tar tar a folder and return a reader?
-func Tar(fpath string, f *os.File) error {
+func Tar(fpath string, w *os.File) error {
 	var err error
 
-	w := f
-
 	if !filepath.IsAbs(fpath) {
-		return errors.New("Path must be absolute")
+		return errors.New("Path must be absolute??")
 	}
 	// Add slash to path end so files can be relative
 	if fpath[len(fpath)-1] != '/' {
@@ -25,6 +23,7 @@ func Tar(fpath string, f *os.File) error {
 	}
 
 	tw := tar.NewWriter(w)
+
 	err = filepath.Walk(fpath, func(p string, info os.FileInfo, err error) error {
 		if info.IsDir() && info.Name()[0] == '.' {
 			return filepath.SkipDir
@@ -33,8 +32,16 @@ func Tar(fpath string, f *os.File) error {
 			return nil
 		}
 
+		var link string
+
+		if info.Mode()&os.ModeSymlink == os.ModeSymlink {
+			if link, err = os.Readlink(p); err != nil {
+				return err
+			}
+		}
+
 		npath := strings.TrimPrefix(p, fpath)
-		theader, err := tar.FileInfoHeader(info, "")
+		theader, err := tar.FileInfoHeader(info, link)
 		if err != nil {
 			return err
 		}
@@ -46,6 +53,10 @@ func Tar(fpath string, f *os.File) error {
 			return err
 		}
 
+		if !info.Mode().IsRegular() {
+			return nil
+		}
+
 		f, err := os.Open(p)
 		if err != nil {
 			return err
@@ -62,8 +73,6 @@ func Tar(fpath string, f *os.File) error {
 	}
 	tw.Flush()
 
-	f.Seek(0, os.SEEK_SET)
-
 	return nil
 }