No O_EXCL.
[lightning.git] / cmd.go
1 package main
2
3 import (
4         "fmt"
5         "io"
6         "io/ioutil"
7         "os"
8         "os/exec"
9
10         "git.arvados.org/arvados.git/lib/cmd"
11 )
12
13 var (
14         handler = cmd.Multi(map[string]cmd.Handler{
15                 "version":   cmd.Version,
16                 "-version":  cmd.Version,
17                 "--version": cmd.Version,
18
19                 "ref2genome":         &ref2genome{},
20                 "vcf2fasta":          &vcf2fasta{},
21                 "import":             &importer{},
22                 "export-numpy":       &exportNumpy{},
23                 "filter":             &filterer{},
24                 "build-docker-image": &buildDockerImage{},
25                 "pca":                &pythonPCA{},
26                 "plot":               &pythonPlot{},
27         })
28 )
29
30 func main() {
31         os.Exit(handler.RunCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
32 }
33
34 type buildDockerImage struct{}
35
36 func (cmd *buildDockerImage) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
37         tmpdir, err := ioutil.TempDir("", "")
38         if err != nil {
39                 fmt.Fprint(stderr, err)
40                 return 1
41         }
42         defer os.RemoveAll(tmpdir)
43         err = ioutil.WriteFile(tmpdir+"/Dockerfile", []byte(`FROM debian:10
44 RUN DEBIAN_FRONTEND=noninteractive \
45   apt-get update && \
46   apt-get dist-upgrade -y && \
47   apt-get install -y --no-install-recommends bcftools bedtools samtools python3-sklearn python3-matplotlib && \
48   apt-get clean
49 `), 0644)
50         if err != nil {
51                 fmt.Fprint(stderr, err)
52                 return 1
53         }
54         docker := exec.Command("docker", "build", "--tag=lightning-runtime", tmpdir)
55         docker.Stdout = stdout
56         docker.Stderr = stderr
57         err = docker.Run()
58         if err != nil {
59                 return 1
60         }
61         fmt.Fprintf(stderr, "built and tagged new docker image, lightning-runtime\n")
62         return 0
63 }