1#!/usr/bin/env python
2# Copyright (c) 2009 Chris Moyer http://coredumped.org/
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
22#
23# Task/Job Administration utility
24#
25VERSION="0.1"
26__version__ = VERSION
27usage = """%prog [options] [command]
28Commands:
29    list|ls                                List all Tasks in SDB
30    delete      <id>                       Delete Task with id <id>
31    get         <name>                     Get Task <name>
32    create|mk   <name> <hour> <command>    Create a new Task <name> with command <command> running every <hour>
33"""
34
35def list():
36    """List all Tasks in SDB"""
37    from boto.manage.task import Task
38    print "%-8s %-40s %s" %  ("Hour", "Name", "Command")
39    print "-"*100
40    for t in Task.all():
41        print "%-8s %-40s %s" % (t.hour, t.name, t.command)
42
43def get(name):
44    """Get a task
45    :param name: The name of the task to fetch
46    :type name: str
47    """
48    from boto.manage.task import Task
49    q = Task.find()
50    q.filter("name like", "%s%%" % name)
51    for t in q:
52        print "="*80
53        print "|               ", t.id
54        print "|%s" %  ("-"*79)
55        print "| Name:         ", t.name
56        print "| Hour:         ", t.hour
57        print "| Command:      ", t.command
58        if t.last_executed:
59            print "| Last Run:     ", t.last_executed.ctime()
60            print "| Last Status:  ", t.last_status
61            print "| Last Run Log: ", t.last_output
62        print "="*80
63
64def delete(id):
65    from boto.manage.task import Task
66    t = Task.get_by_id(id)
67    print "Deleting task: %s" % t.name
68    if raw_input("Are you sure? ").lower() in ["y", "yes"]:
69        t.delete()
70        print "Deleted"
71    else:
72        print "Canceled"
73
74def create(name, hour, command):
75    """Create a new task
76    :param name: Name of the task to create
77    :type name: str
78    :param hour: What hour to run it at, "*" for every hour
79    :type hour: str
80    :param command: The command to execute
81    :type command: str
82    """
83    from boto.manage.task import Task
84    t = Task()
85    t.name = name
86    t.hour = hour
87    t.command = command
88    t.put()
89    print "Created task: %s" % t.id
90
91if __name__ == "__main__":
92    try:
93        import readline
94    except ImportError:
95        pass
96    import boto
97    import sys
98    from optparse import OptionParser
99    from boto.mashups.iobject import IObject
100    parser = OptionParser(version=__version__, usage=usage)
101
102    (options, args) = parser.parse_args()
103
104    if len(args) < 1:
105        parser.print_help()
106        sys.exit(1)
107
108    command = args[0].lower()
109    if command in ("ls", "list"):
110        list()
111    elif command == "get":
112        get(args[1])
113    elif command == "create":
114        create(args[1], args[2], args[3])
115    elif command == "delete":
116        delete(args[1])
117