View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.runnables;
20  
21  import java.util.Iterator;
22  
23  import jgroup.experiment.PropertyDefinition;
24  import jgroup.experiment.Runnable;
25  import jgroup.experiment.ShellCommand;
26  import jgroup.relacs.config.ExperimentConfig;
27  import jgroup.relacs.config.Host;
28  import jgroup.relacs.config.HostSet;
29  
30  /**
31   * @author Hein Meling
32   */
33  public class SynchronizeCodebase
34    implements Runnable 
35  {
36  
37    ////////////////////////////////////////////////////////////////////////////////////////////
38    // Methods from Runnable
39    ////////////////////////////////////////////////////////////////////////////////////////////
40  
41    /* (non-Javadoc)
42     * @see jgroup.experiment.Runnable#run(jgroup.relacs.config.ExperimentConfig)
43     */
44    public void run(ExperimentConfig ec)
45    {
46      // get the server hosts
47      HostSet hosts = (HostSet) ec.getServerConfig().getAllHosts().clone();
48      // get the client hosts
49      HostSet clients = ec.getClientConfig().getAllHosts();
50      hosts.addHosts(clients);
51  
52      StringBuilder buf = new StringBuilder("rsync ");
53      buf.append(ec.getProperty(this, "rsync.options", "-aL --delete "));
54      String includes = ec.getProperty(this, "rsync.includes", "");
55      String[] tokens = includes.split(",");
56      for (int i = 0; i < tokens.length; i++) {
57        buf.append(" --include=");
58        buf.append(tokens[i]);
59      }
60      String excludes = ec.getProperty(this, "rsync.excludes", "");
61      tokens = excludes.split(",");
62      for (int i = 0; i < tokens.length; i++) {
63        buf.append(" --exclude=");
64        buf.append(tokens[i]);
65      }
66      buf.append(" -e ssh ");
67      String localDir = ec.getProperty(this, "local.code.dir");
68      String remoteDir = ec.getProperty(this, "remote.code.dir");
69      buf.append(localDir);
70      buf.append(" ");
71      buf.append(ec.getProperty("user.name"));
72      buf.append("@");
73  
74      ThreadGroup threadGroup = new ThreadGroup("SynchronizeCodebase");
75      for (Iterator iter = hosts.iterator(); iter.hasNext();) {
76        StringBuilder rsyncCmd = new StringBuilder(buf.toString());
77        Host host = (Host) iter.next();
78        if (host.isLocal())
79          continue;
80        String hostName = host.getCanonicalHostName();
81        rsyncCmd.append(hostName);
82        rsyncCmd.append(":");
83        rsyncCmd.append(remoteDir);
84        try {
85          ShellCommand.exec(rsyncCmd.toString(), threadGroup);
86        } catch (Exception e) {
87          e.printStackTrace();
88          System.out.println("Could not synchronize codebase to " + hostName);
89        }
90      }
91      if (ec.getProperty("bootstrap.host", "localhost").equals("localhost")) {
92        StringBuilder rsyncCmd = new StringBuilder(buf.toString());
93        rsyncCmd.append("localhost:");
94        rsyncCmd.append(remoteDir);
95        try {
96          ShellCommand.exec(rsyncCmd.toString(), threadGroup);
97        } catch (Exception e) {
98          e.printStackTrace();
99          System.out.println("Could not synchronize codebase to localhost");
100       }
101     }
102     ShellCommand.waitFor(threadGroup);  
103   }
104 
105 
106   /* (non-Javadoc)
107    * @see jgroup.experiment.Runnable#getProperties()
108    */
109   public PropertyDefinition[] getProperties() {
110     return null;
111   }
112 
113 } // END SynchronizeCodebase