Beispiel #1
0
func (cmd *mkdir) Run(ctx context.Context, f *flag.FlagSet) error {
	args := f.Args()
	if len(args) == 0 {
		return errors.New("missing operand")
	}

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	if cmd.isNamespace {
		var uuid string
		var ds *object.Datastore

		if ds, err = cmd.Datastore(); err != nil {
			return err
		}

		path := args[0]

		nm := object.NewDatastoreNamespaceManager(c)
		if uuid, err = nm.CreateDirectory(ctx, ds, path, ""); err != nil {
			return err
		}

		fmt.Println(uuid)
	} else {
		var dc *object.Datacenter
		var path string

		dc, err = cmd.Datacenter()
		if err != nil {
			return err
		}

		path, err = cmd.DatastorePath(args[0])
		if err != nil {
			return err
		}

		m := object.NewFileManager(c)
		err = m.MakeDirectory(ctx, path, dc, cmd.createParents)

		// ignore EEXIST if -p flag is given
		if err != nil && cmd.createParents {
			if soap.IsSoapFault(err) {
				soapFault := soap.ToSoapFault(err)
				if _, ok := soapFault.VimFault().(types.FileAlreadyExists); ok {
					return nil
				}
			}
		}
	}

	return err
}
Beispiel #2
0
// This creates the root directory in the datastore and sets the rooturl and
// rootdir in the datastore struct so we can reuse it for other routines.  This
// handles vsan + vc, vsan + esx, and esx.  The URI conventions are not the
// same for each and this tries to create the directory and stash the relevant
// result so the URI doesn't need to be recomputed for every datastore
// operation.
func (d *Helper) mkRootDir(ctx context.Context, rootdir string) error {
	if rootdir == "" {
		return fmt.Errorf("root directory is empty")
	}

	if path.IsAbs(rootdir) {
		return fmt.Errorf("root directory (%s) must not be an absolute path", rootdir)
	}

	// Handle vsan
	// Vsan will complain if the root dir exists.  Just call it directly and
	// swallow the error if it's already there.
	if d.IsVSAN(ctx) {
		comps := strings.Split(rootdir, "/")

		nm := object.NewDatastoreNamespaceManager(d.s.Vim25())

		// This returns the vmfs path (including the datastore and directory
		// UUIDs).  Use the directory UUID in future operations because it is
		// the stable path which we can use regardless of vsan state.
		uuid, err := nm.CreateDirectory(ctx, d.ds, comps[0], "")
		if err != nil {
			if !soap.IsSoapFault(err) {
				return err
			}

			soapFault := soap.ToSoapFault(err)
			if _, ok := soapFault.VimFault().(types.FileAlreadyExists); !ok {
				return err
			}

			// XXX UGLY HACK until we move this into the installer.  Use the
			// display name if the dir exists since we can't get the UUID after the
			// directory is created.
			uuid = comps[0]
			err = nil
		}

		rootdir = path.Join(path.Base(uuid), path.Join(comps[1:]...))
	}

	rooturl := d.ds.Path(rootdir)

	// create the rest of the root dir in case of vSAN, otherwise
	// create the full path
	if _, err := mkdir(ctx, d.s, d.fm, true, rooturl); err != nil {
		if !os.IsExist(err) {
			return err
		}

		log.Infof("datastore root %s already exists", rooturl)
	}

	d.RootURL = rooturl
	return nil
}
Beispiel #3
0
// This creates the root directory in the datastore and sets the rooturl and
// rootdir in the datastore struct so we can reuse it for other routines.  This
// handles vsan + vc, vsan + esx, and esx.  The URI conventions are not the
// same for each and this tries to create the directory and stash the relevant
// result so the URI doesn't need to be recomputed for every datastore
// operation.
func (d *Helper) mkRootDir(ctx context.Context, rootdir string) error {

	// Handle vsan
	// Vsan will complain if the root dir exists.  Just call it directly and
	// swallow the error if it's already there.
	if d.IsVSAN(ctx) {
		nm := object.NewDatastoreNamespaceManager(d.s.Vim25())

		// This returns the vmfs path (including the datastore and directory
		// UUIDs).  Use the directory UUID in future operations because it is
		// the stable path which we can use regardless of vsan state.
		uuid, err := nm.CreateDirectory(ctx, d.ds, rootdir, "")
		if err != nil {
			if !soap.IsSoapFault(err) {
				return err
			}

			soapFault := soap.ToSoapFault(err)
			_, ok := soapFault.VimFault().(types.FileAlreadyExists)
			if ok {
				// XXX UGLY HACK until we move this into the installer.  Use the
				// display name if the dir exists since we can't get the UUID after the
				// directory is created.

				uuid = rootdir
				err = nil
			} else {
				return err
			}
		}

		// set the root url to the UUID of the dir we created
		d.RootURL = d.ds.Path(path.Base(uuid))
		log.Infof("Created store parent directory (%s) at %s", rootdir, d.RootURL)
	} else {

		// Handle regular local datastore
		// check if it already exists

		d.RootURL = d.ds.Path(rootdir)
		if _, err := d.Mkdir(ctx, true); err != nil {
			if os.IsExist(err) {
				log.Debugf("%s already exists", d.RootURL)
				return nil
			}
			return err
		}
	}

	return nil
}
Beispiel #4
0
func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
	args := f.Args()
	if len(args) == 0 {
		return errors.New("missing operand")
	}

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	var dc *object.Datacenter
	dc, err = cmd.Datacenter()
	if err != nil {
		return err
	}

	if cmd.isNamespace {
		path := args[0]

		nm := object.NewDatastoreNamespaceManager(c)
		err = nm.DeleteDirectory(ctx, dc, path)
	} else {
		var path string
		var task *object.Task

		// TODO(PN): Accept multiple args
		path, err = cmd.DatastorePath(args[0])
		if err != nil {
			return err
		}

		m := object.NewFileManager(c)
		task, err = m.DeleteDatastoreFile(ctx, path, dc)
		if err != nil {
			return err
		}

		err = task.Wait(ctx)
	}

	if err != nil {
		if types.IsFileNotFound(err) && cmd.force {
			// Ignore error
			return nil
		}
	}

	return err
}