Compare commits
14 Commits
1e223e7b57
...
jwt
Author | SHA1 | Date | |
---|---|---|---|
424273360d | |||
ae6dfb2644 | |||
d78d581c65 | |||
52c878b0ab | |||
5666c01bdc | |||
3a9243bfb8 | |||
bcd17e1038 | |||
47d5ed77d5 | |||
02c38589d4 | |||
cb2dbe135e | |||
cb0e4b5d5d | |||
722d4894a1 | |||
4020f97db0 | |||
a6383f0c2c |
42
README.md
42
README.md
@ -20,13 +20,14 @@ docker run -d --add-host reload.example.com:127.0.0.1 -p 80:80 coudot/lemonldap-
|
||||
|
||||
Démarrer un serveur postgresql de test
|
||||
```
|
||||
podman pull docker.io/library/postgres:11-alpine
|
||||
podman run -dt -p 5432:5432 postgres:11-alpine
|
||||
docker run -dt -p 5432:5432 --name postgres postgres:11-alpine
|
||||
docker exec -ti postgres bash
|
||||
|
||||
psql -U postgres -h localhost -c "CREATE ROLE risotto WITH LOGIN PASSWORD 'risotto';"
|
||||
psql -U postgres -h localhost -c "CREATE DATABASE risotto;"
|
||||
psql -U postgres -h localhost -c "GRANT ALL ON DATABASE risotto TO risotto;"
|
||||
psql -U postgres -h localhost -c "CREATE EXTENSION hstore;" risotto
|
||||
psql -U postgres -h localhost -c "CREATE EXTENSION hstore;"
|
||||
psql -U postgres -h localhost -c "CREATE EXTENSION pgcrypto;"
|
||||
```
|
||||
|
||||
Gestion de la base de données avec Sqitch
|
||||
@ -35,3 +36,38 @@ Gestion de la base de données avec Sqitch
|
||||
cpanm --quiet --notest App::Sqitch
|
||||
sqitch init risotto --uri https://forge.cadoles.com/Infra/risotto --engine pg
|
||||
```
|
||||
|
||||
|
||||
Commande :
|
||||
|
||||
# Empty database:
|
||||
su - postgres
|
||||
psql -U postgres risotto
|
||||
drop table log; drop table userrole; drop table release; drop table source; drop table server; drop table servermodel; drop table applicationservice; drop table roleuri; drop table risottouser; drop table uri;
|
||||
|
||||
# Import EOLE
|
||||
./script/cucchiaiata source.create -n eole -u http://localhost
|
||||
./script/cucchiaiata source.release.create -s eole -n 2.7.1.1 -d last
|
||||
./script/cucchiaiata applicationservice.dataset.updated -s eole -r last
|
||||
./script/cucchiaiata servermodel.dataset.updated -s eole -r last
|
||||
|
||||
# Create a server
|
||||
./script/cucchiaiata server.create -s test -d description -m eolebase -n eole -r last
|
||||
|
||||
# Configure the server
|
||||
./script/cucchiaiata session.server.start -s test
|
||||
S=xxxxxxxxxxxxxxxxxxxxxx
|
||||
./script/cucchiaiata session.server.configure -s $S --creole.reseau.unbound_ip_address_cidr 192.168.1.1/24 --creole.reseau.unbound_route_address 192.168.1.2 --creole.serveur_dns.unbound_allowed_client_cidr 192.168.1.0/24 --creole.serveur_dns.unbound_local_zones cadoles.com
|
||||
./script/cucchiaiata session.server.configure -s $S --creole.reseau.unbound_domain_name test.cadoles.com
|
||||
./script/cucchiaiata session.server.filter -s $S -n unbound
|
||||
./script/cucchiaiata session.server.configure -s $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
|
||||
./script/cucchiaiata session.server.validate -s $S
|
||||
./script/cucchiaiata session.server.stop -s $S -a
|
||||
|
||||
# Generate configuration
|
||||
./script/cucchiaiata config.configuration.server.deploy -s test
|
||||
./script/cucchiaiata template.generate -s test
|
||||
|
||||
# Create a new user and set role 'server_rw' for this server
|
||||
./script/cucchiaiata user.create -l gnunux -n gnunux -s gnunux
|
||||
./script/cucchiaiata user.role.create -u gnunux -n 'server_rw' -a 'Server.ServerName' -v test
|
||||
|
29
docker/Dockerfile
Normal file
29
docker/Dockerfile
Normal file
@ -0,0 +1,29 @@
|
||||
FROM python:3.7
|
||||
|
||||
# Requirements
|
||||
ARG TIRAMISU_REPO_URL=https://framagit.org/tiramisu/tiramisu.git
|
||||
ARG RISOTTO_REPO_URL=https://forge.cadoles.com/Infra/risotto.git
|
||||
ARG ROUGAIL_REPO_URL=https://forge.cadoles.com/Infra/rougail.git
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
vim \
|
||||
curl \
|
||||
git \
|
||||
jq \
|
||||
&& apt-get clean
|
||||
|
||||
RUN git clone --branch develop ${TIRAMISU_REPO_URL} /srv/src/tiramisu
|
||||
RUN git clone --branch docker ${RISOTTO_REPO_URL} /srv/src/risotto
|
||||
RUN git clone --branch master ${ROUGAIL_REPO_URL} /srv/src/rougail
|
||||
|
||||
RUN ln -s /srv/src/tiramisu/tiramisu /usr/local/lib/python3.7
|
||||
RUN ln -s /srv/src/rougail/src/rougail /usr/local/lib/python3.7
|
||||
RUN ln -s /srv/src/risotto/src/risotto /usr/local/lib/python3.7
|
||||
|
||||
RUN pip install Cheetah3 PyJWT
|
||||
RUN cd /srv/src/risotto && pip install -r requirements.txt
|
||||
|
||||
# Installation
|
||||
RUN cp -r /srv/src/risotto/messages/ /usr/local/lib/
|
||||
RUN mkdir -p /var/cache/risotto/servermodel
|
||||
RUN mkdir -p /var/cache/risotto/database
|
14
docker/README.md
Normal file
14
docker/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
Docker
|
||||
```
|
||||
cd docker
|
||||
docker build -t cadoles/risotto .
|
||||
docker run -t -d --name risotto cadoles/risotto
|
||||
docker exec -ti risotto bash
|
||||
|
||||
```
|
||||
|
||||
Docker-Compose
|
||||
```
|
||||
cd docker
|
||||
docker-compose up
|
||||
```
|
29
docker/docker-compose.yaml
Normal file
29
docker/docker-compose.yaml
Normal file
@ -0,0 +1,29 @@
|
||||
version: '2.2'
|
||||
services:
|
||||
risotto:
|
||||
build:
|
||||
context: ../
|
||||
dockerfile: docker/Dockerfile
|
||||
volumes:
|
||||
- ../.:/srv/src/risotto
|
||||
- ../messages:/usr/local/lib/messages
|
||||
ports:
|
||||
- "8080:8080"
|
||||
depends_on:
|
||||
- postgres
|
||||
links:
|
||||
- postgres
|
||||
#command: tail -F /var/log
|
||||
command: python -u /srv/src/risotto/script/server.py
|
||||
restart: on-failure
|
||||
postgres:
|
||||
image: postgres:11-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
|
||||
PGDATA: /data/postgres
|
||||
volumes:
|
||||
- ./postgres-init/:/docker-entrypoint-initdb.d/
|
||||
ports:
|
||||
- "5432:5432"
|
||||
restart: unless-stopped
|
105
docker/postgres-init/10-postgres.init.sh
Executable file
105
docker/postgres-init/10-postgres.init.sh
Executable file
@ -0,0 +1,105 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
psql --username "$POSTGRES_USER" <<-EOSQL
|
||||
CREATE ROLE risotto WITH LOGIN PASSWORD 'risotto';
|
||||
CREATE DATABASE risotto;
|
||||
GRANT ALL ON DATABASE risotto TO risotto;
|
||||
\c risotto
|
||||
CREATE EXTENSION hstore;
|
||||
CREATE EXTENSION pgcrypto;
|
||||
EOSQL
|
||||
|
||||
psql --username "risotto" --password "risotto" <<-EOSQL
|
||||
-- Création de la table Source
|
||||
CREATE TABLE Source (
|
||||
SourceId SERIAL PRIMARY KEY,
|
||||
SourceName VARCHAR(255) NOT NULL UNIQUE,
|
||||
SourceURL TEXT
|
||||
);
|
||||
|
||||
-- Création de la table Release
|
||||
CREATE TABLE Release (
|
||||
ReleaseId SERIAL PRIMARY KEY,
|
||||
ReleaseName VARCHAR(255) NOT NULL,
|
||||
ReleaseSourceId INTEGER NOT NULL,
|
||||
ReleaseDistribution VARCHAR(20) CONSTRAINT releasedistribution_choice CHECK (ReleaseDistribution IN ('last', 'n-1', 'n-2')),
|
||||
UNIQUE (ReleaseName, ReleaseSourceId),
|
||||
UNIQUE (ReleaseDistribution, ReleaseSourceId),
|
||||
FOREIGN KEY (ReleaseSourceId) REFERENCES Source(SourceId)
|
||||
);
|
||||
|
||||
|
||||
-- Création de la table Servermodel
|
||||
CREATE TABLE Servermodel (
|
||||
ServermodelId SERIAL PRIMARY KEY,
|
||||
ServermodelName VARCHAR(255) NOT NULL,
|
||||
ServermodelDescription VARCHAR(255) NOT NULL,
|
||||
ServermodelParentsId INTEGER [] DEFAULT '{}',
|
||||
ServermodelReleaseId INTEGER NOT NULL,
|
||||
ServermodelApplicationServiceId INTEGER NOT NULL,
|
||||
ServermodelUsers hstore,
|
||||
UNIQUE (ServermodelName, ServermodelReleaseId)
|
||||
);
|
||||
|
||||
-- Création de la table ApplicationService
|
||||
CREATE TABLE ApplicationService (
|
||||
ApplicationServiceId SERIAL PRIMARY KEY,
|
||||
ApplicationServiceName VARCHAR(255) NOT NULL,
|
||||
ApplicationServiceDescription VARCHAR(255) NOT NULL,
|
||||
ApplicationServiceReleaseId INTEGER NOT NULL,
|
||||
ApplicationServiceDependencies JSON,
|
||||
UNIQUE (ApplicationServiceName, ApplicationServiceReleaseId)
|
||||
);
|
||||
|
||||
-- Server table creation
|
||||
CREATE TABLE Server (
|
||||
ServerId SERIAL PRIMARY KEY,
|
||||
ServerName VARCHAR(255) NOT NULL UNIQUE,
|
||||
ServerDescription VARCHAR(255) NOT NULL,
|
||||
ServerServermodelId INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- User, Role and ACL table creation
|
||||
|
||||
CREATE TABLE RisottoUser (
|
||||
UserId SERIAL PRIMARY KEY,
|
||||
UserLogin VARCHAR(100) NOT NULL UNIQUE,
|
||||
UserPassword TEXT NOT NULL,
|
||||
UserName VARCHAR(100) NOT NULL,
|
||||
UserSurname VARCHAR(100) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE UserRole (
|
||||
RoleId SERIAL PRIMARY KEY,
|
||||
RoleUserId INTEGER NOT NULL,
|
||||
RoleName VARCHAR(255) NOT NULL,
|
||||
RoleAttribute VARCHAR(255),
|
||||
RoleAttributeValue VARCHAR(255),
|
||||
FOREIGN KEY (RoleUserId) REFERENCES RisottoUser(UserId)
|
||||
);
|
||||
|
||||
CREATE TABLE URI (
|
||||
URIId SERIAL PRIMARY KEY,
|
||||
URIName VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE RoleURI (
|
||||
RoleName VARCHAR(255) NOT NULL,
|
||||
URIId INTEGER NOT NULL,
|
||||
FOREIGN KEY (URIId) REFERENCES URI(URIId),
|
||||
PRIMARY KEY (RoleName, URIId)
|
||||
);
|
||||
|
||||
-- Log table creation
|
||||
|
||||
CREATE TABLE log(
|
||||
Msg VARCHAR(255) NOT NULL,
|
||||
Level VARCHAR(10) NOT NULL,
|
||||
Path VARCHAR(255),
|
||||
Username VARCHAR(100) NOT NULL,
|
||||
Data JSON,
|
||||
Date timestamp DEFAULT current_timestamp
|
||||
);
|
||||
|
||||
EOSQL
|
@ -1,13 +1,17 @@
|
||||
---
|
||||
uri: config.configuration.server.deploy
|
||||
|
||||
description: |
|
||||
Déployer la configuration d'un serveur.
|
||||
description: Déployer la configuration d'un serveur.
|
||||
|
||||
pattern: event
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
server_id:
|
||||
type: Number
|
||||
description: |
|
||||
Identifiant du serveur.
|
||||
server_name:
|
||||
type: String
|
||||
ref: Server.ServerName
|
||||
shortarg: s
|
||||
description: Nom du serveur.
|
||||
|
||||
response:
|
||||
type: Deploy
|
||||
description: La configuration du serveur est déployée.
|
||||
|
@ -7,11 +7,10 @@ description: |
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
server_id:
|
||||
type: Number
|
||||
ref: Server.ServerId
|
||||
description: |
|
||||
Identifiant du serveur.
|
||||
server_name:
|
||||
type: String
|
||||
ref: Server.ServerName
|
||||
description: Nom du serveur.
|
||||
deployed:
|
||||
type: Boolean
|
||||
description: Configuration de type déployée.
|
||||
|
@ -11,6 +11,11 @@ parameters:
|
||||
type: Number
|
||||
description: |
|
||||
Identifiant du serveur.
|
||||
server_name:
|
||||
type: String
|
||||
ref: Server.ServerName
|
||||
shortarg: s
|
||||
description: Nom du serveur.
|
||||
deployed:
|
||||
type: Boolean
|
||||
description: Configuration de type déployée.
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne des informations sur la session HTTP courante de l'utilisateur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client identity.session-user.get
|
||||
cucchiaiata identity.session-user.get
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne les préférences de l'utilisateur spécifié.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client identity.settings.get -u yo
|
||||
cucchiaiata identity.settings.get -u yo
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Enregistre des préférences pour l'utilisateur spécifié.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client identity.settings.set -u yo
|
||||
cucchiaiata identity.settings.set -u yo
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -11,7 +11,7 @@ pattern: rpc
|
||||
domain: server-domain
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.delete -s 1
|
||||
cucchiaiata server.delete -s 1
|
||||
|
||||
parameters:
|
||||
serverid:
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Transmet une commande à exécuter sur un serveur donné.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.exec.command -s 1 -c reconfigure
|
||||
cucchiaiata server.exec.command -s 1 -c reconfigure
|
||||
|
||||
domain: server-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Déploie la configuration sur un serveur donné.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.exec.deploy -s 1
|
||||
cucchiaiata server.exec.deploy -s 1
|
||||
|
||||
domain: server-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Liste les commandes exécuté pour un identifiant de tâche.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.exec.list -j 1
|
||||
cucchiaiata server.exec.list -j 1
|
||||
|
||||
domain: execution-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Liste les commandes exécutées sur un serveur donné.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.exec.list -s 1
|
||||
cucchiaiata server.exec.list -s 1
|
||||
|
||||
domain: execution-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne la liste des sélections de serveurs d'un serveur
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.serverselection.list
|
||||
cucchiaiata server.serverselection.list
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -7,7 +7,7 @@ description: |
|
||||
public: true
|
||||
|
||||
sampleuse: |
|
||||
zephir-client server.update -s 1 -n toto -d "server description"
|
||||
cucchiaiata server.update -s 1 -n toto -d "server description"
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Associe un service applicatif à un modèle de serveur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client servermodel.applicationservice.join -m 1 -s 1
|
||||
cucchiaiata servermodel.applicationservice.join -m 1 -s 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne la liste des applications service.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client servermodel.applicationservice.list -s 6
|
||||
cucchiaiata servermodel.applicationservice.list -s 6
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Crée un modèle de serveur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client servermodel.create -p 1 -n "MonServeurModele" -d "Ma description" -s 1
|
||||
cucchiaiata servermodel.create -p 1 -n "MonServeurModele" -d "Ma description" -s 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -1,32 +0,0 @@
|
||||
---
|
||||
uri: servermodel.describe
|
||||
|
||||
description: |
|
||||
Retourne les attributs détaillés d'un modèle de serveur.
|
||||
|
||||
pattern: rpc
|
||||
|
||||
public: true
|
||||
|
||||
parameters:
|
||||
servermodel_id:
|
||||
type: Number
|
||||
shortarg: s
|
||||
description: Identifiant du modèle de serveur à récupérer.
|
||||
ref: Servermodel.ServermodelId
|
||||
|
||||
response:
|
||||
type: Servermodel
|
||||
description: Description du modèle de serveur.
|
||||
|
||||
errors:
|
||||
- uri: servermodel.describe.error.database_not_available
|
||||
- uri: servermodel.describe.error.invalid_servermodel_id
|
||||
- uri: servermodel.describe.error.unknown_servermodel_id
|
||||
|
||||
related:
|
||||
- servermodel.list
|
||||
- servermodel.create
|
||||
- servermodel.update
|
||||
- servermodel.delete
|
||||
- servermodel.event
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne la liste des subreleases.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client servermodel.subrelease.list
|
||||
cucchiaiata servermodel.subrelease.list
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Crée un sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.create -n Select1 -d "Ma description"
|
||||
cucchiaiata serverselection.create -n Select1 -d "Ma description"
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Supprime une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.delete -s 1
|
||||
cucchiaiata serverselection.delete -s 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -6,7 +6,7 @@ description: |
|
||||
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.describe -s 1
|
||||
cucchiaiata serverselection.describe -s 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Transmet une commande à exécuter sur une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.exec.command -s 1 -c reconfigure
|
||||
cucchiaiata serverselection.exec.command -s 1 -c reconfigure
|
||||
|
||||
domain: server-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Déploie la configuration sur les serveurs d'une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.exec.deploy -s 1
|
||||
cucchiaiata serverselection.exec.deploy -s 1
|
||||
|
||||
domain: server-domain
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne la liste des sélections de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.list
|
||||
cucchiaiata serverselection.list
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Ajoute un serveur à une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.add.server -s 1 -i 1
|
||||
cucchiaiata serverselection.add.server -s 1 -i 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Supprime un serveur d'une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.remove.server -s 1 -i 1
|
||||
cucchiaiata serverselection.remove.server -s 1 -i 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Renseigne une liste de serveur dans une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.server.set -s 1 -i 1
|
||||
cucchiaiata serverselection.server.set -s 1 -i 1
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Modifie une sélection de serveur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.update -s 1 -n Select1 -d "Ma description"
|
||||
cucchiaiata serverselection.update -s 1 -n Select1 -d "Ma description"
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Associe un utilisateur à une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.add.user -s 1 -u yo -r admin
|
||||
cucchiaiata serverselection.add.user -s 1 -u yo -r admin
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne la sélection de serveurs par défaut de l'utilisateur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.user.default
|
||||
cucchiaiata serverselection.user.default
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne les sélections de serveurs dont l'utilisateur fait parti.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.user.list
|
||||
cucchiaiata serverselection.user.list
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Dissocie un utilisateur d'une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.remove.user -s 1 -u yo
|
||||
cucchiaiata serverselection.remove.user -s 1 -u yo
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne le rôle d'utilisateur sur une selection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.user.role.get -d '{}'
|
||||
cucchiaiata serverselection.user.role.get -d '{}'
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Retourne le rôle d'un utlisateur sur un serveur.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.user.role.server.get -d '{}'
|
||||
cucchiaiata serverselection.user.role.server.get -d '{}'
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -5,7 +5,7 @@ description: |
|
||||
Modifie le rôle d'un utilisateur pour une sélection de serveurs.
|
||||
|
||||
sampleuse: |
|
||||
zephir-client serverselection.update.user -s 1 -u yo -r admin
|
||||
cucchiaiata serverselection.update.user -s 1 -u yo -r admin
|
||||
|
||||
pattern: rpc
|
||||
|
||||
|
@ -14,11 +14,21 @@ parameters:
|
||||
type: String
|
||||
shortarg: d
|
||||
description: Description du serveur.
|
||||
server_servermodel_id:
|
||||
type: Number
|
||||
servermodel_name:
|
||||
type: String
|
||||
shortarg: m
|
||||
ref: Servermodel.ServermodelId
|
||||
description: Identifiant du modèle de serveur.
|
||||
ref: Servermodel.ServermodelName
|
||||
description: Nom du modèle de serveur.
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: n
|
||||
ref: Source.SourceName
|
||||
description: Nom de la source.
|
||||
release_distribution:
|
||||
type: String
|
||||
shortarg: r
|
||||
ref: Source.ReleaseDistribution
|
||||
description: Nom de la sous-version.
|
||||
|
||||
response:
|
||||
type: Server
|
||||
|
@ -8,6 +8,7 @@ pattern: rpc
|
||||
parameters:
|
||||
server_name:
|
||||
type: String
|
||||
shortarg: s
|
||||
ref: Server.ServerName
|
||||
description: Nom du serveur.
|
||||
|
||||
|
28
messages/v1/messages/servermodel.describe.yml
Normal file
28
messages/v1/messages/servermodel.describe.yml
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
uri: servermodel.describe
|
||||
|
||||
description: |
|
||||
Retourne les attributs détaillés d'un modèle de serveur.
|
||||
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
servermodel_name:
|
||||
type: String
|
||||
shortarg: s
|
||||
description: Identifiant du modèle de serveur à récupérer.
|
||||
ref: Servermodel.ServermodelId
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: n
|
||||
description: Nom de la source.
|
||||
ref: Source.SourceName
|
||||
release_distribution:
|
||||
type: String
|
||||
shortarg: r
|
||||
description: Nom de la distribution.
|
||||
ref: Source.ReleaseDistribution
|
||||
|
||||
response:
|
||||
type: Servermodel
|
||||
description: Description du modèle de serveur.
|
@ -7,12 +7,11 @@ description: |
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
id:
|
||||
type: Number
|
||||
ref: Server.ServerId
|
||||
shortarg: c
|
||||
description: |
|
||||
Identifiant de la configuration.
|
||||
server_name:
|
||||
type: String
|
||||
ref: Server.ServerName
|
||||
shortarg: s
|
||||
description: Nom du serveur.
|
||||
|
||||
response:
|
||||
type: Session
|
||||
|
@ -6,12 +6,21 @@ description: |
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
id:
|
||||
type: Number
|
||||
ref: Servermodel.ServermodelId
|
||||
shortarg: c
|
||||
description: |
|
||||
Identifiant de la configuration.
|
||||
servermodel_name:
|
||||
type: String
|
||||
ref: Servermodel.ServermodelName
|
||||
shortarg: s
|
||||
description: Nom du serveurmodel.
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: n
|
||||
description: Nom de la source.
|
||||
ref: Source.SourceName
|
||||
release_distribution:
|
||||
type: String
|
||||
shortarg: r
|
||||
description: Nom de la distribution.
|
||||
ref: Source.ReleaseDistribution
|
||||
|
||||
response:
|
||||
type: Session
|
||||
|
@ -7,10 +7,10 @@ description: |
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
source_id:
|
||||
type: Number
|
||||
shortarg: i
|
||||
description: ID de la source.
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: s
|
||||
description: Nom de la source.
|
||||
release_name:
|
||||
type: String
|
||||
shortarg: n
|
||||
|
21
messages/v1/messages/source.release.describe.yml
Normal file
21
messages/v1/messages/source.release.describe.yml
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
uri: source.release.describe
|
||||
|
||||
description: |
|
||||
Retourne la sous-version.
|
||||
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: s
|
||||
description: Nom de la source.
|
||||
release_distribution:
|
||||
type: String
|
||||
shortarg: r
|
||||
description: Nom de la sous-version
|
||||
|
||||
response:
|
||||
type: 'Release'
|
||||
description: Sous-version.
|
@ -6,6 +6,12 @@ description: |
|
||||
|
||||
pattern: rpc
|
||||
|
||||
parameters:
|
||||
source_name:
|
||||
type: String
|
||||
shortarg: s
|
||||
description: Nom de la source.
|
||||
|
||||
response:
|
||||
type: '[]Release'
|
||||
description: Liste des versions disponibles.
|
||||
|
@ -11,8 +11,7 @@ parameters:
|
||||
type: String
|
||||
ref: Server.ServerName
|
||||
shortarg: s
|
||||
description: |
|
||||
Nom du serveur.
|
||||
description: Nom du serveur.
|
||||
|
||||
response:
|
||||
type: Template
|
||||
|
@ -11,6 +11,10 @@ parameters:
|
||||
shortarg: l
|
||||
description: Login de l'utilisateur.
|
||||
ref: User.Login
|
||||
user_password:
|
||||
type: String
|
||||
shortarg: p
|
||||
description: Password de l'utilisateur.
|
||||
user_name:
|
||||
type: String
|
||||
shortarg: n
|
||||
|
@ -8,7 +8,7 @@ pattern: rpc
|
||||
parameters:
|
||||
user_login:
|
||||
type: String
|
||||
shortarg: l
|
||||
shortarg: u
|
||||
description: Login de l'utilisateur.
|
||||
ref: User.UserLogin
|
||||
role_name:
|
||||
|
@ -3,10 +3,10 @@ title: ConfigConfiguration
|
||||
type: object
|
||||
description: Description de la configuration.
|
||||
properties:
|
||||
server_id:
|
||||
type: number
|
||||
description: Identifiant du serveur.
|
||||
ref: Server.ServerId
|
||||
server_name:
|
||||
type: string
|
||||
description: Nom du serveur.
|
||||
ref: Server.ServerName
|
||||
deployed:
|
||||
type: boolean
|
||||
description: La configuration est déployée.
|
||||
|
20
messages/v1/types/deploy.yml
Normal file
20
messages/v1/types/deploy.yml
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
title: Deploy
|
||||
type: object
|
||||
description: État de déploiement de la configuration.
|
||||
properties:
|
||||
server_id:
|
||||
type: number
|
||||
description: ID du serveur.
|
||||
ref: Server.ServerID
|
||||
server_name:
|
||||
type: string
|
||||
ref: Server.ServerName
|
||||
description: Nom du server.
|
||||
deployed:
|
||||
type: boolean
|
||||
description: État de déploiement.
|
||||
required:
|
||||
- server_id
|
||||
- server_name
|
||||
- deployed
|
@ -3,12 +3,13 @@ title: Template
|
||||
type: object
|
||||
description: Les fichiers de configuration générés.
|
||||
properties:
|
||||
server_id:
|
||||
type: Number
|
||||
description: Identifiant du serveur.
|
||||
server_name:
|
||||
type: String
|
||||
description: Nom du serveur.
|
||||
ref: Server.ServerName
|
||||
template_dir:
|
||||
type: String
|
||||
description: Nom du répertoire avec les fichiers de configuration générés.
|
||||
required:
|
||||
- server_id
|
||||
- server_name
|
||||
- template_dir
|
||||
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -0,0 +1,7 @@
|
||||
asyncpg==0.20.1
|
||||
lxml==4.4.2
|
||||
requests==2.22.0
|
||||
aiohttp==3.6.2
|
||||
pytest==5.3.3
|
||||
PyYAML==5.3
|
||||
tiramisu==3.0rc15
|
@ -57,6 +57,7 @@ CREATE TABLE Server (
|
||||
CREATE TABLE RisottoUser (
|
||||
UserId SERIAL PRIMARY KEY,
|
||||
UserLogin VARCHAR(100) NOT NULL UNIQUE,
|
||||
UserPassword TEXT NOT NULL,
|
||||
UserName VARCHAR(100) NOT NULL,
|
||||
UserSurname VARCHAR(100) NOT NULL
|
||||
);
|
||||
@ -82,11 +83,30 @@ CREATE TABLE RoleURI (
|
||||
PRIMARY KEY (RoleName, URIId)
|
||||
);
|
||||
|
||||
-- Log table creation
|
||||
|
||||
CREATE TABLE log(
|
||||
Msg VARCHAR(255) NOT NULL,
|
||||
Level VARCHAR(10) NOT NULL,
|
||||
Path VARCHAR(255),
|
||||
Username VARCHAR(100) NOT NULL,
|
||||
Data JSON,
|
||||
Date timestamp DEFAULT current_timestamp
|
||||
);
|
||||
|
||||
"""
|
||||
|
||||
async def main():
|
||||
db_conf = get_config().get('database')
|
||||
pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user'))
|
||||
#asyncpg.connect('postgresql://postgres@localhost/test')
|
||||
engine = db_conf.get('engine')
|
||||
host = db_conf.get('host')
|
||||
dbname = db_conf.get('dbname')
|
||||
dbuser = db_conf.get('user')
|
||||
dbpassword = db_conf.get('password')
|
||||
dbport = db_conf.get('port')
|
||||
cfg = "{}://{}:{}@{}:{}/{}".format(engine, dbuser, dbpassword, host, dbport, dbname)
|
||||
pool = await asyncpg.create_pool(cfg)
|
||||
async with pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
returns = await connection.execute(VERSION_INIT)
|
||||
@ -95,3 +115,4 @@ if __name__ == '__main__':
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(main())
|
||||
# asyncio.run(main())
|
||||
|
||||
|
@ -1,32 +1,45 @@
|
||||
HTTP_PORT = 8080
|
||||
MESSAGE_ROOT_PATH = 'messages'
|
||||
DEBUG = True
|
||||
DATABASE_DIR = 'database'
|
||||
DATABASE_DIR = '/var/cache/risotto/database'
|
||||
INTERNAL_USER = 'internal'
|
||||
CONFIGURATION_DIR = 'configurations'
|
||||
TEMPLATE_DIR = 'templates'
|
||||
TMP_DIR = 'tmp'
|
||||
ROUGAIL_DTD_PATH = '../rougail/data/creole.dtd'
|
||||
POSTGRESQL_ADDRESS = '192.168.56.106'
|
||||
POSTGRESQL_PORT = 5432
|
||||
DEFAULT_USER = 'gnunux'
|
||||
DEFAULT_USER_PASSWORD = 'gnunux'
|
||||
URI = 'http://localhost'
|
||||
PORT = 8080
|
||||
JWT_SECRET = 'MY_SUPER_SECRET'
|
||||
JWT_TOKEN_EXPIRE = 3600
|
||||
JWT_TOKEN_AUDIENCE = "Risotto"
|
||||
|
||||
import os
|
||||
from pathlib import PurePosixPath
|
||||
CURRENT_PATH = PurePosixPath(__file__)
|
||||
|
||||
def get_config():
|
||||
return {'database': {'host': 'localhost',
|
||||
return {'database': {'engine': 'postgres',
|
||||
'host': 'postgres',
|
||||
'port': 5432,
|
||||
'dbname': 'risotto',
|
||||
'user': 'risotto',
|
||||
'password': 'risotto',
|
||||
},
|
||||
'http_server': {'port': 8080},
|
||||
'http_server': {'port': PORT,
|
||||
'default_user': DEFAULT_USER},
|
||||
'global': {'message_root_path': CURRENT_PATH.parents[2] / 'messages',
|
||||
'debug': DEBUG,
|
||||
'debug': True,
|
||||
'internal_user': 'internal',
|
||||
'check_role': False,
|
||||
'rougail_dtd_path': '../rougail/data/creole.dtd'},
|
||||
'check_role': True,
|
||||
'rougail_dtd_path': '../rougail/data/creole.dtd',
|
||||
'admin_user': DEFAULT_USER,
|
||||
'admin_user_password': DEFAULT_USER_PASSWORD},
|
||||
'source': {'root_path': '/srv/seed'},
|
||||
'cache': {'root_path': '/var/cache/risotto'}
|
||||
'cache': {'root_path': '/var/cache/risotto'},
|
||||
'jwt': {
|
||||
'secret': JWT_SECRET,
|
||||
'token_expire': JWT_TOKEN_EXPIRE,
|
||||
'issuer': URI,
|
||||
'audience': JWT_TOKEN_AUDIENCE}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,12 @@
|
||||
from tiramisu import Config
|
||||
from traceback import print_exc
|
||||
from copy import copy
|
||||
from typing import Dict, Callable
|
||||
from typing import Dict, Callable, List, Optional
|
||||
from json import dumps, loads
|
||||
|
||||
from .utils import _
|
||||
from .error import CallError, NotAllowedError
|
||||
from .logger import log
|
||||
from .config import DEBUG
|
||||
from .config import get_config
|
||||
from .context import Context
|
||||
from . import register
|
||||
@ -17,21 +16,22 @@ import asyncpg
|
||||
class CallDispatcher:
|
||||
async def valid_call_returns(self,
|
||||
risotto_context: Context,
|
||||
function,
|
||||
returns: Dict,
|
||||
kwargs: Dict):
|
||||
response = self.messages[risotto_context.version][risotto_context.message]['response']
|
||||
module_name = risotto_context.function.__module__.split('.')[-2]
|
||||
function_name = risotto_context.function.__name__
|
||||
module_name = function.__module__.split('.')[-2]
|
||||
function_name = function.__name__
|
||||
if response.impl_get_information('multi'):
|
||||
if not isinstance(returns, list):
|
||||
err = _(f'function {module_name}.{function_name} has to return a list')
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
else:
|
||||
if not isinstance(returns, dict):
|
||||
log.error_msg(risotto_context, kwargs, returns)
|
||||
await log.error_msg(risotto_context, kwargs, returns)
|
||||
err = _(f'function {module_name}.{function_name} has to return a dict')
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
returns = [returns]
|
||||
if response is None:
|
||||
@ -46,11 +46,11 @@ class CallDispatcher:
|
||||
await config.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
err = _(f'function {module_name}.{function_name} return the unknown parameter "{key}"')
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
except ValueError:
|
||||
err = _(f'function {module_name}.{function_name} return the parameter "{key}" with an unvalid value "{value}"')
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
await config.property.read_only()
|
||||
mandatories = await config.value.mandatory()
|
||||
@ -61,7 +61,7 @@ class CallDispatcher:
|
||||
await config.value.dict()
|
||||
except Exception as err:
|
||||
err = _(f'function {module_name}.{function_name} return an invalid response {err}')
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
|
||||
async def call(self,
|
||||
@ -77,21 +77,17 @@ class CallDispatcher:
|
||||
version,
|
||||
message,
|
||||
'rpc')
|
||||
self.check_message_type(risotto_context,
|
||||
kwargs)
|
||||
try:
|
||||
kw = await self.load_kwargs_to_config(risotto_context,
|
||||
f'{version}.{message}',
|
||||
kwargs,
|
||||
check_role)
|
||||
function_obj = self.messages[version][message]
|
||||
risotto_context.function = function_obj['function']
|
||||
if function_obj['risotto_context']:
|
||||
kw['risotto_context'] = risotto_context
|
||||
function_objs = [self.messages[version][message]]
|
||||
# do not start a new database connection
|
||||
if function_obj['database'] and hasattr(old_risotto_context, 'connection'):
|
||||
if hasattr(old_risotto_context, 'connection'):
|
||||
risotto_context.connection = old_risotto_context.connection
|
||||
if function_obj['database'] and not hasattr(risotto_context, 'connection'):
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
else:
|
||||
async with self.pool.acquire() as connection:
|
||||
await connection.set_type_codec(
|
||||
'json',
|
||||
@ -101,39 +97,12 @@ class CallDispatcher:
|
||||
)
|
||||
risotto_context.connection = connection
|
||||
async with connection.transaction():
|
||||
returns = await risotto_context.function(self.injected_self[function_obj['module']], **kw)
|
||||
else:
|
||||
returns = await risotto_context.function(self.injected_self[function_obj['module']], **kw)
|
||||
except CallError as err:
|
||||
raise err
|
||||
except Exception as err:
|
||||
if get_config().get('global').get('debug'):
|
||||
print_exc()
|
||||
log.error_msg(risotto_context,
|
||||
kwargs,
|
||||
err)
|
||||
raise CallError(str(err))
|
||||
# valid returns
|
||||
await self.valid_call_returns(risotto_context,
|
||||
returns,
|
||||
kwargs)
|
||||
# log the success
|
||||
log.info_msg(risotto_context,
|
||||
kwargs,
|
||||
_(f'returns {returns}'))
|
||||
# notification
|
||||
if function_obj.get('notification'):
|
||||
notif_version, notif_message = function_obj['notification'].split('.', 1)
|
||||
if not isinstance(returns, list):
|
||||
send_returns = [returns]
|
||||
else:
|
||||
send_returns = returns
|
||||
for ret in send_returns:
|
||||
await self.publish(notif_version,
|
||||
notif_message,
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
**ret)
|
||||
return returns
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
|
||||
|
||||
class PublishDispatcher:
|
||||
@ -147,38 +116,17 @@ class PublishDispatcher:
|
||||
version,
|
||||
message,
|
||||
'event')
|
||||
self.check_message_type(risotto_context,
|
||||
kwargs)
|
||||
try:
|
||||
config_arguments = await self.load_kwargs_to_config(risotto_context,
|
||||
kwargs)
|
||||
except CallError as err:
|
||||
return
|
||||
except Exception as err:
|
||||
# if there is a problem with arguments, just send an error et do nothing
|
||||
if DEBUG:
|
||||
print_exc()
|
||||
log.error_msg(risotto_context, kwargs, err)
|
||||
return
|
||||
|
||||
# config is ok, so publish the message
|
||||
for function_obj in self.messages[version][message].get('functions', []):
|
||||
function = function_obj['function']
|
||||
module_name = function.__module__.split('.')[-2]
|
||||
function_name = function.__name__
|
||||
info_msg = _(f'in module {module_name}.{function_name}')
|
||||
try:
|
||||
# build argument for this function
|
||||
kw = {}
|
||||
for key, value in config_arguments.items():
|
||||
if key in function_obj['arguments']:
|
||||
kw[key] = value
|
||||
if function_obj['risotto_context']:
|
||||
kw['risotto_context'] = risotto_context
|
||||
# send event
|
||||
if function_obj['database'] and hasattr(old_risotto_context, 'connection'):
|
||||
function_objs = self.messages[version][message].get('functions', [])
|
||||
# do not start a new database connection
|
||||
if hasattr(old_risotto_context, 'connection'):
|
||||
risotto_context.connection = old_risotto_context.connection
|
||||
if function_obj['database'] and not hasattr(risotto_context, 'connection'):
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
else:
|
||||
async with self.pool.acquire() as connection:
|
||||
await connection.set_type_codec(
|
||||
'json',
|
||||
@ -188,23 +136,12 @@ class PublishDispatcher:
|
||||
)
|
||||
risotto_context.connection = connection
|
||||
async with connection.transaction():
|
||||
returns = await function(self.injected_self[function_obj['module']], **kw)
|
||||
else:
|
||||
returns = await function(self.injected_self[function_obj['module']], **kw)
|
||||
except Exception as err:
|
||||
if DEBUG:
|
||||
print_exc()
|
||||
log.error_msg(risotto_context, kwargs, err, info_msg)
|
||||
continue
|
||||
else:
|
||||
log.info_msg(risotto_context, kwargs, info_msg)
|
||||
# notification
|
||||
if function_obj.get('notification'):
|
||||
notif_version, notif_message = function_obj['notification'].split('.', 1)
|
||||
await self.publish(notif_version,
|
||||
notif_message,
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
**returns)
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
|
||||
|
||||
class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher):
|
||||
@ -229,12 +166,12 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
risotto_context.version = version
|
||||
return risotto_context
|
||||
|
||||
def check_message_type(self,
|
||||
async def check_message_type(self,
|
||||
risotto_context: Context,
|
||||
kwargs: Dict):
|
||||
if self.messages[risotto_context.version][risotto_context.message]['pattern'] != risotto_context.type:
|
||||
msg = _(f'{risotto_context.uri} is not a {risotto_context.type} message')
|
||||
log.error_msg(risotto_context, kwargs, msg)
|
||||
await log.error_msg(risotto_context, kwargs, msg)
|
||||
raise CallError(msg)
|
||||
|
||||
async def load_kwargs_to_config(self,
|
||||
@ -255,7 +192,7 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
try:
|
||||
await subconfig.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
if DEBUG:
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
raise ValueError(_(f'unknown parameter in "{uri}": "{key}"'))
|
||||
# check mandatories options
|
||||
@ -322,6 +259,88 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
return
|
||||
raise NotAllowedError(_(f'You ({user_login}) don\'t have any authorisation to access to "{uri}"'))
|
||||
|
||||
async def launch(self,
|
||||
version: str,
|
||||
message: str,
|
||||
risotto_context: Context,
|
||||
check_role: bool,
|
||||
kwargs: Dict,
|
||||
function_objs: List) -> Optional[Dict]:
|
||||
await self.check_message_type(risotto_context,
|
||||
kwargs)
|
||||
try:
|
||||
config_arguments = await self.load_kwargs_to_config(risotto_context,
|
||||
f'{version}.{message}',
|
||||
kwargs,
|
||||
check_role)
|
||||
except Exception as err:
|
||||
# if there is a problem with arguments, just send an error and do nothing
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
if risotto_context.type == 'rpc':
|
||||
raise err
|
||||
return
|
||||
# config is ok, so send the message
|
||||
for function_obj in function_objs:
|
||||
function = function_obj['function']
|
||||
module_name = function.__module__.split('.')[-2]
|
||||
function_name = function.__name__
|
||||
info_msg = _(f'in module {module_name}.{function_name}')
|
||||
try:
|
||||
# build argument for this function
|
||||
if risotto_context.type == 'rpc':
|
||||
kw = config_arguments
|
||||
else:
|
||||
kw = {}
|
||||
for key, value in config_arguments.items():
|
||||
if key in function_obj['arguments']:
|
||||
kw[key] = value
|
||||
|
||||
|
||||
|
||||
kw['risotto_context'] = risotto_context
|
||||
returns = await function(self.injected_self[function_obj['module']], **kw)
|
||||
except CallError as err:
|
||||
if risotto_context.type == 'rpc':
|
||||
raise err
|
||||
continue
|
||||
except Exception as err:
|
||||
if get_config().get('global').get('debug'):
|
||||
print_exc()
|
||||
await log.error_msg(risotto_context,
|
||||
kwargs,
|
||||
err)
|
||||
if risotto_context.type == 'rpc':
|
||||
raise CallError(str(err))
|
||||
continue
|
||||
else:
|
||||
if risotto_context.type == 'rpc':
|
||||
# valid returns
|
||||
await self.valid_call_returns(risotto_context,
|
||||
function,
|
||||
returns,
|
||||
kwargs)
|
||||
# log the success
|
||||
await log.info_msg(risotto_context,
|
||||
{'arguments': kwargs,
|
||||
'returns': returns},
|
||||
info_msg)
|
||||
# notification
|
||||
if function_obj.get('notification'):
|
||||
notif_version, notif_message = function_obj['notification'].split('.', 1)
|
||||
if not isinstance(returns, list):
|
||||
send_returns = [returns]
|
||||
else:
|
||||
send_returns = returns
|
||||
for ret in send_returns:
|
||||
await self.publish(notif_version,
|
||||
notif_message,
|
||||
risotto_context,
|
||||
**ret)
|
||||
if risotto_context.type == 'rpc':
|
||||
return returns
|
||||
|
||||
|
||||
dispatcher = Dispatcher()
|
||||
register.dispatcher = dispatcher
|
||||
|
@ -1,8 +1,10 @@
|
||||
from aiohttp.web import Application, Response, get, post, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound
|
||||
from aiohttp.web import Application, Response, get, post, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound, HTTPUnauthorized
|
||||
from aiohttp import BasicAuth, RequestInfo
|
||||
from json import dumps
|
||||
from traceback import print_exc
|
||||
from tiramisu import Config
|
||||
|
||||
import datetime
|
||||
import jwt
|
||||
|
||||
from .dispatcher import dispatcher
|
||||
from .utils import _
|
||||
@ -10,13 +12,24 @@ from .context import Context
|
||||
from .error import CallError, NotAllowedError, RegistrationError
|
||||
from .message import get_messages
|
||||
from .logger import log
|
||||
from .config import DEBUG, HTTP_PORT
|
||||
from .config import get_config
|
||||
from .services import load_services
|
||||
|
||||
|
||||
def create_context(request):
|
||||
risotto_context = Context()
|
||||
risotto_context.username = request.match_info.get('username', "Anonymous")
|
||||
if 'Authorization' in request.headers:
|
||||
token = request.headers['Authorization']
|
||||
if not token.startswith("Bearer "):
|
||||
raise HTTPBadRequest(reason='Unexpected bearer format')
|
||||
token = token[7:]
|
||||
decoded = verify_token(token)
|
||||
if 'user' in decoded:
|
||||
risotto_context.username = decoded['user']
|
||||
return risotto_context
|
||||
else:
|
||||
risotto_context.username = request.match_info.get('username',
|
||||
get_config()['http_server']['default_user'])
|
||||
return risotto_context
|
||||
|
||||
|
||||
@ -48,15 +61,15 @@ class extra_route_handler:
|
||||
try:
|
||||
returns = await cls.function(**kwargs)
|
||||
except NotAllowedError as err:
|
||||
raise HTTPNotFound(reason=str(err))
|
||||
raise HTTPUnauthorized(reason=str(err))
|
||||
except CallError as err:
|
||||
raise HTTPBadRequest(reason=str(err))
|
||||
except Exception as err:
|
||||
if DEBUG:
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
raise HTTPInternalServerError(reason=str(err))
|
||||
log.info_msg(kwargs['risotto_context'],
|
||||
dict(request.match_info))
|
||||
# await log.info_msg(kwargs['risotto_context'],
|
||||
# dict(request.match_info))
|
||||
return Response(text=dumps(returns))
|
||||
|
||||
|
||||
@ -76,11 +89,11 @@ async def handle(request):
|
||||
check_role=True,
|
||||
**kwargs)
|
||||
except NotAllowedError as err:
|
||||
raise HTTPNotFound(reason=str(err))
|
||||
raise HTTPUnauthorized(reason=str(err))
|
||||
except CallError as err:
|
||||
raise HTTPBadRequest(reason=str(err).replace('\n', ' '))
|
||||
except Exception as err:
|
||||
if DEBUG:
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
raise HTTPInternalServerError(reason=str(err))
|
||||
return Response(text=dumps({'response': text}))
|
||||
@ -89,7 +102,19 @@ async def handle(request):
|
||||
async def api(request, risotto_context):
|
||||
global tiramisu
|
||||
if not tiramisu:
|
||||
config = await Config(get_messages(load_shortarg=True)[1])
|
||||
# check all URI that have an associated role
|
||||
# all URI without role is concidered has a private URI
|
||||
uris = []
|
||||
async with dispatcher.pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
# Check role with ACL
|
||||
sql = '''
|
||||
SELECT URI.URIName
|
||||
FROM URI, RoleURI
|
||||
WHERE RoleURI.URIId = URI.URIId
|
||||
'''
|
||||
uris = [uri['uriname'] for uri in await connection.fetch(sql)]
|
||||
config = await Config(get_messages(load_shortarg=True, uris=uris)[1])
|
||||
await config.property.read_write()
|
||||
tiramisu = await config.option.dict(remotable='none')
|
||||
return tiramisu
|
||||
@ -128,8 +153,76 @@ async def get_app(loop):
|
||||
print()
|
||||
del extra_routes
|
||||
app.add_routes(routes)
|
||||
app.router.add_post('/auth', auth)
|
||||
app.router.add_post('/access_token', access_token)
|
||||
await dispatcher.on_join()
|
||||
return await loop.create_server(app.make_handler(), '*', HTTP_PORT)
|
||||
return await loop.create_server(app.make_handler(), '*', get_config()['http_server']['port'])
|
||||
|
||||
async def auth(request):
|
||||
auth_code = request.headers['Authorization']
|
||||
if not auth_code.startswith("Basic "):
|
||||
raise HTTPBadRequest(reason='Unexpected bearer format')
|
||||
auth = BasicAuth.decode(auth_code)
|
||||
async with dispatcher.pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
# Check role with ACL
|
||||
sql = '''
|
||||
SELECT UserName
|
||||
FROM RisottoUser
|
||||
WHERE UserLogin = $1
|
||||
AND UserPassword = crypt($2, UserPassword);
|
||||
'''
|
||||
res = await connection.fetch(sql, auth.login, auth.password)
|
||||
if res:
|
||||
res = gen_token(auth)
|
||||
if verify_token(res):
|
||||
return Response(text=str(res.decode('utf-8')))
|
||||
else:
|
||||
return HTTPInternalServerError(reason='Token could not be verified just after creation')
|
||||
else:
|
||||
raise HTTPUnauthorized(reason='Unauthorized')
|
||||
|
||||
def gen_token(auth):
|
||||
secret = get_config()['jwt']['secret']
|
||||
expire = get_config()['jwt']['token_expire']
|
||||
issuer = get_config()['jwt']['issuer']
|
||||
audience = get_config()['jwt']['audience']
|
||||
payload = {
|
||||
'user': auth.login,
|
||||
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=expire),
|
||||
'iss': issuer,
|
||||
'aud': audience
|
||||
}
|
||||
token = jwt.encode(payload, secret, algorithm='HS256')
|
||||
return token
|
||||
|
||||
def access_token(request):
|
||||
expire = get_config()['jwt']['token_expire']
|
||||
secret = get_config()['jwt']['secret']
|
||||
token = request.headers['Authorization']
|
||||
if not token.startswith("Bearer "):
|
||||
raise HTTPBadRequest(reason='Unexpected bearer format')
|
||||
token = token[7:]
|
||||
decoded = verify_token(token)
|
||||
if decoded:
|
||||
decoded['exp'] = datetime.datetime.utcnow() + datetime.timedelta(seconds=expire)
|
||||
token = jwt.encode(decoded, secret, algorithm='HS256')
|
||||
return Response(text=str(token.decode('utf-8')))
|
||||
else:
|
||||
return HTTPUnauthorized(reason='Token could not be verified')
|
||||
|
||||
def verify_token(token):
|
||||
secret = get_config()['jwt']['secret']
|
||||
issuer = get_config()['jwt']['issuer']
|
||||
audience = get_config()['jwt']['audience']
|
||||
try:
|
||||
decoded = jwt.decode(token, secret, issuer=issuer, audience=audience, algorithms=['HS256'])
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPUnauthorized(reason='Token Expired')
|
||||
except jwt.InvalidIssuerError:
|
||||
raise HTTPUnauthorized(reason='Token could not be verified')
|
||||
except jwt.InvalidAudienceError:
|
||||
raise HTTPUnauthorized(reason='Token audience not match')
|
||||
return decoded
|
||||
|
||||
tiramisu = None
|
||||
|
@ -1,13 +1,30 @@
|
||||
from typing import Dict
|
||||
from typing import Dict, Any
|
||||
from json import dumps
|
||||
from .context import Context
|
||||
from .utils import _
|
||||
from .config import DEBUG
|
||||
from .config import get_config
|
||||
|
||||
|
||||
class Logger:
|
||||
""" An object to manager log
|
||||
FIXME should add event to a database
|
||||
"""
|
||||
async def insert(self,
|
||||
msg: str,
|
||||
path: str,
|
||||
risotto_context: str,
|
||||
level: str,
|
||||
data: Any= None) -> None:
|
||||
insert = 'INSERT INTO log(Msg, Path, Username, Level'
|
||||
values = 'VALUES($1,$2,$3,$4'
|
||||
args = [msg, path, risotto_context.username, level]
|
||||
if data:
|
||||
insert += ', Data'
|
||||
values += ',$5'
|
||||
args.append(dumps(data))
|
||||
|
||||
sql = insert + ') ' + values + ')'
|
||||
await risotto_context.connection.fetch(sql, *args)
|
||||
|
||||
def _get_message_paths(self,
|
||||
risotto_context: Context):
|
||||
paths = risotto_context.paths
|
||||
@ -20,10 +37,9 @@ class Logger:
|
||||
else:
|
||||
paths_msg += f'sub-messages: '
|
||||
paths_msg += ' > '.join(paths)
|
||||
paths_msg += ':'
|
||||
return paths_msg
|
||||
|
||||
def error_msg(self,
|
||||
async def error_msg(self,
|
||||
risotto_context: Context,
|
||||
arguments,
|
||||
error: str,
|
||||
@ -31,10 +47,14 @@ class Logger:
|
||||
""" send message when an error append
|
||||
"""
|
||||
paths_msg = self._get_message_paths(risotto_context)
|
||||
# if DEBUG:
|
||||
print(_(f'{risotto_context.username}: ERROR: {error} ({paths_msg} with arguments "{arguments}": {msg})'))
|
||||
await self.insert(msg,
|
||||
paths_msg,
|
||||
risotto_context,
|
||||
'Error',
|
||||
arguments)
|
||||
|
||||
def info_msg(self,
|
||||
async def info_msg(self,
|
||||
risotto_context: Context,
|
||||
arguments: Dict,
|
||||
msg: str=''):
|
||||
@ -44,19 +64,23 @@ class Logger:
|
||||
paths_msg = self._get_message_paths(risotto_context)
|
||||
else:
|
||||
paths_msg = ''
|
||||
tmsg = _(f'{risotto_context.username}: INFO:{paths_msg}')
|
||||
if arguments:
|
||||
tmsg += _(f' with arguments "{arguments}"')
|
||||
if msg:
|
||||
tmsg += f' {msg}'
|
||||
if get_config()['global']['debug']:
|
||||
print(_(f'{risotto_context.username}: INFO:{paths_msg}'))
|
||||
await self.insert(msg,
|
||||
paths_msg,
|
||||
risotto_context,
|
||||
'Info',
|
||||
arguments)
|
||||
|
||||
if DEBUG:
|
||||
print(tmsg)
|
||||
|
||||
def info(self,
|
||||
async def info(self,
|
||||
risotto_context,
|
||||
msg):
|
||||
if DEBUG:
|
||||
if get_config()['global']['debug']:
|
||||
print(msg)
|
||||
await self.insert(msg,
|
||||
None,
|
||||
risotto_context,
|
||||
'Info')
|
||||
|
||||
|
||||
log = Logger()
|
||||
|
@ -38,7 +38,7 @@ class AnyOption(Option):
|
||||
|
||||
class MessageDefinition:
|
||||
"""
|
||||
A MessageDefinition is a representation of a message in the Zephir application messaging context
|
||||
A MessageDefinition is a representation of a message in the Risotto application messaging context
|
||||
"""
|
||||
__slots__ = ('version',
|
||||
'uri',
|
||||
@ -135,7 +135,7 @@ class ParameterDefinition:
|
||||
|
||||
class ResponseDefinition:
|
||||
"""
|
||||
An ResponseDefinition is a representation of a response in the Zephir application messaging context
|
||||
An ResponseDefinition is a representation of a response in the Risotto application messaging context
|
||||
"""
|
||||
__slots__ = ('description',
|
||||
'type',
|
||||
@ -184,7 +184,7 @@ class ResponseDefinition:
|
||||
|
||||
class ErrorDefinition:
|
||||
"""
|
||||
An ErrorDefinition is a representation of an error in the Zephir application messaging context
|
||||
An ErrorDefinition is a representation of an error in the Risotto application messaging context
|
||||
"""
|
||||
__slots__ = ('uri',)
|
||||
|
||||
@ -237,13 +237,16 @@ def split_message_uri(uri):
|
||||
def get_message_file_path(version, message):
|
||||
return join(MESSAGE_ROOT_PATH, version, 'messages', message + '.yml')
|
||||
|
||||
def list_messages():
|
||||
def list_messages(uris):
|
||||
messages = listdir(MESSAGE_ROOT_PATH)
|
||||
messages.sort()
|
||||
for version in messages:
|
||||
for message in listdir(join(MESSAGE_ROOT_PATH, version, 'messages')):
|
||||
if message.endswith('.yml'):
|
||||
yield version + '.' + message.rsplit('.', 1)[0]
|
||||
uri = version + '.' + message.rsplit('.', 1)[0]
|
||||
if uris is not None and uri not in uris:
|
||||
continue
|
||||
yield uri
|
||||
|
||||
class CustomParam:
|
||||
__slots__ = ('name',
|
||||
@ -573,14 +576,15 @@ def _get_root_option(select_option, optiondescriptions):
|
||||
return OptionDescription('root', 'root', options_obj)
|
||||
|
||||
|
||||
def get_messages(load_shortarg=False):
|
||||
def get_messages(load_shortarg=False,
|
||||
uris=None):
|
||||
"""generate description from yml files
|
||||
"""
|
||||
optiondescriptions = OrderedDict()
|
||||
optiondescriptions_name = []
|
||||
optiondescriptions_info = {}
|
||||
needs = OrderedDict()
|
||||
messages = list(list_messages())
|
||||
messages = list(list_messages(uris))
|
||||
messages.sort()
|
||||
for message_name in messages:
|
||||
message_def = get_message(message_name)
|
||||
|
@ -2,8 +2,9 @@ from tiramisu import Config
|
||||
from inspect import signature
|
||||
from typing import Callable, Optional
|
||||
import asyncpg
|
||||
from json import dumps, loads
|
||||
|
||||
from .utils import undefined, _
|
||||
from .utils import _
|
||||
from .error import RegistrationError
|
||||
from .message import get_messages
|
||||
from .context import Context
|
||||
@ -11,8 +12,7 @@ from .config import INTERNAL_USER, get_config
|
||||
|
||||
|
||||
def register(uris: str,
|
||||
notification: str=undefined,
|
||||
database: bool=False):
|
||||
notification: str=None):
|
||||
""" Decorator to register function to the dispatcher
|
||||
"""
|
||||
if not isinstance(uris, list):
|
||||
@ -24,7 +24,6 @@ def register(uris: str,
|
||||
dispatcher.set_function(version,
|
||||
message,
|
||||
notification,
|
||||
database,
|
||||
function)
|
||||
return decorator
|
||||
|
||||
@ -71,7 +70,6 @@ class RegisterDispatcher:
|
||||
def get_function_args():
|
||||
function_args = self.get_function_args(function)
|
||||
# risotto_context is a special argument, remove it
|
||||
if function_args and function_args[0] == 'risotto_context':
|
||||
function_args = function_args[1:]
|
||||
return set(function_args)
|
||||
|
||||
@ -114,7 +112,6 @@ class RegisterDispatcher:
|
||||
def get_function_args():
|
||||
function_args = self.get_function_args(function)
|
||||
# risotto_context is a special argument, remove it
|
||||
if function_args[0] == 'risotto_context':
|
||||
function_args = function_args[1:]
|
||||
return set(function_args)
|
||||
|
||||
@ -135,7 +132,6 @@ class RegisterDispatcher:
|
||||
version: str,
|
||||
message: str,
|
||||
notification: str,
|
||||
database: bool,
|
||||
function: Callable):
|
||||
""" register a function to an URI
|
||||
URI is a message
|
||||
@ -153,22 +149,12 @@ class RegisterDispatcher:
|
||||
|
||||
# True if first argument is the risotto_context
|
||||
function_args = self.get_function_args(function)
|
||||
if function_args and function_args[0] == 'risotto_context':
|
||||
inject_risotto_context = True
|
||||
function_args.pop(0)
|
||||
else:
|
||||
inject_risotto_context = False
|
||||
|
||||
# check if already register
|
||||
if 'function' in self.messages[version][message]:
|
||||
raise RegistrationError(_(f'uri {version}.{message} already registered'))
|
||||
|
||||
# check notification
|
||||
if self.messages[version][message]['pattern'] == 'rpc':
|
||||
if notification is undefined:
|
||||
function_name = function.__name__
|
||||
raise RegistrationError(_(f'notification is mandatory when registered "{message}" with "{module_name}.{function_name}" even if you set None'))
|
||||
|
||||
# register
|
||||
if self.messages[version][message]['pattern'] == 'rpc':
|
||||
register = self.register_rpc
|
||||
@ -179,8 +165,6 @@ class RegisterDispatcher:
|
||||
module_name,
|
||||
function,
|
||||
function_args,
|
||||
inject_risotto_context,
|
||||
database,
|
||||
notification)
|
||||
|
||||
def register_rpc(self,
|
||||
@ -189,14 +173,10 @@ class RegisterDispatcher:
|
||||
module_name: str,
|
||||
function: Callable,
|
||||
function_args: list,
|
||||
inject_risotto_context: bool,
|
||||
database: bool,
|
||||
notification: Optional[str]):
|
||||
self.messages[version][message]['module'] = module_name
|
||||
self.messages[version][message]['function'] = function
|
||||
self.messages[version][message]['arguments'] = function_args
|
||||
self.messages[version][message]['risotto_context'] = inject_risotto_context
|
||||
self.messages[version][message]['database'] = database
|
||||
if notification:
|
||||
self.messages[version][message]['notification'] = notification
|
||||
|
||||
@ -206,18 +186,14 @@ class RegisterDispatcher:
|
||||
module_name: str,
|
||||
function: Callable,
|
||||
function_args: list,
|
||||
inject_risotto_context: bool,
|
||||
database: bool,
|
||||
notification: Optional[str]):
|
||||
if 'functions' not in self.messages[version][message]:
|
||||
self.messages[version][message]['functions'] = []
|
||||
|
||||
dico = {'module': module_name,
|
||||
'function': function,
|
||||
'arguments': function_args,
|
||||
'database': database,
|
||||
'risotto_context': inject_risotto_context}
|
||||
if notification and notification is not undefined:
|
||||
'arguments': function_args}
|
||||
if notification and notification:
|
||||
dico['notification'] = notification
|
||||
self.messages[version][message]['functions'].append(dico)
|
||||
|
||||
@ -244,11 +220,20 @@ class RegisterDispatcher:
|
||||
raise RegistrationError(_(f'missing uri {missing_messages}'))
|
||||
|
||||
async def on_join(self):
|
||||
async with self.pool.acquire() as connection:
|
||||
await connection.set_type_codec(
|
||||
'json',
|
||||
encoder=dumps,
|
||||
decoder=loads,
|
||||
schema='pg_catalog'
|
||||
)
|
||||
async with connection.transaction():
|
||||
for module_name, module in self.injected_self.items():
|
||||
risotto_context = Context()
|
||||
risotto_context.username = INTERNAL_USER
|
||||
risotto_context.paths.append(f'{module_name}.on_join')
|
||||
risotto_context.type = None
|
||||
risotto_context.connection = connection
|
||||
await module.on_join(risotto_context)
|
||||
|
||||
async def insert_message(self,
|
||||
@ -263,7 +248,15 @@ class RegisterDispatcher:
|
||||
async def load(self):
|
||||
# valid function's arguments
|
||||
db_conf = get_config().get('database')
|
||||
self.pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user'))
|
||||
|
||||
engine = db_conf.get('engine')
|
||||
host = db_conf.get('host')
|
||||
dbname = db_conf.get('dbname')
|
||||
dbuser = db_conf.get('user')
|
||||
dbpassword = db_conf.get('password')
|
||||
dbport = db_conf.get('port')
|
||||
cfg = "{}://{}:{}@{}:{}/{}".format(engine, dbuser, dbpassword, host, dbport, dbname)
|
||||
self.pool = await asyncpg.create_pool(cfg)
|
||||
async with self.pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
for version, messages in self.messages.items():
|
||||
@ -286,3 +279,4 @@ class RegisterDispatcher:
|
||||
module_name)
|
||||
await self.insert_message(connection,
|
||||
f'{version}.{message}')
|
||||
|
||||
|
@ -35,7 +35,7 @@ class Risotto(Controller):
|
||||
'applicationservice_release_id': release_id,
|
||||
'applicationservice_id': applicationservice_id}
|
||||
|
||||
@register('v1.applicationservice.create', None, database=True)
|
||||
@register('v1.applicationservice.create')
|
||||
async def applicationservice_create(self,
|
||||
risotto_context: Context,
|
||||
applicationservice_name: str,
|
||||
@ -48,7 +48,7 @@ class Risotto(Controller):
|
||||
applicationservice_dependencies,
|
||||
release_id)
|
||||
|
||||
@register('v1.applicationservice.dataset.updated', None, database=True)
|
||||
@register('v1.applicationservice.dataset.updated')
|
||||
async def applicationservice_update(self,
|
||||
risotto_context: Context,
|
||||
source_name: str,
|
||||
@ -90,7 +90,7 @@ class Risotto(Controller):
|
||||
return {'retcode': 0,
|
||||
'returns': _('Application Services successfully loaded')}
|
||||
|
||||
@register('v1.applicationservice.get_by_id', None, database=True)
|
||||
@register('v1.applicationservice.get_by_id')
|
||||
async def applicationservice_get_by_id(self,
|
||||
risotto_context: Context,
|
||||
applicationservice_id: int) -> Dict:
|
||||
@ -104,7 +104,7 @@ class Risotto(Controller):
|
||||
raise Exception(_(f'unknown service with ID {applicationservice_id}'))
|
||||
return dict(applicationservice)
|
||||
|
||||
@register('v1.applicationservice.describe', None, database=True)
|
||||
@register('v1.applicationservice.describe')
|
||||
async def applicationservice_describe(self,
|
||||
risotto_context: Context,
|
||||
applicationservice_name,
|
||||
|
@ -10,7 +10,7 @@ from rougail import load as rougail_load
|
||||
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...config import DATABASE_DIR, DEBUG, ROUGAIL_DTD_PATH, get_config
|
||||
from ...config import DATABASE_DIR, ROUGAIL_DTD_PATH, get_config
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
from ...error import CallError, RegistrationError
|
||||
@ -43,7 +43,7 @@ class Risotto(Controller):
|
||||
risotto_context: Context) -> None:
|
||||
""" load all available servermodels
|
||||
"""
|
||||
log.info_msg(risotto_context,
|
||||
await log.info_msg(risotto_context,
|
||||
None,
|
||||
'Load servermodels')
|
||||
servermodels = await self.call('v1.servermodel.list',
|
||||
@ -79,17 +79,25 @@ class Risotto(Controller):
|
||||
"""
|
||||
cache_file = join(self.cache_root_path, str(servermodel_id), "dictionaries.xml")
|
||||
funcs_file = self.get_funcs_filename(servermodel_id)
|
||||
log.info_msg(risotto_context,
|
||||
await log.info_msg(risotto_context,
|
||||
None,
|
||||
f'Load servermodel {servermodel_name} ({servermodel_id})')
|
||||
|
||||
# use file in cache
|
||||
with open(cache_file) as fileio:
|
||||
xmlroot = parse(fileio).getroot()
|
||||
try:
|
||||
self.servermodel[servermodel_id] = await self.build_metaconfig(servermodel_id,
|
||||
servermodel_name,
|
||||
xmlroot,
|
||||
funcs_file)
|
||||
except Exception as err:
|
||||
if get_config().get('global').get('debug'):
|
||||
print_exc()
|
||||
msg = _(f'unable to load {servermodel_name}: {err}')
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
|
||||
async def build_metaconfig(self,
|
||||
servermodel_id: int,
|
||||
@ -148,17 +156,15 @@ class Risotto(Controller):
|
||||
if servermodel_parent_id is None:
|
||||
return
|
||||
if not self.servermodel.get(servermodel_parent_id):
|
||||
if DEBUG:
|
||||
msg = _(f'Servermodel with id {servermodel_parent_id} not loaded, skipping legacy for servermodel {servermodel_name} ({servermodel_id})')
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
return
|
||||
servermodel_parent = self.servermodel[servermodel_parent_id]
|
||||
servermodel_parent_name = await servermodel_parent.information.get('servermodel_name')
|
||||
if DEBUG:
|
||||
msg = _(f'Create legacy of servermodel {servermodel_name} ({servermodel_id}) with parent {servermodel_parent_name} ({servermodel_parent_id})')
|
||||
log.info_msg(risotto_context,
|
||||
await log.info_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
|
||||
@ -167,8 +173,7 @@ class Risotto(Controller):
|
||||
try:
|
||||
await mix.config.add(self.servermodel[servermodel_id])
|
||||
except Exception as err:
|
||||
if DEBUG:
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
str(err))
|
||||
|
||||
@ -176,7 +181,7 @@ class Risotto(Controller):
|
||||
risotto_context: Context) -> None:
|
||||
""" load all available servers
|
||||
"""
|
||||
log.info_msg(risotto_context,
|
||||
await log.info_msg(risotto_context,
|
||||
None,
|
||||
f'Load servers')
|
||||
# get all servers
|
||||
@ -190,12 +195,12 @@ class Risotto(Controller):
|
||||
server['server_name'],
|
||||
server['server_servermodel_id'])
|
||||
except Exception as err:
|
||||
if DEBUG:
|
||||
if get_config().get('global').get('debug'):
|
||||
print_exc()
|
||||
server_name = server['server_name']
|
||||
server_id = server['server_id']
|
||||
msg = _(f'unable to load server {server_name} ({server_id}): {err}')
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
|
||||
@ -208,12 +213,12 @@ class Risotto(Controller):
|
||||
"""
|
||||
if server_id in self.server:
|
||||
return
|
||||
log.info_msg(risotto_context,
|
||||
await log.info_msg(risotto_context,
|
||||
None,
|
||||
f'Load server {server_name} ({server_id})')
|
||||
if not server_servermodel_id in self.servermodel:
|
||||
msg = f'unable to find servermodel with id {server_servermodel_id}'
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
raise CallError(msg)
|
||||
@ -364,13 +369,18 @@ class Risotto(Controller):
|
||||
# await child.information.get('servermodel_id'),
|
||||
# servermodel_id)
|
||||
|
||||
@register('v1.config.configuration.server.get', None)
|
||||
@register('v1.config.configuration.server.get')
|
||||
async def get_configuration(self,
|
||||
server_id: int,
|
||||
risotto_context: Context,
|
||||
server_name: str,
|
||||
deployed: bool) -> bytes:
|
||||
server = await self.call('v1.server.describe',
|
||||
risotto_context,
|
||||
server_name=server_name)
|
||||
server_id = server['server_id']
|
||||
if server_id not in self.server:
|
||||
msg = _(f'cannot find server with id {server_id}')
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
raise CallError(msg)
|
||||
@ -382,25 +392,32 @@ class Risotto(Controller):
|
||||
|
||||
await server.property.read_only()
|
||||
try:
|
||||
configuration = await server.value.dict(fullpath=True)
|
||||
configuration = await server.value.dict(fullpath=True,
|
||||
leader_to_list=True)
|
||||
except:
|
||||
if deployed:
|
||||
msg = _(f'No configuration available for server {server_id}')
|
||||
else:
|
||||
msg = _(f'No undeployed configuration available for server {server_id}')
|
||||
log.error_msg(risotto_context,
|
||||
await log.error_msg(risotto_context,
|
||||
None,
|
||||
msg)
|
||||
raise CallError(msg)
|
||||
return {'server_id': server_id,
|
||||
return {'server_name': server_name,
|
||||
'deployed': deployed,
|
||||
'configuration': configuration}
|
||||
|
||||
@register('v1.config.configuration.server.deploy', 'v1.config.configuration.server.updated')
|
||||
async def deploy_configuration(self,
|
||||
server_id: int) -> Dict:
|
||||
risotto_context: Context,
|
||||
server_name: str) -> Dict:
|
||||
"""Copy values, permissions, permissives from config 'to deploy' to active config
|
||||
"""
|
||||
server = await self.call('v1.server.describe',
|
||||
risotto_context,
|
||||
server_name=server_name)
|
||||
server_id = server['server_id']
|
||||
# FIXME is server_to_deploy working?
|
||||
config = self.server[server_id]['server']
|
||||
config_std = self.server[server_id]['server_to_deploy']
|
||||
|
||||
@ -420,4 +437,5 @@ class Risotto(Controller):
|
||||
await config.property.importation(await config_std.property.exportation())
|
||||
|
||||
return {'server_id': server_id,
|
||||
'server_name': server_name,
|
||||
'deployed': True}
|
||||
|
@ -7,7 +7,7 @@ from ...utils import _
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
@register('v1.server.list', None, database=True)
|
||||
@register('v1.server.list')
|
||||
async def server_list(self,
|
||||
risotto_context: Context) -> Dict:
|
||||
sql = '''
|
||||
@ -17,16 +17,19 @@ class Risotto(Controller):
|
||||
servers = await risotto_context.connection.fetch(sql)
|
||||
return [dict(r) for r in servers]
|
||||
|
||||
@register('v1.server.create', 'v1.server.created', database=True)
|
||||
@register('v1.server.create', 'v1.server.created')
|
||||
async def server_create(self,
|
||||
risotto_context: Context,
|
||||
server_name: str,
|
||||
server_description: str,
|
||||
server_servermodel_id: int) -> Dict:
|
||||
|
||||
await self.call('v1.servermodel.get_by_id',
|
||||
servermodel_name: str,
|
||||
source_name: str,
|
||||
release_distribution: str) -> Dict:
|
||||
servermodel = await self.call('v1.servermodel.describe',
|
||||
risotto_context,
|
||||
servermodel_id=server_servermodel_id)
|
||||
servermodel_name=servermodel_name,
|
||||
source_name=source_name,
|
||||
release_distribution=release_distribution)
|
||||
server_insert = """INSERT INTO Server(ServerName, ServerDescription, ServerServermodelId)
|
||||
VALUES ($1,$2,$3)
|
||||
RETURNING ServerId
|
||||
@ -34,13 +37,19 @@ class Risotto(Controller):
|
||||
server_id = await risotto_context.connection.fetchval(server_insert,
|
||||
server_name,
|
||||
server_description,
|
||||
server_servermodel_id)
|
||||
servermodel['servermodel_id'])
|
||||
await self.call('v1.user.role.create',
|
||||
risotto_context,
|
||||
user_login=risotto_context.username,
|
||||
role_name='server_rw',
|
||||
role_attribute='Server.ServerName',
|
||||
role_attribute_value=server_name)
|
||||
return {'server_id': server_id,
|
||||
'server_name': server_name,
|
||||
'server_description': server_description,
|
||||
'server_servermodel_id': server_servermodel_id}
|
||||
'server_servermodel_id': servermodel['servermodel_id']}
|
||||
|
||||
@register('v1.server.describe', None, database=True)
|
||||
@register('v1.server.describe')
|
||||
async def server_describe(self,
|
||||
risotto_context: Context,
|
||||
server_name: str) -> Dict:
|
||||
|
@ -31,16 +31,17 @@ class Risotto(Controller):
|
||||
source_url='none')
|
||||
internal_release = await self.call('v1.source.release.create',
|
||||
risotto_context,
|
||||
source_id=internal_source['source_id'],
|
||||
source_name='internal',
|
||||
release_name='none',
|
||||
release_distribution='stable')
|
||||
release_distribution='last')
|
||||
self.internal_release_id = internal_release['release_id']
|
||||
|
||||
def servermodel_gen_funcs(self,
|
||||
async def servermodel_gen_funcs(self,
|
||||
servermodel_name: str,
|
||||
servermodel_id: int,
|
||||
dependencies: Dict,
|
||||
release_cache: Dict) -> None:
|
||||
release_cache: Dict,
|
||||
risotto_context: Context) -> None:
|
||||
as_names = []
|
||||
dest_file = self.get_servermodel_cache(servermodel_id, 'funcs.py')
|
||||
with open(dest_file, 'wb') as funcs:
|
||||
@ -65,14 +66,16 @@ class Risotto(Controller):
|
||||
funcs.write(b'\n')
|
||||
|
||||
as_names_str = '", "'.join(as_names)
|
||||
log.info(_(f'gen funcs for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
await log.info(risotto_context,
|
||||
_(f'gen funcs for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
eolobj = CreoleObjSpace(dtdfilename)
|
||||
|
||||
def servermodel_gen_schema(self,
|
||||
async def servermodel_gen_schema(self,
|
||||
servermodel_name: str,
|
||||
servermodel_id: int,
|
||||
dependencies: Dict,
|
||||
release_cache: Dict) -> None:
|
||||
release_cache: Dict,
|
||||
risotto_context: Context) -> None:
|
||||
paths = []
|
||||
extras = []
|
||||
as_names = set()
|
||||
@ -105,7 +108,8 @@ class Risotto(Controller):
|
||||
extras.append((namespace, [extra_dir]))
|
||||
eolobj = CreoleObjSpace(dtdfilename)
|
||||
as_names_str = '", "'.join(as_names)
|
||||
log.info(_(f'gen schema for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
await log.info(risotto_context,
|
||||
_(f'gen schema for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
eolobj.create_or_populate_from_xml('creole', paths)
|
||||
for extra in extras:
|
||||
eolobj.create_or_populate_from_xml(extra[0], extra[1])
|
||||
@ -122,11 +126,12 @@ class Risotto(Controller):
|
||||
return join(self.cache_root_path, str(servermodel_id), subdir)
|
||||
return join(self.cache_root_path, str(servermodel_id))
|
||||
|
||||
def servermodel_copy_templates(self,
|
||||
async def servermodel_copy_templates(self,
|
||||
servermodel_name: str,
|
||||
servermodel_id: int,
|
||||
dependencies: Dict,
|
||||
release_cache: Dict) -> None:
|
||||
release_cache: Dict,
|
||||
risotto_context: Context) -> None:
|
||||
as_names = []
|
||||
dest_dir = self.get_servermodel_cache(servermodel_id, 'templates')
|
||||
makedirs(dest_dir)
|
||||
@ -147,7 +152,8 @@ class Risotto(Controller):
|
||||
copyfile(join(path, template), template_path)
|
||||
as_names.append(applicationservice_name)
|
||||
as_names_str = '", "'.join(as_names)
|
||||
log.info(_(f'copy templates for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
await log.info(risotto_context,
|
||||
_(f'copy templates for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
|
||||
async def _servermodel_create(self,
|
||||
risotto_context: Context,
|
||||
@ -193,18 +199,21 @@ class Risotto(Controller):
|
||||
risotto_context,
|
||||
release_id=as_release_id)
|
||||
|
||||
self.servermodel_gen_funcs(servermodel_name,
|
||||
await self.servermodel_gen_funcs(servermodel_name,
|
||||
servermodel_id,
|
||||
dependencies,
|
||||
release_cache)
|
||||
self.servermodel_gen_schema(servermodel_name,
|
||||
release_cache,
|
||||
risotto_context)
|
||||
await self.servermodel_gen_schema(servermodel_name,
|
||||
servermodel_id,
|
||||
dependencies,
|
||||
release_cache)
|
||||
self.servermodel_copy_templates(servermodel_name,
|
||||
release_cache,
|
||||
risotto_context)
|
||||
await self.servermodel_copy_templates(servermodel_name,
|
||||
servermodel_id,
|
||||
dependencies,
|
||||
release_cache)
|
||||
release_cache,
|
||||
risotto_context)
|
||||
sm_dict = {'servermodel_name': servermodel_name,
|
||||
'servermodel_description': servermodel_description,
|
||||
'servermodel_parents_id': servermodel_parents_id,
|
||||
@ -236,7 +245,7 @@ class Risotto(Controller):
|
||||
servermodel_name,
|
||||
release_id)['servermodel_id']
|
||||
|
||||
@register('v1.servermodel.dataset.updated', None, database=True)
|
||||
@register('v1.servermodel.dataset.updated')
|
||||
async def servermodel_update(self,
|
||||
risotto_context: Context,
|
||||
source_name: str,
|
||||
@ -313,7 +322,7 @@ class Risotto(Controller):
|
||||
servermodel_description['done'] = True
|
||||
return {'retcode': 0, 'returns': _('Servermodels successfully loaded')}
|
||||
|
||||
@register('v1.servermodel.list', None, database=True)
|
||||
@register('v1.servermodel.list')
|
||||
async def servermodel_list(self,
|
||||
risotto_context: Context,
|
||||
source_id: int):
|
||||
@ -324,10 +333,32 @@ class Risotto(Controller):
|
||||
servermodels = await risotto_context.connection.fetch(sql)
|
||||
return [dict(r) for r in servermodels]
|
||||
|
||||
@register('v1.servermodel.get_by_id', None, database=True)
|
||||
@register('v1.servermodel.describe')
|
||||
async def servermodel_describe(self,
|
||||
risotto_context: Context,
|
||||
servermodel_name,
|
||||
source_name,
|
||||
release_distribution) -> Dict:
|
||||
release = await self.call('v1.source.release.describe',
|
||||
risotto_context,
|
||||
source_name=source_name,
|
||||
release_distribution=release_distribution)
|
||||
sql = '''
|
||||
SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id
|
||||
FROM Servermodel
|
||||
WHERE ServermodelName=$1 AND ServermodelReleaseId=$2
|
||||
'''
|
||||
servermodel = await risotto_context.connection.fetchrow(sql,
|
||||
servermodel_name,
|
||||
release['release_id'])
|
||||
if not servermodel:
|
||||
raise Exception(_(f'{servermodel_id} is not a valid ID for a servermodel'))
|
||||
return dict(servermodel)
|
||||
|
||||
@register('v1.servermodel.get_by_id')
|
||||
async def servermodel_get_by_id(self,
|
||||
risotto_context: Context,
|
||||
servermodel_id: int):
|
||||
servermodel_id: int) -> Dict:
|
||||
sql = '''
|
||||
SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id
|
||||
FROM Servermodel
|
||||
|
@ -6,7 +6,6 @@ from tiramisu import Storage
|
||||
|
||||
|
||||
from ...http import register as register_http
|
||||
from ...config import DEBUG
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
from .storage import storage_server, storage_servermodel
|
||||
@ -64,24 +63,22 @@ class Risotto(Controller):
|
||||
'mode': session['mode'],
|
||||
'debug': session['debug']}
|
||||
|
||||
@register(['v1.session.server.start', 'v1.session.servermodel.start'], None)
|
||||
async def start_session(self,
|
||||
@register('v1.session.server.start')
|
||||
async def start_session_server(self,
|
||||
risotto_context: Context,
|
||||
id: int) -> Dict:
|
||||
""" start a new config session for a server or a servermodel
|
||||
server_name: str) -> Dict:
|
||||
""" start a new config session for a server
|
||||
"""
|
||||
type = risotto_context.message.rsplit('.', 2)[-2]
|
||||
config_module = dispatcher.get_service('config')
|
||||
if type == 'server':
|
||||
if id not in config_module.server:
|
||||
raise Exception(_(f'cannot find {type} with id {id}'))
|
||||
config = config_module.server[id]['server']
|
||||
else:
|
||||
if id not in config_module.servermodel:
|
||||
raise Exception(_(f'cannot find {type} with id {id}'))
|
||||
config = config_module.servermodel[id]
|
||||
server = await self.call('v1.server.describe',
|
||||
risotto_context,
|
||||
server_name=server_name)
|
||||
if not server or server['server_id'] not in config_module.server:
|
||||
raise Exception(_(f'cannot find server with name {server_name}'))
|
||||
id = server['server_id']
|
||||
config = config_module.server[id]['server_to_deploy']
|
||||
|
||||
storage = self.get_storage(type)
|
||||
storage = self.get_storage('server')
|
||||
|
||||
# check if a session already exists
|
||||
sessions = storage.get_sessions()
|
||||
@ -108,9 +105,57 @@ class Risotto(Controller):
|
||||
# return session's information
|
||||
return self.get_session_informations(risotto_context,
|
||||
session_id,
|
||||
type)
|
||||
'server')
|
||||
|
||||
@register(['v1.session.server.list', 'v1.session.servermodel.list'], None)
|
||||
@register('v1.session.servermodel.start')
|
||||
async def start_session_servermodel(self,
|
||||
risotto_context: Context,
|
||||
servermodel_name: str,
|
||||
source_name: str,
|
||||
release_distribution: str) -> Dict:
|
||||
""" start a new config session for a server or a servermodel
|
||||
"""
|
||||
config_module = dispatcher.get_service('config')
|
||||
servermodel = await self.call('v1.servermodel.describe',
|
||||
risotto_context,
|
||||
servermodel_name=servermodel_name,
|
||||
source_name=source_name,
|
||||
release_distribution=release_distribution)
|
||||
if not servermodel or servermodel['servermodel_id'] not in config_module.servermodel:
|
||||
raise Exception(_(f'cannot find servermodel with name {servermodel_name}'))
|
||||
id = servermodel['servermodel_id']
|
||||
config = config_module.servermodel[id]
|
||||
|
||||
storage = self.get_storage('servermodel')
|
||||
|
||||
# check if a session already exists
|
||||
sessions = storage.get_sessions()
|
||||
for sess_id, session in sessions.items():
|
||||
if session['id'] == id:
|
||||
if session['username'] == risotto_context.username:
|
||||
# same user so returns it
|
||||
return self.format_session(sess_id,
|
||||
session)
|
||||
else:
|
||||
raise Exception(_(f'{username} already edits this configuration'))
|
||||
|
||||
# create a new session
|
||||
while True:
|
||||
session_id = 'z' + hexlify(urandom(23)).decode()
|
||||
if not session_id in sessions:
|
||||
break
|
||||
await storage.add_session(session_id,
|
||||
config,
|
||||
id,
|
||||
risotto_context.username,
|
||||
self.modify_storage)
|
||||
|
||||
# return session's information
|
||||
return self.get_session_informations(risotto_context,
|
||||
session_id,
|
||||
'servermodel')
|
||||
|
||||
@register(['v1.session.server.list', 'v1.session.servermodel.list'])
|
||||
async def list_session_server(self,
|
||||
risotto_context: Context) -> Dict:
|
||||
type = risotto_context.message.rsplit('.', 2)[-2]
|
||||
@ -118,7 +163,7 @@ class Risotto(Controller):
|
||||
return [self.format_session(session_id, session) for session_id, session in storage.get_sessions().items()]
|
||||
|
||||
|
||||
@register(['v1.session.server.filter', 'v1.session.servermodel.filter'], None)
|
||||
@register(['v1.session.server.filter', 'v1.session.servermodel.filter'])
|
||||
async def filter_session(self,
|
||||
risotto_context: Context,
|
||||
session_id: str,
|
||||
@ -145,7 +190,7 @@ class Risotto(Controller):
|
||||
session_id,
|
||||
type)
|
||||
|
||||
@register(['v1.session.server.configure', 'v1.session.servermodel.configure'], None)
|
||||
@register(['v1.session.server.configure', 'v1.session.servermodel.configure'])
|
||||
async def configure_session(self,
|
||||
risotto_context: Context,
|
||||
session_id: str,
|
||||
@ -182,7 +227,7 @@ class Risotto(Controller):
|
||||
ret['index'] = index
|
||||
return ret
|
||||
|
||||
@register(['v1.session.server.validate', 'v1.session.servermodel.validate'], None)
|
||||
@register(['v1.session.server.validate', 'v1.session.servermodel.validate'])
|
||||
async def validate_session(self,
|
||||
risotto_context: Context,
|
||||
session_id: str) -> Dict:
|
||||
@ -211,7 +256,7 @@ class Risotto(Controller):
|
||||
return self.format_session(session_id,
|
||||
session)
|
||||
|
||||
@register(['v1.session.server.get', 'v1.session.servermodel.get'], None)
|
||||
@register(['v1.session.server.get', 'v1.session.servermodel.get'])
|
||||
async def get_session_server(self,
|
||||
risotto_context: Context,
|
||||
session_id: str,
|
||||
@ -229,7 +274,7 @@ class Risotto(Controller):
|
||||
info['content'] = content
|
||||
return info
|
||||
|
||||
@register(['v1.session.server.stop', 'v1.session.servermodel.stop'], None)
|
||||
@register(['v1.session.server.stop', 'v1.session.servermodel.stop'])
|
||||
async def stop_session(self,
|
||||
risotto_context: Context,
|
||||
session_id: str,
|
||||
@ -241,7 +286,7 @@ class Risotto(Controller):
|
||||
id_ = session['id']
|
||||
config_module = dispatcher.get_service('config')
|
||||
if type == 'server':
|
||||
config = config_module.server[id_]['server']
|
||||
config = config_module.server[id_]['server_to_deploy']
|
||||
else:
|
||||
config = config_module.servermodel[id_]
|
||||
if save:
|
||||
|
@ -10,7 +10,7 @@ from ...config import get_config
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
@register('v1.source.create', None, database=True)
|
||||
@register('v1.source.create')
|
||||
async def source_create(self,
|
||||
risotto_context: Context,
|
||||
source_name: str,
|
||||
@ -43,7 +43,7 @@ class Risotto(Controller):
|
||||
'source_url': source_url,
|
||||
'source_id': source_id}
|
||||
|
||||
@register('v1.source.describe', None, database=True)
|
||||
@register('v1.source.describe')
|
||||
async def source_describe(self,
|
||||
risotto_context: Context,
|
||||
source_name: str) -> Dict:
|
||||
@ -57,7 +57,7 @@ class Risotto(Controller):
|
||||
raise Exception(_(f'unknown source with name {source_name}'))
|
||||
return dict(source)
|
||||
|
||||
@register('v1.source.list', None, database=True)
|
||||
@register('v1.source.list')
|
||||
async def source_list(self,
|
||||
risotto_context: Context) -> List[Dict]:
|
||||
source_list = """SELECT SourceId as source_id, SourceName as source_name, SourceURL as source_url
|
||||
@ -66,7 +66,7 @@ class Risotto(Controller):
|
||||
result = await risotto_context.connection.fetch(source_list)
|
||||
return [dict(r) for r in result]
|
||||
|
||||
@register('v1.source.dataset.update', None, database=True)
|
||||
@register('v1.source.dataset.update')
|
||||
async def version_update(self,
|
||||
risotto_context: Context,
|
||||
source_id: int,
|
||||
@ -81,25 +81,28 @@ class Risotto(Controller):
|
||||
return {'release_id': release_id,
|
||||
'release_name': release_name}
|
||||
|
||||
@register('v1.source.release.create', None, database=True)
|
||||
@register('v1.source.release.create')
|
||||
async def source_release_create(self,
|
||||
risotto_context: Context,
|
||||
source_id: int,
|
||||
source_name: str,
|
||||
release_name: str,
|
||||
release_distribution: str) -> Dict:
|
||||
source_get = """SELECT SourceId as source_id, SourceName as source_name, SourceURL as source_url
|
||||
FROM Source
|
||||
WHERE SourceId = $1
|
||||
WHERE SourceName = $1
|
||||
"""
|
||||
release_upsert = """INSERT INTO Release(ReleaseName, ReleaseSourceId, ReleaseDistribution) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (ReleaseName, ReleaseSourceId) DO UPDATE SET ReleaseName = $1
|
||||
RETURNING ReleaseId
|
||||
"""
|
||||
source = dict(await risotto_context.connection.fetchrow(source_get,
|
||||
source_id))
|
||||
source_obj = await risotto_context.connection.fetchrow(source_get,
|
||||
source_name)
|
||||
if not source_obj:
|
||||
raise Exception(_(f'unable to find a source with name {source_name}'))
|
||||
source = dict(source_obj)
|
||||
release_id = await risotto_context.connection.fetchval(release_upsert,
|
||||
release_name,
|
||||
source_id,
|
||||
source['source_id'],
|
||||
release_distribution)
|
||||
del source['source_id']
|
||||
source['release_id'] = release_id
|
||||
@ -107,16 +110,33 @@ class Risotto(Controller):
|
||||
source['release_distribution'] = release_distribution
|
||||
return source
|
||||
|
||||
@register('v1.source.release.list', None, database=True)
|
||||
@register('v1.source.release.list')
|
||||
async def release_list(self,
|
||||
risotto_context):
|
||||
risotto_context,
|
||||
source_name: str) -> Dict:
|
||||
release_query = """SELECT ReleaseId as release_id, SourceName as source_name, SourceURL as source_url, ReleaseName as release_name, ReleaseDistribution as release_distribution
|
||||
FROM Release, Source
|
||||
WHERE Source.SourceId=Release.ReleaseSourceId"""
|
||||
result = await risotto_context.connection.fetch(release_query)
|
||||
WHERE Source.SourceName=$1 AND Source.SourceId=Release.ReleaseSourceId"""
|
||||
result = await risotto_context.connection.fetch(release_query,
|
||||
source_name)
|
||||
return [dict(r) for r in result]
|
||||
|
||||
@register('v1.source.release.get_by_id', None, database=True)
|
||||
@register('v1.source.release.describe')
|
||||
async def release_list(self,
|
||||
risotto_context,
|
||||
source_name: str,
|
||||
release_distribution: str) -> Dict:
|
||||
release_query = """SELECT ReleaseId as release_id, SourceName as source_name, SourceURL as source_url, ReleaseName as release_name, ReleaseDistribution as release_distribution
|
||||
FROM Release, Source
|
||||
WHERE Source.SourceName=$1 AND Source.SourceId=Release.ReleaseSourceId AND Release.ReleaseDistribution=$2"""
|
||||
result = await risotto_context.connection.fetchrow(release_query,
|
||||
source_name,
|
||||
release_distribution)
|
||||
if not result:
|
||||
raise Exception(_(f'unknown release distribution {release_distribution} in source {source_name}'))
|
||||
return dict(result)
|
||||
|
||||
@register('v1.source.release.get_by_id')
|
||||
async def release_get_by_id(self,
|
||||
risotto_context: Context,
|
||||
release_id: int) -> Dict:
|
||||
@ -129,7 +149,7 @@ class Risotto(Controller):
|
||||
raise Exception(_(f'unknown release id {release_id}'))
|
||||
return dict(result)
|
||||
|
||||
@register('v1.source.release.get_by_distribution', None, database=True)
|
||||
@register('v1.source.release.get_by_distribution')
|
||||
async def release_get_by_distribution(self,
|
||||
risotto_context: Context,
|
||||
source_id: int,
|
||||
|
@ -17,7 +17,7 @@ class Risotto(Controller):
|
||||
self.storage = Storage(engine='dictionary')
|
||||
self.cache_root_path = join(get_config().get('cache').get('root_path'), 'servermodel')
|
||||
|
||||
@register('v1.template.generate', None)
|
||||
@register('v1.template.generate')
|
||||
async def template_get(self,
|
||||
risotto_context,
|
||||
server_name: str) -> Dict:
|
||||
@ -28,10 +28,13 @@ class Risotto(Controller):
|
||||
servermodel_id = server['server_servermodel_id']
|
||||
config_module = dispatcher.get_service('config')
|
||||
server = config_module.server[server_id]
|
||||
config = meta = server['server'].config.deepcopy(storage=self.storage)
|
||||
export = await server['server'].value.exportation()
|
||||
if not export[0]:
|
||||
raise Exception(_(f'configuration for server "{server_name}" is empty, you should deploy it first'))
|
||||
config = meta = await server['server'].config.deepcopy(storage=self.storage)
|
||||
while True:
|
||||
try:
|
||||
children = list(config.config.list())
|
||||
children = list(await config.config.list())
|
||||
except:
|
||||
break
|
||||
if children:
|
||||
@ -48,11 +51,11 @@ class Risotto(Controller):
|
||||
rmtree(tmp_dir)
|
||||
mkdir(tmp_dir)
|
||||
templates_dir = join(self.cache_root_path, str(servermodel_id), 'templates')
|
||||
generate(config,
|
||||
await generate(config,
|
||||
server['funcs_file'],
|
||||
templates_dir,
|
||||
tmp_dir,
|
||||
configurations_dir)
|
||||
|
||||
return {'server_id': server_id,
|
||||
return {'server_name': server_name,
|
||||
'template_dir': configurations_dir}
|
||||
|
@ -24,10 +24,10 @@ class Risotto(Controller):
|
||||
'v1.user.delete',
|
||||
'v1.user.list',
|
||||
'v1.user.role.create',
|
||||
'v1.config.configuration.server.get',
|
||||
'v1.user.role.list']:
|
||||
try:
|
||||
await self.call('v1.uri.role.join',
|
||||
risotto_context,
|
||||
await self._uri_role_join(risotto_context,
|
||||
role_name='administrator',
|
||||
uri_name=uri)
|
||||
except:
|
||||
@ -56,18 +56,35 @@ class Risotto(Controller):
|
||||
'v1.source.release.get_by_id',
|
||||
'v1.source.release.list']:
|
||||
try:
|
||||
await self.call('v1.uri.role.join',
|
||||
risotto_context,
|
||||
await self._uri_role_join(risotto_context,
|
||||
role_name='all',
|
||||
uri_name=uri)
|
||||
except:
|
||||
pass
|
||||
for uri in ['v1.server.describe',
|
||||
'v1.config.configuration.server.get',
|
||||
'v1.config.configuration.server.deploy',
|
||||
'v1.session.server.start',
|
||||
'v1.template.generate']:
|
||||
try:
|
||||
await self._uri_role_join(risotto_context,
|
||||
role_name='server_rw',
|
||||
uri_name=uri)
|
||||
except:
|
||||
pass
|
||||
|
||||
@register('v1.uri.role.join', None, database=True)
|
||||
@register('v1.uri.role.join')
|
||||
async def uri_role_join(self,
|
||||
risotto_context: Context,
|
||||
role_name: str,
|
||||
uri_name: str) -> Dict:
|
||||
return await self._uri_role_join(risotto_context,
|
||||
role_name,
|
||||
uri_name)
|
||||
async def _uri_role_join(self,
|
||||
risotto_context: Context,
|
||||
role_name: str,
|
||||
uri_name: str) -> Dict:
|
||||
# Verify if user exists and get ID
|
||||
sql = '''
|
||||
SELECT URIId
|
||||
@ -89,11 +106,11 @@ class Risotto(Controller):
|
||||
return {'role_name': role_name,
|
||||
'uri_name': uri_name}
|
||||
|
||||
@register('v1.uri.role.list', None, database=True)
|
||||
@register('v1.uri.role.list')
|
||||
async def uri_role_list(self,
|
||||
risotto_context: Context) -> List[Dict]:
|
||||
sql = '''
|
||||
SELECT RoleName as role_name, URI.URIName as uri
|
||||
SELECT RoleName as role_name, URI.URIName as uri_name
|
||||
FROM RoleURI, URI
|
||||
WHERE RoleURI.URIId = URI.URIId
|
||||
'''
|
||||
|
@ -4,21 +4,47 @@ from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
from ...config import get_config
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
@register('v1.user.create', None, database=True)
|
||||
async def user_create(self,
|
||||
async def on_join(self,
|
||||
risotto_context: Context) -> None:
|
||||
""" pre-load servermodel and server
|
||||
"""
|
||||
user_login = get_config()['global']['admin_user']
|
||||
user_password = get_config()['global']['admin_user_password']
|
||||
sql = '''
|
||||
SELECT UserId
|
||||
FROM RisottoUser
|
||||
WHERE UserLogin = $1
|
||||
'''
|
||||
if await risotto_context.connection.fetchval(sql,
|
||||
user_login) is None:
|
||||
await self._user_create(risotto_context,
|
||||
user_login,
|
||||
user_password,
|
||||
user_login,
|
||||
user_login)
|
||||
await self._user_role_create(risotto_context,
|
||||
user_login,
|
||||
'administrator',
|
||||
None,
|
||||
None)
|
||||
|
||||
async def _user_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
user_password: str,
|
||||
user_name: str,
|
||||
user_surname: str) -> Dict:
|
||||
user_insert = """INSERT INTO RisottoUser(UserLogin, UserName, UserSurname)
|
||||
VALUES ($1,$2,$3)
|
||||
user_insert = """INSERT INTO RisottoUser(UserLogin, UserPassword, UserName, UserSurname)
|
||||
VALUES ($1,crypt($2, gen_salt('bf')),$3,$4)
|
||||
RETURNING UserId
|
||||
"""
|
||||
user_id = await risotto_context.connection.fetchval(user_insert,
|
||||
user_login,
|
||||
user_password,
|
||||
user_name,
|
||||
user_surname)
|
||||
await self.call('v1.user.role.create',
|
||||
@ -30,7 +56,20 @@ class Risotto(Controller):
|
||||
'user_name': user_name,
|
||||
'user_surname': user_surname}
|
||||
|
||||
@register('v1.user.list', None, database=True)
|
||||
@register('v1.user.create')
|
||||
async def user_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
user_password: str,
|
||||
user_name: str,
|
||||
user_surname: str) -> Dict:
|
||||
return await self._user_create(risotto_context,
|
||||
user_login,
|
||||
user_password,
|
||||
user_name,
|
||||
user_surname)
|
||||
|
||||
@register('v1.user.list')
|
||||
async def user_list(self,
|
||||
risotto_context: Context) -> Dict:
|
||||
sql = '''
|
||||
@ -40,7 +79,7 @@ class Risotto(Controller):
|
||||
users = await risotto_context.connection.fetch(sql)
|
||||
return [dict(r) for r in users]
|
||||
|
||||
@register('v1.user.delete', None, database=True)
|
||||
@register('v1.user.delete')
|
||||
async def user_delete(self,
|
||||
risotto_context: Context,
|
||||
user_login: str) -> Dict:
|
||||
@ -55,8 +94,7 @@ class Risotto(Controller):
|
||||
raise Exception(_(f'unable to find user {user_login}'))
|
||||
return dict(user)
|
||||
|
||||
@register('v1.user.role.create', None, database=True)
|
||||
async def user_role_create(self,
|
||||
async def _user_role_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
role_name: str,
|
||||
@ -72,6 +110,25 @@ class Risotto(Controller):
|
||||
user_login)
|
||||
if user_id is None:
|
||||
raise Exception(_(f'unable to find user {user_login}'))
|
||||
if role_attribute == role_attribute_value == None:
|
||||
sql = '''SELECT RoleId
|
||||
FROM UserRole
|
||||
WHERE RoleUserId = $1 AND RoleName = $2
|
||||
'''
|
||||
role_id = await risotto_context.connection.fetchval(sql,
|
||||
user_id,
|
||||
role_name)
|
||||
else:
|
||||
sql = '''SELECT RoleId
|
||||
FROM UserRole
|
||||
WHERE RoleUserId = $1 AND RoleName = $2 AND RoleAttribute = $3 AND RoleAttributeValue = $4
|
||||
'''
|
||||
role_id = await risotto_context.connection.fetchval(sql,
|
||||
user_id,
|
||||
role_name,
|
||||
role_attribute,
|
||||
role_attribute_value)
|
||||
if role_id is None:
|
||||
sql = '''INSERT INTO UserRole(RoleUserId, RoleName, RoleAttribute, RoleAttributeValue)
|
||||
VALUES($1,$2,$3,$4)
|
||||
RETURNING RoleId
|
||||
@ -87,7 +144,20 @@ class Risotto(Controller):
|
||||
'role_attribute': role_attribute,
|
||||
'role_attribute_value': role_attribute_value}
|
||||
|
||||
@register('v1.user.role.list', None, database=True)
|
||||
@register('v1.user.role.create')
|
||||
async def user_role_create(self,
|
||||
risotto_context: Context,
|
||||
user_login: str,
|
||||
role_name: str,
|
||||
role_attribute: str,
|
||||
role_attribute_value: str) -> Dict:
|
||||
return await self._user_role_create(risotto_context,
|
||||
user_login,
|
||||
role_name,
|
||||
role_attribute,
|
||||
role_attribute_value)
|
||||
|
||||
@register('v1.user.role.list')
|
||||
async def user_role_list(self,
|
||||
risotto_context: Context,
|
||||
user_login: Optional[str]) -> Dict:
|
||||
@ -119,7 +189,7 @@ class Risotto(Controller):
|
||||
return [dict(r) for r in roles]
|
||||
#
|
||||
# FIXME comment savoir quel role il faut supprimer ? avec attribut ou juste l'ID ?
|
||||
# @register('v1.user.role.delete', None, database=True)
|
||||
# @register('v1.user.role.delete')
|
||||
# async def user_role_delete(self,
|
||||
# risotto_context: Context,
|
||||
# user_login: str,
|
||||
|
Reference in New Issue
Block a user