mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Violette Roche-Montane
16.14.2013 4571675a785cf27b01455331d41629d0aa0c4c6d
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2007-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2012 Delta-Victor Consultants
 *      Portions Copyright 2013 ForgeRock AS
 */
package org.opends.build.tools;
 
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
 
import static org.opends.build.tools.Utilities.*;
 
import java.io.File;
import java.io.FileFilter;
import java.io.PrintWriter;
 
/**
 * Generates an RPM spec file.
 */
public class GenerateRpm extends Task
{
 
  private File topDir;
  private String topDirAbsolutePath;
  private String sourceDirName;
  private File destFile;
  private String prefix;
  private String productName;
  private String referenceURL;
  private String shortName;
  private String version;
  private String release;
  private boolean overwrite;
  private StringBuilder sb;
 
  private final String filePrefix = "%{_prefix}";
  private final String dirPrefix = "%dir %{_prefix}";
 
  /**
   * Sets the top directory for the rpm build.
   *
   * @param topDir
   *          File representing top directory for rpm build directory
   */
  public void setTopDir(File topDir)
  {
    this.topDir = topDir;
    topDirAbsolutePath = topDir.getAbsolutePath();
  }
 
  /**
   * Sets the prefix for the RPM.
   *
   * @param prefix
   *          Used for package relocation
   */
  public void setPrefix(String prefix)
  {
    this.prefix = prefix;
  }
 
  /**
   * Sets the short description for the RPM.
   *
   * @param productName
   *          The RPM short description
   */
  public void setproductName(String productName)
  {
    this.productName = productName;
  }
 
  /**
   * Sets the application name for the RPM.
   *
   * @param shortName
   *          The RPM application name
   */
  public void setshortName(String shortName)
  {
    this.shortName = shortName;
  }
 
  /**
   * Sets the reference URL for the RPM.
   *
   * @param referenceURL
   *          The RPM reference URL
   */
  public void setreferenceURL(String referenceURL)
  {
    this.referenceURL = referenceURL;
  }
 
  /**
   * Sets the name of the source directory.
   *
   * @param sourceDirName
   *          name of the source directory.
   */
  public void setSourceDirName(String sourceDirName)
  {
    this.sourceDirName = sourceDirName;
  }
 
  /**
   * Sets the RPM spec file that will be generated.
   *
   * @param dest
   *          The spec file
   */
  public void setSpecFileName(File dest)
  {
    this.destFile = dest;
  }
 
  /**
   * Sets the version number.
   *
   * @param version
   *          The version number
   */
  public void setVersion(String version)
  {
    this.version = version;
  }
 
  /**
   * Sets the release number.
   *
   * @param release
   *          The release number
   */
  public void setRelease(String release)
  {
    this.release = release;
  }
 
