oc_scale.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class OCScale(OpenShiftCLI):
  5. ''' Class to wrap the oc command line tools '''
  6. # pylint allows 5
  7. # pylint: disable=too-many-arguments
  8. def __init__(self,
  9. resource_name,
  10. namespace,
  11. replicas,
  12. kind,
  13. kubeconfig='/etc/origin/master/admin.kubeconfig',
  14. verbose=False):
  15. ''' Constructor for OCScale '''
  16. super(OCScale, self).__init__(namespace, kubeconfig=kubeconfig, verbose=verbose)
  17. self.kind = kind
  18. self.replicas = replicas
  19. self.name = resource_name
  20. self._resource = None
  21. @property
  22. def resource(self):
  23. ''' property function for resource var '''
  24. if not self._resource:
  25. self.get()
  26. return self._resource
  27. @resource.setter
  28. def resource(self, data):
  29. ''' setter function for resource var '''
  30. self._resource = data
  31. def get(self):
  32. '''return replicas information '''
  33. vol = self._get(self.kind, self.name)
  34. if vol['returncode'] == 0:
  35. if self.kind == 'dc':
  36. # The resource returned from a query could be an rc or dc.
  37. # pylint: disable=redefined-variable-type
  38. self.resource = DeploymentConfig(content=vol['results'][0])
  39. vol['results'] = [self.resource.get_replicas()]
  40. if self.kind == 'rc':
  41. # The resource returned from a query could be an rc or dc.
  42. # pylint: disable=redefined-variable-type
  43. self.resource = ReplicationController(content=vol['results'][0])
  44. vol['results'] = [self.resource.get_replicas()]
  45. return vol
  46. def put(self):
  47. '''update replicas into dc '''
  48. self.resource.update_replicas(self.replicas)
  49. return self._replace_content(self.kind, self.name, self.resource.yaml_dict)
  50. def needs_update(self):
  51. ''' verify whether an update is needed '''
  52. return self.resource.needs_update_replicas(self.replicas)
  53. # pylint: disable=too-many-return-statements
  54. @staticmethod
  55. def run_ansible(params, check_mode):
  56. '''perform the idempotent ansible logic'''
  57. oc_scale = OCScale(params['name'],
  58. params['namespace'],
  59. params['replicas'],
  60. params['kind'],
  61. params['kubeconfig'],
  62. verbose=params['debug'])
  63. state = params['state']
  64. api_rval = oc_scale.get()
  65. if api_rval['returncode'] != 0:
  66. return {'failed': True, 'msg': api_rval}
  67. #####
  68. # Get
  69. #####
  70. if state == 'list':
  71. return {'changed': False, 'result': api_rval['results'], 'state': 'list'} # noqa: E501
  72. elif state == 'present':
  73. ########
  74. # Update
  75. ########
  76. if oc_scale.needs_update():
  77. if check_mode:
  78. return {'changed': True, 'result': 'CHECK_MODE: Would have updated.'} # noqa: E501
  79. api_rval = oc_scale.put()
  80. if api_rval['returncode'] != 0:
  81. return {'failed': True, 'msg': api_rval}
  82. # return the created object
  83. api_rval = oc_scale.get()
  84. if api_rval['returncode'] != 0:
  85. return {'failed': True, 'msg': api_rval}
  86. return {'changed': True, 'result': api_rval['results'], 'state': 'present'} # noqa: E501
  87. return {'changed': False, 'result': api_rval['results'], 'state': 'present'} # noqa: E501
  88. return {'failed': True, 'msg': 'Unknown state passed. [{}]'.format(state)}