backend_linux.go 21 KB

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