  /**
   * Indicates when true that an existing destination file will be overwritten.
   *
   * @param o
   *          boolean where true means overwrite
   */
  public void setOverwrite(boolean o)
  {
    this.overwrite = o;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public void execute() throws BuildException
  {
 
    try
    {
      if (!topDir.exists())
      {
        throw new BuildException("directory " + topDir.getName()
            + " does not exist");
      }
      if (!topDir.isDirectory())
      {
        throw new BuildException(topDir.getName() + " is not a directory");
      }
 
      if (destFile.exists())
      {
        if (this.overwrite)
        {
          destFile.delete();
          log("Regenerating " + destFile.getName() + " from "
              + topDir.getName());
        }
        else
        {
          log(destFile.getName() + " has not been regenerated");
        }
      }
 
      sb = new StringBuilder();
      final File rootDir = new File(sourceDirName);
      final String opendsDir = rootDir.getName();
      final File[] listFiles = rootDir.listFiles(new PkgFileFilter());
 
      // Generate the package information
      sb.append("%define _topdir " + topDirAbsolutePath + EOL);
      sb.append("%define _arch noarch" + EOL);
      sb.append("%define _prefix " + prefix + EOL);
      sb.append("%define _pre " + opendsDir + EOL);
      sb.append("%define __os_install_post %{nil}" + EOL);
      sb.append(EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Header" + EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Short Description" + EOL);
      sb.append("Summary: " + productName + EOL);
      sb.append("# Application Name" + EOL);
      sb.append("Name: " + shortName + EOL);
      sb.append("# Application Version" + EOL);
      sb.append("Version: " + version + EOL);
      sb.append("# Packaging Revision" + EOL);
      sb.append("Release: " + release + EOL);
      sb.append("# Software Licenced Under" + EOL);
      sb.append("License: CDDL" + EOL);
      sb.append("# RPM Group" + EOL);
      sb.append("Group: Applications/Network" + EOL);
      sb.append("# Link to Application web site" + EOL);
      sb.append("URL: " + referenceURL + EOL);
      sb.append("# Distributing Organisation" + EOL);
      sb.append("Vendor: ForgeRock AS" + EOL);
      sb.append("# Build Architecture" + EOL);
      sb.append("BuildArch: noarch" + EOL);
      sb.append(EOL);
      sb.append("Requires: jre >= 1.6" + EOL);
      sb.append(EOL);
      sb.append("# Long Description" + EOL);
      sb.append("%Description" + EOL);
      sb.append("OpenDJ LDAP Server" + EOL);
      sb.append("OpenDJ is an LDAPv3 compliant directory service, developed for the Java"
          + EOL);
      sb.append("platform, providing a high performance, highly available and secure store"
          + EOL);
      sb.append("for the identities managed by enterprises. Its easy installation process,"
          + EOL);
      sb.append("combined with the power of the Java platform makes OpenDJ one of the"
          + EOL);
      sb.append("simplest and fastest directory servers to deploy and manage."
          + EOL);
      sb.append(EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Pre & Post Install" + EOL);
      sb.append("# =========================" + EOL);
      sb.append("# If the first argument to %pre is 1, the RPM operation is an initial"
          + EOL);
      sb.append("#  installation. If the argument to %pre is 2, the operation is an upgrade"
          + EOL);
      sb.append("#  from an existing version to a new one." + EOL);
      sb.append("# Similarly, the arguments to a %post are 1 and 2 for a new installation"
          + EOL);
      sb.append("#  and upgrade, respectively. (%pre and %post aren't executed during"
          + EOL);
      sb.append("#  an uninstallation.)" + EOL);
      sb.append(EOL);
      sb.append("# Pre Install" + EOL);
      sb.append("%pre" + EOL);
      sb.append("if [ \"$1\" == \"1\" ]; then" + EOL);
      sb.append("   echo \"Pre Install - initial install\"" + EOL);
      sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
      sb.append("   echo \"Pre Install - upgrade install\"" + EOL);
      sb.append("# If the server is running before upgrade, creates a file flag" + EOL);
      sb.append("           if [ -f %{_prefix}/logs/server.pid ] " + EOL);
      sb.append("           then" + EOL);
      sb.append("                   touch %{_prefix}/logs/status" + EOL);
      sb.append("           fi" + EOL);
      sb.append("       stopds=$(%{_prefix}/bin/stop-ds)" + EOL);
      sb.append("       echo $stopds" + EOL);
      sb.append("     fi" + EOL);
      sb.append("fi" + EOL);
      sb.append(EOL);
      sb.append("# Post Install" + EOL);
      sb.append("%post" + EOL);
      sb.append("if [ \"$1\" == \"1\" ] ; then" + EOL);
      sb.append("   echo \"Post Install - initial install\"" + EOL);
      sb.append("   [[ `java -version 2>&1 | /bin/sed 's/java version \"\\(.*\\)\\.\\(.*\\)\\.\\(.*\\)\\_\\(.*\\)\\.*\"/\\1\\2\\3\\4/; 1q'` < 16022 ]] && echo \"WARNING - For best server performance, use at least Java 1.6.0_22, which includes a major security fix for TLS.\""
          + EOL);
      sb.append("       echo \"\"" + EOL);
      sb.append("else if [ \"$1\" == \"2\" ] ; then" + EOL);
      sb.append("   echo \"Post Install - upgrade install\"" + EOL);
      // Starts the upgrade. The new files are automatically imported
      // by rpm manager, which compares files between last & actual version.
      // Copies / deletes files depending of new package.
      sb.append("       %{_prefix}/./upgrade -n" + EOL);
      sb.append("# Upgrade ok " + EOL);
      sb.append("         if [ \"$?\" == \"0\" ] ; then " + EOL);
      sb.append("# Checks the server status flag for restart. " + EOL);
      sb.append("             if [ -f %{_prefix}/logs/status ] " + EOL);
      sb.append("             then" + EOL);
      sb.append("                 echo \"\"" + EOL);
      sb.append("                 echo \"Restarting server...\" " + EOL);
      sb.append("                 %{_prefix}/./bin/start-ds " + EOL);
      sb.append("                 echo \"\"" + EOL);
      sb.append("                 rm -f %{_prefix}/logs/status " + EOL);
      sb.append("             fi" + EOL);
      sb.append("         fi" + EOL);
      sb.append("# Upgrade fails, needs user interaction (eg. manual mode)" + EOL);
      sb.append("         if [ \"$?\" == \"2\" ] ; then " + EOL);
      sb.append("           exit \"0\" " + EOL);
      sb.append("         fi " + EOL);
      sb.append("   fi " + EOL);
      sb.append("fi" + EOL);
      sb.append(EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Pre & Post Uninstall" + EOL);
      sb.append("# =========================" + EOL);
      sb.append("# If the first argument to %preun and %postun is 0, the action is"
          + EOL);
      sb.append("#  uninstallation." + EOL);
      sb.append("# If the first argument to %preun and %postun is 1, the action is an upgrade."
          + EOL);
      sb.append("# Pre Uninstall" + EOL);
      sb.append("%preun" + EOL);
      sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
      sb.append("   echo \"Pre Uninstall - uninstall\"" + EOL);
      sb.append("   %{_prefix}/bin/stop-ds" + EOL);
      sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
      sb.append("   echo \"Pre Uninstall - upgrade uninstall\"" + EOL);
      sb.append("   fi" + EOL);
      sb.append("fi" + EOL);
      sb.append("# Post Uninstall" + EOL);
      sb.append("%postun" + EOL);
      sb.append("if [ \"$1\" == \"0\" ] ; then" + EOL);
      sb.append("   echo \"Post Uninstall - uninstall\"" + EOL);
      sb.append("       echo \"OpenDJ successfully removed.\"" + EOL);
      sb.append("else if [ \"$1\" == \"1\" ] ; then" + EOL);
      sb.append("   echo \"Post Uninstall - upgrade uninstall\"" + EOL);
      sb.append("   fi" + EOL);
      sb.append("fi" + EOL);
      sb.append(EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Prepare, Build, Install" + EOL);
      sb.append("# =========================" + EOL);
      sb.append("# %prep" + EOL);
      sb.append(EOL);
      sb.append("# %build" + EOL);
      sb.append(EOL);
      sb.append("%install" + EOL);
      sb.append("mkdir -p $RPM_BUILD_ROOT%{_prefix}" + EOL);
      sb.append("cd $RPM_BUILD_ROOT%{_prefix}" + EOL);
      for (final File f : listFiles)
      {
        sb.append("cp -r " + f.getPath() + " ." + EOL);
      }
      sb.append(EOL);
      sb.append("# =========================" + EOL);
      sb.append("# Files Layout" + EOL);
      sb.append("# =========================" + EOL);
      sb.append("%files" + EOL);
      sb.append(dirPrefix + EOL);
      // In order to handle upgrades, we need to start
      // with the contents of the rootDir rather than
      // the actual directory, because the
      // rootDir has the OpenDJ version number included
      // into it
      for (final File build : listFiles)
      {
        generatedLevel("", build);
      }
 
      // flush the spec file.
      PrintWriter destWriter = new PrintWriter(destFile);
      destWriter.print(sb.toString());
      destWriter.close();
    }
    catch (Exception e)
    {
      // Don't leave a malformed file laying around. Delete
      // it so it will be forced to be regenerated.
      if (destFile.exists())
      {
        destFile.deleteOnExit();
      }
      e.printStackTrace();
      throw new BuildException("Error processing " + topDir + ":  "
          + e.getMessage());
    }
  }
 
  private void generatedLevel(String parent, File source)
  {
    if (source.isDirectory())
    {
      sb.append(dirPrefix + parent + "/" + source.getName());
      sb.append(EOL);
      for (File child : source.listFiles())
      {
        generatedLevel(parent + "/" + source.getName(), child);
      }
    }
    else
    {
      sb.append(filePrefix + parent + "/" + source.getName());
      sb.append(EOL);
    }
  }
 
  /**
   * A file filter for the rpm. Excludes all '.bat', '.exe' and '.app' files.
   */
  static final class PkgFileFilter implements FileFilter
  {
    /** {@inheritDoc} */
    public boolean accept(File file)
    {
      final String fileName = file.getName().toLowerCase();
      if (file.isDirectory()
          && (fileName.equals("bat") || fileName.endsWith(".app") || fileName
              .endsWith(".bat")))
      {
        return false;
      }
      else if (file.isFile())
      {
        if (fileName.endsWith(".app") || fileName.endsWith(".bat")
            || fileName.endsWith(".exe"))
        {
          return false;
        }
      }
      return true;
    }
  }
}