๋ฌธ์์๋ ๊ทธ ์๋ฅผ ํฌํจํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ด ์ฝ๋๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
from fabric import ThreadingGroup as Group
from fabric.exceptions import GroupException
hosts = ['web1', 'web2', 'web3']
g = Group(*hosts)
try:
results = g.run('date', hide='both')
except GroupException as e:
print e.args # I read from code that arguments are passed to exception but I'm unable to use that. `args` argument in my case is an empty tuple.
web1๊ณผ web3๋ ์ฑ๊ณตํ์ง๋ง web2๋ ์ฐ๊ฒฐ์ ์คํจํ๊ฑฐ๋ ๋ช ๋ น์ด 0์ด ์๋ ์ข ๋ฃ ์ฝ๋์ ํจ๊ป ๋ฐํ๋์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
๋์์ ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
์๋ ํ์ธ์ @shadyabhi ,
ํน์ ์์ธ๋ฅผ ํฌ์ฐฉํ๋ ค๋ ๊ฒ์ธ์ง ์๋๋ฉด ๋ณ๋์ ์ค๋ ๋์์ ๋ฐ์ํ๋ ๋ชจ๋ ์์ธ๋ฅผ ํฌ์ฐฉํ๋ ค๋ ๊ฒ์ธ์ง ํ์คํ์ง ์์ต๋๋ค. ThreadingGroup ๊ฐ์ฒด๋ ์ด๋ฏธ ํ๋ฅผ ์ฌ์ฉํ์ฌ ์์ธ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ณ ์คํ ์ค์ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ๋ค์ ์ฝ๋ ์ค๋ํซ์ ์ฌ์ฉํ์ฌ ์คํ ์ถ์ ์ ๋ณผ ์ ์์์ต๋๋ค.
def run_data(c):
c.run('date', hide='both')
<strong i="8">@task</strong>
def thread_exception_example(c):
hosts = ['localhost', 'fakehost', 'localhost']
# python list comprehension
{c: run_data(c) for c in Group(*hosts)}
๋ช
๋ น์ค์์ ๋ค์์ ์คํํ์ต๋๋ค. fab thread-exception-example
์๋ ํ์ธ์ @bossjones ์ ๋๋ค .
๋ต์ฅํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋๋ fab ๋ช ๋ น์ ์ฌ์ฉํ์ง ์๊ณ ์คํ๋ ค ํจ๋ธ๋ฆญ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ฌ์ฉํ์ฌ ์ด ๋ชจ๋ ๊ฒ์ ์คํํฉ๋๋ค. ๋ฐ๋ผ์ ๋ต๋ณ์ด ์์ง ๋ณด๋ฅ ์ค์ด๋ฉฐ ๊ฐ์ฌํ ์๊ฐํฉ๋๋ค.
์ฆ, fab
๋ ๋ด ์ฝ๋์์ ์ง์ ์ฒ๋ฆฌํ๋ ค๋ ๊ฒ์ ์ฒ๋ฆฌํฉ๋๋ค. ์์
์ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ ๋ ๋ง์ ์ ์ฐ์ฑ์ ๊ฐ๊ณ ์ถ๊ธฐ ๋๋ฌธ์ fab
๋ช
๋ น์ ์ง์ ์ฌ์ฉํ๊ณ ์ถ์ง ์์ ์ด์ ๊ฐ ์์ต๋๋ค.
๊ฐ์ฌ ํด์
์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ,
์ฃ์กํฉ๋๋ค. ์ธ๋ถ ์ฌํญ์ ๊ฐ๊ณผํ์ต๋๋ค. ํจ๋ธ๋ฆญ ์ฝ๋๋ฅผ ํ์ธํ๊ณ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ป์์ต๋๋ค.
GroupException์ ์ธ์๋ ํค๊ฐ fabric.connection.Connection
๊ฐ์ฒด์ด๊ณ ๊ฐ์ด fabric.runners.Result
๊ฐ์ฒด์ธ ์ฌ์ ์
๋๋ค.
except GroupException as e:
for c, r in e.result.items():
print "Connection: {}, Result: {}".format(c, r)
์๋ ํ์ธ์,
shadyabhi์ ์๊ฒฌ ์ธ์๋ GroupException์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
๋๋ ์ด๊ฒ์ด ๋์์ด๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค !!
import logging, socket, paramiko.ssh_exception
from fabric import Connection, Config, ThreadingGroup, exceptions, runners
...
g = ThreadingGroup.from_connections(c)
try:
result = g.run('hostname' ) # (just a basic command)
except exceptions.GroupException as e:
i = 0
for c, r in e.result.items():
print("Host[{}] = [{}]".format(i,c.host) )
if isinstance(r,runners.Result) :
print("ok, fine")
elif isinstance(r,socket.gaierror) :
print("Network error")
elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
print("Auth failed")
else:
print("something else")
i+=1
( ActivCloud ์ง์์์ ์ป์ ๊ฒฝ์ฐ)
์๋ฅผ ๋ค์ด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค @akiuni ! ์ ๋ Python/Fabric์ ์ฒ์ ์ ํ๊ณ ThreadedGroup์ ์ฌ์ฉํ์ฌ ์์ฒญํ๊ณ ์์ธ๊ฐ ์์ ๋ ๊ฒฐ๊ณผ๋ฅผ ์์งํ๋ ๋ฐฉ๋ฒ์ ์ดํดํ๋ ๋ฐ ๋์์ด ๋์์ต๋๋ค. ๋ค์์ ๊ทํ์ ์๋ฅผ ํ์ฅํ ๋ฒ์ ์ ๋๋ค. ๋งค์ฐ ๊ฐ์ฌ! ๊ทธ๋งํ ๊ฐ์น๊ฐ ์์ผ๋ฏ๋ก ์ด๊ฒ์ Stackoverflow์๋ ๊ฒ์ํ์ต๋๋ค .
# requires fabric 2.x - run 'pip install fabric' to install it
import logging, socket, paramiko.ssh_exception
from fabric import Connection, Config, SerialGroup, ThreadingGroup, exceptions, runners
from fabric.exceptions import GroupException
# Note: You need to supply your own valid servers here to ssh to of course!
def main():
testHosts("All should succeed", "validServer1,validServer2,validServer3")
testHosts("Some should fail", "validServer1,validServer2,BADSERVER1,validServer3,BADSERVER2")
def testHosts(message, hostsAsString):
print("")
print(message)
# Get list of hosts from somewhere, and convert them to connections
hosts = hostsAsString.split(",")
servers = [Connection(host=host) for host in hosts]
# Create a thread group to run requests in parallel
g = ThreadingGroup.from_connections(servers)
try:
command = "df -h / | tail -n1 | awk '{print $5}'"
results = g.run(command, hide=True)
for r in results:
connection = results[r]
print("{}".format(r.host) )
print(" SUCCESS, " + connection.stdout.strip())
except GroupException as e:
# If an exception occurred, at least one request failed.
# Iterate through results here
for c, r in e.result.items():
print("{}".format(c.host) )
if isinstance(r,runners.Result) :
print(" SUCCESS, " + r.stdout.strip())
elif isinstance(r,socket.gaierror) :
print(" FAILED, Network error")
elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
print(" FAILED, Auth failed")
else:
print(" FAILED, Something other reason")
main()
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋ ํ์ธ์,
shadyabhi์ ์๊ฒฌ ์ธ์๋ GroupException์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
๋๋ ์ด๊ฒ์ด ๋์์ด๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค !!
( ActivCloud ์ง์์์ ์ป์ ๊ฒฝ์ฐ)