[PYTHON] Define a task to set the fabric env in YAML

Tasks for environment definition

One way to define the server configuration by role in fabric is to define a task that specifies a host list in ʻenv.roledefs`.

environments.py


@task
def env_test():
    u'''Test settings'''
    env.roledefs = {
        "web": ["www.test"],
        "db" : ["db.test"]
    }

@task
def env_production():
    u'''Production settings'''
    env.roledefs = {
        "web": ["www01","www02"],
        "db" : ["db01"]
    }

However, it is a bit redundant, so I tried to generate it automatically from YAML.

YAML version

env.yaml


test:
    roledefs:
        web: [www.test]
        db: [db.test]
production:
    roledefs:
        web: [www01, www02]
        db: [db01]

Define a helper that reads data and creates tasks like this. (Reference: http://www.backlog.jp/blog/2013/09/fabric-advanced.html)

helper/yaml_loader.py


# -*- coding: utf-8 -*-

import os, time
import yaml
from fabric.api import env,task

def load_env_from_yaml(yaml_file, namespace):
    u'''Create an execution environment task from a yaml file'''
    if os.path.exists(yaml_file):
        env_data = yaml.safe_load(open(yaml_file).read())
        for env_name, env_dict in env_data.items():
            __create_env_task(env_name, env_dict, namespace)
    else:
        print('No {0} found. skipped.'.format(yaml_file))

def __create_env_task(env_name, env_dict, namespace):
    u'''Execution environment task definition'''

    f = lambda: env.update(env_dict)
    f.__doc__ = u'''Set environment for {0}'''.format(env_name)

    task_name = 'env_{0}'.format(env_name)
    wrapper = task(name=task_name)
    rand = '%d' % (time.time() * 100000)
    namespace['task_%s_%s' % (task_name, rand)] = wrapper(f)

Read and use with fabfile.py.

fabfile.py


import os
from helper.yaml_loader import load_env_from_yaml

# Execute fabric at fabfile.py directory.
__fabric_dir = os.path.abspath(os.path.dirname(__file__))
os.chdir(__fabric_dir)

# Load Environments
load_env_from_yaml('env.yaml', globals())

Note that the task cannot be used unless globals () is passed from the fabfile.

TODO

--Exception handling --Allow task_name and task_description to be defined for readability. ――Is there a key that should not be used in ʻenv`? (However, it can be written by the above method)

Recommended Posts

Define a task to set the fabric env in YAML
I want to set a life cycle in the task definition of ECS
If you want to switch the execution user in the middle of a Fabric task, settings context manager
Set a fixed IP in the Linux environment
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
The easiest way to set up Last-Modified in Flask
Register a task in cron for the first time
Set the form DateField to type = date in Django
How to set a shared folder with the host OS in CentOS7 on VirtualBOX
Find a guideline for the number of processes / threads to set in the application server
Dynamically set env.hosts in fabric
A memorandum to register the library written in Hy in PyPI
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to define multiple variables in a python for statement
How to set the html class attribute in Django's forms.py
Put the lists together in pandas to make a DataFrame
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
To write a test in Go, first design the interface
Output the key list included in S3 Bucket to a file
How to set the output resolution for each keyframe in Blender
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to get all the possible values in a regular expression
How to check the memory size of a variable in Python
Notify the contents of the task before and after executing the task in Fabric
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
[Python3] Define a decorator to measure the execution time of a function
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
Find all patterns to extract a specific number from the set
I made a command to display a colorful calendar in the terminal
Attempt to extend a function in the library (add copy function to pathlib)
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Set opset to embed in ONNX
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
Pass arguments to Task in discord.py
Cython to try in the shortest
Programming to fight in the world ~ 5-2
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
[Python] What is a formal argument? How to set the initial value
How to specify a .ui file in the dialog / widget GUI in PySide
[Linux] Command to get a list of commands executed in the past
I want to sort a list in the order of other lists
Added a function to register desired shifts in the Django shift table
[Development environment] How to create a data set close to the production DB
Use a shortcut to enable or disable the touchpad in Linux Mint
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
What is the fastest way to create a reverse dictionary in python?
How to sort by specifying a column in the Python Numpy array.
I want to print in a comprehension
How to dynamically define variables in Python
Write the test in a python docstring
Set the time zone to Japan Standard Time
Give a title to the ipywidgets tab