oc_env.py 66 KB

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