diff --git a/provisioning_example.sh b/provisioning_example.sh index 73c4e24..62421da 100755 --- a/provisioning_example.sh +++ b/provisioning_example.sh @@ -1,174 +1,120 @@ #!/usr/bin/env bash +set -xe -# Configuration -# -> base (cadoles' servermodel) -# |-> cluster (servermodel) -# | '-> node1.cadoles.com (server) -# | -# |-> aca (servermodel) + unbound (AS) + openssh (AS) -# | '-> etab1 (servermodel) -------------------, -# | | -# '-> fedora-32 (cadoles' servermodel) | -# '-> unbound (servermodel) + unbound (AS) --| -# '-> unbound_etab1 (servermodel) -# '-> dns.cadoles.com (server) +#======================================================================================================= +# Import Cadoles seed +#======================================================================================================= -# Site and zone +cucchiaiata-cli v1.setting.source.create --source_name cadoles --source_directory /srv/risotto/seed/cadoles + +#======================================================================================================= +# Infrastructure +#======================================================================================================= + +# Create a new user and set role 'server_rw' for the server dns.cadoles.com +cucchiaiata-cli v1.user.user.create --user_login gnunux --user_name gnunux --user_surname gnunux +cucchiaiata-cli v1.user.role.create --user_login gnunux --role_name 'server_rw' --role_attribute 'Server.ServerName' --role_attribute_value dns.cadoles.com + +# Sites and zone # + = = = = = = = = = = = = = = = = = = = = = = = + -# " SITE cluster " -# " " +# " SITE site_cluster " # " +-------------------+ " # " | node1.cadoles.com | "-----------+ -# " | | " | -# " +-------------------+ " | -# " " +~~~~~~~~+ +# " +-------------------+ " +~~~~~~~~+ # + = = = = = = = = = = = = = = = = = = = = = = = + ! ZONE ! # ! pedago ! # + = = = = = = = = = = = = = = = = = = = = = = = + +~~~~~~~~+ # " SITE etab1 " | -# " +-----------------+ " | -# " | dns.cadoles.com | "-----------+ -# " | | " +# " +-----------------+ "-----------+ +# " | dns.cadoles.com | " # " +-----------------+ " -# " " # + = = = = = = = = = = = = = = = = = = = = = = = + -# +cucchiaiata-cli v1.infra.site.create --site_name site_cluster +cucchiaiata-cli v1.infra.site.create --site_name etab1 +# Zone +cucchiaiata-cli v1.infra.zone.create --zone_name pedago --sites_name etab1 site_cluster --network "192.168.1.0/24" --host_start "192.168.1.10" --host_end "192.168.1.100" --dns 192.168.1.1 --gateway 192.168.1.254 +# Servermodels +# -> base-fedora-32 (cadoles' servermodel) +# |-> sm_cluster (servermodel) +# | '-> node1.cadoles.com (server) +# | +# |-> aca (servermodel) + unbound (AS) + openssh (AS) +# | '-> etab1 (servermodel) -----------------, +# | |--> unbound_etab1 (servermodel) +# '-> unbound (servermodel) + unbound (AS) --' '-> dns.cadoles.com (server) +## sm_cluster +cucchiaiata-cli v1.setting.servermodel.create --servermodel_name sm_cluster --applicationservices base-fedora-32 +## ACA +cucchiaiata-cli v1.setting.servermodel.create --servermodel_name aca --applicationservices unbound openssh +## etab1 +cucchiaiata-cli v1.setting.servermodel.create --servermodel_name etab1 --parents_name aca +## unbound +cucchiaiata-cli v1.setting.servermodel.create --servermodel_name unbound --applicationservices unbound +## unbound_etab1 +cucchiaiata-cli v1.setting.servermodel.create --servermodel_name unbound_etab1 --parents_name etab1 unbound + +# Servers +cucchiaiata-cli v1.infra.server.create --server_name node1.cadoles.com --servermodel_name sm_cluster --site_name site_cluster --zones_name pedago +cucchiaiata-cli v1.infra.server.create --server_name dns.cadoles.com --servermodel_name unbound_etab1 --site_name etab1 --zones_name pedago + +# Create a cluster +cucchiaiata-cli v1.provider.factory.cluster.create --cluster_name cluster.cadoles.com --virtual_ip 192.168.1.1 --zone_name pedago # Cluster description: # +-----------------------------+ # + cluster.cadoles.com + -# + + # + +--------------------+ + # + + node1.cadoles.com + + -# + + + + -# + + + + # + +--------------------+ + -# + + # +-----------------------------+ - +cucchiaiata-cli v1.provider.factory.cluster.join --cluster_name cluster.cadoles.com --node_name node1.cadoles.com # Cluster execution: # +-----------------------------+ # + cluster.cadoles.com + -# + + # + +--------------------+ + # + + dns.cadoles.com + + -# + + + + -# + + + + # + +--------------------+ + -# + + # +-----------------------------+ +cucchiaiata-cli v1.provider.factory.server.add --server dns.cadoles.com --cluster_name cluster.cadoles.com --cpu 0.2 --vcpu 2 --memory 2 -set -xe +#======================================================================================================= +# Configuration +#======================================================================================================= -verif() { - i=0 - argv=("$@") - for V in configuration.reseau.unbound_route_address configuration.reseau.unbound_domain_name configuration.serveur_dns.unbound_local_zones configuration.reseau.unbound_ip_address_cidr; do - i=$((i+1)) - R=$(cucchiaiata-cli "setting.session.$1.get" -s "$S" -n "$V" | jq -Mcr .content[]) - [ "$R" = "${argv[i]}" ] - done -} +# Servermodel ACA +S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name aca| jq -r .session_id) +cucchiaiata-cli v1.setting.session.servermodel.configure --session_id "$S" --configuration.reseau.unbound_route_address 192.168.1.2 \ + --configuration.serveur_dns.unbound_allowed_client_cidr 192.168.1.0/24 \ + --configuration.serveur_ssh.ssh_allow_networks admin.cadoles.com +cucchiaiata-cli v1.setting.session.servermodel.stop --session_id "$S" --save -# Import Cadoles seed -cucchiaiata-cli setting.source.create -o cadoles -d /srv/risotto/seed/cadoles +# Servermodel etab1 +S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name etab1 | jq -r .session_id) +cucchiaiata-cli v1.setting.session.servermodel.configure --session_id "$S" --configuration.reseau.unbound_domain_name dns.cadoles.com +cucchiaiata-cli v1.setting.session.servermodel.stop --session_id "$S" --save -# Create a new user and set role 'server_rw' for the server dns.cadoles.com -cucchiaiata-cli user.user.create -u gnunux -n gnunux -s gnunux -cucchiaiata-cli user.role.create -u gnunux -o 'server_rw' -a 'Server.ServerName' -v dns.cadoles.com +# Servermodel unbound +S=$(cucchiaiata-cli v1.setting.session.servermodel.start --servermodel_name unbound| jq -r .session_id) +cucchiaiata-cli v1.setting.session.servermodel.configure --session_id "$S" --configuration.serveur_dns.unbound_local_zones cadoles.com +cucchiaiata-cli v1.setting.session.servermodel.filter --session_id "$S" --namespace unbound +cucchiaiata-cli v1.setting.session.servermodel.configure --session_id "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.hostname_cadoles_com toto titi \ + --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.ip_cadoles_com 0 192.168.1.25 \ + --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.type_cadoles_com 1 CNAME \ + --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.cname_cadoles_com 1 toto.cadoles.com +cucchiaiata-cli v1.setting.session.servermodel.stop --session_id "$S" --save -# Heritage -## ACA -cucchiaiata-cli setting.servermodel.create -m aca -d Aca -p base -o cadoles -e last -cucchiaiata-cli setting.servermodel.dependency.add -m aca -a unbound -o cadoles -e last - -S=$(cucchiaiata-cli setting.session.servermodel.start -m aca -e last| jq -r .session_id) -verif servermodel null null [] null -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --configuration.reseau.unbound_route_address 192.168.1.2 -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --configuration.serveur_dns.unbound_allowed_client_cidr 192.168.1.0/24 -verif servermodel 192.168.1.2 null [] null -cucchiaiata-cli setting.session.servermodel.stop -s "$S" -a - -## etab1 -cucchiaiata-cli setting.servermodel.create -m etab1 -d "Etab 1" -p aca -o internal -e last - -S=$(cucchiaiata-cli setting.session.servermodel.start -m etab1 -e last| jq -r .session_id) -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --configuration.reseau.unbound_domain_name dns.cadoles.com -verif servermodel 192.168.1.2 dns.cadoles.com [] null -cucchiaiata-cli setting.session.servermodel.stop -s "$S" -a - -## unbound -cucchiaiata-cli setting.servermodel.create -m unbound -d "generic unbound configuration" -p fedora-32 -o cadoles -e last -cucchiaiata-cli setting.servermodel.dependency.add -m unbound -a unbound -o cadoles -e last - -S=$(cucchiaiata-cli setting.session.servermodel.start -m unbound -e last| jq -r .session_id) -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --configuration.serveur_dns.unbound_local_zones cadoles.com -cucchiaiata-cli setting.session.servermodel.filter -s "$S" -n unbound -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.hostname_cadoles_com toto titi -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.ip_cadoles_com 0 192.168.1.25 -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.type_cadoles_com 1 CNAME -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --unbound.unbound_zone_cadoles_com.hostname_cadoles_com.cname_cadoles_com 1 toto.cadoles.com -cucchiaiata-cli setting.session.servermodel.filter -s "$S" -n configuration -verif servermodel null null '["cadoles.com"]' null -cucchiaiata-cli setting.session.servermodel.stop -s "$S" -a - -## unbound_etab1 -cucchiaiata-cli setting.servermodel.create -m unbound_etab1 -d "unbound configuration for etab1" -p etab1 unbound -o internal -e last - -S=$(cucchiaiata-cli setting.session.servermodel.start -m unbound_etab1 -e last | jq -r .session_id) -verif servermodel 192.168.1.2 dns.cadoles.com '["cadoles.com"]' null -cucchiaiata-cli setting.session.servermodel.stop -s "$S" -a - -# Create sites and zone -cucchiaiata-cli infra.site.create -i cluster -d "Cluster" -cucchiaiata-cli infra.site.create -i etab1 -d "Etab 1" -cucchiaiata-cli infra.zone.create -z pedago -d "Pedago" -s etab1 cluster -n "192.168.1.0/24" -t "192.168.1.10" -e "192.168.1.100" -q 192.168.1.1 -g 192.168.1.254 - -# Create a servermodel for node in cluster and a server "node1" -cucchiaiata-cli setting.servermodel.create -m cluster -d "Node in the cluster" -p base -o cadoles -e last -cucchiaiata-cli infra.server.create -s node1.cadoles.com -d node1 -m cluster -e last -i cluster -z pedago - -# Create a new server "dns" -cucchiaiata-cli infra.server.create -s dns.cadoles.com -d description -m unbound_etab1 -e last -i etab1 -z pedago - -# Create a cluster with one node and one VM -cucchiaiata-cli provider.factory.cluster.create -c cluster.cadoles.com -d toto -v 192.168.1.1 -z pedago -cucchiaiata-cli provider.factory.cluster.join -c cluster.cadoles.com -n node1.cadoles.com -cucchiaiata-cli provider.factory.server.add -s dns.cadoles.com -c cluster.cadoles.com -p 0.2 -v 2 -m 2 - -# Configure server -S=$(cucchiaiata-cli setting.session.server.start -s dns.cadoles.com | jq -r .session_id) -cucchiaiata-cli setting.session.server.configure -s "$S" --configuration.reseau.unbound_ip_address_cidr 192.168.1.1/24 -cucchiaiata-cli setting.session.server.validate -s $S -verif server 192.168.1.2 dns.cadoles.com '["cadoles.com"]' 192.168.1.1/24 -cucchiaiata-cli setting.session.server.stop -s "$S" -a +# Server dns.cadoles.com +S=$(cucchiaiata-cli v1.setting.session.server.start --server dns.cadoles.com | jq -r .session_id) +cucchiaiata-cli v1.setting.session.server.configure --session_id "$S" --configuration.reseau.unbound_ip_address_cidr 192.168.1.1/24 +cucchiaiata-cli v1.setting.session.server.validate --session_id $S +cucchiaiata-cli v1.setting.session.server.stop --session_id "$S" --save # Generate configuration -cucchiaiata-cli setting.config.configuration.server.deploy -s dns.cadoles.com -cucchiaiata-cli setting.template.generate -s dns.cadoles.com - -# Add OpenSSH dependency -cucchiaiata-cli setting.servermodel.dependency.add -m aca -a openssh -o cadoles -e last - -S=$(cucchiaiata-cli setting.session.server.start -s dns.cadoles.com | jq -r .session_id) -R=$(cucchiaiata-cli setting.session.server.get -s "$S" -n configuration.serveur_ssh.ssh_allow_networks | jq -Mcr .content[]) -[ "$R" = "[]" ] -cucchiaiata-cli setting.session.server.stop -s "$S" - -S=$(cucchiaiata-cli setting.session.servermodel.start -m aca -e last| jq -r .session_id) -cucchiaiata-cli setting.session.servermodel.configure -s "$S" --configuration.serveur_ssh.ssh_allow_networks admin.cadoles.com -cucchiaiata-cli setting.session.servermodel.stop -s "$S" -a - -S=$(cucchiaiata-cli setting.session.server.start -s dns.cadoles.com | jq -r .session_id) -R=$(cucchiaiata-cli setting.session.server.get -s "$S" -n configuration.serveur_ssh.ssh_allow_networks | jq -Mcr .content[]) -[ "$R" = '["admin.cadoles.com"]' ] -cucchiaiata-cli setting.session.server.stop -s "$S" - -# Regenerate configuration -cucchiaiata-cli setting.config.configuration.server.deploy -s dns.cadoles.com -cucchiaiata-cli setting.template.generate -s dns.cadoles.com +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 provider.factory.configure -c cluster.cadoles.com +#cucchiaiata-cli v1.provider.factory.configure -c cluster.cadoles.com +#cucchiaiata-cli v1.provider.factory.deploy --server dns.cadoles.com echo "fin" diff --git a/script/cucchiaiata-cli b/script/cucchiaiata-cli index 4f24fdd..059538d 100755 --- a/script/cucchiaiata-cli +++ b/script/cucchiaiata-cli @@ -10,13 +10,15 @@ from cucchiaiata.i18n import _ def main(): try: - if len(argv) > 2 and argv[1] in ['setting.session.server.configure', - 'setting.session.servermodel.configure']: + if len(argv) > 2 and argv[1] in ['v1.setting.session.server.configure', + 'v1.setting.session.servermodel.configure']: Configuration().get() else: parser = Parser() print(dumps(parser.get(), indent=config.indent)) + except KeyboardInterrupt: + pass except Exception as err: if config.debug: print_exc() @@ -25,7 +27,4 @@ def main(): if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - pass + main() diff --git a/src/cucchiaiata/common.py b/src/cucchiaiata/common.py index e67ddef..c0fa6a5 100644 --- a/src/cucchiaiata/common.py +++ b/src/cucchiaiata/common.py @@ -32,16 +32,16 @@ class Common: return err def remote_json_to_config(self, - url=None, - config_type=Config): + url, + config_type=Config, + ): "retrieves the remote config from the distant api description" - if url is None: - url = self.cucchiaiata_config.remote_url token = self.get_token() headers = {'Authorization':'Bearer {}'.format(token)} req = get(url, headers=headers, - verify=config.allow_insecure_https) + verify=config.allow_insecure_https, + ) code = req.status_code if code != 200: raise Exception(self.get_error_from_http(req)) @@ -49,9 +49,14 @@ class Common: return config_type(json) -def send_data(message: str, - payload: Dict): - final_url = '{}/{}'.format(config.remote_url, message) +def send_data(uri: str, + payload: Dict, + ): + version, message = uri.split('.', 1) + final_url = '{}/{}/{}'.format(config.remote_url, + version, + message, + ) ret = post(final_url, data=dumps(payload), verify=config.allow_insecure_https) diff --git a/src/cucchiaiata/config.py b/src/cucchiaiata/config.py index 26db22c..b68c1c9 100644 --- a/src/cucchiaiata/config.py +++ b/src/cucchiaiata/config.py @@ -11,11 +11,7 @@ class Config: if not isfile(config_file): print(_('Attention, there is no configuration file')) url = input(_('Address to Risotto server: ')) - version = input(_('Risotto API\'s version (default: "v1"): ')) - if not version: - version = "v1" - yaml_template = f"""url: {url} -version: {version}""" + yaml_template = f'url: {url}' with open(config_file, 'w') as fh: fh.write(yaml_template) @@ -28,9 +24,8 @@ version: {version}""" raise Exception(_('Error when creating the config file {}').format(err)) self.url = config['url'] - self.version = config['version'] self.debug = config.get('debug', False) - self.remote_url = 'http://{}/api/{}'.format(self.url, self.version) + self.remote_url = f'http://{self.url}/api' self.token_file = join(expanduser("~"), '.zephir-client.jwt.token') self.indent = config.get('indent', 2) self.allow_insecure_https = config.get('allow_insecure_https', False) diff --git a/src/cucchiaiata/configuration.py b/src/cucchiaiata/configuration.py index a9d844e..a05dbce 100644 --- a/src/cucchiaiata/configuration.py +++ b/src/cucchiaiata/configuration.py @@ -3,16 +3,16 @@ from sys import argv, exit from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_api import Config -from cucchiaiata.i18n import _ - +from .i18n import _ +from .parser import Parser from .common import send_data, Common class ConfigAPI(Config): def send_data(self, data): - for index, payload in enumerate(data['updates']): + for index, payload in enumerate(data['updates']): payload['session_id'] = self.session_id if isinstance(payload['value'], list): payload['value_multi'] = payload['value'] @@ -25,15 +25,16 @@ class ConfigAPI(Config): class Configuration(Common): def configure_server(self): - if self.message == 'setting.session.server.configure': - type = 'server' - else: - type = 'servermodel' - url = '{}/setting/{}/{}'.format(self.cucchiaiata_config.remote_url, - type, - self.session_id) + smessage = self.message.split('.') + version = smessage[0] + type = smessage[-2] + url = '{}/{}/setting/{}/{}'.format(self.cucchiaiata_config.remote_url, + version, + type, + self.session_id) tconfig = self.remote_json_to_config(url, - ConfigAPI) + ConfigAPI, + ) tconfig.message = self.message tconfig.session_id = self.session_id return tconfig @@ -52,14 +53,9 @@ class Configuration(Common): index = parameters.index('-s') except ValueError: try: - index = parameters.index('--sessionid') + index = parameters.index('--session_id') except ValueError: - # FIXME not working ... - tiramisu_config = self.remote_json_to_config(ConfigAPI) - parser = TiramisuCmdlineParser(tiramisu_config, - self.prog, - unrestraint=True, - fullpath=True) + parser = Parser() parser.print_help() exit(1) parameters.pop(index) diff --git a/src/cucchiaiata/parser.py b/src/cucchiaiata/parser.py index 3ba4156..4f2f27a 100644 --- a/src/cucchiaiata/parser.py +++ b/src/cucchiaiata/parser.py @@ -19,12 +19,14 @@ class Parser(Common): def __init__(self): super().__init__() # build a tiramisu parser and parse argument - self.remote_config = self.remote_json_to_config() + url = self.cucchiaiata_config.remote_url + self.remote_config = self.remote_json_to_config(url) parser = CucchiaiataParser(self.remote_config, fullpath=False, remove_empty_od=True, display_modified_value=False, - formatter_class=RawDescriptionHelpFormatter) + formatter_class=RawDescriptionHelpFormatter, + ) parser.parse_args() def get(self):