From 67c2a0ed3ee426451a799b6f20d910c1caa95017 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 18 Dec 2022 18:42:14 +0100 Subject: [PATCH] add or update dns with API OVH --- inventory/group_vars/local_vault | 83 +++++++++++---------- scaleway-k8s/files/requierements.txt | 1 + scaleway-k8s/tasks/deploy-backend.yml | 64 ++++++++++++---- scaleway-k8s/templates/dns_add_update.py.j2 | 49 ++++++++++++ 4 files changed, 143 insertions(+), 54 deletions(-) create mode 100644 scaleway-k8s/files/requierements.txt create mode 100644 scaleway-k8s/templates/dns_add_update.py.j2 diff --git a/inventory/group_vars/local_vault b/inventory/group_vars/local_vault index 1887b86..e6c23c7 100644 --- a/inventory/group_vars/local_vault +++ b/inventory/group_vars/local_vault @@ -1,39 +1,46 @@ $ANSIBLE_VAULT;1.1;AES256 -64323034306466353139343765626366333236376138306561336430316565616134356565373865 -3330336538643132373635646435646637643735633030660a643538356333356232663664316630 -62326138353933333565626363376135393266666162643936643138303438393565383435333737 -3239363964386630350a653736393435643239373732646637646433623332623336643338663839 -38303433303634613238393233343230386636366137616231623131323861316537623035396630 -32366666393633326665643063306132616631383966353737646439643162336435316538346265 -66356539643530646338363862633963306634613136363337613738653162333163643130613035 -31643330303437616431316165323365376364306664623936356334656362663165623239333231 -66616662643539383164613234343733663236613733313633353961303038666266633739323830 -64633165633832343130316338376638353631326362336663386431636631396334646430336562 -34363666643035643439666362396363663936333763363263393734353438653931363735313032 -35356132383131353538653338386566353032356635653233396264333139303632623939323664 -64363833393230636666323531323630346361346336393239316661636537313030636532653733 -61343338623861626336636636323162343666366238653730386464353736633338313034323633 -31366133643561333464653830343465303836366537633866636335373536613430343339366436 -64656162316437383264666264346633646238633535386165303936633139356436646162396135 -62346163383834343332646437326439343238663334313730656462343530373063386236616135 -35333862376466306637623063323631373530383631316561366365326162663466663338616238 -36376363646466356565303963393562653534663736343730613536643936663836613732373161 -30616232373134376366616263663866313233666437613235643263306332653130383730383937 -64613330653735303734656235376262643432383736613939363963643438373239366137336336 -61353761376332643961363330643364633230383339383234636234633766363037383565336630 -66643431333634646365353233336166626665386339383864303634323335306334666536306339 -65396238623236646464346531333736336538336463376632323663373634666464363737623661 -35616236363634313130316639386333343264316533353131643431363766393632346531323133 -32366132313737326462373635626461666365653339623630623966646663383261663664646338 -63346537366463323361616530333834663238353461306133323464643563616335343762663735 -34316435386362663564316536613134336337346135616434356433643066303831336533343432 -39353938656461376234636561353061303339373039643831643466613861396562376162353161 -31616432326136343238613734356432663539393039363530323030613436376536393734333035 -33653262316639323935646165333337313466303963306132613730366530346532663833393537 -33323839333431373437663039353063343133623831343465303338303437633331396132363634 -61656231653133653866663135643436343639326431616432326235643131356136333266653333 -62643232313231633234343937383365636431316437313463396432393265376164343836356265 -38623335343961616339623961393164366439373439643566393931616237316437613735396634 -64656363326465323466396466623734306639613063323437646431343833346337363937363334 -33646339636132333735353464646431626564633862366139626636663161653931336634373430 -6464626131353032393730623766393631366433636137393165 +30356465363432393266326366393162306331353564346565663139336538613832396135306537 +6262336438613736636266623766333233336631656661640a663838373936613666373133383632 +32613962623330303964333161373165363164646266396461633233346136336630646631306564 +3939363432643961610a313338306136303132666461393637643235623361393830653534643137 +38623038613364396538353533336631633863343834333030653164303231346263316435356231 +61383966663834333662393138303035613330366533663037326532356332633839363763356466 +63396536323864383933346434363231396164613838316162656333626634373936643738386565 +32326532346334343666383632306264636132613266333863626462366530306439613130333564 +38623461613739343461386564323865316332653530623862313163396566616230653030653266 +61613836303231363534393531373233363332316639316137653132336638356161333764373562 +34363034353935633437353466303364653039626137383534633239316563373663623030646330 +38383639326563643266346530616666646337373765643336326536303864613363663536643465 +34343663373131323437666462303763366439356362306337356131383932353731393935343931 +32613333373439343864653864636438646431306637613038313061663961653033643465643864 +31333366663037373835326330316535326134393761333662326665333464666164623336666439 +35313066303830343737343063396335383461653338303734353461643736343163346565323864 +65356437376663386535646437653566326532346462313932346435393730366530646164323537 +35363633333065356162653762623239343836356230303734316130383234313536623339626531 +36643434336233626234333235636566313566353939616433373465666362316663353336653162 +32346534333665353934653738343335356137643539376639323164393837383065323436363064 +35643762656365313261373030336339616266333765613735346565623138366533626335633033 +38373732626337353535656230636235373535363332376535343162666266393931303236653631 +33653432633337306230396434316230663438363630313038386538663732393863633837313638 +31663763303462343063613638663361626462623035336465383661646433643833636361346236 +32386263326664363338633861643264383462313863666662623033323761613132666130353235 +34633261303761623033633964323862666137333035306333323932346334356365333563316630 +34313263306561303538353134613233313337323636623230616532383133613561333439663337 +64376230363038306238656236653366353738633436623330383239373062313831393066313838 +38653532356632383765613530373034396639633064383336393934353061656266633463336333 +66356538616233313936376565386432303134306363343734333563333266643062666231303532 +37363664396165653333656661376334333432376566666237316232643234386332323936346630 +63303563363162356666643732366131393066333838363935613336353062633862353838613761 +64333939663733643132366334653835633733623539393735313961613131303934363731663231 +64313434613934336630306165343665376261353562656634356365613137346232633431343734 +63363532313765646434346132313434373366656332353133383530306639633062633365613932 +65383865643262666666383933363534623838653661376531303763623261323937626335373635 +32623262343130383462316462643338363433653133353737633562353330313533303835666634 +65386336616466316663356236316466353164353139326333316362303737343335353731366531 +36653062323764316332353136343738616338313465333261363664663934393038343237313135 +64363036636634646137616365366538613338343761646137623432396539363035663031313464 +66646663373838316161643161613838343139346165663331373334383334316562353235633461 +65316666616334653034393165616434306664633336613639636433663761363932396663396630 +64383961623136623639306435663965303632663039653538646463663235653333306434313931 +32636536643536636561663534346633663464333830646139383939626665393964353739616435 +35663834393738646130316564633966653765303033356132393961653831646362 diff --git a/scaleway-k8s/files/requierements.txt b/scaleway-k8s/files/requierements.txt new file mode 100644 index 0000000..759a68d --- /dev/null +++ b/scaleway-k8s/files/requierements.txt @@ -0,0 +1 @@ +ovh \ No newline at end of file diff --git a/scaleway-k8s/tasks/deploy-backend.yml b/scaleway-k8s/tasks/deploy-backend.yml index ae14526..0a09c15 100644 --- a/scaleway-k8s/tasks/deploy-backend.yml +++ b/scaleway-k8s/tasks/deploy-backend.yml @@ -45,22 +45,54 @@ ansible.builtin.pause: minutes: 2 -- name: Fetch services - set_fact: - service: "{{ query('kubernetes.core.k8s', kind='Service', namespace='testing') }}" - register: result - tags: [ "services"] +- name: Get IP Cluster + block: + - name: Fetch services + set_fact: + service: "{{ query('kubernetes.core.k8s', kind='Service', namespace='testing') }}" + register: result -- name: debug services - debug: - msg: "{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}" - tags: [ "services"] - -- name: Test API Backend - ansible.builtin.uri: - url: "http://{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}:{{ port }}/api/hello" - status_code: 200 - method: GET - tags: [ "services" ] + - name: debug services + debug: + msg: "{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}" + - name: Test API Backend + ansible.builtin.uri: + url: "http://{{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}:{{ port }}/api/hello" + status_code: 200 + method: GET + tags: ["services", "dns"] +- name: Add DNS for IP Cluster + block: + - name: Copy script add or update DNS + template: + src: "dns_add_update.py.j2" + dest: "{{ project_backend }}/dns_add_update.py" + + - name: Copy requierements python + copy: + src: "requierements.txt" + dest: "{{ project_backend }}" + + - name: Pip install requierements + shell: "pip3.10 install -r {{ project_backend }}/requierements.txt" + + - name: En attente de l'installation des packages + ansible.builtin.pause: + minutes: 2 + + - name: Add or update DNS + shell: "python3.10 {{ project_backend }}/dns_add_update.py --ip {{ result.ansible_facts.service[0].status.loadBalancer.ingress[0].ip }}" + register: result + + - name: Display result script + debug: + msg: "{{ result }}" + + - name: Test API Backend with DNS + ansible.builtin.uri: + url: "http://api.valczeryba.ovh:{{ port }}/api/hello" + status_code: 200 + method: GET + tags: [ "dns" ] \ No newline at end of file diff --git a/scaleway-k8s/templates/dns_add_update.py.j2 b/scaleway-k8s/templates/dns_add_update.py.j2 new file mode 100644 index 0000000..c9ade17 --- /dev/null +++ b/scaleway-k8s/templates/dns_add_update.py.j2 @@ -0,0 +1,49 @@ +# -*- encoding: utf-8 -*- +''' +First, install the latest release of Python wrapper: $ pip install ovh +''' +import json +import ovh +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--ip', help='foo help') +args = parser.parse_args() + +# Instanciate an OVH Client. +# You can generate new credentials with full access to your account on +# the token creation page +client = ovh.Client( + endpoint='ovh-eu', # Endpoint of API OVH Europe (List of available endpoints) + application_key='{{ application_key }}', # Application Key + application_secret='{{ application_secret }}', # Application Secret + consumer_key='{{ consumer_key }}', # Consumer Key +) + +result = client.get('/domain/zone/valczeryba.ovh/record', + fieldType='A', + subDomain='api', +) + +# Pretty print + +if len(result) > 0: + for idDns in result: + result = client.get('/domain/zone/valczeryba.ovh/record/{0}'.format(idDns)) + if result["target"] != args.ip: + result = client.put('/domain/zone/valczeryba.ovh/record/{0}'.format(idDns), + subDomain='api', + target=args.ip, + ) + print(result) + + + +else: + result = client.post('/domain/zone/valczeryba.ovh/record', + fieldType='A', + subDomain='api', + target=args.ip, + ttl=None, + ) + print(result) \ No newline at end of file