Skip to content

Commit 23f6c30

Browse files
authored
Merge pull request #15 from lucj/master
Use ContainerPilot 3.3.0 and Consul 0.9.0
2 parents 69cc5e5 + 007a836 commit 23f6c30

File tree

4 files changed

+119
-44
lines changed

4 files changed

+119
-44
lines changed

Dockerfile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ RUN curl -Ls -o get-pip.py https://bootstrap.pypa.io/get-pip.py && \
2121
mock==2.0.0
2222

2323
# Add consul agent
24-
RUN export CONSUL_VERSION=0.7.5 \
25-
&& export CONSUL_CHECKSUM=40ce7175535551882ecdff21fdd276cef6eaab96be8a8260e0599fadb6f1f5b8 \
24+
RUN export CONSUL_VERSION=1.0.6 \
25+
&& export CONSUL_CHECKSUM=bcc504f658cef2944d1cd703eda90045e084a15752d23c038400cf98c716ea01 \
2626
&& curl --retry 7 --fail -vo /tmp/consul.zip "https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip" \
2727
&& echo "${CONSUL_CHECKSUM} /tmp/consul.zip" | sha256sum -c \
2828
&& unzip /tmp/consul -d /usr/local/bin \
2929
&& rm /tmp/consul.zip \
3030
&& mkdir -p /opt/consul/config
3131

3232
# Add ContainerPilot and set its configuration file path
33-
ENV CONTAINERPILOT_VER 2.7.2
34-
ENV CONTAINERPILOT file:///etc/containerpilot.json
35-
RUN export CONTAINERPILOT_CHECKSUM=e886899467ced6d7c76027d58c7f7554c2fb2bcc \
33+
ENV CONTAINERPILOT_VER 3.7.0
34+
ENV CONTAINERPILOT /etc/containerpilot.json5
35+
RUN export CONTAINERPILOT_CHECKSUM=b10b30851de1ae1c095d5f253d12ce8fe8e7be17 \
3636
&& curl -Lso /tmp/containerpilot.tar.gz \
3737
"https://github.com/joyent/containerpilot/releases/download/${CONTAINERPILOT_VER}/containerpilot-${CONTAINERPILOT_VER}.tar.gz" \
3838
&& echo "${CONTAINERPILOT_CHECKSUM} /tmp/containerpilot.tar.gz" | sha1sum -c \
@@ -44,11 +44,8 @@ ENV MONGO_SECONDARY_CATCHUP_PERIOD 8
4444
ENV MONGO_STEPDOWN_TIME 60
4545
ENV MONGO_ELECTION_TIMEOUT 30
4646

