backend_linux.go 22 KB

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