wiki:GroupsApi
Last modified 4 years ago Last modified on 04/16/10 13:28:49

Func groups api is a flexible way to group your machines and query them as you need. An example maybe to group your web servers as "www_group" group and query them without knowing what machines you have in that group. Func groups api is independent from its backend which means it is very easy to add your backend and use it to suit to your needs. An example of that maybe to add ldap as a backend and query machines from there ... Currently 2 backends are supported :

  • Configuration file (the old one)
  • Sqlite backend

The default groups backend is configuration file for now because it is a little bit faster than sqlite backend.If you need to change to sqlite backend what you need to do is go to : /etc/func/overlord.conf and change:

backend = sqlite group_db = /path/you/want

Groups CLI usage

(For testing purposes i added 100 test nonexisting minions range(100))


Listing all of the current stuff

[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 1
         Host : 3
         Host : 2
         Host : 5
         Host : 4


Adding a new group :

[root@fedorabig func]# func "*" group --ag "newgr"
[root@fedorabig func]#

[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 1
         Host : 3
         Host : 2
         Host : 5
         Host : 4
Group : newgr


Listing some of the groups by glob:

[root@fedorabig func]# func "*" group --lg "gr*"
GROUPS :
          ['group5']
[root@fedorabig func]#


Removing a group

[root@fedorabig func]# func "*" group --ag "tmpgr1"
[root@fedorabig func]# func "*" group --ag "tmpgr2"
[root@fedorabig func]# func "*" group --ag "tmpgr3"
[root@fedorabig func]# func "*" group --lg "*"
GROUPS :
          ['group5', 'newgr', 'tmpgr2', 'tmpgr3', 'tmpgr1']
[root@fedorabig func]# func "*" group --rg "tmpgr1"
[root@fedorabig func]# func "*" group --lg "*"
GROUPS :
          ['group5', 'newgr', 'tmpgr2', 'tmpgr3']


Removing groups by globbing :
[root@fedorabig func]# func "*" group --rg "tmp*"
[root@fedorabig func]# func "*" group --lg "*"
GROUPS :
          ['group5', 'newgr']


Adding hosts in group by globbing: (adding host from 0-9 in groups)

[root@fedorabig func]# func "*" group --ah "@group5:[0-9]"
[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 1
         Host : 0
         Host : 3
         Host : 2
         Host : 5
         Host : 4
         Host : 7
         Host : 6
         Host : 9
         Host : 8
Group : newgr




Adding hosts in group by globbing with exclusion :

[root@fedorabig func]# func "*" group --e "@group:[1][2,4]" --ah "@group5:[1]
[0-5]"
[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 1
         Host : 0
         Host : 3
         Host : 2
         Host : 5
         Host : 4
         Host : 7
         Host : 6
         Host : 9
         Host : 8
         Host : 11
         Host : 10
         Host : 13
         Host : 15
Group : newgr

In above example we added all [1][0-5] and excluded [1][2,4] with that option we dont need anymore the need of subgrouping, we subgroup on the fly :)


Listing hosts via globbing :

[root@fedorabig func]# func "*" group --lh "@group5"
['11', '10', '13', '15', '1', '0', '3', '2', '5', '4', '7', '6', '9', '8']
[root@fedorabig func]# func "*" group --lh "@group5:[1][0-5]"
['11', '10', '13', '15']

Example of exclusion :
[root@fedorabig func]# func "*" group --e "@group5:[1,3,5,7,9]" --lh 
"@group5:*"
['11', '10', '13', '15', '0', '2', '4', '6', '8']


Removing hosts from a group via globbing :

[root@fedorabig func]# func "*" group --rh "@group5:[1-5]"
[root@fedorabig func]# func "*" group --lh "@group5:*"
['11', '10', '13', '15', '0', '7', '6', '9', '8']

Removing hosts via exclusion:

[root@fedorabig func]# func "*" group --e "@group5:[0,2,4,6,8]" --rh 
"@group5:*"
[root@fedorabig func]# func "*" group --lh "@group5:*"
['0', '8', '6']


You can also do the above operations on multiple groups at the same cli:

Adding on multiple groups :

[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 0
         Host : 6
         Host : 8
Group : newgr

[root@fedorabig func]# func "*" group --ah "@group5:[1][5-9];@newgr:[2][0-5]"
[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 0
         Host : 6
         Host : 8
         Host : 19
         Host : 18
         Host : 15
         Host : 17
         Host : 16
Group : newgr
         Host : 24
         Host : 25
         Host : 20
         Host : 21
         Host : 22
         Host : 23


Listing on multiple

root@fedorabig func]# func "*" group --lh "@group5:[1][5-9];@newgr:[2][0-5]"
['24', '25', '23', '15', '21', '17', '16', '19', '18', '22', '20']

Removing on multiple:

[root@fedorabig func]# func "*" group --rh "@group5:[1][5-9];@newgr:[2][0-5]"
[root@fedorabig func]# func "*" group --la
Group : group5
         Host : 0
         Host : 6
         Host : 8
Group : newgr

You can also use the --exclude keyword when working on multiple groups,but you should be sure that the number of operations match. If you work on 3 groups you should have 3 groups on exclude parameter also.

/etc/func/groups config file format

The config backend for the groups support lives in /etc/func/groups

[webservers]
host = http1.example.com; http2.example.com

[mailservers]
host = mail1.example.com; mail2.example.com

ADDING NEW BACKENDS :

Adding new backends to groups shouldn't be hard what you need to do is just :

  • put a file in /func/overlord/group directory
  • extend the class /func/overlord/group/base.py BaseBackend and implement all

of its methods

  • add your backend into choose_backend method in /func/overlord/group/base.py
  • add your backend into /test/unittests/test_groups_api.py if it passes all

the tests you have a new backend