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.aaf.Defaults;
28 import org.onap.aaf.cadi.config.Config;
29 import org.onap.aaf.cadi.util.Chmod;
30 import org.onap.aaf.misc.env.Trans;
31 import org.onap.aaf.misc.env.util.Chrono;
32 import org.onap.aaf.misc.env.util.Split;
34 import certman.v1_0.Artifacts.Artifact;
35 import certman.v1_0.CertInfo;
37 public class PlaceArtifactScripts extends ArtifactDir {
39 public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
41 // Setup check.sh script
42 String filename = arti.getNs()+".check.sh";
43 File f1 = new File(dir,filename);
44 String email = arti.getNotification() + '\n';
45 if (email.startsWith("mailto:")) {
46 email=email.substring(7);
48 email=arti.getOsUser() + '\n';
51 StringBuilder classpath = new StringBuilder();
53 for (String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {
57 classpath.append(File.pathSeparatorChar);
59 File f = new File(pth);
60 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+",Defaults.AAF_VERSION+".*"));
64 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
65 "# Certificate Manager Check Script\n",
66 "# Check on Certificate, and renew if needed.\n",
67 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
68 "# by Deployer " + trans.getProperty(Config.AAF_APPID,"") + '\n',
70 "DIR="+arti.getDir()+'\n',
71 "APP_ID=" + arti.getMechid() + '\n',
72 "FQDN=" + arti.getMachine()+ '\n',
73 "APP="+arti.getNs()+'\n',
75 "JAR=\""+classpath.toString()+"\"\n",
76 "if [ -z \"$JAVA_HOME\" ]; then \n",
77 " JAVA=\""+javaHome() + "/bin/" +"java\"\n",
79 " JAVA=\"$JAVA_HOME/bin/java\"\n",
84 // Setup check.sh script
85 File f2 = new File(dir,arti.getNs()+".crontab.sh");
87 "#!/bin/bash " + f2.getCanonicalPath()+'\n',
88 "# Certificate Manager Crontab Loading Script\n",
89 "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
90 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
91 "TFILE=\"/tmp/cmcron$$.temp\"\n",
92 "DIR=\""+arti.getDir()+"\"\n",
93 "CF=\""+arti.getNs()+" Certificate Check Script\"\n",
94 "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
98 } catch (Exception e) {
99 throw new CadiException(e);
105 * Note: java.home gets Absolute Path of Java, where we probably want soft links from
109 private final static String javaHome() {
110 String rc = System.getenv("JAVA_HOME");
111 return rc==null?System.getProperty("java.home"):rc;
113 private final static String checkScript =
114 "function mailit {\n" +
115 " if [ -e /bin/mail ]; then\n" +
116 " MAILER=/bin/mail\n" +
117 " elif [ -e /usr/bin/mail ]; then \n" +
118 " MAILER=/usr/bin/mail\n" +
122 " if [ \"$MAILER\" = \"\" ]; then\n" +
125 " printf \"$*\" | $MAILER -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
128 "> $DIR/$APP.msg\n\n" +
129 "$JAVA -jar $JAR check $APP_ID $FQDN cadi_prop_files=$DIR/$APP.props 2> $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
132 " # Note: Validation will be mailed only the first day after any modification\n" +
133 " if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
134 " mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
136 " cat $DIR/$APP.msg\n" +
139 " 1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
141 " 2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
143 " 10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
144 " if [ -e $DIR/$APP.restart.sh ]; then\n" +
145 " # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
146 " /bin/sh $DIR/$APP.restart.sh\n" +
149 " *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
152 " # Note: make sure to cover this sripts' exit Code\n";
154 private final static String cronScript =
155 "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
156 "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
157 "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
158 "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
159 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
160 "echo \"#### END $CF ####\" >> $TFILE\n" +