2020-03-05 00:47:55 +00:00
#!/usr/bin/env python3
2020-03-04 02:15:59 +00:00
import argparse
import subprocess
import os
2020-06-28 00:25:15 +00:00
def parse_args ( ) :
parser = argparse . ArgumentParser ( description = ' frappe_docker common CI elements ' , add_help = True )
2020-03-04 02:15:59 +00:00
2020-06-28 00:25:15 +00:00
parser . add_argument ( ' service ' , action = ' store ' , type = str , help = ' Name of the service to build: " erpnext " or " frappe " ' )
parser . add_argument ( ' -o ' , ' --tag-only ' , required = False , action = ' store_true ' , dest = ' tag_only ' , help = ' Only tag an image and push it. ' )
2020-03-04 20:27:53 +00:00
2020-06-28 00:25:15 +00:00
image_type = parser . add_mutually_exclusive_group ( required = True )
image_type . add_argument ( ' -a ' , ' --nginx ' , action = ' store_const ' , dest = ' image_type ' , const = ' nginx ' , help = ' Build the nginx + static assets image ' )
image_type . add_argument ( ' -s ' , ' --socketio ' , action = ' store_const ' , dest = ' image_type ' , const = ' socketio ' , help = ' Build the frappe-socketio image ' )
image_type . add_argument ( ' -w ' , ' --worker ' , action = ' store_const ' , dest = ' image_type ' , const = ' worker ' , help = ' Build the python environment image ' )
2020-03-04 02:15:59 +00:00
2020-06-28 00:25:15 +00:00
tag_type = parser . add_mutually_exclusive_group ( required = True )
tag_type . add_argument ( ' -g ' , ' --git-version ' , action = ' store ' , type = str , dest = ' version ' , help = ' The version number of service (i.e. " 11 " , " 12 " , etc.) ' )
tag_type . add_argument ( ' -t ' , ' --tag ' , action = ' store ' , type = str , dest = ' tag ' , help = ' The image tag (i.e. erpnext-worker:$TAG ) ' )
args = parser . parse_args ( )
return args
2020-03-04 02:15:59 +00:00
2020-06-27 18:14:13 +00:00
def git_version ( service , version , branch ) :
2020-06-28 00:25:15 +00:00
print ( f ' Pulling { service } v { version } ' )
subprocess . run ( f ' git clone https://github.com/frappe/ { service } --branch { branch } ' , shell = True )
cd = os . getcwd ( )
os . chdir ( os . getcwd ( ) + f ' / { service } ' )
subprocess . run ( ' git fetch --tags ' , shell = True )
# XX-beta becomes XX for tags search
version = version . split ( ' - ' ) [ 0 ]
2020-05-01 18:51:56 +00:00
2020-06-28 00:25:15 +00:00
version_tag = subprocess . check_output ( f ' git tag --list --sort=-version:refname " v { version } * " | sed -n 1p | sed -e \' s#.*@ \ ( \ )# \\ 1# \' ' , shell = True ) . strip ( ) . decode ( ' ascii ' )
os . chdir ( cd )
return version_tag
2020-05-01 18:51:56 +00:00
2020-03-04 02:15:59 +00:00
2020-06-27 18:14:13 +00:00
def build ( service , tag , image , branch ) :
2020-06-28 00:25:15 +00:00
build_args = f ' --build-arg GIT_BRANCH= { branch } '
if service == ' nginx ' and branch == ' version-11 ' :
build_args + = f ' --build-arg NODE_IMAGE_TAG=10-prod '
print ( f ' Building { service } { image } image ' )
subprocess . run ( f ' docker build { build_args } -t { service } - { image } -f build/ { service } - { image } /Dockerfile . ' , shell = True )
tag_and_push ( f ' { service } - { image } ' , tag )
2020-06-27 18:14:13 +00:00
2020-03-04 02:15:59 +00:00
def tag_and_push ( image_name , tag ) :
2020-06-28 00:25:15 +00:00
print ( f ' Tagging { image_name } as " { tag } " and pushing ' )
subprocess . run ( f ' docker tag { image_name } frappe/ { image_name } : { tag } ' , shell = True )
subprocess . run ( f ' docker push frappe/ { image_name } : { tag } ' , shell = True )
2020-03-04 02:15:59 +00:00
def main ( ) :
2020-06-28 00:25:15 +00:00
args = parse_args ( )
tag = args . tag
branch = ' develop '
2020-03-04 02:15:59 +00:00
2020-06-28 00:25:15 +00:00
if args . version :
branch = ' version- ' + args . version
tag = git_version ( args . service , args . version , branch )
2020-03-04 02:15:59 +00:00
2020-06-28 00:25:15 +00:00
if args . tag_only :
tag_and_push ( f ' { args . service } - { args . image_type } ' , tag )
else :
build ( args . service , tag , args . image_type , branch )
2020-03-04 02:15:59 +00:00
2020-06-28 00:25:15 +00:00
if __name__ == " __main__ " :
main ( )