Merge "Chronos bootstrap tasks"
This commit is contained in:
commit
c05b25f457
60
kolla_mesos/chronos.py
Normal file
60
kolla_mesos/chronos.py
Normal file
@ -0,0 +1,60 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# TODO(nihilifer): Contribute to https://github.com/mesosphere/dcos-cli and
|
||||
# remove this module when possible.
|
||||
|
||||
import json
|
||||
from oslo_config import cfg
|
||||
import requests
|
||||
from six.moves.urllib import parse
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class Client(object):
|
||||
"""Class for talking to the Chronos server.
|
||||
|
||||
:param chronos_url: the base URL for the Chronos server
|
||||
:type chronos_url: str
|
||||
:param timeout: timeout for request to the Chronos server
|
||||
:type timeout: int
|
||||
"""
|
||||
|
||||
def _create_url(self, path):
|
||||
"""Create URL for the specific Chronos API resource.
|
||||
|
||||
:param path: the path to the Chronos API resource
|
||||
:type path: str
|
||||
"""
|
||||
return parse.urljoin(CONF.chronos.host, path)
|
||||
|
||||
def add_job(self, job_resource):
|
||||
"""Add job to Chronos.
|
||||
|
||||
:param job_resource: data about job to run on Chronos
|
||||
:type job_resource: dict
|
||||
"""
|
||||
url = self._create_url('scheduler/iso8601')
|
||||
response = requests.post(url, data=json.dumps(job_resource),
|
||||
timeout=CONF.chronos.timeout,
|
||||
headers={'Content-Type': 'application/json'})
|
||||
|
||||
assert response.status_code in [200, 204]
|
||||
|
||||
def get_jobs(self):
|
||||
"""Get list of running jobs in Chronos"""
|
||||
url = self._create_url('scheduler/jobs')
|
||||
response = requests.get(url, timeout=CONF.chronos.timeout)
|
||||
|
||||
return response.json()
|
@ -27,6 +27,7 @@ from six.moves import configparser
|
||||
from six.moves import cStringIO
|
||||
import yaml
|
||||
|
||||
from kolla_mesos import chronos
|
||||
from kolla_mesos.common import file_utils
|
||||
from kolla_mesos.common import jinja_utils
|
||||
from kolla_mesos.common import zk_utils
|
||||
@ -59,6 +60,7 @@ class KollaWorker(object):
|
||||
LOG.debug("Kolla-Mesos base directory: " + self.base_dir)
|
||||
self.required_vars = {}
|
||||
self.marathon_client = marathon.create_client()
|
||||
self.chronos_client = chronos.Client()
|
||||
|
||||
def setup_working_dir(self):
|
||||
"""Creates a working directory for use while building"""
|
||||
@ -252,6 +254,9 @@ class KollaWorker(object):
|
||||
else:
|
||||
cmd = 'curl -X POST "%s/scheduler/iso8601" -d @"%s" %s' % (
|
||||
chronos_api, app_path, content_type)
|
||||
with open(app_path, 'r') as app_file:
|
||||
job_resource = json.load(app_file)
|
||||
self.chronos_client.add_job(job_resource)
|
||||
LOG.info(cmd)
|
||||
|
||||
|
||||
|
81
kolla_mesos/tests/test_chronos.py
Normal file
81
kolla_mesos/tests/test_chronos.py
Normal file
@ -0,0 +1,81 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
import requests_mock
|
||||
|
||||
from kolla_mesos import chronos
|
||||
from kolla_mesos.tests import base
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
EXAMPLE_CHRONOS_JOB = {
|
||||
"name": "/keystone-bootstrap",
|
||||
"mem": 32.0,
|
||||
"cpus": 0.3,
|
||||
"container": {
|
||||
"type": "DOCKER",
|
||||
"image": "/--kolla_ansible:",
|
||||
"network": "HOST"
|
||||
},
|
||||
"environmentVariables": [
|
||||
{"name": "KOLLA_CONFIG_STRATEGY", "value": "COPY_ONCE"},
|
||||
{"name": "KOLLA_CONFIG", "value": {
|
||||
"command": "/usr/local/bin/kolla_mesos_start",
|
||||
"config_files": [{
|
||||
"source": ("zk://localhost:2181/kolla/config/mariadb/mariadb/"
|
||||
"kolla_mesos_start.py"),
|
||||
"dest": "/usr/local/bin/kolla_mesos_start",
|
||||
"owner": "root",
|
||||
"perm": "0755"
|
||||
}]
|
||||
}},
|
||||
{"name": "KOLLA_GROUP", "value": "keystone"},
|
||||
{"name": "KOLLA_ROLE", "value": "keystone_bootstrap"},
|
||||
{"name": "KOLLA_ZK_HOSTS", "value": "localhost:2181"}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
class TestClient(base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestClient, self).setUp()
|
||||
self.client = chronos.Client()
|
||||
CONF.set_override('host', 'http://localhost:4400', group='chronos')
|
||||
CONF.set_override('timeout', 5, group='chronos')
|
||||
|
||||
def test_create_client(self):
|
||||
self.assertIsInstance(self.client, chronos.Client)
|
||||
|
||||
def test_create_url(self):
|
||||
url = self.client._create_url('test')
|
||||
self.assertEqual(url, 'http://localhost:4400/test')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_add_job(self, req_mock):
|
||||
req_mock.post('http://localhost:4400/scheduler/iso8601')
|
||||
self.client.add_job(EXAMPLE_CHRONOS_JOB)
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_get_jobs(self, req_mock):
|
||||
req_mock.get('http://localhost:4400/scheduler/jobs',
|
||||
json=[EXAMPLE_CHRONOS_JOB])
|
||||
response = self.client.get_jobs()
|
||||
|
||||
self.assertIsInstance(response, list)
|
||||
|
||||
job = response[0]
|
||||
|
||||
self.assertIsInstance(job, dict)
|
||||
self.assertDictEqual(job, EXAMPLE_CHRONOS_JOB)
|
@ -11,3 +11,4 @@ netifaces>=0.10.4
|
||||
oslo.config>=2.7.0 # Apache-2.0
|
||||
oslo.utils>=2.8.0 # Apache-2.0
|
||||
PyYAML>=3.1.0
|
||||
six>=1.9.0
|
||||
|
@ -7,6 +7,7 @@ mock>=1.2
|
||||
oslo.log>=1.12.0 # Apache-2.0
|
||||
oslotest>=1.10.0 # Apache-2.0
|
||||
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0
|
||||
requests-mock>=0.7.0 # Apache-2.0
|
||||
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2
|
||||
testrepository>=0.0.18
|
||||
testscenarios>=0.4
|
||||
|
Loading…
Reference in New Issue
Block a user