pharによるワンバイナリアプリケーションの可能性を探ってみた

 pharによるワンバイナリアプリケーションの可能性を探ってみた

PHPカンファレンス北海道2019 LT発表資料

SwooleのフレームワークSwoftにはアプリケーションをpharファイル1つにまとめるコマンドがあります
つまり、ワンバイナリーでアプリケーションをデプロイ、実行する仕組みです
このpharの作成とデプロイの可能性を探ってみます

※(9/24:最後のスライドが発表時と異なっていたので差し替えました)

Ec2fcdc4ea7905b289967a2c4c43e154?s=128

CyberAgent SGE Engineer

September 21, 2019
Tweet

Transcript

  1. QIBSʹΑΔϫϯόΠφϦΞϓϦέʔ γϣϯͷՄೳੑΛ୳ͬͯΈͨ !HPPEPP  QIQDPOEP

  2. • גࣜձࣾαΠόʔΤʔδΣϯτ • 4(&౷ׅຊ෦ٕज़౷ׅࣨࣨ௕ • ࢠձࣾࣾ$50 • גࣜձࣾ$SBGU&HH • גࣜձࣾδʔΫϨετ

    • גࣜձࣾαϜβοϓ • നҪɹӳ • ΤϯδχΞ • #MPH IUUQBNFCMPKQHPPEPP ͓લɺ୭Α • 5XJUUFS !HPPEPP • %2 ͙ͪ͢Ή '# 
 ϓΫϦϙ ཱྀܳਓ
  3. "HFOEB wϫϯόΠφϦ w1IBS wQIBSͷར༻Λศརʹ͢ΔϥΠϒϥϦ w)551αʔόʔΛϫϯόΠφϦͰ w·ͱΊ

  4. ϫϯόΠφϦ

  5. ϫϯόΠφϦɾɾɾ ͍͍ڹ͖Ͱ͢Ͷ

  6. (PͰϏϧυͨ͠ όΠφϦ͸ ϫϯόΠφϦͰ ಈ͖·͢Ͷ

  7. ഑෍෺ͷϙʔλϏϦ ςΟ͕ॏཁͰ͢Ͷ

  8. σϓϩΠ͕̍ϑΝΠϧ ͍͍ͬͯͰ͢Ͷ

  9. (P͍ͬͯ͢͝Ͱ͢Ͷ

  10. 1)1Ͱ Ͱ͖ͳ͍ͷʁ

  11. (PʹͰ͖ͯ 1)1ʹͰ͖ͳ͍ Θ͚͕ͳ͍

  12. 1IBS

  13. ಥવͰ͕࣭͢໰Ͱ͢

  14. 1IBSͳΜͯಡΈ·͔͢ʁ  ͽʔ΁͍ͪ͑ʔ͋ʔΔ  ͺʔ  ;͊ʔ

  15. ࢲ͸  ͷʮ;͊ʔʯͰ͢ ;͊ʔ

  16. 1)1FS΋ ϫϯόΠφϦͰ σϓϩΠ͍ͨ͠

  17. ͦ͜Ͱ1IBS

  18. 1IBSͬͯԿʁ 1IBSΞʔΧΠϒ͸ɺෳ਺ͷϑΝΠϧΛͻͱͭ ʹ·ͱΊΔͨΊͷศརͳ࢓૊ΈͰ͢ 1IBSΞʔΧΠϒΛ࢖༻͢Ε͹ɺ1)1ͷΞϓ ϦέʔγϣϯΛͻͱͭͷϑΝΠϧͱͯ͠഑෍Ͱ ͖ΔΑ͏ʹͳΓ·͢ɻ·ͨɺͦΕΛσΟεΫ্ ʹల։͠ͳͯ͘΋ͦͷ··࣮ߦͰ͖ΔͷͰ͢ Ҿ༻ɿ1)1ϚχϡΞϧ IUUQTXXXQIQOFUNBOVBMKBJOUSPQIBSQIQ

  19. 1IBSΞʔΧΠϒΛ࢖༻͢Ε͹ɺ 1)1ͷΞϓϦέʔγϣϯΛͻͱͭ ͷϑΝΠϧͱͯ͠഑෍Ͱ͖ΔΑ͏ʹ ͳΓ·͢ ·ͨɺͦΕΛσΟεΫ্ʹల։͠ͳ ͯ͘΋ͦͷ··࣮ߦͰ͖ΔͷͰ͢

  20. ͦͷ··࣮ߦͰ͖Δ

  21. Ͱ͸࣮ࡍʹ࡞੒ํ๏Λ Έ͍͖ͯ·͠ΐ͏

  22. $phar = new Phar('hoge.phar'); $phar->addFromString('hoge.php', file_get_contents('./hoge.php')); $phar->setStub("<?php define('IN_PHAR', true);\n Phar::mapPhar();\n

    require 'phar://hoge.phar/hoge.php'; \n __HALT_COMPILER();”); ࠷௿ݶͷ࣮ߦՄೳͳ 1IBSϑΝΠϧͷ࡞੒ྫ
  23. $phar = new Phar('hoge.phar'); $phar->addFromString('hoge.php', file_get_contents('./hoge.php')); $phar->setStub("<?php define('IN_PHAR', true);\n Phar::mapPhar();\n

    require 'phar://hoge.phar/hoge.php'; \n __HALT_COMPILER();”); IPHFQIQ͕࣮ߦՄೳͰ͋Ε͹ɺ ࡞੒ͨ͠IPHFQIBS΋ ࣮ߦՄೳͳϑΝΠϧʹͳΔ
  24. $phar = new Phar('hoge.phar'); $phar->addFromString('hoge.php', file_get_contents('./hoge.php')); $phar->setStub("<?php define('IN_PHAR', true);\n Phar::mapPhar();\n

    require 'phar://hoge.phar/hoge.php'; \n __HALT_COMPILER();”); BEE'SPN4USJOHͰඞཁͳϑΝΠ ϧΛQIBSʹ௥Ճ͍ͯ͘͠ͱ׬੒
  25. ໘౗͍͘͞ʁ

  26. QIBSͷར༻Λศརʹ͢Δ ϥΠϒϥϦ

  27. ͖ͬ͞ͷखॱΛ ΑΖ͘͠΍ͬͯ͘ΕΔ ϥΠϒϥϦ

  28. QIBSDPNQPTFS IUUQTHJUIVCDPNDMVFQIBSDPNQPTFS

  29. DPNQPTFSKTPO Λ࢖ͬͯ ΞϓϦέʔγϣϯ ࡞ͬͯ·͢ΑͶʁ

  30. { "name": "goodoo/hoge-phar", "description": "hoge phar", "authors": [ { "name":

    "goodoo } ], "bin": [ "./hoge.php" ], "require": { "guzzlehttp/guzzle": "~6.0" } } DPNQPTFSKTPOʹʮCJOʯͷ߲໨ʹ ΤϯτϦʔϙΠϯτͱͳΔ QIQϑΝΠϧΛࢦఆ͢Δ͚ͩ
  31. $ php -d phar.readonly=false phar-composer.phar build . ͦͯ͠Ϗϧυʂ

  32. ͓खܰʂ

  33. ͳΜ͚ͩͲ ڧେͳϓϩδΣΫτ͸ Ϗϧυࣦഊ͢Δɾɾ ˞໌֬ͳ৚݅͸͓͔͚ͬͯͳ͍ͷͰ؀ڥʹΑΔ͔΋͠Ε·ͤΜ

  34. (PΈ͍ͨʹ ϫϯόΠφϦʔͰ )551αʔόʹ ͳΓ͍ͨ

  35. )551αʔόʔΛ ϫϯόΠφϦͰ

  36. )551αʔόʔΛ 1)1୯ମͰʂ

  37. ͦ͜Ͱ4XPPMFΛͭ ͔ͬͨϑϨʔϜϫʔΫ 4XPGUͷग़൪

  38. 1)1ͰΠϕϯτۦಈͷඇಉظ ˍίϧʔνϯϕʔεͷฒߦॲཧ

  39. 4XPGU https://www.swoft.org/

  40. •Base on Swoole extension •Built-in HTTP, TCP, WebSocket Coroutine Server

    •Powerful AOP (Aspect Oriented Programming) •Flexible and comprehensive annotations framework •Global dependency injection container •PSR-7 based HTTP message implementation •PSR-14 based event manager •PSR-15 based middleware •PSR-16 based cache design •Scalable high performance RPC •Holistic service governance, fallback, load balance, service registration and discovery •Database ORM •Universal connection pools •Mysql, Redis, RPC, HTTP Coroutine Clients •Coroutine driver client and blocking driver client seamlessly switch automatically •Coroutine and asynchronous task delivery •Custom user processes •RESTful supported •Internationalization (i18n) supported •High performance router •Fast and flexible parameter validator •Alias mechanism •Powerful log component •Cross-platform application auto- reload mechanism https://github.com/swoft-cloud/swoft ͷREADMEͷӳ༁ Ҿ༻ɿ
  41. 4XPGUʹ͸QBDLίϚ ϯυ͕༻ҙ͞Ε͍ͯΔ ˞WܥͷΈ

  42. $ php bin/swoft app:pack --help Description: pack project to a

    phar package Usage: bin/swoft app:pack [--dir DIR] [--output FILE] Options: --dir STRING Setting the project directory for packing. default is current work-dir.(/Users/a11924/Git/isucon8-swoft) --fast BOOL Fast build. only add modified files by git status -s --refresh BOOL Whether build vendor folder files on phar file exists(False) -o,--output STRING Setting the output file name(app.phar) -c, --config STRING Use the defined config for build phar. Example: bin/swoft app:pack Pack current dir to a phar file. bin/swoft app:pack --dir vendor/swoft/devtool Pack the specified dir to a phar file. BQQQBDL
  43. ࢼ͠ʹલճൃදͰͭͬ͘ ͨϓϩδΣΫτΛQBDL

  44. $ php app.phar start Server Information ******************************************************************** * HTTP |

    host: 0.0.0.0, port: 8888, type: 1, worker: 1, mode: 2 * TCP | host: 0.0.0.0, port: 8099, type: 1, worker: 1 (Disabled) ******************************************************************** Server has been started. (master PID: 7533, manager PID: 7534) You can use CTRL + C to stop run. ىಈʂ
  45. ಈ͍ͯΔʂ

  46. 1)1Ͱ΋ ϫϯόΠφϦʔͰ )551αʔόʹ ͳΕͨʂ

  47. ͍͍͜ͱ͹͔Γ Ͱ͸ͳ͍

  48. Ϗϧυ͕࣌ؒ௕͍ɾɾ

  49. $ php bin/swoft app:pack --help Description: pack project to a

    phar package Usage: bin/swoft app:pack [--dir DIR] [--output FILE] Options: --dir STRING Setting the project directory for packing. default is current work-dir.(/Users/a11924/Git/isucon8-swoft) --fast BOOL Fast build. only add modified files by git status -s --refresh BOOL Whether build vendor folder files on phar file exists(False) -o,--output STRING Setting the output file name(app.phar) -c, --config STRING Use the defined config for build phar. Example: bin/swoft app:pack Pack current dir to a phar file. bin/swoft app:pack --dir vendor/swoft/devtool Pack the specified dir to a phar file. ŠGBTUΦϓγϣϯͷ ࠩ෼Ϗϧυͷར༻͕Αͦ͞͏
  50. ·ͱΊ

  51. 1IBS͸1)1ͷΞϓϦ έʔγϣϯΛ࣮ߦՄೳͳ ঢ়ଶͰͻͱͭͷϑΝΠϧ ʹ·ͱΊΔ͜ͱ͕Ͱ͖Δ

  52. 4XPGUͷϑϨʔϜϫʔΫͷ QBDLίϚϯυͰ ϫϯόΠφϦͰ࣮ߦՄೳͳ )551αʔό͕ 1)1Ͱ΋Ͱ͖Δ

  53. (PͰͰ͖Δ͜ͱ͸ 1)1Ͱ΋Ͱ͖Δ ͋͘·Ͱݸਓͷײ૝Ͱ͢

  54. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