Add lvs_service and lvs_server state modules to manage lvs state

This commit is contained in:
pengyao 2013-11-22 18:10:24 +08:00
parent e76fd64a4e
commit 47e8f3f5b6
2 changed files with 299 additions and 0 deletions

161
salt/states/lvs_server.py Normal file
View File

@ -0,0 +1,161 @@
# -*- coding: utf-8 -*-
'''
Management of LVS(Linux Virtual Server) Real Server.
=====================================================
This lvs_server module is used to add and manage LVS Real Server in the specified service. Server can be set as either absent or present.
'''
def __virtual__():
'''
Only load if the lvs module is available in __salt__
'''
return 'lvs_server' if 'lvs.get_rules' in __salt__ else False
def present(name,
protocol=None,
service_address=None,
server_address=None,
packet_forward_method='dr',
weight=1
):
'''
Ensure that the named service is present.
name
The LVS server name
protocol
The service protocol
service_address
The LVS service address
server_address
The real server address.
packet_forward_method
The LVS packet forwarding method(``dr`` for direct routing, ``tunnel`` for tunneling, ``nat`` for network access translation).
weight
The capacity of a server relative to the others in the pool.
.. code-block:: yaml
lvsrs:
lvs_server.present:
- protocol: tcp
- service_address: 1.1.1.1:80
- server_address: 192.168.0.11:8080
- packet_forward_method: dr
- weight: 10
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
#check server
server_check = __salt__['lvs.check_server'](protocol=protocol,
service_address=service_address,
server_address=server_address)
if server_check is True:
server_rule_check = __salt__['lvs.check_server'](protocol=protocol,
service_address=service_address,
server_address=server_address,
packet_forward_method=packet_forward_method,
weight=weight)
if server_rule_check is True:
ret['comment'] = 'LVS Server {0} in service {1}({2}) is present'.format(name, service_address, protocol)
return ret
else:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'LVS Server {0} in service {1}({2}) is present but some options should update'.format(name, service_address, protocol)
return ret
else:
server_edit = __salt__['lvs.edit_server'](protocol=protocol,
service_address=service_address,
server_address=server_address,
packet_forward_method=packet_forward_method,
weight=weight)
if server_edit is True:
ret['comment'] = 'LVS Server {0} in service {1}({2}) has been updated'.format(name, service_address, protocol)
ret['change'][name] = 'Update'
return ret
else:
ret['result'] = False
ret['comment'] = 'LVS Server {0} in service {1}({2}) update failed({3})'.format(name, service_address, protocol, server_edit)
return ret
else:
if __opts__['test']:
ret['comment'] = 'LVS Server {0} in service {1}({2}) is not present and needs to be created'.format(name, service_address, protocol)
ret['result'] = None
return ret
else:
server_add = __salt__['lvs.add_server'](protocol=protocol,
service_address=service_address,
server_address=server_address,
packet_forward_method=packet_forward_method,
weight=weight)
if server_add is True:
ret['comment'] = 'LVS Server {0} in service {1}({2}) has been created'.format(name, service_address, protocol)
ret['change'][name] = 'Present'
return ret
else:
ret['comment'] = 'LVS Service {0} in service {1}({2}) create failed({3})'.format(name, service_address, protocol, server_add)
ret['result'] = False
return ret
return ret
def absent(name, protocol=None, service_address=None, server_address=None):
'''
Ensure the LVS Real Server in specified service is absent.
name
The name of the LVS server.
protocol
The service protocol(only support ``tcp``, ``udp`` and ``fwmark`` service).
service_address
The LVS service adress.
server_address
The LVS real server address.
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
#check if server exists and remove it
server_check = __salt__['lvs.check_server'](protocol=protocol,
service_address=service_address,
server_address=server_address)
if server_check is True:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'LVS Server {0} in service {1}({2}) is present and needs to be removed'.format(name, service_address, protocol)
return ret
server_delete = __salt__['lvs.delete_server'](protocol=protocol,
service_address=service_address,
server_address=server_address)
if server_delete is True:
ret['comment'] = 'LVS Server {0} in service {1}(2) has been removed'.format(name, service_address, protocol)
ret['changes'][name] = 'Absent'
return ret
else:
ret['comment'] = 'LVS Server {0} in service {1}(2) removed failed({3})'.format(name, service_address, protocol, server_delete)
ret['result'] = False
return ret
else:
ret['comment'] = 'LVS Server {0} in service {1}({2}) is not present, so it cannot be removed'.format(name, service_address, protocol)
return ret

138
salt/states/lvs_service.py Normal file
View File

@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
'''
Management of LVS(Linux Virtual Server) Service.
================================================
This lvs_service module is used to create and manage LVS Service. Service can be set as either absent or present.
'''
def __virtual__():
'''
Only load if the lvs module is available in __salt__
'''
return 'lvs_service' if 'lvs.get_rules' in __salt__ else False
def present(name,
protocol=None,
service_address=None,
scheduler='wlc',
):
'''
Ensure that the named service is present.
name
The LVS service name
protocol
The service protocol
service_address
The LVS service address
scheduler
Algorithm for allocating TCP connections and UDP datagrams to real servers.
.. code-block:: yaml
lvstest:
lvs_service.present:
- service_address: 1.1.1.1:80
- protocol: tcp
- scheduler: rr
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
#check service
service_check = __salt__['lvs.check_service'](protocol=protocol,
service_address=service_address)
if service_check is True:
service_rule_check = __salt__['lvs.check_service'](protocol=protocol,
service_address=service_address,
scheduler=scheduler)
if service_rule_check is True:
ret['comment'] = 'LVS Service {0} is present'.format(name)
return ret
else:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'LVS Service {0} is present but some options should update'.format(name)
return ret
else:
service_edit = __salt__['lvs.edit_service'](protocol=protocol,
service_address=service_address,
scheduler=scheduler)
if service_edit is True:
ret['comment'] = 'LVS Service {0} has been updated'.format(name)
ret['changes'][name] = 'Update'
return ret
else:
ret['result'] = False
ret['comment'] = 'LVS Service {0} update failed'.format(name, service_edit)
return ret
else:
if __opts__['test']:
ret['comment'] = 'LVS Service {0} is not present and needs to be created'.format(name)
ret['result'] = None
return ret
else:
service_add = __salt__['lvs.add_service'](protocol=protocol,
service_address=service_address,
scheduler=scheduler)
if service_add is True:
ret['comment'] = 'LVS Service {0} has been created'.format(name)
ret['changes'][name] = 'Present'
return ret
else:
ret['comment'] = 'LVS Service {0} create failed({1})'.format(name, service_add)
ret['result'] = False
return ret
return ret
def absent(name, protocol=None, service_address=None):
'''
Ensure the LVS service is absent.
name
The name of the LVS service
protocol
The service protocol
service_address
The LVS service adress
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
#check if service exists and remove it
service_check = __salt__['lvs.check_service'](protocol=protocol,
service_address=service_address)
if service_check is True:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'LVS Service {0} is present and needs to be removed'.format(name)
return ret
service_delete = __salt__['lvs.delete_service'](protocol=protocol,
service_address=service_address)
if service_delete is True:
ret['comment'] = 'LVS Service {0} has been removed'.format(name)
ret['changes'][name] = 'Absent'
return ret
else:
ret['comment'] = 'LVS Service {0} removed failed({1})'.format(name, service_delete)
ret['result'] = False
return ret
else:
ret['comment'] = 'LVS Service {0} is not present, so it cannot be removed'.format(name)
return ret