Using the context manager called fabric.api.settings, you can work while switching the execution user in the middle of a predetermined task.
For example
If you want to create such a task, write the following code.
operationwithswitchinguser.py
from fabric.api as fb
import cuisine as cs
def refresh(branch="origin/master", restart="yes"):
with fb.settings(
cs.mode_user(),
user="www",
secretkey="./fixtures/id_rsa",
):
with fb.cd("/home/www/repo"):
cs.run("git fetch")
cs.run("git checkout %s" % branch)
if restart == "yes":
with fb.settings(
cs.mode_sudo(),
user="admin",
secretkey="./fixtures/id_rsa",
):
cs.run("/etc/init.d/nginx restart")
cs.run("/etc/init.d/supervisord restart")
settings context manager
It has such specifications. In other words
with fb.settings(
cs.mode_user(),
user="www",
secretkey="./fixtures/id_rsa",
):
If you write code like this
It moves like that.
I wish I had noticed this specification sooner. Because I was wasting myself writing a decorator for the user switch.
Recommended Posts