Skip to content

Commit

Permalink
update testing and manage.py application for ContainerPilot 3.0.0-RC1
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Jun 1, 2017
1 parent c4be96c commit 9202b92
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 49 deletions.
58 changes: 23 additions & 35 deletions bin/manager/containerpilot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
""" autopilotpattern/mysql ContainerPilot configuraton wrapper """
import json
import os
import signal
import subprocess

import json5

from manager.utils import debug, env, to_flag, log, UNASSIGNED

# pylint: disable=invalid-name,no-self-use,dangerous-default-value
Expand All @@ -19,38 +22,20 @@ def __init__(self):

def load(self, envs=os.environ):
"""
Parses the ContainerPilot config file and interpolates the
environment into it the same way that ContainerPilot does.
The `state` attribute will be populated with the state
derived from the configuration file and not the state known
by Consul.
Fetches the ContainerPilot config file and asks ContainerPilot
to render it out so that all environment variables have been
interpolated.
"""
self.path = env('CONTAINERPILOT', None, envs,
lambda x: x.replace('file://', ''))
with open(self.path, 'r') as f:
cfg = f.read()

# remove templating so that we can parse it as JSON; we'll
# override the attributes directly in the resulting dict
cfg = cfg.replace('[{{ if .CONSUL_AGENT }}', '[')
cfg = cfg.replace('}{{ end }}', '}')

# remove templating for SERVICE_NAME
service_name = env('SERVICE_NAME', 'mysql')
cfg = cfg.replace('{{ if .SERVICE_NAME }}{{ .SERVICE_NAME }}{{ else }}mysql{{ end }}',service_name)
config = json.loads(cfg)

if env('CONSUL_AGENT', False, envs, to_flag):
config['consul'] = 'localhost:8500'
cmd = config['coprocesses'][0]['command']
host_cfg_idx = cmd.index('-retry-join') + 1
cmd[host_cfg_idx] = env('CONSUL', 'consul', envs)
config['coprocesses'][0]['command'] = cmd
else:
config['consul'] = env('CONSUL', 'consul', envs,
fn='{}:8500'.format)
config['coprocesses'] = []
self.path = env('CONTAINERPILOT', None, envs)
try:
cfg = subprocess.check_output(['containerpilot', '-config',
self.path, '-template'],
env=envs.copy())
except (subprocess.CalledProcessError, OSError) as ex:
log.error('containerpilot -template returned error: %s', ex)
raise(ex)

config = json5.loads(cfg)
self.config = config

@debug(log_output=True)
Expand All @@ -61,21 +46,24 @@ def update(self):
"""
if self.state == UNASSIGNED:
return False
if self.state and self.config['services'][0]['name'] != self.state:
self.config['services'][0]['name'] = self.state
if self.state and self.config['jobs'][1]['name'] != self.state:
self.config['jobs'][1]['name'] = self.state
self._render()
return True
return False

@debug
def _render(self):
""" Writes the current config to file. """
new_config = json.dumps(self.config)
new_config = json5.dumps(self.config)
with open(self.path, 'w') as f:
log.info('rewriting ContainerPilot config: %s', new_config)
f.write(new_config)

def reload(self):
""" Force ContainerPilot to reload its configuration """
log.info('Reloading ContainerPilot configuration.')
os.kill(1, signal.SIGHUP)
try:
subprocess.check_output(['containerpilot', '-reload'])
except subprocess.CalledProcessError:
log.info("call to 'containerpilot -reload' failed")
19 changes: 7 additions & 12 deletions bin/test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from datetime import datetime, timedelta
import fcntl
import json
import logging
import os
import tempfile
import unittest

# pylint: disable=import-error
import consul as pyconsul
import json5
import mock

import manage
Expand Down Expand Up @@ -835,25 +835,20 @@ def test_parse_with_consul_agent(self):
self.environ['CONSUL_AGENT'] = '1'
cp = ContainerPilot()
cp.load(envs=self.environ)

self.assertEqual(cp.config['consul'], 'localhost:8500')
cmd = cp.config['coprocesses'][0]['command']
cmd = cp.config['jobs'][4]['exec']
host_cfg_idx = cmd.index('-retry-join') + 1
self.assertEqual(cmd[host_cfg_idx], 'my.consul.example.com')
self.assertEqual(cp.state, UNASSIGNED)

def test_parse_without_consul_agent(self):
self.environ['CONSUL_AGENT'] = '0'
cp = ContainerPilot()
cp.load(envs=self.environ)
self.assertEqual(cp.config['consul'], 'my.consul.example.com:8500')
self.assertEqual(cp.config['coprocesses'], [])
self.assertEqual(cp.state, UNASSIGNED)

self.environ['CONSUL_AGENT'] = ''
cp = ContainerPilot()
cp.load(envs=self.environ)
self.assertEqual(cp.config['consul'], 'my.consul.example.com:8500')
self.assertEqual(cp.config['coprocesses'], [])
self.assertFalse('consul-agent' in
[job['name'] for job in cp.config['jobs']])
self.assertEqual(cp.state, UNASSIGNED)

def test_update(self):
Expand All @@ -873,9 +868,9 @@ def test_update(self):
cp.state = PRIMARY
cp.update()
with open(temp_file.name, 'r') as updated:
config = json.loads(updated.read())
config = json5.loads(updated.read())
self.assertEqual(config['consul'], 'localhost:8500')
cmd = config['coprocesses'][0]['command']
cmd = config['jobs'][4]['exec']
host_cfg_idx = cmd.index('-retry-join') + 1
self.assertEqual(cmd[host_cfg_idx], 'my.consul.example.com')

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ services:
dns:
- 127.0.0.1
labels:
- triton.cns.services=mysql-consul
- triton.cns.services=mc
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ integration-test:
-e MANTA_USER=$(MANTA_USER) \
-e MANTA_SUBUSER=$(MANTA_SUBUSER) \
-e MANTA_ROLE=$(MANTA_ROLE) \
-e CONSUL=mysql-consul.svc.$(TRITON_ACCOUNT).$(TRITON_DC).cns.joyent.com \
-e CONSUL=mc.svc.$(TRITON_ACCOUNT).$(TRITON_DC).cns.joyent.com \
$(SDC_KEYS_VOL) -w /src \
$(test_image):$(tag) python3 tests.py

Expand Down

0 comments on commit 9202b92

Please sign in to comment.