backend_linux.go 21 KB

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