2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.configure;
26 import org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.util.Chmod;
28 import org.onap.aaf.misc.env.Trans;
29 import org.onap.aaf.misc.env.util.Chrono;
30 import org.onap.aaf.misc.env.util.Split;
32 import certman.v1_0.Artifacts.Artifact;
33 import certman.v1_0.CertInfo;
35 public class PlaceArtifactScripts extends ArtifactDir {
37 public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
39 // Setup check.sh script
40 String filename = arti.getNs()+".check.sh";
41 File f1 = new File(dir,filename);
42 String email = arti.getNotification() + '\n';
43 if (email.startsWith("mailto:")) {
44 email=email.substring(7);
46 email=arti.getOsUser() + '\n';
49 StringBuilder classpath = new StringBuilder();
51 for (String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {
55 classpath.append(File.pathSeparatorChar);
57 File f = new File(pth);
58 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+","*"));
62 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
63 "# Certificate Manager Check Script\n",
64 "# Check on Certificate, and renew if needed.\n",
65 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
66 "DIR="+arti.getDir()+'\n',
67 "APP="+arti.getNs()+'\n',
69 "CP=\""+classpath.toString()+"\"\n",
73 // Setup check.sh script
74 File f2 = new File(dir,arti.getNs()+".crontab.sh");
76 "#!/bin/bash " + f2.getCanonicalPath()+'\n',
77 "# Certificate Manager Crontab Loading Script\n",
78 "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
79 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
80 "TFILE=\"/tmp/cmcron$$.temp\"\n",
81 "DIR=\""+arti.getDir()+"\"\n",
82 "CF=\""+arti.getNs()+" Certificate Check Script\"\n",
83 "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
87 } catch (Exception e) {
88 throw new CadiException(e);
94 * Note: java.home gets Absolute Path of Java, where we probably want soft links from
98 private final static String javaHome() {
99 String rc = System.getenv("JAVA_HOME");
100 return rc==null?System.getProperty("java.home"):rc;
102 private final static String checkScript =
103 "> $DIR/$APP.msg\n\n" +
104 "function mailit {\n" +
105 " if [ -e /bin/mail ]; then\n" +
106 " MAILER=/bin/mail\n" +
107 " elif [ -e /usr/bin/mail ]; then \n" +
108 " MAILER=/usr/bin/mail\n" +
112 " if [ \"$MAILER\" = \"\" ]; then\n" +
115 " printf \"$*\" | $MAILER -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
118 javaHome() + "/bin/" +"java -cp $CP " +
119 Agent.class.getName() +
120 " cadi_prop_files=$DIR/$APP.props check 2> $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
123 " # Note: Validation will be mailed only the first day after any modification\n" +
124 " if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
125 " mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
127 " cat $DIR/$APP.msg\n" +
130 " 1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
132 " 2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
134 " 10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
135 " if [ -e $DIR/$APP.restart.sh ]; then\n" +
136 " # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
137 " /bin/sh $DIR/$APP.restart.sh\n" +
140 " *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
143 " # Note: make sure to cover this sripts' exit Code\n";
145 private final static String cronScript =
146 "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
147 "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
148 "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
149 "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
150 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
151 "echo \"#### END $CF ####\" >> $TFILE\n" +