Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Fabricでお手軽サーバ管理

1,958 views

Published on

#ssmjp 2014年10月 LT資料

Published in: Software
  • Be the first to comment

Fabricでお手軽サーバ管理

  1. 1. 'BCSJDsV7´–Ï v TTNKQñK
  2. 2. ߅Ãu _|c]ŸPm !/JSBUBNB ¼–¶â걖ß5 1FSMXÝžžz{f ?ß{£òÔètWiomzfÆX*[r| Xop–Öì¯è߄PrvP÷d
  3. 3. 'BCSJDt{ IUUQXXXGBCGJMFPSH ¶òÖêvÆÖ죀¶¸ÅßvÖê t–NTf¥—zw³T—_tX vP 1ZUIPOksÔ £êŽ1ZUIPOz¸®éÖÇ Pbtv˜|1ZUIPOs„]—_t{vŸsŽsY— 44)-,s)z´–ÏwٖŸsfÆe— ٖŸl–zݶòwP™P™£ò¸Ç–êe— «P{vP .Z42-'BCSJDt{vPse
  4. 4. ”Ÿq] $IFG1VQQFUsXop–ve—zt|¶¦ê¸®é ÖÇs
  5. 5. zwop]—zz¯Nk– 1ZUIPOz¸®éÖÇvzs|Öì¯èÝz€ÝW•f —tPeP X¶¦ê¸®éÖÇvv•¤ícePWŽ
  6. 6. Ô £ê fabfile.pytPRؼsÔ £ê 1ZUIPOz¸®éÖÇ Ô £ê žJe— ¾¸® 1ZUIPOzD w{vž„[ DÔ £êw Çcrá·ä–êBsY—zs|Lé XN— _RY[vorŽŠ
  7. 7. from fabric.api import * @task def hostname(): run('hostname') @task def upload_module(): put('~/works/webapp/lib', '/home/user/webapp/') @task def download_logs(): get('/home/user/webapp/log/access.log') @task def reboot_httpd(): sudo('/sbin/service httpd restart')
  8. 8. $PSF1* éá–Çz²ÝòȞöí|éá–ÇtzÔ £êD ²òŬ¸ÇÝ̖·âžžok§p—sz/Ò Æ²ë–¾žžok¾¸®z[/Ò ì¯àº–·z¼Ü|ªè–¼Ü iz)å–Å¢éÅ¢
  9. 9. $POUSJC1* éá–ÇzÔ £êìf Ŭ¸ÇzðH€Ÿú|Ô  £êz#WÀ¦Â® Öì·¦®ÇŠ—`tz¡ÂÖì–È %KBOHPtz×%
  10. 10. 'BCSJD
  11. 11. $VJTJOF IUUQTHJUIVCDPNTFCBTUJFODVJTJOF ˜[XXcPtYwžRðH1* šòvX•È¬äàòÇXNŠ–^˜a˜rvPzs| žRw{cuisine.pyž­Œ«PXN—
  12. 12. from fabric.api import * from fabric.contrib.files import * import cuisine from pit import Pit cuisine.select_package('yum') @task def create_user(): with settings(user='root'): cuisine.user_ensure('ssmjp') append('/etc/sudoers', 'ssmjp ALL=(ALL) ALL') cuisine.ssh_authorize('ssmjp', cuisine.file_local_read('~/.ssh/ssmjp.pub')) conf = Pit.get('ssmjp-user', { 'require': { 'password': 'Your password' } }) cuisine.user_passwd('ssmjp', conf['password']) @task def install_packages(): with settings(user='root'): cuisine.package_ensure('nginx')
  13. 13. 5JQT
  14. 14. 44)zDPOGJHÔ £êžˆ¹e— óà{44)zDPOGJHÔ £ê{ˆ¹cvPX|’Az žcrV[tˆ¹cr[˜— *EFOUJUZGJMF1SPYZ$PNNBOEŽfr[˜—zs6q env.use_ssh_config = True
  15. 15. éá–ÇwÅòÜèéÆ¢ë®Çé éá–Çsmktemp -džöícr|try›finallys? w  ¥²e— run()zöíHQwfokÆ¢ë®ÇéØXû— def diff_hosts(): tempdir = run('mktemp -d') try: tempfile = '%s/hosts' % tempdir put('/etc/hosts', tempfile) run('diff -u /etc/hosts %s' % tempfile, warn_only=True) finally: run('rm -r %s' % tempdir)
  16. 16. á·ä–êB fabfiletPRÆ¢ë®Çéw__init__.pytPRÔ £êž for|iz¯s¾¸®ž„PkÔ £êžJNQPSUe —tá·ä–êBsY— ¾¸®zcc{fab module.task‹kPvscg —
  17. 17. Ô £êÖ . └── fabfile/ ├── __init__.py ├── foo.py └── math.py __init__.py import foo import math
  18. 18. ȬäàòŖ¶æòÐÊ ¾¸®wȬäàòŖ¶æòÐʞÔ]rV[ t|fab -lžöícktYw¾¸®zÚtcr¼Üa˜ — @task def calc_add(x, y): ''' add two integer ''' print '%d + %d = %d' % (int(x), int(y), int(x) + int(y))
  19. 19. Ú¸ÇØzv ²ÝòÈè£ò©Ö¶æòsUckÚ¸ÇØ{ FOWIPTUTs¡®º¸sY—zs|GBCGJMFQZw  Œ_tŽIÿ env.hosts = ['server1', 'server2']
  20. 20. FOWSPMFEFGžcrV[t|ڸǞ3PMFsŠt rT— env.roledefs = { 'web': ['web01','web02'], 'db': ['db01'] }
  21. 21. 1ZUIPO¸®éÖÇvzs|“óW•Ú¸Ç´pž­‹ Œ_tŽIÿ 1Z:.-X
  22. 22. z import yaml def load_servers(filename): config_yaml = open(filename).read() config = yaml.load(config_yaml) roledefs = config['roles'] all_hosts = [] for role in roledefs: all_hosts.extend(roledefs[role]) roledefs['all'] = all_hosts return roledefs env.roledefs.update(load_servers('./servers.yaml'))
  23. 23. servers.yaml roles: even: - conoha01.poi.jp - conoha03.poi.jp odd: - conoha02.poi.jp
  24. 24. Ú¸Çé¸Çz„YúTtWŽIÿ host[00-03]âhost00 host01 host02 host03 import re def expand_hosts(hosts): new_hosts = [] for host in hosts: m = re.search('[(d+)-(d+)]', host) if m: pre = host[:m.start()] post = host[m.end():] prec = len(m.group(1)) for n in range(int(m.group(1)), int(m.group(2)) + 1): new_hosts.append(pre + ('%%0%dd' % prec % n) + post) else: new_hosts.append(host) return new_hosts env.hosts = expand_hosts(env.hosts)

×