oc_pvc.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. #!/usr/bin/env python
  2. # pylint: disable=missing-docstring
  3. # flake8: noqa: T001
  4. # ___ ___ _ _ ___ ___ _ _____ ___ ___
  5. # / __| __| \| | __| _ \ /_\_ _| __| \
  6. # | (_ | _|| .` | _|| / / _ \| | | _|| |) |
  7. # \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
  8. # | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|
  9. # | |) | (_) | | .` | (_) || | | _|| |) | | | |
  10. # |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|
  11. #
  12. # Copyright 2016 Red Hat, Inc. and/or its affiliates
  13. # and other contributors as indicated by the @author tags.
  14. #
  15. # Licensed under the Apache License, Version 2.0 (the "License");
  16. # you may not use this file except in compliance with the License.
  17. # You may obtain a copy of the License at
  18. #
  19. # http://www.apache.org/licenses/LICENSE-2.0
  20. #
  21. # Unless required by applicable law or agreed to in writing, software
  22. # distributed under the License is distributed on an "AS IS" BASIS,
  23. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. # See the License for the specific language governing permissions and
  25. # limitations under the License.
  26. #
  27. # -*- -*- -*- Begin included fragment: lib/import.py -*- -*- -*-
  28. '''
  29. OpenShiftCLI class that wraps the oc commands in a subprocess
  30. '''
  31. # pylint: disable=too-many-lines
  32. from __future__ import print_function
  33. import atexit
  34. import copy
  35. import fcntl
  36. import json
  37. import time
  38. import os
  39. import re
  40. import shutil
  41. import subprocess
  42. import tempfile
  43. # pylint: disable=import-error
  44. try:
  45. import ruamel.yaml as yaml
  46. except ImportError:
  47. import yaml
  48. from ansible.module_utils.basic import AnsibleModule
  49. # -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
  50. # -*- -*- -*- Begin included fragment: doc/pvc -*- -*- -*-
  51. DOCUMENTATION = '''
  52. ---
  53. module: oc_pvc
  54. short_description: Modify, and idempotently manage openshift persistent volume claims
  55. description:
  56. - Modify openshift persistent volume claims programmatically.
  57. options:
  58. state:
  59. description:
  60. - Supported states, present, absent, list
  61. - present - will ensure object is created or updated to the value specified
  62. - list - will return a pvc
  63. - absent - will remove a pvc
  64. required: False
  65. default: present
  66. choices: ["present", 'absent', 'list']
  67. aliases: []
  68. kubeconfig:
  69. description:
  70. - The path for the kubeconfig file to use for authentication
  71. required: false
  72. default: /etc/origin/master/admin.kubeconfig
  73. aliases: []
  74. debug:
  75. description:
  76. - Turn on debug output.
  77. required: false
  78. default: False
  79. aliases: []
  80. name:
  81. description:
  82. - Name of the object that is being queried.
  83. required: false
  84. default: None
  85. aliases: []
  86. namespace:
  87. description:
  88. - The namespace where the object lives.
  89. required: false
  90. default: str
  91. aliases: []
  92. volume_capacity:
  93. description:
  94. - The requested volume capacity
  95. required: False
  96. default: 1G
  97. aliases: []
  98. access_modes:
  99. description:
  100. - The access modes allowed for the pvc
  101. - Expects a list
  102. required: False
  103. default: ReadWriteOnce
  104. choices:
  105. - ReadWriteOnce
  106. - ReadOnlyMany
  107. - ReadWriteMany
  108. aliases: []
  109. storage_class_name:
  110. description:
  111. - The storage class name for the PVC
  112. required: false
  113. default: None
  114. aliases: []
  115. selector:
  116. description:
  117. - A hash of key/values for the matchLabels
  118. required: false
  119. default: None
  120. aliases: []
  121. author:
  122. - "Kenny Woodson <kwoodson@redhat.com>"
  123. extends_documentation_fragment: []
  124. '''
  125. EXAMPLES = '''
  126. - name: create a pvc
  127. oc_pvc:
  128. namespace: awesomeapp
  129. name: dbstorage
  130. access_modes:
  131. - ReadWriteOnce
  132. volume_capacity: 5G
  133. register: pvcout
  134. '''
  135. # -*- -*- -*- End included fragment: doc/pvc -*- -*- -*-
  136. # -*- -*- -*- Begin included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  137. class YeditException(Exception): # pragma: no cover
  138. ''' Exception class for Yedit '''
  139. pass
  140. # pylint: disable=too-many-public-methods,too-many-instance-attributes
  141. class Yedit(object): # pragma: no cover
  142. ''' Class to modify yaml files '''
  143. re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$"
  144. re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z{}/_-]+)"
  145. com_sep = set(['.', '#', '|', ':'])
  146. # pylint: disable=too-many-arguments
  147. def __init__(self,
  148. filename=None,
  149. content=None,
  150. content_type='yaml',
  151. separator='.',
  152. backup_ext=None,
  153. backup=False):
  154. self.content = content
  155. self._separator = separator
  156. self.filename = filename
  157. self.__yaml_dict = content
  158. self.content_type = content_type
  159. self.backup = backup
  160. if backup_ext is None:
  161. self.backup_ext = ".{}".format(time.strftime("%Y%m%dT%H%M%S"))
  162. else:
  163. self.backup_ext = backup_ext
  164. self.load(content_type=self.content_type)
  165. if self.__yaml_dict is None:
  166. self.__yaml_dict = {}
  167. @property
  168. def separator(self):
  169. ''' getter method for separator '''
  170. return self._separator
  171. @separator.setter
  172. def separator(self, inc_sep):
  173. ''' setter method for separator '''
  174. self._separator = inc_sep
  175. @property
  176. def yaml_dict(self):
  177. ''' getter method for yaml_dict '''
  178. return self.__yaml_dict
  179. @yaml_dict.setter
  180. def yaml_dict(self, value):
  181. ''' setter method for yaml_dict '''
  182. self.__yaml_dict = value
  183. @staticmethod
  184. def parse_key(key, sep='.'):
  185. '''parse the key allowing the appropriate separator'''
  186. common_separators = list(Yedit.com_sep - set([sep]))
  187. return re.findall(Yedit.re_key.format(''.join(common_separators)), key)
  188. @staticmethod
  189. def valid_key(key, sep='.'):
  190. '''validate the incoming key'''
  191. common_separators = list(Yedit.com_sep - set([sep]))
  192. if not re.match(Yedit.re_valid_key.format(''.join(common_separators)), key):
  193. return False
  194. return True
  195. # pylint: disable=too-many-return-statements,too-many-branches
  196. @staticmethod
  197. def remove_entry(data, key, index=None, value=None, sep='.'):
  198. ''' remove data at location key '''
  199. if key == '' and isinstance(data, dict):
  200. if value is not None:
  201. data.pop(value)
  202. elif index is not None:
  203. raise YeditException("remove_entry for a dictionary does not have an index {}".format(index))
  204. else:
  205. data.clear()
  206. return True
  207. elif key == '' and isinstance(data, list):
  208. ind = None
  209. if value is not None:
  210. try:
  211. ind = data.index(value)
  212. except ValueError:
  213. return False
  214. elif index is not None:
  215. ind = index
  216. else:
  217. del data[:]
  218. if ind is not None:
  219. data.pop(ind)
  220. return True
  221. if not (key and Yedit.valid_key(key, sep)) and \
  222. isinstance(data, (list, dict)):
  223. return None
  224. key_indexes = Yedit.parse_key(key, sep)
  225. for arr_ind, dict_key in key_indexes[:-1]:
  226. if dict_key and isinstance(data, dict):
  227. data = data.get(dict_key)
  228. elif (arr_ind and isinstance(data, list) and
  229. int(arr_ind) <= len(data) - 1):
  230. data = data[int(arr_ind)]
  231. else:
  232. return None
  233. # process last index for remove
  234. # expected list entry
  235. if key_indexes[-1][0]:
  236. if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  237. del data[int(key_indexes[-1][0])]
  238. return True
  239. # expected dict entry
  240. elif key_indexes[-1][1]:
  241. if isinstance(data, dict):
  242. del data[key_indexes[-1][1]]
  243. return True
  244. @staticmethod
  245. def add_entry(data, key, item=None, sep='.'):
  246. ''' Get an item from a dictionary with key notation a.b.c
  247. d = {'a': {'b': 'c'}}}
  248. key = a#b
  249. return c
  250. '''
  251. if key == '':
  252. pass
  253. elif (not (key and Yedit.valid_key(key, sep)) and
  254. isinstance(data, (list, dict))):
  255. return None
  256. key_indexes = Yedit.parse_key(key, sep)
  257. for arr_ind, dict_key in key_indexes[:-1]:
  258. if dict_key:
  259. if isinstance(data, dict) and dict_key in data and data[dict_key]: # noqa: E501
  260. data = data[dict_key]
  261. continue
  262. elif data and not isinstance(data, dict):
  263. raise YeditException("Unexpected item type found while going through key " +
  264. "path: {} (at key: {})".format(key, dict_key))
  265. data[dict_key] = {}
  266. data = data[dict_key]
  267. elif (arr_ind and isinstance(data, list) and
  268. int(arr_ind) <= len(data) - 1):
  269. data = data[int(arr_ind)]
  270. else:
  271. raise YeditException("Unexpected item type found while going through key path: {}".format(key))
  272. if key == '':
  273. data = item
  274. # process last index for add
  275. # expected list entry
  276. elif key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  277. data[int(key_indexes[-1][0])] = item
  278. # expected dict entry
  279. elif key_indexes[-1][1] and isinstance(data, dict):
  280. data[key_indexes[-1][1]] = item
  281. # didn't add/update to an existing list, nor add/update key to a dict
  282. # so we must have been provided some syntax like a.b.c[<int>] = "data" for a
  283. # non-existent array
  284. else:
  285. raise YeditException("Error adding to object at path: {}".format(key))
  286. return data
  287. @staticmethod
  288. def get_entry(data, key, sep='.'):
  289. ''' Get an item from a dictionary with key notation a.b.c
  290. d = {'a': {'b': 'c'}}}
  291. key = a.b
  292. return c
  293. '''
  294. if key == '':
  295. pass
  296. elif (not (key and Yedit.valid_key(key, sep)) and
  297. isinstance(data, (list, dict))):
  298. return None
  299. key_indexes = Yedit.parse_key(key, sep)
  300. for arr_ind, dict_key in key_indexes:
  301. if dict_key and isinstance(data, dict):
  302. data = data.get(dict_key)
  303. elif (arr_ind and isinstance(data, list) and
  304. int(arr_ind) <= len(data) - 1):
  305. data = data[int(arr_ind)]
  306. else:
  307. return None
  308. return data
  309. @staticmethod
  310. def _write(filename, contents):
  311. ''' Actually write the file contents to disk. This helps with mocking. '''
  312. tmp_filename = filename + '.yedit'
  313. with open(tmp_filename, 'w') as yfd:
  314. fcntl.flock(yfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
  315. yfd.write(contents)
  316. fcntl.flock(yfd, fcntl.LOCK_UN)
  317. os.rename(tmp_filename, filename)
  318. def write(self):
  319. ''' write to file '''
  320. if not self.filename:
  321. raise YeditException('Please specify a filename.')
  322. if self.backup and self.file_exists():
  323. shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))
  324. # Try to set format attributes if supported
  325. try:
  326. self.yaml_dict.fa.set_block_style()
  327. except AttributeError:
  328. pass
  329. # Try to use RoundTripDumper if supported.
  330. if self.content_type == 'yaml':
  331. try:
  332. Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
  333. except AttributeError:
  334. Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
  335. elif self.content_type == 'json':
  336. Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
  337. else:
  338. raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
  339. 'Please specify a content_type of yaml or json.')
  340. return (True, self.yaml_dict)
  341. def read(self):
  342. ''' read from file '''
  343. # check if it exists
  344. if self.filename is None or not self.file_exists():
  345. return None
  346. contents = None
  347. with open(self.filename) as yfd:
  348. contents = yfd.read()
  349. return contents
  350. def file_exists(self):
  351. ''' return whether file exists '''
  352. if os.path.exists(self.filename):
  353. return True
  354. return False
  355. def load(self, content_type='yaml'):
  356. ''' return yaml file '''
  357. contents = self.read()
  358. if not contents and not self.content:
  359. return None
  360. if self.content:
  361. if isinstance(self.content, dict):
  362. self.yaml_dict = self.content
  363. return self.yaml_dict
  364. elif isinstance(self.content, str):
  365. contents = self.content
  366. # check if it is yaml
  367. try:
  368. if content_type == 'yaml' and contents:
  369. # Try to set format attributes if supported
  370. try:
  371. self.yaml_dict.fa.set_block_style()
  372. except AttributeError:
  373. pass
  374. # Try to use RoundTripLoader if supported.
  375. try:
  376. self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
  377. except AttributeError:
  378. self.yaml_dict = yaml.safe_load(contents)
  379. # Try to set format attributes if supported
  380. try:
  381. self.yaml_dict.fa.set_block_style()
  382. except AttributeError:
  383. pass
  384. elif content_type == 'json' and contents:
  385. self.yaml_dict = json.loads(contents)
  386. except yaml.YAMLError as err:
  387. # Error loading yaml or json
  388. raise YeditException('Problem with loading yaml file. {}'.format(err))
  389. return self.yaml_dict
  390. def get(self, key):
  391. ''' get a specified key'''
  392. try:
  393. entry = Yedit.get_entry(self.yaml_dict, key, self.separator)
  394. except KeyError:
  395. entry = None
  396. return entry
  397. def pop(self, path, key_or_item):
  398. ''' remove a key, value pair from a dict or an item for a list'''
  399. try:
  400. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  401. except KeyError:
  402. entry = None
  403. if entry is None:
  404. return (False, self.yaml_dict)
  405. if isinstance(entry, dict):
  406. # AUDIT:maybe-no-member makes sense due to fuzzy types
  407. # pylint: disable=maybe-no-member
  408. if key_or_item in entry:
  409. entry.pop(key_or_item)
  410. return (True, self.yaml_dict)
  411. return (False, self.yaml_dict)
  412. elif isinstance(entry, list):
  413. # AUDIT:maybe-no-member makes sense due to fuzzy types
  414. # pylint: disable=maybe-no-member
  415. ind = None
  416. try:
  417. ind = entry.index(key_or_item)
  418. except ValueError:
  419. return (False, self.yaml_dict)
  420. entry.pop(ind)
  421. return (True, self.yaml_dict)
  422. return (False, self.yaml_dict)
  423. def delete(self, path, index=None, value=None):
  424. ''' remove path from a dict'''
  425. try:
  426. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  427. except KeyError:
  428. entry = None
  429. if entry is None:
  430. return (False, self.yaml_dict)
  431. result = Yedit.remove_entry(self.yaml_dict, path, index, value, self.separator)
  432. if not result:
  433. return (False, self.yaml_dict)
  434. return (True, self.yaml_dict)
  435. def exists(self, path, value):
  436. ''' check if value exists at path'''
  437. try:
  438. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  439. except KeyError:
  440. entry = None
  441. if isinstance(entry, list):
  442. if value in entry:
  443. return True
  444. return False
  445. elif isinstance(entry, dict):
  446. if isinstance(value, dict):
  447. rval = False
  448. for key, val in value.items():
  449. if entry[key] != val:
  450. rval = False
  451. break
  452. else:
  453. rval = True
  454. return rval
  455. return value in entry
  456. return entry == value
  457. def append(self, path, value):
  458. '''append value to a list'''
  459. try:
  460. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  461. except KeyError:
  462. entry = None
  463. if entry is None:
  464. self.put(path, [])
  465. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  466. if not isinstance(entry, list):
  467. return (False, self.yaml_dict)
  468. # AUDIT:maybe-no-member makes sense due to loading data from
  469. # a serialized format.
  470. # pylint: disable=maybe-no-member
  471. entry.append(value)
  472. return (True, self.yaml_dict)
  473. # pylint: disable=too-many-arguments
  474. def update(self, path, value, index=None, curr_value=None):
  475. ''' put path, value into a dict '''
  476. try:
  477. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  478. except KeyError:
  479. entry = None
  480. if isinstance(entry, dict):
  481. # AUDIT:maybe-no-member makes sense due to fuzzy types
  482. # pylint: disable=maybe-no-member
  483. if not isinstance(value, dict):
  484. raise YeditException('Cannot replace key, value entry in dict with non-dict type. ' +
  485. 'value=[{}] type=[{}]'.format(value, type(value)))
  486. entry.update(value)
  487. return (True, self.yaml_dict)
  488. elif isinstance(entry, list):
  489. # AUDIT:maybe-no-member makes sense due to fuzzy types
  490. # pylint: disable=maybe-no-member
  491. ind = None
  492. if curr_value:
  493. try:
  494. ind = entry.index(curr_value)
  495. except ValueError:
  496. return (False, self.yaml_dict)
  497. elif index is not None:
  498. ind = index
  499. if ind is not None and entry[ind] != value:
  500. entry[ind] = value
  501. return (True, self.yaml_dict)
  502. # see if it exists in the list
  503. try:
  504. ind = entry.index(value)
  505. except ValueError:
  506. # doesn't exist, append it
  507. entry.append(value)
  508. return (True, self.yaml_dict)
  509. # already exists, return
  510. if ind is not None:
  511. return (False, self.yaml_dict)
  512. return (False, self.yaml_dict)
  513. def put(self, path, value):
  514. ''' put path, value into a dict '''
  515. try:
  516. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  517. except KeyError:
  518. entry = None
  519. if entry == value:
  520. return (False, self.yaml_dict)
  521. # deepcopy didn't work
  522. # Try to use ruamel.yaml and fallback to pyyaml
  523. try:
  524. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  525. default_flow_style=False),
  526. yaml.RoundTripLoader)
  527. except AttributeError:
  528. tmp_copy = copy.deepcopy(self.yaml_dict)
  529. # set the format attributes if available
  530. try:
  531. tmp_copy.fa.set_block_style()
  532. except AttributeError:
  533. pass
  534. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  535. if result is None:
  536. return (False, self.yaml_dict)
  537. # When path equals "" it is a special case.
  538. # "" refers to the root of the document
  539. # Only update the root path (entire document) when its a list or dict
  540. if path == '':
  541. if isinstance(result, list) or isinstance(result, dict):
  542. self.yaml_dict = result
  543. return (True, self.yaml_dict)
  544. return (False, self.yaml_dict)
  545. self.yaml_dict = tmp_copy
  546. return (True, self.yaml_dict)
  547. def create(self, path, value):
  548. ''' create a yaml file '''
  549. if not self.file_exists():
  550. # deepcopy didn't work
  551. # Try to use ruamel.yaml and fallback to pyyaml
  552. try:
  553. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  554. default_flow_style=False),
  555. yaml.RoundTripLoader)
  556. except AttributeError:
  557. tmp_copy = copy.deepcopy(self.yaml_dict)
  558. # set the format attributes if available
  559. try:
  560. tmp_copy.fa.set_block_style()
  561. except AttributeError:
  562. pass
  563. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  564. if result is not None:
  565. self.yaml_dict = tmp_copy
  566. return (True, self.yaml_dict)
  567. return (False, self.yaml_dict)
  568. @staticmethod
  569. def get_curr_value(invalue, val_type):
  570. '''return the current value'''
  571. if invalue is None:
  572. return None
  573. curr_value = invalue
  574. if val_type == 'yaml':
  575. curr_value = yaml.safe_load(str(invalue))
  576. elif val_type == 'json':
  577. curr_value = json.loads(invalue)
  578. return curr_value
  579. @staticmethod
  580. def parse_value(inc_value, vtype=''):
  581. '''determine value type passed'''
  582. true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE',
  583. 'on', 'On', 'ON', ]
  584. false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE',
  585. 'off', 'Off', 'OFF']
  586. # It came in as a string but you didn't specify value_type as string
  587. # we will convert to bool if it matches any of the above cases
  588. if isinstance(inc_value, str) and 'bool' in vtype:
  589. if inc_value not in true_bools and inc_value not in false_bools:
  590. raise YeditException('Not a boolean type. str=[{}] vtype=[{}]'.format(inc_value, vtype))
  591. elif isinstance(inc_value, bool) and 'str' in vtype:
  592. inc_value = str(inc_value)
  593. # There is a special case where '' will turn into None after yaml loading it so skip
  594. if isinstance(inc_value, str) and inc_value == '':
  595. pass
  596. # If vtype is not str then go ahead and attempt to yaml load it.
  597. elif isinstance(inc_value, str) and 'str' not in vtype:
  598. try:
  599. inc_value = yaml.safe_load(inc_value)
  600. except Exception:
  601. raise YeditException('Could not determine type of incoming value. ' +
  602. 'value=[{}] vtype=[{}]'.format(type(inc_value), vtype))
  603. return inc_value
  604. @staticmethod
  605. def process_edits(edits, yamlfile):
  606. '''run through a list of edits and process them one-by-one'''
  607. results = []
  608. for edit in edits:
  609. value = Yedit.parse_value(edit['value'], edit.get('value_type', ''))
  610. if edit.get('action') == 'update':
  611. # pylint: disable=line-too-long
  612. curr_value = Yedit.get_curr_value(
  613. Yedit.parse_value(edit.get('curr_value')),
  614. edit.get('curr_value_format'))
  615. rval = yamlfile.update(edit['key'],
  616. value,
  617. edit.get('index'),
  618. curr_value)
  619. elif edit.get('action') == 'append':
  620. rval = yamlfile.append(edit['key'], value)
  621. else:
  622. rval = yamlfile.put(edit['key'], value)
  623. if rval[0]:
  624. results.append({'key': edit['key'], 'edit': rval[1]})
  625. return {'changed': len(results) > 0, 'results': results}
  626. # pylint: disable=too-many-return-statements,too-many-branches
  627. @staticmethod
  628. def run_ansible(params):
  629. '''perform the idempotent crud operations'''
  630. yamlfile = Yedit(filename=params['src'],
  631. backup=params['backup'],
  632. content_type=params['content_type'],
  633. backup_ext=params['backup_ext'],
  634. separator=params['separator'])
  635. state = params['state']
  636. if params['src']:
  637. rval = yamlfile.load()
  638. if yamlfile.yaml_dict is None and state != 'present':
  639. return {'failed': True,
  640. 'msg': 'Error opening file [{}]. Verify that the '.format(params['src']) +
  641. 'file exists, that it is has correct permissions, and is valid yaml.'}
  642. if state == 'list':
  643. if params['content']:
  644. content = Yedit.parse_value(params['content'], params['content_type'])
  645. yamlfile.yaml_dict = content
  646. if params['key']:
  647. rval = yamlfile.get(params['key'])
  648. return {'changed': False, 'result': rval, 'state': state}
  649. elif state == 'absent':
  650. if params['content']:
  651. content = Yedit.parse_value(params['content'], params['content_type'])
  652. yamlfile.yaml_dict = content
  653. if params['update']:
  654. rval = yamlfile.pop(params['key'], params['value'])
  655. else:
  656. rval = yamlfile.delete(params['key'], params['index'], params['value'])
  657. if rval[0] and params['src']:
  658. yamlfile.write()
  659. return {'changed': rval[0], 'result': rval[1], 'state': state}
  660. elif state == 'present':
  661. # check if content is different than what is in the file
  662. if params['content']:
  663. content = Yedit.parse_value(params['content'], params['content_type'])
  664. # We had no edits to make and the contents are the same
  665. if yamlfile.yaml_dict == content and \
  666. params['value'] is None:
  667. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  668. yamlfile.yaml_dict = content
  669. # If we were passed a key, value then
  670. # we enapsulate it in a list and process it
  671. # Key, Value passed to the module : Converted to Edits list #
  672. edits = []
  673. _edit = {}
  674. if params['value'] is not None:
  675. _edit['value'] = params['value']
  676. _edit['value_type'] = params['value_type']
  677. _edit['key'] = params['key']
  678. if params['update']:
  679. _edit['action'] = 'update'
  680. _edit['curr_value'] = params['curr_value']
  681. _edit['curr_value_format'] = params['curr_value_format']
  682. _edit['index'] = params['index']
  683. elif params['append']:
  684. _edit['action'] = 'append'
  685. edits.append(_edit)
  686. elif params['edits'] is not None:
  687. edits = params['edits']
  688. if edits:
  689. results = Yedit.process_edits(edits, yamlfile)
  690. # if there were changes and a src provided to us we need to write
  691. if results['changed'] and params['src']:
  692. yamlfile.write()
  693. return {'changed': results['changed'], 'result': results['results'], 'state': state}
  694. # no edits to make
  695. if params['src']:
  696. # pylint: disable=redefined-variable-type
  697. rval = yamlfile.write()
  698. return {'changed': rval[0],
  699. 'result': rval[1],
  700. 'state': state}
  701. # We were passed content but no src, key or value, or edits. Return contents in memory
  702. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  703. return {'failed': True, 'msg': 'Unkown state passed'}
  704. # -*- -*- -*- End included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  705. # -*- -*- -*- Begin included fragment: lib/base.py -*- -*- -*-
  706. # pylint: disable=too-many-lines
  707. # noqa: E301,E302,E303,T001
  708. class OpenShiftCLIError(Exception):
  709. '''Exception class for openshiftcli'''
  710. pass
  711. ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')]
  712. def locate_oc_binary():
  713. ''' Find and return oc binary file '''
  714. # https://github.com/openshift/openshift-ansible/issues/3410
  715. # oc can be in /usr/local/bin in some cases, but that may not
  716. # be in $PATH due to ansible/sudo
  717. paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS
  718. oc_binary = 'oc'
  719. # Use shutil.which if it is available, otherwise fallback to a naive path search
  720. try:
  721. which_result = shutil.which(oc_binary, path=os.pathsep.join(paths))
  722. if which_result is not None:
  723. oc_binary = which_result
  724. except AttributeError:
  725. for path in paths:
  726. if os.path.exists(os.path.join(path, oc_binary)):
  727. oc_binary = os.path.join(path, oc_binary)
  728. break
  729. return oc_binary
  730. # pylint: disable=too-few-public-methods
  731. class OpenShiftCLI(object):
  732. ''' Class to wrap the command line tools '''
  733. def __init__(self,
  734. namespace,
  735. kubeconfig='/etc/origin/master/admin.kubeconfig',
  736. verbose=False,
  737. all_namespaces=False):
  738. ''' Constructor for OpenshiftCLI '''
  739. self.namespace = namespace
  740. self.verbose = verbose
  741. self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig)
  742. self.all_namespaces = all_namespaces
  743. self.oc_binary = locate_oc_binary()
  744. # Pylint allows only 5 arguments to be passed.
  745. # pylint: disable=too-many-arguments
  746. def _replace_content(self, resource, rname, content, edits=None, force=False, sep='.'):
  747. ''' replace the current object with the content '''
  748. res = self._get(resource, rname)
  749. if not res['results']:
  750. return res
  751. fname = Utils.create_tmpfile(rname + '-')
  752. yed = Yedit(fname, res['results'][0], separator=sep)
  753. updated = False
  754. if content is not None:
  755. changes = []
  756. for key, value in content.items():
  757. changes.append(yed.put(key, value))
  758. if any([change[0] for change in changes]):
  759. updated = True
  760. elif edits is not None:
  761. results = Yedit.process_edits(edits, yed)
  762. if results['changed']:
  763. updated = True
  764. if updated:
  765. yed.write()
  766. atexit.register(Utils.cleanup, [fname])
  767. return self._replace(fname, force)
  768. return {'returncode': 0, 'updated': False}
  769. def _replace(self, fname, force=False):
  770. '''replace the current object with oc replace'''
  771. # We are removing the 'resourceVersion' to handle
  772. # a race condition when modifying oc objects
  773. yed = Yedit(fname)
  774. results = yed.delete('metadata.resourceVersion')
  775. if results[0]:
  776. yed.write()
  777. cmd = ['replace', '-f', fname]
  778. if force:
  779. cmd.append('--force')
  780. return self.openshift_cmd(cmd)
  781. def _create_from_content(self, rname, content):
  782. '''create a temporary file and then call oc create on it'''
  783. fname = Utils.create_tmpfile(rname + '-')
  784. yed = Yedit(fname, content=content)
  785. yed.write()
  786. atexit.register(Utils.cleanup, [fname])
  787. return self._create(fname)
  788. def _create(self, fname):
  789. '''call oc create on a filename'''
  790. return self.openshift_cmd(['create', '-f', fname])
  791. def _delete(self, resource, name=None, selector=None):
  792. '''call oc delete on a resource'''
  793. cmd = ['delete', resource]
  794. if selector is not None:
  795. cmd.append('--selector={}'.format(selector))
  796. elif name is not None:
  797. cmd.append(name)
  798. else:
  799. raise OpenShiftCLIError('Either name or selector is required when calling delete.')
  800. return self.openshift_cmd(cmd)
  801. def _process(self, template_name, create=False, params=None, template_data=None): # noqa: E501
  802. '''process a template
  803. template_name: the name of the template to process
  804. create: whether to send to oc create after processing
  805. params: the parameters for the template
  806. template_data: the incoming template's data; instead of a file
  807. '''
  808. cmd = ['process']
  809. if template_data:
  810. cmd.extend(['-f', '-'])
  811. else:
  812. cmd.append(template_name)
  813. if params:
  814. param_str = ["{}={}".format(key, str(value).replace("'", r'"')) for key, value in params.items()]
  815. cmd.append('-p')
  816. cmd.extend(param_str)
  817. results = self.openshift_cmd(cmd, output=True, input_data=template_data)
  818. if results['returncode'] != 0 or not create:
  819. return results
  820. fname = Utils.create_tmpfile(template_name + '-')
  821. yed = Yedit(fname, results['results'])
  822. yed.write()
  823. atexit.register(Utils.cleanup, [fname])
  824. return self.openshift_cmd(['create', '-f', fname])
  825. def _get(self, resource, name=None, selector=None, field_selector=None):
  826. '''return a resource by name '''
  827. cmd = ['get', resource]
  828. if selector is not None:
  829. cmd.append('--selector={}'.format(selector))
  830. if field_selector is not None:
  831. cmd.append('--field-selector={}'.format(field_selector))
  832. # Name cannot be used with selector or field_selector.
  833. if selector is None and field_selector is None and name is not None:
  834. cmd.append(name)
  835. cmd.extend(['-o', 'json'])
  836. rval = self.openshift_cmd(cmd, output=True)
  837. # Ensure results are retuned in an array
  838. if 'items' in rval:
  839. rval['results'] = rval['items']
  840. elif not isinstance(rval['results'], list):
  841. rval['results'] = [rval['results']]
  842. return rval
  843. def _schedulable(self, node=None, selector=None, schedulable=True):
  844. ''' perform oadm manage-node scheduable '''
  845. cmd = ['manage-node']
  846. if node:
  847. cmd.extend(node)
  848. else:
  849. cmd.append('--selector={}'.format(selector))
  850. cmd.append('--schedulable={}'.format(schedulable))
  851. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') # noqa: E501
  852. def _list_pods(self, node=None, selector=None, pod_selector=None):
  853. ''' perform oadm list pods
  854. node: the node in which to list pods
  855. selector: the label selector filter if provided
  856. pod_selector: the pod selector filter if provided
  857. '''
  858. cmd = ['manage-node']
  859. if node:
  860. cmd.extend(node)
  861. else:
  862. cmd.append('--selector={}'.format(selector))
  863. if pod_selector:
  864. cmd.append('--pod-selector={}'.format(pod_selector))
  865. cmd.extend(['--list-pods', '-o', 'json'])
  866. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  867. # pylint: disable=too-many-arguments
  868. def _evacuate(self, node=None, selector=None, pod_selector=None, dry_run=False, grace_period=None, force=False):
  869. ''' perform oadm manage-node evacuate '''
  870. cmd = ['manage-node']
  871. if node:
  872. cmd.extend(node)
  873. else:
  874. cmd.append('--selector={}'.format(selector))
  875. if dry_run:
  876. cmd.append('--dry-run')
  877. if pod_selector:
  878. cmd.append('--pod-selector={}'.format(pod_selector))
  879. if grace_period:
  880. cmd.append('--grace-period={}'.format(int(grace_period)))
  881. if force:
  882. cmd.append('--force')
  883. cmd.append('--evacuate')
  884. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  885. def _version(self):
  886. ''' return the openshift version'''
  887. return self.openshift_cmd(['version'], output=True, output_type='raw')
  888. def _import_image(self, url=None, name=None, tag=None):
  889. ''' perform image import '''
  890. cmd = ['import-image']
  891. image = '{0}'.format(name)
  892. if tag:
  893. image += ':{0}'.format(tag)
  894. cmd.append(image)
  895. if url:
  896. cmd.append('--from={0}/{1}'.format(url, image))
  897. cmd.append('-n{0}'.format(self.namespace))
  898. cmd.append('--confirm')
  899. return self.openshift_cmd(cmd)
  900. def _run(self, cmds, input_data):
  901. ''' Actually executes the command. This makes mocking easier. '''
  902. curr_env = os.environ.copy()
  903. curr_env.update({'KUBECONFIG': self.kubeconfig})
  904. proc = subprocess.Popen(cmds,
  905. stdin=subprocess.PIPE,
  906. stdout=subprocess.PIPE,
  907. stderr=subprocess.PIPE,
  908. env=curr_env)
  909. stdout, stderr = proc.communicate(input_data)
  910. return proc.returncode, stdout.decode('utf-8'), stderr.decode('utf-8')
  911. # pylint: disable=too-many-arguments,too-many-branches
  912. def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None):
  913. '''Base command for oc '''
  914. cmds = [self.oc_binary]
  915. if oadm:
  916. cmds.append('adm')
  917. cmds.extend(cmd)
  918. if self.all_namespaces:
  919. cmds.extend(['--all-namespaces'])
  920. elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501
  921. cmds.extend(['-n', self.namespace])
  922. if self.verbose:
  923. print(' '.join(cmds))
  924. try:
  925. returncode, stdout, stderr = self._run(cmds, input_data)
  926. except OSError as ex:
  927. returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex)
  928. rval = {"returncode": returncode,
  929. "cmd": ' '.join(cmds)}
  930. if output_type == 'json':
  931. rval['results'] = {}
  932. if output and stdout:
  933. try:
  934. rval['results'] = json.loads(stdout)
  935. except ValueError as verr:
  936. if "No JSON object could be decoded" in verr.args:
  937. rval['err'] = verr.args
  938. elif output_type == 'raw':
  939. rval['results'] = stdout if output else ''
  940. if self.verbose:
  941. print("STDOUT: {0}".format(stdout))
  942. print("STDERR: {0}".format(stderr))
  943. if 'err' in rval or returncode != 0:
  944. rval.update({"stderr": stderr,
  945. "stdout": stdout})
  946. return rval
  947. class Utils(object): # pragma: no cover
  948. ''' utilities for openshiftcli modules '''
  949. @staticmethod
  950. def _write(filename, contents):
  951. ''' Actually write the file contents to disk. This helps with mocking. '''
  952. with open(filename, 'w') as sfd:
  953. sfd.write(str(contents))
  954. @staticmethod
  955. def create_tmp_file_from_contents(rname, data, ftype='yaml'):
  956. ''' create a file in tmp with name and contents'''
  957. tmp = Utils.create_tmpfile(prefix=rname)
  958. if ftype == 'yaml':
  959. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  960. # pylint: disable=no-member
  961. if hasattr(yaml, 'RoundTripDumper'):
  962. Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
  963. else:
  964. Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))
  965. elif ftype == 'json':
  966. Utils._write(tmp, json.dumps(data))
  967. else:
  968. Utils._write(tmp, data)
  969. # Register cleanup when module is done
  970. atexit.register(Utils.cleanup, [tmp])
  971. return tmp
  972. @staticmethod
  973. def create_tmpfile_copy(inc_file):
  974. '''create a temporary copy of a file'''
  975. tmpfile = Utils.create_tmpfile('lib_openshift-')
  976. Utils._write(tmpfile, open(inc_file).read())
  977. # Cleanup the tmpfile
  978. atexit.register(Utils.cleanup, [tmpfile])
  979. return tmpfile
  980. @staticmethod
  981. def create_tmpfile(prefix='tmp'):
  982. ''' Generates and returns a temporary file name '''
  983. with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as tmp:
  984. return tmp.name
  985. @staticmethod
  986. def create_tmp_files_from_contents(content, content_type=None):
  987. '''Turn an array of dict: filename, content into a files array'''
  988. if not isinstance(content, list):
  989. content = [content]
  990. files = []
  991. for item in content:
  992. path = Utils.create_tmp_file_from_contents(item['path'] + '-',
  993. item['data'],
  994. ftype=content_type)
  995. files.append({'name': os.path.basename(item['path']),
  996. 'path': path})
  997. return files
  998. @staticmethod
  999. def cleanup(files):
  1000. '''Clean up on exit '''
  1001. for sfile in files:
  1002. if os.path.exists(sfile):
  1003. if os.path.isdir(sfile):
  1004. shutil.rmtree(sfile)
  1005. elif os.path.isfile(sfile):
  1006. os.remove(sfile)
  1007. @staticmethod
  1008. def exists(results, _name):
  1009. ''' Check to see if the results include the name '''
  1010. if not results:
  1011. return False
  1012. if Utils.find_result(results, _name):
  1013. return True
  1014. return False
  1015. @staticmethod
  1016. def find_result(results, _name):
  1017. ''' Find the specified result by name'''
  1018. rval = None
  1019. for result in results:
  1020. if 'metadata' in result and result['metadata']['name'] == _name:
  1021. rval = result
  1022. break
  1023. return rval
  1024. @staticmethod
  1025. def get_resource_file(sfile, sfile_type='yaml'):
  1026. ''' return the service file '''
  1027. contents = None
  1028. with open(sfile) as sfd:
  1029. contents = sfd.read()
  1030. if sfile_type == 'yaml':
  1031. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  1032. # pylint: disable=no-member
  1033. if hasattr(yaml, 'RoundTripLoader'):
  1034. contents = yaml.load(contents, yaml.RoundTripLoader)
  1035. else:
  1036. contents = yaml.safe_load(contents)
  1037. elif sfile_type == 'json':
  1038. contents = json.loads(contents)
  1039. return contents
  1040. @staticmethod
  1041. def filter_versions(stdout):
  1042. ''' filter the oc version output '''
  1043. version_dict = {}
  1044. version_search = ['oc', 'openshift', 'kubernetes']
  1045. for line in stdout.strip().split('\n'):
  1046. for term in version_search:
  1047. if not line:
  1048. continue
  1049. if line.startswith(term):
  1050. version_dict[term] = line.split()[-1]
  1051. # horrible hack to get openshift version in Openshift 3.2
  1052. # By default "oc version in 3.2 does not return an "openshift" version
  1053. if "openshift" not in version_dict:
  1054. version_dict["openshift"] = version_dict["oc"]
  1055. return version_dict
  1056. @staticmethod
  1057. def add_custom_versions(versions):
  1058. ''' create custom versions strings '''
  1059. versions_dict = {}
  1060. for tech, version in versions.items():
  1061. # clean up "-" from version
  1062. if "-" in version:
  1063. version = version.split("-")[0]
  1064. if version.startswith('v'):
  1065. version = version[1:] # Remove the 'v' prefix
  1066. versions_dict[tech + '_numeric'] = version.split('+')[0]
  1067. # "3.3.0.33" is what we have, we want "3.3"
  1068. versions_dict[tech + '_short'] = "{}.{}".format(*version.split('.'))
  1069. return versions_dict
  1070. @staticmethod
  1071. def openshift_installed():
  1072. ''' check if openshift is installed '''
  1073. import rpm
  1074. transaction_set = rpm.TransactionSet()
  1075. rpmquery = transaction_set.dbMatch("name", "atomic-openshift")
  1076. return rpmquery.count() > 0
  1077. # Disabling too-many-branches. This is a yaml dictionary comparison function
  1078. # pylint: disable=too-many-branches,too-many-return-statements,too-many-statements
  1079. @staticmethod
  1080. def check_def_equal(user_def, result_def, skip_keys=None, debug=False):
  1081. ''' Given a user defined definition, compare it with the results given back by our query. '''
  1082. # Currently these values are autogenerated and we do not need to check them
  1083. skip = ['metadata', 'status']
  1084. if skip_keys:
  1085. skip.extend(skip_keys)
  1086. for key, value in result_def.items():
  1087. if key in skip:
  1088. continue
  1089. # Both are lists
  1090. if isinstance(value, list):
  1091. if key not in user_def:
  1092. if debug:
  1093. print('User data does not have key [%s]' % key)
  1094. print('User data: %s' % user_def)
  1095. return False
  1096. if not isinstance(user_def[key], list):
  1097. if debug:
  1098. print('user_def[key] is not a list key=[%s] user_def[key]=%s' % (key, user_def[key]))
  1099. return False
  1100. if len(user_def[key]) != len(value):
  1101. if debug:
  1102. print("List lengths are not equal.")
  1103. print("key=[%s]: user_def[%s] != value[%s]" % (key, len(user_def[key]), len(value)))
  1104. print("user_def: %s" % user_def[key])
  1105. print("value: %s" % value)
  1106. return False
  1107. for values in zip(user_def[key], value):
  1108. if isinstance(values[0], dict) and isinstance(values[1], dict):
  1109. if debug:
  1110. print('sending list - list')
  1111. print(type(values[0]))
  1112. print(type(values[1]))
  1113. result = Utils.check_def_equal(values[0], values[1], skip_keys=skip_keys, debug=debug)
  1114. if not result:
  1115. print('list compare returned false')
  1116. return False
  1117. elif value != user_def[key]:
  1118. if debug:
  1119. print('value should be identical')
  1120. print(user_def[key])
  1121. print(value)
  1122. return False
  1123. # recurse on a dictionary
  1124. elif isinstance(value, dict):
  1125. if key not in user_def:
  1126. if debug:
  1127. print("user_def does not have key [%s]" % key)
  1128. return False
  1129. if not isinstance(user_def[key], dict):
  1130. if debug:
  1131. print("dict returned false: not instance of dict")
  1132. return False
  1133. # before passing ensure keys match
  1134. api_values = set(value.keys()) - set(skip)
  1135. user_values = set(user_def[key].keys()) - set(skip)
  1136. if api_values != user_values:
  1137. if debug:
  1138. print("keys are not equal in dict")
  1139. print(user_values)
  1140. print(api_values)
  1141. return False
  1142. result = Utils.check_def_equal(user_def[key], value, skip_keys=skip_keys, debug=debug)
  1143. if not result:
  1144. if debug:
  1145. print("dict returned false")
  1146. print(result)
  1147. return False
  1148. # Verify each key, value pair is the same
  1149. else:
  1150. if key not in user_def or value != user_def[key]:
  1151. if debug:
  1152. print("value not equal; user_def does not have key")
  1153. print(key)
  1154. print(value)
  1155. if key in user_def:
  1156. print(user_def[key])
  1157. return False
  1158. if debug:
  1159. print('returning true')
  1160. return True
  1161. class OpenShiftCLIConfig(object):
  1162. '''Generic Config'''
  1163. def __init__(self, rname, namespace, kubeconfig, options):
  1164. self.kubeconfig = kubeconfig
  1165. self.name = rname
  1166. self.namespace = namespace
  1167. self._options = options
  1168. @property
  1169. def config_options(self):
  1170. ''' return config options '''
  1171. return self._options
  1172. def to_option_list(self, ascommalist=''):
  1173. '''return all options as a string
  1174. if ascommalist is set to the name of a key, and
  1175. the value of that key is a dict, format the dict
  1176. as a list of comma delimited key=value pairs'''
  1177. return self.stringify(ascommalist)
  1178. def stringify(self, ascommalist=''):
  1179. ''' return the options hash as cli params in a string
  1180. if ascommalist is set to the name of a key, and
  1181. the value of that key is a dict, format the dict
  1182. as a list of comma delimited key=value pairs '''
  1183. rval = []
  1184. for key in sorted(self.config_options.keys()):
  1185. data = self.config_options[key]
  1186. if data['include'] \
  1187. and (data['value'] is not None or isinstance(data['value'], int)):
  1188. if key == ascommalist:
  1189. val = ','.join(['{}={}'.format(kk, vv) for kk, vv in sorted(data['value'].items())])
  1190. else:
  1191. val = data['value']
  1192. rval.append('--{}={}'.format(key.replace('_', '-'), val))
  1193. return rval
  1194. # -*- -*- -*- End included fragment: lib/base.py -*- -*- -*-
  1195. # -*- -*- -*- Begin included fragment: lib/pvc.py -*- -*- -*-
  1196. # pylint: disable=too-many-instance-attributes
  1197. class PersistentVolumeClaimConfig(object):
  1198. ''' Handle pvc options '''
  1199. # pylint: disable=too-many-arguments
  1200. def __init__(self,
  1201. sname,
  1202. namespace,
  1203. kubeconfig,
  1204. access_modes=None,
  1205. vol_capacity='1G',
  1206. selector=None,
  1207. storage_class_name=None):
  1208. ''' constructor for handling pvc options '''
  1209. self.kubeconfig = kubeconfig
  1210. self.name = sname
  1211. self.namespace = namespace
  1212. self.access_modes = access_modes
  1213. self.vol_capacity = vol_capacity
  1214. self.data = {}
  1215. self.selector = selector
  1216. self.storage_class_name = storage_class_name
  1217. self.create_dict()
  1218. def create_dict(self):
  1219. ''' return a service as a dict '''
  1220. # version
  1221. self.data['apiVersion'] = 'v1'
  1222. # kind
  1223. self.data['kind'] = 'PersistentVolumeClaim'
  1224. # metadata
  1225. self.data['metadata'] = {}
  1226. self.data['metadata']['name'] = self.name
  1227. # spec
  1228. self.data['spec'] = {}
  1229. self.data['spec']['accessModes'] = ['ReadWriteOnce']
  1230. if self.access_modes:
  1231. self.data['spec']['accessModes'] = self.access_modes
  1232. if self.selector:
  1233. self.data['spec']['selector'] = {'matchLabels': self.selector}
  1234. # storage capacity
  1235. self.data['spec']['resources'] = {}
  1236. self.data['spec']['resources']['requests'] = {}
  1237. self.data['spec']['resources']['requests']['storage'] = self.vol_capacity
  1238. if self.storage_class_name:
  1239. self.data['spec']['storageClassName'] = self.storage_class_name
  1240. # pylint: disable=too-many-instance-attributes,too-many-public-methods
  1241. class PersistentVolumeClaim(Yedit):
  1242. ''' Class to wrap the oc command line tools '''
  1243. access_modes_path = "spec.accessModes"
  1244. volume_capacity_path = "spec.requests.storage"
  1245. volume_name_path = "spec.volumeName"
  1246. bound_path = "status.phase"
  1247. kind = 'PersistentVolumeClaim'
  1248. selector_path = "spec.selector.matchLabels"
  1249. storage_class_name_path = "spec.storageClassName"
  1250. def __init__(self, content):
  1251. '''PersistentVolumeClaim constructor'''
  1252. super(PersistentVolumeClaim, self).__init__(content=content)
  1253. self._access_modes = None
  1254. self._volume_capacity = None
  1255. self._volume_name = None
  1256. self._selector = None
  1257. self._storage_class_name = None
  1258. @property
  1259. def storage_class_name(self):
  1260. ''' storage_class_name property '''
  1261. if self._storage_class_name is None:
  1262. self._storage_class_name = self.get_storage_class_name()
  1263. return self._storage_class_name
  1264. @storage_class_name.setter
  1265. def storage_class_name(self, data):
  1266. ''' storage_class_name property setter'''
  1267. self._storage_class_name = data
  1268. @property
  1269. def volume_name(self):
  1270. ''' volume_name property '''
  1271. if self._volume_name is None:
  1272. self._volume_name = self.get_volume_name()
  1273. return self._volume_name
  1274. @volume_name.setter
  1275. def volume_name(self, data):
  1276. ''' volume_name property setter'''
  1277. self._volume_name = data
  1278. @property
  1279. def selector(self):
  1280. ''' selector property '''
  1281. if self._selector is None:
  1282. self._selector = self.get_selector()
  1283. if not isinstance(self._selector, dict):
  1284. self._selector = dict(self._selector)
  1285. return self._selector
  1286. @selector.setter
  1287. def selector(self, data):
  1288. ''' selector property setter'''
  1289. if not isinstance(data, dict):
  1290. data = dict(data)
  1291. self._selector = data
  1292. @property
  1293. def access_modes(self):
  1294. ''' access_modes property '''
  1295. if self._access_modes is None:
  1296. self._access_modes = self.get_access_modes()
  1297. if not isinstance(self._access_modes, list):
  1298. self._access_modes = list(self._access_modes)
  1299. return self._access_modes
  1300. @access_modes.setter
  1301. def access_modes(self, data):
  1302. ''' access_modes property setter'''
  1303. if not isinstance(data, list):
  1304. data = list(data)
  1305. self._access_modes = data
  1306. @property
  1307. def volume_capacity(self):
  1308. ''' volume_capacity property '''
  1309. if self._volume_capacity is None:
  1310. self._volume_capacity = self.get_volume_capacity()
  1311. return self._volume_capacity
  1312. @volume_capacity.setter
  1313. def volume_capacity(self, data):
  1314. ''' volume_capacity property setter'''
  1315. self._volume_capacity = data
  1316. def get_storage_class_name(self):
  1317. '''get storage_class_name'''
  1318. return self.get(PersistentVolumeClaim.storage_class_name_path) or []
  1319. def get_selector(self):
  1320. '''get selector'''
  1321. return self.get(PersistentVolumeClaim.selector_path) or []
  1322. def get_access_modes(self):
  1323. '''get access_modes'''
  1324. return self.get(PersistentVolumeClaim.access_modes_path) or []
  1325. def get_volume_capacity(self):
  1326. '''get volume_capacity'''
  1327. return self.get(PersistentVolumeClaim.volume_capacity_path) or []
  1328. def get_volume_name(self):
  1329. '''get volume_name'''
  1330. return self.get(PersistentVolumeClaim.volume_name_path) or []
  1331. def is_bound(self):
  1332. '''return whether volume is bound'''
  1333. return self.get(PersistentVolumeClaim.bound_path) or []
  1334. #### ADD #####
  1335. def add_access_mode(self, inc_mode):
  1336. ''' add an access_mode'''
  1337. if self.access_modes:
  1338. self.access_modes.append(inc_mode)
  1339. else:
  1340. self.put(PersistentVolumeClaim.access_modes_path, [inc_mode])
  1341. return True
  1342. #### /ADD #####
  1343. #### Remove #####
  1344. def remove_access_mode(self, inc_mode):
  1345. ''' remove an access_mode'''
  1346. try:
  1347. self.access_modes.remove(inc_mode)
  1348. except ValueError as _:
  1349. return False
  1350. return True
  1351. #### /REMOVE #####
  1352. #### UPDATE #####
  1353. def update_access_mode(self, inc_mode):
  1354. ''' update an access_mode'''
  1355. try:
  1356. index = self.access_modes.index(inc_mode)
  1357. except ValueError as _:
  1358. return self.add_access_mode(inc_mode)
  1359. self.access_modes[index] = inc_mode
  1360. return True
  1361. #### /UPDATE #####
  1362. #### FIND ####
  1363. def find_access_mode(self, inc_mode):
  1364. ''' find a user '''
  1365. index = None
  1366. try:
  1367. index = self.access_modes.index(inc_mode)
  1368. except ValueError as _:
  1369. return index
  1370. return index
  1371. # -*- -*- -*- End included fragment: lib/pvc.py -*- -*- -*-
  1372. # -*- -*- -*- Begin included fragment: class/oc_pvc.py -*- -*- -*-
  1373. # pylint: disable=too-many-instance-attributes
  1374. class OCPVC(OpenShiftCLI):
  1375. ''' Class to wrap the oc command line tools '''
  1376. kind = 'pvc'
  1377. # pylint allows 5
  1378. # pylint: disable=too-many-arguments
  1379. def __init__(self,
  1380. config,
  1381. verbose=False):
  1382. ''' Constructor for OCVolume '''
  1383. super(OCPVC, self).__init__(config.namespace, config.kubeconfig)
  1384. self.config = config
  1385. self.namespace = config.namespace
  1386. self._pvc = None
  1387. @property
  1388. def pvc(self):
  1389. ''' property function pvc'''
  1390. if not self._pvc:
  1391. self.get()
  1392. return self._pvc
  1393. @pvc.setter
  1394. def pvc(self, data):
  1395. ''' setter function for yedit var '''
  1396. self._pvc = data
  1397. def bound(self):
  1398. '''return whether the pvc is bound'''
  1399. if self.pvc.get_volume_name():
  1400. return True
  1401. return False
  1402. def exists(self):
  1403. ''' return whether a pvc exists '''
  1404. if self.pvc:
  1405. return True
  1406. return False
  1407. def get(self):
  1408. '''return pvc information '''
  1409. result = self._get(self.kind, self.config.name)
  1410. if result['returncode'] == 0:
  1411. self.pvc = PersistentVolumeClaim(content=result['results'][0])
  1412. elif '\"%s\" not found' % self.config.name in result['stderr']:
  1413. result['returncode'] = 0
  1414. result['results'] = [{}]
  1415. return result
  1416. def delete(self):
  1417. '''delete the object'''
  1418. return self._delete(self.kind, self.config.name)
  1419. def create(self):
  1420. '''create the object'''
  1421. return self._create_from_content(self.config.name, self.config.data)
  1422. def update(self):
  1423. '''update the object'''
  1424. # need to update the tls information and the service name
  1425. return self._replace_content(self.kind, self.config.name, self.config.data)
  1426. def needs_update(self):
  1427. ''' verify an update is needed '''
  1428. if self.pvc.get_volume_name() or self.pvc.is_bound():
  1429. return False
  1430. skip = []
  1431. return not Utils.check_def_equal(self.config.data, self.pvc.yaml_dict, skip_keys=skip, debug=True)
  1432. # pylint: disable=too-many-branches,too-many-return-statements
  1433. @staticmethod
  1434. def run_ansible(params, check_mode):
  1435. '''run the idempotent ansible code'''
  1436. pconfig = PersistentVolumeClaimConfig(params['name'],
  1437. params['namespace'],
  1438. params['kubeconfig'],
  1439. params['access_modes'],
  1440. params['volume_capacity'],
  1441. params['selector'],
  1442. params['storage_class_name'],
  1443. )
  1444. oc_pvc = OCPVC(pconfig, verbose=params['debug'])
  1445. state = params['state']
  1446. api_rval = oc_pvc.get()
  1447. if api_rval['returncode'] != 0:
  1448. return {'failed': True, 'msg': api_rval}
  1449. #####
  1450. # Get
  1451. #####
  1452. if state == 'list':
  1453. return {'changed': False, 'results': api_rval['results'], 'state': state}
  1454. ########
  1455. # Delete
  1456. ########
  1457. if state == 'absent':
  1458. if oc_pvc.exists():
  1459. if check_mode:
  1460. return {'changed': False, 'msg': 'CHECK_MODE: Would have performed a delete.'}
  1461. api_rval = oc_pvc.delete()
  1462. if api_rval['returncode'] != 0:
  1463. return {'failed': True, 'msg': api_rval}
  1464. return {'changed': True, 'results': api_rval, 'state': state}
  1465. return {'changed': False, 'state': state}
  1466. if state == 'present':
  1467. ########
  1468. # Create
  1469. ########
  1470. if not oc_pvc.exists():
  1471. if check_mode:
  1472. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'}
  1473. # Create it here
  1474. api_rval = oc_pvc.create()
  1475. if api_rval['returncode'] != 0:
  1476. return {'failed': True, 'msg': api_rval}
  1477. # return the created object
  1478. api_rval = oc_pvc.get()
  1479. if api_rval['returncode'] != 0:
  1480. return {'failed': True, 'msg': api_rval}
  1481. return {'changed': True, 'results': api_rval, 'state': state}
  1482. ########
  1483. # Update
  1484. ########
  1485. if oc_pvc.pvc.is_bound() or oc_pvc.pvc.get_volume_name():
  1486. api_rval['msg'] = '##### - This volume is currently bound. Will not update - ####'
  1487. return {'changed': False, 'results': api_rval, 'state': state}
  1488. if oc_pvc.needs_update():
  1489. api_rval = oc_pvc.update()
  1490. if api_rval['returncode'] != 0:
  1491. return {'failed': True, 'msg': api_rval}
  1492. # return the created object
  1493. api_rval = oc_pvc.get()
  1494. if api_rval['returncode'] != 0:
  1495. return {'failed': True, 'msg': api_rval}
  1496. return {'changed': True, 'results': api_rval, 'state': state}
  1497. return {'changed': False, 'results': api_rval, 'state': state}
  1498. return {'failed': True, 'msg': 'Unknown state passed. {}'.format(state)}
  1499. # -*- -*- -*- End included fragment: class/oc_pvc.py -*- -*- -*-
  1500. # -*- -*- -*- Begin included fragment: ansible/oc_pvc.py -*- -*- -*-
  1501. #pylint: disable=too-many-branches
  1502. def main():
  1503. '''
  1504. ansible oc module for pvc
  1505. '''
  1506. module = AnsibleModule(
  1507. argument_spec=dict(
  1508. kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'),
  1509. state=dict(default='present', type='str',
  1510. choices=['present', 'absent', 'list']),
  1511. debug=dict(default=False, type='bool'),
  1512. name=dict(default=None, required=True, type='str'),
  1513. namespace=dict(default=None, required=True, type='str'),
  1514. volume_capacity=dict(default='1G', type='str'),
  1515. storage_class_name=dict(default=None, required=False, type='str'),
  1516. selector=dict(default=None, required=False, type='dict'),
  1517. access_modes=dict(default=['ReadWriteOnce'], type='list'),
  1518. ),
  1519. supports_check_mode=True,
  1520. )
  1521. rval = OCPVC.run_ansible(module.params, module.check_mode)
  1522. if 'failed' in rval:
  1523. module.fail_json(**rval)
  1524. return module.exit_json(**rval)
  1525. if __name__ == '__main__':
  1526. main()
  1527. # -*- -*- -*- End included fragment: ansible/oc_pvc.py -*- -*- -*-