11 "git.arvados.org/arvados.git/sdk/go/arvados"
12 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
13 "git.arvados.org/arvados.git/sdk/go/keepclient"
14 "golang.org/x/crypto/blake2b"
17 type arvadosContainerRunner struct {
18 Client *arvados.Client
23 Prog string // if empty, run /proc/self/exe
25 Mounts map[string]string
29 collectionInPathRe = regexp.MustCompile(`^(.*/)?([0-9a-f]{32}\+[0-9]+|[0-9a-z]{5}-[0-9a-z]{5}-[0-9a-z]{15})(/.*)?$`)
32 func (runner *arvadosContainerRunner) Run() error {
33 if runner.ProjectUUID == "" {
34 return errors.New("cannot run arvados container: ProjectUUID not provided")
36 mounts := map[string]map[string]interface{}{
40 "capacity": 100000000000,
46 prog = "/mnt/cmd/lightning"
47 cmdUUID, err := runner.makeCommandCollection()
51 mounts["/mnt/cmd"] = map[string]interface{}{
56 command := append([]string{prog}, runner.Args...)
58 for uuid, mnt := range runner.Mounts {
59 mounts[mnt] = map[string]interface{}{
64 rc := arvados.RuntimeConstraints{
67 KeepCacheRAM: (1 << 26) * 2 * int64(runner.VCPUs),
69 var cr arvados.ContainerRequest
70 err := runner.Client.RequestAndDecode(&cr, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
71 "container_request": map[string]interface{}{
72 "owner_uuid": runner.ProjectUUID,
74 "container_image": "lightning-runtime",
78 "output_path": "/mnt/output",
79 "runtime_constraints": rc,
81 "state": arvados.ContainerRequestStateCommitted,
88 func (runner *arvadosContainerRunner) TranslatePaths(paths ...*string) error {
89 if runner.Mounts == nil {
90 runner.Mounts = make(map[string]string)
92 for _, path := range paths {
96 m := collectionInPathRe.FindStringSubmatch(*path)
98 return fmt.Errorf("cannot find uuid in path: %q", *path)
101 mnt, ok := runner.Mounts[uuid]
104 runner.Mounts[uuid] = mnt
111 func (runner *arvadosContainerRunner) makeCommandCollection() (string, error) {
112 exe, err := ioutil.ReadFile("/proc/self/exe")
116 b2 := blake2b.Sum256(exe)
117 cname := fmt.Sprintf("lightning-%x", b2)
118 var existing arvados.CollectionList
119 err = runner.Client.RequestAndDecode(&existing, "GET", "arvados/v1/collections", nil, arvados.ListOptions{
122 Filters: []arvados.Filter{
123 {Attr: "name", Operator: "=", Operand: cname},
124 {Attr: "owner_uuid", Operator: "=", Operand: runner.ProjectUUID},
130 if len(existing.Items) > 0 {
131 uuid := existing.Items[0].UUID
132 log.Printf("using existing collection %q named %q (did not verify whether content matches)", uuid, cname)
135 log.Printf("writing lightning binary to new collection %q", cname)
136 ac, err := arvadosclient.New(runner.Client)
140 kc := keepclient.New(ac)
141 var coll arvados.Collection
142 fs, err := coll.FileSystem(runner.Client, kc)
146 f, err := fs.OpenFile("lightning", os.O_CREATE|os.O_WRONLY, 0777)
150 _, err = f.Write(exe)
158 mtxt, err := fs.MarshalManifest(".")
162 err = runner.Client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
163 "collection": map[string]interface{}{
164 "owner_uuid": runner.ProjectUUID,
165 "manifest_text": mtxt,
172 log.Printf("collection: %#v", coll)
173 return coll.UUID, nil