1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#!/usr/bin/env python3
#
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""CI Automation: mojo spec (branch & stage) watcher."""
import argparse
import logging
import os
import sys
import subprocess
from ci_automation.branch import get_identifier
def deploy(args):
"""Call the mojo wrapper script with the given arguments.
Swallow any failures.
"""
mojo_cmd = os.path.join(os.path.dirname(__file__), 'mojo.py')
return subprocess.call([mojo_cmd] + args)
def main():
parser = argparse.ArgumentParser(
description=('Deploys mojo spec as a new environment if it is not '
'yet deployed.')
)
parser.add_argument('--branch', required=True)
parser.add_argument('--stage', required=True)
# Parse known arguments, collect (and augment) the rest as 'mojo_args'.
args, mojo_args = parser.parse_known_args()
mojo_args.extend([
'--branch', args.branch,
'--stage', args.stage
])
logging.basicConfig(
format='%(asctime)s %(name)s %(levelname)s: %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Optional remote logging via logstash.
logstash_host = os.environ.get('CI_LOGSTASH_HOST')
if logstash_host is not None:
try:
import logstash
except ImportError:
print('Follow the README instructions for installing '
'python-logstash')
return 1
else:
logger.addHandler(
logstash.LogstashHandler(logstash_host, 5959, 1))
# Deploy new environment if the stage identifier isn't recorded.
idenfifier_dir = os.path.expanduser('~/ci-cd-identifiers/')
if not os.path.exists(idenfifier_dir):
os.makedirs(idenfifier_dir)
info, identifier = get_identifier(args.branch, args.stage)
identifier_path = os.path.join(idenfifier_dir, identifier)
if os.path.exists(identifier_path):
return 0
# Register deployment identifier regardless its deployment status.
# We don't want to deploy it again.
with open(identifier_path, 'w') as fd:
fd.write(info + '\n')
# `mojo.py` prints additional information to stdout, which is only
# recorded locally, for now ...
status = 'SUCCEEDED' if deploy(mojo_args) == 0 else 'FAILED'
# We only log a sucint but descriptive message about the deployment
# status. This content will be shipped to logstash and be handled
# from there (recording, stats, pagerduty, etc).
extra = {
'service': 'ci/cd',
'deployment_status': status,
'deployment_branch': args.branch,
'deployment_name': os.path.dirname(args.stage),
'deployment_identifier': identifier,
'deployment_info': info,
}
logger.info(
'Deploy attempt for %s %s:\n====\n%s\n====',
identifier, status, info, extra=extra)
# Never fail as far as cron is concerned.
return 0
if __name__ == '__main__':
sys.exit(main())
|