recipe_template.py

Recipes define subsystems that are distinct parts of larger systems. They are the basic units of setup in frycook. Generally a recipe corresponds to an os-level package that needs to be installed or configured.

Recipe

class frycook.recipe_template.Recipe(settings, environment, ok_to_be_rude, no_prompt)[source]

The Recipe class is the base class for all recipes to subclass. It defines the framework for recipes and some helper functions. By itself it doesn’t do much.

It has a set of helper functions that are hooks called by frycooker to apply the recipe to a remote server:

handle_pre_apply_message()
handle_post_apply_message()
run_apply()
run_messages()

It has another set of helper functions used within recipes for copying files to remote servers:

get_local_file_perms()
push_file()
push_template()
push_package_file_set()

It has a final set of helper functions used within recipes for managing git repos on remote servers:

push_git_repo()
clone_git_repo()
update_git_repo()
is_git_repo()
ensure_git_repo()
apply(computer)[source]

Define the actions to take place when this recipe is applied to a computer. Override this function in your subclass of Recipe if you have any actions to apply. If you don’t have any actions, then why are you creating a recipe?

Parameters:computer (string) – name of computer to apply recipe to
clone_git_repo(user, git_url, target_path)[source]

Clone a git repo on a remote server.

Parameters:
  • git_url (string) – git url of repo (probably from github)
  • target_path (string) – root path on remote server to clone git repo into
ensure_git_repo(user, git_url, target_path)[source]

Make sure a git repo exists in the target path on the remote computer.

Parameters:
  • git_url (string) – git url of repo (probably from github)
  • target_path (string) – root path on remote server to check git repo
Return type:

boolean

Returns:

True if repo already existed, False if not

get_local_file_perms(local_name)[source]

Get a string of the permissions set on a local file.

Parameters:local_name (string) – path to file on local file system
Return type:string
Returns:string containing perms of file, ie. ‘655’
handle_post_apply_message()[source]

Print the post-apply message for the user and wait for him/her to hit return before continuing.

handle_pre_apply_message()[source]

Print the pre-apply message for the user and wait for him/her to hit return before continuing.

is_git_repo(target_path)[source]

Make sure the target path exists on the remote computer and is really a git repo.

Parameters:
  • git_url (string) – git url of repo (probably from github)
  • target_path (string) – root path on remote server to check git repo
Return type:

boolean

Returns:

True if repo already existed, False if not

pre_apply_checks(computer)[source]

Define checks to run before applying the recipe. The base class checks that the designated computer exists in the environment dictionary. This is a good place to check other things in your environment dictionary that the apply function expects to be there. Override this function in your subclass of Recipe if you have any checks to perform. Raise a RecipeException if you encounter exceptional conditions. Be sure to call the base class function to make sure its checks get done as well.

Parameters:computer (string) – name of computer to apply recipe checks to
Raises RecipeException:
 raised if the computer name is not in the environment being processed
push_file(local_name, remote_name, owner, group, perms=None)[source]

Copy a file to a remote server if the file is different or doesn’t exist.

Parameters:
  • local_name (string) – path within packages dir of file to upload (path + filename)
  • remote_name (string) – remote path to write file to (path + filename)
  • owner (string) – owner of the file
  • group (string) – group of the file
  • perms (string) – permissions for the file, ie. ‘655’
push_git_repo(computer, user, group, git_url, target_path)[source]

Make a local clone of the repo in git_url into the temp directory specified in the settings file, then rsync it to the remote path.

Parameters:
  • computer (string) – computer name to push to
  • user (string) – user to own the files in the repo
  • group (string) – group to own the files in the repo
  • git_url (string) – git url of repo (probably from github)
  • target_path (string) – root path on remote server to copy git repo to
push_package_file_set(package_name, computer_name, aux_env=None)[source]

Copy a set of files to a remote server, maintaining the same directory structure and processing any templates encountered. This copies the files to the root of the destination, so that things like /etc/hosts or /etc/nginx/nginx.conf get put in the right place.

First create a directory with the same name as package_name in your root packages directory. Then create a directory structure under that that mirrors the target machine and all files will get copied there in the correct place.

