__init__.py 12.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# 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)
        
43
44
        
        
45
    def process(self):
46
47
48
49
50
51
52
53
54
55
56
        from datetime import datetime
        
        #We open a log file
        f = open("/work/ng6-test/jflow_rtherville/ng6_python.log", "a")
        #f = open(os.path.join(os.path.dirname(__file__), '../../ng6_python.log'), "a")
        
        now = datetime.now()
        datetime = now.strftime("%d/%m/%Y %H:%M:%S")
        
        logging.getLogger("SwitchProjectSpaceId.process").debug(datetime + " Begin SwitchProjectSpaceId workflow.")
        f.write("\n\n" + datetime + " Begin SwitchProjectSpaceId workflow.\n")
57
58
59
        self.project_id = int(self.project_id)
        
        if self.project_id and self.space_id :
60
            logging.getLogger("SwitchProjectSpaceId.process").debug("project_id =" + str(self.project_id)  + " , new space_id = " + str(self.space_id) )
61
62
            f.write("project_id =" + str(self.project_id)  + " , new space_id = " + str(self.space_id) +"\n")
            
63
64
            project = Project.get_from_id(self.project_id)
            old_space_id = project.space_id
65
            
66
            #We log a message if the project already has the given space_id
Romain Therville's avatar
Romain Therville committed
67
            if str(old_space_id) == str(self.space_id) :
68
69
                logging.getLogger("SwitchProjectSpaceId.process").debug("The project " + project.name + " already belongs to the space_id '" + str(self.space_id) + "'\n" )
                f.write("The project " + project.name + " already belongs to the space_id '" + str(self.space_id) + "'\n" )
Romain Therville's avatar
Romain Therville committed
70
            
71
72
73
            #Retrieve the old and new directories
            ng6conf = NG6ConfigReader()
            old_directory = ng6conf.get_space_directory(old_space_id)
74
75
76
77
            try:
                new_directory = ng6conf.get_space_directory(self.space_id)
            except Exception as err:
                logging.getLogger("SwitchProjectSpaceId.process").error("Error while searching space_id '" + str(self.space_id) + "' in application.properties, it certainly doesn't exist. \n" + str(err))
78
79
                f.write("Error while searching space_id '" + str(self.space_id) + "' in application.properties, it certainly doesn't exist. \n" + str(err) + "\n")
                raise Exception("Error while searching space_id '" + str(self.space_id) + "' in application.properties, it certainly doesn't exist. \n" + str(err) + "\n")
80
81
82
83
            
            base_path = ng6conf.get_save_directory()
            
            #Process for the project's runs
84
            runs_ids = project.get_project_run_ids()
85
            for run_id in runs_ids :
86
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating run " + str(run_id) )
87
88
                f.write("Migrating run " + str(run_id)  + "\n")
                
89
90
91
                run = Run.get_from_id(run_id)
                
                path_old_run_directory = base_path + run.get_directory()
92
93
94
                path_new_run_directory = path_old_run_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
95
                new_run_directory = run.get_directory()
96
97
                new_run_directory = new_run_directory.replace(old_directory, new_directory)
                
98
99
                logging.getLogger("SwitchProjectSpaceId.process").debug("Beginning run files migration, from " + str(path_old_run_directory) + " to " + str(path_new_run_directory) +"\n" )
                f.write("Beginning run files migration, from " + str(path_old_run_directory) + " to " + str(path_new_run_directory) +"\n" )
100
101
102
                try:
                    shutil.move(path_old_run_directory, path_new_run_directory)
                except Exception as err:
103
104
                    logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err) + "\n")
                    f.write("Error while moving " + path_old_run_directory + " to " + path_new_run_directory + " \n" + str(err) + "\n")
105
106
107
108
                    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) :
