Skip to content

Commit 903b073

Browse files
Platform tart:// improve logging / fix tool_cache (#61)
1 parent 00231e5 commit 903b073

File tree

4 files changed

+61
-84
lines changed

4 files changed

+61
-84
lines changed

pkg/runner/run_context_darwin.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ func (rc *RunContext) startTartEnvironment() common.Executor {
6464
SSHPassword: "admin",
6565
Softnet: query.Get("softnet") == "1",
6666
Headless: query.Get("headless") != "0",
67-
AlwaysPull: query.Get("pull") != "0",
67+
AlwaysPull: rc.Config.ForcePull,
6868
},
6969
Env: &tart.Env{
7070
JobImage: platURI.Host + platURI.EscapedPath(),
7171
JobID: rc.jobContainerName(),
7272
},
7373
Miscpath: miscpath,
7474
}
75+
if query.Has("pull") {
76+
tenv.Config.AlwaysPull = query.Get("pull") != "0"
77+
}
7578
rc.JobContainer = tenv
7679
if query.Has("sshusername") {
7780
tenv.Config.SSHUsername = query.Get("sshusername")
@@ -85,14 +88,6 @@ func (rc *RunContext) startTartEnvironment() common.Executor {
8588
rc.Env[fmt.Sprintf("RUNNER_%s", strings.ToUpper(k))] = v
8689
}
8790
}
88-
// for _, env := range os.Environ() {
89-
// if k, v, ok := strings.Cut(env, "="); ok {
90-
// // don't override
91-
// if _, ok := rc.Env[k]; !ok {
92-
// rc.Env[k] = v
93-
// }
94-
// }
95-
// }
9691

9792
return common.NewPipelineExecutor(
9893
// rc.JobContainer.Remove(),

pkg/tart/config_darwin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package tart
22

3+
import "io"
4+
35
type Config struct {
46
SSHUsername string
57
SSHPassword string
68
Softnet bool
79
Headless bool
810
AlwaysPull bool
11+
Writer io.Writer
912
}

pkg/tart/environment_darwin.go

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"log"
87
"os"
98
"path/filepath"
109
"strings"
1110

12-
"github.com/kballard/go-shellquote"
1311
"github.com/actions-oss/act-cli/pkg/common"
1412
"github.com/actions-oss/act-cli/pkg/container"
13+
"github.com/kballard/go-shellquote"
1514
)
1615

