Skip to content

Commit 0def454

Browse files
authored
fix(lam): wait for service after start (#1883)
* fix(lam): wait for service after start * f * f * f * f * f
1 parent 2e3f2bc commit 0def454

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

cmd/installer/cli/install.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func installAndStartCluster(ctx context.Context, networkInterface string, airgap
608608
return nil, fmt.Errorf("create config file: %w", err)
609609
}
610610
logrus.Debugf("creating systemd unit files")
611-
if err := createSystemdUnitFiles(false, proxy); err != nil {
611+
if err := createSystemdUnitFiles(ctx, false, proxy); err != nil {
612612
return nil, fmt.Errorf("create systemd unit files: %w", err)
613613
}
614614

@@ -769,7 +769,7 @@ func validateAdminConsolePassword(password, passwordCheck string) bool {
769769

770770
// createSystemdUnitFiles links the k0s systemd unit file. this also creates a new
771771
// systemd unit file for the local artifact mirror service.
772-
func createSystemdUnitFiles(isWorker bool, proxy *ecv1beta1.ProxySpec) error {
772+
func createSystemdUnitFiles(ctx context.Context, isWorker bool, proxy *ecv1beta1.ProxySpec) error {
773773
dst := systemdUnitFileName()
774774
if _, err := os.Lstat(dst); err == nil {
775775
if err := os.Remove(dst); err != nil {
@@ -793,7 +793,7 @@ func createSystemdUnitFiles(isWorker bool, proxy *ecv1beta1.ProxySpec) error {
793793
if _, err := helpers.RunCommand("systemctl", "daemon-reload"); err != nil {
794794
return fmt.Errorf("unable to get reload systemctl daemon: %w", err)
795795
}
796-
if err := installAndEnableLocalArtifactMirror(); err != nil {
796+
if err := installAndEnableLocalArtifactMirror(ctx); err != nil {
797797
return fmt.Errorf("unable to install and enable local artifact mirror: %w", err)
798798
}
799799
return nil
@@ -828,7 +828,7 @@ Environment="NO_PROXY=%s"`, httpProxy, httpsProxy, noProxy)
828828
// installAndEnableLocalArtifactMirror installs and enables the local artifact mirror. This
829829
// service is responsible for serving on localhost, through http, all files that are used
830830
// during a cluster upgrade.
831-
func installAndEnableLocalArtifactMirror() error {
831+
func installAndEnableLocalArtifactMirror(ctx context.Context) error {
832832
materializer := goods.NewMaterializer()
833833
if err := materializer.LocalArtifactMirrorUnitFile(); err != nil {
834834
return fmt.Errorf("failed to materialize artifact mirror unit: %w", err)
@@ -839,15 +839,50 @@ func installAndEnableLocalArtifactMirror() error {
839839
if _, err := helpers.RunCommand("systemctl", "daemon-reload"); err != nil {
840840
return fmt.Errorf("unable to get reload systemctl daemon: %w", err)
841841
}
842+
if _, err := helpers.RunCommand("systemctl", "enable", "local-artifact-mirror"); err != nil {
843+
return fmt.Errorf("unable to enable the local artifact mirror service: %w", err)
844+
}
845+
logrus.Debugf("Starting local artifact mirror")
842846
if _, err := helpers.RunCommand("systemctl", "start", "local-artifact-mirror"); err != nil {
843847
return fmt.Errorf("unable to start the local artifact mirror: %w", err)
844848
}
845-
if _, err := helpers.RunCommand("systemctl", "enable", "local-artifact-mirror"); err != nil {
846-
return fmt.Errorf("unable to start the local artifact mirror service: %w", err)
849+
if err := waitForLocalArtifactMirror(ctx); err != nil {
850+
return fmt.Errorf("unable to wait for the local artifact mirror: %w", err)
847851
}
852+
logrus.Debugf("Local artifact mirror started!")
848853
return nil
849854
}
850855

856+
func waitForLocalArtifactMirror(ctx context.Context) error {
857+
consecutiveSuccesses := 0
858+
requiredSuccesses := 3
859+
maxAttempts := 30
860+
checkInterval := 2 * time.Second
861+
862+
var lastErr error
863+
for attempt := 0; attempt < maxAttempts; attempt++ {
864+
_, err := helpers.RunCommand("systemctl", "status", "local-artifact-mirror")
865+
if err == nil {
866+
consecutiveSuccesses++
867+
if consecutiveSuccesses >= requiredSuccesses {
868+
return nil
869+
}
870+
} else {
871+
consecutiveSuccesses = 0
872+
lastErr = err
873+
}
874+
875+
select {
876+
case <-ctx.Done():
877+
return ctx.Err()
878+
case <-time.After(checkInterval):
879+
continue
880+
}
881+
}
882+
883+
return lastErr
884+
}
885+
851886
const (
852887
localArtifactMirrorDropInFileContents = `[Service]
853888
Environment="LOCAL_ARTIFACT_MIRROR_PORT=%d"

cmd/installer/cli/join.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ func installAndJoinCluster(ctx context.Context, jcmd *kotsadm.JoinCommandRespons
299299

300300
logrus.Debugf("creating systemd unit files")
301301
// both controller and worker nodes will have 'worker' in the join command
302-
if err := createSystemdUnitFiles(!strings.Contains(jcmd.K0sJoinCommand, "controller"), jcmd.InstallationSpec.Proxy); err != nil {
302+
isWorker := !strings.Contains(jcmd.K0sJoinCommand, "controller")
303+
if err := createSystemdUnitFiles(ctx, isWorker, jcmd.InstallationSpec.Proxy); err != nil {
303304
return fmt.Errorf("unable to create systemd unit files: %w", err)
304305
}
305306

cmd/installer/kotscli/kotscli.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package kotscli
22

33
import (
4-
"bytes"
54
"fmt"
6-
"io"
75
"os"
86
"regexp"
97
"strings"
@@ -14,7 +12,6 @@ import (
1412
"github.com/replicatedhq/embedded-cluster/pkg/release"
1513
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
1614
"github.com/replicatedhq/embedded-cluster/pkg/spinner"
17-
"github.com/sirupsen/logrus"
1815
)
1916

2017
var (
@@ -78,21 +75,14 @@ func Install(opts InstallOptions, msg *spinner.MessageWriter) error {
7875
defer msg.SetMask(nil)
7976
defer msg.SetLineBreaker(nil)
8077

81-
stdout := bytes.NewBuffer(nil)
82-
stderr := bytes.NewBuffer(nil)
83-
8478
runCommandOptions := helpers.RunCommandOptions{
85-
Stdout: io.MultiWriter(msg, stdout),
86-
Stderr: stderr,
79+
Stdout: msg,
80+
LogOnSuccess: true,
8781
Env: map[string]string{
8882
"EMBEDDED_CLUSTER_ID": metrics.ClusterID().String(),
8983
},
9084
}
9185
err = helpers.RunCommandWithOptions(runCommandOptions, kotsBinPath, installArgs...)
92-
logrus.Debugf("kotscli stdout: %s", stdout.String())
93-
if out := strings.TrimSpace(stderr.String()); out != "" {
94-
logrus.Debugf("kotscli stderr: %s", out)
95-
}
9686
if err != nil {
9787
return fmt.Errorf("unable to install the application: %w", err)
9888
}

0 commit comments

Comments
 (0)