Adjust Agent for none K8s
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / PlaceArtifactScripts.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.configure;
23
24 import java.io.File;
25
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;
33
34 import certman.v1_0.Artifacts.Artifact;
35 import certman.v1_0.CertInfo;
36
37 public class PlaceArtifactScripts extends ArtifactDir {
38     @Override
39     public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
40         try {
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);
47             }  else {
48                 email=arti.getOsUser() + '\n';
49             }
50             
51             StringBuilder classpath = new StringBuilder();
52             boolean first = true;
53             for (String pth : Split.split(File.pathSeparatorChar, System.getProperty("java.class.path"))) {
54                 if (first) {
55                     first=false;
56                 } else {
57                     classpath.append(File.pathSeparatorChar);
58                 }
59                 File f = new File(pth);
60                 classpath.append(f.getCanonicalPath().replaceAll("[0-9]+\\.[0-9]+\\.[0-9]+",Defaults.AAF_VERSION+".*"));
61             }
62             
63             write(f1,Chmod.to644,
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', 
69                     "#\n",
70                     "DIR="+arti.getDir()+'\n',
71                     "APP_ID=" + arti.getMechid() + '\n',
72                     "FQDN=" + arti.getMachine()+ '\n',
73                     "APP="+arti.getNs()+'\n',
74                     "EMAIL="+email+ '\n',
75                     "JAR=\""+classpath.toString()+"\"\n",
76                     "JAVA=\""+javaHome() + "/bin/" +"java\"\n",
77                     checkScript
78                     );
79             
80             // Setup check.sh script
81             File f2 = new File(dir,arti.getNs()+".crontab.sh");
82             write(f2,Chmod.to644,
83                     "#!/bin/bash " + f2.getCanonicalPath()+'\n',
84                     "# Certificate Manager Crontab Loading Script\n",
85                     "# Add/Update a Crontab entry, that adds a check on Certificate Manager generated Certificate nightly.\n",
86                     "# Generated by Certificate Manager " + Chrono.timeStamp()+'\n',
87                     "TFILE=\"/tmp/cmcron$$.temp\"\n",
88                     "DIR=\""+arti.getDir()+"\"\n",
89                     "CF=\""+arti.getNs()+" Certificate Check Script\"\n",
90                     "SCRIPT=\""+f1.getCanonicalPath()+"\"\n",
91                     cronScript
92                     );
93
94         } catch (Exception e) {
95             throw new CadiException(e);
96         }
97         return true;
98     }
99     
100     /**
101      * Note: java.home gets Absolute Path of Java, where we probably want soft links from 
102      * JAVA_HOME
103      * @return
104      */
105     private final static String javaHome() {
106         String rc = System.getenv("JAVA_HOME");
107         return rc==null?System.getProperty("java.home"):rc;
108     }
109     private final static String checkScript = 
110             "function mailit {\n" +
111             "  if [ -e /bin/mail ]; then\n" +
112             "     MAILER=/bin/mail\n" +
113             "  elif [ -e /usr/bin/mail ]; then \n" +
114             "     MAILER=/usr/bin/mail\n" +
115             "  else \n" +
116             "     MAILER=\"\"\n" +
117             "  fi\n" +
118             " if [ \"$MAILER\" = \"\" ]; then\n" +
119             "    printf \"$*\"\n" +
120             " else \n" +
121             "    printf \"$*\" | $MAILER -s \"AAF Certman Notification for `uname -n`\" $EMAIL\n"+
122             " fi\n" +
123             "}\n\n" +
124             "> $DIR/$APP.msg\n\n" +
125             "$JAVA -jar $JAR check $APP_ID $FQDN cadi_prop_files=$DIR/$APP.props 2>  $DIR/$APP.STDERR > $DIR/$APP.STDOUT\n" +
126             "case \"$?\" in\n" +
127             "  0)\n" +
128             "    # Note: Validation will be mailed only the first day after any modification\n" +
129             "    if [ \"`find $DIR -mtime 0 -name $APP.check.sh`\" != \"\" ] ; then\n" +
130             "       mailit `echo \"Certficate Validated:\\n\\n\" | cat - $DIR/$APP.msg`\n" +
131             "    else\n" +
132             "       cat $DIR/$APP.msg\n" +
133             "    fi\n" +
134             "    ;;\n" +
135             "  1) mailit \"Error with Certificate Check:\\\\n\\\\nCheck logs $DIR/$APP.STDOUT and $DIR/$APP.STDERR on `uname -n`\"\n" +
136             "    ;;\n" +
137             "  2) mailit `echo \"Certificate Check Error\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
138             "    ;;\n" +
139             "  10) mailit `echo \"Certificate Replaced\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
140             "      if [ -e $DIR/$APP.restart.sh ]; then\n" +
141             "        # Note: it is THIS SCRIPT'S RESPONSIBILITY to notify upon success or failure as necessary!!\n" +
142             "        /bin/sh $DIR/$APP.restart.sh\n" +
143             "      fi\n" +
144             "    ;;\n" +
145             "  *) mailit `echo \"Unknown Error code for CM Agent\\\\n\\\\n\" | cat - $DIR/$APP.msg`\n" +
146             "    ;;\n" +
147             " esac\n\n" +
148             " # Note: make sure to cover this sripts' exit Code\n";
149     
150     private final static String cronScript = 
151             "crontab -l | sed -n \"/#### BEGIN $CF/,/END $CF ####/!p\" > $TFILE\n" +
152             "# Note: Randomize Minutes (0-60) and hours (1-4)\n" +
153             "echo \"#### BEGIN $CF ####\" >> $TFILE\n" +
154             "echo \"$(( $RANDOM % 60)) $(( $(( $RANDOM % 3 )) + 1 )) * * * /bin/bash $SCRIPT " +
155                 ">> $DIR/cronlog 2>&1 \" >> $TFILE\n" +
156             "echo \"#### END $CF ####\" >> $TFILE\n" +
157             "crontab $TFILE\n" +
158             "rm $TFILE\n";
159 }
160
161
162