47-
# configure ContainerPilot and MySQL
47+
# Configure ContainerPilot and Mongo
4848
COPY etc/* /etc/
4949
COPY bin/* /usr/local/bin/
5050

51-
ENTRYPOINT ["containerpilot", "mongod"]
52-
53-
# Define CMD so the name of the replicaset can be overridden in the compose file
54-
CMD ["--replSet=joyent"]
51+
ENTRYPOINT ["containerpilot"]

bin/manage.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import struct
99
import sys
1010
import time
11+
import datetime
12+
import subprocess
13+
import manta
1114

1215
from functools import wraps
1316

@@ -84,6 +87,12 @@ def get_environ(key, default):
8487
MONGO_SECONDARY_CATCHUP_PERIOD=int(get_environ('MONGO_SECONDARY_CATCHUP_PERIOD', 8))
8588
MONGO_ELECTION_TIMEOUT=int(get_environ('MONGO_ELECTION_TIMEOUT', 30))
8689

90+
# Manta related information
91+
MANTA_URL=get_environ('MANTA_URL','https://us-east.manta.joyent.com')
92+
MANTA_USER=get_environ('MANTA_USER','')
93+
MANTA_KEY_ID=get_environ('MANTA_KEY_ID','')
94+
MANTA_TLS_INSECURE=bool(get_environ('MANTA_TLS_INSECURE',True))
95+
8796
# ---------------------------------------------------------
8897
# Top-level functions called by ContainerPilot or forked by this program
8998

@@ -155,14 +164,58 @@ def pre_stop():
155164

156165
return True
157166

167+
@debug
168+
def upload(filename):
169+
try:
170+
log.debug(filename)
171+
client = manta.MantaClient(url=MANTA_URL,
172+
account=MANTA_USER,
173+
signer=manta.SSHAgentSigner([MANTA_KEY_ID]),
174+
disable_ssl_certificate_validation=MANTA_TLS_INSECURE
175+
)
176+
client.put_object('/%s/stor/%s' % (MANTA_USER, filename), path='/tmp/%s' % filename)
177+
except Exception as e:
178+
log.debug(e)
179+
return False
180+
return True
181+
182+
@debug
183+
def backup():
184+
"""
185+
Run periodic mongodump and save backup to MANTA
186+
"""
187+
188+
hostname = socket.gethostname()
189+
ip = get_ip()
190+
local_mongo = MongoClient(ip, connect=False)
191+
192+
timestamp = datetime.datetime.utcnow().isoformat()
193+
dump = "dump-%s.gz" % timestamp
194+
195+
try:
196+
backup_output = subprocess.check_output([ 'mongodump', '--archive=/tmp/%s' % dump, '--gzip' ])
197+
log.debug(backup_output)
198+
199+
# Upload dump onto Manta
200+
#TODO: do not manage to upload to Manta
201+
# upload(dump)
202+
203+
# Delete local dump once uploaded
204+
# os.remove('/tmp/%s' % dump)
205+
except Exception as e:
206+
log.debug('Error during backup process');
207+
log.debug(e);
208+
return False
209+
210+
return True
211+
158212
@debug
159213
def health():
160214
"""
161215
Run a simple health check. Also acts as a check for whether the
162216
ContainerPilot configuration needs to be reloaded (if it's been
163217
changed externally).
164218
"""
165-
# TODO periodic mongodumps to Manta
166219

167220
hostname = socket.gethostname()
168221
ip = get_ip()

etc/containerpilot.json

Lines changed: 0 additions & 33 deletions
This file was deleted.

etc/containerpilot.json5

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
consul: "{{ if .CONSUL_AGENT }}localhost{{ else }}{{ if .CONSUL }}{{ .CONSUL }}{{ else }}consul{{ end }}{{ end }}:8500",
3+
logging: {
4+
level: "DEBUG",
5+
format: "text"
6+
},
7+
jobs: [
8+
{
9+
name: "preStart",
10+
exec: "python /usr/local/bin/manage.py"
11+
},
12+
{
13+
name: "preStop",
14+
exec: "python /usr/local/bin/manage.py pre_stop"
15+
},
16+
{{ if .CONSUL_AGENT }}
17+
{
18+
name: "consul-agent",
19+
exec: ["/usr/local/bin/consul", "agent",
20+
"-data-dir=/opt/consul/data",
21+
"-config-dir=/opt/consul/config",
22+
"-rejoin",
23+
"-retry-join", "{{ if .CONSUL }}{{ .CONSUL }}{{ else }}consul{{ end }}",
24+
"-retry-max", "10",
25+
"-retry-interval", "10s"],
26+
restarts: "unlimited"
27+
},
28+
{{ end }}
29+
{
30+
name: "mongodb-replicaset",
31+
port: 27017,
32+
exec: "mongod --replSet={{ if .REPLICASET }}{{ .REPLICASET }}{{ else }}joyent{{ end }}",
33+
when: {
34+
source: "preStart",
35+
once: "exitSuccess"
36+
},
37+
health: {
38+
exec: "python /usr/local/bin/manage.py health",
39+
interval: 10,
40+
ttl: 25
41+
},
42+
},
43+
{
44+
name: "onchange-mongo",
45+
exec: "python /usr/local/bin/manage.py on_change",
46+
when: {
47+
source: "watch.mongodb-replicaset",
48+
each: "changed"
49+
}
50+
},
51+
],
52+
watches: [
53+
{
54+
name: "mongodb-replicaset",
55+
interval: 2
56+
}
57+
]
58+
}

0 commit comments

Comments
 (0)