Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ var createCmd = &cobra.Command{
}
}

remote.WaitForCloudInit()
remote.WaitForCloudInit(viper.GetString("vm.cloud-init.timeout"))
s.Stop()
fmt.Println("\033[32m\u2714\033[0m VM is Ready")
log.Println("[DEBUG] cloud-init finished")
Expand Down
4 changes: 2 additions & 2 deletions internal/cloud/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (p ProviderAzure) List() (VmList, error) {
log.Fatalf("failed to finish the request: %v", err)
} else {
// Print the obtained query results
log.Printf("[DEBUG] Resources found: " + strconv.FormatInt(*resp.TotalRecords, 10) + "\n")
log.Printf("[DEBUG] Results: " + fmt.Sprint(resp.Data) + "\n")
log.Printf("[DEBUG] Resources found: %d\n", *resp.TotalRecords)
log.Printf("[DEBUG] Results: %v\n", resp.Data)
}
if len(strconv.FormatInt(*resp.TotalRecords, 10)) == 0 {
return VmList{}, nil
Expand Down
2 changes: 2 additions & 0 deletions internal/files/init/onctl.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
vm:
name: onctl-vm
cloud-init:
timeout: 3m # ex. 3m or 180s
ssh:
# publicKey: ~/.ssh/id_ed25519.pub
# privateKey: ~/.ssh/id_ed25519
Expand Down
2 changes: 1 addition & 1 deletion internal/tools/cicd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GenerateMachineUniqueName() string {
func GenerateUserName() string {
userCurrent, err := user.Current()
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%s", err.Error())
}
userName := strings.ReplaceAll(userCurrent.Username, "\\", "-")
userName = strings.ReplaceAll(userName, " ", "-")
Expand Down
44 changes: 25 additions & 19 deletions internal/tools/cloud-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,34 @@ func FileToBase64(filepath string) string {
}

// WaitForCloudInit waits for cloud-init to finish
func (r *Remote) WaitForCloudInit() {
var tries int

func (r *Remote) WaitForCloudInit(timeout string) {
log.Println("[DEBUG] Waiting for cloud-init to finish timeout:", timeout)
command := "[ -f /run/cloud-init/result.json ] && echo -n \"OK\""
for {

isOK, err := r.RemoteRun(&RemoteRunConfig{
Command: command,
})
if err != nil {
log.Println("[DEBUG] RemoteRun:" + err.Error())
}
if err == nil {
if isOK == "OK" {
break
// Parse the timeout string into a time.Duration
duration, err := time.ParseDuration(timeout)
if err != nil {
log.Fatalf("Invalid timeout value: %v", err)
}

timer := time.After(duration)

for {
select {
case <-timer:
log.Fatalln("Exiting.. Timeout reached while waiting for cloud-init to finish on IP " + r.IPAddress + " on port " + strconv.Itoa(r.SSHPort))
return
default:
isOK, err := r.RemoteRun(&RemoteRunConfig{
Command: command,
})
if err != nil {
log.Println("[DEBUG] RemoteRun:" + err.Error())
}
}
time.Sleep(3 * time.Second)
tries++
log.Println("[DEBUG] :" + strconv.Itoa(tries))
if tries > 15 {
log.Fatalln("Exiting.. Could not connect to IP " + r.IPAddress + " on port " + strconv.Itoa(r.SSHPort))
if err == nil && isOK == "OK" {
return
}
time.Sleep(3 * time.Second)
}
}
}