A collection of CLI tools and utilities used by Trigger.
Returns an md5-crypt hash of a clear-text password.
To get md5-crypt from crypt(3) you must pass an 8-char string starting with ‘$1$’ and ending with ‘$’, resulting in a 12-char salt. This only works on systems where md5-crypt is default and is currently assumed to be Linux.
Parameters: | passwd – Password string to be encrypted |
---|
Command-line interface utilities for Trigger tools. Intended for re-usable pieces of code like user prompts, that don’t fit in other utils modules.
Present a yes-or-no prompt, get input, and return a boolean.
The default argument is ignored if autoyes is set.
Parameters: |
|
---|
Default behavior (hitting “enter” returns False):
>>> yesno('Blow up the moon?')
Blow up the moon? (y/N)
False
Reversed behavior (hitting “enter” returns True):
>>> yesno('Blow up the moon?', default=True)
Blow up the moon? (Y/n)
True
Automatically return True with autoyes; no prompt is displayed:
>>> yesno('Blow up the moon?', autoyes=True)
True
Find and return stdout’s terminal width, if applicable.
Find and return stdouts terminal size as (height, width)
Prints a whirlygig for use in displaying pending operation in a command-line tool. Guaranteed to make the user feel warm and fuzzy and be 1000% bug-free.
Parameters: |
|
---|
Example:
>>> Whirlygig("Doing stuff:", "Done.", 12).run()
Executes the whirlygig!
Used to supress output to sys.stdout (aka print).
Example:
>>> from trigger.utils.cli import NullDevice
>>> import sys
>>> print "1 - this will print to STDOUT"
1 - this will print to STDOUT
>>> original_stdout = sys.stdout # keep a reference to STDOUT
>>> sys.stdout = NullDevice() # redirect the real STDOUT
>>> print "2 - this won't print"
>>>
>>> sys.stdout = original_stdout # turn STDOUT back on
>>> print "3 - this will print to SDTDOUT"
3 - this will print to SDTDOUT
Prints a demon holding a severed head. Best used when things go wrong, like production-impacting network outages caused by fat-fingered ACL changes.
Thanks to Jeff Sullivan for this best error message ever.
Takes an epoch timestamp and returns string of minutes:seconds.
Parameters: | secs – Timestamp (in seconds) |
---|
>>> import time
>>> start = time.time() # Wait a few seconds
>>> finish = time.time()
>>> min_sec(finish - start)
'0:11'
Print a pretty version of timestamp, including timezone info. Expects the incoming datetime object to have proper tzinfo.
Parameters: | t – A datetime.datetime object |
---|
>>> import datetime
>>> from pytz import timezone
>>> localzone = timezone('US/Eastern')
<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>
>>> t = datetime.datetime.now(localzone)
>>> print t
2011-07-19 12:40:30.820920-04:00
>>> print pretty_time(t)
09:40 PDT
>>> t = datetime.datetime(2011,07,20,04,13,tzinfo=localzone)
>>> print t
2011-07-20 04:13:00-05:00
>>> print pretty_time(t)
tomorrow 02:13 PDT
Present a proceed prompt. Return True if Y, else False
Utils to import modules.
Taken verbatim from django.utils.importlib in Django 1.4.
Import a module and return the module object.
The package argument is required when performing a relative import. It specifies the package to use as the anchor point from which to resolve the relative import to an absolute import.
Import a module from a file path and return the module object.
Allows one to import from anywhere, something __import__() does not do. The module is added to sys.modules as global_name.
Parameters: |
|
---|
Functions that perform network-based things like ping, port tests, etc.
Returns pass/fail for a ping. Supports POSIX only.
Parameters: |
|
---|
>>> from trigger.utils import network
>>> network.ping('aol.com')
True
>>> network.ping('192.168.199.253')
False
Attempts to connect to a TCP port. Returns a Boolean.
If check_result is set, the first line of output is retreived from the connection and the starting characters must match expected_result.
Parameters: |
|
---|
>>> test_tcp_port('aol.com', 80)
True
>>> test_tcp_port('aol.com', 12345)
False
Connect to a TCP port and confirm the SSH version. Defaults to SSHv2.
Note that the default of (‘SSH-1.99’, ‘SSH-2.0’) both indicate SSHv2 per RFC 4253. (Ref: http://en.wikipedia.org/wiki/Secure_Shell#Version_1.99)
Parameters: |
|
---|
>>> test_ssh('localhost')
True
>>> test_ssh('localhost', version='SSH-1.5')
False
Determines if an IP address is internal to your network. Relies on networks specified in settings.INTERNAL_NETWORKS.
Parameters: | ip – IP address to test. |
---|
>>> address_is_internal('1.1.1.1')
False
Pluggable event notification system for Trigger.
Sends an email to a list of recipients. Returns True when done.
Parameters: |
|
---|
Simple entry point into notify that takes any arguments and tries to handle them to send a notification.
This relies on handlers to be definied within settings.NOTIFICATION_HANDLERS.
Default email notification handler.
Iterate thru registered handlers to handle events and send notifications.
Handlers should return True if they have performed the desired action or None if they have not.
Handlers for event notifications.
Handlers are specified by full module path within settings.NOTIFICATION_HANDLERS. These are then imported and registered internally in this module.
The primary public interface to this module is notify which is in turn called by send_notification to send notifications.
Handlers should return True if they have performed the desired action or None if they have not.
A handler can either define its own custom behavior, or leverage a custom Event object. The goal was to provide a simple public interface to customizing event notifications.
If not customized within NOTIFICATION_HANDLERS, the default notification type is an EmailEvent that is handled by email_handler.
Default email notification handler.
Iterate thru registered handlers to handle events and send notifications.
Handlers should return True if they have performed the desired action or None if they have not.
Event objects for the notification system.
These are intended to be used within event handlers such as email_handler().
If not customized within NOTIFICATION_HANDLERS, the default notification type is an EmailEvent that is handled by email_handler.
Base class for events.
It just populates the attribute dict with all keyword arguments thrown at the constructor.
All Event objects are expected to have a .handle() method that willl be called by a handler function. Any user-defined event objects must have a working .handle() method that returns True upon success or None upon a failure when handling the event passed to it.
If you specify required_args, these must have a value other than None when passed to the constructor.
Base class for notification events.
The title and message arguments are the only two that are required. This is to simplify the interface when sending notifications and will cause notifications to send from the default sender to the default ``recipients that are specified withing the global settings.
If sender or recipients are specified, they will override the global defaults.
Note that this base class has no .handle() method defined.
Parameters: |
|
---|
An email notification event.
Provides a CVS like wrapper for local RCS (Revision Control System) with common commands.
Simple wrapper for CLI rcs command. An instance is bound to a file.
Parameters: |
|
---|
>>> from trigger.utils.rcs import RCS
>>> rcs = RCS('foo')
>>> rcs.lock()
True
>>> f = open('foo', 'w')
>>> f.write('bar\n')
>>> f.close()
>>> rcs.checkin('This is my commit message')
True
>>> print rcs.log()
RCS file: RCS/foo,v
Working file: foo
head: 1.2
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 2; selected revisions: 2
description:
----------------------------
revision 1.2
date: 2011/07/08 21:01:28; author: jathan; state: Exp; lines: +1 -0
This is my commit message
----------------------------
revision 1.1
date: 2011/07/08 20:56:53; author: jathan; state: Exp;
first commit
Perform an RCS checkin. If successful this also unlocks the file, so there is no need to unlock it afterward.
Parameters: |
|
---|
>>> rcs.checkin('This is my commit message')
True
Perform an RCS checkout with lock. Returns boolean of whether lock was sucessful.
Parameters: | verbose – Print command output |
---|
>>> rcs.lock()
True
Keep trying to lock the file until a lock is obtained.
Parameters: |
|
---|
>>> rcs.lock_loop(timeout=1)
Sleeping to wait for the lock on the file: foo
Sleeping to wait for the lock on the file: foo
>>> rcs.lock_loop(timeout=1, verbose=True)
RCS/foo,v --> foo
co: RCS/foo,v: Revision 1.2 is already locked by joe.
Sleeping to wait for the lock on the file: foo
RCS/foo,v --> foo
co: RCS/foo,v: Revision 1.2 is already locked by joe.
Returns the RCS log as a string (see above).
Perform an RCS checkout with unlock (for cancelling changes).
Parameters: | verbose – Print command output |
---|
>>> rcs.unlock()
True