Koozali.org: home of the SME Server

beginner db question

Offline JoshuaR

  • ***
  • 125
  • +0/-0
    • Tech-Eze
beginner db question
« on: May 13, 2010, 08:39:59 AM »
Hi,
Please bear with me as I'm new to developing contribs  :-P   (I have read through the wiki developer file--several times :grin: )
I've created a database file under /etc/e-smith/db/configuration/defaults/mycontribname/file.conf
This file is updated by the web interface. I need to get the data from that file into my contrib template (the database file is the entire content of what needs to be in the template), now in the developer guide I can see something like the following:
Code: [Select]
{
    $OUT .= " ${mycontribname}";
}
The problem is, the database file is a multi-line file (and it needs to be), and when the template creates the final file, the multiple lines that were in the database file are all in one line now, eg,
Code: [Select]
|mycontrib|somedataline1\nsomedataline2 I know that \n is a line break which is why the code is producing it. Does anyone know how I can pull the multiple line db file into my template while keeping the correct multiple line format in tact? On a side note that code is also prepending the actual db file name into the final file as well. Any help would be appreciated  :D

Thanks,
--Josh R
Life's tragedy is that we get old too soon, and wise too late...

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: beginner db question
« Reply #1 on: May 13, 2010, 08:53:54 AM »
Hi,
Please bear with me as I'm new to developing contribs  :-P   (I have read through the wiki developer file--several times :grin: )
I've created a database file under /etc/e-smith/db/configuration/defaults/mycontribname/file.conf
You are doing it wrong fundamentally. The defaults file are used to initialize the database, you have understood that properly. Each directory directly under the defaults tree is a database key, each file in that directory is a property. The file reflecting the property contains one (the default) value in plain text:

Code: [Select]
/etc/e-smith/db/configuration/defaults/
+key1
| +prop1
| +prop2
| +propn
+key2
| +prop1
| +prop2
| +propn
+keyn
| +prop1
| +prop2
| +propn
...

For instance the default values for passwordstrength, documented in the wiki, look like this:
Code: [Select]
[root@smetest defaults]# ls -la passwordstrength/
total 24
drwxr-xr-x   2 root root 4096 May 11 11:24 .
drwxr-xr-x 114 root root 4096 Mar 14  2006 ..
-rw-r--r--   1 root root    7 Nov 21  2005 Admin
-rw-r--r--   1 root root    7 Nov 21  2005 Ibays
-rw-r--r--   1 root root   14 Nov 21  2005 type
-rw-r--r--   1 root root    7 Nov 21  2005 Users
[root@smetest defaults]# cat passwordstrength/type
configuration
[root@smetest defaults]# cat passwordstrength/Admin
strong
[root@smetest defaults]# cat passwordstrength/Users
strong
[root@smetest defaults]# cat passwordstrength/Ibays
strong
[root@smetest defaults]#

The problem is, the database file is a multi-line file (and it needs to be), and when the template creates the final file, the multiple lines that were in the database file are all in one line now, eg,
Code: [Select]
|mycontrib|somedataline1\nsomedataline2 I know that \n is a line break which is why the code is producing it.
Does it really need to be that way? What are you trying to achieve? Could you elaborate some more on that? Perhaps we can help you find a way which makes it easier or better suited.

Does anyone know how I can pull the multiple line db file into my template while keeping the correct multiple line format in tact? On a side note that code is also prepending the actual db file name into the final file as well. Any help would be appreciated  :D
Not from the top off my head as normally on SME Server database properties are single values, not containing line breaks.
« Last Edit: May 13, 2010, 09:03:13 AM by cactus »
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline JoshuaR

  • ***
  • 125
  • +0/-0
    • Tech-Eze
Re: beginner db question
« Reply #2 on: May 13, 2010, 09:11:28 AM »
Quote
Does it really need to be that way? What are you trying to achieve? Could you elaborate some more on that? Perhaps we can help you find a way which makes it easier or better suited.
I should have said as far as I can understand :P
The file is an access control list, that contains data like the following:
Code: [Select]
192\.168\.99\.44 50Mb/day 20h/week
192\.168\.99\.45 500Mb/week
192\.168\.99\.46 24h/day
I can't really think of how I could create individual keys for each, as I won't know how many entries will be created by the user.

Am I correct in that any data I want to pull from my web panel has to go to a db file before I can pull it into my template and generate my final config file?
« Last Edit: May 13, 2010, 09:15:11 AM by JoshuaR »
Life's tragedy is that we get old too soon, and wise too late...

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: beginner db question
« Reply #3 on: May 13, 2010, 09:22:12 AM »
I should have said as far as I can understand :P
No problem.

The file is an access control list, that contains data like the following:
Code: [Select]
192\.168\.99\.44 50Mb/day 20h/week
192\.168\.99\.45 500Mb/week
192\.168\.99\.46 24h/day
I can't really think of how I could create individual keys for each, as I won't know how many entries will be created by the user.
Why not make each IP a key and create properties per key like this for you example:

Judging from above you are trying to cater for some sort of internet cafe like access system, right?

I suggest you make a key for each IP number, with a type of host (which is a property in fact as well) and the following properties:
  • LimitWkMb
  • LimitWkHrs
  • LimitDayMb
  • LimitDayHrs

Since I guess you do not want to setup any host by default you do not need to create default entries at all. All you need to do is to store a record using the page you use to query the data from the user (I am guessing you are making a server-manager panel as well).
« Last Edit: May 13, 2010, 09:24:35 AM by cactus »
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: beginner db question
« Reply #4 on: May 13, 2010, 09:27:01 AM »
Am I correct in that any data I want to pull from my web panel has to go to a db file before I can pull it into my template and generate my final config file?
Yes.
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline JoshuaR

  • ***
  • 125
  • +0/-0
    • Tech-Eze
