re base code
[sdc.git] / build.gradle
1 import javax.imageio.ImageIO
2 import javax.swing.*
3 import java.awt.*
4 import java.awt.event.ActionEvent
5 import java.awt.event.ActionListener
6 import java.util.List
7 import java.util.concurrent.Callable
8 import java.util.concurrent.Executors
9 import java.util.concurrent.atomic.AtomicBoolean
10
11 import static ServiceControl.*
12 import static Services.*
13
14 group 'com.att.ecomp'
15 version '1.01-SNAPSHOT'
16
17 apply plugin: 'groovy'
18 apply plugin: 'java'
19 apply plugin: "org.hidetake.ssh"
20 apply plugin: 'java-gradle-plugin'
21 apply plugin: 'idea'
22
23 sourceCompatibility = 1.8
24
25 buildscript {
26     repositories {
27         jcenter()
28         mavenCentral()
29     }
30     dependencies {
31         classpath 'org.codehaus.groovy:groovy-all:2.4.12'
32         classpath "org.hidetake:gradle-ssh-plugin:2.9.0"
33     }
34 }
35
36 enum Services {
37     BACKEND ,   //be
38     FRONTEND ,  //fe
39     DB ,        //cassandra
40     CACHING ,   //elawsrticsearch
41     SECURITY ,   //webseal
42     ALL //all services
43 }
44 enum ServiceControl {
45     HEALTH ,
46     START ,
47     RESTART ,
48     STOP ,
49     KILL
50 }
51 //env variables
52 //fill YOUR_WINDOWS_USER_HOME
53 project.ext.set("NEW_VAG",Boolean.FALSE) //flags to use new vagrant configuration
54 project.ext.set("IS_HOTSWAP",Boolean.FALSE) //flags to use new vagrant configuration
55 project.ext.set("PROJECT_PATH", System.getenv("SDC")) //ex. 'C:\\GIT_WORK\\asdc\\sdc')
56 project.ext.set("VAGRANT_HOME", NEW_VAG ? System.getenv("NEW_VAG") : System.getenv("VAG")) //ex. 'C:\\GIT_WORK\\vagrant-asdc-all-in-one')
57 project.ext.set("USER_HOME", "${System.getenv("USERPROFILE")}\\.ssh")
58 project.ext.set("BE_REMOTE", NEW_VAG ? '/opt/app/jetty/base/be' : '/home/vagrant/catalog-be' )
59 project.ext.set("FE_REMOTE", NEW_VAG ? '/opt/app/jetty/base/fe' : '/home/vagrant/catalog-fe' )
60 project.ext.set("VAGRANT_USER",     NEW_VAG ? 'm11981' : 'vagrant' )
61 project.ext.set("RSA_PRIVATE_KEY_PATH", NEW_VAG ? "$VAGRANT_HOME/id_rsa" : '' )
62 project.ext.set("VAGRANT_PASSWORD", NEW_VAG ? 'Aa123456' : 'vagrant' )
63 project.ext.set("X_FOLDER",'/xFolder' )
64 project.ext.set("BE_DEPENDENCIES", 'common-be,common-app-api,catalog-dao,catalog-model,security-utils' )
65 project.ext.set("command", [ (ALL) :  [  (HEALTH) : { NEW_VAG ? 'sudo curl -i http://localhost:8181/sdc1/rest/healthCheck' : 'curl -i localhost:8080/sdc2/rest/healthCheck' } ,
66                                          (KILL) : { NEW_VAG ? 'sudo pkill java' : 'pkill java'} ]  ,   //  TODO: refine kill only for services
67                              (BACKEND) : [  (START) : { NEW_VAG ? 'sudo service jettyBE start' : 'service catalog-be start'} ,
68                                             (STOP) : { NEW_VAG ? 'sudo service jettyBE stop' : 'service catalog-be stop'} ,
69                                             (RESTART) : { NEW_VAG ? 'sudo service jettyBE restart' : 'service catalog-be restart'}]  ,
70                              (DB) : [  (START) : { NEW_VAG ? 'sudo service cassandra start' : 'start-asdc-storage.sh' } ,
71                                        (STOP) : { NEW_VAG ? 'sudo service cassandra stop' : 'service cassandra stop'} ,
72                                        (RESTART) : { NEW_VAG ? 'sudo service cassandra restart' : 'service cassandra restart'} ]  ,
73                              (FRONTEND): [  (START) : {  NEW_VAG ? 'sudo service jettyFE start' : 'service catalog-fe start'   } ,
74                                             (STOP) : { NEW_VAG ? 'sudo service jettyFE stop' : 'service catalog-fe stop'} ,
75                                             (RESTART) : {  NEW_VAG ? 'sudo service jettyFE restart' : 'service catalog-fe restart'   } ]  ,
76                              (CACHING): [  (START) : { NEW_VAG ? 'sudo service elasticsearch start' : 'echo "starting es is not yet supported"'   } ],
77                              (SECURITY): [  (START) : {  NEW_VAG ? 'sudo docker start sdc-WebSeal-Simulator' : 'service webseal-simulator start'   } ,
78                                             (STOP) : { NEW_VAG ? 'sudo docker stop sdc-WebSeal-Simulator' : 'service webseal-simulator stop'} ,
79                                             (RESTART) : { NEW_VAG ? 'sudo docker restart sdc-WebSeal-Simulator' : 'service webseal-simulator restart'}]
80                              ] )      //abstraction level to shell scripts , support old and new vagrant bash commands
81
82 //icons
83 project.ext.set("warnImg",'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAADvSURBVDhPY2RgYPgPxGQDiAGLXkB4UMD56DuD9YFUhj179oD5Li4uDEcdZjN8l+ME8+EgTgLTAJDm7zWKYPb/enUwzdh4E0xzttxHNQRoABOUCQYYmj+9QrCBACQHUoMM4AYga74ZDiRAmvnEwHwQGywGBOiGgA1A16wmJYjQDAJANkgMmyEosYBVMzIAuuTWs/cM6iuhfCCAGwDWrAHxKyFw68ZNuCE40wGygcga0AEkEEHRiIxxASzqUKKRHIAwAJgo4BgXwKIGxQUgf8MwOsAlh+EFUMDBMAxgE0MGoLwAignSMFQPzmgkDjAwAACSmn13nChk1QAAAABJRU5ErkJggg==')
84 project.ext.set("okImg1",'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAD4SURBVDhPY2RgYPgPxGQDiAGLXkB4UMD56DuD9YFUhj179oD5Li4uDEcdZjN8l+ME8+EgTgLTAJDm7zWKYLbSRh8wfc9/C5jmbLmPagjQACYoEwwwNLMxgzHMIJAcSA0ygBuArFm81gyi+ddfCAaywWJAgG4I2AB0zdxWkhCNMABkg8SwGYISC1g1IwOgS74ee87wsvkUVADJAJjpyIDbRAxMfz3zCkwjA5ghONMB2DVIBiDbigwggQiKRmSMC2BRhxKN5ACEAcBEAce4ABY1LFAaDLAFJAwgyyGHB4YXQAEHwzCATQwZgPICKCZIw1A9OKOROMDAAAA4gXvZorg7ZgAAAABJRU5ErkJggg==')
85 project.ext.set("okImg2" , 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAADjSURBVDhPY2RgYPgPxGQDiAGLXkB4UMD56DuD9YFUhj179oD5Li4uDEcdZjN8l+ME8+EgTgLTAJDm7zWKYPbWZ7Jg2lvqMZjmbLmPagjQACYoEwywaQYBGBskB1KDDOAGIGuetFsUTCMDmBi6IWAD0DUra3OA2cgAJIbNEJRYwKUZGdy9+oMhz/U1lIdkADZnwwwDaUIHMENwpgNk16DbigwggQiKRmSMC2BRhxKN5ACEAcBEAce4ABY1LFAaDLAFJAwgyyGHB4YXQAEHwzCATQwZgPICKCZIw1A9OKOROMDAAAAZD3X55epfOAAAAABJRU5ErkJggg==')
86 project.ext.set("errorImg" , 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAADlSURBVDhPY2RgYPgPxGQDiAGLXkB4UMD56DuD9YFUhj179oD5Li4uDEcdZjN8l+ME8+EgTgLTAJDm7zWKYPZbGRUwLfzkDpjmbLmPagjQACYoEwywaQYBGBskB1KDDOAGIGtexisCppEBTAzdELAB6Jrd+QXAbGQAEsNmCEos4NKMDHZ+/MAQ9fkNlIdkADZnwwwDaUIHyIaADMDAQAP/AwMPjEFsbGpAGGs6AEUPsnfgzsaiDiUayQUgF2A4jaAXoHpQXAByNgyjA1xyGF4A+RuGYQCbGDLA6gWCGKoHJSGRDhgYAL/hkunRq+joAAAAAElFTkSuQmCC')
87 project.ext.set("unavailableImg" , 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAABJ0AAASdAHeZh94AAAAB3RJTUUH4QwNDgEDrNoaqgAAAOhJREFUOMulkz8OgjAUhz8MQ7mBkwnnkBAH4mbCCdxcXLwAoxdwYXHjBCRupoMheg4TJ29QNhwshMofSfpLOry2v6/vta8OUGEhF4DsbUx6L8XytkNKCUAURTxWZ9TCM93buQb8mFXiI4E43gCQ5xeQPt7x2YG4fWa0OQwDhBANRCVdyGyKOQyDJhuV+HgvZQLGzABCiEGI036FPnNbZVlSFPfvnWg1gJpea72OjPh6lUZcQ5yhPkjTkxHv94fpfcB23t81PftmWMr9e+pQZjobd6zuobX2fViXAFCRvSv9GtOH9ji23/kDswRrCVqtQOAAAAAASUVORK5CYII=')
88
89
90 //health params
91 project.ext.set("trayIcon", null)
92 project.ext.set("lastStatus", null)
93 project.ext.set("isStopHealthCheck", false)
94 project.ext.set("isHaltHealth", new AtomicBoolean(false) )
95 project.ext.set("startedAwait", Long.MAX_VALUE)
96 project.ext.set("logFile", 'C:/ProgramData/all.log')
97 project.ext.set("pomList" , ["${System?.getenv('SDC')}/catalog-fe/pom.xml" ,"${System?.getenv('SDC')}/catalog-be/pom.xml" ] )   //empty list will scan all openecomp poms
98 project.ext.set("pomChangesMap" , [:] )
99 project.ext.set("beConfigFilesToCopyMapping" , [ 'src/main/resources/config/*.yaml' : 'config/catalog-be/' ,
100                                                'src/main/resources/config/*.properties' : 'config/catalog-be/'] )
101
102 //menu item strings
103 project.ext.set("toggleHealthString" , "Halt Health" )
104 //menu item
105 project.ext.set("toggleHealthItemView" , null )
106
107 //other
108 project.ext.set("IS_MVN_INSTALL",false)
109 project.ext.set("executor" , null )
110 project.ext.set("lockObj" , new Object() )
111 /*compile?.doLast {
112     println "2. hello compile2"
113 }*/
114
115 /*def post(String host , String serviceName,String msg){
116     // POST
117     def post = new URL("$host/$serviceName").openConnection();
118     def message = '{"message":"this is a message"}'
119     post.setRequestMethod("POST")
120     post.setDoOutput(true)
121     post.setRequestProperty("Content-Type", "application/json")
122     post.getOutputStream().write(message.getBytes("UTF-8"));
123     def postRC = post.getResponseCode();
124     println(postRC);
125     if( postRC.equals(200)) {
126         println(post.getInputStream().getText());
127     }
128 }
129
130 def postStat( long operationTime, String user , String meta  ){
131     def host = 'http://135.76.123.70:8888'
132     def params = "user=$user&meta=$meta"
133     post host , "UserStats" , params
134 }*/
135
136
137 def hash( List list ){
138     def map = list?.collectEntries { File file -> [(file?.absolutePath) : file?.text?.hashCode() ]}
139
140     map
141 }
142
143 def pomChanges(){
144     long started = System.currentTimeMillis()
145     if ( !pomList )
146         listPom()
147     //find hash changes
148     def changes = pomList?.findAll {
149         def File file = new File(it);
150         pomChangesMap[it] != file?.text?.hashCode()
151     }
152     println "\n\n[MasterD][POM]--> detected changes for -> $changes"
153     //update changes in map
154     changes?.each { pomChangesMap[it] = new File(it)?.text?.hashCode() }
155     println "\n\n[MasterD][POM]--> pom map -> $pomChangesMap"
156
157     println """ 
158             ****** POM changes detection finished after -> ${System.currentTimeMillis()- started}ms  ******
159             """
160
161     changes
162 }
163 //list pom with updated file hashes
164 def listPom(){
165     long started = System.currentTimeMillis()
166     if (!pomList) {
167         def tree = fileTree( PROJECT_PATH ).include '**/pom.xml'//.filter { it.isFile() && it?.toString()?.toLowerCase()?.endsWith('pom.xml') }
168         //println "$PROJECT_PATH list is ->${ list?.collect { it?.absolutePath } }"
169         //flatten and filter openecomp poms
170         pomList = tree?.flatten()?.findAll { File file -> file?.text?.contains('org.openecomp.sdc') }?.collect {File file -> file?.absolutePath }
171     }
172     pomChangesMap = pomList.collectEntries { absolutePath ->[ ( absolutePath ) : new File(absolutePath)?.text?.hashCode() ] }
173
174     println """ [MasterD][Init] intializing POM detector
175
176             *********       POM listing finished after -> ${System.currentTimeMillis()- started}ms    *********
177             """
178     return pomList
179 }
180
181
182 task initialization(){
183     listPom()
184     executor = Executors.newCachedThreadPool();
185 }
186
187 def parallel( closure ){
188     executor?.submit(new Callable<Object>(){
189         @Override
190         public Object call() {
191             closure();
192             return null;
193         }
194     })
195 }
196 /*class Preferences {
197     def String Username
198     def String IsNewVagrant
199     def String IsRapidMode
200 }
201
202 def initXFolder(){
203     def folder = new File(X_FOLDER);
204     folder?.exists() ?: folder?.mkdirs()
205
206     new File("${folder?.absolutePath}/$PREFERENCES_FILENAME")
207 }*/
208
209 task tester{
210     /*doLast{
211         //postStat(10000, "shay" , "report/index")
212         listPom()
213         new File('catalog-be\\pom.xml') << "#hello"
214         pomChanges()
215     }*/
216 }
217
218
219 def fetchFilesByExtention(remote, local , ext ){
220     ssh.run {
221         def started = System.currentTimeMillis()
222         println "folder diff"
223         session(remotes.vagrant) {
224             //execute "cd /home/vagrant/catalog-be/tmp ; ls -lt | grep catalog-be" //todo- use my dates to filter
225             get from: remote  , into: local , filter: {  it?.absolutePath =~ /jetty.*catalog-be.*\.dir.*\.$ext/  } // {  it?.absolutePath =~ /.*catalog-be.*dir.*classes.*/  }
226         }
227         println "fetched files in ${System.currentTimeMillis() - started} ms"
228     }
229 }
230
231 def updateRemoteFile(String remote , String local){
232     ssh.run {
233         def to = "$BE_REMOTE${remote[remote?.indexOf("tmp\\")..remote.size()-1].replaceAll("\\\\","/")}"
234         println "copying $local \nto\n $to"
235         session(remotes.vagrant) {
236             put from: local  , into: to }
237     }
238 }
239
240 def compareAndSwap(){
241     def final LIMIT = 10
242     def newClasses = new File("$PROJECT_PATH\\catalog-be\\target\\classes")
243     def File jettyClasses ;
244     //locate classes
245     println "traversing.."
246     new File("build/hotswap").traverse { if (it?.directory && it?.name?.equals("classes")){
247         jettyClasses = it
248         return;
249     }  }
250     def jettyClassesList = []
251     jettyClasses?.traverse { jettyClassesList << it }
252
253     println "$jettyClasses"
254     //Sort compiled classes
255     def files = []
256     newClasses?.traverse {  files << it }
257     def result = files.sort{ a,b -> b.lastModified() <=> a.lastModified() }
258     println "show only last $LIMIT changes"
259     result[0..LIMIT]?.each{ println it?.lastModified() +" | "+ it?.name + (it?.directory ? "[Directory]" : "") } //show only last 10 changes
260
261     //update
262     def changesMap = [ : ] //<old,new>
263     println "updating changes"
264     result[0..LIMIT]?.each { File f -> def File other = jettyClassesList.find{ File other-> other?.absolutePath?.endsWith(f?.name) };
265         if ( !(f.directory) && f?.text?.hashCode() != other?.text?.hashCode() )
266             updateRemoteFile( other?.getAbsolutePath() , f?.getAbsolutePath() )
267     } //use hashing
268 }
269
270 task hotswap(){
271     doLast {
272         new File("build/hotswap")?.deleteDir()
273         new File("build/hotswap")?.mkdirs()
274         ssh.settings {
275             knownHosts = allowAnyHosts
276         }
277         fetchFilesByExtention( "$BE_REMOTE/tmp/" , 'build/hotswap' , "class")
278         compareAndSwap()
279     }
280 }
281
282 remotes {
283     vagrant {
284         host = '127.0.0.1'
285         port = 2222
286         user = VAGRANT_USER
287         password = VAGRANT_PASSWORD
288         identity = NEW_VAG ? new File(RSA_PRIVATE_KEY_PATH) : null
289     }
290
291 }
292
293 def gitLatest(){
294
295 }
296
297 def newEcoSystem(){
298     //cleanTitan()
299     backupDB() //and clean all
300     //restoreDB() //restore latest
301     createSchema()
302     fillSchema()
303     postCreate()
304     startAll()
305     //todo- conside updating from git
306     updaterBEFull()
307     updaterFE()
308 }
309 def importHeatTypes(){
310    //todo- impl
311 }
312 def postCreate(){
313     importNormative()
314     importHeatTypes()
315 }
316 def fillSchema(){
317     // add conformence level
318 }
319 def createSchemaPreStep(){
320     //todo- DB up
321 }
322 def createSchemaPostStep(){
323     ////todo- impl create amdocs dox
324 }
325 def createSchema(){
326     createSchemaPreStep()
327     //todo- create schema
328     //todo- create titan
329     createSchemaPostStep()
330 }
331
332 def cleanTitan(){
333     execSafe{
334         ssh.settings {
335             knownHosts = allowAnyHosts
336         }
337         ssh.run {
338             session(remotes.vagrant) {
339                 execute "cqlsh -e 'DROP KEYSPACE titan;'"
340                 println "[MasterD][DB_DROP]-> Dropped 'titan' KEYSPACE."
341             }
342         }
343     }
344 }
345 task cleanTitan {
346     doLast{
347         cleanTitan()
348     }
349 }
350
351 task fetchE2EDB(){
352     doLast{
353         fetchE2EDB()
354     }
355 }
356 def fetchE2EDB(){
357     execSafe{
358         ssh.settings {
359             knownHosts = allowAnyHosts
360         }
361         ssh.run {
362             session(remotes.vagrant) {
363                 def tmp = '$CASSANDRA_HOME/backup/e2e'
364                 //execute 'mkdir $CASSANDRA_HOME/backup/e2e/'
365                 //execute 'wget http://135.76.210.202:8080/ETE_backup_files/latest_ETE_backup_file.zip -P /vagrant/db'
366                 println "[MasterD] download finished, unzipping.."
367                 execute "unzip -u $tmp/latest_ETE_backup_file.zip" //execute 'unzip -u /vagrant/db/latest_ETE_backup_file.zip'//'
368                 def folder = execute "cd $tmp; ls -d -- */"
369                 println "[MasterD] unzipping finished into -> $folder , untaring.."
370                 execute "tar zxf $tmp/$folder/* --strip 3 -C" +'$CASSANDRA_HOME/data | pv -l >/dev/null'
371                 println "[MasterD][E2E_DB]-> Downloaded & unzipped e2e data successfully."
372             }
373         }
374     }
375 }
376 def copyExplodedBE() {
377     execSafe{
378         println "[MasterD][BackEnd] copying exploded war."
379         ssh.settings {
380             knownHosts = allowAnyHosts
381         }
382         long started = System.currentTimeMillis()
383         def dirPath = "${PROJECT_PATH}/catalog-be/target/catalog-be-1.1.0-SNAPSHOT"
384         def dir = new File(dirPath);
385         println "[MasterD][BackEnd] copying ${dir?.directorySize()/(1024*1024)} MB, from ${dir?.name} to $BE_REMOTE/webapps"
386                 ssh.run {
387                     session(remotes.vagrant) {
388                         execute "rm -R $BE_REMOTE/webapps/catalog-be-1.1.0-SNAPSHOT"
389                         put from: dir?.absolutePath , into: "$BE_REMOTE/webapps"
390                     }
391                 }
392         println "[MasterD][BackEnd] Successfully copied exploded war in ${System.currentTimeMillis()-started}ms."
393     }
394 }
395 task copyExplodedBE(){
396     doLast{
397         copyExplodedBE()
398     }
399 }
400 def backupDB() {
401     execSafe{
402         ssh.settings {
403             knownHosts = allowAnyHosts
404         }
405         ssh.run {
406             session(remotes.vagrant) {
407                 execute 'mkdir -p $CASSANDRA_HOME/backup'
408                 def res = execute 'mv $CASSANDRA_HOME/data $CASSANDRA_HOME/backup/data_$(date +\'%Y_%m_%d__%H:%M:%S\')'
409                 println "[MasterD][DB_BACKUP]-> snapshot DB finished. $res"
410             }
411         }
412     }
413 }
414 task backupDB{
415     doLast{
416         backupDB()
417     }
418 }
419
420 def restoreDB(){
421     execSafe{
422         ssh.settings {
423             knownHosts = allowAnyHosts
424         }
425         ssh.run {
426             session(remotes.vagrant) {
427                 println "[MasterD]-> restoring DB"
428                 execute 'mkdir -p $CASSANDRA_HOME/data'
429                 def res = execute 'mv $CASSANDRA_HOME/backup/data_*/* $CASSANDRA_HOME/data'
430                 println "[MasterD]-> DB restore FINISHED!! $res"
431             }
432         }
433     }
434 }
435 task restoreDB() {
436     doLast {
437         restoreDB()
438     }
439 }
440
441 def vm( ){
442     exec{
443         if (VAGRANT_HOME){
444             workingDir VAGRANT_HOME     //vagrant path
445             println "*****************\nworking dir -> $VAGRANT_HOME"
446             commandLine "cmd","/c", "vagrant up"
447             //args = [ ]
448         } else {
449             println "[MasterD]--> please define windows enviroment variable VAG pointing to vagrant project"
450         }
451     }
452 }
453
454 task vm{
455     doLast{
456         vm()
457     }
458 }
459
460 def copyFE() {
461     println "[MasterD][FrontEnd] starting war copy."
462     ssh.settings {
463         knownHosts = allowAnyHosts
464     }
465     long started = System.currentTimeMillis()
466     def target = "${PROJECT_PATH}/catalog-fe/target/"
467     def files = GFileUtils.listFiles( new File(target) , ["war"] as String[] , false);
468     files?.each{ File file ->
469         if (!file?.name?.contains('classes')){
470             println "[MasterD][FrontEnd] copying ${file.length()/(1024*1024)} MB, from ${file?.name} to $FE_REMOTE/webapps"
471             ssh.run {
472                 session(remotes.vagrant) {
473                     put from: file?.absolutePath , into: "$FE_REMOTE/webapps"
474                 }
475             }
476         }
477
478     }
479     println "[MasterD][FrontEnd] Successfully copied war in ${System.currentTimeMillis()-started}ms."
480 }
481
482 task deployFE{
483     doLast {
484         copyFE()
485     }
486 }
487
488
489 def copyBE(){
490     println "[MasterD][BackEnd] starting war copy."
491     ssh.settings {
492         knownHosts = allowAnyHosts
493     }
494     def target = "${PROJECT_PATH}/catalog-be/target/"
495     def files = GFileUtils.listFiles( new File(target) , ["war"] as String[] , false);
496     long started = System.currentTimeMillis()
497     files?.each{ File file ->
498         if (!file?.name?.contains('classes')){
499             println "[MasterD][BackEnd] copying ${file.length()/(1024*1024)} MB, from ${file?.name} to $BE_REMOTE/webapps"
500             ssh.run {
501                 session(remotes.vagrant) {
502                     put from: file?.absolutePath , into: "$BE_REMOTE/webapps"
503                 }
504             }
505         }
506     }
507     println "[MasterD][BackEnd] SUCCESSFULY copied be war in ${System.currentTimeMillis()-started}ms."
508 }
509
510 task deployBE {
511     doLast {
512         copyBE()
513     }
514 }
515 def compileFE(){
516     exec{
517         println "[MasterD][FE]--> compiling project at -> ${PROJECT_PATH}\\catalog-fe"
518         workingDir "${PROJECT_PATH}"    //vagrant path
519         commandLine 'cmd','/c','mvn -T 2 compile -pl catalog-fe,catalog-ui -am -Pcatalog -Dmaven.test.skip'
520     }
521 }
522 task compileFE(){
523     doLast{
524         compileFE()
525     }
526 }
527 def compileBE(){
528     exec{
529         println "[MasterD][BE]--> compiling project at -> ${PROJECT_PATH}\\catalog-be"
530         workingDir "${PROJECT_PATH}"    //vagrant path
531         commandLine 'cmd','/c','mvn -T 2 compile -pl catalog-be -Pcatalog -Dmaven.test.skip'
532     }
533 }
534 task compileBE{
535     doLast{
536         compileBE()
537     }
538 }
539
540 def compile(){
541     exec{
542         println "[MasterD]--> compiling project at -> ${PROJECT_PATH}"
543         workingDir "${PROJECT_PATH}"    //vagrant path
544         commandLine 'cmd','/c','mvn -T 2 compile -am -Pcatalog -Dmaven.test.skip'
545     }
546 }
547
548 task compile{
549     doLast{
550         compile()
551     }
552 }
553 def compileDependencies(){
554     def cmd = IS_MVN_INSTALL ? 'install' : 'compile'
555     exec{
556         println "[MasterD]--> compiling BE dependencies  -> $BE_DEPENDENCIES [ SKIPPING TESTS!! ]"
557         workingDir "${PROJECT_PATH}"    //vagrant path
558         commandLine 'cmd','/c',"mvn $cmd -pl $BE_DEPENDENCIES -Pcatalog -Dmaven.test.skip" //commandLine 'cmd', '/c','mvn -T 1C package -pl catalog-model,catalog-dao,catalog-be -P catalog -Dmaven.test.skip'
559     }
560 }
561 task compileDependencies {
562     doLast{
563         compileDependencies()
564     }
565 }
566 def compileWar(){
567     def cmd = IS_MVN_INSTALL ? 'install' : 'compile'
568     exec{
569         println "--> compiling project at -> ${PROJECT_PATH}\\catalog-be"
570         workingDir "${PROJECT_PATH}"    //vagrant path
571         commandLine 'cmd','/c',"mvn -T 1 $cmd war:war -pl catalog-be -Pcatalog -Dmaven.test.skip" //commandLine 'cmd', '/c','mvn -T 1C package -pl catalog-model,catalog-dao,catalog-be -P catalog -Dmaven.test.skip'
572     }
573 }
574 task compileWar(){
575     doLast{
576         compileWar()
577     }
578 }
579
580 //deprecated - use deployBE()
581 task be() {
582     doLast{
583         def started = System.currentTimeMillis();
584         exec{
585             println "[MasterD]--> copying be... [from $VAGRANT_HOME]"
586             workingDir VAGRANT_HOME     //vagrant path
587             commandLine 'cmd','/c', 'copy_war_be.bat','localhost' , "$PROJECT_PATH\\catalog-be\\target\\catalog-be*.war" , "$BE_REMOTE/webapps"
588             //args = [ ]
589         }
590         println """
591                 ****    copying finished in -> ${System.currentTimeMillis() - started}ms         ****
592                 """
593     }
594 }
595
596 task beConfig( ) {
597     doLast{
598         exec{
599             workingDir "${System.env.VAG}"      //vagrant path
600             commandLine 'cmd','/c','copy_war_be_with_configuration','localhost' , "$PROJECT_PATH\\catalog-be\\target\\catalog-be*.war" , "$BE_REMOTE/webapps"
601             //args = [ ]
602         }
603     }
604
605 }
606 task feConfig( ) {
607     doLast{
608         exec{
609             workingDir "${System.env.VAG}"      //vagrant path
610             commandLine 'cmd','/c','copy_war_fe_with_configuration','localhost' , "$PROJECT_PATH\\catalog-fe\\target\\catalog-fe*.war" , "$FE_REMOTE/webapps"
611             //args = [ ]
612         }
613     }
614
615 }
616
617 task fe() {
618     doLast{
619         exec {
620             workingDir "${System.env.VAG}"    //vagrant path
621             commandLine 'cmd','/c', 'copy_war_fe.bat', 'localhost', "$PROJECT_PATH\\catalog-fe\\target\\catalog-fe*.war", "$FE_REMOTE/webapps"
622             //args = [ ]
623         }
624     }
625 }
626
627 def installAllProject(){
628     exec{
629         println "[MasterD]--> Compiling&Installing project at -> ${PROJECT_PATH}"
630         workingDir "${PROJECT_PATH}"    //vagrant path
631         commandLine 'cmd','/c','mvn -T 2 clean install -U -Pcatalog -Dmaven.test.skip'
632     }
633 }
634 task installAllProject {
635     doLast {
636         installAllProject()
637     }
638 }
639
640 task installAll {
641     doLast{
642         println '[MasterD]--> Finished!!'
643     }
644 }
645 installAll.dependsOn { tasks.findAll { task -> task.name.startsWith('install_') } }
646
647 def install_BE(){
648     exec {
649             println '[MasterD][Install]--> Installing BE!!'
650             workingDir "${PROJECT_PATH}"
651             commandLine 'cmd','/c', 'mvn clean install -pl catalog-be -am -Pcatalog -Dmaven.test.skip'
652             //args = [ ]
653     }
654 }
655
656 task install_BE() {
657     doLast{
658         install_BE()
659     }
660 }
661
662 def install_FE() {
663     exec {
664         workingDir "${PROJECT_PATH}"
665         commandLine 'cmd','/c', 'mvn clean install -pl catalog-ui,catalog-fe -am -Pcatalog -Dmaven.test.skip'
666     }
667 }
668 task install_FE() {
669     doLast {
670         install_FE()
671     }
672 }
673
674 def updaterBERapid(){
675     /* if ( ticket() > PREVIOUS_BUILD_VAR ){
676      PREVIOUS_BUILD_VAR = ticket()*/
677     def started = System.currentTimeMillis();
678     println "[MasterD][Rapid]--> compiling changes using maven"
679     compileWar()
680     println "[MasterD][Rapid]--> copying war"
681     copyBE() //use this if you want to deploy entire war //hotswap.execute()
682     restartBackend()
683     println msg(" redeploy finished in -> ${System.currentTimeMillis() - started}ms ")
684 }
685 task updaterBERapid(){
686     doLast {
687         updaterBERapid()
688     }
689 }
690 def updaterBE(){
691     def started = System.currentTimeMillis();
692     IS_MVN_INSTALL = pomChanges() ? true :  false
693     println "[MasterD]--> compiling changes using maven"
694     compileDependencies()
695     compileWar()
696     println "[MasterD]--> copying war"
697     IS_HOTSWAP ? copyExplodedBE() : copyBE() //execute() //use this if you want to deploy entire war //hotswap.execute()
698     restartBackend()
699     println msg("redeploy finished in -> ${System.currentTimeMillis() - started}ms ")
700 }
701 task updaterBE(){
702     doLast {
703         updaterBE()
704     }
705 }
706 def copyBEConfiguration(){
707 /*    execSafe {
708         ssh.settings {
709             knownHosts = allowAnyHosts
710         }
711         ssh.run {
712             session(remotes.vagrant) {
713                 println msg("Stopping BackEnd Server")
714                 execute command[BACKEND][STOP]()
715             }
716         }
717     }*/
718 }
719 def updaterBEFull(){
720     def started = System.currentTimeMillis();
721     compile()
722     println "[MasterD]--> copying war"
723     copyBE() //use this if you want to deploy entire war //hotswap.execute()
724     copyBEConfiguration()
725     println msg("redeploy finished in -> ${System.currentTimeMillis() - started}ms ")
726 }
727 task updaterBEFull(){
728     doLast {
729         updaterBEFull()
730     }
731 }
732 def copyFEConfiguration(){
733     //todo- implement
734 }
735 def updaterFE(){
736     def started = System.currentTimeMillis();
737     println "[MasterD]--> compiling changes using maven"
738     compileFE()
739     println "[MasterD]--> copying war"
740     copyFE() //.execute() //use this if you want to deploy entire war //hotswap.execute()
741     copyFEConfiguration()
742     println msg("redeploy finished in -> ${System.currentTimeMillis() - started}ms ")
743 }
744 task updaterFE(){
745     doLast {
746       updaterFE()
747     }
748 }
749 def stopBackend(){
750     execSafe {
751         ssh.settings {
752             knownHosts = allowAnyHosts
753         }
754         ssh.run {
755             session(remotes.vagrant) {
756                 println msg("Stopping BackEnd Server")
757                 execute command[BACKEND][STOP]()
758             }
759         }
760     }
761 }
762 task stopBackend(){
763     doLast {
764         stopBackend()
765     }
766 }
767
768 def startBackend(){
769     execSafe {
770         ssh.settings {
771             knownHosts = allowAnyHosts
772         }
773         ssh.run {
774             session(remotes.vagrant) {
775                 println msg("[MasterD] starting backend sever")
776
777                 execute command[BACKEND][START]()
778             }
779         }
780     }
781     println """[MasterD]->  finished !!
782                                             """
783 }
784 task startBackend(){
785     doLast{
786         startBackend()
787     }
788 }
789
790 def restartBackend(){
791     execSafe {
792         ssh.settings {
793             knownHosts = allowAnyHosts
794         }
795         ssh.run {
796             session(remotes.vagrant) {
797                 println msg("[MasterD] restarting backend sever")
798
799                 execute command[BACKEND][RESTART]()
800             }
801         }
802     }
803     println """[MasterD]->  finished !!
804                                             """
805 }
806 //todo- remove this if you want to auto-deploy on every file save
807 /*
808 compileJava.doFirst{
809     updater?.execute()
810 }*/
811
812 enum STATUS { UP, DOWN , UNKNOWN , UNAVAILABLE }
813
814 task health(){
815     doLast {
816         prepareTray()
817     }
818 }
819
820 def execSafe( closure){
821    if (!lockObj) {
822         [0..4].forEach( {println "Critical ERROR : lock object is not initialized\n\nCritical ERROR : cannot run tasks\n"}() )
823         return;
824    }
825    synchronized (lockObj){
826            boolean prev = isHaltHealth.get()
827            try {
828                isHaltHealth.set(true)
829                closure()
830            } catch (Exception e) {
831                println e
832            } finally {
833                isHaltHealth.set(prev)
834            }
835    }
836 }
837
838 def fetchFiles( remote, local ){
839     ssh.run {
840         session(remotes.vagrant) {
841             //execute "cd /home/vagrant/catalog-be/tmp ; ls -lt | grep catalog-be" //todo- use my dates to filter
842             def f = get from: remote  , into: local
843             println f?.name
844             //return f
845         }
846         //println "fetched files in ${System.currentTimeMillis() - started} ms"
847     }
848
849     //return null
850 }
851
852
853 def killJava(){
854     execSafe {
855         def res
856         ssh.run {
857             session( remotes.vagrant ) {
858                 println """                                      *-*-****************************-*-*
859                                                                 killing all java proccesses
860                                                 *-*-****************************-*-*
861                                         """
862                 res = execute command[ALL][KILL]()
863             }
864         }
865         println res?.toString()
866     }
867 }
868
869 def importNormative(){
870     execSafe {
871         ssh.run {
872             session(remotes.vagrant) {
873                 println """                                      *-*-************************************-*-*
874                                                                                         importNormative
875                                                                 *-*-************************************-*-*
876                                                         """
877                 execute "python -v $BE_REMOTE/scripts/import/tosca/importNormativeAll.py"
878             }
879         }
880     }
881 }
882
883 def startAll(){
884     def startCassandra = """
885                         #!/bin/bash
886                         
887                         cassandra&
888                         elasticsearch -d
889                         
890                         #Wait until ES is up
891                         until curl localhost:9200/_cluster/health;
892                         do
893                             printf "."
894                             sleep 3
895                         done
896                         
897                         # Create Elastic Mapping if not exist in ES
898                         createESMapping.sh
899                         """
900     execSafe {
901         ssh.run {
902             session(remotes.vagrant) {
903                 println """                                      *-*-************************************-*-*
904                                                                 starting all SDC services(DB,BE,FE,Webseal)
905                                                         *-*-************************************-*-*
906                                 """
907                 if ( NEW_VAG ){
908                     execute command[DB][START]()
909                     Thread.sleep(5000)
910                     execute command[CACHING][START]()
911                 }
912                 else
913                     execute startCassandra
914                 //[0..4]?.forEach( Thread?.sleep(2000) )
915                 Thread?.sleep(10000)
916                 Thread?.sleep(10000)
917                 execute command[BACKEND][START]()
918                 execute command[FRONTEND][START]()
919                 execute command[SECURITY][START]()
920             }
921         }
922     }
923 }
924
925
926 /*def clearLog(type: Delete){
927     delete{
928         delete 'C:/ProgramData/all.log'
929         followSymlinks = true
930     }
931 }*/
932 task clearLog(type: Delete){
933     doLast{
934         delete 'C:/ProgramData/all.log'
935         followSymlinks = true
936     }
937 }
938 def logBE(){
939     try{
940         println "\n*** logging BE all.log ***\n"
941
942             ssh.run {
943                 session( remotes.vagrant ) {
944                     //String now = execute 'echo \"\$(date +\'%Y_%m_%d\')\"'
945                     //println "\n\n*******************************************************\n\n"+now?.toString()
946                     clearLog?.execute() //todo- remove this .execute()
947                     fetchFiles( '/home/vagrant/catalog-be/logs/SDC/SDC-BE/all.log' , 'C:/ProgramData') //"%USERPROFILE%\AppData\Local\")
948                     //project.ext.set( "logFile" , 'C:/ProgramData/all.log' )
949
950                     //                          -f /home/vagrant/catalog-fe/logs/*$now.stderrout.log -f /home/vagrant/catalog-be/logs/*$now.stderrout.log -f /home/vagrant/catalog-be/logs/ASDC/ASDC-BE/debug.log*'
951                 }
952             }
953
954         parallel {
955             exec {
956                 //if ( logFile ){
957                 String notepad = 'C:\\Program Files (x86)\\Notepad++\\notepad++.exe'
958                 println "logging $logFile to notepad++ [$notepad]"
959                 commandLine 'cmd','/c' , notepad ,logFile
960             }
961         }
962     }catch(Exception e){
963         println "cannot open logs!!!"
964         e.printStackTrace()
965     }
966 }
967 task logBE(){
968     doLast{
969         logBE()
970     }
971 }
972
973 def toggleHealthPolling(){
974     isHaltHealth.set(!isHaltHealth.get())
975 }
976 //converts predefined icon to Image
977 def Image convert( imageName ){
978     String encodedimage = project.ext.get(imageName);
979     byte[] byteImage = encodedimage?.split(',')[1]?.decodeBase64()
980     Image image = ImageIO.read(new ByteArrayInputStream(byteImage));
981 }
982
983 def refreshMenu(String imageName){
984     switch( imageName ) {
985         case 'unavailableImg' : toggleHealthString = "Resume Health";  break;
986         default : toggleHealthString = "Halt Health";
987     }
988     if (((MenuItem)toggleHealthItemView).getLabel() != toggleHealthString)
989         ((MenuItem)toggleHealthItemView).setLabel(toggleHealthString);
990 }
991 def startDB(){
992         println "[MasterD] Starting database.."
993         execSafe {
994             ssh.settings {
995                 knownHosts = allowAnyHosts
996             }
997             ssh.run {
998                 session(remotes.vagrant) {
999                     execute command[DB][START]()
1000                 }
1001             }
1002         }
1003 }
1004 task startDB(){
1005     doLast {
1006         startDB()
1007     }
1008 }
1009 def stopDB(){
1010     execSafe {
1011         ssh.settings {
1012             knownHosts = allowAnyHosts
1013         }
1014         ssh.run {
1015             session(remotes.vagrant) {
1016                 execute command[DB][STOP]()
1017             }
1018         }
1019     }
1020 }
1021 task stopDB(){
1022     doLast {
1023         stopDB()
1024     }
1025 }
1026 def startFE(){
1027     execSafe {
1028         ssh.settings {
1029             knownHosts = allowAnyHosts
1030         }
1031         ssh.run {
1032             session(remotes.vagrant) {
1033                 execute command[FRONTEND][START]()
1034             }
1035         }
1036     }
1037 }
1038 task startFE(){
1039     doLast {
1040         startFE()
1041     }
1042 }
1043 def stopFE(){
1044     execSafe {
1045         ssh.settings {
1046             knownHosts = allowAnyHosts
1047         }
1048         ssh.run {
1049             session(remotes.vagrant) {
1050                 execute command[FRONTEND][STOP]()
1051             }
1052         }
1053     }
1054 }
1055 task stopFE(){
1056     doLast {
1057         stopFE()
1058     }
1059 }
1060
1061 def ActionListener newListener( closure  ){
1062     ActionListener listener = new ActionListener() {
1063         public void actionPerformed(ActionEvent e) {
1064             try {
1065                     closure()
1066             } catch (AWTException e1) {
1067                 System.err.println(e1);
1068             }
1069         }
1070     }
1071
1072     listener
1073 }
1074
1075 ext.updateTray = { STATUS status ->
1076     lastStatus = status
1077     if (SystemTray.isSupported()) {
1078         // get the SystemTray instance
1079         SystemTray tray = SystemTray.getSystemTray();
1080         // load an image
1081         String imageName = status==STATUS.UP ? (Math.random()>0.5 ?'okImg1':'okImg2') : status==STATUS.UNAVAILABLE ? 'unavailableImg' : status==STATUS.DOWN ? 'errorImg' : 'warnImg'
1082         Image image = convert imageName
1083         if (trayIcon != null) {
1084             trayIcon.setImage(image)
1085             refreshMenu(imageName);
1086             return ;
1087         }
1088         //region  -> Menu UI
1089         // create a popup menu
1090         PopupMenu popup = new PopupMenu();
1091         // create menu item for the default action
1092
1093         MenuItem hotswapItem = new MenuItem("===> WAR <===");
1094         hotswapItem.setFont(new Font("MONOSPACED" , Font.BOLD ,15f ))
1095
1096         //region Multilevel Menus
1097         Menu deployMasterMenu = new Menu("DeployMaster");
1098         Menu backendMenu = new Menu("Backend");
1099         Menu frontendMenu = new Menu("Frontend");
1100         Menu dbMenu = new Menu("Database");
1101         try{
1102             deployMasterMenu.setFont(new Font("Cooper Black" ,Font.BOLD ,14f ))
1103             backendMenu.setFont(new Font("Cooper Black" ,Font.PLAIN ,13f ))
1104             frontendMenu.setFont(new Font("Cooper Black" ,Font.PLAIN ,13f ))
1105             dbMenu.setFont(new Font("Cooper Black" ,Font.PLAIN ,13f ))
1106         }catch(Exception e){
1107             println e
1108         }
1109
1110         //DeployMaster Menu
1111         MenuItem updaterBeWithDependenciesItem = new MenuItem("[BE] Quick Compile -> Deploy");
1112         MenuItem updaterFullBeItem = new MenuItem("[BE] Full Install  -> Deploy");
1113         MenuItem updaterFeItem = new MenuItem("[FE] Quick Compile -> Deploy");
1114         MenuItem updaterFullFeItem = new MenuItem("[FE] Full Install -> Deploy");
1115
1116         //Menu UI build
1117
1118         deployMasterMenu.add(updaterFullBeItem);
1119         deployMasterMenu.add(updaterBeWithDependenciesItem);
1120
1121         deployMasterMenu.addSeparator();
1122         deployMasterMenu.add(updaterFullFeItem);
1123         deployMasterMenu.add(updaterFeItem);
1124
1125
1126         //BE menu
1127         MenuItem startItem = new MenuItem("[BE] Start");
1128         MenuItem stopItem = new MenuItem("[BE] Stop BackEnd");
1129         MenuItem copyBeWarItem = new MenuItem("[BE] Copy War");
1130         backendMenu.add(startItem);
1131         backendMenu.add(stopItem);
1132         backendMenu.add(copyBeWarItem);
1133
1134         //FE menu
1135         MenuItem startFEItem = new MenuItem("[FE] Start");
1136         MenuItem stopFEItem = new MenuItem("[FE] Stop");
1137         MenuItem copyFeWarItem = new MenuItem("[FE] Copy War");
1138         frontendMenu.add(startFEItem);
1139         frontendMenu.add(stopFEItem);
1140         frontendMenu.add(copyFeWarItem);
1141
1142         //DB menu
1143         MenuItem startDBItem = new MenuItem("[DB] Start");
1144         MenuItem stopDBItem = new MenuItem("[DB] Stop");
1145         MenuItem backupDBItem = new MenuItem("[DB] Backup");
1146         MenuItem restoreDBItem = new MenuItem("[DB] Restore");
1147         dbMenu.add(startDBItem);
1148         dbMenu.add(stopDBItem);
1149         dbMenu.add(backupDBItem);
1150         dbMenu.add(restoreDBItem);
1151         //endregion
1152
1153
1154         MenuItem killItem = new MenuItem("Kill All");
1155         MenuItem startAllItem = new MenuItem("Start All");
1156         MenuItem importItem = new MenuItem("Import Normative");
1157         MenuItem healthInfoItem = new MenuItem("[Info] Health");
1158         MenuItem toggleHealthItem = new MenuItem(toggleHealthString);
1159         MenuItem logsItem = new MenuItem("Logs [Beta]");
1160         MenuItem exitItem = new MenuItem("Exit");
1161
1162         toggleHealthItemView = toggleHealthItem;
1163
1164         popup.add(hotswapItem);
1165         popup?.addSeparator();
1166         popup.add(deployMasterMenu);
1167         popup?.addSeparator();
1168         popup.add(backendMenu)
1169         popup.add(frontendMenu)
1170         popup.add(dbMenu)
1171         popup?.addSeparator();
1172         popup.add(startAllItem);
1173         popup.add(killItem);
1174         popup?.addSeparator();
1175         popup.add(toggleHealthItem);
1176         popup.add(healthInfoItem);
1177         popup?.addSeparator();
1178         popup.add(importItem);
1179         popup.add(logsItem);
1180         popup?.addSeparator();
1181         popup.add(exitItem);
1182         //endregion UI
1183         // construct a TrayIcon
1184         trayIcon = new TrayIcon(image, "HealthTray", popup);
1185
1186         //region -> Button actions
1187         def listenerHotswap = newListener { project.ext.set("IS_HOTSWAP", !IS_HOTSWAP); hotswapItem?.setLabel( IS_HOTSWAP ? "==> HotSwap <==" : "===> WAR <===")   }
1188         // create a action listener to listen for default action executed on the tray icon
1189         def listenerFullBE = newListener { parallel { install_BE(); IS_HOTSWAP ? copyExplodedBE() : copyBE(); restartBackend() } }
1190         def listenerFullFE = newListener { parallel { install_FE(); copyFE() } }
1191         ActionListener listenerFE = newListener { parallel { updaterFE() } }
1192         ActionListener listenerBE = newListener { parallel { updaterBE() } }
1193         ActionListener exitListener = newListener {
1194                     executor?.isShutdown() ?: executor?.shutdown()
1195                     tray.remove(trayIcon);
1196                     project.ext.set("isStopHealthCheck", true)
1197                     println "Shutting down.. bye bye.."
1198                 }
1199         ActionListener stopBackendListener = newListener { stopBackend() }
1200         ActionListener startBEListener = newListener { parallel { startBackend() } }
1201         ActionListener killJavaListener = newListener { killJava() }
1202
1203         ActionListener startAllListener = newListener { parallel { startAll() } }
1204
1205         ActionListener listener5 = new ActionListener() {
1206             public void actionPerformed(ActionEvent e) {
1207                 try {
1208                     parallel { importNormative() }
1209                 } catch (AWTException e1) {
1210                     System.err.println(e1);
1211                 }
1212             }
1213         };
1214
1215         ActionListener listener6 = new ActionListener() {
1216             public void actionPerformed(ActionEvent e) {
1217                 try {
1218                     parallel { healthPopup() }
1219                 } catch (AWTException e1) {
1220                     System.err.println(e1);
1221                 }
1222             }
1223         };
1224
1225         ActionListener listener7 = new ActionListener() {
1226             public void actionPerformed(ActionEvent e) {
1227                 try {
1228                     logBE()//tasks.logger.execute()
1229                 } catch (AWTException e1) {
1230                     System.err.println(e1);
1231                 }
1232             }
1233         };
1234         ActionListener listener8 = new ActionListener() {
1235             public void actionPerformed(ActionEvent e) {
1236                 try {
1237                     toggleHealthPolling()//tasks.logger.execute()
1238                 } catch (AWTException e1) {
1239                     System.err.println(e1);
1240                 }
1241             }
1242         };
1243         ActionListener copyBeWarListener = new ActionListener() {
1244             public void actionPerformed(ActionEvent e) {
1245                 try {
1246                     parallel { copyBE() }//.execute()//tasks.logger.execute()
1247                 } catch (AWTException e1) {
1248                     System.err.println(e1);
1249                 }
1250             }
1251         };
1252
1253         ActionListener startDBListener = new ActionListener() {
1254             public void actionPerformed(ActionEvent e) {
1255                 try {
1256                     parallel {
1257                         startDB()
1258                     }//tasks.logger.execute()
1259                 } catch (AWTException e1) {
1260                     System.err.println(e1);
1261                 }
1262             }
1263         }
1264
1265         ActionListener stopDBListener = new ActionListener() {
1266             public void actionPerformed(ActionEvent e) {
1267                 try {
1268                     stopDB()//tasks.logger.execute()
1269                 } catch (AWTException e1) {
1270                     System.err.println(e1);
1271                 }
1272             }
1273         }
1274
1275         ActionListener dbBackupListener = new ActionListener() {
1276             public void actionPerformed(ActionEvent e) {
1277                 try {
1278                     parallel {
1279                         backupDB()//tasks.logger.execute()
1280                     }
1281                 } catch (AWTException e1) {
1282                     System.err.println(e1);
1283                 }
1284             }
1285         }
1286
1287         ActionListener feStartListener = new ActionListener() {
1288             public void actionPerformed(ActionEvent e) {
1289                 try {
1290                     parallel {
1291                         startFE()
1292                     }//tasks.logger.execute()
1293                 } catch (AWTException e1) {
1294                     System.err.println(e1);
1295                 }
1296             }
1297         }
1298
1299         ActionListener feStopListener = new ActionListener() {
1300             public void actionPerformed(ActionEvent e) {
1301                 try {
1302                     stopFE()//tasks.logger.execute()
1303                 } catch (AWTException e1) {
1304                     System.err.println(e1);
1305                 }
1306             }
1307         }
1308
1309         ActionListener feCopyListener = new ActionListener() {
1310             public void actionPerformed(ActionEvent e) {
1311                 try {
1312                     parallel {
1313                         copyFE() //.execute()//tasks.logger.execute()
1314                     }
1315                 } catch (AWTException e1) {
1316                     System.err.println(e1);
1317                 }
1318             }
1319         }
1320
1321
1322         //def listenerFullModules = newListener { parallel { installAllProject() } }
1323         //region -> Button<=Listener
1324         hotswapItem.addActionListener(listenerHotswap)
1325         updaterFeItem.addActionListener(listenerFE)
1326         updaterFullBeItem.addActionListener( listenerFullBE )
1327         updaterBeWithDependenciesItem.addActionListener( listenerBE )
1328         updaterFullFeItem.addActionListener( listenerFullFE )
1329         stopItem.addActionListener(stopBackendListener)
1330         startItem.addActionListener(startBEListener)
1331         copyBeWarItem.addActionListener(copyBeWarListener);
1332         killItem.addActionListener(killJavaListener)
1333         startAllItem.addActionListener(startAllListener)
1334         importItem.addActionListener(listener5)
1335         healthInfoItem.addActionListener(listener6)
1336         toggleHealthItem.addActionListener(listener8)
1337         logsItem.addActionListener(listener7)
1338         exitItem.addActionListener(exitListener)
1339
1340         startDBItem.addActionListener( startDBListener )
1341         stopDBItem.addActionListener( stopDBListener )
1342         backupDBItem.addActionListener( dbBackupListener )
1343         copyFeWarItem.addActionListener( feCopyListener )
1344         startFEItem.addActionListener( feStartListener )
1345         stopFEItem.addActionListener( feStopListener )
1346         //endregion
1347         //endregion
1348         // set the TrayIcon properties
1349
1350         // ...
1351         // add the tray image
1352         try {
1353             tray.add(trayIcon);
1354
1355         } catch (AWTException e) {
1356             System.err.println(e);
1357         }
1358         // ...
1359     } else {
1360         println "Java TrayIcon Option is not supported in your System, try enabling it. Bye Bye"
1361     }
1362
1363 }
1364
1365 def prepareTray(){
1366     long UPDATE_THRESHOLD = 3500
1367     float SCALAR = 1
1368     ssh.settings {
1369         knownHosts = allowAnyHosts
1370     }
1371     while(!isStopHealthCheck) {
1372         if (!isHaltHealth.get()) {    //if await or await is more then 60 second return health check
1373             ssh.run {
1374                 session(remotes.vagrant) {
1375                     try {
1376                         def healthOutput = execute command[ALL][HEALTH]()
1377                         if (healthOutput?.contains("Failed command .* with status 7") || healthOutput?.contains("Problem accessing /sdc2/rest/healthCheck"))
1378                             updateTray(STATUS.DOWN)
1379                         def statusCollecion = healthOutput?.findAll "\"healthCheckStatus\": \".*\""
1380                         def upCount = statusCollecion?.count { it?.contains("UP") }
1381                         def downCount = statusCollecion?.count { it?.contains("DOWN") }
1382                         def uknownCount = (statusCollecion?.size() - upCount) - downCount
1383                         println " UP -> $upCount | downCount=$downCount | uknownCount=$uknownCount "
1384                         (uknownCount > 0 || (downCount > 0 && upCount > 0)) ? updateTray(STATUS.UNKNOWN) : ((upCount > 0) ? updateTray(STATUS.UP) : updateTray(STATUS.DOWN))
1385                         SCALAR = 1
1386                     } catch (Exception e) {
1387                         updateTray(STATUS.DOWN)
1388                         println e
1389                         SCALAR = Math.min(SCALAR * 1.1, 5) //slow down on errors
1390                     }
1391                     //green color effects
1392                     if (lastStatus && lastStatus == STATUS.UP) {
1393                         trayIcon.setImage(convert(Math.random() > 0.5 ? 'okImg1' : 'okImg2'))
1394                         //randomly green change color
1395                     }
1396                     Thread.yield()
1397                     Thread?.sleep((long) (UPDATE_THRESHOLD * SCALAR))
1398                 }
1399             }
1400         }else{
1401             updateTray(STATUS.UNAVAILABLE)
1402             Thread.yield()
1403         }
1404     }
1405 }
1406
1407 def healthPopup(){
1408     ssh.run {
1409         session(remotes.vagrant) {
1410             def healthOutput = execute command[ALL][HEALTH]()
1411             JOptionPane.showMessageDialog(null,
1412                     healthOutput,
1413                     "HEALTH",
1414                     JOptionPane.INFORMATION_MESSAGE);
1415         }
1416     }
1417 }
1418
1419 project.ext.set("msg", { content -> """
1420                         ************************************************************************************************
1421                         ************************************************************************************************
1422                         *******                                                                                *********
1423                         **************                                                                  ****************
1424                                                 $content      
1425
1426                         ************************************************************************************************
1427                         ************************************************************************************************
1428                         """} )