forked from gaia-pipeline/gaia
/
update_pipeline.go
56 lines (48 loc) · 1.49 KB
/
update_pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package pipeline
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/gaia-pipeline/gaia"
)
var (
// virtualEnvName is the binary name of virtual environment app.
virtualEnvName = "virtualenv"
// pythonPipInstallCmd is the command used to install the python distribution
// package.
pythonPipInstallCmd = ". bin/activate; python -m pip install %s.tar.gz"
)
// updatePipeline executes update steps dependent on the pipeline type.
// Some pipeline types may don't require this.
func updatePipeline(p *gaia.Pipeline) error {
switch p.Type {
case gaia.PTypePython:
// Remove virtual environment if existend
virtualEnvPath := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpPythonFolder, p.Name)
os.RemoveAll(virtualEnvPath)
// Create virtual environment
path, err := exec.LookPath(virtualEnvName)
if err != nil {
return errors.New("cannot find virtualenv executeable")
}
cmd := exec.Command(path, virtualEnvPath)
if err := cmd.Run(); err != nil {
return err
}
// copy distribution file to environment and remove pipeline type at the end.
// we have to do this otherwise pip will fail.
err = copyFileContents(p.ExecPath, filepath.Join(virtualEnvPath, p.Name+".tar.gz"))
if err != nil {
return err
}
// install plugin in this environment
cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf(pythonPipInstallCmd, filepath.Join(virtualEnvPath, p.Name)))
cmd.Dir = virtualEnvPath
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}