func LoginToFedora(vmi *v1.VirtualMachineInstance) error {
	virtClient, err := kubecli.GetKubevirtClient()
	if err != nil {
		panic(err)
	}

	expecter, _, err := console.NewExpecter(virtClient, vmi, 10*time.Second)
	if err != nil {
		return err
	}
	defer expecter.Close()

	err = expecter.Send("\n")
	if err != nil {
		return err
	}

	// Do not login, if we already logged in
	b := append([]expect.Batcher{
		&expect.BSnd{S: "\n"},
		&expect.BExp{R: fmt.Sprintf(`(\[fedora@(localhost|%s) ~\]\$ |\[root@(localhost|%s) fedora\]\# )`, vmi.Name, vmi.Name)},
	})
	_, err = expecter.ExpectBatch(b, 5*time.Second)
	if err == nil {
		return nil
	}

	b = append([]expect.Batcher{
		&expect.BSnd{S: "\n"},
		&expect.BSnd{S: "\n"},
		&expect.BCas{C: []expect.Caser{
			&expect.Case{
				// Using only "login: " would match things like "Last failed login: Tue Jun  9 22:25:30 UTC 2020 on ttyS0"
				// and in case the VM's did not get hostname form DHCP server try the default hostname
				R:  regexp.MustCompile(fmt.Sprintf(`(localhost|%s) login: `, vmi.Name)),
				S:  "fedora\n",
				T:  expect.Next(),
				Rt: 10,
			},
			&expect.Case{
				R:  regexp.MustCompile(`Password:`),
				S:  "fedora\n",
				T:  expect.Next(),
				Rt: 10,
			},
			&expect.Case{
				R:  regexp.MustCompile(`Login incorrect`),
				T:  expect.LogContinue("Failed to log in", expect.NewStatus(codes.PermissionDenied, "login failed")),
				Rt: 10,
			},
			&expect.Case{
				R: regexp.MustCompile(fmt.Sprintf(`\[fedora@(localhost|%s) ~\]\$ `, vmi.Name)),
				T: expect.OK(),
			},
		}},
		&expect.BSnd{S: "sudo su\n"},
		&expect.BExp{R: console.PromptExpression},
	})
	res, err := expecter.ExpectBatch(b, 2*time.Minute)
	if err != nil {
		log.Printf("Login attempt failed: %+v", res)
		// Try once more since sometimes the login prompt is ripped apart by asynchronous daemon updates
		res, err := expecter.ExpectBatch(b, 1*time.Minute)
		if err != nil {
			log.Printf("Retried login attempt after two minutes failed: %+v", res)
			return err
		}
	}

	err = configureConsole(expecter, false)
	if err != nil {
		return err
	}
	return nil
}
// LoginToFedora performs a console login to a Fedora base VM
LoginToFedora performs a console login to a Fedora base VM
// LoginToFedora performs a console login to a Fedora base VM
func LoginToFedora(vmi *v1.VirtualMachineInstance) error {
	virtClient, err := kubecli.GetKubevirtClient()
	if err != nil {
		panic(err)
	}
	expecter, _, err := console.NewExpecter(virtClient, vmi, 10*time.Second)
	if err != nil {
		return err
	}
	defer expecter.Close()
	err = expecter.Send("\n")
	if err != nil {
		return err
	}
	b := append([]expect.Batcher{
		&expect.BSnd{S: "\n"},
		&expect.BExp{R: fmt.Sprintf(`(\[fedora@(localhost|%s) ~\]\$ |\[root@(localhost|%s) fedora\]\# )`, vmi.Name, vmi.Name)},
	})
	_, err = expecter.ExpectBatch(b, 5*time.Second)
	if err == nil {
		return nil
	}
	b = append([]expect.Batcher{
		&expect.BSnd{S: "\n"},
		&expect.BSnd{S: "\n"},
		&expect.BCas{C: []expect.Caser{
			&expect.Case{
				R:  regexp.MustCompile(fmt.Sprintf(`(localhost|%s) login: `, vmi.Name)),
				S:  "fedora\n",
				T:  expect.Next(),
				Rt: 10,
			},
			&expect.Case{
				R:  regexp.MustCompile(`Password:`),
				S:  "fedora\n",
				T:  expect.Next(),
				Rt: 10,
			},
			&expect.Case{
				R:  regexp.MustCompile(`Login incorrect`),
				T:  expect.LogContinue("Failed to log in", expect.NewStatus(codes.PermissionDenied, "login failed")),
				Rt: 10,
			},
			&expect.Case{
				R: regexp.MustCompile(fmt.Sprintf(`\[fedora@(localhost|%s) ~\]\$ `, vmi.Name)),
				T: expect.OK(),
			},
		}},
		&expect.BSnd{S: "sudo su\n"},
		&expect.BExp{R: console.PromptExpression},
	})
	res, err := expecter.ExpectBatch(b, 2*time.Minute)
	if err != nil {
		log.Printf("Login attempt failed: %+v", res)
		res, err := expecter.ExpectBatch(b, 1*time.Minute)
		if err != nil {
			log.Printf("Retried login attempt after two minutes failed: %+v", res)
			return err
		}
	}
	err = configureConsole(expecter, false)
	if err != nil {
		return err
	}
	return nil
}
func LoginToFedora ( vmi * v1 . VirtualMachineInstance ) error { virtClient , err := kubecli . GetKubevirtClient ( ) if err != nil { panic ( err ) } expecter , _ , err := console . NewExpecter ( virtClient , vmi , 10 * time . Second ) if err != nil { return err } defer expecter . Close ( ) err = expecter . Send ( " \n " ) if err != nil { return err } b := append ( [ ] expect . Batcher { & expect . BSnd { S : " \n " } , & expect . BExp { R : fmt . Sprintf ( `(\[fedora@(localhost|%s) ~\]\$ |\[root@(localhost|%s) fedora\]\# )` , vmi . Name , vmi . Name ) } , } ) _ , err = expecter . ExpectBatch ( b , 5 * time . Second ) if err == nil { return nil } b = append ( [ ] expect . Batcher { & expect . BSnd { S : " \n " } , & expect . BSnd { S : " \n " } , & expect . BCas { C : [ ] expect . Caser { & expect . Case { R : regexp . MustCompile ( fmt . Sprintf ( `(localhost|%s) login: ` , vmi . Name ) ) , S : " \n " , T : expect . Next ( ) , Rt : 10 , } , & expect . Case { R : regexp . MustCompile ( `Password:` ) , S : " \n " , T : expect . Next ( ) , Rt : 10 , } , & expect . Case { R : regexp . MustCompile ( `Login incorrect` ) , T : expect . LogContinue ( " " , expect . NewStatus ( codes . PermissionDenied , " " ) ) , Rt : 10 , } , & expect . Case { R : regexp . MustCompile ( fmt . Sprintf ( `\[fedora@(localhost|%s) ~\]\$ ` , vmi . Name ) ) , T : expect . OK ( ) , } , } } , & expect . BSnd { S : " \n " } , & expect . BExp { R : console . PromptExpression } , } ) res , err := expecter . ExpectBatch ( b , 2 * time . Minute ) if err != nil { log . Printf ( " " , res ) res , err := expecter . ExpectBatch ( b , 1 * time . Minute ) if err != nil { log . Printf ( " " , res ) return err } } err = configureConsole ( expecter , false ) if err != nil { return err } return nil }
LoginToFedora performs a console login to a Fedora base VM
LoginToFedora performs a console login to a Fedora base VM
// Do not login, if we already logged in // Using only "login: " would match things like "Last failed login: Tue Jun  9 22:25:30 UTC 2020 on ttyS0" // and in case the VM's did not get hostname form DHCP server try the default hostname // Try once more since sometimes the login prompt is ripped apart by asynchronous daemon updates