oc_adm_router.py 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251
  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/router -*- -*- -*-
  51. DOCUMENTATION = '''
  52. ---
  53. module: oc_adm_router
  54. short_description: Module to manage openshift router
  55. description:
  56. - Manage openshift router programmatically.
  57. options:
  58. state:
  59. description:
  60. - Whether to create or delete the router
  61. - present - create the router
  62. - absent - remove the router
  63. - list - return the current representation of a router
  64. required: false
  65. default: present
  66. choices:
  67. - present
  68. - absent
  69. aliases: []
  70. kubeconfig:
  71. description:
  72. - The path for the kubeconfig file to use for authentication
  73. required: false
  74. default: /etc/origin/master/admin.kubeconfig
  75. aliases: []
  76. debug:
  77. description:
  78. - Turn on debug output.
  79. required: false
  80. default: False
  81. aliases: []
  82. name:
  83. description:
  84. - The name of the router
  85. required: false
  86. default: router
  87. aliases: []
  88. namespace:
  89. description:
  90. - The namespace where to manage the router.
  91. required: false
  92. default: default
  93. aliases: []
  94. images:
  95. description:
  96. - The image to base this router on - ${component} will be replaced with --type
  97. required: 'openshift3/ose-${component}:${version}'
  98. default: None
  99. aliases: []
  100. latest_images:
  101. description:
  102. - If true, attempt to use the latest image for the registry instead of the latest release.
  103. required: false
  104. default: False
  105. aliases: []
  106. labels:
  107. description:
  108. - A set of labels to uniquely identify the registry and its components.
  109. required: false
  110. default: None
  111. aliases: []
  112. ports:
  113. description:
  114. - A list of strings in the 'port:port' format
  115. required: False
  116. default:
  117. - 80:80
  118. - 443:443
  119. aliases: []
  120. replicas:
  121. description:
  122. - The replication factor of the registry; commonly 2 when high availability is desired.
  123. required: False
  124. default: 1
  125. aliases: []
  126. selector:
  127. description:
  128. - Selector used to filter nodes on deployment. Used to run routers on a specific set of nodes.
  129. required: False
  130. default: None
  131. aliases: []
  132. service_account:
  133. description:
  134. - Name of the service account to use to run the router pod.
  135. required: False
  136. default: router
  137. aliases: []
  138. router_type:
  139. description:
  140. - The router image to use - if you specify --images this flag may be ignored.
  141. required: false
  142. default: haproxy-router
  143. aliases: []
  144. external_host:
  145. description:
  146. - If the underlying router implementation connects with an external host, this is the external host's hostname.
  147. required: false
  148. default: None
  149. aliases: []
  150. external_host_vserver:
  151. description:
  152. - If the underlying router implementation uses virtual servers, this is the name of the virtual server for HTTP connections.
  153. required: false
  154. default: None
  155. aliases: []
  156. external_host_insecure:
  157. description:
  158. - If the underlying router implementation connects with an external host
  159. - over a secure connection, this causes the router to skip strict certificate verification with the external host.
  160. required: false
  161. default: False
  162. aliases: []
  163. external_host_partition_path:
  164. description:
  165. - If the underlying router implementation uses partitions for control boundaries, this is the path to use for that partition.
  166. required: false
  167. default: None
  168. aliases: []
  169. external_host_username:
  170. description:
  171. - If the underlying router implementation connects with an external host, this is the username for authenticating with the external host.
  172. required: false
  173. default: None
  174. aliases: []
  175. external_host_password:
  176. description:
  177. - If the underlying router implementation connects with an external host, this is the password for authenticating with the external host.
  178. required: false
  179. default: None
  180. aliases: []
  181. external_host_private_key:
  182. description:
  183. - If the underlying router implementation requires an SSH private key, this is the path to the private key file.
  184. required: false
  185. default: None
  186. aliases: []
  187. expose_metrics:
  188. description:
  189. - This is a hint to run an extra container in the pod to expose metrics - the image
  190. - will either be set depending on the router implementation or provided with --metrics-image.
  191. required: false
  192. default: False
  193. aliases: []
  194. metrics_image:
  195. description:
  196. - If expose_metrics is specified this is the image to use to run a sidecar container
  197. - in the pod exposing metrics. If not set and --expose-metrics is true the image will
  198. - depend on router implementation.
  199. required: false
  200. default: None
  201. aliases: []
  202. author:
  203. - "Kenny Woodson <kwoodson@redhat.com>"
  204. extends_documentation_fragment:
  205. - There are some exceptions to note when doing the idempotency in this module.
  206. - The strategy is to use the oc adm router command to generate a default
  207. - configuration when creating or updating a router. Often times there
  208. - differences from the generated template and what is in memory in openshift.
  209. - We make exceptions to not check these specific values when comparing objects.
  210. - Here are a list of exceptions:
  211. - - DeploymentConfig:
  212. - dnsPolicy
  213. - terminationGracePeriodSeconds
  214. - restartPolicy
  215. - timeoutSeconds
  216. - livenessProbe
  217. - readinessProbe
  218. - terminationMessagePath
  219. - hostPort
  220. - defaultMode
  221. - Service:
  222. - portalIP
  223. - clusterIP
  224. - sessionAffinity
  225. - type
  226. - ServiceAccount:
  227. - secrets
  228. - imagePullSecrets
  229. '''
  230. EXAMPLES = '''
  231. - name: create routers
  232. oc_adm_router:
  233. name: router
  234. service_account: router
  235. replicas: 2
  236. namespace: default
  237. selector: type=infra
  238. cert_file: /etc/origin/master/named_certificates/router.crt
  239. key_file: /etc/origin/master/named_certificates/router.key
  240. cacert_file: /etc/origin/master/named_certificates/router.ca
  241. edits:
  242. - key: spec.strategy.rollingParams
  243. value:
  244. intervalSeconds: 1
  245. maxSurge: 50%
  246. maxUnavailable: 50%
  247. timeoutSeconds: 600
  248. updatePeriodSeconds: 1
  249. action: put
  250. - key: spec.template.spec.containers[0].resources.limits.memory
  251. value: 2G
  252. action: put
  253. - key: spec.template.spec.containers[0].resources.requests.memory
  254. value: 1G
  255. action: put
  256. - key: spec.template.spec.containers[0].env
  257. value:
  258. name: EXTENDED_VALIDATION
  259. value: 'false'
  260. action: update
  261. register: router_out
  262. run_once: True
  263. '''
  264. # -*- -*- -*- End included fragment: doc/router -*- -*- -*-
  265. # -*- -*- -*- Begin included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  266. class YeditException(Exception): # pragma: no cover
  267. ''' Exception class for Yedit '''
  268. pass
  269. # pylint: disable=too-many-public-methods,too-many-instance-attributes
  270. class Yedit(object): # pragma: no cover
  271. ''' Class to modify yaml files '''
  272. re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$"
  273. re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z{}/_-]+)"
  274. com_sep = set(['.', '#', '|', ':'])
  275. # pylint: disable=too-many-arguments
  276. def __init__(self,
  277. filename=None,
  278. content=None,
  279. content_type='yaml',
  280. separator='.',
  281. backup_ext=None,
  282. backup=False):
  283. self.content = content
  284. self._separator = separator
  285. self.filename = filename
  286. self.__yaml_dict = content
  287. self.content_type = content_type
  288. self.backup = backup
  289. if backup_ext is None:
  290. self.backup_ext = ".{}".format(time.strftime("%Y%m%dT%H%M%S"))
  291. else:
  292. self.backup_ext = backup_ext
  293. self.load(content_type=self.content_type)
  294. if self.__yaml_dict is None:
  295. self.__yaml_dict = {}
  296. @property
  297. def separator(self):
  298. ''' getter method for separator '''
  299. return self._separator
  300. @separator.setter
  301. def separator(self, inc_sep):
  302. ''' setter method for separator '''
  303. self._separator = inc_sep
  304. @property
  305. def yaml_dict(self):
  306. ''' getter method for yaml_dict '''
  307. return self.__yaml_dict
  308. @yaml_dict.setter
  309. def yaml_dict(self, value):
  310. ''' setter method for yaml_dict '''
  311. self.__yaml_dict = value
  312. @staticmethod
  313. def parse_key(key, sep='.'):
  314. '''parse the key allowing the appropriate separator'''
  315. common_separators = list(Yedit.com_sep - set([sep]))
  316. return re.findall(Yedit.re_key.format(''.join(common_separators)), key)
  317. @staticmethod
  318. def valid_key(key, sep='.'):
  319. '''validate the incoming key'''
  320. common_separators = list(Yedit.com_sep - set([sep]))
  321. if not re.match(Yedit.re_valid_key.format(''.join(common_separators)), key):
  322. return False
  323. return True
  324. # pylint: disable=too-many-return-statements,too-many-branches
  325. @staticmethod
  326. def remove_entry(data, key, index=None, value=None, sep='.'):
  327. ''' remove data at location key '''
  328. if key == '' and isinstance(data, dict):
  329. if value is not None:
  330. data.pop(value)
  331. elif index is not None:
  332. raise YeditException("remove_entry for a dictionary does not have an index {}".format(index))
  333. else:
  334. data.clear()
  335. return True
  336. elif key == '' and isinstance(data, list):
  337. ind = None
  338. if value is not None:
  339. try:
  340. ind = data.index(value)
  341. except ValueError:
  342. return False
  343. elif index is not None:
  344. ind = index
  345. else:
  346. del data[:]
  347. if ind is not None:
  348. data.pop(ind)
  349. return True
  350. if not (key and Yedit.valid_key(key, sep)) and \
  351. isinstance(data, (list, dict)):
  352. return None
  353. key_indexes = Yedit.parse_key(key, sep)
  354. for arr_ind, dict_key in key_indexes[:-1]:
  355. if dict_key and isinstance(data, dict):
  356. data = data.get(dict_key)
  357. elif (arr_ind and isinstance(data, list) and
  358. int(arr_ind) <= len(data) - 1):
  359. data = data[int(arr_ind)]
  360. else:
  361. return None
  362. # process last index for remove
  363. # expected list entry
  364. if key_indexes[-1][0]:
  365. if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  366. del data[int(key_indexes[-1][0])]
  367. return True
  368. # expected dict entry
  369. elif key_indexes[-1][1]:
  370. if isinstance(data, dict):
  371. del data[key_indexes[-1][1]]
  372. return True
  373. @staticmethod
  374. def add_entry(data, key, item=None, sep='.'):
  375. ''' Get an item from a dictionary with key notation a.b.c
  376. d = {'a': {'b': 'c'}}}
  377. key = a#b
  378. return c
  379. '''
  380. if key == '':
  381. pass
  382. elif (not (key and Yedit.valid_key(key, sep)) and
  383. isinstance(data, (list, dict))):
  384. return None
  385. key_indexes = Yedit.parse_key(key, sep)
  386. for arr_ind, dict_key in key_indexes[:-1]:
  387. if dict_key:
  388. if isinstance(data, dict) and dict_key in data and data[dict_key]: # noqa: E501
  389. data = data[dict_key]
  390. continue
  391. elif data and not isinstance(data, dict):
  392. raise YeditException("Unexpected item type found while going through key " +
  393. "path: {} (at key: {})".format(key, dict_key))
  394. data[dict_key] = {}
  395. data = data[dict_key]
  396. elif (arr_ind and isinstance(data, list) and
  397. int(arr_ind) <= len(data) - 1):
  398. data = data[int(arr_ind)]
  399. else:
  400. raise YeditException("Unexpected item type found while going through key path: {}".format(key))
  401. if key == '':
  402. data = item
  403. # process last index for add
  404. # expected list entry
  405. elif key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  406. data[int(key_indexes[-1][0])] = item
  407. # expected dict entry
  408. elif key_indexes[-1][1] and isinstance(data, dict):
  409. data[key_indexes[-1][1]] = item
  410. # didn't add/update to an existing list, nor add/update key to a dict
  411. # so we must have been provided some syntax like a.b.c[<int>] = "data" for a
  412. # non-existent array
  413. else:
  414. raise YeditException("Error adding to object at path: {}".format(key))
  415. return data
  416. @staticmethod
  417. def get_entry(data, key, sep='.'):
  418. ''' Get an item from a dictionary with key notation a.b.c
  419. d = {'a': {'b': 'c'}}}
  420. key = a.b
  421. return c
  422. '''
  423. if key == '':
  424. pass
  425. elif (not (key and Yedit.valid_key(key, sep)) and
  426. isinstance(data, (list, dict))):
  427. return None
  428. key_indexes = Yedit.parse_key(key, sep)
  429. for arr_ind, dict_key in key_indexes:
  430. if dict_key and isinstance(data, dict):
  431. data = data.get(dict_key)
  432. elif (arr_ind and isinstance(data, list) and
  433. int(arr_ind) <= len(data) - 1):
  434. data = data[int(arr_ind)]
  435. else:
  436. return None
  437. return data
  438. @staticmethod
  439. def _write(filename, contents):
  440. ''' Actually write the file contents to disk. This helps with mocking. '''
  441. tmp_filename = filename + '.yedit'
  442. with open(tmp_filename, 'w') as yfd:
  443. fcntl.flock(yfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
  444. yfd.write(contents)
  445. fcntl.flock(yfd, fcntl.LOCK_UN)
  446. os.rename(tmp_filename, filename)
  447. def write(self):
  448. ''' write to file '''
  449. if not self.filename:
  450. raise YeditException('Please specify a filename.')
  451. if self.backup and self.file_exists():
  452. shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))
  453. # Try to set format attributes if supported
  454. try:
  455. self.yaml_dict.fa.set_block_style()
  456. except AttributeError:
  457. pass
  458. # Try to use RoundTripDumper if supported.
  459. if self.content_type == 'yaml':
  460. try:
  461. Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
  462. except AttributeError:
  463. Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
  464. elif self.content_type == 'json':
  465. Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
  466. else:
  467. raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
  468. 'Please specify a content_type of yaml or json.')
  469. return (True, self.yaml_dict)
  470. def read(self):
  471. ''' read from file '''
  472. # check if it exists
  473. if self.filename is None or not self.file_exists():
  474. return None
  475. contents = None
  476. with open(self.filename) as yfd:
  477. contents = yfd.read()
  478. return contents
  479. def file_exists(self):
  480. ''' return whether file exists '''
  481. if os.path.exists(self.filename):
  482. return True
  483. return False
  484. def load(self, content_type='yaml'):
  485. ''' return yaml file '''
  486. contents = self.read()
  487. if not contents and not self.content:
  488. return None
  489. if self.content:
  490. if isinstance(self.content, dict):
  491. self.yaml_dict = self.content
  492. return self.yaml_dict
  493. elif isinstance(self.content, str):
  494. contents = self.content
  495. # check if it is yaml
  496. try:
  497. if content_type == 'yaml' and contents:
  498. # Try to set format attributes if supported
  499. try:
  500. self.yaml_dict.fa.set_block_style()
  501. except AttributeError:
  502. pass
  503. # Try to use RoundTripLoader if supported.
  504. try:
  505. self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
  506. except AttributeError:
  507. self.yaml_dict = yaml.safe_load(contents)
  508. # Try to set format attributes if supported
  509. try:
  510. self.yaml_dict.fa.set_block_style()
  511. except AttributeError:
  512. pass
  513. elif content_type == 'json' and contents:
  514. self.yaml_dict = json.loads(contents)
  515. except yaml.YAMLError as err:
  516. # Error loading yaml or json
  517. raise YeditException('Problem with loading yaml file. {}'.format(err))
  518. return self.yaml_dict
  519. def get(self, key):
  520. ''' get a specified key'''
  521. try:
  522. entry = Yedit.get_entry(self.yaml_dict, key, self.separator)
  523. except KeyError:
  524. entry = None
  525. return entry
  526. def pop(self, path, key_or_item):
  527. ''' remove a key, value pair from a dict or an item for a list'''
  528. try:
  529. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  530. except KeyError:
  531. entry = None
  532. if entry is None:
  533. return (False, self.yaml_dict)
  534. if isinstance(entry, dict):
  535. # AUDIT:maybe-no-member makes sense due to fuzzy types
  536. # pylint: disable=maybe-no-member
  537. if key_or_item in entry:
  538. entry.pop(key_or_item)
  539. return (True, self.yaml_dict)
  540. return (False, self.yaml_dict)
  541. elif isinstance(entry, list):
  542. # AUDIT:maybe-no-member makes sense due to fuzzy types
  543. # pylint: disable=maybe-no-member
  544. ind = None
  545. try:
  546. ind = entry.index(key_or_item)
  547. except ValueError:
  548. return (False, self.yaml_dict)
  549. entry.pop(ind)
  550. return (True, self.yaml_dict)
  551. return (False, self.yaml_dict)
  552. def delete(self, path, index=None, value=None):
  553. ''' remove path from a dict'''
  554. try:
  555. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  556. except KeyError:
  557. entry = None
  558. if entry is None:
  559. return (False, self.yaml_dict)
  560. result = Yedit.remove_entry(self.yaml_dict, path, index, value, self.separator)
  561. if not result:
  562. return (False, self.yaml_dict)
  563. return (True, self.yaml_dict)
  564. def exists(self, path, value):
  565. ''' check if value exists at path'''
  566. try:
  567. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  568. except KeyError:
  569. entry = None
  570. if isinstance(entry, list):
  571. if value in entry:
  572. return True
  573. return False
  574. elif isinstance(entry, dict):
  575. if isinstance(value, dict):
  576. rval = False
  577. for key, val in value.items():
  578. if entry[key] != val:
  579. rval = False
  580. break
  581. else:
  582. rval = True
  583. return rval
  584. return value in entry
  585. return entry == value
  586. def append(self, path, value):
  587. '''append value to a list'''
  588. try:
  589. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  590. except KeyError:
  591. entry = None
  592. if entry is None:
  593. self.put(path, [])
  594. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  595. if not isinstance(entry, list):
  596. return (False, self.yaml_dict)
  597. # AUDIT:maybe-no-member makes sense due to loading data from
  598. # a serialized format.
  599. # pylint: disable=maybe-no-member
  600. entry.append(value)
  601. return (True, self.yaml_dict)
  602. # pylint: disable=too-many-arguments
  603. def update(self, path, value, index=None, curr_value=None):
  604. ''' put path, value into a dict '''
  605. try:
  606. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  607. except KeyError:
  608. entry = None
  609. if isinstance(entry, dict):
  610. # AUDIT:maybe-no-member makes sense due to fuzzy types
  611. # pylint: disable=maybe-no-member
  612. if not isinstance(value, dict):
  613. raise YeditException('Cannot replace key, value entry in dict with non-dict type. ' +
  614. 'value=[{}] type=[{}]'.format(value, type(value)))
  615. entry.update(value)
  616. return (True, self.yaml_dict)
  617. elif isinstance(entry, list):
  618. # AUDIT:maybe-no-member makes sense due to fuzzy types
  619. # pylint: disable=maybe-no-member
  620. ind = None
  621. if curr_value:
  622. try:
  623. ind = entry.index(curr_value)
  624. except ValueError:
  625. return (False, self.yaml_dict)
  626. elif index is not None:
  627. ind = index
  628. if ind is not None and entry[ind] != value:
  629. entry[ind] = value
  630. return (True, self.yaml_dict)
  631. # see if it exists in the list
  632. try:
  633. ind = entry.index(value)
  634. except ValueError:
  635. # doesn't exist, append it
  636. entry.append(value)
  637. return (True, self.yaml_dict)
  638. # already exists, return
  639. if ind is not None:
  640. return (False, self.yaml_dict)
  641. return (False, self.yaml_dict)
  642. def put(self, path, value):
  643. ''' put path, value into a dict '''
  644. try:
  645. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  646. except KeyError:
  647. entry = None
  648. if entry == value:
  649. return (False, self.yaml_dict)
  650. # deepcopy didn't work
  651. # Try to use ruamel.yaml and fallback to pyyaml
  652. try:
  653. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  654. default_flow_style=False),
  655. yaml.RoundTripLoader)
  656. except AttributeError:
  657. tmp_copy = copy.deepcopy(self.yaml_dict)
  658. # set the format attributes if available
  659. try:
  660. tmp_copy.fa.set_block_style()
  661. except AttributeError:
  662. pass
  663. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  664. if result is None:
  665. return (False, self.yaml_dict)
  666. # When path equals "" it is a special case.
  667. # "" refers to the root of the document
  668. # Only update the root path (entire document) when its a list or dict
  669. if path == '':
  670. if isinstance(result, list) or isinstance(result, dict):
  671. self.yaml_dict = result
  672. return (True, self.yaml_dict)
  673. return (False, self.yaml_dict)
  674. self.yaml_dict = tmp_copy
  675. return (True, self.yaml_dict)
  676. def create(self, path, value):
  677. ''' create a yaml file '''
  678. if not self.file_exists():
  679. # deepcopy didn't work
  680. # Try to use ruamel.yaml and fallback to pyyaml
  681. try:
  682. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  683. default_flow_style=False),
  684. yaml.RoundTripLoader)
  685. except AttributeError:
  686. tmp_copy = copy.deepcopy(self.yaml_dict)
  687. # set the format attributes if available
  688. try:
  689. tmp_copy.fa.set_block_style()
  690. except AttributeError:
  691. pass
  692. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  693. if result is not None:
  694. self.yaml_dict = tmp_copy
  695. return (True, self.yaml_dict)
  696. return (False, self.yaml_dict)
  697. @staticmethod
  698. def get_curr_value(invalue, val_type):
  699. '''return the current value'''
  700. if invalue is None:
  701. return None
  702. curr_value = invalue
  703. if val_type == 'yaml':
  704. curr_value = yaml.safe_load(str(invalue))
  705. elif val_type == 'json':
  706. curr_value = json.loads(invalue)
  707. return curr_value
  708. @staticmethod
  709. def parse_value(inc_value, vtype=''):
  710. '''determine value type passed'''
  711. true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE',
  712. 'on', 'On', 'ON', ]
  713. false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE',
  714. 'off', 'Off', 'OFF']
  715. # It came in as a string but you didn't specify value_type as string
  716. # we will convert to bool if it matches any of the above cases
  717. if isinstance(inc_value, str) and 'bool' in vtype:
  718. if inc_value not in true_bools and inc_value not in false_bools:
  719. raise YeditException('Not a boolean type. str=[{}] vtype=[{}]'.format(inc_value, vtype))
  720. elif isinstance(inc_value, bool) and 'str' in vtype:
  721. inc_value = str(inc_value)
  722. # There is a special case where '' will turn into None after yaml loading it so skip
  723. if isinstance(inc_value, str) and inc_value == '':
  724. pass
  725. # If vtype is not str then go ahead and attempt to yaml load it.
  726. elif isinstance(inc_value, str) and 'str' not in vtype:
  727. try:
  728. inc_value = yaml.safe_load(inc_value)
  729. except Exception:
  730. raise YeditException('Could not determine type of incoming value. ' +
  731. 'value=[{}] vtype=[{}]'.format(type(inc_value), vtype))
  732. return inc_value
  733. @staticmethod
  734. def process_edits(edits, yamlfile):
  735. '''run through a list of edits and process them one-by-one'''
  736. results = []
  737. for edit in edits:
  738. value = Yedit.parse_value(edit['value'], edit.get('value_type', ''))
  739. if edit.get('action') == 'update':
  740. # pylint: disable=line-too-long
  741. curr_value = Yedit.get_curr_value(
  742. Yedit.parse_value(edit.get('curr_value')),
  743. edit.get('curr_value_format'))
  744. rval = yamlfile.update(edit['key'],
  745. value,
  746. edit.get('index'),
  747. curr_value)
  748. elif edit.get('action') == 'append':
  749. rval = yamlfile.append(edit['key'], value)
  750. else:
  751. rval = yamlfile.put(edit['key'], value)
  752. if rval[0]:
  753. results.append({'key': edit['key'], 'edit': rval[1]})
  754. return {'changed': len(results) > 0, 'results': results}
  755. # pylint: disable=too-many-return-statements,too-many-branches
  756. @staticmethod
  757. def run_ansible(params):
  758. '''perform the idempotent crud operations'''
  759. yamlfile = Yedit(filename=params['src'],
  760. backup=params['backup'],
  761. content_type=params['content_type'],
  762. backup_ext=params['backup_ext'],
  763. separator=params['separator'])
  764. state = params['state']
  765. if params['src']:
  766. rval = yamlfile.load()
  767. if yamlfile.yaml_dict is None and state != 'present':
  768. return {'failed': True,
  769. 'msg': 'Error opening file [{}]. Verify that the '.format(params['src']) +
  770. 'file exists, that it is has correct permissions, and is valid yaml.'}
  771. if state == 'list':
  772. if params['content']:
  773. content = Yedit.parse_value(params['content'], params['content_type'])
  774. yamlfile.yaml_dict = content
  775. if params['key']:
  776. rval = yamlfile.get(params['key'])
  777. return {'changed': False, 'result': rval, 'state': state}
  778. elif state == 'absent':
  779. if params['content']:
  780. content = Yedit.parse_value(params['content'], params['content_type'])
  781. yamlfile.yaml_dict = content
  782. if params['update']:
  783. rval = yamlfile.pop(params['key'], params['value'])
  784. else:
  785. rval = yamlfile.delete(params['key'], params['index'], params['value'])
  786. if rval[0] and params['src']:
  787. yamlfile.write()
  788. return {'changed': rval[0], 'result': rval[1], 'state': state}
  789. elif state == 'present':
  790. # check if content is different than what is in the file
  791. if params['content']:
  792. content = Yedit.parse_value(params['content'], params['content_type'])
  793. # We had no edits to make and the contents are the same
  794. if yamlfile.yaml_dict == content and \
  795. params['value'] is None:
  796. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  797. yamlfile.yaml_dict = content
  798. # If we were passed a key, value then
  799. # we enapsulate it in a list and process it
  800. # Key, Value passed to the module : Converted to Edits list #
  801. edits = []
  802. _edit = {}
  803. if params['value'] is not None:
  804. _edit['value'] = params['value']
  805. _edit['value_type'] = params['value_type']
  806. _edit['key'] = params['key']
  807. if params['update']:
  808. _edit['action'] = 'update'
  809. _edit['curr_value'] = params['curr_value']
  810. _edit['curr_value_format'] = params['curr_value_format']
  811. _edit['index'] = params['index']
  812. elif params['append']:
  813. _edit['action'] = 'append'
  814. edits.append(_edit)
  815. elif params['edits'] is not None:
  816. edits = params['edits']
  817. if edits:
  818. results = Yedit.process_edits(edits, yamlfile)
  819. # if there were changes and a src provided to us we need to write
  820. if results['changed'] and params['src']:
  821. yamlfile.write()
  822. return {'changed': results['changed'], 'result': results['results'], 'state': state}
  823. # no edits to make
  824. if params['src']:
  825. # pylint: disable=redefined-variable-type
  826. rval = yamlfile.write()
  827. return {'changed': rval[0],
  828. 'result': rval[1],
  829. 'state': state}
  830. # We were passed content but no src, key or value, or edits. Return contents in memory
  831. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  832. return {'failed': True, 'msg': 'Unkown state passed'}
  833. # -*- -*- -*- End included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  834. # -*- -*- -*- Begin included fragment: lib/base.py -*- -*- -*-
  835. # pylint: disable=too-many-lines
  836. # noqa: E301,E302,E303,T001
  837. class OpenShiftCLIError(Exception):
  838. '''Exception class for openshiftcli'''
  839. pass
  840. ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')]
  841. def locate_oc_binary():
  842. ''' Find and return oc binary file '''
  843. # https://github.com/openshift/openshift-ansible/issues/3410
  844. # oc can be in /usr/local/bin in some cases, but that may not
  845. # be in $PATH due to ansible/sudo
  846. paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS
  847. oc_binary = 'oc'
  848. # Use shutil.which if it is available, otherwise fallback to a naive path search
  849. try:
  850. which_result = shutil.which(oc_binary, path=os.pathsep.join(paths))
  851. if which_result is not None:
  852. oc_binary = which_result
  853. except AttributeError:
  854. for path in paths:
  855. if os.path.exists(os.path.join(path, oc_binary)):
  856. oc_binary = os.path.join(path, oc_binary)
  857. break
  858. return oc_binary
  859. # pylint: disable=too-few-public-methods
  860. class OpenShiftCLI(object):
  861. ''' Class to wrap the command line tools '''
  862. def __init__(self,
  863. namespace,
  864. kubeconfig='/etc/origin/master/admin.kubeconfig',
  865. verbose=False,
  866. all_namespaces=False):
  867. ''' Constructor for OpenshiftCLI '''
  868. self.namespace = namespace
  869. self.verbose = verbose
  870. self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig)
  871. self.all_namespaces = all_namespaces
  872. self.oc_binary = locate_oc_binary()
  873. # Pylint allows only 5 arguments to be passed.
  874. # pylint: disable=too-many-arguments
  875. def _replace_content(self, resource, rname, content, edits=None, force=False, sep='.'):
  876. ''' replace the current object with the content '''
  877. res = self._get(resource, rname)
  878. if not res['results']:
  879. return res
  880. fname = Utils.create_tmpfile(rname + '-')
  881. yed = Yedit(fname, res['results'][0], separator=sep)
  882. updated = False
  883. if content is not None:
  884. changes = []
  885. for key, value in content.items():
  886. changes.append(yed.put(key, value))
  887. if any([change[0] for change in changes]):
  888. updated = True
  889. elif edits is not None:
  890. results = Yedit.process_edits(edits, yed)
  891. if results['changed']:
  892. updated = True
  893. if updated:
  894. yed.write()
  895. atexit.register(Utils.cleanup, [fname])
  896. return self._replace(fname, force)
  897. return {'returncode': 0, 'updated': False}
  898. def _replace(self, fname, force=False):
  899. '''replace the current object with oc replace'''
  900. # We are removing the 'resourceVersion' to handle
  901. # a race condition when modifying oc objects
  902. yed = Yedit(fname)
  903. results = yed.delete('metadata.resourceVersion')
  904. if results[0]:
  905. yed.write()
  906. cmd = ['replace', '-f', fname]
  907. if force:
  908. cmd.append('--force')
  909. return self.openshift_cmd(cmd)
  910. def _create_from_content(self, rname, content):
  911. '''create a temporary file and then call oc create on it'''
  912. fname = Utils.create_tmpfile(rname + '-')
  913. yed = Yedit(fname, content=content)
  914. yed.write()
  915. atexit.register(Utils.cleanup, [fname])
  916. return self._create(fname)
  917. def _create(self, fname):
  918. '''call oc create on a filename'''
  919. return self.openshift_cmd(['create', '-f', fname])
  920. def _delete(self, resource, name=None, selector=None):
  921. '''call oc delete on a resource'''
  922. cmd = ['delete', resource]
  923. if selector is not None:
  924. cmd.append('--selector={}'.format(selector))
  925. elif name is not None:
  926. cmd.append(name)
  927. else:
  928. raise OpenShiftCLIError('Either name or selector is required when calling delete.')
  929. return self.openshift_cmd(cmd)
  930. def _process(self, template_name, create=False, params=None, template_data=None): # noqa: E501
  931. '''process a template
  932. template_name: the name of the template to process
  933. create: whether to send to oc create after processing
  934. params: the parameters for the template
  935. template_data: the incoming template's data; instead of a file
  936. '''
  937. cmd = ['process']
  938. if template_data:
  939. cmd.extend(['-f', '-'])
  940. else:
  941. cmd.append(template_name)
  942. if params:
  943. param_str = ["{}={}".format(key, str(value).replace("'", r'"')) for key, value in params.items()]
  944. cmd.append('-p')
  945. cmd.extend(param_str)
  946. results = self.openshift_cmd(cmd, output=True, input_data=template_data)
  947. if results['returncode'] != 0 or not create:
  948. return results
  949. fname = Utils.create_tmpfile(template_name + '-')
  950. yed = Yedit(fname, results['results'])
  951. yed.write()
  952. atexit.register(Utils.cleanup, [fname])
  953. return self.openshift_cmd(['create', '-f', fname])
  954. def _get(self, resource, name=None, selector=None, field_selector=None):
  955. '''return a resource by name '''
  956. cmd = ['get', resource]
  957. if selector is not None:
  958. cmd.append('--selector={}'.format(selector))
  959. if field_selector is not None:
  960. cmd.append('--field-selector={}'.format(field_selector))
  961. # Name cannot be used with selector or field_selector.
  962. if selector is None and field_selector is None and name is not None:
  963. cmd.append(name)
  964. cmd.extend(['-o', 'json'])
  965. rval = self.openshift_cmd(cmd, output=True)
  966. # Ensure results are retuned in an array
  967. if 'items' in rval:
  968. rval['results'] = rval['items']
  969. elif not isinstance(rval['results'], list):
  970. rval['results'] = [rval['results']]
  971. return rval
  972. def _schedulable(self, node=None, selector=None, schedulable=True):
  973. ''' perform oadm manage-node scheduable '''
  974. cmd = ['manage-node']
  975. if node:
  976. cmd.extend(node)
  977. else:
  978. cmd.append('--selector={}'.format(selector))
  979. cmd.append('--schedulable={}'.format(schedulable))
  980. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') # noqa: E501
  981. def _list_pods(self, node=None, selector=None, pod_selector=None):
  982. ''' perform oadm list pods
  983. node: the node in which to list pods
  984. selector: the label selector filter if provided
  985. pod_selector: the pod selector filter if provided
  986. '''
  987. cmd = ['manage-node']
  988. if node:
  989. cmd.extend(node)
  990. else:
  991. cmd.append('--selector={}'.format(selector))
  992. if pod_selector:
  993. cmd.append('--pod-selector={}'.format(pod_selector))
  994. cmd.extend(['--list-pods', '-o', 'json'])
  995. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  996. # pylint: disable=too-many-arguments
  997. def _evacuate(self, node=None, selector=None, pod_selector=None, dry_run=False, grace_period=None, force=False):
  998. ''' perform oadm manage-node evacuate '''
  999. cmd = ['manage-node']
  1000. if node:
  1001. cmd.extend(node)
  1002. else:
  1003. cmd.append('--selector={}'.format(selector))
  1004. if dry_run:
  1005. cmd.append('--dry-run')
  1006. if pod_selector:
  1007. cmd.append('--pod-selector={}'.format(pod_selector))
  1008. if grace_period:
  1009. cmd.append('--grace-period={}'.format(int(grace_period)))
  1010. if force:
  1011. cmd.append('--force')
  1012. cmd.append('--evacuate')
  1013. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  1014. def _version(self):
  1015. ''' return the openshift version'''
  1016. return self.openshift_cmd(['version'], output=True, output_type='raw')
  1017. def _import_image(self, url=None, name=None, tag=None):
  1018. ''' perform image import '''
  1019. cmd = ['import-image']
  1020. image = '{0}'.format(name)
  1021. if tag:
  1022. image += ':{0}'.format(tag)
  1023. cmd.append(image)
  1024. if url:
  1025. cmd.append('--from={0}/{1}'.format(url, image))
  1026. cmd.append('-n{0}'.format(self.namespace))
  1027. cmd.append('--confirm')
  1028. return self.openshift_cmd(cmd)
  1029. def _run(self, cmds, input_data):
  1030. ''' Actually executes the command. This makes mocking easier. '''
  1031. curr_env = os.environ.copy()
  1032. curr_env.update({'KUBECONFIG': self.kubeconfig})
  1033. proc = subprocess.Popen(cmds,
  1034. stdin=subprocess.PIPE,
  1035. stdout=subprocess.PIPE,
  1036. stderr=subprocess.PIPE,
  1037. env=curr_env)
  1038. stdout, stderr = proc.communicate(input_data)
  1039. return proc.returncode, stdout.decode('utf-8'), stderr.decode('utf-8')
  1040. # pylint: disable=too-many-arguments,too-many-branches
  1041. def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None):
  1042. '''Base command for oc '''
  1043. cmds = [self.oc_binary]
  1044. if oadm:
  1045. cmds.append('adm')
  1046. cmds.extend(cmd)
  1047. if self.all_namespaces:
  1048. cmds.extend(['--all-namespaces'])
  1049. elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501
  1050. cmds.extend(['-n', self.namespace])
  1051. if self.verbose:
  1052. print(' '.join(cmds))
  1053. try:
  1054. returncode, stdout, stderr = self._run(cmds, input_data)
  1055. except OSError as ex:
  1056. returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex)
  1057. rval = {"returncode": returncode,
  1058. "cmd": ' '.join(cmds)}
  1059. if output_type == 'json':
  1060. rval['results'] = {}
  1061. if output and stdout:
  1062. try:
  1063. rval['results'] = json.loads(stdout)
  1064. except ValueError as verr:
  1065. if "No JSON object could be decoded" in verr.args:
  1066. rval['err'] = verr.args
  1067. elif output_type == 'raw':
  1068. rval['results'] = stdout if output else ''
  1069. if self.verbose:
  1070. print("STDOUT: {0}".format(stdout))
  1071. print("STDERR: {0}".format(stderr))
  1072. if 'err' in rval or returncode != 0:
  1073. rval.update({"stderr": stderr,
  1074. "stdout": stdout})
  1075. return rval
  1076. class Utils(object): # pragma: no cover
  1077. ''' utilities for openshiftcli modules '''
  1078. @staticmethod
  1079. def _write(filename, contents):
  1080. ''' Actually write the file contents to disk. This helps with mocking. '''
  1081. with open(filename, 'w') as sfd:
  1082. sfd.write(str(contents))
  1083. @staticmethod
  1084. def create_tmp_file_from_contents(rname, data, ftype='yaml'):
  1085. ''' create a file in tmp with name and contents'''
  1086. tmp = Utils.create_tmpfile(prefix=rname)
  1087. if ftype == 'yaml':
  1088. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  1089. # pylint: disable=no-member
  1090. if hasattr(yaml, 'RoundTripDumper'):
  1091. Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
  1092. else:
  1093. Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))
  1094. elif ftype == 'json':
  1095. Utils._write(tmp, json.dumps(data))
  1096. else:
  1097. Utils._write(tmp, data)
  1098. # Register cleanup when module is done
  1099. atexit.register(Utils.cleanup, [tmp])
  1100. return tmp
  1101. @staticmethod
  1102. def create_tmpfile_copy(inc_file):
  1103. '''create a temporary copy of a file'''
  1104. tmpfile = Utils.create_tmpfile('lib_openshift-')
  1105. Utils._write(tmpfile, open(inc_file).read())
  1106. # Cleanup the tmpfile
  1107. atexit.register(Utils.cleanup, [tmpfile])
  1108. return tmpfile
  1109. @staticmethod
  1110. def create_tmpfile(prefix='tmp'):
  1111. ''' Generates and returns a temporary file name '''
  1112. with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as tmp:
  1113. return tmp.name
  1114. @staticmethod
  1115. def create_tmp_files_from_contents(content, content_type=None):
  1116. '''Turn an array of dict: filename, content into a files array'''
  1117. if not isinstance(content, list):
  1118. content = [content]
  1119. files = []
  1120. for item in content:
  1121. path = Utils.create_tmp_file_from_contents(item['path'] + '-',
  1122. item['data'],
  1123. ftype=content_type)
  1124. files.append({'name': os.path.basename(item['path']),
  1125. 'path': path})
  1126. return files
  1127. @staticmethod
  1128. def cleanup(files):
  1129. '''Clean up on exit '''
  1130. for sfile in files:
  1131. if os.path.exists(sfile):
  1132. if os.path.isdir(sfile):
  1133. shutil.rmtree(sfile)
  1134. elif os.path.isfile(sfile):
  1135. os.remove(sfile)
  1136. @staticmethod
  1137. def exists(results, _name):
  1138. ''' Check to see if the results include the name '''
  1139. if not results:
  1140. return False
  1141. if Utils.find_result(results, _name):
  1142. return True
  1143. return False
  1144. @staticmethod
  1145. def find_result(results, _name):
  1146. ''' Find the specified result by name'''
  1147. rval = None
  1148. for result in results:
  1149. if 'metadata' in result and result['metadata']['name'] == _name:
  1150. rval = result
  1151. break
  1152. return rval
  1153. @staticmethod
  1154. def get_resource_file(sfile, sfile_type='yaml'):
  1155. ''' return the service file '''
  1156. contents = None
  1157. with open(sfile) as sfd:
  1158. contents = sfd.read()
  1159. if sfile_type == 'yaml':
  1160. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  1161. # pylint: disable=no-member
  1162. if hasattr(yaml, 'RoundTripLoader'):
  1163. contents = yaml.load(contents, yaml.RoundTripLoader)
  1164. else:
  1165. contents = yaml.safe_load(contents)
  1166. elif sfile_type == 'json':
  1167. contents = json.loads(contents)
  1168. return contents
  1169. @staticmethod
  1170. def filter_versions(stdout):
  1171. ''' filter the oc version output '''
  1172. version_dict = {}
  1173. version_search = ['oc', 'openshift', 'kubernetes']
  1174. for line in stdout.strip().split('\n'):
  1175. for term in version_search:
  1176. if not line:
  1177. continue
  1178. if line.startswith(term):
  1179. version_dict[term] = line.split()[-1]
  1180. # horrible hack to get openshift version in Openshift 3.2
  1181. # By default "oc version in 3.2 does not return an "openshift" version
  1182. if "openshift" not in version_dict:
  1183. version_dict["openshift"] = version_dict["oc"]
  1184. return version_dict
  1185. @staticmethod
  1186. def add_custom_versions(versions):
  1187. ''' create custom versions strings '''
  1188. versions_dict = {}
  1189. for tech, version in versions.items():
  1190. # clean up "-" from version
  1191. if "-" in version:
  1192. version = version.split("-")[0]
  1193. if version.startswith('v'):
  1194. versions_dict[tech + '_numeric'] = version[1:].split('+')[0]
  1195. # "v3.3.0.33" is what we have, we want "3.3"
  1196. versions_dict[tech + '_short'] = version[1:4]
  1197. return versions_dict
  1198. @staticmethod
  1199. def openshift_installed():
  1200. ''' check if openshift is installed '''
  1201. import rpm
  1202. transaction_set = rpm.TransactionSet()
  1203. rpmquery = transaction_set.dbMatch("name", "atomic-openshift")
  1204. return rpmquery.count() > 0
  1205. # Disabling too-many-branches. This is a yaml dictionary comparison function
  1206. # pylint: disable=too-many-branches,too-many-return-statements,too-many-statements
  1207. @staticmethod
  1208. def check_def_equal(user_def, result_def, skip_keys=None, debug=False):
  1209. ''' Given a user defined definition, compare it with the results given back by our query. '''
  1210. # Currently these values are autogenerated and we do not need to check them
  1211. skip = ['metadata', 'status']
  1212. if skip_keys:
  1213. skip.extend(skip_keys)
  1214. for key, value in result_def.items():
  1215. if key in skip:
  1216. continue
  1217. # Both are lists
  1218. if isinstance(value, list):
  1219. if key not in user_def:
  1220. if debug:
  1221. print('User data does not have key [%s]' % key)
  1222. print('User data: %s' % user_def)
  1223. return False
  1224. if not isinstance(user_def[key], list):
  1225. if debug:
  1226. print('user_def[key] is not a list key=[%s] user_def[key]=%s' % (key, user_def[key]))
  1227. return False
  1228. if len(user_def[key]) != len(value):
  1229. if debug:
  1230. print("List lengths are not equal.")
  1231. print("key=[%s]: user_def[%s] != value[%s]" % (key, len(user_def[key]), len(value)))
  1232. print("user_def: %s" % user_def[key])
  1233. print("value: %s" % value)
  1234. return False
  1235. for values in zip(user_def[key], value):
  1236. if isinstance(values[0], dict) and isinstance(values[1], dict):
  1237. if debug:
  1238. print('sending list - list')
  1239. print(type(values[0]))
  1240. print(type(values[1]))
  1241. result = Utils.check_def_equal(values[0], values[1], skip_keys=skip_keys, debug=debug)
  1242. if not result:
  1243. print('list compare returned false')
  1244. return False
  1245. elif value != user_def[key]:
  1246. if debug:
  1247. print('value should be identical')
  1248. print(user_def[key])
  1249. print(value)
  1250. return False
  1251. # recurse on a dictionary
  1252. elif isinstance(value, dict):
  1253. if key not in user_def:
  1254. if debug:
  1255. print("user_def does not have key [%s]" % key)
  1256. return False
  1257. if not isinstance(user_def[key], dict):
  1258. if debug:
  1259. print("dict returned false: not instance of dict")
  1260. return False
  1261. # before passing ensure keys match
  1262. api_values = set(value.keys()) - set(skip)
  1263. user_values = set(user_def[key].keys()) - set(skip)
  1264. if api_values != user_values:
  1265. if debug:
  1266. print("keys are not equal in dict")
  1267. print(user_values)
  1268. print(api_values)
  1269. return False
  1270. result = Utils.check_def_equal(user_def[key], value, skip_keys=skip_keys, debug=debug)
  1271. if not result:
  1272. if debug:
  1273. print("dict returned false")
  1274. print(result)
  1275. return False
  1276. # Verify each key, value pair is the same
  1277. else:
  1278. if key not in user_def or value != user_def[key]:
  1279. if debug:
  1280. print("value not equal; user_def does not have key")
  1281. print(key)
  1282. print(value)
  1283. if key in user_def:
  1284. print(user_def[key])
  1285. return False
  1286. if debug:
  1287. print('returning true')
  1288. return True
  1289. class OpenShiftCLIConfig(object):
  1290. '''Generic Config'''
  1291. def __init__(self, rname, namespace, kubeconfig, options):
  1292. self.kubeconfig = kubeconfig
  1293. self.name = rname
  1294. self.namespace = namespace
  1295. self._options = options
  1296. @property
  1297. def config_options(self):
  1298. ''' return config options '''
  1299. return self._options
  1300. def to_option_list(self, ascommalist=''):
  1301. '''return all options as a string
  1302. if ascommalist is set to the name of a key, and
  1303. the value of that key is a dict, format the dict
  1304. as a list of comma delimited key=value pairs'''
  1305. return self.stringify(ascommalist)
  1306. def stringify(self, ascommalist=''):
  1307. ''' return the options hash as cli params in a string
  1308. if ascommalist is set to the name of a key, and
  1309. the value of that key is a dict, format the dict
  1310. as a list of comma delimited key=value pairs '''
  1311. rval = []
  1312. for key in sorted(self.config_options.keys()):
  1313. data = self.config_options[key]
  1314. if data['include'] \
  1315. and (data['value'] is not None or isinstance(data['value'], int)):
  1316. if key == ascommalist:
  1317. val = ','.join(['{}={}'.format(kk, vv) for kk, vv in sorted(data['value'].items())])
  1318. else:
  1319. val = data['value']
  1320. rval.append('--{}={}'.format(key.replace('_', '-'), val))
  1321. return rval
  1322. # -*- -*- -*- End included fragment: lib/base.py -*- -*- -*-
  1323. # -*- -*- -*- Begin included fragment: lib/service.py -*- -*- -*-
  1324. # pylint: disable=too-many-instance-attributes
  1325. class ServiceConfig(object):
  1326. ''' Handle service options '''
  1327. # pylint: disable=too-many-arguments
  1328. def __init__(self,
  1329. sname,
  1330. namespace,
  1331. ports,
  1332. annotations=None,
  1333. selector=None,
  1334. labels=None,
  1335. cluster_ip=None,
  1336. portal_ip=None,
  1337. session_affinity=None,
  1338. service_type=None,
  1339. external_ips=None):
  1340. ''' constructor for handling service options '''
  1341. self.name = sname
  1342. self.namespace = namespace
  1343. self.ports = ports
  1344. self.annotations = annotations
  1345. self.selector = selector
  1346. self.labels = labels
  1347. self.cluster_ip = cluster_ip
  1348. self.portal_ip = portal_ip
  1349. self.session_affinity = session_affinity
  1350. self.service_type = service_type
  1351. self.external_ips = external_ips
  1352. self.data = {}
  1353. self.create_dict()
  1354. def create_dict(self):
  1355. ''' instantiates a service dict '''
  1356. self.data['apiVersion'] = 'v1'
  1357. self.data['kind'] = 'Service'
  1358. self.data['metadata'] = {}
  1359. self.data['metadata']['name'] = self.name
  1360. self.data['metadata']['namespace'] = self.namespace
  1361. if self.labels:
  1362. self.data['metadata']['labels'] = {}
  1363. for lab, lab_value in self.labels.items():
  1364. self.data['metadata']['labels'][lab] = lab_value
  1365. if self.annotations:
  1366. self.data['metadata']['annotations'] = self.annotations
  1367. self.data['spec'] = {}
  1368. if self.ports:
  1369. self.data['spec']['ports'] = self.ports
  1370. else:
  1371. self.data['spec']['ports'] = []
  1372. if self.selector:
  1373. self.data['spec']['selector'] = self.selector
  1374. self.data['spec']['sessionAffinity'] = self.session_affinity or 'None'
  1375. if self.cluster_ip:
  1376. self.data['spec']['clusterIP'] = self.cluster_ip
  1377. if self.portal_ip:
  1378. self.data['spec']['portalIP'] = self.portal_ip
  1379. if self.service_type:
  1380. self.data['spec']['type'] = self.service_type
  1381. if self.external_ips:
  1382. self.data['spec']['externalIPs'] = self.external_ips
  1383. # pylint: disable=too-many-instance-attributes,too-many-public-methods
  1384. class Service(Yedit):
  1385. ''' Class to model the oc service object '''
  1386. port_path = "spec.ports"
  1387. portal_ip = "spec.portalIP"
  1388. cluster_ip = "spec.clusterIP"
  1389. selector_path = 'spec.selector'
  1390. kind = 'Service'
  1391. external_ips = "spec.externalIPs"
  1392. def __init__(self, content):
  1393. '''Service constructor'''
  1394. super(Service, self).__init__(content=content)
  1395. def get_ports(self):
  1396. ''' get a list of ports '''
  1397. return self.get(Service.port_path) or []
  1398. def get_selector(self):
  1399. ''' get the service selector'''
  1400. return self.get(Service.selector_path) or {}
  1401. def add_ports(self, inc_ports):
  1402. ''' add a port object to the ports list '''
  1403. if not isinstance(inc_ports, list):
  1404. inc_ports = [inc_ports]
  1405. ports = self.get_ports()
  1406. if not ports:
  1407. self.put(Service.port_path, inc_ports)
  1408. else:
  1409. ports.extend(inc_ports)
  1410. return True
  1411. def find_ports(self, inc_port):
  1412. ''' find a specific port '''
  1413. for port in self.get_ports():
  1414. if port['port'] == inc_port['port']:
  1415. return port
  1416. return None
  1417. def delete_ports(self, inc_ports):
  1418. ''' remove a port from a service '''
  1419. if not isinstance(inc_ports, list):
  1420. inc_ports = [inc_ports]
  1421. ports = self.get(Service.port_path) or []
  1422. if not ports:
  1423. return True
  1424. removed = False
  1425. for inc_port in inc_ports:
  1426. port = self.find_ports(inc_port)
  1427. if port:
  1428. ports.remove(port)
  1429. removed = True
  1430. return removed
  1431. def add_cluster_ip(self, sip):
  1432. '''add cluster ip'''
  1433. self.put(Service.cluster_ip, sip)
  1434. def add_portal_ip(self, pip):
  1435. '''add cluster ip'''
  1436. self.put(Service.portal_ip, pip)
  1437. def get_external_ips(self):
  1438. ''' get a list of external_ips '''
  1439. return self.get(Service.external_ips) or []
  1440. def add_external_ips(self, inc_external_ips):
  1441. ''' add an external_ip to the external_ips list '''
  1442. if not isinstance(inc_external_ips, list):
  1443. inc_external_ips = [inc_external_ips]
  1444. external_ips = self.get_external_ips()
  1445. if not external_ips:
  1446. self.put(Service.external_ips, inc_external_ips)
  1447. else:
  1448. external_ips.extend(inc_external_ips)
  1449. return True
  1450. def find_external_ips(self, inc_external_ip):
  1451. ''' find a specific external IP '''
  1452. val = None
  1453. try:
  1454. idx = self.get_external_ips().index(inc_external_ip)
  1455. val = self.get_external_ips()[idx]
  1456. except ValueError:
  1457. pass
  1458. return val
  1459. def delete_external_ips(self, inc_external_ips):
  1460. ''' remove an external IP from a service '''
  1461. if not isinstance(inc_external_ips, list):
  1462. inc_external_ips = [inc_external_ips]
  1463. external_ips = self.get(Service.external_ips) or []
  1464. if not external_ips:
  1465. return True
  1466. removed = False
  1467. for inc_external_ip in inc_external_ips:
  1468. external_ip = self.find_external_ips(inc_external_ip)
  1469. if external_ip:
  1470. external_ips.remove(external_ip)
  1471. removed = True
  1472. return removed
  1473. # -*- -*- -*- End included fragment: lib/service.py -*- -*- -*-
  1474. # -*- -*- -*- Begin included fragment: lib/deploymentconfig.py -*- -*- -*-
  1475. # pylint: disable=too-many-public-methods
  1476. class DeploymentConfig(Yedit):
  1477. ''' Class to model an openshift DeploymentConfig'''
  1478. default_deployment_config = '''
  1479. apiVersion: v1
  1480. kind: DeploymentConfig
  1481. metadata:
  1482. name: default_dc
  1483. namespace: default
  1484. spec:
  1485. replicas: 0
  1486. selector:
  1487. default_dc: default_dc
  1488. strategy:
  1489. resources: {}
  1490. rollingParams:
  1491. intervalSeconds: 1
  1492. maxSurge: 0
  1493. maxUnavailable: 25%
  1494. timeoutSeconds: 600
  1495. updatePercent: -25
  1496. updatePeriodSeconds: 1
  1497. type: Rolling
  1498. template:
  1499. metadata:
  1500. spec:
  1501. containers:
  1502. - env:
  1503. - name: default
  1504. value: default
  1505. image: default
  1506. imagePullPolicy: IfNotPresent
  1507. name: default_dc
  1508. ports:
  1509. - containerPort: 8000
  1510. hostPort: 8000
  1511. protocol: TCP
  1512. name: default_port
  1513. resources: {}
  1514. terminationMessagePath: /dev/termination-log
  1515. dnsPolicy: ClusterFirst
  1516. hostNetwork: true
  1517. nodeSelector:
  1518. type: compute
  1519. restartPolicy: Always
  1520. securityContext: {}
  1521. serviceAccount: default
  1522. serviceAccountName: default
  1523. terminationGracePeriodSeconds: 30
  1524. triggers:
  1525. - type: ConfigChange
  1526. '''
  1527. replicas_path = "spec.replicas"
  1528. env_path = "spec.template.spec.containers[0].env"
  1529. volumes_path = "spec.template.spec.volumes"
  1530. container_path = "spec.template.spec.containers"
  1531. volume_mounts_path = "spec.template.spec.containers[0].volumeMounts"
  1532. def __init__(self, content=None):
  1533. ''' Constructor for deploymentconfig '''
  1534. if not content:
  1535. content = DeploymentConfig.default_deployment_config
  1536. super(DeploymentConfig, self).__init__(content=content)
  1537. def add_env_value(self, key, value):
  1538. ''' add key, value pair to env array '''
  1539. rval = False
  1540. env = self.get_env_vars()
  1541. if env:
  1542. env.append({'name': key, 'value': value})
  1543. rval = True
  1544. else:
  1545. result = self.put(DeploymentConfig.env_path, {'name': key, 'value': value})
  1546. rval = result[0]
  1547. return rval
  1548. def exists_env_value(self, key, value):
  1549. ''' return whether a key, value pair exists '''
  1550. results = self.get_env_vars()
  1551. if not results:
  1552. return False
  1553. for result in results:
  1554. if result['name'] == key and result['value'] == value:
  1555. return True
  1556. return False
  1557. def exists_env_key(self, key):
  1558. ''' return whether a key, value pair exists '''
  1559. results = self.get_env_vars()
  1560. if not results:
  1561. return False
  1562. for result in results:
  1563. if result['name'] == key:
  1564. return True
  1565. return False
  1566. def get_env_var(self, key):
  1567. '''return a environment variables '''
  1568. results = self.get(DeploymentConfig.env_path) or []
  1569. if not results:
  1570. return None
  1571. for env_var in results:
  1572. if env_var['name'] == key:
  1573. return env_var
  1574. return None
  1575. def get_env_vars(self):
  1576. '''return a environment variables '''
  1577. return self.get(DeploymentConfig.env_path) or []
  1578. def delete_env_var(self, keys):
  1579. '''delete a list of keys '''
  1580. if not isinstance(keys, list):
  1581. keys = [keys]
  1582. env_vars_array = self.get_env_vars()
  1583. modified = False
  1584. idx = None
  1585. for key in keys:
  1586. for env_idx, env_var in enumerate(env_vars_array):
  1587. if env_var['name'] == key:
  1588. idx = env_idx
  1589. break
  1590. if idx:
  1591. modified = True
  1592. del env_vars_array[idx]
  1593. if modified:
  1594. return True
  1595. return False
  1596. def update_env_var(self, key, value):
  1597. '''place an env in the env var list'''
  1598. env_vars_array = self.get_env_vars()
  1599. idx = None
  1600. for env_idx, env_var in enumerate(env_vars_array):
  1601. if env_var['name'] == key:
  1602. idx = env_idx
  1603. break
  1604. if idx:
  1605. env_vars_array[idx]['value'] = value
  1606. else:
  1607. self.add_env_value(key, value)
  1608. return True
  1609. def exists_volume_mount(self, volume_mount):
  1610. ''' return whether a volume mount exists '''
  1611. exist_volume_mounts = self.get_volume_mounts()
  1612. if not exist_volume_mounts:
  1613. return False
  1614. volume_mount_found = False
  1615. for exist_volume_mount in exist_volume_mounts:
  1616. if exist_volume_mount['name'] == volume_mount['name']:
  1617. volume_mount_found = True
  1618. break
  1619. return volume_mount_found
  1620. def exists_volume(self, volume):
  1621. ''' return whether a volume exists '''
  1622. exist_volumes = self.get_volumes()
  1623. volume_found = False
  1624. for exist_volume in exist_volumes:
  1625. if exist_volume['name'] == volume['name']:
  1626. volume_found = True
  1627. break
  1628. return volume_found
  1629. def find_volume_by_name(self, volume, mounts=False):
  1630. ''' return the index of a volume '''
  1631. volumes = []
  1632. if mounts:
  1633. volumes = self.get_volume_mounts()
  1634. else:
  1635. volumes = self.get_volumes()
  1636. for exist_volume in volumes:
  1637. if exist_volume['name'] == volume['name']:
  1638. return exist_volume
  1639. return None
  1640. def get_replicas(self):
  1641. ''' return replicas setting '''
  1642. return self.get(DeploymentConfig.replicas_path)
  1643. def get_volume_mounts(self):
  1644. '''return volume mount information '''
  1645. return self.get_volumes(mounts=True)
  1646. def get_volumes(self, mounts=False):
  1647. '''return volume mount information '''
  1648. if mounts:
  1649. return self.get(DeploymentConfig.volume_mounts_path) or []
  1650. return self.get(DeploymentConfig.volumes_path) or []
  1651. def delete_volume_by_name(self, volume):
  1652. '''delete a volume '''
  1653. modified = False
  1654. exist_volume_mounts = self.get_volume_mounts()
  1655. exist_volumes = self.get_volumes()
  1656. del_idx = None
  1657. for idx, exist_volume in enumerate(exist_volumes):
  1658. if 'name' in exist_volume and exist_volume['name'] == volume['name']:
  1659. del_idx = idx
  1660. break
  1661. if del_idx != None:
  1662. del exist_volumes[del_idx]
  1663. modified = True
  1664. del_idx = None
  1665. for idx, exist_volume_mount in enumerate(exist_volume_mounts):
  1666. if 'name' in exist_volume_mount and exist_volume_mount['name'] == volume['name']:
  1667. del_idx = idx
  1668. break
  1669. if del_idx != None:
  1670. del exist_volume_mounts[idx]
  1671. modified = True
  1672. return modified
  1673. def add_volume_mount(self, volume_mount):
  1674. ''' add a volume or volume mount to the proper location '''
  1675. exist_volume_mounts = self.get_volume_mounts()
  1676. if not exist_volume_mounts and volume_mount:
  1677. self.put(DeploymentConfig.volume_mounts_path, [volume_mount])
  1678. else:
  1679. exist_volume_mounts.append(volume_mount)
  1680. def add_volume(self, volume):
  1681. ''' add a volume or volume mount to the proper location '''
  1682. exist_volumes = self.get_volumes()
  1683. if not volume:
  1684. return
  1685. if not exist_volumes:
  1686. self.put(DeploymentConfig.volumes_path, [volume])
  1687. else:
  1688. exist_volumes.append(volume)
  1689. def update_replicas(self, replicas):
  1690. ''' update replicas value '''
  1691. self.put(DeploymentConfig.replicas_path, replicas)
  1692. def update_volume(self, volume):
  1693. '''place an env in the env var list'''
  1694. exist_volumes = self.get_volumes()
  1695. if not volume:
  1696. return False
  1697. # update the volume
  1698. update_idx = None
  1699. for idx, exist_vol in enumerate(exist_volumes):
  1700. if exist_vol['name'] == volume['name']:
  1701. update_idx = idx
  1702. break
  1703. if update_idx != None:
  1704. exist_volumes[update_idx] = volume
  1705. else:
  1706. self.add_volume(volume)
  1707. return True
  1708. def update_volume_mount(self, volume_mount):
  1709. '''place an env in the env var list'''
  1710. modified = False
  1711. exist_volume_mounts = self.get_volume_mounts()
  1712. if not volume_mount:
  1713. return False
  1714. # update the volume mount
  1715. for exist_vol_mount in exist_volume_mounts:
  1716. if exist_vol_mount['name'] == volume_mount['name']:
  1717. if 'mountPath' in exist_vol_mount and \
  1718. str(exist_vol_mount['mountPath']) != str(volume_mount['mountPath']):
  1719. exist_vol_mount['mountPath'] = volume_mount['mountPath']
  1720. modified = True
  1721. break
  1722. if not modified:
  1723. self.add_volume_mount(volume_mount)
  1724. modified = True
  1725. return modified
  1726. def needs_update_volume(self, volume, volume_mount):
  1727. ''' verify a volume update is needed '''
  1728. exist_volume = self.find_volume_by_name(volume)
  1729. exist_volume_mount = self.find_volume_by_name(volume, mounts=True)
  1730. results = []
  1731. results.append(exist_volume['name'] == volume['name'])
  1732. if 'secret' in volume:
  1733. results.append('secret' in exist_volume)
  1734. results.append(exist_volume['secret']['secretName'] == volume['secret']['secretName'])
  1735. results.append(exist_volume_mount['name'] == volume_mount['name'])
  1736. results.append(exist_volume_mount['mountPath'] == volume_mount['mountPath'])
  1737. elif 'emptyDir' in volume:
  1738. results.append(exist_volume_mount['name'] == volume['name'])
  1739. results.append(exist_volume_mount['mountPath'] == volume_mount['mountPath'])
  1740. elif 'persistentVolumeClaim' in volume:
  1741. pvc = 'persistentVolumeClaim'
  1742. results.append(pvc in exist_volume)
  1743. if results[-1]:
  1744. results.append(exist_volume[pvc]['claimName'] == volume[pvc]['claimName'])
  1745. if 'claimSize' in volume[pvc]:
  1746. results.append(exist_volume[pvc]['claimSize'] == volume[pvc]['claimSize'])
  1747. elif 'hostpath' in volume:
  1748. results.append('hostPath' in exist_volume)
  1749. results.append(exist_volume['hostPath']['path'] == volume_mount['mountPath'])
  1750. return not all(results)
  1751. def needs_update_replicas(self, replicas):
  1752. ''' verify whether a replica update is needed '''
  1753. current_reps = self.get(DeploymentConfig.replicas_path)
  1754. return not current_reps == replicas
  1755. # -*- -*- -*- End included fragment: lib/deploymentconfig.py -*- -*- -*-
  1756. # -*- -*- -*- Begin included fragment: lib/serviceaccount.py -*- -*- -*-
  1757. class ServiceAccountConfig(object):
  1758. '''Service account config class
  1759. This class stores the options and returns a default service account
  1760. '''
  1761. # pylint: disable=too-many-arguments
  1762. def __init__(self, sname, namespace, kubeconfig, secrets=None, image_pull_secrets=None):
  1763. self.name = sname
  1764. self.kubeconfig = kubeconfig
  1765. self.namespace = namespace
  1766. self.secrets = secrets or []
  1767. self.image_pull_secrets = image_pull_secrets or []
  1768. self.data = {}
  1769. self.create_dict()
  1770. def create_dict(self):
  1771. ''' instantiate a properly structured volume '''
  1772. self.data['apiVersion'] = 'v1'
  1773. self.data['kind'] = 'ServiceAccount'
  1774. self.data['metadata'] = {}
  1775. self.data['metadata']['name'] = self.name
  1776. self.data['metadata']['namespace'] = self.namespace
  1777. self.data['secrets'] = []
  1778. if self.secrets:
  1779. for sec in self.secrets:
  1780. self.data['secrets'].append({"name": sec})
  1781. self.data['imagePullSecrets'] = []
  1782. if self.image_pull_secrets:
  1783. for sec in self.image_pull_secrets:
  1784. self.data['imagePullSecrets'].append({"name": sec})
  1785. class ServiceAccount(Yedit):
  1786. ''' Class to wrap the oc command line tools '''
  1787. image_pull_secrets_path = "imagePullSecrets"
  1788. secrets_path = "secrets"
  1789. def __init__(self, content):
  1790. '''ServiceAccount constructor'''
  1791. super(ServiceAccount, self).__init__(content=content)
  1792. self._secrets = None
  1793. self._image_pull_secrets = None
  1794. @property
  1795. def image_pull_secrets(self):
  1796. ''' property for image_pull_secrets '''
  1797. if self._image_pull_secrets is None:
  1798. self._image_pull_secrets = self.get(ServiceAccount.image_pull_secrets_path) or []
  1799. return self._image_pull_secrets
  1800. @image_pull_secrets.setter
  1801. def image_pull_secrets(self, secrets):
  1802. ''' property for secrets '''
  1803. self._image_pull_secrets = secrets
  1804. @property
  1805. def secrets(self):
  1806. ''' property for secrets '''
  1807. if not self._secrets:
  1808. self._secrets = self.get(ServiceAccount.secrets_path) or []
  1809. return self._secrets
  1810. @secrets.setter
  1811. def secrets(self, secrets):
  1812. ''' property for secrets '''
  1813. self._secrets = secrets
  1814. def delete_secret(self, inc_secret):
  1815. ''' remove a secret '''
  1816. remove_idx = None
  1817. for idx, sec in enumerate(self.secrets):
  1818. if sec['name'] == inc_secret:
  1819. remove_idx = idx
  1820. break
  1821. if remove_idx:
  1822. del self.secrets[remove_idx]
  1823. return True
  1824. return False
  1825. def delete_image_pull_secret(self, inc_secret):
  1826. ''' remove a image_pull_secret '''
  1827. remove_idx = None
  1828. for idx, sec in enumerate(self.image_pull_secrets):
  1829. if sec['name'] == inc_secret:
  1830. remove_idx = idx
  1831. break
  1832. if remove_idx:
  1833. del self.image_pull_secrets[remove_idx]
  1834. return True
  1835. return False
  1836. def find_secret(self, inc_secret):
  1837. '''find secret'''
  1838. for secret in self.secrets:
  1839. if secret['name'] == inc_secret:
  1840. return secret
  1841. return None
  1842. def find_image_pull_secret(self, inc_secret):
  1843. '''find secret'''
  1844. for secret in self.image_pull_secrets:
  1845. if secret['name'] == inc_secret:
  1846. return secret
  1847. return None
  1848. def add_secret(self, inc_secret):
  1849. '''add secret'''
  1850. if self.secrets:
  1851. self.secrets.append({"name": inc_secret}) # pylint: disable=no-member
  1852. else:
  1853. self.put(ServiceAccount.secrets_path, [{"name": inc_secret}])
  1854. def add_image_pull_secret(self, inc_secret):
  1855. '''add image_pull_secret'''
  1856. if self.image_pull_secrets:
  1857. self.image_pull_secrets.append({"name": inc_secret}) # pylint: disable=no-member
  1858. else:
  1859. self.put(ServiceAccount.image_pull_secrets_path, [{"name": inc_secret}])
  1860. # -*- -*- -*- End included fragment: lib/serviceaccount.py -*- -*- -*-
  1861. # -*- -*- -*- Begin included fragment: lib/secret.py -*- -*- -*-
  1862. # pylint: disable=too-many-instance-attributes
  1863. class SecretConfig(object):
  1864. ''' Handle secret options '''
  1865. # pylint: disable=too-many-arguments
  1866. def __init__(self,
  1867. sname,
  1868. namespace,
  1869. kubeconfig,
  1870. secrets=None,
  1871. stype=None,
  1872. annotations=None):
  1873. ''' constructor for handling secret options '''
  1874. self.kubeconfig = kubeconfig
  1875. self.name = sname
  1876. self.type = stype
  1877. self.namespace = namespace
  1878. self.secrets = secrets
  1879. self.annotations = annotations
  1880. self.data = {}
  1881. self.create_dict()
  1882. def create_dict(self):
  1883. ''' assign the correct properties for a secret dict '''
  1884. self.data['apiVersion'] = 'v1'
  1885. self.data['kind'] = 'Secret'
  1886. self.data['type'] = self.type
  1887. self.data['metadata'] = {}
  1888. self.data['metadata']['name'] = self.name
  1889. self.data['metadata']['namespace'] = self.namespace
  1890. self.data['data'] = {}
  1891. if self.secrets:
  1892. for key, value in self.secrets.items():
  1893. self.data['data'][key] = value
  1894. if self.annotations:
  1895. self.data['metadata']['annotations'] = self.annotations
  1896. # pylint: disable=too-many-instance-attributes
  1897. class Secret(Yedit):
  1898. ''' Class to wrap the oc command line tools '''
  1899. secret_path = "data"
  1900. kind = 'secret'
  1901. def __init__(self, content):
  1902. '''secret constructor'''
  1903. super(Secret, self).__init__(content=content)
  1904. self._secrets = None
  1905. @property
  1906. def secrets(self):
  1907. '''secret property getter'''
  1908. if self._secrets is None:
  1909. self._secrets = self.get_secrets()
  1910. return self._secrets
  1911. @secrets.setter
  1912. def secrets(self):
  1913. '''secret property setter'''
  1914. if self._secrets is None:
  1915. self._secrets = self.get_secrets()
  1916. return self._secrets
  1917. def get_secrets(self):
  1918. ''' returns all of the defined secrets '''
  1919. return self.get(Secret.secret_path) or {}
  1920. def add_secret(self, key, value):
  1921. ''' add a secret '''
  1922. if self.secrets:
  1923. self.secrets[key] = value
  1924. else:
  1925. self.put(Secret.secret_path, {key: value})
  1926. return True
  1927. def delete_secret(self, key):
  1928. ''' delete secret'''
  1929. try:
  1930. del self.secrets[key]
  1931. except KeyError as _:
  1932. return False
  1933. return True
  1934. def find_secret(self, key):
  1935. ''' find secret'''
  1936. rval = None
  1937. try:
  1938. rval = self.secrets[key]
  1939. except KeyError as _:
  1940. return None
  1941. return {'key': key, 'value': rval}
  1942. def update_secret(self, key, value):
  1943. ''' update a secret'''
  1944. if key in self.secrets:
  1945. self.secrets[key] = value
  1946. else:
  1947. self.add_secret(key, value)
  1948. return True
  1949. # -*- -*- -*- End included fragment: lib/secret.py -*- -*- -*-
  1950. # -*- -*- -*- Begin included fragment: lib/rolebinding.py -*- -*- -*-
  1951. # pylint: disable=too-many-instance-attributes
  1952. class RoleBindingConfig(object):
  1953. ''' Handle rolebinding config '''
  1954. # pylint: disable=too-many-arguments
  1955. def __init__(self,
  1956. name,
  1957. namespace,
  1958. kubeconfig,
  1959. group_names=None,
  1960. role_ref=None,
  1961. subjects=None,
  1962. usernames=None):
  1963. ''' constructor for handling rolebinding options '''
  1964. self.kubeconfig = kubeconfig
  1965. self.name = name
  1966. self.namespace = namespace
  1967. self.group_names = group_names
  1968. self.role_ref = role_ref
  1969. self.subjects = subjects
  1970. self.usernames = usernames
  1971. self.data = {}
  1972. self.create_dict()
  1973. def create_dict(self):
  1974. ''' create a default rolebinding as a dict '''
  1975. self.data['apiVersion'] = 'v1'
  1976. self.data['kind'] = 'RoleBinding'
  1977. self.data['groupNames'] = self.group_names
  1978. self.data['metadata']['name'] = self.name
  1979. self.data['metadata']['namespace'] = self.namespace
  1980. self.data['roleRef'] = self.role_ref
  1981. self.data['subjects'] = self.subjects
  1982. self.data['userNames'] = self.usernames
  1983. # pylint: disable=too-many-instance-attributes,too-many-public-methods
  1984. class RoleBinding(Yedit):
  1985. ''' Class to model a rolebinding openshift object'''
  1986. group_names_path = "groupNames"
  1987. role_ref_path = "roleRef"
  1988. subjects_path = "subjects"
  1989. user_names_path = "userNames"
  1990. kind = 'RoleBinding'
  1991. def __init__(self, content):
  1992. '''RoleBinding constructor'''
  1993. super(RoleBinding, self).__init__(content=content)
  1994. self._subjects = None
  1995. self._role_ref = None
  1996. self._group_names = None
  1997. self._user_names = None
  1998. @property
  1999. def subjects(self):
  2000. ''' subjects property '''
  2001. if self._subjects is None:
  2002. self._subjects = self.get_subjects()
  2003. return self._subjects
  2004. @subjects.setter
  2005. def subjects(self, data):
  2006. ''' subjects property setter'''
  2007. self._subjects = data
  2008. @property
  2009. def role_ref(self):
  2010. ''' role_ref property '''
  2011. if self._role_ref is None:
  2012. self._role_ref = self.get_role_ref()
  2013. return self._role_ref
  2014. @role_ref.setter
  2015. def role_ref(self, data):
  2016. ''' role_ref property setter'''
  2017. self._role_ref = data
  2018. @property
  2019. def group_names(self):
  2020. ''' group_names property '''
  2021. if self._group_names is None:
  2022. self._group_names = self.get_group_names()
  2023. return self._group_names
  2024. @group_names.setter
  2025. def group_names(self, data):
  2026. ''' group_names property setter'''
  2027. self._group_names = data
  2028. @property
  2029. def user_names(self):
  2030. ''' user_names property '''
  2031. if self._user_names is None:
  2032. self._user_names = self.get_user_names()
  2033. return self._user_names
  2034. @user_names.setter
  2035. def user_names(self, data):
  2036. ''' user_names property setter'''
  2037. self._user_names = data
  2038. def get_group_names(self):
  2039. ''' return groupNames '''
  2040. return self.get(RoleBinding.group_names_path) or []
  2041. def get_user_names(self):
  2042. ''' return usernames '''
  2043. return self.get(RoleBinding.user_names_path) or []
  2044. def get_role_ref(self):
  2045. ''' return role_ref '''
  2046. return self.get(RoleBinding.role_ref_path) or {}
  2047. def get_subjects(self):
  2048. ''' return subjects '''
  2049. return self.get(RoleBinding.subjects_path) or []
  2050. #### ADD #####
  2051. def add_subject(self, inc_subject):
  2052. ''' add a subject '''
  2053. if self.subjects:
  2054. # pylint: disable=no-member
  2055. self.subjects.append(inc_subject)
  2056. else:
  2057. self.put(RoleBinding.subjects_path, [inc_subject])
  2058. return True
  2059. def add_role_ref(self, inc_role_ref):
  2060. ''' add a role_ref '''
  2061. if not self.role_ref:
  2062. self.put(RoleBinding.role_ref_path, {"name": inc_role_ref})
  2063. return True
  2064. return False
  2065. def add_group_names(self, inc_group_names):
  2066. ''' add a group_names '''
  2067. if self.group_names:
  2068. # pylint: disable=no-member
  2069. self.group_names.append(inc_group_names)
  2070. else:
  2071. self.put(RoleBinding.group_names_path, [inc_group_names])
  2072. return True
  2073. def add_user_name(self, inc_user_name):
  2074. ''' add a username '''
  2075. if self.user_names:
  2076. # pylint: disable=no-member
  2077. self.user_names.append(inc_user_name)
  2078. else:
  2079. self.put(RoleBinding.user_names_path, [inc_user_name])
  2080. return True
  2081. #### /ADD #####
  2082. #### Remove #####
  2083. def remove_subject(self, inc_subject):
  2084. ''' remove a subject '''
  2085. try:
  2086. # pylint: disable=no-member
  2087. self.subjects.remove(inc_subject)
  2088. except ValueError as _:
  2089. return False
  2090. return True
  2091. def remove_role_ref(self, inc_role_ref):
  2092. ''' remove a role_ref '''
  2093. if self.role_ref and self.role_ref['name'] == inc_role_ref:
  2094. del self.role_ref['name']
  2095. return True
  2096. return False
  2097. def remove_group_name(self, inc_group_name):
  2098. ''' remove a groupname '''
  2099. try:
  2100. # pylint: disable=no-member
  2101. self.group_names.remove(inc_group_name)
  2102. except ValueError as _:
  2103. return False
  2104. return True
  2105. def remove_user_name(self, inc_user_name):
  2106. ''' remove a username '''
  2107. try:
  2108. # pylint: disable=no-member
  2109. self.user_names.remove(inc_user_name)
  2110. except ValueError as _:
  2111. return False
  2112. return True
  2113. #### /REMOVE #####
  2114. #### UPDATE #####
  2115. def update_subject(self, inc_subject):
  2116. ''' update a subject '''
  2117. try:
  2118. # pylint: disable=no-member
  2119. index = self.subjects.index(inc_subject)
  2120. except ValueError as _:
  2121. return self.add_subject(inc_subject)
  2122. self.subjects[index] = inc_subject
  2123. return True
  2124. def update_group_name(self, inc_group_name):
  2125. ''' update a groupname '''
  2126. try:
  2127. # pylint: disable=no-member
  2128. index = self.group_names.index(inc_group_name)
  2129. except ValueError as _:
  2130. return self.add_group_names(inc_group_name)
  2131. self.group_names[index] = inc_group_name
  2132. return True
  2133. def update_user_name(self, inc_user_name):
  2134. ''' update a username '''
  2135. try:
  2136. # pylint: disable=no-member
  2137. index = self.user_names.index(inc_user_name)
  2138. except ValueError as _:
  2139. return self.add_user_name(inc_user_name)
  2140. self.user_names[index] = inc_user_name
  2141. return True
  2142. def update_role_ref(self, inc_role_ref):
  2143. ''' update a role_ref '''
  2144. self.role_ref['name'] = inc_role_ref
  2145. return True
  2146. #### /UPDATE #####
  2147. #### FIND ####
  2148. def find_subject(self, inc_subject):
  2149. ''' find a subject '''
  2150. index = None
  2151. try:
  2152. # pylint: disable=no-member
  2153. index = self.subjects.index(inc_subject)
  2154. except ValueError as _:
  2155. return index
  2156. return index
  2157. def find_group_name(self, inc_group_name):
  2158. ''' find a group_name '''
  2159. index = None
  2160. try:
  2161. # pylint: disable=no-member
  2162. index = self.group_names.index(inc_group_name)
  2163. except ValueError as _:
  2164. return index
  2165. return index
  2166. def find_user_name(self, inc_user_name):
  2167. ''' find a user_name '''
  2168. index = None
  2169. try:
  2170. # pylint: disable=no-member
  2171. index = self.user_names.index(inc_user_name)
  2172. except ValueError as _:
  2173. return index
  2174. return index
  2175. def find_role_ref(self, inc_role_ref):
  2176. ''' find a user_name '''
  2177. if self.role_ref and self.role_ref['name'] == inc_role_ref['name']:
  2178. return self.role_ref
  2179. return None
  2180. # -*- -*- -*- End included fragment: lib/rolebinding.py -*- -*- -*-
  2181. # -*- -*- -*- Begin included fragment: class/oc_adm_router.py -*- -*- -*-
  2182. class RouterException(Exception):
  2183. ''' Router exception'''
  2184. pass
  2185. class RouterConfig(OpenShiftCLIConfig):
  2186. ''' RouterConfig is a DTO for the router. '''
  2187. def __init__(self, rname, namespace, kubeconfig, router_options):
  2188. super(RouterConfig, self).__init__(rname, namespace, kubeconfig, router_options)
  2189. class Router(OpenShiftCLI):
  2190. ''' Class to wrap the oc command line tools '''
  2191. def __init__(self,
  2192. router_config,
  2193. verbose=False):
  2194. ''' Constructor for OpenshiftOC
  2195. a router consists of 3 or more parts
  2196. - dc/router
  2197. - svc/router
  2198. - sa/router
  2199. - secret/router-certs
  2200. - clusterrolebinding/router-router-role
  2201. '''
  2202. super(Router, self).__init__('default', router_config.kubeconfig, verbose)
  2203. self.config = router_config
  2204. self.verbose = verbose
  2205. self.router_parts = [{'kind': 'dc', 'name': self.config.name},
  2206. {'kind': 'svc', 'name': self.config.name},
  2207. {'kind': 'sa', 'name': self.config.config_options['service_account']['value']},
  2208. {'kind': 'secret', 'name': self.config.name + '-certs'},
  2209. {'kind': 'clusterrolebinding', 'name': 'router-' + self.config.name + '-role'},
  2210. ]
  2211. self.__prepared_router = None
  2212. self.dconfig = None
  2213. self.svc = None
  2214. self._secret = None
  2215. self._serviceaccount = None
  2216. self._rolebinding = None
  2217. @property
  2218. def prepared_router(self):
  2219. ''' property for the prepared router'''
  2220. if self.__prepared_router is None:
  2221. results = self._prepare_router()
  2222. if not results or 'returncode' in results and results['returncode'] != 0:
  2223. if 'stderr' in results:
  2224. raise RouterException('Could not perform router preparation: %s' % results['stderr'])
  2225. raise RouterException('Could not perform router preparation.')
  2226. self.__prepared_router = results
  2227. return self.__prepared_router
  2228. @prepared_router.setter
  2229. def prepared_router(self, obj):
  2230. '''setter for the prepared_router'''
  2231. self.__prepared_router = obj
  2232. @property
  2233. def deploymentconfig(self):
  2234. ''' property deploymentconfig'''
  2235. return self.dconfig
  2236. @deploymentconfig.setter
  2237. def deploymentconfig(self, config):
  2238. ''' setter for property deploymentconfig '''
  2239. self.dconfig = config
  2240. @property
  2241. def service(self):
  2242. ''' property for service '''
  2243. return self.svc
  2244. @service.setter
  2245. def service(self, config):
  2246. ''' setter for property service '''
  2247. self.svc = config
  2248. @property
  2249. def secret(self):
  2250. ''' property secret '''
  2251. return self._secret
  2252. @secret.setter
  2253. def secret(self, config):
  2254. ''' setter for property secret '''
  2255. self._secret = config
  2256. @property
  2257. def serviceaccount(self):
  2258. ''' property for serviceaccount '''
  2259. return self._serviceaccount
  2260. @serviceaccount.setter
  2261. def serviceaccount(self, config):
  2262. ''' setter for property serviceaccount '''
  2263. self._serviceaccount = config
  2264. @property
  2265. def rolebinding(self):
  2266. ''' property rolebinding '''
  2267. return self._rolebinding
  2268. @rolebinding.setter
  2269. def rolebinding(self, config):
  2270. ''' setter for property rolebinding '''
  2271. self._rolebinding = config
  2272. def get_object_by_kind(self, kind):
  2273. '''return the current object kind by name'''
  2274. if re.match("^(dc|deploymentconfig)$", kind, flags=re.IGNORECASE):
  2275. return self.deploymentconfig
  2276. elif re.match("^(svc|service)$", kind, flags=re.IGNORECASE):
  2277. return self.service
  2278. elif re.match("^(sa|serviceaccount)$", kind, flags=re.IGNORECASE):
  2279. return self.serviceaccount
  2280. elif re.match("secret", kind, flags=re.IGNORECASE):
  2281. return self.secret
  2282. elif re.match("clusterrolebinding", kind, flags=re.IGNORECASE):
  2283. return self.rolebinding
  2284. return None
  2285. def get(self):
  2286. ''' return the self.router_parts '''
  2287. self.service = None
  2288. self.deploymentconfig = None
  2289. self.serviceaccount = None
  2290. self.secret = None
  2291. self.rolebinding = None
  2292. for part in self.router_parts:
  2293. result = self._get(part['kind'], name=part['name'])
  2294. if result['returncode'] == 0 and part['kind'] == 'dc':
  2295. self.deploymentconfig = DeploymentConfig(result['results'][0])
  2296. elif result['returncode'] == 0 and part['kind'] == 'svc':
  2297. self.service = Service(content=result['results'][0])
  2298. elif result['returncode'] == 0 and part['kind'] == 'sa':
  2299. self.serviceaccount = ServiceAccount(content=result['results'][0])
  2300. elif result['returncode'] == 0 and part['kind'] == 'secret':
  2301. self.secret = Secret(content=result['results'][0])
  2302. elif result['returncode'] == 0 and part['kind'] == 'clusterrolebinding':
  2303. self.rolebinding = RoleBinding(content=result['results'][0])
  2304. return {'deploymentconfig': self.deploymentconfig,
  2305. 'service': self.service,
  2306. 'serviceaccount': self.serviceaccount,
  2307. 'secret': self.secret,
  2308. 'clusterrolebinding': self.rolebinding,
  2309. }
  2310. def exists(self):
  2311. '''return a whether svc or dc exists '''
  2312. if self.deploymentconfig and self.service and self.secret and self.serviceaccount:
  2313. return True
  2314. return False
  2315. def delete(self):
  2316. '''return all pods '''
  2317. parts = []
  2318. for part in self.router_parts:
  2319. parts.append(self._delete(part['kind'], part['name']))
  2320. rval = 0
  2321. for part in parts:
  2322. if part['returncode'] != 0 and not 'already exist' in part['stderr']:
  2323. rval = part['returncode']
  2324. return {'returncode': rval, 'results': parts}
  2325. def add_modifications(self, deploymentconfig):
  2326. '''modify the deployment config'''
  2327. # We want modifications in the form of edits coming in from the module.
  2328. # Let's apply these here
  2329. edit_results = []
  2330. for edit in self.config.config_options['edits'].get('value', []):
  2331. if edit['action'] == 'put':
  2332. edit_results.append(deploymentconfig.put(edit['key'],
  2333. edit['value']))
  2334. if edit['action'] == 'update':
  2335. edit_results.append(deploymentconfig.update(edit['key'],
  2336. edit['value'],
  2337. edit.get('index', None),
  2338. edit.get('curr_value', None)))
  2339. if edit['action'] == 'append':
  2340. edit_results.append(deploymentconfig.append(edit['key'],
  2341. edit['value']))
  2342. if edit_results and not any([res[0] for res in edit_results]):
  2343. return None
  2344. return deploymentconfig
  2345. # pylint: disable=too-many-branches
  2346. def _prepare_router(self):
  2347. '''prepare router for instantiation'''
  2348. # if cacert, key, and cert were passed, combine them into a pem file
  2349. if (self.config.config_options['cacert_file']['value'] and
  2350. self.config.config_options['cert_file']['value'] and
  2351. self.config.config_options['key_file']['value']):
  2352. router_pem = '/tmp/router.pem'
  2353. with open(router_pem, 'w') as rfd:
  2354. rfd.write(open(self.config.config_options['cert_file']['value']).read())
  2355. rfd.write(open(self.config.config_options['key_file']['value']).read())
  2356. if self.config.config_options['cacert_file']['value'] and \
  2357. os.path.exists(self.config.config_options['cacert_file']['value']):
  2358. rfd.write(open(self.config.config_options['cacert_file']['value']).read())
  2359. atexit.register(Utils.cleanup, [router_pem])
  2360. self.config.config_options['default_cert']['value'] = router_pem
  2361. elif self.config.config_options['default_cert']['value'] is None:
  2362. # No certificate was passed to us. do not pass one to oc adm router
  2363. self.config.config_options['default_cert']['include'] = False
  2364. options = self.config.to_option_list(ascommalist='labels')
  2365. cmd = ['router', self.config.name]
  2366. cmd.extend(options)
  2367. cmd.extend(['--dry-run=True', '-o', 'json'])
  2368. results = self.openshift_cmd(cmd, oadm=True, output=True, output_type='json')
  2369. # pylint: disable=maybe-no-member
  2370. if results['returncode'] != 0 or 'items' not in results['results']:
  2371. return results
  2372. oc_objects = {'DeploymentConfig': {'obj': None, 'path': None, 'update': False},
  2373. 'Secret': {'obj': None, 'path': None, 'update': False},
  2374. 'ServiceAccount': {'obj': None, 'path': None, 'update': False},
  2375. 'ClusterRoleBinding': {'obj': None, 'path': None, 'update': False},
  2376. 'Service': {'obj': None, 'path': None, 'update': False},
  2377. }
  2378. # pylint: disable=invalid-sequence-index
  2379. for res in results['results']['items']:
  2380. if res['kind'] == 'DeploymentConfig':
  2381. oc_objects['DeploymentConfig']['obj'] = DeploymentConfig(res)
  2382. elif res['kind'] == 'Service':
  2383. oc_objects['Service']['obj'] = Service(res)
  2384. elif res['kind'] == 'ServiceAccount':
  2385. oc_objects['ServiceAccount']['obj'] = ServiceAccount(res)
  2386. elif res['kind'] == 'Secret':
  2387. oc_objects['Secret']['obj'] = Secret(res)
  2388. elif res['kind'] == 'ClusterRoleBinding':
  2389. oc_objects['ClusterRoleBinding']['obj'] = RoleBinding(res)
  2390. # Currently only deploymentconfig needs updating
  2391. # Verify we got a deploymentconfig
  2392. if not oc_objects['DeploymentConfig']['obj']:
  2393. return results
  2394. # add modifications added
  2395. oc_objects['DeploymentConfig']['obj'] = self.add_modifications(oc_objects['DeploymentConfig']['obj'])
  2396. for oc_type, oc_data in oc_objects.items():
  2397. if oc_data['obj'] is not None:
  2398. oc_data['path'] = Utils.create_tmp_file_from_contents(oc_type, oc_data['obj'].yaml_dict)
  2399. return oc_objects
  2400. def create(self):
  2401. '''Create a router
  2402. This includes the different parts:
  2403. - deploymentconfig
  2404. - service
  2405. - serviceaccount
  2406. - secrets
  2407. - clusterrolebinding
  2408. '''
  2409. results = []
  2410. self.needs_update()
  2411. # pylint: disable=maybe-no-member
  2412. for kind, oc_data in self.prepared_router.items():
  2413. if oc_data['obj'] is not None:
  2414. time.sleep(1)
  2415. if self.get_object_by_kind(kind) is None:
  2416. results.append(self._create(oc_data['path']))
  2417. elif oc_data['update']:
  2418. results.append(self._replace(oc_data['path']))
  2419. rval = 0
  2420. for result in results:
  2421. if result['returncode'] != 0 and not 'already exist' in result['stderr']:
  2422. rval = result['returncode']
  2423. return {'returncode': rval, 'results': results}
  2424. def update(self):
  2425. '''run update for the router. This performs a replace'''
  2426. results = []
  2427. # pylint: disable=maybe-no-member
  2428. for _, oc_data in self.prepared_router.items():
  2429. if oc_data['update']:
  2430. results.append(self._replace(oc_data['path']))
  2431. rval = 0
  2432. for result in results:
  2433. if result['returncode'] != 0:
  2434. rval = result['returncode']
  2435. return {'returncode': rval, 'results': results}
  2436. # pylint: disable=too-many-return-statements,too-many-branches
  2437. def needs_update(self):
  2438. ''' check to see if we need to update '''
  2439. # ServiceAccount:
  2440. # Need to determine changes from the pregenerated ones from the original
  2441. # Since these are auto generated, we can skip
  2442. skip = ['secrets', 'imagePullSecrets']
  2443. if self.serviceaccount is None or \
  2444. not Utils.check_def_equal(self.prepared_router['ServiceAccount']['obj'].yaml_dict,
  2445. self.serviceaccount.yaml_dict,
  2446. skip_keys=skip,
  2447. debug=self.verbose):
  2448. self.prepared_router['ServiceAccount']['update'] = True
  2449. # Secret:
  2450. # See if one was generated from our dry-run and verify it if needed
  2451. if self.prepared_router['Secret']['obj']:
  2452. if not self.secret:
  2453. self.prepared_router['Secret']['update'] = True
  2454. if self.secret is None or \
  2455. not Utils.check_def_equal(self.prepared_router['Secret']['obj'].yaml_dict,
  2456. self.secret.yaml_dict,
  2457. skip_keys=skip,
  2458. debug=self.verbose):
  2459. self.prepared_router['Secret']['update'] = True
  2460. # Service:
  2461. # Fix the ports to have protocol=TCP
  2462. for port in self.prepared_router['Service']['obj'].get('spec.ports'):
  2463. port['protocol'] = 'TCP'
  2464. skip = ['portalIP', 'clusterIP', 'sessionAffinity', 'type']
  2465. if self.service is None or \
  2466. not Utils.check_def_equal(self.prepared_router['Service']['obj'].yaml_dict,
  2467. self.service.yaml_dict,
  2468. skip_keys=skip,
  2469. debug=self.verbose):
  2470. self.prepared_router['Service']['update'] = True
  2471. # DeploymentConfig:
  2472. # Router needs some exceptions.
  2473. # We do not want to check the autogenerated password for stats admin
  2474. if self.deploymentconfig is not None:
  2475. if not self.config.config_options['stats_password']['value']:
  2476. for idx, env_var in enumerate(self.prepared_router['DeploymentConfig']['obj'].get(\
  2477. 'spec.template.spec.containers[0].env') or []):
  2478. if env_var['name'] == 'STATS_PASSWORD':
  2479. env_var['value'] = \
  2480. self.deploymentconfig.get('spec.template.spec.containers[0].env[%s].value' % idx)
  2481. break
  2482. # dry-run doesn't add the protocol to the ports section. We will manually do that.
  2483. for idx, port in enumerate(self.prepared_router['DeploymentConfig']['obj'].get(\
  2484. 'spec.template.spec.containers[0].ports') or []):
  2485. if not 'protocol' in port:
  2486. port['protocol'] = 'TCP'
  2487. # These are different when generating
  2488. skip = ['dnsPolicy',
  2489. 'terminationGracePeriodSeconds',
  2490. 'restartPolicy', 'timeoutSeconds',
  2491. 'livenessProbe', 'readinessProbe',
  2492. 'terminationMessagePath', 'hostPort',
  2493. 'defaultMode',
  2494. ]
  2495. if self.deploymentconfig is None or \
  2496. not Utils.check_def_equal(self.prepared_router['DeploymentConfig']['obj'].yaml_dict,
  2497. self.deploymentconfig.yaml_dict,
  2498. skip_keys=skip,
  2499. debug=self.verbose):
  2500. self.prepared_router['DeploymentConfig']['update'] = True
  2501. # Check if any of the parts need updating, if so, return True
  2502. # else, no need to update
  2503. # pylint: disable=no-member
  2504. return any([self.prepared_router[oc_type]['update'] for oc_type in self.prepared_router.keys()])
  2505. @staticmethod
  2506. def run_ansible(params, check_mode):
  2507. '''run ansible idempotent code'''
  2508. rconfig = RouterConfig(params['name'],
  2509. params['namespace'],
  2510. params['kubeconfig'],
  2511. {'default_cert': {'value': params['default_cert'], 'include': True},
  2512. 'cert_file': {'value': params['cert_file'], 'include': False},
  2513. 'key_file': {'value': params['key_file'], 'include': False},
  2514. 'images': {'value': params['images'], 'include': True},
  2515. 'latest_images': {'value': params['latest_images'], 'include': True},
  2516. 'labels': {'value': params['labels'], 'include': True},
  2517. 'ports': {'value': ','.join(params['ports']), 'include': True},
  2518. 'replicas': {'value': params['replicas'], 'include': True},
  2519. 'selector': {'value': params['selector'], 'include': True},
  2520. 'service_account': {'value': params['service_account'], 'include': True},
  2521. 'router_type': {'value': params['router_type'], 'include': False},
  2522. 'host_network': {'value': params['host_network'], 'include': True},
  2523. 'external_host': {'value': params['external_host'], 'include': True},
  2524. 'external_host_vserver': {'value': params['external_host_vserver'],
  2525. 'include': True},
  2526. 'external_host_insecure': {'value': params['external_host_insecure'],
  2527. 'include': True},
  2528. 'external_host_partition_path': {'value': params['external_host_partition_path'],
  2529. 'include': True},
  2530. 'external_host_username': {'value': params['external_host_username'],
  2531. 'include': True},
  2532. 'external_host_password': {'value': params['external_host_password'],
  2533. 'include': True},
  2534. 'external_host_private_key': {'value': params['external_host_private_key'],
  2535. 'include': True},
  2536. 'expose_metrics': {'value': params['expose_metrics'], 'include': True},
  2537. 'metrics_image': {'value': params['metrics_image'], 'include': True},
  2538. 'stats_user': {'value': params['stats_user'], 'include': True},
  2539. 'stats_password': {'value': params['stats_password'], 'include': True},
  2540. 'stats_port': {'value': params['stats_port'], 'include': True},
  2541. # extra
  2542. 'cacert_file': {'value': params['cacert_file'], 'include': False},
  2543. # edits
  2544. 'edits': {'value': params['edits'], 'include': False},
  2545. })
  2546. state = params['state']
  2547. ocrouter = Router(rconfig, verbose=params['debug'])
  2548. api_rval = ocrouter.get()
  2549. ########
  2550. # get
  2551. ########
  2552. if state == 'list':
  2553. return {'changed': False, 'results': api_rval, 'state': state}
  2554. ########
  2555. # Delete
  2556. ########
  2557. if state == 'absent':
  2558. if not ocrouter.exists():
  2559. return {'changed': False, 'state': state}
  2560. if check_mode:
  2561. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete.'}
  2562. # In case of delete we return a list of each object
  2563. # that represents a router and its result in a list
  2564. # pylint: disable=redefined-variable-type
  2565. api_rval = ocrouter.delete()
  2566. return {'changed': True, 'results': api_rval, 'state': state}
  2567. if state == 'present':
  2568. ########
  2569. # Create
  2570. ########
  2571. if not ocrouter.exists():
  2572. if check_mode:
  2573. return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'}
  2574. api_rval = ocrouter.create()
  2575. if api_rval['returncode'] != 0:
  2576. return {'failed': True, 'msg': api_rval}
  2577. return {'changed': True, 'results': api_rval, 'state': state}
  2578. ########
  2579. # Update
  2580. ########
  2581. if not ocrouter.needs_update():
  2582. return {'changed': False, 'state': state}
  2583. if check_mode:
  2584. return {'changed': False, 'msg': 'CHECK_MODE: Would have performed an update.'}
  2585. api_rval = ocrouter.update()
  2586. if api_rval['returncode'] != 0:
  2587. return {'failed': True, 'msg': api_rval}
  2588. return {'changed': True, 'results': api_rval, 'state': state}
  2589. # -*- -*- -*- End included fragment: class/oc_adm_router.py -*- -*- -*-
  2590. # -*- -*- -*- Begin included fragment: ansible/oc_adm_router.py -*- -*- -*-
  2591. def main():
  2592. '''
  2593. ansible oc module for router
  2594. '''
  2595. module = AnsibleModule(
  2596. argument_spec=dict(
  2597. state=dict(default='present', type='str',
  2598. choices=['present', 'absent']),
  2599. debug=dict(default=False, type='bool'),
  2600. namespace=dict(default='default', type='str'),
  2601. name=dict(default='router', type='str'),
  2602. kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'),
  2603. default_cert=dict(default=None, type='str'),
  2604. cert_file=dict(default=None, type='str'),
  2605. key_file=dict(default=None, type='str'),
  2606. images=dict(default=None, type='str'), #'openshift3/ose-${component}:${version}'
  2607. latest_images=dict(default=False, type='bool'),
  2608. labels=dict(default=None, type='dict'),
  2609. ports=dict(default=['80:80', '443:443'], type='list'),
  2610. replicas=dict(default=1, type='int'),
  2611. selector=dict(default=None, type='str'),
  2612. service_account=dict(default='router', type='str'),
  2613. router_type=dict(default='haproxy-router', type='str'),
  2614. host_network=dict(default=True, type='bool'),
  2615. # external host options
  2616. external_host=dict(default=None, type='str'),
  2617. external_host_vserver=dict(default=None, type='str'),
  2618. external_host_insecure=dict(default=False, type='bool'),
  2619. external_host_partition_path=dict(default=None, type='str'),
  2620. external_host_username=dict(default=None, type='str'),
  2621. external_host_password=dict(default=None, type='str', no_log=True),
  2622. external_host_private_key=dict(default=None, type='str', no_log=True),
  2623. # Metrics
  2624. expose_metrics=dict(default=False, type='bool'),
  2625. metrics_image=dict(default=None, type='str'),
  2626. # Stats
  2627. stats_user=dict(default=None, type='str'),
  2628. stats_password=dict(default=None, type='str', no_log=True),
  2629. stats_port=dict(default=1936, type='int'),
  2630. # extra
  2631. cacert_file=dict(default=None, type='str'),
  2632. # edits
  2633. edits=dict(default=[], type='list'),
  2634. ),
  2635. mutually_exclusive=[["router_type", "images"],
  2636. ["key_file", "default_cert"],
  2637. ["cert_file", "default_cert"],
  2638. ["cacert_file", "default_cert"],
  2639. ],
  2640. required_together=[['cacert_file', 'cert_file', 'key_file']],
  2641. supports_check_mode=True,
  2642. )
  2643. results = Router.run_ansible(module.params, module.check_mode)
  2644. if 'failed' in results:
  2645. module.fail_json(**results)
  2646. module.exit_json(**results)
  2647. if __name__ == '__main__':
  2648. main()
  2649. # -*- -*- -*- End included fragment: ansible/oc_adm_router.py -*- -*- -*-