コード例 #1
0
ファイル: vsphere.go プロジェクト: containerx/machine
//
// uploadBundle creates and uploads the ssh key tar bundle to the VM
// using the ProcessManager.
//
// Parameters:
//   vmMoRef: ManagedObjectReference of the VM to which the bundle has to be
//     uploaded
//   ctx: The context for this API call
//   client: Client object that contains the vSphere connection
//
// Returns:
//   (error): errors from generating, uploading bundles from OperationsManager
//      and ProcessManager
//
func (d *Driver) uploadBundle(vmMoRef types.ManagedObjectReference, ctx context.Context, client *vim25.Client) error {
	log.Infof("Provisioning certs and ssh keys...")
	// Generate a tar keys bundle
	if err := d.generateKeyBundle(); err != nil {
		return err
	}

	opman := guest.NewOperationsManager(client, vmMoRef)

	fileman, err := opman.FileManager(ctx)
	if err != nil {
		return err
	}

	src := d.ResolveStorePath("userdata.tar")
	s, err := os.Stat(src)
	if err != nil {
		return err
	}

	auth := AuthFlag{}
	flag := FileAttrFlag{}
	auth.auth.Username = B2DUser
	auth.auth.Password = B2DPass
	flag.SetPerms(0, 0, 660)

	log.Infof("Uploading the tar bundle to the VM")
	url, err := fileman.InitiateFileTransferToGuest(ctx, auth.Auth(), "/tmp/userdata.tar", flag.Attr(), s.Size(), true)
	if err != nil {
		return err
	}
	u, err := client.ParseURL(url)
	if err != nil {
		return err
	}
	if err = client.UploadFile(src, u, nil); err != nil {
		return err
	}

	procman, err := opman.ProcessManager(ctx)
	if err != nil {
		return err
	}

	var env []string
	guestspec := types.GuestProgramSpec{
		ProgramPath:      "/usr/bin/sudo",
		Arguments:        "tar xf /tmp/userdata.tar -C /home/docker/",
		WorkingDirectory: "",
		EnvVariables:     env,
	}

	log.Debugf("Unbundling the keys into user directory")
	pid, err := procman.StartProgram(ctx, auth.Auth(), &guestspec)
	if err != nil {
		return err
	}

	// Wait for tar to complete
	pids := []int64{pid}
	done := false
	for done != true {
		procs, err := procman.ListProcesses(ctx, auth.Auth(), pids)
		if err != nil {
			return err
		}
		if procs[0].EndTime != nil {
			done = true
		}
	}

	guestspec = types.GuestProgramSpec{
		ProgramPath:      "/usr/bin/sudo",
		Arguments:        "chown -R docker:staff /home/docker",
		WorkingDirectory: "",
		EnvVariables:     env,
	}

	log.Debugf("Setting permissions for untarred files")
	_, err = procman.StartProgram(ctx, auth.Auth(), &guestspec)
	if err != nil {
		log.Debugf("Error Setting permissions for untarred files")
		return err
	}

	return nil
}