forked from ckan/ckan-packaging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckan-package
executable file
·111 lines (88 loc) · 3.74 KB
/
ckan-package
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
#!/usr/bin/env python
import subprocess
import re
import os
import sys
import argparse
def set_env_vars(version, iteration, datapusher='y', ansible_verbose='vv'):
os.environ['CKAN_PACKAGE_VERSION'] = version
os.environ['CKAN_PACKAGE_ITERATION'] = iteration
os.environ['CKAN_PACKAGE_DATAPUSHER'] = datapusher
os.environ['CKAN_PACKAGE_ANSIBLE_VERBOSE'] = ansible_verbose
def clear_env_vars():
for key in ['CKAN_PACKAGE_VERSION',
'CKAN_PACKAGE_ITERATION',
'CKAN_PACKAGE_DATAPUSHER',
'CKAN_PACKAGE_ANSIBLE_VERBOSE']:
if key in os.environ:
del os.environ[key]
def run(target=None):
status_trusty = None
status_xenial = None
status_bionic = None
print 'Checking Vagrant machines status...'
out = subprocess.check_output(['vagrant', 'status'])
if target is None or target == 'trusty':
status_trusty = re.search('trusty\s*(\w+)', out).group(1)
print 'Machine "trusty" is ' + status_trusty
if target is None or target == 'xenial':
status_xenial = re.search('xenial\s*(\w+)', out).group(1)
print 'Machine "xenial" is ' + status_xenial
if target is None or target == 'bionic':
status_bionic = re.search('bionic\s*(\w+)', out).group(1)
print 'Machine "bionic" is ' + status_bionic
if (status_trusty and status_trusty != 'running') or \
(status_xenial and status_xenial != 'running') or \
(status_bionic and status_bionic != 'running'):
print 'Starting up machine(s)'
command = ['vagrant', 'up', '--provision']
else:
print 'Machine(s) already started, provisioning'
command = ['vagrant', 'provision']
if target:
command.append(target)
subprocess.call(command)
if __name__ == '__main__':
description = '''Builds CKAN deb packages.
This script essentially sets up the necessary env vars and calls `vagrant up`
or `vagrant provision` as appropiate.'''
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-v', '--version',
help='''The CKAN branch or tag to build, eg master, dev-v2.6, release-v2.5.3.
If not provided you will be propmt for it''')
parser.add_argument('-i', '--iteration',
help='''The iteration number to add to the package name.
If not provided you will be propmt for it''')
parser.add_argument('-d', '--datapusher',
action='store_true',
default='y',
help='''Whether to add the DataPusher to the package,
defaults to true''')
parser.add_argument('-t', '--target',
help='''The distribution to target (trusty or xenial or bionic).
If omitted, both are built''')
parser.add_argument('-a', '--ansible-verbose',
default='vv',
help='Ansible verbosity level, defaults to "vv"')
def _check_arg(args, var, prompt):
if getattr(args, var):
return getattr(args, var)
var = raw_input('{0}:'.format(prompt))
if not var:
print('Please provide a value for the {0}'.format(prompt))
sys.exit(1)
return var
args = parser.parse_args()
version = _check_arg(args, 'version', 'CKAN Version (branch or tag)')
iteration = _check_arg(args, 'iteration', 'Iteration')
datapusher = args.datapusher
target = args.target
if target and target not in ('trusty', 'xenial', 'bionic'):
print 'Wrong target: ' + target
sys.exit(1)
ansible_verbose = args.ansible_verbose
set_env_vars(version, iteration, datapusher, ansible_verbose)
try:
run(target)
finally:
clear_env_vars()