zoukankan      html  css  js  c++  java
  • Importing fabric into your own scripts

    Importing fabric into your own scripts | California Dreams

    Importing fabric into your own scripts

    “Fabric is a simple pythonic remote deployment tool” states the Fabric website. But fabric can offer more (or could, if certain assumptions were fixed). Fabric offers simple function calls to run commands locally, over the network, upload and download files and so on. You can of course program all this yourself using various stdlib and 3rd party libraries, but the simplicity of the fabric API feels nice, like this example shows (using fabric 0.0.9 or earlier):

    import fabric
     
    fabric.local('echo hello')
     
    fabric.set(fab_hosts=['host-a'])
    fabric.put('myfile.txt', 'myfile.txt')
    fabric.run('uname -a')

    I had made a script that used fabric like this. It worked fine when I tested against a single host. But when I tried to connect to different hosts in my script, and doing different things on each host, I run into a problem. It turns out that fabric cached the connection to the first host, and later on all my operations were executed against the first host. Luckily I found a simple workaround for that. After each fabric.set() do this:

    fabric.CONNECTIONS = []

    It may be that fabric caches other fab_* parameters as well, but my use case worked so I haven’t looked further into this yet.

    Unfortunately fabric 0.1.0 breaks this import pattern. The developers are aware of the issue, though, so it may be that later versions of fabric could be used like this as well. But for the time being you should mark the dependency at 0.0.9 or 0.0.8.

    There are some additional warts when importing fabric into your script. While it is great that fabric notices when a command fails, it will actually exit rather than raise an exception. Probably not an ideal situation for most scripts, so it would be nice if this was configurable. Another feature I would really like added was timeouts for all of the commands. Running external commands that can be killed after a timeout is difficult in Python.

  • 相关阅读:
    Windows Phone 7 利用计时器DispatcherTimer创建时钟
    杭电1163 Eddy's digital Roots
    随感1
    杭电 1194 Beat the Spread!
    杭电 1017 A Mathematical Curiosity
    杭电1202 The calculation of GPA
    关于递归的思想
    杭电1197 Specialized FourDigit Numbers
    杭电 1062 Text Reverse
    杭电1196 Lowest Bit
  • 原文地址:https://www.cnblogs.com/lexus/p/2355127.html
Copyright © 2011-2022 走看看