Posquit0 - Fabric을 이용한 ssh streamlining

  • View
    1.205

  • Download
    2

Embed Size (px)

Text of Posquit0 - Fabric을 이용한 ssh streamlining

  • 1. Application Deployment , Fabric SSH Streamlining Fabric SSH Streamlining (Posquit0) | POSTECH CSE 10 | Jul. 2013 posquit0.bj@gmail.com Must be the change that you want to see in the world! PoApper

2. No Content 1 Intro 2 Deployment with Fabric 3 Tips PoApper Fabric SSH Streamlining 3. Fabric System Administration Application Deployment SSH (Python) , fab . => fabfile.py Fabric 1) 2) sudo 3) / Intro: Fabric from fabric.api import run def host_info(): run(uname a) Fabric SSH Streamlining posquit0:~# fab H localhost host_info [localhost] Executing task host_info [localhost] run: uname a [localhost] Login password for posquit0: (Ruby) Capistrano . (Ruby , .) 4. Fabric sudo . 1) sudo fabric . 2) pip fabric . Intro: Fabric sudo fabric . Fabric SSH Streamlining posquit0:~# mkvirtualenv --no-site-packages --distribute myenv New python executable in myenv/bin/python Installing distributedone. Installing pipdone. posquit0:~ (myenv) # posquit0:~ (myenv) # pip install fabric Downloading/unpacking fabric 5. Fabric (1) Hello, Fabric! 1) fabfile.py . 2) Fab . Intro: Fabric (1) from fabric.api import run def hello(): Hello Fabric! print Hello, Fabric! Fabric SSH Streamlining posquit0:~# fab hello Hello, Fabric! Done. 6. Fabric (2) 1) fabfile.py . 2) Fab . Intro: Fabric (2) # ... () def helloTo(name=world): Hello to {name} print Hello, %s % name Fabric SSH Streamlining posquit0:~# fab helloTo:Posquit0 Hello, Posquit0 posquit0:~# fab helloTo:name=LasVegas Hello, LasVegas 7. Fabric (3) 1) fabfile.py . 2) Fab . Intro: Fabric (3) # ... () def restartServices(name=None): Restart services as apache2, nginx, mysql, etc. if name is None: local(sudo service apache2 restart) local(sudo service nginx restart) local(sudo service mysql restart) else: local(sudo service %s restart % name) Fabric SSH Streamlining posquit0:~# fab restartServices posquit0:~# fab restartServices:mysql 8. Fabric (4) 1) fabfile.py . 2) Fab . Intro: Fabric (4) # ... () def host_info(): Check host information run(uname a) Fabric SSH Streamlining posquit0:~# fab H localhost host_info [localhost] Executing task host_info [localhost] run: uname a [localhost] Login password for posquit0: 9. Fabric (5) sudo 1) fabfile.py . 2) Fab . Intro: Fabric (5) # ... () def reboot(): Reboot remote server. sudo(reboot) Fabric SSH Streamlining posquit0:~# fab H ubuntu.poapper.com:2222 reboot Bye 10. Fabric (6) 1) fabfile.py . 2) Fab . Intro: Fabric (6) # ... () def copyVimConfigTo(): Copy Vim Config to remote server: .vim/, .vimrc run(rm rf ~/.vim;rm .vimrc) put(~/.vim, ~/) run(ln s ~/.vim/.vimrc ~/.vimrc) Fabric SSH Streamlining posquit0:~# fab H ubuntu.poapper.com:2222 copyVimConfigTo [Ubuntu.poapper.com] put: ~/.vim/ 11. Fabric (7) 1) fabfile.py . 2) Fab . Intro: Fabric (7) # ... () def copyVimConfigFrom(): Copy Vim Config from remote server: .vim/, .vimrc local(rm rf ~/.vim;rm .vimrc) get(~/.vim, ~/) local(ln s ~/.vim/.vimrc ~/.vimrc) Fabric SSH Streamlining posquit0:~# fab H ubuntu.poapper.com:2222 copyVimConfigFrom [Ubuntu.poapper.com] get: ~/.vim/ 12. Fabric , fabfile.py . . Example : Podori - (root) fabfile.py - (nginx, podori, diet ) fab . - (Podori ) . => fabfile.py Intro: Fabric Fabric SSH Streamlining Project/podori.poapper.com/ apache2/ log/ nginx/ podori/ main/ diet/ AUTHORS fabfile.py INSTALL pip-requirements README.md robot.txt 13. Core Functionality - Run() : . - Local() : . - Sudo() : . - Reboot() : (Reboot). - Put() : . - Get() : . - Require() : . => . Intro: Core Functionality Fabric SSH Streamlining def run( command, shell=True, pty=True, combine_stderr=None, quit=False, warn_only=False, stdout=None, stderr=None, timeout=None ) def local(command, capture=False, shell=None) def sudo( command, shell=True, pty=True, combine_stderr=None, quit=False, user=None, warn_only=False, stdout=None, stderr=None, group=None, timeout=None ) 14. Configuration (1) Fabric Environment (env) Fabric API env , Python Dictionary Wrapper Class. - Hosts & Roles - Code Repositories - Shell - User & Password - etc. Intro: Configuration (1) Fabric SSH Streamlining from fabric.api import env env.roledefs = { develop : [ubuntu.poapper.com, work.posquit0.com], product : [pou.poapper.com], } env.user = posquit0 # 15. Configuration (2) Global Settings (~/.fabricrc) ~/.fabricrc Fabric , Fabric . (Python ) Intro: Configuration (2) Fabric SSH Streamlining from fabric.api import env env.roledefs = { develop : [ubuntu.poapper.com, work.posquit0.com], product : [pou.poapper.com], } env.passwords = { posquit0@posquit0.com : skRtldhkd> roles . Tips:Roles (2) Fabric SSH Streamlining get_version() test Hosts . # ... () def default_roles(*role_list): def selectively_attach(func): if not env.roles and not env.hosts: return roles(*role_list)(func) else: if env.hosts: func = hosts(*env.hosts)(func) if env.roles: func = roles(*env.roles)(func) return func return selectively_attach @default_roles(develop, product) def get_version(): run(cat /etc/issue) posquit0:~/develop# fab get_version --roles test 28. Dealing with Failures With - Task ( ), Fabric . => Failure , Context Manager . Tips: Dealing with Failures Fabric SSH Streamlining from __future__ import with_statement from fabric.api import local, settings, run def symlinkTo(src, dst): Make a symbolic link with settings(warn_only=True): run(rm %s % dst) run(ln s %s %s % (src, dst)) Python 2.5 with __future__ import . 29. Invoke sudo - fabfile invoke . Tips: Invoke Fabric SSH Streamlining @roles(all) def invoke(command): Invoke arbitrary commands to remote server sudo(command) 30. Server Package Updates - fabfile . - Fab . Tips: Server Package Updates Fabric SSH Streamlining posquit0:~/develop# fab update def update(): Update Packages on server sudo(aptitude update; aptitude safe-upgrade) 31. ? Fabric Docs: http://docs.fabfile.org PoApper Fabric SSH Streamlining 32. Thank You for Listening! PoApper Fabric SSH Streamlining