109
110
111
112
113
114
115
                    try:
                        os.rmdir(path_old_run_directory)
                    except Exception as err:
                        logging.getLogger("SwitchProjectSpaceId.process").error("Error while deleting " + path_old_run_directory + "\n" + str(err) + "\n")
                        f.write("Error while deleting " + path_old_run_directory + "\n" + str(err) + "\n")
                        raise Exception("Error while deleting " + path_old_run_directory + "\n" + str(err) + "\n")
                        
116
                #Update the retention in DB
117
118
                old_retention = run.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
119
                run.set_retention(new_retention)
120
                run.set_directory(new_run_directory)
121
122
                
            #Process for the project's analyzes
123
            analyzes_ids = project.get_project_analyzes_ids()
124
            for analysis_id in analyzes_ids :
125
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) )
126
127
                f.write("Migrating analysis " + str(analysis_id)  + "\n")
                
128
129
130
                analysis = Analysis.get_from_id(analysis_id)
                
                path_old_analysis_directory = base_path + analysis.get_directory()
131
132
133
                path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
134
                new_analysis_directory = analysis.get_directory()
135
136
                new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
                
137
138
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) + "\n" )
                f.write( "Beginning analysis files migration, from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) + "\n" )
139
140
141
                try:
                    shutil.move(path_old_analysis_directory, path_new_analysis_directory)
                except Exception as err:
142
143
144
                    logging.getLogger("SwitchProjectSpaceId.process").error("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err) + "\n")
                    f.write("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + "\n" + str(err) + "\n")
                    raise Exception("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + " \n" + str(err) + "\n")
145
146
147
                
                #Delete the old directory if empty
                if os.path.isdir(path_old_analysis_directory) and not os.listdir(path_old_analysis_directory) :
148
149
150
151
152
153
                    try:
                        os.rmdir(path_old_analysis_directory)
                    except Exception as err:
                        logging.getLogger("SwitchProjectSpaceId.process").error("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
                        f.write("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
                        raise Exception("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
154
155
                
                #Update the retention in DB
156
157
158
                old_retention = analysis.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
                analysis.set_retention(new_retention)
159
                analysis.set_directory(new_analysis_directory)
160
161
                
            #Process for the project's run's analyzes
162
            run_analyzes_ids = project.get_project_runs_analyzes_ids()
163
            for analysis_id in run_analyzes_ids :
164
165
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis " + str(analysis_id) + "\n")
                f.write("Migrating analysis " + str(analysis_id)  + "\n")
166
167
168
                analysis = Analysis.get_from_id(analysis_id)
                
                path_old_analysis_directory = base_path + analysis.get_directory()
169
170
171
                path_new_analysis_directory = path_old_analysis_directory.replace(old_directory,new_directory)
                
                #Update the directory in DB
172
                new_analysis_directory = analysis.get_directory()
173
174
                new_analysis_directory = new_analysis_directory.replace(old_directory, new_directory)
                
175
176
                logging.getLogger("SwitchProjectSpaceId.process").debug("Migrating analysis files from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) + "\n" )
                f.write( "Beginning analysis files migration, from " + str(path_old_analysis_directory) + " to " + str(path_new_analysis_directory) + "\n" )
177
178
179
180
                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))
181
                    f.write("Error while moving " + path_old_analysis_directory + " to " + path_new_analysis_directory + "\n" + str(err) + "\n")
182
183
184
185
                    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) :
186
187
188
189
190
191
                    try:
                        os.rmdir(path_old_analysis_directory)
                    except Exception as err:
                        logging.getLogger("SwitchProjectSpaceId.process").error("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
                        f.write("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
                        raise Exception("Error while deleting " + path_old_analysis_directory + "\n" + str(err) + "\n")
192
193
                
                #Update the retention in DB
194
195
                old_retention = analysis.get_creation_date()
                new_retention = ng6conf.compute_new_retention_date(self.space_id,old_retention)
196
197
198
                analysis.set_retention(new_retention)
                analysis.set_directory(new_analysis_directory)
            
199
200
            #We close the log file
            f.close()
201
            #Update to the projcet's space_id
202
203
            project.update_space_id(self.space_id)