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