1716
type Environment struct {
@@ -24,20 +23,32 @@ type Environment struct {
2423

2524
// "/Volumes/My Shared Files/act/"
2625
func (e *Environment) ToHostPath(path string) string {
27-
actPath := filepath.Clean("/private/tmp/act/")
28-
altPath := filepath.Clean(path)
29-
if strings.HasPrefix(altPath, actPath) {
30-
return e.Miscpath + altPath[len(actPath):]
31-
}
32-
return altPath
26+
return e.translatePath(path, false)
3327
}
3428

3529
func (e *Environment) ToContainerPath(path string) string {
3630
path = e.HostEnvironment.ToContainerPath(path)
37-
actPath := filepath.Clean(e.Miscpath)
31+
return e.translatePath(path, true)
32+
}
33+
34+
func (e *Environment) translatePath(path string, reverse bool) string {
35+
mounts := map[string]string{
36+
"/private/tmp/act": e.Miscpath,
37+
"/private/tmp/tool_cache": e.ToolCache,
38+
}
3839
altPath := filepath.Clean(path)
39-
if strings.HasPrefix(altPath, actPath) {
40-
return "/private/tmp/act/" + altPath[len(actPath):]
40+
for k, v := range mounts {
41+
if reverse {
42+
v, k = k, v
43+
}
44+
actPath := filepath.Clean(k)
45+
add := 0
46+
if strings.HasPrefix(altPath, actPath+"/") {
47+
add = 1
48+
}
49+
if altPath == actPath || add > 0 {
50+
return filepath.Join(v, altPath[len(actPath)+add:])
51+
}
4152
}
4253
return altPath
4354
}
@@ -71,35 +82,39 @@ func (e *Environment) start(ctx context.Context) error {
7182

7283
config := e.Config
7384

85+
config.Writer = e.StdOut
86+
7487
if config.AlwaysPull {
75-
log.Printf("Pulling the latest version of %s...\n", actEnv.JobImage)
88+
common.Logger(ctx).Infof("Pulling the latest version of %s...\n", actEnv.JobImage)
7689
_, _, err := ExecWithEnv(ctx, nil,
7790
"pull", actEnv.JobImage)
7891
if err != nil {
7992
return err
8093
}
8194
}
8295

83-
log.Println("Cloning and configuring a new VM...")
96+
common.Logger(ctx).Info("Cloning and configuring a new VM...")
8497
vm, err := CreateNewVM(ctx, *actEnv, 0, 0)
8598
if err != nil {
8699
_ = e.Stop(ctx)
87100
return err
88101
}
89102
var customDirectoryMounts []string
90-
_ = os.MkdirAll(e.Miscpath, 0666)
103+
_ = os.MkdirAll(e.Miscpath, 0777)
104+
_ = os.MkdirAll(e.ToolCache, 0777)
91105
customDirectoryMounts = append(customDirectoryMounts, "act:"+e.Miscpath)
106+
customDirectoryMounts = append(customDirectoryMounts, "tool_cache:"+e.ToolCache)
92107
e.vm = vm
93-
err = vm.Start(config, actEnv, customDirectoryMounts)
108+
err = vm.Start(ctx, config, actEnv, customDirectoryMounts)
94109
if err != nil {
95110
_ = e.Stop(ctx)
96111
return err
97112
}
98113

99-
return e.execRaw(ctx, "ln -sf '/Volumes/My Shared Files/act' /private/tmp/act")
114+
return e.execRaw(ctx, "ln -sf '/Volumes/My Shared Files/act' /private/tmp/act && ln -sf '/Volumes/My Shared Files/tool_cache' /private/tmp/tool_cache")
100115
}
101116
func (e *Environment) Stop(ctx context.Context) error {
102-
log.Println("Stop VM?")
117+
common.Logger(ctx).Debug("Preparing stopping VM")
103118

104119
actEnv := e.Env
105120

@@ -110,12 +125,12 @@ func (e *Environment) Stop(ctx context.Context) error {
110125
vm = ExistingVM(*actEnv)
111126
}
112127

113-
if err := vm.Stop(); err != nil {
114-
log.Printf("Failed to stop VM: %v", err)
128+
if err := vm.Stop(ctx); err != nil {
129+
common.Logger(ctx).Errorf("Failed to stop VM: %v", err)
115130
}
116131

117-
if err := vm.Delete(); err != nil {
118-
log.Printf("Failed to delete VM: %v", err)
132+
if err := vm.Delete(ctx); err != nil {
133+
common.Logger(ctx).Error("Failed to delete VM: %v", err)
119134

120135
return err
121136
}
@@ -126,7 +141,7 @@ func (e *Environment) Stop(ctx context.Context) error {
126141
func (e *Environment) Remove() common.Executor {
127142
return func(ctx context.Context) error {
128143
_ = e.Stop(ctx)
129-
log.Println("Remove VM?")
144+
common.Logger(ctx).Debug("Stopped VM, removing...")
130145
if e.CleanUp != nil {
131146
e.CleanUp()
132147
}
@@ -162,9 +177,6 @@ func (e *Environment) execRaw(ctx context.Context, script string) error {
162177
vm = ExistingVM(*actEnv)
163178
}
164179

165-
// Monitor "tart run" command's output so it's not silenced
166-
go vm.MonitorTartRunOutput()
167-
168180
config := e.Config
169181

170182
ssh, err := vm.OpenSSH(ctx, config)
@@ -179,7 +191,7 @@ func (e *Environment) execRaw(ctx context.Context, script string) error {
179191
}
180192
defer session.Close()
181193

182-
os.Stdout.WriteString(script + "\n")
194+
common.Logger(ctx).Debug(script)
183195

184196
session.Stdin = strings.NewReader(
185197
script,

pkg/tart/vm_darwin.go

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io"
9-
"log"
108
"net"
119
"os"
1210
"os/exec"
1311
"path/filepath"
1412
"strconv"
1513
"strings"
1614
"syscall"
17-
"time"
1815

16+
"github.com/actions-oss/act-cli/pkg/common"
1917
"github.com/avast/retry-go"
2018
"golang.org/x/crypto/ssh"
2119
)
@@ -45,7 +43,7 @@ func CreateNewVM(
4543
cpuOverride uint64,
4644
memoryOverride uint64,
4745
) (*VM, error) {
48-
log.Print("CreateNewVM")
46+
common.Logger(ctx).Debug("CreateNewVM")
4947
vm := &VM{
5048
id: actEnv.VirtualMachineID(),
5149
}
@@ -85,7 +83,7 @@ func (vm *VM) cloneAndConfigure(
8583
return nil
8684
}
8785

88-
func (vm *VM) Start(config Config, _ *Env, customDirectoryMounts []string) error {
86+
func (vm *VM) Start(ctx context.Context, config Config, _ *Env, customDirectoryMounts []string) error {
8987
os.Remove(vm.tartRunOutputPath())
9088
var runArgs = []string{"run"}
9189

@@ -103,56 +101,25 @@ func (vm *VM) Start(config Config, _ *Env, customDirectoryMounts []string) error
103101

104102
runArgs = append(runArgs, vm.id)
105103

106-
cmd := exec.Command(tartCommandName, runArgs...)
104+
cmd := exec.CommandContext(ctx, tartCommandName, runArgs...)
107105

108-
outputFile, err := os.OpenFile(vm.tartRunOutputPath(), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
109-
if err != nil {
110-
return err
111-
}
112-
_, _ = outputFile.WriteString(strings.Join(runArgs, " ") + "\n")
106+
common.Logger(ctx).Debug(strings.Join(runArgs, " "))
113107

114-
cmd.Stdout = outputFile
115-
cmd.Stderr = outputFile
108+
cmd.Stdout = config.Writer
109+
cmd.Stderr = config.Writer
116110

117111
cmd.SysProcAttr = &syscall.SysProcAttr{
118112
Setsid: true,
119113
}
120114

121-
err = cmd.Start()
115+
err := cmd.Start()
122116
if err != nil {
123117
return err
124118
}
125119
vm.runcmd = cmd
126120
return nil
127121
}
128122

129-
func (vm *VM) MonitorTartRunOutput() {
130-
outputFile, err := os.Open(vm.tartRunOutputPath())
131-
if err != nil {
132-
fmt.Fprintf(os.Stderr, "Failed to open VM's output file, "+
133-
"looks like the VM wasn't started in \"prepare\" step?\n")
134-
135-
return
136-
}
137-
defer func() {
138-
_ = outputFile.Close()
139-
}()
140-
141-
for {
142-
n, err := io.Copy(os.Stdout, outputFile)
143-
if err != nil {
144-
fmt.Fprintf(os.Stderr, "Failed to display VM's output: %v\n", err)
145-
146-
break
147-
}
148-
if n == 0 {
149-
time.Sleep(100 * time.Millisecond)
150-
151-
continue
152-
}
153-
}
154-
}
155-
156123
func (vm *VM) OpenSSH(ctx context.Context, config Config) (*ssh.Client, error) {
157124
ip, err := vm.IP(ctx)
158125
if err != nil {
@@ -198,22 +165,22 @@ func (vm *VM) IP(ctx context.Context) (string, error) {
198165
return strings.TrimSpace(stdout), nil
199166
}
200167

201-
func (vm *VM) Stop() error {
202-
log.Println("Stop VM REAL?")
168+
func (vm *VM) Stop(ctx context.Context) error {
169+
common.Logger(ctx).Debug("Stop VM REAL?")
203170
if vm.runcmd != nil {
204-
log.Println("send sigint?")
171+
common.Logger(ctx).Debug("send sigint")
205172
_ = vm.runcmd.Process.Signal(os.Interrupt)
206-
log.Println("wait?")
173+
common.Logger(ctx).Debug("wait for cmd")
207174
_ = vm.runcmd.Wait()
208-
log.Println("wait done?")
175+
common.Logger(ctx).Debug("cmd stopped")
209176
return nil
210177
}
211-
_, _, err := Exec(context.Background(), "stop", vm.id)
178+
_, _, err := Exec(ctx, "stop", vm.id)
212179
return err
213180
}
214181

215-
func (vm *VM) Delete() error {
216-
_, _, err := Exec(context.Background(), "delete", vm.id)
182+
func (vm *VM) Delete(ctx context.Context) error {
183+
_, _, err := Exec(ctx, "delete", vm.id)
217184
if err != nil {
218185
return fmt.Errorf("%w: failed to delete VM %s: %v", ErrVMFailed, vm.id, err)
219186
}

0 commit comments

Comments
 (0)