Re: beginner db question
« Reply #5 on: May 13, 2010, 11:07:13 AM »
I guess I'm just not too sure how to get the server manager web panel to handle different kinds of data. At the moment the setup is easy because it's just writing a full text file (like the loginscript rpm).
The only thing is that it's not necessarily going to be an ip. The contrib is an acl for squid that allocates a download quota for either ip, user (if squid_auth is setup), and a catchall)

there's also no set number of, umm trailing commands.
Ie I could have 192.168.1.50 5mb/day or even 192.168.1.50 5mb/day 2hr/day

would it work for that do you think?

Thanks for the help btw
« Last Edit: May 13, 2010, 11:13:15 AM by JoshuaR »
Life's tragedy is that we get old too soon, and wise too late...

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: beginner db question
« Reply #6 on: May 13, 2010, 11:28:00 AM »
I guess I'm just not too sure how to get the web panel to handle different kinds of data. At the moment the setup is easy because it's just writing a full text file (like the loginscript rpm).
That totally depends on the wishes and plans you have with the contrib. But writing total files is a bad thing, as the templates normally should take care of that. Perhaps the loginscript is an exception because of the wide range of instructions that can be set, but for the purpose of generating configuration files as you seem to be doing, the template engine in combination with the database is the best and preferred way to go.

The only thing is that it's not necessarily going to be an ip. The contrib is an acl for squid that allocates a download quota for either ip, user (if squid_auth is setup), and a catchall)
That should not have to be a problem as you could easily store other types in your database, for instance host/ip and/or user. The templates then could cater for a catch all which you can define in your templates, but also in the database if you like.

there's also no set number of, umm trailing commands.
Ie I could have 192.168.1.50 5mb/day or even 192.168.1.50 5mb/day 2hr/day
I guess there is, somehow as I can not imagine that a user could have two limits for a day or a week, therefore the maximum number of parameters is limited to the number of limits you are defining, but my guess is the two major categories would be total amount of traffic allowed (in whatever unit of bytes) and the total amount of access time per period (which can be as finely grained as you would like it, e.g. mins/hr, hrs/day, days/week, weeks/month, months/year, but also seconds/year if you really desire. You could also opt to store the limit amount and the limit period in a different property, per key,for instance:

Code: [Select]
Limit = 10
LimitAmount = days
LimitPeriod = 1
LimitPeriodAmount = month

or:

Code: [Select]
Limit = 5
LimitAmount = Gb
LimitPeriod = 1
LimitPeriodAmount = year

or:

Code: [Select]
Limit = 3
LimitAmount = hrs
LimitPeriod = 1
LimitPeriodAmount = day

The logic to generate the required rules should be in your templates. The database is only used to store the data so it can be retrieved from the database at the time the templates are expanded and the configuration files are being generated.

By using above approach you get a very clear and simple interface as the administrator only has to select four items to define the limits for the rule, two of them (the amounts) can be free text fields, the other two could be drop down lists to prevent administrators from entering data you can not handle or your templates do not support. The use of dropdown lists also prevent typos which might render your configuration files with errors, preventing rules from working, or worse a whole service not working as intended.

Thanks for the help btw
You're welcome. I suggest you think through your plans properly before starting out the coding process. If you are looking for certain methods there is the SME Server Developers Guide, which you already found, the perldoc command for the different esmith::DB classes as well as the already written code in other contribs, or perhaps better the SME Server's own code to learn and borrow from.

As a last peace of advice: Not that we do not want to support you in the forums, but for development we have a special mailinglist: http://lists.contribs.org/mailman/listinfo/devinfo . Perhaps it is easier to subscribe to that list and ask your questions there.
« Last Edit: May 13, 2010, 11:38:04 AM by cactus »
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: beginner db question
« Reply #7 on: May 13, 2010, 11:34:52 AM »
there's also no set number of, umm trailing commands.
Ie I could have 192.168.1.50 5mb/day or even 192.168.1.50 5mb/day 2hr/day
It seems I overlooked something in my previous reply, you could cater for above scenario by extending the database properties provided in my previous post by something like this for instance:
Code: [Select]
LimitTime = 10
LimitTimeAmount = days
LimitTimePeriodAmount = month
LimitTimePeriod = 1
LimitData = 10
LimitDataAmount = Gb
LimitDataPeriodAmount = month
LimitDataPeriod = 1
« Last Edit: May 13, 2010, 11:39:29 AM by cactus »
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline JoshuaR

  • ***
  • 125
  • +0/-0
    • Tech-Eze
Re: beginner db question
« Reply #8 on: May 13, 2010, 02:46:55 PM »
Thanks cactus...looks like I'll have to do more study before I go any further  :(
Life's tragedy is that we get old too soon, and wise too late...

Offline JoshuaR

  • ***
  • 125
  • +0/-0
    • Tech-Eze
Re: beginner db question
« Reply #9 on: June 23, 2010, 12:49:30 PM »
I've raised a bug to track the progress as I write this: http://bugs.contribs.org/show_bug.cgi?id=6077
Excuse the question, but to use the devinfo mailing list I just email devinfo@lists.contribs.org with my question/topic? I've signed up to the mailing list.
« Last Edit: June 23, 2010, 12:54:06 PM by JoshuaR »
Life's tragedy is that we get old too soon, and wise too late...

Offline janet

  • ****
  • 4,812
  • +0/-0
Re: beginner db question
« Reply #10 on: June 23, 2010, 02:38:58 PM »
Quote from: JoshuaR

[quote
...to use the devinfo mailing list I just email devinfo@lists.contribs.org with my question/topic? I've signed up to the mailing list.

Yes
Please search before asking, an answer may already exist.
The Search & other links to useful information are at top of Forum.