Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1cba1f105e81eac4df9147b6c14fbd95b93d2d11
[hpcvm.git] / src / and / hpcvm / VirtualMachine.java
1 package and.hpcvm ;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7
8 public class VirtualMachine 
9 {
10         private String name ;
11         private String ip ;
12         private String initial_archive_name ;
13         private String directory ;
14         private String vmx_name ;
15         private String vmx_name_normal ;
16         private String vmx_name_crash ;
17         private String save_current ;
18         private String save_last ;
19         private int no_save ;
20         private int no_save_last ;
21         private Status status ;
22         private int computation_id ;
23         private String working_directory ;
24         private ArrayList<ServicesClient> save_neighbors ;
25         private String clientInVM ;
26         private String vm_user ;
27         private String vm_user_passwd ;
28         
29         public VirtualMachine()
30         {
31                 name = "VmTest" ;
32                 ip = "127.0.0.1" ;
33                 initial_archive_name = "VmTest.tgz" ;
34                 directory = "VmTest" ;
35                 vmx_name = "VmTest.vmx" ;
36                 vmx_name_normal = "VmTest.vmx.normal" ;
37                 vmx_name_crash = "VmTest.vmx.crash" ;
38                 save_current = "VmTest.tgz" ;
39                 save_last = "VmTest.tgz" ;
40                 working_directory = "/localhome/vmware" ;
41                 no_save = 0 ;
42                 no_save_last = 0 ;
43                 status = new Status() ;
44                 status.setStatus( "stopped" ) ;
45                 computation_id  = -1 ;
46                 save_neighbors = new ArrayList<ServicesClient>() ;
47                 clientInVM = "/home/mpi/InGuest" ;
48                 vm_user = "mpi" ;
49                 vm_user_passwd = "mpi" ;
50         }
51         
52         
53         public ArrayList<ServicesClient> getSaveNeighbors() { return save_neighbors ; }
54         
55         @SuppressWarnings("unchecked")
56         public void setSaveNeighbors( ArrayList<ServicesClient> _sn )
57         {
58                 if( _sn != null ) 
59                 {
60                         save_neighbors = (ArrayList<ServicesClient>) _sn.clone() ;
61                 }
62         }
63         
64         public void addSaveNeighbor( ServicesClient _sn )
65         {
66                 if( _sn != null )
67                 {
68                         save_neighbors.add( _sn ) ;
69                 }
70         }
71         
72         
73         public String getVmUser()
74         {
75                 return vm_user ;
76         }
77         
78         
79         public void setVmUser( String _user )
80         {
81                 vm_user = _user ;
82         }
83         
84         
85         public String getVmUserPasswd()
86         {
87                 return vm_user_passwd ;
88         }
89         
90         
91         public void setVmUserPasswd( String _user_passwd )
92         {
93                 vm_user_passwd = _user_passwd ;
94         }
95         
96         
97         public String getClientInVM()
98         {
99                 return clientInVM ;
100         }
101         
102         
103         public void setClientInVM( String _civ )
104         {
105                 clientInVM = _civ ;
106         }
107         
108         
109         public String getStatus()
110         {
111                 return status.getStatus() ;
112         }
113         
114         
115         public void setStatus( String _status )
116         {
117                 status.setStatus( _status ) ;
118         }
119         
120         
121         public String getWorkingDirectory()
122         {
123                 return working_directory ;
124         }
125         
126         
127         public void setWorkingDirectory( String _wd )
128         {
129                 working_directory = _wd ;
130         }
131
132         public String getName() {
133                 return name;
134         }
135
136         
137         public void setName(String name) {
138                 this.name = name;
139         }
140
141         public String getIp() {
142                 return ip;
143         }
144
145         public void setIp(String ip) {
146                 this.ip = ip;
147         }
148
149         public String getInitial_archive_name() {
150                 return initial_archive_name;
151         }
152
153         public void setInitial_archive_name(String initial_archive_name) {
154                 this.initial_archive_name = initial_archive_name;
155         }
156
157         public String getDirectory() {
158                 return directory;
159         }
160
161         public void setDirectory(String directory) {
162                 this.directory = directory;
163         }
164
165         public String getVmx_name() {
166                 return vmx_name;
167         }
168
169         public void setVmx_name(String vmx_name) {
170                 this.vmx_name = vmx_name;
171         }
172
173         public String getVmx_name_normal() {
174                 return vmx_name_normal;
175         }
176
177         public void setVmx_name_normal(String vmx_name_normal) {
178                 this.vmx_name_normal = vmx_name_normal;
179         }
180
181         public String getVmx_name_crash() {
182                 return vmx_name_crash;
183         }
184
185         public void setVmx_name_crash(String vmx_name_crash) {
186                 this.vmx_name_crash = vmx_name_crash;
187         }
188
189         public String getSave_current() {
190                 return save_current;
191         }
192
193         public void setSave_current(String save_current) {
194                 this.save_current = save_current;
195         }
196
197         public String getSave_last() {
198                 return save_last;
199         }
200
201         public void setSave_last(String save_last) {
202                 System.out.println( "Save name: " + save_last ) ;
203                 this.save_last = save_last;
204         }
205
206         public int getNo_save() 
207         {
208                 return no_save ;
209         }
210
211         public void setNo_save( int no_save ) 
212         {
213                 this.no_save = no_save ;
214         }
215
216         public int getNo_save_last() 
217         {
218                 return no_save_last ;
219         }
220
221         public void setNo_save_last( int no_save_last ) 
222         {
223                 this.no_save_last = no_save_last ;
224         }
225         
226         public int getComputationId() { return computation_id ; }
227         
228         
229         public void setComputationId( int _id )
230         {
231                 computation_id = _id ;
232         }
233
234         
235         public int deployLastSave()
236         {
237                 System.out.print( "Removing current VM ... " ) ;
238                 
239                 String[] command = new String[] { "/bin/rm", "-rf", 
240                                 working_directory + "/" + directory } ;
241                 Process pr = null ;
242                 try {
243                         pr = Runtime.getRuntime().exec( command ) ;
244 //                      synchronized( pr ){
245                         pr.waitFor() ; //;}
246                 } catch( IOException e ) {
247                         System.err.println( "Error while removing current VM!" ) ;
248                         e.printStackTrace() ;
249                         return 1 ;
250                 } catch( InterruptedException e ) {
251                         e.printStackTrace() ;
252                         return 1 ;
253                 }
254                 
255                 if( pr.exitValue() == 0 )
256                 {
257                         System.out.println( "Successful deletion of current VM." ) ;
258                 } else {
259                         System.err.println( "Error: " + pr.exitValue() ) ;
260                         BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ;
261                         
262                         String l ;
263                         try {
264                                 while( (l = b.readLine()) != null )
265                                 {
266                                         System.err.println( l ) ;
267                                 }
268                         } catch( IOException e ) {
269                                         e.printStackTrace() ;
270                         }
271                 }
272                 
273                 System.out.print( "Deploying the last save ... " ) ;
274                 
275                 command = new String[] { "/bin/tar", "-xzf", 
276                                 working_directory + "/" + save_last,
277                                 "-C", working_directory } ;
278                 pr = null ;
279                 try {
280                         pr = Runtime.getRuntime().exec( command ) ;
281 //                      synchronized( pr ){
282                         pr.waitFor() ; //;}
283                 } catch( IOException e ) {
284                         System.err.println( "Error while deploying the last secure save!" ) ;
285                         e.printStackTrace() ;
286                         return 1 ;
287                 } catch( InterruptedException e ) {
288                         e.printStackTrace() ;
289                         return 1 ;
290                 }
291                         
292                 
293                 if( pr.exitValue() == 0 )
294                 {
295                         System.out.println( "Successful extraction of the save archive." ) ;
296                         return 0 ;
297                 } else {
298                         System.err.println( "Error: " + pr.exitValue() ) ;
299                         BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ;
300                         
301                         String l ;
302                         try {
303                                 while( (l = b.readLine()) != null )
304                                 {
305                                         System.err.println( l ) ;
306                                 }
307                         } catch( IOException e ) {
308                                         e.printStackTrace() ;
309                         }
310                 }
311                 
312                 return 1 ;
313         }
314
315         
316 }
317
318 /** La programmation est un art, respectons ceux qui la pratiquent !! **/