Commit 2e82534c authored by Romain Therville's avatar Romain Therville 🐭

New "switch_projet_space_id" workflow, update for t3MySQLdb.py.

The new workflow allows to migrate a project to a space_id defined in
application.properties.
Every run and analyse gets a new retention and new path, the directories
and files are moved, the old directories are removed.

Use:
python3.4 /save/ng6-test/src/ng6-git-rtherville/bin/ng6_cli.py
switchprojectspaceid --project-id 1292 --space-id default --admin-login
CTD

issue #129
parent 00b365f2
......@@ -1031,3 +1031,64 @@ class t3MySQLdb(object):
self.update_fields('tx_nG6_purge_demand',demand_ids,
["processed_date","demand_state"],
[date,"deleted"])
def set_run_retention_from_duration(self, run_id, duration):
"""
Updates a run's retention from a given duration (in months)
@param run_id : the run id
@param duration : the duration (in months)
"""
sql = "UPDATE tx_nG6_run SET retention_date = UNIX_TIMESTAMP(TIMESTAMPADD(MONTH,%s,FROM_UNIXTIME(crdate))) WHERE uid= '%s' "% ( duration, run_id )
self.execute(sql, commit = True )
def set_analysis_retention_from_duration(self, analysis_id, duration):
"""
Updates a run's retention from a given duration (in months)
@param analysis_id : the analysis id
@param duration : the duration (in months)
"""
sql = "UPDATE tx_nG6_analyze SET retention_date = UNIX_TIMESTAMP(TIMESTAMPADD(MONTH,%s,FROM_UNIXTIME(crdate))) WHERE uid= '%s' "% ( duration, analysis_id )
self.execute(sql, commit = True )
def set_run_retention_from_date(self, run_id, date):
"""
Updates a run's retention from a given date
@param run_id : the run id
@param date : the date (format dd/mm/yyyy)
"""
sql = "UPDATE tx_nG6_run SET retention_date = UNIX_TIMESTAMP( str_to_date('%s','%d/%m/%Y')) WHERE uid= '%s' "% ( date, run_id )
self.execute(sql, commit = True )
def set_analysis_retention_from_date(self, analysis_id, date):
"""
Updates a run's retention from a given date
@param analysis_id : the analysis id
@param date : the date (format dd/mm/yyyy)
"""
sql = "UPDATE tx_nG6_analyze SET retention_date = UNIX_TIMESTAMP( str_to_date('%s','%d/%m/%Y')) WHERE uid= '%s' "% ( date, analysis_id )
self.execute(sql, commit = True )
def set_run_retention_from_epoch_timestamp(self, run_id, date):
"""
Updates a run's retention from a given duration (in months)
@param run_id : the run id
@param duration : the duration (in months)
"""
sql = "UPDATE tx_nG6_run SET retention_date = '%s' WHERE uid= '%s' "% ( date, run_id )
self.execute(sql, commit = True )
def set_analysis_retention_from_depoch_timestamp(self, analysis_id, duration):
"""
Updates a run's retention from a given duration (in months)
@param analysis_id : the analysis id
@param duration : the duration (in months)
"""
sql = "UPDATE tx_nG6_analyze SET retention_date = '%s' WHERE uid= '%s' "% ( duration, analysis_id )
self.execute(sql, commit = True )
def get_run_analyzes_id_from_project(self, project_id):
sql = "SELECT analyze_id FROM tx_nG6_view_project_run_analyze WHERE project_id='%s'" % project_id
qresult = self.execute(sql)
return [ r[0] for r in qresult.rows ]
\ No newline at end of file
#
# Copyright (C) 2015 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
import logging
import shutil
from ng6.ng6workflow import BasicNG6Workflow
from ng6.config_reader import NG6ConfigReader
from ng6.utils import Utils
from ng6.project import Project
from ng6.run import Run
from ng6.analysis import Analysis
from ng6.t3MySQLdb import t3MySQLdb
class SwitchProjectSpaceId (BasicNG6Workflow):
def get_description(self):
return """This workflow allows you to migrate a project from a space_id to another.
It updates the project's space_id in DB, moves the runs files, updates the runs and analyzes's files paths, and updates the corresponding retentions."""
def define_parameters(self, function="process"):
self.add_parameter('project_id', 'The project id', type=int, required = True)
self.add_parameter('space_id', 'The new space_id', required = True)
def process(self):
#self.space_id = self.space_id
#self.project_id = self.project_id
self.project_id = int(self.project_id)
if self.project_id and self.space_id :
self.project = Project.get_from_id(self.project_id)
old_space_id = self.project.space_id
#Update to the projcet's space_id
t3mysql = t3MySQLdb()
t3mysql.update_fields('tx_nG6_project', str(self.project_id), ['space_id'], [str(self.space_id)] )
#Retrieve the old and new directories
ng6conf = NG6ConfigReader()
old_directory = ng6conf.get_space_directory(old_space_id)
new_directory = ng6conf.get_space_directory(self.space_id)
new_retention = ng6conf.get_retention_date(self.space_id)
base_path = ng6conf.get_save_directory()
#Process for the project's runs
runs_ids = t3mysql.get_project_runs_ids(self.project_id)
for run_id in runs_ids :
path_old_run_directory = base_path + t3mysql.select_run_directory(run_id)
path_new_run_directory = path_old_run_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_run_directory = t3mysql.select_run_directory(run_id)
new_run_directory = new_run_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_run', str(run_id), ['directory'], [str(new_run_directory)] )
#Moving the files
if not os.path.isdir(path_new_run_directory) :
os.mkdir(path_new_run_directory)
try:
shutil.move(path_old_run_directory, path_new_run_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_run_directory) and not os.listdir(path_old_run_directory) :
os.rmdir(path_old_run_directory)
#Update the retention in DB
t3mysql.set_run_retention_from_epoch_timestamp(run_id, new_retention)
#Process for the project's analyzes
analyzes_ids = t3mysql.get_project_analysis_ids(self.project_id)
for analysis_id in analyzes_ids :
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_analysis_directory = t3mysql.select_analysis_directory(analysis_id)
new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
#Moving the files
if not os.path.isdir(path_new_run_directory) :
os.mkdir(path_new_analysis_directory)
try:
shutil.move(path_old_analysis_directory, path_new_analysis_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_analysis_directory) and not os.listdir(path_old_analysis_directory) :
os.rmdir(path_old_analysis_directory)
#Update the retention in DB
t3mysql.set_analysis_retention_from_depoch_timestamp(analysis_id, new_retention)
#Process for the project's run's analyzes
run_analyzes_ids = t3mysql.get_run_analyzes_id_from_project(self.project_id)
for analysis_id in run_analyzes_ids :
path_old_analysis_directory = base_path + t3mysql.select_analysis_directory(analysis_id)
path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
#Update the directory in DB
new_analysis_directory = t3mysql.select_analysis_directory(analysis_id)
new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
t3mysql.update_fields('tx_nG6_analyze', str(analysis_id), ['directory'], [str(new_analysis_directory)] )
#Moving the files
if not os.path.isdir(path_new_analysis_directory) :
os.mkdir(path_new_analysis_directory)
try:
shutil.move(path_old_analysis_directory, path_new_analysis_directory)
except Exception as err:
logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
raise Exception("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err))
#Delete the old directory if empty
if os.path.isdir(path_old_analysis_directory) and not os.listdir(path_old_analysis_directory) :
os.rmdir(path_old_analysis_directory)
#Update the retention in DB
t3mysql.set_analysis_retention_from_depoch_timestamp(analysis_id, new_retention)
\ No newline at end of file
#
# Copyright (C) 2015 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment