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 com.att.cadi.cm;
\r
25 import java.io.File;
\r
27 import com.att.cadi.CadiException;
\r
28 import com.att.cadi.util.Chmod;
\r
29 import com.att.inno.env.Trans;
\r
30 import com.att.inno.env.util.Chrono;
\r
32 import certman.v1_0.Artifacts.Artifact;
\r
33 import certman.v1_0.CertInfo;
\r
35 public class PlaceArtifactScripts extends ArtifactDir {
\r
37 public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
\r
39 // Setup check.sh script
\r
40 String filename = arti.getAppName()+".check.sh";
\r
41 File f1 = new File(dir,filename);
\r
42 String email = arti.getNotification() + '\n';
\r
43 if(email.startsWith("mailto:")) {
\r
44 email=email.substring(7);
\r
46 email=arti.getOsUser() + '\n';
\r
48 write(f1,Chmod.to644,
\r
49 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
\r
50 "# Certificate Manager Check Script\n",
\r
51 "# Check on Certificate, and renew if needed.\n",
\r
52 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
\r
53 "DIR="+arti.getDir()+'\n',
\r
54 "APP="+arti.getAppName()+'\n',
\r
59 // Setup check.sh script
\r
60 File f2 = new File(dir,arti.getAppName()+".crontab.sh");
\r
61 write(f2,Chmod.to644,
\r
62 "#!/bin/bash " + f1.getCanonicalPath()+'\n',
\r
63 "# Certificate Manager Crontab Loading Script\n",
\r
64 "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
\r
65 "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
\r
66 "TFILE=\"/tmp/cmcron$$.temp\"\n",
\r
67 "DIR=\""+arti.getDir()+"\"\n",
\r
68 "CF=\""+arti.getAppName()+" Certificate Check Script\"\n",
\r
69 "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
\r
73 } catch (Exception e) {
\r
74 throw new CadiException(e);
\r
79 private final static String checkScript =
\r
80 "> $DIR/$APP.msg\n\n" +
\r
81 "function mailit {\n" +
\r
82 " printf \"$*\" | /bin/mail -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
\r
84 System.getProperty("java.home") + "/bin/" +"java -jar " +
\r
85 System.getProperty("java.class.path") +
\r
86 " cadi_prop_files=$DIR/$APP.props check 2> $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
\r
87 "case \"$?\" in\n" +
\r
89 " # Note: Validation will be mailed only the first day after any modification\n" +
\r
90 " if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
\r
91 " mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
\r
93 " cat $DIR/$APP.msg\n" +
\r
96 " 1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
\r
98 " 2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
100 " 10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
101 " if [ -e $DIR/$APP.restart.sh ]; then\n" +
\r
102 " # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
\r
103 " /bin/sh $DIR/$APP.restart.sh\n" +
\r
106 " *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
\r
109 " # Note: make sure to cover this sripts' exit Code\n";
\r
111 private final static String cronScript =
\r
112 "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
\r
113 "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
\r
114 "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
\r
115 "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
\r
116 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
\r
117 "echo \"#### END $CF ####\" >> $TFILE\n" +
\r
118 "crontab $TFILE\n" +
\r