Skip to content

Commit f94c7da

Browse files
authored
refactor(timesync): improve guest time sync (#53)
1 parent 1342d82 commit f94c7da

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

cmd/bauklotze/machine/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func start(cmd *cobra.Command, args []string) error {
138138

139139
logrus.Infof("Starting machine %q\n", vmName)
140140
go func() {
141-
errCh <- shim.Start(ctx, mc, provider, dirs, startOpts)
141+
errCh <- shim.Start(mc, provider, dirs, startOpts)
142142
}()
143143

144144
select {

pkg/api/backend/timesync.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,14 @@ import (
77
"errors"
88
"fmt"
99
"net/http"
10-
"time"
1110

1211
"bauklotze/pkg/api/utils"
13-
"bauklotze/pkg/machine"
1412
"bauklotze/pkg/machine/env"
1513
provider2 "bauklotze/pkg/machine/provider"
14+
"bauklotze/pkg/machine/system"
1615
"bauklotze/pkg/machine/vmconfigs"
1716
)
1817

19-
type timeStruct struct {
20-
Time string `json:"time"`
21-
Tz string `json:"tz"`
22-
}
23-
24-
func getCurrentTime() *timeStruct {
25-
currentTime := time.Now()
26-
tz, _ := currentTime.Zone()
27-
return &timeStruct{
28-
Time: currentTime.Format("2006-01-02 15:04:05"),
29-
Tz: tz,
30-
}
31-
}
32-
3318
func getVMMc(vmName string) (*vmconfigs.MachineConfig, error) {
3419
providers = provider2.GetAll()
3520
for _, sprovider := range providers {
@@ -49,20 +34,16 @@ func getVMMc(vmName string) (*vmconfigs.MachineConfig, error) {
4934
}
5035

5136
func TimeSync(w http.ResponseWriter, r *http.Request) {
52-
timeSt := getCurrentTime()
53-
5437
name := utils.GetName(r)
5538
mc, err := getVMMc(name)
56-
5739
if err != nil {
5840
utils.Error(w, http.StatusInternalServerError, err)
5941
return
6042
}
61-
62-
if sshError := machine.CommonSSHSilent(mc.SSH.RemoteUsername, mc.SSH.IdentityPath, mc.Name, mc.SSH.Port, []string{"date -s " + "'" + timeSt.Time + "'"}); sshError != nil {
63-
utils.Error(w, http.StatusInternalServerError, sshError)
43+
err = system.TimeSync(mc)
44+
if err != nil {
45+
utils.Error(w, http.StatusInternalServerError, err)
6446
return
6547
}
66-
67-
utils.WriteResponse(w, http.StatusOK, timeSt)
48+
utils.WriteResponse(w, http.StatusOK, "")
6849
}

pkg/machine/shim/host.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package shim
55

66
import (
7-
"context"
87
"encoding/json"
98
"fmt"
109
"runtime"
@@ -176,7 +175,7 @@ func getMCsOverProviders(vmstubbers []vmconfigs.VMProvider) (map[string]*vmconfi
176175
return mcs, nil
177176
}
178177

179-
func Start(ctx context.Context, mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *define.MachineDirs, opts define.StartOptions) error {
178+
func Start(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *define.MachineDirs, opts define.StartOptions) error {
180179
var err error
181180

182181
if err := mc.Refresh(); err != nil {
@@ -262,6 +261,19 @@ func Start(ctx context.Context, mc *vmconfigs.MachineConfig, mp vmconfigs.VMProv
262261
return fmt.Errorf("failed to update last boot time: %w", err)
263262
}
264263

264+
const defaultTimeSyncInterval = 10 * time.Minute
265+
266+
go func() {
267+
for {
268+
logrus.Infof("Time sync starting...")
269+
err := system.TimeSync(mc)
270+
if err != nil {
271+
logrus.Warnf("Time sync failed: %v", err)
272+
}
273+
time.Sleep(defaultTimeSyncInterval)
274+
}
275+
}()
276+
265277
for {
266278
pids := []int32{int32(gvcmd.Process.Pid), int32(krunCmd.Process.Pid)}
267279
running, err := system.IsProcesSAlive(pids)

pkg/machine/system/time.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: 2024-2025 OOMOL, Inc. <https://www.oomol.com>
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package system
5+
6+
import (
7+
"fmt"
8+
"time"
9+
10+
"bauklotze/pkg/machine"
11+
"bauklotze/pkg/machine/vmconfigs"
12+
)
13+
14+
func TimeSync(mc *vmconfigs.MachineConfig) error {
15+
syncTimeCmd := fmt.Sprintf("date -s @%d", time.Now().Unix())
16+
if err := machine.CommonSSHSilent(mc.SSH.RemoteUsername, mc.SSH.IdentityPath, mc.Name, mc.SSH.Port, []string{syncTimeCmd}); err != nil {
17+
return fmt.Errorf("failed to sync timestamp: %w", err)
18+
}
19+
return nil
20+
}

0 commit comments

Comments
 (0)