Skip to content

Commit a992787

Browse files
authored
Merge pull request #3982 from norio-nomura/use-journalctl-on-progress-reporting
`--progress`: use `journalctl -u cloud-init-main.service` if `cloud-init-main.service` is detected
2 parents 69538a7 + 1e89fd1 commit a992787

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

pkg/hostagent/hostagent.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -948,12 +948,16 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
948948
"-p", strconv.Itoa(a.sshLocalPort),
949949
"127.0.0.1",
950950
"sh", "-c",
951-
`"sudo tail -n +$(sudo awk '
952-
BEGIN{b=1; e=1}
953-
/^Cloud-init.* finished/{e=NR}
954-
/.*/{if(NR>e){b=e+1}}
955-
END{print b}
956-
' /var/log/cloud-init-output.log) -f /var/log/cloud-init-output.log"`,
951+
`"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
952+
sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
953+
else
954+
sudo tail -n +$(sudo awk '
955+
BEGIN{b=1; e=1}
956+
/^Cloud-init.* finished/{e=NR}
957+
/.*/{if(NR>e){b=e+1}}
958+
END{print b}
959+
' /var/log/cloud-init-output.log) -f /var/log/cloud-init-output.log
960+
fi"`,
957961
)
958962

959963
cmd = exec.CommandContext(ctx, a.sshConfig.Binary(), args...)
@@ -971,6 +975,7 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
971975
}
972976

973977
scanner := bufio.NewScanner(stdout)
978+
cloudInitMainServiceStarted := false
974979
cloudInitFinished := false
975980

976981
for scanner.Scan() {
@@ -979,11 +984,19 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
979984
continue
980985
}
981986

982-
if !cloudInitFinished {
983-
if isCloudInitFinished(line) {
984-
logrus.Debug("Cloud-init completion detected via log pattern")
985-
cloudInitFinished = true
987+
if !cloudInitMainServiceStarted {
988+
if isStartedCloudInitMainService(line) {
989+
logrus.Debug("cloud-init-main.service started detected via log pattern")
990+
cloudInitMainServiceStarted = true
991+
} else if !cloudInitFinished {
992+
if isCloudInitFinished(line) {
993+
logrus.Debug("Cloud-init completion detected via log pattern")
994+
cloudInitFinished = true
995+
}
986996
}
997+
} else if !cloudInitFinished && isDeactivatedCloudInitMainService(line) {
998+
logrus.Debug("cloud-init-main.service deactivated detected via log pattern")
999+
cloudInitFinished = true
9871000
}
9881001

9891002
a.emitCloudInitProgressEvent(ctx, &events.CloudInitProgress{
@@ -1048,6 +1061,17 @@ func isCloudInitFinished(line string) bool {
10481061
return strings.Contains(line, "cloud-init") && strings.Contains(line, "finished")
10491062
}
10501063

1064+
func isStartedCloudInitMainService(line string) bool {
1065+
line = strings.ToLower(strings.TrimSpace(line))
1066+
return strings.HasPrefix(line, "started cloud-init-main.service")
1067+
}
1068+
1069+
func isDeactivatedCloudInitMainService(line string) bool {
1070+
line = strings.ToLower(strings.TrimSpace(line))
1071+
// Deactivated event lines end with a line reporting consumed CPU time, etc.
1072+
return strings.HasPrefix(line, "cloud-init-main.service: consumed")
1073+
}
1074+
10511075
func copyToHost(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local, remote string) error {
10521076
args := sshConfig.Args()
10531077
args = append(args,

0 commit comments

Comments
 (0)