oc_service.py 64 KB

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