backend_linux.go 21 KB

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