예제 #1
0
func doSetupSSH(configModel config.MachineConfigModel) (config.SSHConfigModel, error) {
	log.Infoln("==> doSetupSSH")
	sshConfigModel := config.SSHConfigModel{}

	// Read `vagrant ssh-config` log/output
	outputs, err := utils.RunAndReturnCombinedOutput(MachineWorkdir.Get(), configModel.AllCmdEnvsForConfigType(MachineConfigTypeID.Get()), "vagrant", "ssh-config")
	if err != nil {
		log.Errorf("'vagrant ssh-config' failed with output: %s", outputs)
		return sshConfigModel, err
	}
	log.Debugln("===> (raw) vagrant ssh-config retrieved")

	// Convert `vagrant ssh-config` to our SSHConfigModel
	sshConfigModel, err = config.CreateSSHConfigFromVagrantSSHConfigLog(outputs)
	if err != nil {
		log.Errorf("'vagrant ssh-config' returned an invalid output (failed to scan SSH Config): %s", outputs)
		return sshConfigModel, err
	}
	log.Debugln("===> vagrant ssh-config parsed")

	// Generate SSH Keypair
	privBytes, pubBytes, err := utils.GenerateSSHKeypair()
	if err != nil {
		return sshConfigModel, err
	}
	log.Debugln("===> SSH Keypair generated")

	// Write the SSH Keypair to file
	privKeyFilePth, _, err := config.WriteSSHKeypairToFiles(MachineWorkdir.Get(), privBytes, pubBytes)
	if err != nil {
		return sshConfigModel, err
	}
	log.Debugln("===> SSH Keypair written to file")

	// Replace the ~/.ssh/authorized_keys inside the VM to only allow
	//  the new keypair
	replaceAuthKeysCmd := fmt.Sprintf(`printf "%s" > ~/.ssh/authorized_keys`, pubBytes)
	log.Debugf("===> Running command through SSH: %s", replaceAuthKeysCmd)
	if err := utils.RunCommandThroughSSH(sshConfigModel, replaceAuthKeysCmd); err != nil {
		return sshConfigModel, err
	}
	log.Debugln("===> SSH Keypair is now authorized to access the VM")

	// Save private key as the new identity
	sshConfigModel.IdentityPath = privKeyFilePth
	if err := sshConfigModel.WriteIntoFileInDir(MachineWorkdir.Get()); err != nil {
		return sshConfigModel, err
	}
	log.Debugln("===> New identity (private SSH key) saved into config in workdir")

	log.Debugln("==> doSetupSSH [done]")
	return sshConfigModel, nil
}
예제 #2
0
func getVagrantStatus(configModel config.MachineConfigModel) (vagrant.MachineReadableItem, error) {
	// Read `vagrant status` log/output
	outputs, err := utils.RunAndReturnCombinedOutput(MachineWorkdir.Get(),
		configModel.AllCmdEnvsForConfigType(MachineConfigTypeID.Get()),
		"vagrant", "status", "--machine-readable")
	if err != nil {
		return vagrant.MachineReadableItem{}, fmt.Errorf("'vagrant status' failed. Output was: %s", outputs)
	}
	statusItms := vagrant.ParseMachineReadableItemsFromString(outputs, "", "state")
	if len(statusItms) != 1 {
		return vagrant.MachineReadableItem{}, fmt.Errorf("Failed to determine the 'status' of the machine. Output was: %s", outputs)
	}
	return statusItms[0], nil
}
예제 #3
0
func doCustomCleanup(configModel config.MachineConfigModel) error {
	log.Infoln("Cleanup mode: custom-command")
	if configModel.CustomCleanupCommand == "" {
		return errors.New("cleanup mode was custom-command, but no custom cleanup command specified")
	}
	log.Infof("=> Specified custom command: %s", configModel.CustomCleanupCommand)

	// Read `vagrant status` log/output
	machineStatus := vagrant.MachineReadableItem{}
	if outputs, err := utils.RunAndReturnCombinedOutput(MachineWorkdir.Get(), configModel.AllCmdEnvsForConfigType(MachineConfigTypeID.Get()), "vagrant", "status", "--machine-readable"); err != nil {
		if err != nil {
			log.Errorf("'vagrant status' failed with output: %s", outputs)
			return err
		}
	} else {
		statusItms := vagrant.ParseMachineReadableItemsFromString(outputs, "", "state")
		if len(statusItms) != 1 {
			return fmt.Errorf("Failed to determine the 'status' of the machine. Output was: %s", outputs)
		}
		machineStatus = statusItms[0]
	}

	if machineStatus.Data == "not_created" {
		log.Infoln("Machine not yet created - creating with 'vagrant up'...")
		if err := utils.Run(MachineWorkdir.Get(), configModel.AllCmdEnvsForConfigType(MachineConfigTypeID.Get()), "vagrant", "up"); err != nil {
			return fmt.Errorf("'vagrant up' failed with error: %s", err)
		}
		log.Infoln("Machine created!")
	} else {
		log.Infof("Machine already created - using the specified custom-command (%s) to clean it up...", configModel.CustomCleanupCommand)
		if err := utils.Run(MachineWorkdir.Get(), configModel.AllCmdEnvsForConfigType(MachineConfigTypeID.Get()), "vagrant", configModel.CustomCleanupCommand); err != nil {
			return fmt.Errorf("'vagrant %s' failed with error: %s", configModel.CustomCleanupCommand, err)
		}
		log.Infoln("Successful custom cleanup")
	}

	log.Infoln("Machine created and ready!")
	return nil
}