0001-agent-deploy-twice-in-case-of-new-services-7550.patch
hobo/agent/common/management/commands/hobo_deploy.py | ||
---|---|---|
33 | 33 |
option_list = BaseCommand.option_list + ( |
34 | 34 |
make_option('--ignore-timestamp', dest='ignore_timestamp', |
35 | 35 |
action="store_true", default=False), |
36 |
make_option('--exists', dest='exists', |
|
37 |
action="store_true", default=False), |
|
36 | 38 |
) |
37 | 39 | |
38 |
def handle(self, base_url, json_filename, ignore_timestamp, *args, **kwargs): |
|
40 |
def handle(self, base_url, json_filename, ignore_timestamp, exists, *args, **kwargs):
|
|
39 | 41 |
if json_filename == '-': |
40 | 42 |
hobo_environment = json.load(sys.stdin) |
41 | 43 |
else: |
... | ... | |
43 | 45 |
me = [x for x in hobo_environment.get('services') if x.get('base_url') == base_url][0] |
44 | 46 |
domain = urlparse.urlparse(me.get('base_url')).netloc.split(':')[0] |
45 | 47 | |
48 |
if exists: |
|
49 |
# exit with 0 if the site exists already, 1 if it doesn't. |
|
50 |
try: |
|
51 |
TenantMiddleware.get_tenant_by_hostname(domain) |
|
52 |
sys.exit(0) |
|
53 |
except TenantNotFound: |
|
54 |
sys.exit(1) |
|
55 | ||
46 | 56 |
try: |
47 | 57 |
tenant = TenantMiddleware.get_tenant_by_hostname(domain) |
48 | 58 |
except TenantNotFound: |
hobo/agent/worker/celery.py | ||
---|---|---|
1 | 1 |
from __future__ import absolute_import |
2 |
import datetime |
|
3 |
import time |
|
2 | 4 | |
3 | 5 |
from celery import Celery |
4 | 6 |
from kombu.common import Broadcast |
... | ... | |
10 | 12 |
CELERY_TASK_SERIALIZER='json', |
11 | 13 |
CELERY_ACCEPT_CONTENT=['json'], |
12 | 14 |
CELERY_RESULT_SERIALIZER='json', |
13 |
CELERY_QUEUES=(Broadcast('broadcast_tasks'), ) |
|
15 |
CELERY_QUEUES=(Broadcast('broadcast_tasks'), |
|
16 |
Broadcast('hobo_broadcast_tasks')) |
|
14 | 17 |
) |
15 | 18 | |
16 | 19 |
@app.task(name='hobo-deploy', bind=True) |
17 | 20 |
def deploy(self, environment): |
18 |
services.deploy(environment) |
|
21 |
has_new_service = services.deploy(environment) |
|
22 |
if has_new_service: |
|
23 |
# in case of a new service a "new-site" broadcast call is made and will |
|
24 |
# get caught by the agent running on the master hobo site. |
|
25 |
app.send_task('new-site', |
|
26 |
(environment,), |
|
27 |
expires=settings.BROKER_TASK_EXPIRES, |
|
28 |
queue='hobo_broadcast_tasks') |
|
29 | ||
30 |
@app.task(name='new-site', bind=True) |
|
31 |
def new_site(self, environment): |
|
32 |
# we send a request for a new deployment, so existing services have a |
|
33 |
# chance to take their new sibling in consideration. |
|
34 |
timestamp = datetime.datetime.now() |
|
35 |
environment['timestamp'] = str(time.mktime(timestamp.timetuple()) + timestamp.microsecond/1e6) |
|
36 |
app.send_task('hobo-deploy', |
|
37 |
(environment,), |
|
38 |
expires=settings.BROKER_TASK_EXPIRES, |
|
39 |
queue='broadcast_tasks') |
hobo/agent/worker/services.py | ||
---|---|---|
50 | 50 |
'''Return True if site is uptodate''' |
51 | 51 |
return False |
52 | 52 | |
53 |
def exists(self, environment): |
|
54 |
cmd_process = subprocess.Popen( |
|
55 |
self.service_manage_cmd + ' hobo_deploy ' + ' --exists ' + self.base_url + ' -', |
|
56 |
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
57 |
stdout = cmd_process.communicate(input=json.dumps(environment)) |
|
58 |
return (cmd_process.returncode != 1) |
|
59 | ||
53 | 60 |
def execute(self, environment): |
54 | 61 |
cmd_process = subprocess.Popen(self.service_manage_cmd + ' hobo_deploy ' + self.base_url + ' -', |
55 | 62 |
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
... | ... | |
106 | 113 |
continue |
107 | 114 |
service_classes[service.service_id] = service |
108 | 115 | |
116 |
has_new_service = False |
|
109 | 117 |
for service in environment.get('services', []): |
110 | 118 |
service_id = service.get('service-id') |
111 | 119 |
if not service_id in service_classes: |
... | ... | |
117 | 125 |
if service_obj.check_timestamp(hobo_timestamp): |
118 | 126 |
logger.debug('skipping uptodate site: %r', service_obj) |
119 | 127 |
continue |
128 |
existing_service = service_obj.exists(environment) |
|
120 | 129 |
service_obj.execute(environment) |
130 |
if not existing_service and service_obj.exists(environment): |
|
131 |
has_new_service = service_obj.base_url |
|
132 | ||
133 |
return has_new_service |
hobo/agent/worker/settings.py | ||
---|---|---|
3 | 3 |
# AMQP message broker |
4 | 4 |
BROKER_URL = 'amqp://' |
5 | 5 | |
6 |
# Task expiry time, as seconds after task publish |
|
7 |
BROKER_TASK_EXPIRES = 120 |
|
8 | ||
6 | 9 |
# It's possible to limit agents to particular applications, or particular |
7 | 10 |
# hostnames, using the AGENT_HOST_PATTERNS configuration variable. |
8 | 11 |
# |
9 |
- |