def dockerfile(bid): """ Displays the dockerfile contents for the build in question. :param bid: Identfies the build object in the database. :return: An HTML template rendered with log information, sent periodically from worker nodes. """ if request.method == 'GET': fname = 'dockerfile.html' if demo_mode: fname = 'demo/dockerfile.html' bid_short = bid[0:5] created = "Unknown" description = "Unknown" git_url = "Unknown" state = "Unknown" dockerfile = "<center>This build is waiting for a worker.</center>" err = False b = models.Build.query.filter_by(bid=bid).first() if b is None: err = True else: form = Dockerfile() created = formatting.pretty_date(b.created) description = b.options['description'] git_url = b.options['url'] state = b.state if b.dockerfile is not None: dockerfile = b.dockerfile form.dockerfile.data = dockerfile port_data = json.dumps(b.options['ports']) return render_template(fname, title='Chorus - Logs', ports=port_data, form=form, bid=bid, bid_short=bid_short, created=created, description=description, git_url=git_url, state=state, err=False) elif request.method == 'POST': b = models.Build.query.filter_by(bid=bid).first() if b is None or b.state not in ['Stopped', 'Failed']: return make_response(redirect('/queue')) else: form = Dockerfile(request.form) if form.validate(): options = dict(b.options) options['bid'] = str(uuid.uuid4()) options['timeout'] = app.config['BUILD_TIMEOUT'] options['dockerfile_content'] = form.dockerfile.data # Insert build into db db.session.commit() q.schedule(options) return make_response(redirect('/queue'))
def build(): """ This route is the main build page, and provides a form that the user can fill out to schedule a build. :return: A redirect to the queue page to view the status of the build, or an html response with an error message. """ if request.method == 'GET': form = BuildForm() fname = 'build.html' validation = False if demo_mode: form.dockerfile.data='testing/apache/Dockerfile' form.giturl.data='https://github.com/niall-byrne/containers.git' validation = 'This form is locked down for the demo.' fname = 'demo/build.html' return render_template(fname, title='Chorus - Build', form=form, demo=demo_mode, validation=validation) if request.method == 'POST': form = BuildForm(request.form) fname = 'build.html' if demo_mode: fname = 'demo/build.html' if form.validate(): # Validate Dynamic Fields outside of WTF for key, value in request.form.iteritems(): if 'host' in key: if not value.isdigit(): form.errormsg = "Ports must be integers." if 'container' in key: if not value.isdigit(): form.errormsg = "Ports must be integers." if hasattr(form, 'errormsg'): msg = form.errormsg form = BuildForm(request.form) return render_template(fname, title='Chorus - Build', form=form, demo=demo_mode, validation=msg) else: options = dict() options['bid'] = str(uuid.uuid4()) options['timeout'] = app.config['BUILD_TIMEOUT'] options['description'] = form.description.data options['url'] = form.giturl.data options['dockerfile'] = form.dockerfile.data if len(form.post.data) > 0: options['post'] = form.post.data options['ports'] = { 'container': [], 'host': [] } for key, value in request.form.iteritems(): if 'host' in key: v = int(key.replace('host', '')) options['ports']['host'].append(request.form['host%d' % v]) options['ports']['container'].append(request.form['container%d' % v]) # Insert build into db q.schedule(options) return make_response(redirect("/queue"))