backend_linux.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. package plugin // import "github.com/docker/docker/plugin"
  2. import (
  3. "archive/tar"
  4. "compress/gzip"
  5. "context"
  6. "encoding/json"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "runtime"
  14. "strings"
  15. "github.com/docker/distribution/manifest/schema2"
  16. "github.com/docker/distribution/reference"
  17. "github.com/docker/docker/api/types"
  18. "github.com/docker/docker/api/types/filters"
  19. "github.com/docker/docker/distribution"
  20. progressutils "github.com/docker/docker/distribution/utils"
  21. "github.com/docker/docker/distribution/xfer"
  22. "github.com/docker/docker/dockerversion"
  23. "github.com/docker/docker/errdefs"
  24. "github.com/docker/docker/image"
  25. "github.com/docker/docker/layer"
  26. "github.com/docker/docker/pkg/authorization"
  27. "github.com/docker/docker/pkg/chrootarchive"
  28. "github.com/docker/docker/pkg/mount"
  29. "github.com/docker/docker/pkg/pools"
  30. "github.com/docker/docker/pkg/progress"
  31. "github.com/docker/docker/pkg/system"
  32. "github.com/docker/docker/plugin/v2"
  33. refstore "github.com/docker/docker/reference"
  34. "github.com/opencontainers/go-digest"
  35. specs "github.com/opencontainers/image-spec/specs-go/v1"
  36. "github.com/pkg/errors"
  37. "github.com/sirupsen/logrus"
  38. )
  39. var acceptedPluginFilterTags = map[string]bool{
  40. "enabled": true,
  41. "capability": true,
  42. }
  43. // Disable deactivates a plugin. This means resources (volumes, networks) cant use them.
  44. func (pm *Manager) Disable(refOrID string, config *types.PluginDisableConfig) error {
  45. p, err := pm.config.Store.GetV2Plugin(refOrID)
  46. if err != nil {
  47. return err
  48. }
  49. pm.mu.RLock()
  50. c := pm.cMap[p]
  51. pm.mu.RUnlock()
  52. if !config.ForceDisable && p.GetRefCount() > 0 {
  53. return errors.WithStack(inUseError(p.Name()))
  54. }
  55. for _, typ := range p.GetTypes() {
  56. if typ.Capability == authorization.AuthZApiImplements {
  57. pm.config.AuthzMiddleware.RemovePlugin(p.Name())
  58. }
  59. }
  60. if err := pm.disable(p, c); err != nil {
  61. return err
  62. }
  63. pm.publisher.Publish(EventDisable{Plugin: p.PluginObj})
  64. pm.config.LogPluginEvent(p.GetID(), refOrID, "disable")
  65. return nil
  66. }
  67. // Enable activates a plugin, which implies that they are ready to be used by containers.
  68. func (pm *Manager) Enable(refOrID string, config *types.PluginEnableConfig) error {
  69. p, err := pm.config.Store.GetV2Plugin(refOrID)
  70. if err != nil {
  71. return err
  72. }
  73. c := &controller{timeoutInSecs: config.Timeout}
  74. if err := pm.enable(p, c, false); err != nil {
  75. return err
  76. }
  77. pm.publisher.Publish(EventEnable{Plugin: p.PluginObj})
  78. pm.config.LogPluginEvent(p.GetID(), refOrID, "enable")
  79. return nil
  80. }
  81. // Inspect examines a plugin config
  82. func (pm *Manager) Inspect(refOrID string) (tp *types.Plugin, err error) {
  83. p, err := pm.config.Store.GetV2Plugin(refOrID)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &p.PluginObj, nil
  88. }
  89. func (pm *Manager) pull(ctx context.Context, ref reference.Named, config *distribution.ImagePullConfig, outStream io.Writer) error {
  90. if outStream != nil {
  91. // Include a buffer so that slow client connections don't affect
  92. // transfer performance.
  93. progressChan := make(chan progress.Progress, 100)
  94. writesDone := make(chan struct{})
  95. defer func() {
  96. close(progressChan)
  97. <-writesDone
  98. }()
  99. var cancelFunc context.CancelFunc
  100. ctx, cancelFunc = context.WithCancel(ctx)
  101. go func() {
  102. progressutils.WriteDistributionProgress(cancelFunc, outStream, progressChan)
  103. close(writesDone)
  104. }()
  105. config.ProgressOutput = progress.ChanOutput(progressChan)
  106. } else {
  107. config.ProgressOutput = progress.DiscardOutput()
  108. }
  109. return distribution.Pull(ctx, ref, config)
  110. }
  111. type tempConfigStore struct {
  112. config []byte
  113. configDigest digest.Digest
  114. }
  115. func (s *tempConfigStore) Put(c []byte) (digest.Digest, error) {
  116. dgst := digest.FromBytes(c)
  117. s.config = c
  118. s.configDigest = dgst
  119. return dgst, nil
  120. }
  121. func (s *tempConfigStore) Get(d digest.Digest) ([]byte, error) {
  122. if d != s.configDigest {
  123. return nil, errNotFound("digest not found")
  124. }
  125. return s.config, nil
  126. }
  127. func (s *tempConfigStore) RootFSFromConfig(c []byte) (*image.RootFS, error) {
  128. return configToRootFS(c)
  129. }
  130. func (s *tempConfigStore) PlatformFromConfig(c []byte) (*specs.Platform, error) {
  131. // TODO: LCOW/Plugins. This will need revisiting. For now use the runtime OS
  132. return &specs.Platform{OS: runtime.GOOS}, nil
  133. }
  134. func computePrivileges(c types.PluginConfig) types.PluginPrivileges {
  135. var privileges types.PluginPrivileges
  136. if c.Network.Type != "null" && c.Network.Type != "bridge" && c.Network.Type != "" {
  137. privileges = append(privileges, types.PluginPrivilege{
  138. Name: "network",
  139. Description: "permissions to access a network",
  140. Value: []string{c.Network.Type},
  141. })
  142. }
  143. if c.IpcHost {
  144. privileges = append(privileges, types.PluginPrivilege{
  145. Name: "host ipc namespace",
  146. Description: "allow access to host ipc namespace",
  147. Value: []string{"true"},
  148. })
  149. }
  150. if c.PidHost {
  151. privileges = append(privileges, types.PluginPrivilege{
  152. Name: "host pid namespace",
  153. Description: "allow access to host pid namespace",
  154. Value: []string{"true"},
  155. })
  156. }
  157. for _, mount := range c.Mounts {
  158. if mount.Source != nil {
  159. privileges = append(privileges, types.PluginPrivilege{
  160. Name: "mount",
  161. Description: "host path to mount",
  162. Value: []string{*mount.Source},
  163. })
  164. }
  165. }
  166. for _, device := range c.Linux.Devices {
  167. if device.Path != nil {
  168. privileges = append(privileges, types.PluginPrivilege{
  169. Name: "device",
  170. Description: "host device to access",
  171. Value: []string{*device.Path},
  172. })
  173. }
  174. }
  175. if c.Linux.AllowAllDevices {
  176. privileges = append(privileges, types.PluginPrivilege{
  177. Name: "allow-all-devices",
  178. Description: "allow 'rwm' access to all devices",
  179. Value: []string{"true"},
  180. })
  181. }
  182. if len(c.Linux.Capabilities) > 0 {
  183. privileges = append(privileges, types.PluginPrivilege{
  184. Name: "capabilities",
  185. Description: "list of additional capabilities required",
  186. Value: c.Linux.Capabilities,
  187. })
  188. }
  189. return privileges
  190. }
  191. // Privileges pulls a plugin config and computes the privileges required to install it.
  192. func (pm *Manager) Privileges(ctx context.Context, ref reference.Named, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
  193. // create image store instance
  194. cs := &tempConfigStore{}
  195. // DownloadManager not defined because only pulling configuration.
  196. pluginPullConfig := &distribution.ImagePullConfig{
  197. Config: distribution.Config{
  198. MetaHeaders: metaHeader,
  199. AuthConfig: authConfig,
  200. RegistryService: pm.config.RegistryService,
  201. ImageEventLogger: func(string, string, string) {},
  202. ImageStore: cs,
  203. },
  204. Schema2Types: distribution.PluginTypes,
  205. }
  206. if err := pm.pull(ctx, ref, pluginPullConfig, nil); err != nil {
  207. return nil, err
  208. }
  209. if cs.config == nil {
  210. return nil, errors.New("no configuration pulled")
  211. }
  212. var config types.PluginConfig
  213. if err := json.Unmarshal(cs.config, &config); err != nil {
  214. return nil, errdefs.System(err)
  215. }
  216. return computePrivileges(config), nil
  217. }
  218. // Upgrade upgrades a plugin
  219. func (pm *Manager) Upgrade(ctx context.Context, ref reference.Named, name string, metaHeader http.Header, authConfig *types.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer) (err error) {
  220. p, err := pm.config.Store.GetV2Plugin(name)
  221. if err != nil {
  222. return err
  223. }
  224. if p.IsEnabled() {
  225. return errors.Wrap(enabledError(p.Name()), "plugin must be disabled before upgrading")
  226. }
  227. pm.muGC.RLock()
  228. defer pm.muGC.RUnlock()
  229. // revalidate because Pull is public
  230. if _, err := reference.ParseNormalizedNamed(name); err != nil {
  231. return errors.Wrapf(errdefs.InvalidParameter(err), "failed to parse %q", name)
  232. }
  233. tmpRootFSDir, err := ioutil.TempDir(pm.tmpDir(), ".rootfs")
  234. if err != nil {
  235. return errors.Wrap(errdefs.System(err), "error preparing upgrade")
  236. }
  237. defer os.RemoveAll(tmpRootFSDir)
  238. dm := &downloadManager{
  239. tmpDir: tmpRootFSDir,
  240. blobStore: pm.blobStore,
  241. }
  242. pluginPullConfig := &distribution.ImagePullConfig{
  243. Config: distribution.Config{
  244. MetaHeaders: metaHeader,
  245. AuthConfig: authConfig,
  246. RegistryService: pm.config.RegistryService,
  247. ImageEventLogger: pm.config.LogPluginEvent,
  248. ImageStore: dm,
  249. },
  250. DownloadManager: dm, // todo: reevaluate if possible to substitute distribution/xfer dependencies instead
  251. Schema2Types: distribution.PluginTypes,
  252. }
  253. err = pm.pull(ctx, ref, pluginPullConfig, outStream)
  254. if err != nil {
  255. go pm.GC()
  256. return err
  257. }
  258. if err := pm.upgradePlugin(p, dm.configDigest, dm.blobs, tmpRootFSDir, &privileges); err != nil {
  259. return err
  260. }
  261. p.PluginObj.PluginReference = ref.String()
  262. return nil
  263. }
  264. // Pull pulls a plugin, check if the correct privileges are provided and install the plugin.
  265. func (pm *Manager) Pull(ctx context.Context, ref reference.Named, name string, metaHeader http.Header, authConfig *types.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer, opts ...CreateOpt) (err error) {
  266. pm.muGC.RLock()
  267. defer pm.muGC.RUnlock()
  268. // revalidate because Pull is public
  269. nameref, err := reference.ParseNormalizedNamed(name)
  270. if err != nil {
  271. return errors.Wrapf(errdefs.InvalidParameter(err), "failed to parse %q", name)
  272. }
  273. name = reference.FamiliarString(reference.TagNameOnly(nameref))
  274. if err := pm.config.Store.validateName(name); err != nil {
  275. return errdefs.InvalidParameter(err)
  276. }
  277. tmpRootFSDir, err := ioutil.TempDir(pm.tmpDir(), ".rootfs")
  278. if err != nil {
  279. return errors.Wrap(errdefs.System(err), "error preparing pull")
  280. }
  281. defer os.RemoveAll(tmpRootFSDir)
  282. dm := &downloadManager{
  283. tmpDir: tmpRootFSDir,
  284. blobStore: pm.blobStore,
  285. }
  286. pluginPullConfig := &distribution.ImagePullConfig{
  287. Config: distribution.Config{
  288. MetaHeaders: metaHeader,
  289. AuthConfig: authConfig,
  290. RegistryService: pm.config.RegistryService,
  291. ImageEventLogger: pm.config.LogPluginEvent,
  292. ImageStore: dm,
  293. },
  294. DownloadManager: dm, // todo: reevaluate if possible to substitute distribution/xfer dependencies instead
  295. Schema2Types: distribution.PluginTypes,
  296. }
  297. err = pm.pull(ctx, ref, pluginPullConfig, outStream)
  298. if err != nil {
  299. go pm.GC()
  300. return err
  301. }
  302. refOpt := func(p *v2.Plugin) {
  303. p.PluginObj.PluginReference = ref.String()
  304. }
  305. optsList := make([]CreateOpt, 0, len(opts)+1)
  306. optsList = append(optsList, opts...)
  307. optsList = append(optsList, refOpt)
  308. p, err := pm.createPlugin(name, dm.configDigest, dm.blobs, tmpRootFSDir, &privileges, optsList...)
  309. if err != nil {
  310. return err
  311. }
  312. pm.publisher.Publish(EventCreate{Plugin: p.PluginObj})
  313. return nil
  314. }
  315. // List displays the list of plugins and associated metadata.
  316. func (pm *Manager) List(pluginFilters filters.Args) ([]types.Plugin, error) {
  317. if err := pluginFilters.Validate(acceptedPluginFilterTags); err != nil {
  318. return nil, err
  319. }
  320. enabledOnly := false
  321. disabledOnly := false
  322. if pluginFilters.Contains("enabled") {
  323. if pluginFilters.ExactMatch("enabled", "true") {
  324. enabledOnly = true
  325. } else if pluginFilters.ExactMatch("enabled", "false") {
  326. disabledOnly = true
  327. } else {
  328. return nil, invalidFilter{"enabled", pluginFilters.Get("enabled")}
  329. }
  330. }
  331. plugins := pm.config.Store.GetAll()
  332. out := make([]types.Plugin, 0, len(plugins))
  333. next:
  334. for _, p := range plugins {
  335. if enabledOnly && !p.PluginObj.Enabled {
  336. continue
  337. }
  338. if disabledOnly && p.PluginObj.Enabled {
  339. continue
  340. }
  341. if pluginFilters.Contains("capability") {
  342. for _, f := range p.GetTypes() {
  343. if !pluginFilters.Match("capability", f.Capability) {
  344. continue next
  345. }
  346. }
  347. }
  348. out = append(out, p.PluginObj)
  349. }
  350. return out, nil
  351. }
  352. // Push pushes a plugin to the store.
  353. func (pm *Manager) Push(ctx context.Context, name string, metaHeader http.Header, authConfig *types.AuthConfig, outStream io.Writer) error {
  354. p, err := pm.config.Store.GetV2Plugin(name)
  355. if err != nil {
  356. return err
  357. }
  358. ref, err := reference.ParseNormalizedNamed(p.Name())
  359. if err != nil {
  360. return errors.Wrapf(err, "plugin has invalid name %v for push", p.Name())
  361. }
  362. var po progress.Output
  363. if outStream != nil {
  364. // Include a buffer so that slow client connections don't affect
  365. // transfer performance.
  366. progressChan := make(chan progress.Progress, 100)
  367. writesDone := make(chan struct{})
  368. defer func() {
  369. close(progressChan)
  370. <-writesDone
  371. }()
  372. var cancelFunc context.CancelFunc
  373. ctx, cancelFunc = context.WithCancel(ctx)
  374. go func() {
  375. progressutils.WriteDistributionProgress(cancelFunc, outStream, progressChan)
  376. close(writesDone)
  377. }()
  378. po = progress.ChanOutput(progressChan)
  379. } else {
  380. po = progress.DiscardOutput()
  381. }
  382. // TODO: replace these with manager
  383. is := &pluginConfigStore{
  384. pm: pm,
  385. plugin: p,
  386. }
  387. lss := make(map[string]distribution.PushLayerProvider)
  388. lss[runtime.GOOS] = &pluginLayerProvider{
  389. pm: pm,
  390. plugin: p,
  391. }
  392. rs := &pluginReference{
  393. name: ref,
  394. pluginID: p.Config,
  395. }
  396. uploadManager := xfer.NewLayerUploadManager(3)
  397. imagePushConfig := &distribution.ImagePushConfig{
  398. Config: distribution.Config{
  399. MetaHeaders: metaHeader,
  400. AuthConfig: authConfig,
  401. ProgressOutput: po,
  402. RegistryService: pm.config.RegistryService,
  403. ReferenceStore: rs,
  404. ImageEventLogger: pm.config.LogPluginEvent,
  405. ImageStore: is,
  406. RequireSchema2: true,
  407. },
  408. ConfigMediaType: schema2.MediaTypePluginConfig,
  409. LayerStores: lss,
  410. UploadManager: uploadManager,
  411. }
  412. return distribution.Push(ctx, ref, imagePushConfig)
  413. }
  414. type pluginReference struct {
  415. name reference.Named
  416. pluginID digest.Digest
  417. }
  418. func (r *pluginReference) References(id digest.Digest) []reference.Named {
  419. if r.pluginID != id {
  420. return nil
  421. }
  422. return []reference.Named{r.name}
  423. }
  424. func (r *pluginReference) ReferencesByName(ref reference.Named) []refstore.Association {
  425. return []refstore.Association{
  426. {
  427. Ref: r.name,
  428. ID: r.pluginID,
  429. },
  430. }
  431. }
  432. func (r *pluginReference) Get(ref reference.Named) (digest.Digest, error) {
  433. if r.name.String() != ref.String() {
  434. return digest.Digest(""), refstore.ErrDoesNotExist
  435. }
  436. return r.pluginID, nil
  437. }
  438. func (r *pluginReference) AddTag(ref reference.Named, id digest.Digest, force bool) error {
  439. // Read only, ignore
  440. return nil
  441. }
  442. func (r *pluginReference) AddDigest(ref reference.Canonical, id digest.Digest, force bool) error {
  443. // Read only, ignore
  444. return nil
  445. }
  446. func (r *pluginReference) Delete(ref reference.Named) (bool, error) {
  447. // Read only, ignore
  448. return false, nil
  449. }
  450. type pluginConfigStore struct {
  451. pm *Manager
  452. plugin *v2.Plugin
  453. }
  454. func (s *pluginConfigStore) Put([]byte) (digest.Digest, error) {
  455. return digest.Digest(""), errors.New("cannot store config on push")
  456. }
  457. func (s *pluginConfigStore) Get(d digest.Digest) ([]byte, error) {
  458. if s.plugin.Config != d {
  459. return nil, errors.New("plugin not found")
  460. }
  461. rwc, err := s.pm.blobStore.Get(d)
  462. if err != nil {
  463. return nil, err
  464. }
  465. defer rwc.Close()
  466. return ioutil.ReadAll(rwc)
  467. }
  468. func (s *pluginConfigStore) RootFSFromConfig(c []byte) (*image.RootFS, error) {
  469. return configToRootFS(c)
  470. }
  471. func (s *pluginConfigStore) PlatformFromConfig(c []byte) (*specs.Platform, error) {
  472. // TODO: LCOW/Plugins. This will need revisiting. For now use the runtime OS
  473. return &specs.Platform{OS: runtime.GOOS}, nil
  474. }
  475. type pluginLayerProvider struct {
  476. pm *Manager
  477. plugin *v2.Plugin
  478. }
  479. func (p *pluginLayerProvider) Get(id layer.ChainID) (distribution.PushLayer, error) {
  480. rootFS := rootFSFromPlugin(p.plugin.PluginObj.Config.Rootfs)
  481. var i int
  482. for i = 1; i <= len(rootFS.DiffIDs); i++ {
  483. if layer.CreateChainID(rootFS.DiffIDs[:i]) == id {
  484. break
  485. }
  486. }
  487. if i > len(rootFS.DiffIDs) {
  488. return nil, errors.New("layer not found")
  489. }
  490. return &pluginLayer{
  491. pm: p.pm,
  492. diffIDs: rootFS.DiffIDs[:i],
  493. blobs: p.plugin.Blobsums[:i],
  494. }, nil
  495. }
  496. type pluginLayer struct {
  497. pm *Manager
  498. diffIDs []layer.DiffID
  499. blobs []digest.Digest
  500. }
  501. func (l *pluginLayer) ChainID() layer.ChainID {
  502. return layer.CreateChainID(l.diffIDs)
  503. }
  504. func (l *pluginLayer) DiffID() layer.DiffID {
  505. return l.diffIDs[len(l.diffIDs)-1]
  506. }
  507. func (l *pluginLayer) Parent() distribution.PushLayer {
  508. if len(l.diffIDs) == 1 {
  509. return nil
  510. }
  511. return &pluginLayer{
  512. pm: l.pm,
  513. diffIDs: l.diffIDs[:len(l.diffIDs)-1],
  514. blobs: l.blobs[:len(l.diffIDs)-1],
  515. }
  516. }
  517. func (l *pluginLayer) Open() (io.ReadCloser, error) {
  518. return l.pm.blobStore.Get(l.blobs[len(l.diffIDs)-1])
  519. }
  520. func (l *pluginLayer) Size() (int64, error) {
  521. return l.pm.blobStore.Size(l.blobs[len(l.diffIDs)-1])
  522. }
  523. func (l *pluginLayer) MediaType() string {
  524. return schema2.MediaTypeLayer
  525. }
  526. func (l *pluginLayer) Release() {
  527. // Nothing needs to be release, no references held
  528. }
  529. // Remove deletes plugin's root directory.
  530. func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
  531. p, err := pm.config.Store.GetV2Plugin(name)
  532. pm.mu.RLock()
  533. c := pm.cMap[p]
  534. pm.mu.RUnlock()
  535. if err != nil {
  536. return err
  537. }
  538. if !config.ForceRemove {
  539. if p.GetRefCount() > 0 {
  540. return inUseError(p.Name())
  541. }
  542. if p.IsEnabled() {
  543. return enabledError(p.Name())
  544. }
  545. }
  546. if p.IsEnabled() {
  547. if err := pm.disable(p, c); err != nil {
  548. logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
  549. }
  550. }
  551. defer func() {
  552. go pm.GC()
  553. }()
  554. id := p.GetID()
  555. pluginDir := filepath.Join(pm.config.Root, id)
  556. if err := mount.RecursiveUnmount(pluginDir); err != nil {
  557. return errors.Wrap(err, "error unmounting plugin data")
  558. }
  559. if err := atomicRemoveAll(pluginDir); err != nil {
  560. return err
  561. }
  562. pm.config.Store.Remove(p)
  563. pm.config.LogPluginEvent(id, name, "remove")
  564. pm.publisher.Publish(EventRemove{Plugin: p.PluginObj})
  565. return nil
  566. }
  567. // Set sets plugin args
  568. func (pm *Manager) Set(name string, args []string) error {
  569. p, err := pm.config.Store.GetV2Plugin(name)
  570. if err != nil {
  571. return err
  572. }
  573. if err := p.Set(args); err != nil {
  574. return err
  575. }
  576. return pm.save(p)
  577. }
  578. // CreateFromContext creates a plugin from the given pluginDir which contains
  579. // both the rootfs and the config.json and a repoName with optional tag.
  580. func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.ReadCloser, options *types.PluginCreateOptions) (err error) {
  581. pm.muGC.RLock()
  582. defer pm.muGC.RUnlock()
  583. ref, err := reference.ParseNormalizedNamed(options.RepoName)
  584. if err != nil {
  585. return errors.Wrapf(err, "failed to parse reference %v", options.RepoName)
  586. }
  587. if _, ok := ref.(reference.Canonical); ok {
  588. return errors.Errorf("canonical references are not permitted")
  589. }
  590. name := reference.FamiliarString(reference.TagNameOnly(ref))
  591. if err := pm.config.Store.validateName(name); err != nil { // fast check, real check is in createPlugin()
  592. return err
  593. }
  594. tmpRootFSDir, err := ioutil.TempDir(pm.tmpDir(), ".rootfs")
  595. if err != nil {
  596. return errors.Wrap(err, "failed to create temp directory")
  597. }
  598. defer os.RemoveAll(tmpRootFSDir)
  599. var configJSON []byte
  600. rootFS := splitConfigRootFSFromTar(tarCtx, &configJSON)
  601. rootFSBlob, err := pm.blobStore.New()
  602. if err != nil {
  603. return err
  604. }
  605. defer rootFSBlob.Close()
  606. gzw := gzip.NewWriter(rootFSBlob)
  607. layerDigester := digest.Canonical.Digester()
  608. rootFSReader := io.TeeReader(rootFS, io.MultiWriter(gzw, layerDigester.Hash()))
  609. if err := chrootarchive.Untar(rootFSReader, tmpRootFSDir, nil); err != nil {
  610. return err
  611. }
  612. if err := rootFS.Close(); err != nil {
  613. return err
  614. }
  615. if configJSON == nil {
  616. return errors.New("config not found")
  617. }
  618. if err := gzw.Close(); err != nil {
  619. return errors.Wrap(err, "error closing gzip writer")
  620. }
  621. var config types.PluginConfig
  622. if err := json.Unmarshal(configJSON, &config); err != nil {
  623. return errors.Wrap(err, "failed to parse config")
  624. }
  625. if err := pm.validateConfig(config); err != nil {
  626. return err
  627. }
  628. pm.mu.Lock()
  629. defer pm.mu.Unlock()
  630. rootFSBlobsum, err := rootFSBlob.Commit()
  631. if err != nil {
  632. return err
  633. }
  634. defer func() {
  635. if err != nil {
  636. go pm.GC()
  637. }
  638. }()
  639. config.Rootfs = &types.PluginConfigRootfs{
  640. Type: "layers",
  641. DiffIds: []string{layerDigester.Digest().String()},
  642. }
  643. config.DockerVersion = dockerversion.Version
  644. configBlob, err := pm.blobStore.New()
  645. if err != nil {
  646. return err
  647. }
  648. defer configBlob.Close()
  649. if err := json.NewEncoder(configBlob).Encode(config); err != nil {
  650. return errors.Wrap(err, "error encoding json config")
  651. }
  652. configBlobsum, err := configBlob.Commit()
  653. if err != nil {
  654. return err
  655. }
  656. p, err := pm.createPlugin(name, configBlobsum, []digest.Digest{rootFSBlobsum}, tmpRootFSDir, nil)
  657. if err != nil {
  658. return err
  659. }
  660. p.PluginObj.PluginReference = name
  661. pm.publisher.Publish(EventCreate{Plugin: p.PluginObj})
  662. pm.config.LogPluginEvent(p.PluginObj.ID, name, "create")
  663. return nil
  664. }
  665. func (pm *Manager) validateConfig(config types.PluginConfig) error {
  666. return nil // TODO:
  667. }
  668. func splitConfigRootFSFromTar(in io.ReadCloser, config *[]byte) io.ReadCloser {
  669. pr, pw := io.Pipe()
  670. go func() {
  671. tarReader := tar.NewReader(in)
  672. tarWriter := tar.NewWriter(pw)
  673. defer in.Close()
  674. hasRootFS := false
  675. for {
  676. hdr, err := tarReader.Next()
  677. if err == io.EOF {
  678. if !hasRootFS {
  679. pw.CloseWithError(errors.Wrap(err, "no rootfs found"))
  680. return
  681. }
  682. // Signals end of archive.
  683. tarWriter.Close()
  684. pw.Close()
  685. return
  686. }
  687. if err != nil {
  688. pw.CloseWithError(errors.Wrap(err, "failed to read from tar"))
  689. return
  690. }
  691. content := io.Reader(tarReader)
  692. name := path.Clean(hdr.Name)
  693. if path.IsAbs(name) {
  694. name = name[1:]
  695. }
  696. if name == configFileName {
  697. dt, err := ioutil.ReadAll(content)
  698. if err != nil {
  699. pw.CloseWithError(errors.Wrapf(err, "failed to read %s", configFileName))
  700. return
  701. }
  702. *config = dt
  703. }
  704. if parts := strings.Split(name, "/"); len(parts) != 0 && parts[0] == rootFSFileName {
  705. hdr.Name = path.Clean(path.Join(parts[1:]...))
  706. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(strings.ToLower(hdr.Linkname), rootFSFileName+"/") {
  707. hdr.Linkname = hdr.Linkname[len(rootFSFileName)+1:]
  708. }
  709. if err := tarWriter.WriteHeader(hdr); err != nil {
  710. pw.CloseWithError(errors.Wrap(err, "error writing tar header"))
  711. return
  712. }
  713. if _, err := pools.Copy(tarWriter, content); err != nil {
  714. pw.CloseWithError(errors.Wrap(err, "error copying tar data"))
  715. return
  716. }
  717. hasRootFS = true
  718. } else {
  719. io.Copy(ioutil.Discard, content)
  720. }
  721. }
  722. }()
  723. return pr
  724. }
  725. func atomicRemoveAll(dir string) error {
  726. renamed := dir + "-removing"
  727. err := os.Rename(dir, renamed)
  728. switch {
  729. case os.IsNotExist(err), err == nil:
  730. // even if `dir` doesn't exist, we can still try and remove `renamed`
  731. case os.IsExist(err):
  732. // Some previous remove failed, check if the origin dir exists
  733. if e := system.EnsureRemoveAll(renamed); e != nil {
  734. return errors.Wrap(err, "rename target already exists and could not be removed")
  735. }
  736. if _, err := os.Stat(dir); os.IsNotExist(err) {
  737. // origin doesn't exist, nothing left to do
  738. return nil
  739. }
  740. // attempt to rename again
  741. if err := os.Rename(dir, renamed); err != nil {
  742. return errors.Wrap(err, "failed to rename dir for atomic removal")
  743. }
  744. default:
  745. return errors.Wrap(err, "failed to rename dir for atomic removal")
  746. }
  747. if err := system.EnsureRemoveAll(renamed); err != nil {
  748. os.Rename(renamed, dir)
  749. return err
  750. }
  751. return nil
  752. }