Commit e5bcc50c authored by Daniel Dehennin's avatar Daniel Dehennin

Import from all-in-one zephir repository

parent 68df9c8c
####
#### Temporary layer to prepare installation
####
FROM ubuntu:bionic AS build
# Packages required to build
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y \
curl \
git
ARG CONTAINERPILOT_VERSION=3.4.3
ARG CONTAINERPILOT_CHECKSUM=e8258ed166bcb3de3e06638936dcc2cae32c7c58
RUN curl -Lso /tmp/containerpilot.tar.gz \
"https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VERSION}/containerpilot-${CONTAINERPILOT_VERSION}.tar.gz" \
&& echo "${CONTAINERPILOT_CHECKSUM} /tmp/containerpilot.tar.gz" | sha1sum -c \
&& tar zxf /tmp/containerpilot.tar.gz -C /tmp
ARG CONSULTEMPLATE_VERSION=0.19.3
ARG CONSULTEMPLATE_CHECKSUM=6467b442d8fd68ca0a1eba35f42edb6f3b9941e10b6d58688edc3506fca0bb19
RUN curl -Lso /tmp/consul-template.tar.gz \
"https://releases.hashicorp.com/consul-template/${CONSULTEMPLATE_VERSION}/consul-template_${CONSULTEMPLATE_VERSION}_linux_amd64.tgz" \
&& echo "${CONSULTEMPLATE_CHECKSUM} /tmp/consul-template.tar.gz" | sha256sum -c \
&& tar zxf /tmp/consul-template.tar.gz -C /tmp
# Orcherstration using containerpilot
# Configuration using consul-template
# Use dev-eole repository since gitlab is not public
ARG CONTAINERPILOT_REPO_URL=https://dev-eole.ac-dijon.fr/git/zephir-orchestrate-containerpilot.git
ARG CONTAINERPILOT_REPO_REF=223dafd3d093702c8717292247e65d0c55ffb513
RUN git clone "${CONTAINERPILOT_REPO_URL}" "/tmp/orchestrate" \
&& cd /tmp/orchestrate \
&& git checkout "${CONTAINERPILOT_REPO_REF}"
# API messages description
# Use dev-eole repository since gitlab is not public
ARG MESSAGES_API_REPO_URL=https://dev-eole.ac-dijon.fr/git/zephir-messages-api.git
ARG MESSAGES_API_REPO_REF=e10c743b3a7d1e84ed13bd49a46e7361fd403ccc
RUN git clone "${MESSAGES_API_REPO_URL}" "/tmp/messages-api" \
&& cd /tmp/messages-api \
&& git checkout "${MESSAGES_API_REPO_REF}"
# Common python Zéphir library
# Use dev-eole repository since gitlab is not public
ARG PYTHON_ZEPHIR_REPO_URL=https://dev-eole.ac-dijon.fr/git/python-zephir.git
ARG PYTHON_ZEPHIR_REPO_REF=bc098b33494c7ef7ae010e130bb773507e196b72
RUN git clone "${PYTHON_ZEPHIR_REPO_URL}" "/tmp/python-zephir" \
&& cd /tmp/python-zephir \
&& git checkout "${PYTHON_ZEPHIR_REPO_REF}"
####
#### Target layer
####
FROM ubuntu:bionic
MAINTAINER Pôle EOLE <eole@ac-dijon.fr>
# Packages required for working service
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get install -y \
gnupg \
jq \
locales \
openssl \
python3 \
python3-aiohttp \
python3-autobahn \
python3-dev \
python3-jwt \
python3-psycopg2 \
python3-requests \
python3-yaml \
sqitch \
tzdata
# For debug only
RUN apt-get update -y && apt-get install -y \
curl \
iproute2 \
iputils-ping \
vim
# Force french locale
RUN locale-gen fr_FR.UTF-8
ENV LANG fr_FR.UTF-8
ENV LC_ALL fr_FR.UTF-8
# Force timezone
RUN ln -fs /usr/share/zoneinfo/Europe/Paris /etc/localtime
RUN dpkg-reconfigure --frontend noninteractive tzdata
# Sqitch
RUN sqitch config --user user.name 'Equipe EOLE'\
&& sqitch config --user user.email 'eole@ac-dijon.fr'
# Install tools from build layer
COPY --from=build /tmp/containerpilot /usr/local/bin
COPY --from=build /tmp/consul-template /usr/local/bin
# consul-template wrapper to generate /etc/zephir-services.conf
COPY --from=build /tmp/orchestrate/update-service-conf /usr/local/bin/
RUN chmod +x /usr/local/bin/*
# Service template for consul-template
ENV services_conf_filename=zephir-services.conf
ARG services_conf=/etc/zephir-services.conf
ENV services_conf=$services_conf
COPY --from=build /tmp/orchestrate/${services_conf_filename}.ctmpl ${services_conf}.ctmpl
# Install libraries required by service
COPY --from=build /tmp/python-zephir/zephir /usr/lib/python3/dist-packages/zephir
COPY --from=build /tmp/messages-api/messages /srv/messages
# Manage container with ContainerPilot
CMD ["/usr/local/bin/containerpilot", "-config", "/etc/containerpilot.json5"]
COPY --from=build /tmp/orchestrate/containerpilot.json5 /etc/containerpilot.json5
##
## Service specific
##
# Library
COPY src/python/zone /usr/lib/python3/dist-packages/zone
# Controller
COPY migrations /migrations
RUN mkdir -p /srv/bin
COPY scripts /srv/bin
RUN chmod +x /srv/bin/*
FROM zephir_zone-manager:latest
RUN apt-get update -y && apt-get install -y \
python3-coverage \
python3-pytest \
python3-pytest-cov
COPY test /root/test
-- Deploy servermodel:servermodel_schema to pg
BEGIN;
-- Machine table creation
CREATE TABLE Machine (
MachineId SERIAL PRIMARY KEY,
MachineName VARCHAR(255) NOT NULL,
MachineServers INTEGER
);
-- Site table creation
CREATE TABLE Site (
SiteId SERIAL PRIMARY KEY,
SiteName VARCHAR(255) NOT NULL,
SiteDescription VARCHAR(255) NOT NULL,
SiteParentId INTEGER,
FOREIGN KEY (SiteParentId) REFERENCES Site(SiteId)
);
-- Zone table creation
CREATE TABLE Zone (
ZoneId SERIAL PRIMARY KEY,
ZoneName VARCHAR(255) NOT NULL,
ZoneCIDR VARCHAR(255) NOT NULL,
SiteId INTEGER,
FOREIGN KEY (SiteId) REFERENCES Site(SiteId)
);
COMMIT;
-- Revert servermodel:servermodel_schema from pg
BEGIN;
DROP SCHEMA zone CASCADE;
COMMIT;
[core]
engine = pg
# plan_file = sqitch.plan
# top_dir = .
[engine "pg"]
# target = db:pg:
registry = sqitch_zone
# client = psql
%syntax-version=1.0.0
%project=zone
zone_schema 2017-10-09T09:31:30Z Equipe EOLE <eole@ac-dijon.fr> # Definition du schema de base pour le domaine Zone
-- Verify servermodel:servermodel_schema on pg
BEGIN;
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'zone';
ROLLBACK;
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from autobahn.wamp.exception import ApplicationError
from zephir.controller import ZephirCommonController, run
from zephir.wamp import register
from zone.site import Site
EVENT_QUEUE = 'v1.zone.site.event'
class SiteController(ZephirCommonController):
def __init__(self, *args, **kwargs):
# Initialise self.config
super().__init__(*args, **kwargs)
self.site = Site()
self.conn = None
@register('v1.zone.site.create', notification_uri=EVENT_QUEUE, database=True)
async def create_site(self,
cursor,
sitename: str,
sitedescription: str,
parentsiteid: int):
"""
Handler pour le message 'v1.site.create'
Voir la documentation du message https://dev-eole.ac-dijon.fr/doc/zephir/messages/v1/servermodel.create.html
"""
try:
siteid = self.site.create_site(cursor,
sitename,
sitedescription,
parentsiteid)
except Exception as err:
raise ApplicationError(u'site.create.error', reason=str(err))
else:
result = dict(siteid=siteid)
return result
@register('v1.zone.site.describe', notification_uri=EVENT_QUEUE, database=True)
async def describe_site(self, cursor, siteid: int):
"""
returns the details of the site
:parameters siteid: the site requested id
"""
site = self.site.describe_site(cursor, siteid)
result = dict(site=site)
return result
@register('v1.zone.site.list', notification_uri=EVENT_QUEUE, database=True)
async def list_site(self, cursor):
"""
Handler pour le message 'v1.zone.site.list'
Voir la documentation du message https://dev-eole.ac-dijon.fr/doc/zephir/messages/v1/zone.site.list.html
"""
sites = list(self.site.list_site(cursor))
result = dict(sites=sites)
return result
if __name__ == '__main__':
run(SiteController)
from .lib import Site
__all__ = ['Site']
class SiteError(Exception):
pass
from zephir.i18n import _
from .query import (insert_site, fetch_site, describe_site, erase_site)
class Site:
def create_site(self, cursor,
sitename,
sitedescription,
parentsiteid):
return insert_site(cursor, sitename, sitedescription, parentsiteid)[0]
def describe_site(self, cursor, siteid):
return describe_site(cursor, siteid)
def list_site(self, cursor):
return fetch_site(cursor)
def erase_site(self, cursor):
erase_site(cursor)
from zephir.i18n import _
from .error import SiteError
"""insert site
"""
SITE_INSERT = """
INSERT INTO site (sitename, sitedescription, siteparentid)
VALUES (%s, %s, %s)
RETURNING siteid
"""
"""select all sites
"""
SITE_SELECT_ALL = """
SELECT siteid, sitename, sitedescription, siteparentid FROM site
"""
"""select only one site
"""
SITE_SELECT_ONE = """
SELECT siteid, sitename, sitedescription, siteparentid FROM site
WHERE siteid=%s
"""
#"""
#"""
#SITE_UPDATE = """
#"""
#
#"""
#"""
#SITE_DELETE = """
#"""
"""erase all data in site table
"""
ERASE_SITE = """
DELETE FROM site
"""
def insert_site(cursor, sitename: str, sitedescription: str, siteparentid: int):
cursor.execute(SITE_INSERT, (sitename, sitedescription, siteparentid))
return cursor.fetchone()
def fetch_site(cursor):
cursor.execute(SITE_SELECT_ALL)
return cursor.fetchall()
def describe_site(cursor, siteid: int):
cursor.execute(SITE_SELECT_ONE, (siteid, ))
result = cursor.fetchone()
if result is None:
raise SiteError(_("unable to find site by id {}").format(siteid))
return result
def erase_site(cursor):
cursor.execute(ERASE_SITE)
from zephir.config import ServiceConfig
from zephir.database import connect
from zone.site import Site
def setup_module(module):
CONF = ServiceConfig()
CONN = connect(CONF)
module.CONN = CONN
def teardown_module(module):
module.CONN.rollback()
module.CONN.close()
def setup_function(function):
site = Site()
with CONN.cursor() as cursor:
site.erase_site(cursor)
def test_create_site():
site = Site()
with CONN.cursor() as cursor:
siteid1 = site.create_site(cursor, 'nom du site 1', 'description du site 1', None)
siteid2 = site.create_site(cursor, 'nom du site 2', 'description du site 2', None)
assert siteid2 - siteid1 == 1
def test_list_site():
site = Site()
sites = range(1,11)
with CONN.cursor() as cursor:
siteids = [site.create_site(cursor, 'nom du site {}'.format(i),
'description du site {}'.format(i),
None)
for i in sites]
site_list = site.list_site(cursor)
assert site_list == [(serial, 'nom du site {}'.format(i), 'description du site {}'.format(i), None) for serial, i in zip(siteids, sites)]
def test_describe_site():
site = Site()
with CONN.cursor() as cursor:
siteid = site.create_site(cursor, 'nom du site', 'description du site', None)
site_description = site.describe_site(cursor, siteid)
assert site_description == (siteid, 'nom du site', 'description du site', None)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment