|
| 1 | +package pipeline |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gaia-pipeline/gaia" |
| 13 | + "github.com/gaia-pipeline/gaia/services" |
| 14 | + "github.com/satori/go.uuid" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + pythonBinaryName = "python" |
| 19 | +) |
| 20 | + |
| 21 | +// BuildPipelinePython is the real implementation of BuildPipeline for python |
| 22 | +type BuildPipelinePython struct { |
| 23 | + Type gaia.PipelineType |
| 24 | +} |
| 25 | + |
| 26 | +// PrepareEnvironment prepares the environment before we start the build process. |
| 27 | +func (b *BuildPipelinePython) PrepareEnvironment(p *gaia.CreatePipeline) error { |
| 28 | + // create uuid for destination folder |
| 29 | + uuid := uuid.Must(uuid.NewV4(), nil) |
| 30 | + |
| 31 | + // Create local temp folder for clone |
| 32 | + rootPath := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpPythonFolder) |
| 33 | + cloneFolder := filepath.Join(rootPath, srcFolder, uuid.String()) |
| 34 | + err := os.MkdirAll(cloneFolder, 0700) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + // Set new generated path in pipeline obj for later usage |
| 40 | + p.Pipeline.Repo.LocalDest = cloneFolder |
| 41 | + p.Pipeline.UUID = uuid.String() |
| 42 | + return err |
| 43 | +} |
| 44 | + |
| 45 | +// ExecuteBuild executes the python build process |
| 46 | +func (b *BuildPipelinePython) ExecuteBuild(p *gaia.CreatePipeline) error { |
| 47 | + // Look for python executeable |
| 48 | + path, err := exec.LookPath(pythonBinaryName) |
| 49 | + if err != nil { |
| 50 | + gaia.Cfg.Logger.Debug("cannot find python executeable", "error", err.Error()) |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Set command args for build distribution package |
| 55 | + args := []string{ |
| 56 | + "setup.py", |
| 57 | + "sdist", |
| 58 | + } |
| 59 | + |
| 60 | + // Execute and wait until finish or timeout |
| 61 | + output, err := executeCmd(path, args, os.Environ(), p.Pipeline.Repo.LocalDest) |
| 62 | + if err != nil { |
| 63 | + gaia.Cfg.Logger.Debug("cannot generate python distribution package", "error", err.Error(), "output", string(output)) |
| 64 | + p.Output = string(output) |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// CopyBinary copies the final compiled archive to the |
| 72 | +// destination folder. |
| 73 | +func (b *BuildPipelinePython) CopyBinary(p *gaia.CreatePipeline) error { |
| 74 | + // find all files in dist folder |
| 75 | + distFolder := filepath.Join(p.Pipeline.Repo.LocalDest, "dist") |
| 76 | + files, err := ioutil.ReadDir(distFolder) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + // filter for archives |
| 82 | + archive := []os.FileInfo{} |
| 83 | + for _, file := range files { |
| 84 | + if strings.HasSuffix(file.Name(), ".tar.gz") { |
| 85 | + archive = append(archive, file) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // if we found more or less than one archive we have a problem |
| 90 | + if len(archive) != 1 { |
| 91 | + gaia.Cfg.Logger.Debug("cannot copy python package", "foundPackages", len(archive), "archives", files) |
| 92 | + return errors.New("cannot copy python package: not found") |
| 93 | + } |
| 94 | + |
| 95 | + // Define src and destination |
| 96 | + src := filepath.Join(distFolder, archive[0].Name()) |
| 97 | + dest := filepath.Join(gaia.Cfg.PipelinePath, appendTypeToName(p.Pipeline.Name, p.Pipeline.Type)) |
| 98 | + |
| 99 | + // Copy binary |
| 100 | + if err := copyFileContents(src, dest); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + // Set +x (execution right) for pipeline |
| 105 | + return os.Chmod(dest, 0766) |
| 106 | +} |
| 107 | + |
| 108 | +// SavePipeline saves the current pipeline configuration. |
| 109 | +func (b *BuildPipelinePython) SavePipeline(p *gaia.Pipeline) error { |
| 110 | + dest := filepath.Join(gaia.Cfg.PipelinePath, appendTypeToName(p.Name, p.Type)) |
| 111 | + p.ExecPath = dest |
| 112 | + p.Type = gaia.PTypePython |
| 113 | + p.Name = strings.TrimSuffix(filepath.Base(dest), typeDelimiter+gaia.PTypePython.String()) |
| 114 | + p.Created = time.Now() |
| 115 | + // Our pipeline is finished constructing. Save it. |
| 116 | + storeService, _ := services.StorageService() |
| 117 | + return storeService.PipelinePut(p) |
| 118 | +} |
0 commit comments