[2563] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | """Validate and add a new item to the Invirt build queue. |
---|
| 4 | |
---|
| 5 | This script, intended to be invoked by remctl, first validates the |
---|
| 6 | build submitted parameters, and then adds a new item to the |
---|
| 7 | Invirtibuilder build queue, triggering the Invirtibuilder to start the |
---|
| 8 | build. |
---|
| 9 | |
---|
| 10 | The expected arguments are |
---|
| 11 | |
---|
| 12 | pocket package commit |
---|
| 13 | |
---|
| 14 | This script will also automatically extract the Kerberos principal |
---|
| 15 | used to submit the job, and include that in the queue file for records |
---|
| 16 | keeping. |
---|
| 17 | """ |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | import datetime |
---|
[3028] | 21 | import optparse |
---|
[2563] | 22 | import os |
---|
| 23 | import sys |
---|
| 24 | import tempfile |
---|
| 25 | import uuid |
---|
| 26 | |
---|
| 27 | import invirt.builder as b |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | def main(): |
---|
[3028] | 31 | parser = optparse.OptionParser('Usage: %prog pocket package commit') |
---|
| 32 | opts, args = parser.parse_args() |
---|
| 33 | if len(args) != 3: |
---|
| 34 | parser.print_help() |
---|
| 35 | return 1 |
---|
| 36 | pocket, package, commit = args |
---|
[2563] | 37 | principal = os.environ['REMOTE_USER'] |
---|
| 38 | request_time = datetime.datetime.utcnow() |
---|
| 39 | q_path = os.path.join(b._QUEUE_DIR, |
---|
| 40 | '%s_%s' % (request_time.strftime('%Y%m%d%H%M%S'), |
---|
| 41 | uuid.uuid4())) |
---|
| 42 | |
---|
| 43 | try: |
---|
[3035] | 44 | # TODO: clean up this interface. |
---|
| 45 | b.ensureValidPackage(package) |
---|
| 46 | commit = b.canonicalize_commit(package, commit) |
---|
[2639] | 47 | b.validateBuild(pocket, package, commit) |
---|
[2563] | 48 | except b.InvalidBuild, e: |
---|
[3036] | 49 | msg = "E: %s" % e |
---|
| 50 | print >>sys.stderr, msg |
---|
| 51 | # Prevent an attack by submitting excessively long arguments |
---|
| 52 | args = [arg[:min(len(arg), 80)] for arg in (pocket, package, commit)] |
---|
| 53 | b.runHook('failed-submit', args + [principal], stdin_str=msg) |
---|
[2563] | 54 | sys.exit(1) |
---|
| 55 | |
---|
| 56 | # To keep from triggering the Invirtibuilder before we've actually |
---|
| 57 | # written the file out, first write the queue entry to a temporary |
---|
| 58 | # file, and then move it into the queue directory. |
---|
[2567] | 59 | q_fd, q_name = tempfile.mkstemp() |
---|
[2569] | 60 | q = os.fdopen(q_fd, 'r+') |
---|
[2563] | 61 | print >>q, "%s %s %s %s" % (pocket, package, commit, principal) |
---|
[3028] | 62 | q.close() |
---|
[2567] | 63 | os.rename(q_name, q_path) |
---|
[3036] | 64 | short_commit = b.canonicalize_commit(package, commit, shorten=True) |
---|
| 65 | b.runHook('post-submit', [pocket, package, short_commit, principal]) |
---|
| 66 | print '%s, your job to build %s for %s:%s has been submitted!' % (principal, short_commit, package, pocket) |
---|
[2563] | 67 | |
---|
| 68 | |
---|
| 69 | if __name__ == '__main__': |
---|
| 70 | main() |
---|