...
1
2
3 package runhcs
4
5 import (
6 "context"
7 "fmt"
8 "path/filepath"
9 "strings"
10
11 irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
12 runc "github.com/containerd/go-runc"
13 )
14
15
16 type CreateOpts struct {
17 runc.IO
18
19 PidFile string
20
21 ShimLog string
22
23 VMLog string
24
25 VMConsole string
26 }
27
28 func (opt *CreateOpts) args() ([]string, error) {
29 var out []string
30 if opt.PidFile != "" {
31 abs, err := filepath.Abs(opt.PidFile)
32 if err != nil {
33 return nil, err
34 }
35 out = append(out, "--pid-file", abs)
36 }
37 if opt.ShimLog != "" {
38 if strings.HasPrefix(opt.ShimLog, irunhcs.SafePipePrefix) {
39 out = append(out, "--shim-log", opt.ShimLog)
40 } else {
41 abs, err := filepath.Abs(opt.ShimLog)
42 if err != nil {
43 return nil, err
44 }
45 out = append(out, "--shim-log", abs)
46 }
47 }
48 if opt.VMLog != "" {
49 if strings.HasPrefix(opt.VMLog, irunhcs.SafePipePrefix) {
50 out = append(out, "--vm-log", opt.VMLog)
51 } else {
52 abs, err := filepath.Abs(opt.VMLog)
53 if err != nil {
54 return nil, err
55 }
56 out = append(out, "--vm-log", abs)
57 }
58 }
59 if opt.VMConsole != "" {
60 out = append(out, "--vm-console", opt.VMConsole)
61 }
62 return out, nil
63 }
64
65
66
67 func (r *Runhcs) Create(context context.Context, id, bundle string, opts *CreateOpts) error {
68 args := []string{"create", "--bundle", bundle}
69 if opts != nil {
70 oargs, err := opts.args()
71 if err != nil {
72 return err
73 }
74 args = append(args, oargs...)
75 }
76 cmd := r.command(context, append(args, id)...)
77 if opts != nil && opts.IO != nil {
78 opts.Set(cmd)
79 }
80 if cmd.Stdout == nil && cmd.Stderr == nil {
81 data, err := cmdOutput(cmd, true)
82 if err != nil {
83 return fmt.Errorf("%s: %s", err, data)
84 }
85 return nil
86 }
87 ec, err := runc.Monitor.Start(cmd)
88 if err != nil {
89 return err
90 }
91 if opts != nil && opts.IO != nil {
92 if c, ok := opts.IO.(runc.StartCloser); ok {
93 if err := c.CloseAfterStart(); err != nil {
94 return err
95 }
96 }
97 }
98 status, err := runc.Monitor.Wait(cmd, ec)
99 if err == nil && status != 0 {
100 err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
101 }
102 return err
103 }
104
View as plain text