def on_created(self, event): if isinstance(event, DirCreatedEvent): if self.base not in event.src_path: return rel_path = os.path.relpath(event.src_path, self.base) # we only care about this path if it's under a user dir # user/upload/deployment-name path_parts = rel_path.split(os.sep) if len(path_parts) != 3: return logger.info("New deployment directory: %s", rel_path) with app.app_context(): deployment = db.Deployment.find_one( {'deployment_dir': event.src_path}) if deployment is None: deployment = db.Deployment() usr = db.User.find_one({'username': str(path_parts[0])}) if hasattr(usr, '_id'): deployment.user_id = usr._id deployment.name = str(path_parts[2]) deployment.deployment_dir = str(event.src_path) deployment.updated = datetime.utcnow() deployment.save() elif isinstance(event, FileCreatedEvent): self.file_moved_or_created(event)
def on_created(self, event): if isinstance(event, DirCreatedEvent): if self.base not in event.src_path: return rel_path = os.path.relpath(event.src_path, self.base) # we only care about this path if it's under a user dir # user/upload/deployment-name path_parts = rel_path.split(os.sep) if len(path_parts) != 3: return logger.info("New deployment directory: %s", rel_path) with app.app_context(): deployment = db.Deployment.find_one({'deployment_dir':event.src_path}) if deployment is None: deployment = db.Deployment() usr = db.User.find_one( { 'username' : unicode(path_parts[0]) } ) if hasattr(usr, '_id'): deployment.user_id = usr._id deployment.name = unicode(path_parts[2]) deployment.deployment_dir = unicode(event.src_path) deployment.updated = datetime.utcnow() deployment.save() elif isinstance(event, FileCreatedEvent): if self.base not in event.src_path: return path_parts = os.path.split(event.src_path) with app.app_context(): deployment = db.Deployment.find_one({'deployment_dir' : path_parts[0]}) if deployment is None: logger.error("Cannot find deployment for %s", path_parts[0]) return if path_parts[-1] == "wmoid.txt": rel_path = os.path.relpath(event.src_path, self.base) logger.info("New wmoid.txt in %s", rel_path) if deployment.wmo_id: logger.info("Deployment already has wmoid %s. Updating value with new file.", deployment.wmo_id) with open(event.src_path) as wf: deployment.wmo_id = unicode(wf.readline().strip()) deployment.save() else: # Always save the Deployment when a new dive file is added # so a checksum is calculated and a new deployment.json file # is created fname, ext = os.path.splitext(path_parts[-1]) if ext not in ['.md5', '.txt']: deployment.save()
def new_deployment(username): user = db.User.find_one({'username': username}) if user is None or (user is not None and not current_user.is_admin() and current_user != user): # No permission flash("Permission denied", 'danger') return redirect(url_for("index")) form = NewDeploymentForm() if form.validate_on_submit(): deployment_date = dateparse(form.deployment_date.data) delayed_mode = form.delayed_mode.data deployment_name = form.glider_name.data + '-' + \ deployment_date.strftime('%Y%m%dT%H%M') if delayed_mode: deployment_name += '-delayed' deployment = db.Deployment() # form.populate_obj(deployment) deployment.user_id = user._id deployment.username = username deployment.deployment_dir = os.path.join(username, deployment_name) deployment.updated = datetime.utcnow() deployment.deployment_date = deployment_date deployment.glider_name = form.glider_name.data deployment.name = deployment_name deployment.attribution = form.attribution.data deployment.delayed_mode = delayed_mode try: existing_deployment = db.Deployment.find_one( {'name': deployment_name}) if existing_deployment is not None: raise DuplicateKeyError("Duplicate Key Detected: name") existing_deployment = db.Deployment.find_one({ 'glider_name': form.glider_name.data, 'deployment_date': deployment_date }) if existing_deployment is not None: raise DuplicateKeyError( "Duplicate Key Detected: glider_name and deployment_date") deployment.save() flash("Deployment created", 'success') send_registration_email(username, deployment) except DuplicateKeyError: flash( "Deployment names must be unique across Glider DAC: %s already used" % deployment.name, 'danger') else: error_str = ", ".join( ["%s: %s" % (k, ", ".join(v)) for k, v in form.errors.iteritems()]) flash("Deployment could not be created: %s" % error_str, 'danger') return redirect(url_for('list_user_deployments', username=username))
def new_delayed_mode_deployment(username, deployment_id): ''' Endpoint for submitting a delayed mode deployment from an existing realtime deployment :param string username: Username :param ObjectId deployment_id: Id of the existing realtime deployment ''' user = db.User.find_one({'username': username}) if user is None or (user is not None and not current_user.is_admin() and current_user != user): # No permission flash("Permission denied", 'danger') return redirect(url_for("index")) rt_deployment = db.Deployment.find_one({'_id': deployment_id}) # Need to check if the "real time" deployment is complete yet if not rt_deployment.completed: deployment_url = url_for('show_deployment', username=username, deployment_id=deployment_id) flash( Markup( 'The real time deployment <a href="%s">%s</a> must be marked as complete before adding delayed mode data' % (deployment_url, rt_deployment.name)), 'danger') return redirect(url_for('list_user_deployments', username=username)) deployment = db.Deployment() deployment_name = rt_deployment.name + '-delayed' deployment.name = deployment_name deployment.user_id = user._id deployment.username = username deployment.operator = rt_deployment.operator deployment.deployment_dir = os.path.join(username, deployment_name) deployment.wmo_id = rt_deployment.wmo_id deployment.updated = datetime.utcnow() deployment.glider_name = rt_deployment.glider_name deployment.deployment_date = rt_deployment.deployment_date deployment.attribution = rt_deployment.attribution deployment.delayed_mode = True try: existing_deployment = db.Deployment.find_one({'name': deployment_name}) if existing_deployment is not None: raise DuplicateKeyError("Duplicate Key Detected: name") deployment.save() flash("Deployment created", 'success') send_registration_email(username, deployment) except DuplicateKeyError: flash( "Deployment names must be unique across Glider DAC: %s already used" % deployment.name, 'danger') return redirect(url_for('list_user_deployments', username=username))
def main(base): with app.app_context(): for uname in users: u = db.User.find_one({'username': uname}) assert u fullpath = os.path.join(base, uname, 'upload') for f in os.listdir(fullpath): if not os.path.isdir(os.path.join(fullpath, f)): continue print "Deployment", f deployment = db.Deployment.find_one({'name': f}) if deployment: print "Found: updating timestamp" deployment.updated = datetime.utcfromtimestamp( os.path.getmtime(os.path.join(fullpath, f))) deployment.save() else: print "Not Found: creating" deployment = db.Deployment() deployment.name = unicode(f) deployment.user_id = u._id deployment.deployment_dir = unicode( os.path.join(fullpath, f)) deployment.completed = os.path.exists( os.path.join(fullpath, f, 'completed.txt')) wmoid_file = os.path.join(fullpath, f, 'wmoid.txt') if os.path.exists(wmoid_file): with open(wmoid_file) as wf: deployment.wmo_id = unicode(wf.readline().strip()) deployment.save()
def new_deployment(username): user = db.User.find_one({'username': username}) if user is None or (user is not None and not current_user.is_admin and current_user != user): # No permission flash("Permission denied", 'danger') return redirect(url_for("index")) form = NewDeploymentForm() if form.validate_on_submit(): deployment_date = dateparse(form.deployment_date.data) delayed_mode = form.delayed_mode.data deployment_name = form.glider_name.data + '-' + \ deployment_date.strftime('%Y%m%dT%H%M') if delayed_mode: deployment_name += '-delayed' try: existing_deployment = db.Deployment.find_one( {'name': deployment_name}) if existing_deployment is not None: raise DuplicateKeyError("Duplicate Key Detected: name") existing_deployment = db.Deployment.find_one( {'glider_name': form.glider_name.data, 'deployment_date': deployment_date}) if existing_deployment is not None: # if there is a previous real-time deployment and the one # to create is marked as delayed mode, act as if the delayed # mode modification path had been followed if not existing_deployment.delayed_mode and delayed_mode: return new_delayed_mode_deployment(username, existing_deployment._id) # same combination of glider_name/date/delayed_or_rt_mode should # have been caught by this point by the unique deployment name. # If we reach this, point, the deployment should either not exist # yet, or delayed_mode on the existing deployment should be true and the # new one should be a real-time deployment, so just continue making # the deployment normally. deployment = db.Deployment() deployment.user_id = user._id deployment.username = username deployment.deployment_dir = os.path.join(username, deployment_name) deployment.updated = datetime.utcnow() deployment.deployment_date = deployment_date deployment.glider_name = form.glider_name.data deployment.name = deployment_name deployment.attribution = form.attribution.data deployment.delayed_mode = delayed_mode deployment.save() flash("Deployment created", 'success') send_registration_email(username, deployment) except DuplicateKeyError: flash("Deployment names must be unique across Glider DAC: %s already used" % deployment_name, 'danger') else: error_str = ", ".join(["%s: %s" % (k, ", ".join(v)) for k, v in form.errors.items()]) flash("Deployment could not be created: %s" % error_str, 'danger') return redirect(url_for('list_user_deployments', username=username))