714b23357c5c7d96127b392b9b79c764d2c25b0c
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / cm / PlaceArtifactScripts.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.cm;\r
24 \r
25 import java.io.File;\r
26 \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
31 import com.att.inno.env.util.Split;\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                         \r
50                         StringBuilder classpath = new StringBuilder();\r
51                         boolean first = true;\r
52                         for(String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {\r
53                                 if(first) {\r
54                                         first=false;\r
55                                 } else {\r
56                                         classpath.append(File.pathSeparatorChar);\r
57                                 }\r
58                                 File f = new File(pth);\r
59                                 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+","*"));\r
60                         }\r
61                         \r
62                         write(f1,Chmod.to644,\r
63                                         "#!/bin/bash " + f1.getCanonicalPath()+'\n',\r
64                                         "# Certificate Manager Check Script\n",\r
65                                         "# Check on Certificate, and renew if needed.\n",\r
66                                         "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',\r
67                                         "DIR="+arti.getDir()+'\n',\r
68                                         "APP="+arti.getAppName()+'\n',\r
69                                         "EMAIL="+email,\r
70                                         "CP=\""+classpath.toString()+"\"\n",\r
71                                         checkScript\r
72                                         );\r
73                         \r
74                         // Setup check.sh script\r
75                         File f2 = new File(dir,arti.getAppName()+".crontab.sh");\r
76                         write(f2,Chmod.to644,\r
77                                         "#!/bin/bash " + f1.getCanonicalPath()+'\n',\r
78                                         "# Certificate Manager Crontab Loading Script\n",\r
79                                         "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",\r
80                                         "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',\r
81                                         "TFILE=\"/tmp/cmcron$$.temp\"\n",\r
82                                         "DIR=\""+arti.getDir()+"\"\n",\r
83                                         "CF=\""+arti.getAppName()+" Certificate Check Script\"\n",\r
84                                         "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",\r
85                                         cronScript\r
86                                         );\r
87 \r
88                 } catch (Exception e) {\r
89                         throw new CadiException(e);\r
90                 }\r
91                 return true;\r
92         }\r
93         \r
94         private final static String checkScript = \r
95                         "> $DIR/$APP.msg\n\n" +\r
96                         "function mailit {\n" +\r
97                         "  printf \"$*\" | /bin/mail -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+\r
98                         "}\n\n" +\r
99                         System.getProperty("java.home") + "/bin/" +"java -cp $CP " +\r
100                                 CmAgent.class.getName() + \r
101                                 " cadi_prop_files=$DIR/$APP.props check 2>  $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +\r
102                         "case \"$?\" in\n" +\r
103                         "  0)\n" +\r
104                         "    # Note: Validation will be mailed only the first day after any modification\n" +\r
105                         "    if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +\r
106                         "       mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +\r
107                         "    else\n" +\r
108                         "       cat $DIR/$APP.msg\n" +\r
109                         "    fi\n" +\r
110                         "    ;;\n" +\r
111                         "  1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +\r
112                         "    ;;\n" +\r
113                         "  2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +\r
114                         "    ;;\n" +\r
115                         "  10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +\r
116                         "      if [ -e $DIR/$APP.restart.sh ]; then\n" +\r
117                         "        # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +\r
118                         "        /bin/sh $DIR/$APP.restart.sh\n" +\r
119                         "      fi\n" +\r
120                         "    ;;\n" +\r
121                         "  *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +\r
122                         "    ;;\n" +\r
123                         " esac\n\n" +\r
124                         " # Note: make sure to cover this sripts' exit Code\n";\r
125         \r
126         private final static String cronScript = \r
127                         "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +\r
128                         "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +\r
129                         "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +\r
130                         "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +\r
131                                 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +\r
132                         "echo \"#### END $CF ####\" >> $TFILE\n" +\r
133                         "crontab $TFILE\n" +\r
134                         "rm $TFILE\n";\r
135 }\r
136 \r
137 \r
138 \r