1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.cm;
\r
25 import java.io.File;
\r
27 import org.onap.aaf.cadi.CadiException;
\r
28 import org.onap.aaf.cadi.util.Chmod;
\r
30 import org.onap.aaf.inno.env.Trans;
\r
31 import org.onap.aaf.inno.env.util.Chrono;
\r
32 import org.onap.aaf.inno.env.util.Split;
\r
34 import certman.v1_0.Artifacts.Artifact;
\r
35 import certman.v1_0.CertInfo;
\r
37 public class PlaceArtifactScripts extends ArtifactDir {
\r
39 public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
\r
41 // Setup check.sh script
\r
42 String filename = arti.getAppName()+".check.sh";
\r
43 File f1 = new File(dir,filename);
\r
44 String email = arti.getNotification() + '\n';
\r
45 if(email.startsWith("mailto:")) {
\r
46 email=email.substring(7);
\r
48 email=arti.getOsUser() + '\n';
\r
51 StringBuilder classpath = new StringBuilder();
\r
52 boolean first = true;
\r
53 for(String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {
\r
57 classpath.append(File.pathSeparatorChar);
\r
59 File f = new File(pth);
\r
60 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+","*"));
\r
63 write(f1,Chmod.to644,
\r
64 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
\r
65 "# Certificate Manager Check Script\n",
\r
66 "# Check on Certificate, and renew if needed.\n",
\r
67 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
\r
68 "DIR="+arti.getDir()+'\n',
\r
69 "APP="+arti.getAppName()+'\n',
\r
71 "CP=\""+classpath.toString()+"\"\n",
\r
75 // Setup check.sh script
\r
76 File f2 = new File(dir,arti.getAppName()+".crontab.sh");
\r
77 write(f2,Chmod.to644,
\r
78 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
\r
79 "# Certificate Manager Crontab Loading Script\n",
\r
80 "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
\r
81 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
\r
82 "TFILE=\"/tmp/cmcron$$.temp\"\n",
\r
83 "DIR=\""+arti.getDir()+"\"\n",
\r
84 "CF=\""+arti.getAppName()+" Certificate Check Script\"\n",
\r
85 "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
\r
89 } catch (Exception e) {
\r
90 throw new CadiException(e);
\r
95 private final static String checkScript =
\r
96 "> $DIR/$APP.msg\n\n" +
\r
97 "function mailit {\n" +
\r
98 " printf \"$*\" | /bin/mail -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
\r
100 System.getProperty("java.home") + "/bin/" +"java -cp $CP " +
\r
101 CmAgent.class.getName() +
\r
102 " cadi_prop_files=$DIR/$APP.props check 2> $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
\r
103 "case \"$?\" in\n" +
\r
105 " # Note: Validation will be mailed only the first day after any modification\n" +
\r
106 " if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
\r
107 " mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
\r
109 " cat $DIR/$APP.msg\n" +
\r
112 " 1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
\r
114 " 2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
116 " 10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
117 " if [ -e $DIR/$APP.restart.sh ]; then\n" +
\r
118 " # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
\r
119 " /bin/sh $DIR/$APP.restart.sh\n" +
\r
122 " *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
125 " # Note: make sure to cover this sripts' exit Code\n";
\r
127 private final static String cronScript =
\r
128 "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
\r
129 "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
\r
130 "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
\r
131 "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
\r
132 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
\r
133 "echo \"#### END $CF ####\" >> $TFILE\n" +
\r
134 "crontab $TFILE\n" +
\r