oc_project.py 59 KB

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