Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fabric Python Developers Festa 2013.03 #pyfes

Fabric Python Developers Festa 2013.03 #pyfes

はい

E44e80bc1b6c30d8d40c98443f950c29?s=128

drillbits

March 16, 2013
Tweet

Transcript

  1. Fabric Python Developers Festa 2013.03

  2. ͓·͑୭Α

  3. drillbits

  4. None
  5. BePROUD

  6. BePROUD Web Alchemist

  7. BePROUD Web Alchemist #sht #precure #nitiasa

  8. None
  9. Fabric?

  10. Fabric is a Python (2.5 or higher) library and command-line

    tool for streamlining the use of SSH for application deployment or systems administration tasks.
  11. ͸͍

  12. Fabric is a Python (2.5 or higher) library and command-line

    tool for streamlining the use of SSH for application deployment or systems administration tasks.
  13. Fabric is a Python (2.5 or higher) library and command-line

    tool for streamlining the use of SSH for application deployment or systems administration tasks.
  14. Application Deployment?

  15. Πϯετʔϧ ઃఆ ىಈ

  16. By SSH

  17. ϗϫοτ

  18. ϗϫοτ Python

  19. ϗϫοτ Python MySQL

  20. ϗϫοτ Python MySQL Nginx

  21. ϗϫοτ Python MySQL Nginx gunicorn

  22. ϗϫοτ Python MySQL Nginx gunicorn Jenkins

  23. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial

  24. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django

  25. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios

  26. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios

  27. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

  28. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ
  29. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis
  30. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js
  31. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop
  32. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop daemontoo
  33. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop daemontoo PHP
  34. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop daemontoo PHP mongoDB
  35. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop daemontoo PHP mongoDB POSTFIX
  36. ϗϫοτ Python MySQL Nginx gunicorn Jenkins mercurial Django Nagios memcached

    RabbitMQ redis node.js Hadoop daemontoo PHP mongoDB POSTFIX δϟό
  37. ͨ͘͞Μ

  38. ͔͠΋

  39. To Many Servers

  40. Complex Combination

  41. ͩΔ͍

  42. Only Once?

  43. NO

  44. ։ൃ؀ڥ

  45. ։ൃ؀ڥ εςʔδϯά

  46. ։ൃ؀ڥ εςʔδϯά ຊ൪؀ڥ

  47. Is That All?

  48. NO

  49. ࣍ϑΣʔζͷͨΊͷ Yet Another εςʔδϯά

  50. ʊਓਓਓਓਓਓਓਓʊ ʼɹεςʔδϯά2ɹʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  51. ͓٬͞Μ͕ಈ࡞֬ೝ͢Δ Yet Another εςʔδϯά2

  52. ʊਓਓਓਓਓਓਓਓʊ ʼɹεςʔδϯά3ɹʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  53. ޫ଎ͷҟ໊Λ࣋ͪ͜Ε·Ͱͷ εςʔδϯά 2 Ͱ͸ॲཧ͖͠ Εͳ͔ͬͨେྔσʔλ΋ࣗࡏ ʹૢΔߴՁͳΔڀۃͷ৽؀ڥ

  54. ʊਓਓਓਓਓਓਓਓʊ ʼ εςʔδϯά2_2 ʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  55. αʔόʔ෺ނׂΕͨ

  56. ʊਓਓਓਓਓਓਓਓʊ ʼ ɹ ɹ࠶ߏஙɹ ɹ ʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  57. Կճ΋΍Δ͜ͱʹͳΔ ※ϑΟΫγϣϯͰ͢

  58. ͩΔ͍

  59. Update

  60. ϝϯςφϯεϞʔυ

  61. ϝϯςφϯεϞʔυ ϒϥϯν੾Γସ͑

  62. ϝϯςφϯεϞʔυ ϒϥϯν੾Γସ͑ ϚΠάϨʔγϣϯͱ͔

  63. ϝϯςφϯεϞʔυ ϒϥϯν੾Γସ͑ ϚΠάϨʔγϣϯͱ͔ ͍Ζ͍Ζ࠶ىಈ

  64. ։ൃ؀ڥͰ͸ 1೔Կճ΋΍Δ

  65. darui

  66. Operation Mistakes

  67. όον࠶ىಈ๨Ε ϚΠάϨʔγϣϯ͠๨Ε ϒϥϯν໭͠๨Ε

  68. Yes! खॱॻ

  69. None
  70. None
  71. d

  72. ͩΔ͍ͷ͸͍΍Ͱ͢Ͷ

  73. Fabric is a Python (2.5 or higher) library and command-line

    tool for streamlining the use of SSH for application deployment or systems administration tasks.
  74. Fabric is a Python (2.5 or higher) library and command-line

    tool for streamlining the use of SSH for application deployment or systems administration tasks.
  75. streamlining

  76. ޮ཰Խ

  77. DDD

  78. Darui Driven Deployment

  79. ࣗಈԽ͠·͠ΐ͏

  80. ैདྷͷࣗಈԽ

  81. Shell Script

  82. None
  83. None
  84. None
  85. ແɹཧ

  86. None
  87. ख୳ΓͰൺֱ͠·͢

  88. Shell Script Soldier, Stay aSleep ( ˘ω˘) ɹɹ ⊂˶ʗʋ-ɺʊ_ ɹʗ⊂_/ʊʊʊʊ

    ʗ
  89. Python Remote control Utils

  90. Python Remote control Utils

  91. Getting Started

  92. $ pip install fabric

  93. $ fab Fatal error: Couldn't find any fabfiles! Remember that

    -f can be used to specify fabfile path, and use -h for help. Aborting.
  94. # fabfile.py from fabric.api import local # task def hello():

    local('echo hello') # shell command def uname(): local('uname')
  95. $ fab --list Available commands: hello uname

  96. $ fab hello [localhost] local: echo hello hello Done.

  97. $ fab uname [localhost] local: uname Darwin Done.

  98. $ fab hello uname [localhost] local: echo hello hello [localhost]

    local: uname Darwin Done.
  99. $ fab start_maintenance update restart

  100. # fabfile.py from fabric.api import run def uname(): run('uname')

  101. $ fab uname -u me -H 10.0.0.1 -i id_rsa [10.0.0.1]

    Executing task 'uname' [10.0.0.1] run: uname [10.0.0.1] out: Linux [10.0.0.1] out: Done. Disconnecting from 10.0.0.1... done.
  102. # fabfile.py from fabric.api import sudo def uname(): sudo('uname')

  103. # fabfile.py from fabric.api import env, run env.hosts = ['10.0.0.1']

    env.user = 'me' env.key_filename = 'id_rsa' def uname(): run('uname')
  104. # fabfile.py from fabric.api import env, run def dev(): env.hosts

    = ['10.0.0.1'] env.user = 'me' env.key_filename = 'id_rsa' def uname(): run('uname')
  105. $ fab dev uname

  106. $ fab production uname

  107. Package

  108. $ tree fabfile fabfile ├── __init__.py ├── django.py └── host.py

  109. # fabfile/__init__.py from . import django from . import host

  110. # fabfile/host.py from fabric.api import env, task @task def dev():

    env.hosts = ['10.0.0.1'] env.user = 'me' env.key_filename = 'id_rsa'
  111. # fabfile/django.py from fabric.api import task @task def restart(): pass

  112. $ fab --list Available commands: django.restart host.dev

  113. Roles

  114. # fabfile/host.py from fabric.api import env, task @task def dev():

    env.hosts = ['10.0.0.1', '10.0.0.2'] env.roledefs = { 'app': ['10.0.0.1'], 'db': ['10.0.0.2'], }
  115. # fabfile/django.py from fabric.api import roles, task @task @roles('app') def

    restart(): pass
  116. Shell Script?

  117. None
  118. ͨͿΜେมͰ͢

  119. Python Remote control Utils

  120. None
  121. $ ssh me@10.0.0.1 -i id_rsa uname run('uname')

  122. Python Remote control Utils

  123. put upload_template append, uncomment prefix

  124. put

  125. from fabric.api import put put( local_path='resources/sushi.txt', remote_path='/var/www/sushi.txt', )

  126. upload_template

  127. from fabric.contrib.files \ import upload_template upload_template( filename='templates/my.cnf', destination='/etc/my.cnf', context={ 'buffer_pool_size':

    env.MYSQL_BUFFER_POOL_SIZE, }, )
  128. # templates/my.cnf : # You can set .._buffer_pool_size up to

    50 - 80 %% innodb_buffer_pool_size = %(buffer_pool_size)s :
  129. # templates/my.cnf : # You can set .._buffer_pool_size up to

    50 - 80 %% innodb_buffer_pool_size = %(buffer_pool_size)s :
  130. from fabric.contrib.files \ import upload_template upload_template( filename='templates/my.cnf', remote_path='/etc/my.cnf', context=context, use_jinja=True,

    # jinja2! template_dir='templates', )
  131. # templates/my.cnf : # You can set .._buffer_pool_size up to

    50 - 80 % innodb_buffer_pool_size = {{ buffer_pool_size }} :
  132. append

  133. from fabric.contrib.files import append append( filename='/etc/hosts', text='10.0.0.101 dev-server', )

  134. uncomment

  135. from fabric.contrib.files \ import uncomment uncomment( filename='/etc/vsftpd.conf', regex='write_enable=YES', char='#', )

  136. prefix

  137. from fabric.api import run from fabric.context_managers \ import cd run('tar

    xzvf Python-2.7.3.tgz') with cd('Python-2.7.3'): run('./configure') run('make') sudo('make install')
  138. from fabric.api import run run('tar xzvf Python-2.7.3.tgz') run('cd Python-2.7.3'): run('./configure')

    run('make') sudo('make install')
  139. $ ssh me@10.0.0.1 -i id_rsa tar xzvf Python-2.7.3.tgz $ ssh

    me@10.0.0.1 -i id_rsa cd Python-2.7.3 $ ssh me@10.0.0.1 -i id_rsa ./configure $ ssh me@10.0.0.1 -i id_rsa make $ ssh me@10.0.0.1 -i id_rsa make install
  140. from fabric.api import run from fabric.context_managers \ import cd run('tar

    xzvf Python-2.7.3.tgz') with cd('Python-2.7.3'): run('./configure') run('make') sudo('make install')
  141. $ ssh (...) cd Python-2.7.3 $ ssh (...) cd Python-2.7.3

    && ./configure $ ssh (...) cd Python-2.7.3 && make $ ssh (...) cd Python-2.7.3 && make install
  142. from contextlib import contextmanager from fabric.context_managers \ import prefix @contextmanager

    def workon(venv): with prefix('workon ' + venv): yield
  143. with workon('myproj'): run('pip install xaxtsuxo')

  144. ·ͱΊ

  145. Deploy ͩΔ͍

  146. Deploy ͩΔ͍ Shell Script ࢖͏ඞཁͳ͠

  147. Deploy ͩΔ͍ Shell Script ࢖͏ඞཁͳ͠ ͦΕ Chef Ͱ | @tk0miya

  148. None