blob: 106d4a422da51971c56ac085bf5e81ab0bc64271 [file] [log] [blame]
Bogdan Caprita4d67c042014-08-19 10:41:19 -07001package impl
2
3import (
4 "io/ioutil"
5 "os"
6 "path/filepath"
7 "time"
8
Jiri Simsa519c5072014-09-17 21:37:57 -07009 "veyron.io/veyron/veyron/services/mgmt/lib/binary"
Bogdan Caprita4d67c042014-08-19 10:41:19 -070010
Jiri Simsa519c5072014-09-17 21:37:57 -070011 "veyron.io/veyron/veyron2/context"
12 "veyron.io/veyron/veyron2/services/mgmt/application"
13 "veyron.io/veyron/veyron2/services/mgmt/repository"
14 "veyron.io/veyron/veyron2/vlog"
Bogdan Caprita4d67c042014-08-19 10:41:19 -070015)
16
17func downloadBinary(workspace, fileName, name string) error {
18 data, err := binary.Download(name)
19 if err != nil {
20 vlog.Errorf("Download(%v) failed: %v", name, err)
21 return errOperationFailed
22 }
23 path, perm := filepath.Join(workspace, fileName), os.FileMode(755)
24 if err := ioutil.WriteFile(path, data, perm); err != nil {
25 vlog.Errorf("WriteFile(%v, %v) failed: %v", path, perm, err)
26 return errOperationFailed
27 }
28 return nil
29}
30
31func fetchEnvelope(ctx context.T, origin string) (*application.Envelope, error) {
32 stub, err := repository.BindApplication(origin)
33 if err != nil {
34 vlog.Errorf("BindRepository(%v) failed: %v", origin, err)
35 return nil, errOperationFailed
36 }
37 // TODO(jsimsa): Include logic that computes the set of supported
38 // profiles.
39 profiles := []string{"test"}
40 envelope, err := stub.Match(ctx, profiles)
41 if err != nil {
42 vlog.Errorf("Match(%v) failed: %v", profiles, err)
43 return nil, errOperationFailed
44 }
45 return &envelope, nil
46}
47
48func generateBinary(workspace, fileName string, envelope *application.Envelope, newBinary bool) error {
49 if newBinary {
50 // Download the new binary.
51 return downloadBinary(workspace, fileName, envelope.Binary)
52 }
53 // Link the current binary.
54 path := filepath.Join(workspace, fileName)
55 if err := os.Link(os.Args[0], path); err != nil {
56 vlog.Errorf("Link(%v, %v) failed: %v", os.Args[0], path, err)
57 return errOperationFailed
58 }
59 return nil
60}
61
62func generateVersionDirName() string {
63 return time.Now().Format(time.RFC3339Nano)
64}
Bogdan Caprita48bbd142014-09-04 16:07:23 -070065
66func updateLink(target, link string) error {
67 newLink := link + ".new"
68 fi, err := os.Lstat(newLink)
69 if err == nil {
70 if err := os.Remove(fi.Name()); err != nil {
71 vlog.Errorf("Remove(%v) failed: %v", fi.Name(), err)
72 return errOperationFailed
73 }
74 }
75 if err := os.Symlink(target, newLink); err != nil {
76 vlog.Errorf("Symlink(%v, %v) failed: %v", target, newLink, err)
77 return errOperationFailed
78 }
79 if err := os.Rename(newLink, link); err != nil {
80 vlog.Errorf("Rename(%v, %v) failed: %v", newLink, link, err)
81 return errOperationFailed
82 }
83 return nil
84}
Bogdan Caprita9d285a12014-09-30 15:04:16 -070085
86// cleanupDir is defined like this so we can override its implementation for
87// tests.
88var cleanupDir = func(dir string) {
89 if err := os.RemoveAll(dir); err != nil {
90 vlog.Errorf("RemoveAll(%v) failed: %v", dir, err)
91 }
92}