def training(label=None, desc=[], log=[]): data = [] m = re.match('(\S+)_(\d+)$', label) model, signature = m.group(1), m.group(2) cmd = 'kubectl get pods -l model={},signature=s{}'.format(model, signature) output = check_output(cmd.split()).decode('ascii') if output: for line in output.split('\n'): if line and line.strip(): data.append(line.split()[:3]) data.pop(0) forms = StopForm(prefix='forms') if forms.validate_on_submit(): try: cmd = 'kubectl delete -f {}/train/{}/records/train.yaml'.format( os.environ['SHARED_HOST'], label) os.system(cmd) update_training(label, 'RUNNING') flash('Training Label {} scheduled to stop'.format(label)) except: flash('Failed to stop training label {}'.format(label)) return redirect(url_for('trainings', type='active')) formd = ShowForm(prefix='formd') if formd.validate_on_submit(): name = request.form['name'] try: cmd = 'kubectl describe pod {}'.format(name) desc = check_output(cmd.split()).decode('ascii').split('\n') except: desc = [ 'Oops, getting errors while retrieving descriptions', 'Maybe the conainer is not ready or teminated?' ] forml = ShowForm(prefix='forml') if forml.validate_on_submit(): name = request.form['name'] cmd = 'kubectl logs {}'.format(name) try: log = check_output(cmd.split()).decode('ascii').split('\n') except: log = [ 'Oops, getting error while retrieving logs', 'Maybe the job is not ready or terminated?' ] return render_template('training.html', label=label, data=data, forms=forms, formd=formd, forml=forml, desc=desc, log=log)
def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form # insert form data as a new Show record in the db, instead # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ form = ShowForm(request.form, meta={"csrf": False}) if form.validate_on_submit(): try: show: Show = Show() form.populate_obj(show) db.session.add(show) db.session.commit() # on successful db insert, flash success flash(f"Show {request.form['name']} was successfully listed!") except ValueError as e: print(sys.exc_info()) db.session.rollback() # on unsuccessful db insert, flash an error instead. flash(f"An error occurred: {str(e)}") finally: db.session.close() else: error_msg = [] for field, error in form.errors.items(): error_msg.append(f"{field}: {str(error)}") flash(f"Error occurred: {str(error_msg)}") return render_template('pages/home.html')
def create_show_submission(): form = ShowForm() error = False try: if form.validate_on_submit(): show = Show( artist_id=form.artist_id.data, venue_id=form.venue_id.data, start_time=form.start_time.data ) db.session.add(show) db.session.commit() except Exception as e: print('create_show_submission: ', e) db.session.rollback() print(sys.exc_info()) error = True finally: db.session.close() # on successful db insert, flash success if error: # on successful db insert, flash success # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Show could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ flash('An error occurred. Show could not be listed.') return render_template('forms/new_show.html', form=form) else: flash('Show was successfully listed!') return render_template('pages/home.html')
def create_show_submission(): error = False form = ShowForm() try: if form.validate_on_submit(): v_id = int(form.venue_id.data) show = Show(date_time=form.start_time.data, venue_id=v_id) db.session.add(show) db.session.commit() artist = Artist.query.get(form.artist_id.data) venue = Venue.query.get(v_id) show.artists.append(artist) venue.shows.append(show) db.session.add(venue) db.session.commit() flash('Show was successfully listed on {}!'.format( form.start_time.data)) return redirect(url_for('index')) flash('An error occurred. Show could not be listed. {}'.format( form.errors)) except (): db.session.rollback() error = True print(sys.exc_info()) finally: db.session.close() if error: abort(500) flash('There was an error, please try again.') return render_template('pages/home.html')
def create_show_submission(): form = ShowForm(request.form, meta={'csrf': False}) if form.validate_on_submit(): error = False print(form.artist_id.data.artistId()) try: show = Show(artist_id=form.artist_id.data.artistId(), ven_id=form.venue_id.data.venId(), start_time=form.start_time.data) db.session.add(show) db.session.commit() except BaseException as ee: print(ee) db.session.rollback() error = True finally: db.session.close() if error: flash('Error While Insert') else: flash('Show was successfully listed!') else: flash('Error ' + 'Missing Some Input') return render_template('pages/home.html')
def create_show_submission(): form = ShowForm() error = False try: if form.validate_on_submit(): show = Show(start_time=form.start_time.data, venue_id=form.venue_id.data, artist_id=form.artist_id.data) db.session.add(show) db.session.commit() except Exception as e: print('create_show_submission: ', e) db.session.rollback() print(sys.exc_info()) error = True finally: db.session.close() if error: flash('Oh!, an error occurred. Show could not be listed.') return render_template('forms/new_show.html', form=form) else: flash('Yaaay, Show was successfully listed!') return render_template('pages/home.html')
def create_show_submission(): error = False form = ShowForm(request.form) if not form.validate_on_submit(): flash('An error occurred. Show could not be listed.') return render_template('pages/home.html') # noinspection PyBroadException try: artist_id = request.form['artist_id'] venue_id = request.form['venue_id'] start_time = request.form['start_time'] show = Show(artist_id=artist_id, venue_id=venue_id, start_time=start_time) db.session.add(show) db.session.commit() except Exception: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash('An error occurred. Show could not be listed.') if not error: flash('Show was successfully listed') return render_template('pages/home.html')
def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form # TODO: insert form data as a new Show record in the db, instead form = ShowForm() created = False if form.validate_on_submit(): try: new_show = Show(start_time=form.start_time.data) new_show.venue = form.venue.data new_show.artist = form.artist.data db.session.add(new_show) db.session.commit() # on successful db insert, flash success created = True flash("Show was successfully listed!") except: print(sys.exc_info()) finally: db.session.close() if created: return redirect(url_for("index")) # TODO: on unsuccessful db insert, flash an error instead. flash("An error occurred. Show could not be listed.") return render_template("forms/new_show.html", form=form)
def create_show_submission(): """ Submit callback for show form """ # Get the form data artist_id = request.form['artist_id'] venue_id = request.form['venue_id'] start_time = request.form['start_time'] form = ShowForm() # Validate form data if not form.validate_on_submit(): flash(form.errors) # Redirect to the new_show.html page with the error message in the above line return redirect(url_for('create_show_submission')) error = False venue = Venue.query.get(venue_id) artist = Artist.query.get(artist_id) # Check if the venue id exists or not. if venue is None: flash('The venue id ' + venue_id + ' does not exist') return redirect(url_for('create_show_submission')) # Check if the artist id exists or not. if artist is None: flash('The artist id ' + artist_id + ' does not exist') return redirect(url_for('create_show_submission')) try: # Create Show instance using form data show = Show(venue_id=venue_id, artist_id=artist_id, start_time=start_time) db.session.add(show) db.session.commit() except(): db.session.rollback() error = True finally: db.session.close() if error: flash(f'An error occurred. Show could not be listed') abort(500) else: flash(f'Show listed successfully') return render_template('pages/home.html')
def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form # TODO: insert form data as a new Show record in the db, instead # on successful db insert, flash success # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Show could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ error = False form = ShowForm() try: if form.validate_on_submit(): new_show = Show(start_time=form.start_time.data) venue = Venue.query.get(form.venue_id.data) artist = Artist.query.get(form.artist_id.data) if venue is None or artist is None: flash('Check Id') return render_template('forms/new_show.html', form=form) new_show.venue = venue new_show.artist = artist venue.shows.append(new_show) artist.shows.append(new_show) db.session.add(new_show) db.session.commit() else: error = True for fieldName, errorMessages in form.errors.items(): for err in errorMessages: print(err, file=sys.stdout) except: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() # on successful db insert, flash success if (error): flash('Error!') return render_template('forms/new_show.html', form=form) else: flash('Show was successfully listed!') return redirect(url_for('index')) # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ return render_template('forms/new_show.html', form=form)
def create_show_submission(): form = ShowForm() if form.validate_on_submit(): try: show = Show() show.artist_id = form.artist_id.data.id show.venue_id = form.venue_id.data.id show.start_time = form.start_time.data db.session.add(show) db.session.commit() flash('Show was successfully listed!') except Exception as e: flash('An error occurred: ' + str(e)) db.session.rollback() finally: db.session.close() return redirect(url_for('shows')) return render_template('forms/new_show.html', form=form)
def create_show_submission(): form = ShowForm() if form.validate_on_submit(): try: show = Show() show.artist_id = request.form['artist_id'] show.venue_id = request.form['venue_id'] show.start_time = request.form['start_time'] db.session.add(show) db.session.commit() except Exception as e: db.session.rollback() flash('An error occurred. Show Could not be listed!, {}'.format( str(e))) finally: db.session.close() flash('Show was successfully listed!') return redirect(url_for('shows')) return render_template('forms/new_show.html', form=form)
def create_show_submission(): form = ShowForm() if not form.validate_on_submit(): flash(f'An error occurred. Show could not be listed.', 'error') return render_template('pages/home.html') try: new_show = Show.from_dict(form.data) print(new_show.id) db.session.add(new_show) db.session.commit() flash('Show was successfully listed!') except Exception as e: print(e) flash('An error occurred. Show could not be listed.') finally: db.session.close() return render_template('pages/home.html')
def create_show_submission(): form = ShowForm() if form.validate_on_submit(): try: show = Show(artist_id=form.artist_id.data, venue_id=form.venue_id.data, start_time=form.start_time.data) db.session.add(show) db.session.commit() flash('Show was successfully listed!') return render_template('pages/home.html') except Exception: db.session.rollback() print(sys.exc_info()) flash('An error occurred. Show could not be listed.') return render_template('pages/home.html') finally: db.session.close() else: return render_template('forms/new_show.html', form=form)
def create_show(): from forms import ShowForm form = ShowForm() if form.validate_on_submit(): show = Show() form.populate_obj(show) try: db.session.add(show) db.session.commit() except SQLAlchemyError: print(sys.exc_info()) db.session.rollback() db.session.close() flash('An error occurred. Show could not be listed.') return render_template('forms/new_show.html', form=form) flash('Show was successfully listed!') return render_template('pages/home.html') return render_template('forms/new_show.html', form=form)
def create_show_submission(): form = ShowForm(request.form) if form.validate_on_submit(): error = False try: show = Shows(artist_id=form.artist_id.data, venue_id=form.venue_id.data, start_time=form.start_time.data) show.insert() except Exception: error = True db.session.rollback() print(exc_info()) finally: db.session.close() flash("An error occurred. Show could not be listed")\ if error else flash('Show was successfully listed!') return render_template('pages/home.html') else: flash('Please ensure all details provided are valid') return render_template('forms/new_show.html', form=form)
def create_show_submission(): form = ShowForm() if form.validate_on_submit(): try: data = request.form show = Show(artist_id=data['artist_id'], venue_id=data['venue_id'], start_time=data['start_time']) db.session.add(show) db.session.commit() flash('Show was successfully listed!') except: db.session.rollback() flash(f'An error occured. Show could not be listed') finally: db.session.close() return render_template('pages/home.html') # Flashing current errors for input, errors in form.errors.items(): flash(f'Invalid value for "{input}". {"".join(errors)}') return render_template('forms/new_show.html', form=form)
def create_show_submission(): show = Show() form = ShowForm() if not form.validate_on_submit(): flash('Invalid value found in ' + ', '.join(form.errors.keys()) + ' field(s).') return render_template('forms/new_show.html', form=form) else: error = False try: form.populate_obj(show) db.session.add(show) db.session.flush() db.session.commit() except: error = True db.session.rollback() finally: db.session.close() if error: flash('An error occurred. Show could not be listed.') else: flash('Show was successfully listed!') return redirect(url_for('index'))
def create_show(): """ List a show Request query parameters: artist: id of artist selected for show venue: id of venue selected for show A GET returns an empty form A POST submits the info """ is_post = (request.method == 'POST') artist_id = int(request.args.get('artist', str(NO_SELECTION))) venue_id = int(request.args.get('venue', str(NO_SELECTION))) artists, venues = artists_and_venues() artist_choices = [(a[0], a[1]) for a in artists] artist_choices.insert(0, (NO_SELECTION, "Select artist")) venue_choices = [(v[0], v[1]) for v in venues] venue_choices.insert(0, (NO_SELECTION, "Select venue")) form = ShowForm() if is_post: artists = form.artist_id.data venues = form.venue_id.data start_time = form.start_time.data else: artists = list() venues = list() start_time = request.args.get('starttime', None) start_time = current_datetime( ) if start_time is None else str_to_datetime(start_time) # set choices & validators based on possible options set_singleselect_field_options( form.artist_id, artist_choices, [a[0] for a in artist_choices if a[0] != NO_SELECTION], artists) set_singleselect_field_options( form.venue_id, venue_choices, [v[0] for v in venue_choices if v[0] != NO_SELECTION], venues) if artist_id != NO_SELECTION: form.artist_id.data = artist_id if venue_id != NO_SELECTION: form.venue_id.data = venue_id if start_time is not None: form.start_time.data = start_time response = None status_code = HTTPStatus.OK.value # status code for GET if request.method == 'POST': status_code = HTTPStatus.ACCEPTED.value # status code for errors in form or conflict if form.validate_on_submit(): show = populate_show(show_factory(FactoryObj.OBJECT), form) verification = verify_show(show) artist = label_from_valuelabel_list(artist_choices, form.artist_id.data) venue = label_from_valuelabel_list(venue_choices, form.venue_id.data) if not verification["ok"]: def time_disp(dt): return dt.strftime(AVAILABILITY_TIME_FMT) if verification["show"] is not None: err = verification["show"] flash( f'Booking conflict. A show by {err.name} is booked from ' f'{time_disp(err.start_time)} to {time_disp(err.end_time)}.' ) if verification["artist"] is not None: err = verification["artist"] flash( f'Artist availability conflict. Artist is only available from ' f'{time_disp(err.start_time)} to {time_disp(err.end_time)}.' ) if not verification["availability"]: flash( f'Artist availability conflict. Artist is not available.' ) else: success = create_show_impl(show) if success: # on successful db insert, flash success flash(f'{artist} show at {venue} successfully listed!') else: flash( f'An error occurred. {artist} show at {venue} could not be listed.' ) response = make_response(redirect(url_for('index'))) status_code = response.status_code if response is None: response = make_response( render_template('forms/edit_show.html', form=form, title='Create Show', submit_action=url_for('create_show'), cancel_url=url_for('index'), submit_text='Create', submit_title='Create show')) response.status_code = status_code return response
def training_info(name=None, desc=[], log=[]): data = [] cmd = 'kubectl get pods -l name={}'.format(name) output = check_output(cmd.split()).decode('ascii') if output: for line in output.split('\n'): if line and line.strip(): data.append(line.split()[:3]) data.pop(0) t = db_session.query(TrainingModel).filter_by(name=name).first() if t.status == 'KILLED': forms = DeleteForm(prefix='forms') if forms.validate_on_submit(): try: cmd = 'rm -rf {}'.format(t.record_dir) os.system(cmd) db_session.delete(t) db_session.commit() flash('Training {} deleted'.format(name)) except: flash('Failed to delete training {}'.format(name)) return redirect(url_for('trainings', type='active')) else: forms = StopForm(prefix='forms') if forms.validate_on_submit(): try: cmd = 'kubectl delete -f {}/train.yaml'.format(t.record_dir) os.system(cmd) t.status = 'KILLED' db_session.commit() flash('Training {} killed'.format(name)) except: flash('Failed to kill training {}'.format(name)) return redirect(url_for('trainings', type='active')) formi = ShowForm(prefix='formi') if formi.validate_on_submit(): name = request.form['name'] try: cmd = 'kubectl describe pod {}'.format(name) desc = check_output(cmd.split()).decode('ascii').split('\n') except: desc = [ 'Oops, getting errors while retrieving descriptions', 'Maybe the conainer is not ready or teminated?' ] forml = ShowForm(prefix='forml') if forml.validate_on_submit(): name = request.form['name'] cmd = 'kubectl logs {}'.format(name) try: log = check_output(cmd.split()).decode('ascii').split('\n') except: log = [ 'Oops, getting error while retrieving logs', 'Maybe the job is not ready or terminated?' ] return render_template('training_info.html', name=name, data=data, forms=forms, formi=formi, forml=forml, desc=desc, log=log)