Bash script to add menuitems to Elastix

Discussion in 'General' started by ramoncio, Dec 9, 2008.

  1. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    I have built a bash script to add to Elastix´s menus many programs that I've added.
    Here it is:

    Code:
    #! /usr/bin/env bash
    
    # Add items to Elastix's menus
      
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_1
    insert into menu values('webmin', 'system', 'https://192.168.1.100:10000', 'Webmin', 'framed');
    SQL_ENTRY_TAG_1
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_2
    insert into acl_resource values(150, 'webmin', 'Webmin');
    insert into acl_group_permission values(170, 1, 1, 150);
    SQL_ENTRY_TAG_2
    
    
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_3
    insert into menu values('nagios', 'reports', 'nagios/', 'Nagios', 'framed');
    SQL_ENTRY_TAG_3
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_4
    insert into acl_resource values(151, 'nagios', 'Nagios');
    insert into acl_group_permission values(171, 1, 1, 151);
    SQL_ENTRY_TAG_4
     
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_5
    insert into menu values('munin', 'reports', 'reportesguaposmunin/', 'Munin', 'framed');
    SQL_ENTRY_TAG_5
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_6
    insert into acl_resource values(152, 'munin', 'Munin');
    insert into acl_group_permission values(172, 1, 1, 152);
    SQL_ENTRY_TAG_6
     
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_7
    insert into menu values('backuppc', 'system', 'backuppc/', 'BackupPC', 'framed');
    SQL_ENTRY_TAG_7
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_8
    insert into acl_resource values(153, 'backuppc', 'BackupPC');
    insert into acl_group_permission values(173, 1, 1, 153);
    SQL_ENTRY_TAG_8
    
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_9
    insert into menu values('phpmyadmin','extras','phpmyadmin','phpMyAdmin','framed');
    SQL_ENTRY_TAG_9
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_10
    insert into acl_resource values(154, 'hud', 'HUD');
    insert into acl_group_permission values(174, 1, 1, 154);
    SQL_ENTRY_TAG_10
    
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_11
    insert into menu values('hud', 'im', 'modules/hudadmin/hudadmin.php', 'HudLite', 'framed');
    SQL_ENTRY_TAG_11
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_12
    insert into acl_resource values(155, 'hud', 'HUD');
    insert into acl_group_permission values(175, 1, 1, 155);
    SQL_ENTRY_TAG_12
    
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_13
    insert into menu values('avantfax', 'fax', 'avantfax/', 'Avantfax', 'framed');
    SQL_ENTRY_TAG_13
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_14
    insert into acl_resource values(156, 'avantfax', 'Avantfax');
    insert into acl_group_permission values(176, 1, 1, 156);
    SQL_ENTRY_TAG_14
    
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_15
    insert into menu values('monit', 'reports', 'http://192.168.1.100:2812/', 'Monit', 'framed');
    SQL_ENTRY_TAG_15
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_16
    insert into acl_resource values(157, 'monit', 'Monit');
    insert into acl_group_permission values(177, 1, 1, 157);
    SQL_ENTRY_TAG_16
    
    You can easily adapt it for your programs.
    I have tried it in Elastix 1.3.2 and it works ok. I think it should work in any elastix version after 0.9, as they have sqlite 3 since then (correct me if I'm wrong, maybe it was 0.8)
    Have a look to the thread that explains in detail how to add items to menus, so you can understand what these values are for.
    In the future when the elastix's crew will add more items to the menu, maybe you'll have to change some values in acl table for the script to work.
     
  2. rafael

    Joined:
    May 14, 2007
    Messages:
    1,454
    Likes Received:
    1
    Nice I really like it. With new developer module it won't be hard to call a modified version of this script from the web interfase and just ask for the variables.

    I would try to make a simple module for this.

    Thanks Ramoncio +1 to your karma ;)

    Ragards,

    Rafael
     
  3. wiseoldowl

    Joined:
    Aug 19, 2008
    Messages:
    251
    Likes Received:
    0
    I commented out everything but the above and ran it, completely missing the fact that the IP address and port number was wrong for our system. So I ran it again with the correct IP and port, and it appears to have worked. So far, so good. But then I wanted to add phpMyAdmin. Since it was the only other thing I was adding, I did this:

    Code:
    #! /usr/bin/env bash 
     
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_3 
    insert into menu values('phpmyadmin','extras','modules/dbadmin/','phpMyAdmin','framed'); 
    SQL_ENTRY_TAG_3 
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_4 
    insert into acl_resource values(151, 'phpmyadmin', 'phpMyAdmin'); 
    insert into acl_group_permission values(171, 1, 1, 151); 
    SQL_ENTRY_TAG_4
    
    I changed the path to modules/dbadmin/ because that's where it is on our system. But this time, no added link under the "extras" menu - all I see there are vTigerCRM, Calling Cards, Downloads, and SugarCRM. And when I ran the script I got these errors:

    Code:
    SQL error: near "SQL_ENTRY_TAG_3": syntax error
    SQL error: no such table: acl_group_permission
    Incomplete SQL: SQL_ENTRY_TAG_4
    
    Any thoughts?
     
  4. wiseoldowl

    Joined:
    Aug 19, 2008
    Messages:
    251
    Likes Received:
    0
    This was going to be an edit to my previous message but for some reason the paragraphs kept getting messed up...

    After working with this a bit more I realized a couple of things. First of all, when I didn't use the script but entered each line manually, leaving off the <<SQL_ENTRY_TAG_x and the SQL_ENTRY_TAG_x parts (and of course changing references and paths to fit the reality of our system) everything worked - except that after I had done the first insert, I had to do a .quit (note the leading period) to exit the menu database before I could go into the acl database. This was the main reason I was getting errors - it was still trying to do the last inserts into the menu database and not finding the acl_resource or acl_group_permission tables it spit out error messages.

    Beyond that I also discovered that sqlite doesn't honor # as a comment tag (so remove any sections you don't want, don't just comment them out) and that my assumption that it was necessary to change the numbers was incorrect - they don't have to be sequential as long as they don't conflict with an existing value. I learned a bit by looking at the page at http://souptonuts.sourceforge.net/readm ... orial.html

    So if anyone else is having trouble making this work, TRY this:

    • Copy the original script into a text editor
    • REMOVE (don't just comment out) any sections that don't apply to you
    • Change the target URL(s) and/or path(s) to match what you are using on your system, if necessary (note that programs are assumed to be off the /var/www/html directory so your path should be relative to that - for example for phpMyAdmin I used
      Code:
      insert into menu values('phpmyadmin','extras','modules/dbadmin/','phpMyAdmin','framed');
      note the position of the / characters.
    • If installing phpMyAdmin, note that there is an error in the "insert into acl_resource values" line (wrong program name), you'll want to fix that.
    • I believe you will need to insert a .quit statement (on a line by itself) just before every SQL_ENTRY_TAG_x line (NOT the lines that have <<SQL_ENTRY_TAG_x). That is because you are changing databases and you need to quit one before you can access another!
     
  5. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    I discovered that sqlite could be used by bash scripts a couple of days ago.
    Then I made this script to add all my installed addons to new installations, and wanted to share it with this community.
    But it is not a very good script, just to show you that bash can do the job.

    It would be nice to check the current values in menu.db and acl.db before trying to insert any record, and use values that won't conflict.

    One of the main problems is that if you get an error, you'd better check the tables and delete the inserted records before trying to run the script again.


    You can also check the records from the script too.

    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_1
    select * from acl_resource;
    SQL_ENTRY_TAG_1

    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_2
    select * from acl_group_permission;
    SQL_ENTRY_TAG_2


    I'll try figure out how to check the acl.db values using the script before inserting any record, and maybe even check the menu records too in order to avoid wrong insert attempts.

    I think it should be quite easy, as Rafael says, to make a small bash script that asks you for program name, path, and even desired permissions and then checks the acl.db existing records to automatically insert the menu item using the right values.

    I'll have to learn some more bash programming. :)
     
  6. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    Here is a nice sqlite tutorial
    http://souptonuts.sourceforge.net/readm ... orial.html

    Niiice, you can insert values like this:

    sqlite3 test.db "create table t1 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE);"
    sqlite3 test.db "insert into t1 (data,num) values ('This is sample data',3);"

    and the primary key "t1key" autoincrements.
     
  7. rafael

    Joined:
    May 14, 2007
    Messages:
    1,454
    Likes Received:
    1
    Hi Ramoncio you can edit your script with something like this

    Code:
    #!/usr/bin/bash
    
    module_id=\'$1\'
    parent_menu=\'$2\'
    url=\'$3\'
    menu_name=\'$4\'
    
    # Add items to Elastix's menus 
    sqlite3 /var/www/db/menu.db <<SQL_ENTRY_TAG_1 
    insert into menu values('$module_id', '$parent_menu', '$url', '$menu_name', 'framed'); 
    SQL_ENTRY_TAG_1 
    sqlite3 /var/www/db/acl.db <<SQL_ENTRY_TAG_2 
    insert into acl_resource values(150, '$module_id', '$menu_name'); 
    
    #not sure what this values are and if they should be variables.
    
    insert into acl_group_permission values(170, 1, 1, 150); 
    SQL_ENTRY_TAG_2 
    
    
    A brief explination $1 is going to be the first argument, $2 the second and so forth. So when you call the script you should do something like this. (if the script has the name create_menu)

    Code:
    create_menu webmin System http://ip:10000 Webmin
    
    So $1= menu, $2=System, $3=http://ip:1000 and $4=Webmin. Note, I haven't test it yet, but just want to give you some ideas where to go so your improvements would be nice ;). BTW after the shell script is done you can follow this how to as a reference to create a module in web interfase that calls this script.
     
  8. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    Here is the script.
    You just have to follow instructions and all the data is inserted checking the right values in acl.db.

    Code:
    #! /bin/bash
    echo -n "Insert the name for the submenu item:  "
    read -e NOMBRE
    sqlite3 /var/www/db/menu.db "select distinct IdParent from menu;"
    echo -n "Insert the main menu parent tab where your item will be inserted. You must select from the above values:  "
    read -e PADRE
    echo -n "Insert the link to your new app:  "
    read -e ENLACE
    sqlite3 /var/www/db/menu.db "select distinct TYPE from menu;"
    echo -n "Insert type. You must select from the above values:  "
    read -e TIPO
    echo -n "Insert a short description:  "
    read -e DESCRIPCION
    IDENTIFICADOR=${NOMBRE// /_}
    last_resource=`sqlite3 /var/www/db/acl.db "select * from acl_resource;" | awk -F "|" '1 { print  $1 }' | tail -1`
    resource_to_use=`expr $last_resource + 1`
    last_permission=`sqlite3 /var/www/db/acl.db "select * from acl_group_permission;" | awk -F "|" '1 { print  $1 }' | tail -1`
    group_permission_to_use=`expr $last_permission + 1`
    sqlite3 /var/www/db/menu.db  "insert into menu (id,IdPArent,Link,Name,Type) values ('$IDENTIFICADOR','$PADRE','$ENLACE','$NOMBRE','$TIPO');"
    sqlite3 /var/www/db/acl.db  "insert into acl_resource (id,name,description) values ('$resource_to_use','$NOMBRE','$DESCRIPCION');"
    sqlite3 /var/www/db/acl.db  "insert into acl_group_permission (id,id_action,id_group,id_resource) values ('$group_permission_to_use',1,1,'$resource_to_use');"
    
    
     
  9. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    I want to change IDENTIFICADOR=$NOMBRE , I want IDENTIFICADOR to be the same string as NOMBRE, but changing spaces for _ and in non capital letters, but I still have much to learn. EDIT: The spaces are now replaced by _ It took me some reading to get everything in its place.
    Uff, awk is huge.
    Now I see that I could have done it withouw awk, using something like:

    sqlite3 /var/www/db/acl.db "select distinct id from acl_resource;" |tail -1

    But I still didn't know much about sqlite3.
     
  10. bmacias

    Joined:
    Sep 27, 2007
    Messages:
    205
    Likes Received:
    0
    Hello:

    The function en sqlite3 for lower case is "lower". ex "select lower('BRUNO');"
    In you case ......lower('$IDENTIFICADOR',.....

    In the part in tables acl_resource and acl_group_permission you can use:

    sqlite3 /var/www/db/acl.db "insert into acl_resource(name,description) values (lower('$IDENTIFICADOR'),'$NOMBRE'); insert into acl_group_permission values (1,1,(select last_insert_rowid()));" this is more automatic.
    ;)
     
  11. ramoncio

    Joined:
    May 12, 2010
    Messages:
    1,663
    Likes Received:
    0
    Thanks for the notes Bruno, but this is not exactly what I wanted to do.
    I wanted the script to automatically modify the variable $NOMBRE manually entered by the user and store its value into the IDENTIFICADOR variable (changing spaces for "_" and using non capital letters) before importing them to the sqlite database.
    But I've noticed it is not needed.
    I wanted to do this because in the field "id" from the table "menu" in menu.db, all records have nor spaces nor capitals, but if you store strings with spaces and capitals, it gets them ok, and there is no noticeable problems.
    I've also noticed that this table has no primary key... I'm not sure if this is bad, but I suppose it is not very good.

    EDIT: You are right Bruno, you said to apply the lower in the insert. This is cool too. :)
     

Share This Page