For template file processing, a default environment dictionary will be passed in consisting of:

{"computer": host_env["computers"][computer_name]}

Template files have a .tmplt extension.

If aux_env is given, it will be added to the default dictionary using dict.update() after the default dictionary is constructed. This means that if you pass in an aux_env dictionary with a key called “computer”, that that key will over-write the default key of that name.

If a file named fck_metadata.txt is encountered in a directory, then it’s expected to have contents of lines consisting of <path>:<owner>:<group>:<perms> that specifies the owner, group, and permissions for the path specified (directory-relative).

If a file named fck_delete.txt is encountered in a directory, then it’s expected to have contents of lines containing names of files to delete, one per line. This way you can clean out a directory as well as copy files to it.

Parameters:
  • package_name (string) – name of package to process, corresponds to directory in packages directory
  • template_env (dict) – environment dictionary for template engine
  • aux_env (dict) – additional key/value pairs for the template environment
push_template(templatename, out_path, enviro, owner, group, perms=None)[source]

Process a template file and push its contents to a remote server if it’s different than what’s already there.

Parameters:
  • templatename (string) – path within packages dir of template file to process (path + filename)
  • out_path (string) – path on remote server to write file to (path + filename)
  • enviro (dict) – environment dictionary for template engine
  • owner (string) – owner of the templated file
  • group (string) – group of the templated file
  • perms (string) – permissions for the templated file, ie. ‘655’
run_apply(computer)[source]

Run the apply sequence of functions. This is typically called by frycooker.

Sequence:

pre_apply_checks() -> apply()
Parameters:computer (string) – name of computer to apply recipe to
run_messages()[source]

Print the pre and post apply messages only, as if the apply had been run.

update_git_repo(user, git_url, target_path)[source]

Update an existing git repo on a remote server.

Parameters:
  • git_url (string) – git url of repo (probably from github)
  • target_path (string) – root path on remote server to update git repo in

RecipeException

exception frycook.recipe_template.RecipeException[source]

A RecipeException exception is raised for exceptional conditions encountered while using the Recipe class.

FileMetaDataTracker

class frycook.recipe_template.FileMetaDataTracker[source]

A FileMetaDataTracker object keeps track of owner, group, and permissions for files and directories during a push_package_fileset operation. This hinges on a file named fck_metadata.txt being encountered in the directory being examined. This file should have each line contain the text <filename>:<owner>:<group>:<perms>, where <filename> is either a file name or ‘.’ for the directory itself, <owner> is the owner’s account name, <group> is the group’s name, and <perms> is the permissions string..

check_directory(root, dirs, files)[source]

Read in the metadata for the given directory from the fck_metadata.txt file in the directory, or remember metadata from previous calls if no fck_metadata.txt file is encountered.

The input parameters are expected to be the values returned from a call to os.walk()

Parameters:
  • root (string) – directory being examined
  • dirs (list of strings) – list of sub-directories under the root directory
  • files (list of strings) – list of the files in the root directory
get_metadata(path, filename='')[source]

Lookup the owner, group, and permissions for the given path and filename from the metadata dictionary in this object. If no filename is specified, the data is retrieved for the directory specified in path.

Parameters:
  • path (string) – path to file to get metadata for
  • filename (string) – name of file to get metadata for (leave blank if just getting directory metadata)
Return type:

tuple of strings

Returns:

tuple containing (<owner>, <group>, <perms>, )

FileDeleter

class frycook.recipe_template.FileDeleter[source]

A FileDeleter object deletes unwanted files from a directory on a remote server. This hinges on a file named fck_delete.txt being encountered in the directory being examined. This file should have each line contain the name of a file to be deleted.

check_directory(root, files, remote_rootpath)[source]

Examine the given directory, check for a fck_delete.txt file in the directory, and if it exists delete all remote files named in it.

Parameters:
  • root (string) – local directory possibly containing fck_delete.txt
  • files (list of strings) – list of the files in the local root directory
  • remote_rootpath (string) – path on remote server to delete files from

Table Of Contents

Previous topic

Intro

This Page