Compare commits
13 Commits
pkg/dev/ri
...
pkg/dev/ri
Author | SHA1 | Date | |
---|---|---|---|
326af0cd5c | |||
9cf64d8238 | |||
fbf4fc9643 | |||
1b5ef5021e | |||
24ea05b64a | |||
7fc109842d | |||
14f4bbf234 | |||
6dcb5b3bf4 | |||
1b5dab9406 | |||
43de83cce7 | |||
aacdd094f3 | |||
3d5d831a8c | |||
00017de13c |
2
debian/cucchiaiata.install
vendored
2
debian/cucchiaiata.install
vendored
@ -1 +1 @@
|
||||
script/cucchiaiata-cli usr/bin/
|
||||
scripts/* usr/bin/
|
||||
|
86
provisioning.yaml
Executable file
86
provisioning.yaml
Executable file
@ -0,0 +1,86 @@
|
||||
zones:
|
||||
- name: internet
|
||||
settings:
|
||||
configuration:
|
||||
host:
|
||||
network: 192.168.1.0/24
|
||||
start: 192.168.1.10
|
||||
stop: 192.168.1.100
|
||||
dns: [192.168.1.2]
|
||||
vlan_id: 192
|
||||
gateway: 192.168.1.254
|
||||
|
||||
clusters:
|
||||
- name: hapy.ac-test.fr
|
||||
zone: internet
|
||||
zones: [internet]
|
||||
settings:
|
||||
configuration:
|
||||
virtual_ip: 192.168.1.1
|
||||
opennebula:
|
||||
one_user: eoleone
|
||||
one_password: eole
|
||||
nodes:
|
||||
- name: node1.cadoles.com
|
||||
|
||||
servermodels:
|
||||
- name: unbound
|
||||
applicationservices: [unbound]
|
||||
settings:
|
||||
configuration:
|
||||
serveur_dns:
|
||||
unbound_local_zones: cadoles.com
|
||||
unbound_allowed_client_cidr: 192.168.1.0/24
|
||||
reseau:
|
||||
unbound_route_address: 192.168.1.254
|
||||
unbound:
|
||||
unbound_zone_cadoles_com:
|
||||
hostname_cadoles_com:
|
||||
hostname_cadoles_com:
|
||||
- toto
|
||||
- titi
|
||||
ip_cadoles_com:
|
||||
- index: 0
|
||||
value: 192.168.1.25
|
||||
type_cadoles_com:
|
||||
- index: 1
|
||||
value: CNAME
|
||||
cname_cadoles_com:
|
||||
- index: 1
|
||||
value: toto.cadoles.com
|
||||
- name: aca
|
||||
applicationservices: [openssh-server]
|
||||
settings:
|
||||
configuration:
|
||||
serveur_ssh:
|
||||
ssh_allow_networks: admin.cadoles.com
|
||||
children:
|
||||
- name: etb1
|
||||
children:
|
||||
- name: unbound_etab1
|
||||
other_parents: [unbound]
|
||||
settings:
|
||||
configuration:
|
||||
reseau:
|
||||
unbound_domain_name: dns.cadoles.com
|
||||
|
||||
organizations:
|
||||
- name: cadoles.com
|
||||
sites:
|
||||
- name: etab1
|
||||
zones: [internet]
|
||||
servers:
|
||||
- name: dns.cadoles.com
|
||||
cluster: hapy.ac-test.fr
|
||||
zones: [internet]
|
||||
servermodel: unbound_etab1
|
||||
settings:
|
||||
configuration:
|
||||
reseau:
|
||||
unbound_ip_address_cidr: 192.168.1.2/24
|
||||
opennebula:
|
||||
cpu: 0.2
|
||||
vcpu: 2
|
||||
memory: 2
|
||||
- name: etab2
|
||||
zones: [internet]
|
@ -1,11 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
set -xe
|
||||
|
||||
#=======================================================================================================
|
||||
# Import Cadoles seed
|
||||
#=======================================================================================================
|
||||
|
||||
cucchiaiata-cli v1.setting.source.create --source_name Cadoles --source_directory /usr/share/risotto/seed/cadoles
|
||||
function get_id() {
|
||||
S=$($1)
|
||||
V=$(echo $S|jq -r .session_id)
|
||||
if [ $? = 0 ]; then
|
||||
echo $V
|
||||
else
|
||||
echo $S
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
#=======================================================================================================
|
||||
# Infrastructure
|
||||
@ -28,31 +33,36 @@ cucchiaiata-cli v1.user.role.create --user_login gnunux \
|
||||
# +~~~~~~~~~~+
|
||||
cucchiaiata-cli v1.infra.zone.create --zone_name internet
|
||||
# zone configuration
|
||||
S=$(cucchiaiata-cli v1.setting.session.zone.start --zone_name internet | jq -r .session_id)
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.network.network "192.168.1.0/24" \
|
||||
--configuration.network.host_start "192.168.1.10" \
|
||||
--configuration.network.host_end "192.168.1.100" \
|
||||
--configuration.network.dns 192.168.1.2 \
|
||||
--configuration.network.gateway 192.168.1.254
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.zone.start --zone_name internet")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.host.network "192.168.1.0/24" \
|
||||
--configuration.host.start "192.168.1.10" \
|
||||
--configuration.host.stop "192.168.1.100" \
|
||||
--configuration.dns 192.168.1.2 \
|
||||
--configuration.vlan_id 192 \
|
||||
--configuration.gateway 192.168.1.254
|
||||
cucchiaiata-cli v1.setting.session.validate --session_id $S
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
|
||||
# Create the cluster cluster.cadoles.com
|
||||
# Create the cluster hapy.ac-test.fr
|
||||
# +---------------------------+
|
||||
# | cluster.cadoles.com | +~~~~~~~~~~+
|
||||
# | hapy.ac-test.fr | +~~~~~~~~~~+
|
||||
# | +--------------------+ |-------! ZONE !
|
||||
# | | node1.cadoles.com |---|-------! internet !
|
||||
# | +--------------------+ | +~~~~~~~~~~+
|
||||
# +---------------------------+
|
||||
cucchiaiata-cli v1.infra.cluster.create --cluster_name cluster.cadoles.com \
|
||||
--zone_name internet
|
||||
cucchiaiata-cli v1.infra.cluster.node.create --server_name node1.cadoles.com \
|
||||
--zones_name internet
|
||||
cucchiaiata-cli v1.infra.cluster.create --cluster_name hapy.ac-test.fr \
|
||||
--zone_name internet \
|
||||
--zones_name internet
|
||||
cucchiaiata-cli v1.infra.cluster.node.create --node_name node1.cadoles.com \
|
||||
--cluster_name hapy.ac-test.fr
|
||||
|
||||
# configuration
|
||||
S=$(cucchiaiata-cli v1.setting.session.cluster.start --cluster_name cluster.cadoles.com | jq -r .session_id)
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.network.virtual_ip "192.168.1.1"
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.cluster.start --cluster_name hapy.ac-test.fr")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.virtual_ip "192.168.1.1"
|
||||
# FIXME endpoint == https://<cluster_name>/RPC2
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.opennebula.one_user "eoleone"
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.opennebula.one_password "eole"
|
||||
cucchiaiata-cli v1.setting.session.validate --session_id $S
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
@ -76,31 +86,28 @@ cucchiaiata-cli v1.infra.site.create --site_name etab2 \
|
||||
|
||||
# Servermodels
|
||||
# -> base-fedora-32 (Cadoles)
|
||||
# |-> sm_cluster (servermodel)
|
||||
# |
|
||||
# '-> unbound (Cadoles)
|
||||
# '-> unbound (servermodel) ----,
|
||||
# |
|
||||
# -> openssh-server (Cadoles) |--> unbound_etab1 (servermodel)
|
||||
# '-> aca (servermodel) |
|
||||
# '-> etab1 (servermodel) ------'
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name sm_cluster --applicationservices base-fedora-32
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name unbound --applicationservices unbound
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name aca --applicationservices openssh-server
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name unbound --applicationservices_name unbound
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name aca --applicationservices_name openssh-server
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name etab1 --parents_name aca
|
||||
cucchiaiata-cli v1.setting.servermodel.create --servermodel_name unbound_etab1 --parents_name etab1 unbound
|
||||
|
||||
# configuration
|
||||
|
||||
# Servermodel ACA
|
||||
S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name aca| jq -r .session_id)
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name aca")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.serveur_ssh.ssh_allow_networks admin.cadoles.com
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
# Servermodel unbound
|
||||
S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name unbound| jq -r .session_id)
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name unbound")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.serveur_dns.unbound_local_zones cadoles.com \
|
||||
--configuration.reseau.unbound_route_address 192.168.1.2 \
|
||||
--configuration.reseau.unbound_route_address 192.168.1.254 \
|
||||
--configuration.serveur_dns.unbound_allowed_client_cidr 192.168.1.0/24
|
||||
cucchiaiata-cli v1.setting.session.filter --session_id "$S" --namespace unbound
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.hostname_cadoles_com toto titi \
|
||||
@ -110,7 +117,7 @@ cucchiaiata-cli v1.setting.session.configure --session_id "$S" --unbound.unbound
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
# Servermodel unbound_etab1
|
||||
S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name unbound_etab1 | jq -r .session_id)
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name unbound_etab1")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.reseau.unbound_domain_name dns.cadoles.com
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
@ -125,16 +132,17 @@ cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
# '-> dns.cadoles.com (server)
|
||||
cucchiaiata-cli v1.infra.server.create --server_name dns.cadoles.com \
|
||||
--site_name etab1 \
|
||||
--cluster_name cluster.cadoles.com \
|
||||
--cluster_name hapy.ac-test.fr \
|
||||
--zones_name internet \
|
||||
--servermodel_name unbound_etab1
|
||||
# configuration
|
||||
S=$(cucchiaiata-cli v1.setting.session.server.start --server dns.cadoles.com | jq -r .session_id)
|
||||
sleep 1
|
||||
S=$(get_id "cucchiaiata-cli v1.setting.session.server.start --server dns.cadoles.com")
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --configuration.reseau.unbound_ip_address_cidr 192.168.1.2/24
|
||||
cucchiaiata-cli v1.setting.session.filter --session_id "$S" --namespace opennebula
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --opennebula.configuration.cpu 0.2 \
|
||||
--opennebula.configuration.vcpu 2 \
|
||||
--opennebula.configuration.memory 2
|
||||
cucchiaiata-cli v1.setting.session.configure --session_id "$S" --opennebula.cpu 0.2 \
|
||||
--opennebula.vcpu 2 \
|
||||
--opennebula.memory 2
|
||||
cucchiaiata-cli v1.setting.session.validate --session_id $S
|
||||
cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
|
||||
@ -143,11 +151,8 @@ cucchiaiata-cli v1.setting.session.stop --session_id "$S" --save
|
||||
#=======================================================================================================
|
||||
|
||||
# Generate configuration
|
||||
cucchiaiata-cli v1.setting.config.configuration.server.deploy --server dns.cadoles.com
|
||||
cucchiaiata-cli v1.setting.template.generate --server dns.cadoles.com
|
||||
|
||||
# Generate cluster's configurtion
|
||||
#cucchiaiata-cli v1.provider.configure -c cluster.cadoles.com
|
||||
#cucchiaiata-cli v1.provider.deploy --server dns.cadoles.com
|
||||
cucchiaiata-cli v1.infra.cluster.deploy --cluster_name hapy.ac-test.fr
|
||||
cucchiaiata-cli v1.infra.zone.deploy --zone_name internet
|
||||
cucchiaiata-cli v1.infra.server.deploy --server_name dns.cadoles.com
|
||||
|
||||
echo "fin"
|
||||
|
@ -4,21 +4,26 @@
|
||||
from sys import exit, argv
|
||||
from json import dumps
|
||||
from traceback import print_exc
|
||||
from cucchiaiata import Parser, config, Configuration
|
||||
from cucchiaiata import Parser, config, Configuration, JsonError
|
||||
from cucchiaiata.i18n import _
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
if len(argv) > 2 and argv[1] in ['v1.setting.session.configure',
|
||||
'v1.setting.session.configure']:
|
||||
if len(argv) > 2 and argv[1] == 'v1.setting.session.configure':
|
||||
Configuration().get()
|
||||
else:
|
||||
parser = Parser()
|
||||
print(dumps(parser.get(),
|
||||
indent=config.indent))
|
||||
indent=config.indent),
|
||||
)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except JsonError as err:
|
||||
print(dumps(err.message,
|
||||
indent=config.indent),
|
||||
)
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
if config.debug:
|
||||
print_exc()
|
232
scripts/cucchiaiata-import
Executable file
232
scripts/cucchiaiata-import
Executable file
@ -0,0 +1,232 @@
|
||||
#!/usr/bin/python3
|
||||
"""Zephir-cmd-input script
|
||||
"""
|
||||
from sys import exit, argv
|
||||
from yaml import load, SafeLoader, YAMLError
|
||||
from time import sleep
|
||||
from os.path import isfile
|
||||
from json import dumps
|
||||
|
||||
from cucchiaiata import Configuration, JsonError, config
|
||||
from cucchiaiata.common import Common
|
||||
from cucchiaiata.i18n import _
|
||||
|
||||
|
||||
|
||||
class Import(Common):
|
||||
def __init__(self,
|
||||
config_file: str,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
with open(config_file, 'r') as stream:
|
||||
try:
|
||||
self.config = load(stream,
|
||||
Loader=SafeLoader,
|
||||
)
|
||||
except YAMLError as err:
|
||||
raise Exception(_('unable to lead the YAML file {}').format(err))
|
||||
|
||||
self.remote_config = self.remote_json_to_config(self.cucchiaiata_config.remote_url)
|
||||
|
||||
def parse_zones(self) -> None:
|
||||
for zone in self.config.get('zones', []):
|
||||
self.configuration('infra',
|
||||
'zone',
|
||||
zone,
|
||||
)
|
||||
self.send('v1.infra.zone.deploy',
|
||||
zone_name=zone['name'],
|
||||
)
|
||||
|
||||
def parse_clusters(self) -> None:
|
||||
for cluster in self.config.get('clusters', []):
|
||||
self.configuration('infra',
|
||||
'cluster',
|
||||
cluster,
|
||||
zone_name=cluster['zone'],
|
||||
zones_name=cluster['zones'],
|
||||
)
|
||||
for node in cluster.get('nodes', []):
|
||||
self.configuration('infra',
|
||||
'cluster.node',
|
||||
node,
|
||||
cluster_name=cluster['name'],
|
||||
)
|
||||
self.send('v1.infra.cluster.deploy',
|
||||
cluster_name=cluster['name'],
|
||||
)
|
||||
|
||||
def parse_servermodels(self) -> None:
|
||||
for servermodel in self.config.get('servermodels', []):
|
||||
self.add_servermodel(servermodel)
|
||||
|
||||
def parse_organizations(self) -> None:
|
||||
for organization in self.config.get('organizations', []):
|
||||
self.configuration('infra',
|
||||
'organization',
|
||||
organization,
|
||||
)
|
||||
for site in organization.get('sites', []):
|
||||
self.configuration('infra',
|
||||
'site',
|
||||
site,
|
||||
organization_name=organization['name'],
|
||||
zones_name=site['zones'],
|
||||
)
|
||||
for server in site.get('servers', []):
|
||||
self.configuration('infra',
|
||||
'server',
|
||||
server,
|
||||
site_name=site['name'],
|
||||
cluster_name=server['cluster'],
|
||||
zones_name=server['zones'],
|
||||
servermodel_name=server['servermodel'],
|
||||
)
|
||||
self.send('v1.infra.server.deploy',
|
||||
server_name=server['name'],
|
||||
)
|
||||
|
||||
def configuration(self,
|
||||
domain: str,
|
||||
element: str,
|
||||
dico: dict,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.upset_element(domain,
|
||||
element,
|
||||
dico['name'],
|
||||
kwargs,
|
||||
)
|
||||
if 'settings' in dico:
|
||||
#FIXME
|
||||
sleep(1)
|
||||
try:
|
||||
self.apply_settings(element,
|
||||
dico,
|
||||
)
|
||||
except JsonError as err:
|
||||
raise Exception(f'unable to configure {element} "{dico["name"]}": {err.message["reason"]}')
|
||||
|
||||
def apply_settings(self,
|
||||
element: str,
|
||||
dico: dict,
|
||||
) -> None:
|
||||
session_id = self.send(f'v1.setting.session.{element}.start',
|
||||
**{f'{element}_name': dico['name']},
|
||||
)['session_id']
|
||||
config = Configuration()
|
||||
config.message = 'v1.setting.session.configure'
|
||||
config.session_id = session_id
|
||||
tiramisu = None
|
||||
tiramisu_namespace = None
|
||||
for key, value in self.settings_paths(dico['settings']):
|
||||
current_namespace = key.split('.', 1)[0]
|
||||
if tiramisu_namespace != current_namespace:
|
||||
if tiramisu is not None:
|
||||
self.send_configuration(tiramisu,
|
||||
session_id,
|
||||
)
|
||||
tiramisu_namespace = current_namespace
|
||||
self.send('v1.setting.session.filter',
|
||||
session_id=session_id,
|
||||
namespace=tiramisu_namespace,
|
||||
)
|
||||
tiramisu = config.configure_server()
|
||||
try:
|
||||
if tiramisu.option(key).option.isfollower():
|
||||
for val in value:
|
||||
tiramisu.option(key, val['index']).value.set(val['value'])
|
||||
else:
|
||||
if tiramisu.option(key).option.ismulti() and \
|
||||
not isinstance(value, list):
|
||||
value = [value]
|
||||
tiramisu.option(key).value.set(value)
|
||||
except ValueError as err:
|
||||
print(_(f'error when setting "{element}" "{dico["name"]}": "{key}" with value "{value}": {err}'))
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
print(_(f'unexpected error when setting "{element}" "{dico["name"]}": "{key}" with value "{value}": {err}'))
|
||||
exit(1)
|
||||
self.send_configuration(tiramisu,
|
||||
session_id,
|
||||
)
|
||||
self.send('v1.setting.session.stop',
|
||||
session_id=session_id,
|
||||
save=True,
|
||||
)
|
||||
|
||||
def upset_element(self,
|
||||
domain: str,
|
||||
element: str,
|
||||
name: str,
|
||||
kwargs: dict,
|
||||
) -> None:
|
||||
message_name = element
|
||||
if '.' in message_name:
|
||||
message_name = message_name.rsplit('.', 1)[-1]
|
||||
try:
|
||||
self.send(f'v1.{domain}.{element}.describe',
|
||||
**{f'{message_name}_name': name},
|
||||
)
|
||||
except:
|
||||
# not exists
|
||||
print(f'add "{element}" "{name}"')
|
||||
kwargs[f'{message_name}_name'] = name
|
||||
self.send(f'v1.{domain}.{element}.create',
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def settings_paths(self,
|
||||
dico: dict,
|
||||
subpath: str=None,
|
||||
) -> list:
|
||||
ret = []
|
||||
for key, value in dico.items():
|
||||
if subpath:
|
||||
key_path = f'{subpath}.{key}'
|
||||
else:
|
||||
key_path = key
|
||||
if isinstance(value, dict):
|
||||
ret.extend(self.settings_paths(value, key_path))
|
||||
else:
|
||||
ret.append((key_path, value))
|
||||
return ret
|
||||
|
||||
def add_servermodel(self,
|
||||
servermodel: dict,
|
||||
parents: list=[],
|
||||
) -> None:
|
||||
if 'other_parents' in servermodel:
|
||||
parents = parents.copy()
|
||||
parents.extend(servermodel['other_parents'])
|
||||
self.configuration('setting',
|
||||
'servermodel',
|
||||
servermodel,
|
||||
parents_name=parents,
|
||||
applicationservices_name=servermodel.get('applicationservices', []),
|
||||
)
|
||||
if 'children' in servermodel:
|
||||
child_parents = parents.copy()
|
||||
child_parents.append(servermodel['name'])
|
||||
for child in servermodel['children']:
|
||||
self.add_servermodel(child,
|
||||
child_parents,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(argv) != 2 or not isfile(argv[1]):
|
||||
print(_(f'usage: {argv[0]} filename.yaml'))
|
||||
exit(1)
|
||||
imp = Import(argv[1])
|
||||
try:
|
||||
imp.parse_zones()
|
||||
imp.parse_clusters()
|
||||
imp.parse_servermodels()
|
||||
imp.parse_organizations()
|
||||
except JsonError as err:
|
||||
print(err.message['reason'])
|
||||
exit(1)
|
||||
except Exception as err:
|
||||
print(err)
|
||||
exit(1)
|
@ -1,6 +1,7 @@
|
||||
from .parser import Parser
|
||||
from .configuration import Configuration
|
||||
from .config import config
|
||||
from .common import JsonError
|
||||
|
||||
__all__ = ('Parser', 'config')
|
||||
__all__ = ('Parser', 'config', 'Configuration', 'JsonError')
|
||||
__version__ = "0.0.1"
|
||||
|
@ -3,9 +3,11 @@ from requests import get, post
|
||||
from json import dumps
|
||||
from typing import Dict
|
||||
|
||||
from .config import config
|
||||
from tiramisu_api import Config
|
||||
|
||||
from .config import config
|
||||
from .i18n import _
|
||||
|
||||
|
||||
if config.allow_insecure_https:
|
||||
import warnings
|
||||
@ -13,6 +15,10 @@ if config.allow_insecure_https:
|
||||
warnings.simplefilter('ignore', InsecureRequestWarning)
|
||||
|
||||
|
||||
class JsonError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Common:
|
||||
def __init__(self):
|
||||
self.cucchiaiata_config = config
|
||||
@ -48,6 +54,42 @@ class Common:
|
||||
json = req.json()
|
||||
return config_type(json)
|
||||
|
||||
def get_payload(self,
|
||||
message: str):
|
||||
# remove symlinkoption and default value from payload
|
||||
payload = {}
|
||||
for option in self.remote_config.option(message).list():
|
||||
if not option.owner.isdefault() and not option.option.issymlinkoption():
|
||||
payload[option.option.name()] = option.value.get()
|
||||
return payload
|
||||
|
||||
def send(self,
|
||||
message: str,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.remote_config.option('message').value.set(message)
|
||||
for key, value in kwargs.items():
|
||||
self.remote_config.option(f'{message}.{key}').value.set(value)
|
||||
payload = self.get_payload(message)
|
||||
return send_data(message,
|
||||
payload,
|
||||
)
|
||||
|
||||
def send_configuration(self,
|
||||
tiramisu: 'ConfigAPI',
|
||||
session_id: str,
|
||||
) -> None:
|
||||
tiramisu.send()
|
||||
try:
|
||||
self.send('v1.setting.session.validate',
|
||||
session_id=session_id,
|
||||
)
|
||||
except JsonError as err:
|
||||
self.send('v1.setting.session.stop',
|
||||
session_id=session_id,
|
||||
)
|
||||
raise err from err
|
||||
|
||||
|
||||
def send_data(uri: str,
|
||||
payload: Dict,
|
||||
@ -60,11 +102,12 @@ def send_data(uri: str,
|
||||
ret = post(final_url,
|
||||
data=dumps(payload),
|
||||
verify=config.allow_insecure_https)
|
||||
if ret.status_code != 200:
|
||||
try:
|
||||
response = ret.json()
|
||||
except:
|
||||
raise Exception(ret.text)
|
||||
response = ret.json()
|
||||
if 'error' in response:
|
||||
if 'reason' in response['error']['kwargs']:
|
||||
raise Exception("{}".format(response['error']['kwargs']['reason']))
|
||||
raise Exception('erreur inconnue')
|
||||
if response['type'] == 'error':
|
||||
err = JsonError()
|
||||
err.message = response['response']
|
||||
raise err
|
||||
return response['response']
|
||||
|
@ -25,13 +25,11 @@ class ConfigAPI(Config):
|
||||
|
||||
class Configuration(Common):
|
||||
def configure_server(self):
|
||||
smessage = self.message.split('.')
|
||||
version = smessage[0]
|
||||
type = smessage[-2]
|
||||
version = self.message.split('.', 1)[0]
|
||||
url = '{}/{}/setting/{}'.format(self.cucchiaiata_config.remote_url,
|
||||
version,
|
||||
self.session_id,
|
||||
)
|
||||
version,
|
||||
self.session_id,
|
||||
)
|
||||
tconfig = self.remote_json_to_config(url,
|
||||
ConfigAPI,
|
||||
)
|
||||
@ -65,8 +63,11 @@ class Configuration(Common):
|
||||
def get(self):
|
||||
parameters = self.get_parameters()
|
||||
tiramisu_config = self.configure_server()
|
||||
option = next(tiramisu_config.option.list('all'))
|
||||
namespace = option.option.path().split('.', 1)[0]
|
||||
parser = TiramisuCmdlineParser(tiramisu_config,
|
||||
self.prog,
|
||||
root=namespace,
|
||||
unrestraint=True,
|
||||
fullpath=True)
|
||||
parser.parse_args(parameters,
|
||||
|
@ -36,12 +36,3 @@ class Parser(Common):
|
||||
# send message
|
||||
return send_data(message,
|
||||
payload)
|
||||
|
||||
def get_payload(self,
|
||||
message: str):
|
||||
# remove symlinkoption and default value from payload
|
||||
payload = {}
|
||||
for option in self.remote_config.option(message).list():
|
||||
if not option.owner.isdefault() and not option.option.issymlinkoption():
|
||||
payload[option.option.name()] = option.value.get()
|
||||
return payload
|
||||
|
Reference in New Issue
Block a user