If you want to switch the execution user in the middle of a Fabric task, settings context manager

I want to switch the execution user in one task

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

  1. Pull from github --www Working as a user
  2. Nginx, supervisord restart --work as admin user

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 About context manager

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

  1. First perform sudo / normal mode switching of cuisine.
  2. Set the env user / public key to www, ./fixtures/id_rsa

It moves like that.

Impressions

I wish I had noticed this specification sooner. Because I was wasting myself writing a decorator for the user switch.

Recommended Posts

If you want to switch the execution user in the middle of a Fabric task, settings context manager
I want to set a life cycle in the task definition of ECS
Define a task to set the fabric env in YAML
If you want to assign csv export to a variable in python
[Django] What to do if the model you want to create has a large number of fields
I want to sort a list in the order of other lists
[Python] If you want to draw a scatter plot of multiple clusters
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
To output a value even in the middle of a cell with Jupyter Notebook
If you want to create a Word Cloud.
I made an appdo command to execute a command in the context of the app
I want to see a list of WebDAV files in the Requests module
What is the last programming language you learn in your life? (If you want to catch up with a club with a lifetime salary of 300 million yen)
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
How to check in Python if one of the elements of a list is in another list
A story about trying to introduce Linter in the middle of a Python (Flask) project
I want to grep the execution result of strace
When you want to plt.save in a for statement
If you want to put an argument in the closure function and execute it later
How to pass the execution result of a shell command in a list in Python (non-blocking version)
If you want your colleagues to use the same language
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
How to take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
[Django] Carefully explain the escape route if you really want to use the table of another application
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
I want to record the execution time and keep a log.
If you want to become a data scientist, start with Kaggle
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
[Linux] I want to know the date when the user logged in
What to do if you get a minus zero in Python
How to check the memory size of a variable in Python
If you give a list with the default argument of the function ...
Notify the contents of the task before and after executing the task in Fabric
How to check the memory size of a dictionary in Python
When you want to save the result of the callback function somewhere
[Python3] Define a decorator to measure the execution time of a function
How to get the vertex coordinates of a feature in ArcPy
Check if you can connect to a TCP port in Python
What to do if you can't use the trash in Lubuntu 18.04.
Create a function to get the contents of the database in Go
I want to know the population of each country in the world.
[NNabla] How to remove the middle tier of a pre-built network
A note that you want to manually decorate the parameters passed in the Django template form item by item
If you just want to get the dump file of the server, it was convenient to build an http server
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
Have Alexa run Python to give you a sense of the future
If you want to include awsebcli with CircleCI, specify the python version
Python Note: When you want to know the attributes of an object
[Linux] Command to get a list of commands executed in the past
(Python Selenium) I want to check the settings of the download destination of WebDriver
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
When you want to change the HTTP headers of Flask's test client
[NNabla] How to add a quantization layer to the middle layer of a trained model
If you want to get multiple statistics with groupby in pandas v1
I want to color a part of an Excel string in Python
If you want to count words in Python, it's convenient to use Counter.
Allow Slack to notify you of the end of a time-consuming program process