Back to home page

Enduro/X

 
 

    


0001 #!/usr/bin/pscript
0002 
0003 //Wizard base processor class
0004 class WizardBase {
0005 
0006 
0007         elemOrder = null;
0008         
0009         isDefaulted = false;
0010         
0011         //Calculated min/max order
0012         minOrder = 99;
0013         
0014         maxOrder = -1;
0015         
0016         new_val = "";
0017         
0018         shared_lib_pfx = "so";
0019 
0020         //Queue type...
0021         qType = "";
0022 
0023         //Should come from packages
0024         osType = "";
0025         
0026         //Application home directory (i.e. current dir)
0027         appHome = ".";
0028 
0029         //Queue path
0030         qpath = "";
0031 
0032         //??
0033         mk_mq_dev = false;
0034 
0035         //This is constructor, we shall get some infos from Enduro/X base build
0036         constructor() {
0037                 osType = getosname();
0038                 //qType = "posixq";
0039                 
0040                 //Set app home to pwd
0041                 appHome = getcwd();
0042                 
0043                 elemOrder = this.getSortedParamArray();
0044                 
0045                 //Set shared lib path...
0046                 if (getpoller()=="emq")
0047                 {
0048                         qpath="/tmp/mq";
0049                         mk_mq_dev = true;
0050                 }
0051 
0052                 if (osType=="DARWIN")
0053                 {
0054                         shared_lib_pfx="dylib";
0055                 }
0056                 else if (osType=="CYGWIN")
0057                 {
0058                         shared_lib_pfx="dll";
0059                 }
0060                 else if (osType=="SUNOS")
0061                 {
0062                         //If we have a Solaris, then it is tmp...
0063                         qpath = "/tmp";
0064                 }
0065                 else if (osType=="AIX")
0066                 {
0067                         qpath="/tmp";
0068                         //system v does not need it..
0069                         //mk_mq_dev = true;
0070                 }
0071                 
0072                 /* Load the overrides from cli */
0073                 local root = getroottable();
0074                 if (root.rawin("args"))
0075                 {
0076                         foreach(member,val in this.getclass())
0077                         {
0078                                 
0079                                 if (root.args.rawin(member))
0080                                 {
0081                                         /* set the value from member... */
0082                                         this[member] = root.args[member];
0083                                 }
0084                         }
0085                 }
0086         }
0087         
0088         //Print the value entry line
0089         function printEntryLine(pfx, member) {
0090         
0091                 local attr = this.getclass().getattributes(member);
0092                 ::print(format("%2s:%s %-12s :%s [%s]: ", ""+attr["order"], pfx,
0093                                 member, attr["name"], this[member]));
0094                                 
0095                 ::userlog(format("%2s:%s %-12s :%s [%s]: ", ""+attr["order"], pfx,
0096                                 member, attr["name"], this[member]));
0097         }
0098         
0099         //Validate the value of field
0100         //@param member Class member to attributes from
0101         //@param value  Value to test validity for this field
0102         function validateValue(member, value)
0103         {
0104         
0105                 local attr = this.getclass().getattributes(member);
0106                 local empty = false;
0107                 if (attr["type"]=="number")
0108                 {
0109                         try
0110                         {
0111                                 if (attr.rawin("min"))
0112                                 {
0113                                         if ((value.tointeger()) < (attr["min"]))
0114                                         {
0115                                                 print("Invalid value: Min length "+
0116                                                         attr["min"]+"\n");
0117                                                 return 0;
0118                                         }
0119                                 }
0120                                 
0121                                 if (attr.rawin("max"))
0122                                 {
0123                                         if ((value.tointeger()) > (attr["max"]))
0124                                         {
0125                                                 print("Invalid value: Max length "+
0126                                                         attr["max"]+"\n");
0127                                                 return 0;
0128                                         }
0129                                 }
0130                                 
0131                                 local test = value.tointeger();
0132                         }
0133                         catch (e)
0134                         {
0135                                 print("Invalid number!\n");
0136                                 return 0;
0137                         }
0138                 }
0139                 else
0140                 {
0141                         if (attr.rawin("min"))
0142                         {
0143                         
0144                                 if (value.len() < attr["min"])
0145                                 {
0146                                         print("Invalid value: Min length "+attr["min"]+"\n");
0147                                         return 0;
0148                                 }
0149                                 
0150                                 if (0==attr["min"])
0151                                 {
0152                                         empty=true;
0153                                 }
0154                         }
0155                         
0156                         if (attr.rawin("max"))
0157                         {
0158                                 if (value.len() > (attr["max"]+0))
0159                                 {
0160                                         print("Invalid value: Max length "+attr["max"]+"\n");
0161                                         return 0;
0162                                 }
0163                         }
0164                 }
0165                 
0166                 //Check for regex
0167                 if (!empty && attr.rawin("regex"))
0168                 {
0169                 
0170                         local ex = regexp(attr["regex"]);
0171                         
0172                         if (!ex.match(value))
0173                         {
0174                                 print("Invalid value: Does not match expression: ["+
0175                                         attr["regex"]+"]\n");
0176                                 return 0;
0177                         }
0178                 }
0179                 
0180                 if (attr["type"]=="yn")
0181                 {
0182                 
0183                         local ex = regexp("^[ynYN]$");
0184                         
0185                         if (!ex.match(value))
0186                         {
0187                                 print("Error: Y/N?\n");
0188                                 return 0;
0189                         }
0190                         
0191                         value = value.tolower();
0192                 }
0193                 
0194                 
0195                 //Temporary storage to return the value
0196                 new_val = value;
0197                 
0198                 return 1;
0199         }
0200         
0201         //Returns elements in sorted order
0202         function getSortedParamArray()
0203         {
0204                 local elemOrder = [];
0205                 local clazz = this.getclass();
0206                 //prov.install.CheckLimits();
0207 
0208                 //Sort the fields
0209                 foreach(member,val in this.getclass())
0210                 {
0211                         local attr = this.getclass().getattributes(member);
0212                         
0213                         if (null!=attr)
0214                         {       
0215                                 if (attr["order"]<minOrder)
0216                                 {
0217                                         minOrder = attr["order"];
0218                                 }
0219                                 
0220                                 if (attr["order"]>maxOrder)
0221                                 {
0222                                         maxOrder = attr["order"];
0223                                 }
0224 
0225                                 elemOrder.append(member);
0226                         }
0227                 }
0228 
0229                 //Now sort the array
0230                 elemOrder.sort(function(a,b)
0231                 {
0232                         local attrA = clazz.getattributes(a);
0233                         
0234                         local attrB = clazz.getattributes(b);
0235 
0236                         return attrA["order"] <=> attrB["order"];
0237                 } );
0238                 
0239                 return elemOrder;
0240         }
0241         
0242         
0243         //Test is field actual of the object
0244         //@param member Field to test for need
0245         //@return TRUE field actual for user intervetion, FALSE not needed for user.
0246         function isFieldActual(member)
0247         {
0248                 local attr;
0249                 
0250                 attr = this.getclass().getattributes(member);
0251                 
0252                 //Check that dependency is ok
0253                 if (attr.rawin("depend"))
0254                 {
0255                         local compiledscript = compilestring("return "+attr["depend"]+";");
0256                         return compiledscript();
0257                 }
0258                 else
0259                 {
0260                         return true;
0261                 }
0262         }
0263         
0264         //Enter the field value from console
0265         //@param member         Field to enter.
0266         function enterField(member)
0267         {
0268                 local ok = false;
0269                                 
0270                 while (!ok)
0271                 {
0272                         this.printEntryLine("", member);
0273 
0274                         local new_val  = getline();
0275                         
0276                         if (new_val=="")
0277                         {
0278                                 new_val = this[member];
0279                         }
0280                         
0281                         //Validate input...
0282                         if (this.validateValue(member, new_val))
0283                         {
0284                                 this[member] = this.new_val;
0285                                 ok = true;
0286                         }
0287                 }
0288         }
0289         
0290         //Run the runInteractive configuration
0291         //@return TRUE OK to provision (accepted), FALSE - not ok
0292         function runInteractive()
0293         {
0294                 //Run the wizard...
0295                 //Iterate over the all paramters
0296                 //foreach(member,val in Provision)
0297                 foreach(member in elemOrder)
0298                 {
0299                         if (this.isFieldActual(member))
0300                         {
0301                                 enterField(member);
0302                         }
0303                 }
0304 
0305                 //OK to provision..
0306                 return true;
0307         }
0308         
0309         
0310         //Get element name by order
0311         //@param order  Order number of attributes
0312         //@return       element name, empty if not found
0313         function getMemberByOrder(order)
0314         {
0315                 local ex = regexp(@"[0-9]+");
0316                 
0317                 if (!ex.match(order))
0318                 {
0319                         return ""; /* invalid number... */
0320                 }
0321         
0322                 foreach(member,val in this.getclass())
0323                 {
0324                         local attr = this.getclass().getattributes(member);
0325                         
0326                         if (null!=attr)
0327                         {       
0328                                 if (attr["order"] == order.tointeger())
0329                                 {
0330                                         if (this.isFieldActual(member))
0331                                         {
0332                                                 return member;
0333                                         }
0334                                         else
0335                                         {
0336                                                 return ""; /* currently not editable... */
0337                                         }
0338                                                 
0339                                 }
0340                         }
0341                 }
0342                 
0343                 return "";
0344         }
0345         
0346         
0347         //Print the actual configuration
0348         //@return true - config ok, false - config bad
0349         function printConfig()
0350         {
0351                 local first = true;
0352                 //If not defauled, run over the fields and ask for values..
0353                 if (!isDefaulted)
0354                 {
0355                         foreach(member in this.elemOrder)
0356                         {
0357                                 if (this.isFieldActual(member))
0358                                 {
0359                                         if (!this.validateValue(member, this[member]))
0360                                         {
0361                                                 
0362                                                 if (first)
0363                                                 {
0364                                                         print("*** New dependency fields needs to be filled! ***\n");
0365                                                         first = false;
0366                                                 }
0367                                                 //Enter the value
0368                                                 enterField(member);
0369                                         }
0370                                 }
0371                         }
0372                 }
0373                 
0374                 
0375                 //Run the wizard...
0376                 //Iterate over the all paramters
0377                 //foreach(member,val in Provision)
0378                 foreach(member in this.elemOrder)
0379                 {
0380                         if (this.isFieldActual(member))
0381                         {
0382                                 
0383                                 if (!this.validateValue(member, this[member]))
0384                                 {
0385                                         return false;
0386                                 }
0387                         
0388                                 //Validate input...
0389                                 //Print the field value
0390                                 this.printEntryLine(" Edit", member);
0391                                 print("\n");
0392                         }
0393                 }
0394                 
0395                 return true;
0396         }
0397         
0398         
0399         //Validate and print config
0400         function validatAndPrintConfig()
0401         {
0402                 while (1)
0403                 {
0404                         print("\n");
0405                         print("*** Review & edit configuration ***\n");
0406                         print("\n");
0407                         
0408                         if (!printConfig())
0409                         {
0410                                 return false;
0411                         }
0412                                         
0413                         //If not defauled, then allow to edit the some entry...
0414                         if (!isDefaulted)
0415                         {
0416                                 //Offer entry...
0417                                 ::print("c: Cancel\n");
0418                                 ::print("w: Accept, write\n");
0419                                 ::print("Enter the choice ["+minOrder+"-"+maxOrder+", c, w]: ");
0420                                 
0421                                 local input = getline();
0422                                 
0423                                 userlog("User entered: "+input);
0424                                 if (input=="c" || input=="C")
0425                                 {
0426                                         userlog("Terminating...");
0427                                         return false;
0428                                 }
0429                                 else if (input=="w" || input=="W")
0430                                 {
0431                                         userlog("Accepted");
0432                                         return true;
0433                                 }
0434                                 else if (input!="")
0435                                 {
0436                                         /* Check the range */
0437                                         local ok = false;
0438                                         
0439                                         local member = "";
0440                                         while (!ok)
0441                                         {
0442                                                 member = getMemberByOrder(input);
0443                                                 
0444                                                 if (member=="")
0445                                                 {
0446                                                         ::print("Invalid entry\n");
0447                                                         ::print("Enter the choice ["+
0448                                                                 minOrder+"-"+maxOrder+", 91, 92]: ");
0449                                                         input = getline();
0450                                                 }
0451                                                 else
0452                                                 {
0453                                                         ok = true;
0454                                                 }
0455                                         }
0456                                         
0457                                         /* edit the field */
0458                                         enterField(member);
0459                                 }       
0460                         }
0461                         else
0462                         {
0463                             break;
0464                         }
0465                 }
0466                 
0467                 //OK to provision..
0468                 return true;
0469         }
0470         
0471                 //Write the generate files
0472         //@param filename       Filename to write to
0473         //@param contents (string) to write to
0474         //@return true - ok, false - failed.
0475         function writeFile(filename, contents)
0476         {
0477                 if (!fileexists(filename))
0478                 {
0479                         userlog(format("Writting off [%s] with [%s]", 
0480                         filename, contents));
0481                         try
0482                         {
0483                                 local out = file(filename,"w");
0484                                 out.writes(contents);
0485                                 out.close();
0486                         }
0487                         catch (e)
0488                         {
0489                                 userlog(format("Got exception [%s]", e));
0490                                 
0491                                 print(e+"\n");
0492                                 
0493                                 return false;
0494                         }
0495                 }
0496                 else
0497                 {
0498                         userlog(format("File [%s] exists - not writting.",  filename));
0499                 }
0500                 
0501                 return true;
0502         }
0503         
0504         //Set exec permissions to file
0505         //@param filename       filename
0506         function setExec(filename)
0507         {
0508                 userlog(format("Setting 755 to [%s]",  filename));
0509                 try
0510                 {
0511                         chmod(filename, "755");
0512                 }
0513                 catch (e)
0514                 {
0515                         userlog(format("Got exception [%s]", e));
0516                         
0517                         print(e+"\n");
0518                         
0519                         return false;
0520                 }
0521                 
0522                 return true;
0523         }
0524 
0525 }
0526