github.com/DaoCloud/dao@v0.0.0-20161212064103-c3dbfd13ee36/plugin/backend.go (about)

     1  // +build experimental
     2  
     3  package plugin
     4  
     5  import (
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/Sirupsen/logrus"
    12  	"github.com/docker/docker/pkg/archive"
    13  	"github.com/docker/docker/pkg/stringid"
    14  	"github.com/docker/docker/plugin/distribution"
    15  	"github.com/docker/docker/reference"
    16  	"github.com/docker/engine-api/types"
    17  )
    18  
    19  // Disable deactivates a plugin, which implies that they cannot be used by containers.
    20  func (pm *Manager) Disable(name string) error {
    21  	p, err := pm.get(name)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	if err := pm.disable(p); err != nil {
    26  		return err
    27  	}
    28  	pm.pluginEventLogger(p.PluginObj.ID, name, "disable")
    29  	return nil
    30  }
    31  
    32  // Enable activates a plugin, which implies that they are ready to be used by containers.
    33  func (pm *Manager) Enable(name string) error {
    34  	p, err := pm.get(name)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	if err := pm.enable(p, false); err != nil {
    39  		return err
    40  	}
    41  	pm.pluginEventLogger(p.PluginObj.ID, name, "enable")
    42  	return nil
    43  }
    44  
    45  // Inspect examines a plugin manifest
    46  func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
    47  	p, err := pm.get(name)
    48  	if err != nil {
    49  		return tp, err
    50  	}
    51  	return p.PluginObj, nil
    52  }
    53  
    54  // Pull pulls a plugin and computes the privileges required to install it.
    55  func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
    56  	ref, err := reference.ParseNamed(name)
    57  	if err != nil {
    58  		logrus.Debugf("error in reference.ParseNamed: %v", err)
    59  		return nil, err
    60  	}
    61  	name = ref.String()
    62  
    63  	if p, _ := pm.get(name); p != nil {
    64  		logrus.Debugf("plugin already exists")
    65  		return nil, fmt.Errorf("%s exists", name)
    66  	}
    67  
    68  	pluginID := stringid.GenerateNonCryptoID()
    69  
    70  	if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
    71  		logrus.Debugf("error in MkdirAll: %v", err)
    72  		return nil, err
    73  	}
    74  
    75  	pd, err := distribution.Pull(name, pm.registryService, metaHeader, authConfig)
    76  	if err != nil {
    77  		logrus.Debugf("error in distribution.Pull(): %v", err)
    78  		return nil, err
    79  	}
    80  
    81  	if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
    82  		logrus.Debugf("error in distribution.WritePullData(): %v", err)
    83  		return nil, err
    84  	}
    85  
    86  	p := pm.newPlugin(ref, pluginID)
    87  	if err := pm.initPlugin(p); err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	pm.Lock()
    92  	pm.plugins[pluginID] = p
    93  	pm.nameToID[name] = pluginID
    94  	pm.save()
    95  	pm.Unlock()
    96  
    97  	pm.pluginEventLogger(pluginID, name, "pull")
    98  	return computePrivileges(&p.PluginObj.Manifest), nil
    99  }
   100  
   101  // List displays the list of plugins and associated metadata.
   102  func (pm *Manager) List() ([]types.Plugin, error) {
   103  	out := make([]types.Plugin, 0, len(pm.plugins))
   104  	for _, p := range pm.plugins {
   105  		out = append(out, p.PluginObj)
   106  	}
   107  	return out, nil
   108  }
   109  
   110  // Push pushes a plugin to the store.
   111  func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
   112  	p, err := pm.get(name)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	dest := filepath.Join(pm.libRoot, p.PluginObj.ID)
   117  	config, err := os.Open(filepath.Join(dest, "manifest.json"))
   118  	if err != nil {
   119  		return err
   120  	}
   121  	defer config.Close()
   122  
   123  	rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
   124  	if err != nil {
   125  		return err
   126  	}
   127  	_, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, config, rootfs)
   128  	// XXX: Ignore returning digest for now.
   129  	// Since digest needs to be written to the ProgressWriter.
   130  	return err
   131  }
   132  
   133  // Remove deletes plugin's root directory.
   134  func (pm *Manager) Remove(name string) error {
   135  	p, err := pm.get(name)
   136  	if err != nil {
   137  		return err
   138  	}
   139  	if err := pm.remove(p); err != nil {
   140  		return err
   141  	}
   142  	pm.pluginEventLogger(p.PluginObj.ID, name, "remove")
   143  	return nil
   144  }
   145  
   146  // Set sets plugin args
   147  func (pm *Manager) Set(name string, args []string) error {
   148  	p, err := pm.get(name)
   149  	if err != nil {
   150  		return err
   151  	}
   152  	return pm.set(p, args)
   153  }