Skip to content

Commit 5ca39d1

Browse files
ravanellialinefm
authored andcommitted
Issue #1201 Build and push kimchi/wok packages to Jfrog repo
1 parent 7ced3a3 commit 5ca39d1

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

build_packages.py

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import argparse
2+
import yaml
3+
import shutil
4+
import sys
5+
import subprocess
6+
from subprocess import check_call, check_output, CalledProcessError
7+
from wok.plugins.kimchi.config import get_kimchi_version
8+
from wok.config import get_version
9+
10+
REPOS_LIST = ('production', 'staging')
11+
DISTROS_LIST = ('centos/8', 'fedora/31', 'ubuntu/19.10', 'debian/10', 'opensuse/15.1', 'all')
12+
JFROG_BASE = 'https://kimchi.jfrog.io/kimchi/'
13+
14+
HOMEWOK = '/tmp/wok/'
15+
HOMEKIMCHI = HOMEWOK + 'src/wok/plugins/kimchi/'
16+
17+
WOK = [
18+
'git clone https://github.com/kimchi-project/wok.git ' + HOMEWOK
19+
]
20+
21+
KIMCHI = [
22+
'mkdir -p ' + HOMEKIMCHI,
23+
'git clone https://github.com/kimchi-project/kimchi.git ' + HOMEKIMCHI ,
24+
]
25+
26+
PACKAGES = {}
27+
PACKAGES['wok'] = WOK
28+
PACKAGES['kimchi'] = KIMCHI
29+
BUILD = [['./autogen.sh', '--system'], ['make'], ['make','install']]
30+
31+
COMMANDS_OS = {
32+
'debian' : {
33+
'install' : 'apt install -y',
34+
'update' : 'apt update -y',
35+
'make' : ['make', 'deb'],
36+
'pk' : '.deb',
37+
'pip' : 'sudo -H pip3 install -r ' + HOMEKIMCHI + 'requirements-UBUNTU.txt',
38+
},
39+
'fedora' : {
40+
'install' : 'dnf install -y',
41+
'update' : 'dnf update -y',
42+
'make' : ['make', 'rpm'],
43+
'pk' : '.rpm',
44+
'pip' : 'sudo -H pip3 install -r ' + HOMEKIMCHI + 'requirements-FEDORA.txt',
45+
},
46+
'opensuse/LEAP' : {
47+
'install' : 'zypper install -y',
48+
'update' : 'zypper update -y',
49+
'make' : ['make', 'rpm'],
50+
'pk' : '.rpm',
51+
'pip' : 'sudo -H pip3 install -r ' + HOMEKIMCHI + 'requirements-OPENSUSE-LEAP.txt',
52+
},
53+
}
54+
55+
def usage():
56+
57+
'''
58+
# Handle parameters
59+
60+
@param repo string repository
61+
@param distro string distro
62+
@param user string JFROG user
63+
@param password string Token JFROG
64+
'''
65+
66+
parser = argparse.ArgumentParser(
67+
description='python install.py -r production -d rhel/7 -u username -p password ',
68+
)
69+
70+
parser.add_argument("-r", "--repo", choices=REPOS_LIST, required=True)
71+
parser.add_argument("-d", "--distro", choices=DISTROS_LIST, default="all")
72+
parser.add_argument("-u", "--user", help="Account name at %s. This account needs to be granted to write in \
73+
the repository." % (JFROG_BASE), metavar=("<username>"),required=True)
74+
parser.add_argument("-p", "--password", help="Token at %s. This token needs to be granted to write in."
75+
% (JFROG_BASE),metavar=("<password>"),required=True)
76+
77+
args = parser.parse_args()
78+
repo = args.repo
79+
80+
if args.distro == "all":
81+
distros = DISTROS_LIST
82+
distros.remove("all")
83+
else:
84+
distros = [args.distro]
85+
86+
return repo, distros, args.user, args.password
87+
88+
def run_cmd(command):
89+
90+
'''
91+
Run the given command using check_call and verify its return code.
92+
@param str command command to be executed
93+
'''
94+
95+
try:
96+
check_call(command.split())
97+
except CalledProcessError as e:
98+
print('An exception h:as occurred: {0}'.format(e))
99+
sys.exit(1)
100+
101+
def execute_cmd(list, step):
102+
103+
'''
104+
Execute the given commands using run_cmd function
105+
@param list list commands to be executed
106+
@param step str name of the comand to be executed
107+
'''
108+
print('Step: %s' % (step))
109+
for item in list:
110+
run_cmd(item)
111+
112+
def run_build(list, dir):
113+
'''
114+
Execute the given commands in other directory
115+
@param list list commands to be executed
116+
@param dir str directory path
117+
'''
118+
try:
119+
build = subprocess.Popen(list, cwd=dir)
120+
build.wait()
121+
except CalledProcessError as e:
122+
print('An exception has occurred: {0}'.format(e))
123+
sys.exit(1)
124+
125+
def curl_cmd(repo, distro_name, distro, package_name, user, password, path, component):
126+
'''
127+
Move package to JFROG repository
128+
@param str repo repo
129+
@param str distro_name distro name
130+
@param str distro distro name and version
131+
@param str package_name package name
132+
@param str user JFROG user
133+
@param str password JFROG password
134+
@param str path path to package
135+
@param str component component name
136+
'''
137+
138+
if distro_name == 'debian' or distro_bame == 'ubuntu':
139+
cmd = 'curl --silent -u%s:%s -XPUT \
140+
https://kimchi.jfrog.io/kimchi/%s/%s;deb.distribution=%s;deb.component=%s;deb.architecture=noarch -T %s' \
141+
% (user, password, distro, package_name, distro, component, path)
142+
elif distro_name == 'staging':
143+
cmd = 'curl --silent -u%s:%s -XPUT https://kimchi.jfrog.io/kimchi/staging/%s/ -T %s' \
144+
% (user, password, distro, path)
145+
else:
146+
cmd = 'curl --silent -u%s:%s -XPUT https://kimchi.jfrog.io/kimchi/%s/ -T %s' % (user, password, distro, path)
147+
148+
execute_cmd([cmd], 'Moving package to JFROG')
149+
150+
def install_dependencies(distro, pm):
151+
152+
'''
153+
Install package dependencies
154+
@param str distro distro name
155+
@param str pm package manager
156+
'''
157+
158+
packages = []
159+
for file in (HOMEWOK + 'dependencies.yaml', HOMEKIMCHI + 'dependencies.yaml' ):
160+
with open(file, 'r') as dep_file:
161+
packages_list = yaml.load(dep_file, Loader=yaml.Loader)
162+
if 'kimchi' in str(dep_file):
163+
new_distro = 'ubuntu'
164+
else:
165+
new_distro = distro
166+
packages.append(' '.join([str(elem) for elem in packages_list['development-deps']['common']]))
167+
packages.append(' '.join([str(elem) for elem in packages_list['development-deps'][new_distro]]))
168+
packages.append(' '.join([str(elem) for elem in packages_list['runtime-deps']['common']]))
169+
packages.append(' '.join([str(elem) for elem in packages_list['runtime-deps'][new_distro]]))
170+
171+
for package in packages:
172+
execute_cmd([COMMANDS_OS[pm]['install'] + ' ' + package], 'Installing necessary packages')
173+
174+
execute_cmd(['sudo -H pip3 install -r ' + HOMEWOK+ 'requirements-dev.txt'], 'Installing requirements')
175+
execute_cmd(['sudo -H pip3 install -r ' + HOMEKIMCHI+ 'requirements-dev.txt'], 'Installing requirements')
176+
177+
def main():
178+
179+
repo, distros, user, password = usage()
180+
kimchi_version = get_kimchi_version()
181+
wok_version = get_version()
182+
183+
for distro in distros:
184+
distro_name = distro.split("/")
185+
if distro_name[0] == 'ubuntu':
186+
pm = 'debian'
187+
else:
188+
pm = distro_name[0]
189+
190+
try:
191+
shutil.rmtree(HOMEWOK)
192+
except:
193+
pass
194+
195+
execute_cmd([COMMANDS_OS[pm]['update']], 'Updating system')
196+
execute_cmd(PACKAGES['wok'], 'Cloning Wok')
197+
execute_cmd(PACKAGES['kimchi'], 'Cloning Kimchi')
198+
install_dependencies(distro_name[0], pm)
199+
execute_cmd([COMMANDS_OS[pm]['pip']],'Installing Pip packages')
200+
201+
for item in BUILD:
202+
203+
run_build(item, HOMEWOK)
204+
run_build(item, HOMEKIMCHI)
205+
206+
run_build(COMMANDS_OS[pm]['make'], HOMEWOK)
207+
run_build(COMMANDS_OS[pm]['make'], HOMEKIMCHI)
208+
209+
wok_package = 'wok-' + wok_version + '.' + distro_name[0] + '.noarch' + COMMANDS_OS[pm]['pk']
210+
kimchi_package = 'kimchi-' + kimchi_version + '.noarch' + COMMANDS_OS[pm]['pk']
211+
curl_cmd(repo, distro_name[0], distro, wok_package, user, password, HOMEWOK + wok_package, 'wok')
212+
curl_cmd(repo, distro_name[0], distro, kimchi_package, user, password, HOMEKIMCHI + kimchi_package, 'kimchi')
213+
214+
print("All Good, check JFROG")
215+
216+
if __name__ == "__main__":
217+
main()

0 commit comments

Comments
 (0)