-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cs/sc 3351 compile det exe linux #236
Changes from all commits
1d63cf7
eb641e4
da90bef
860b048
95d3e92
65b2bcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: commcare-export release actions | ||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
generate_release_assets: | ||
name: Generate release assets | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Pull pyinstaller docker image | ||
run: | | ||
docker pull dimagi/commcare-export-pyinstaller-linux | ||
|
||
- name: Compile linux binary | ||
run: | | ||
docker run -v "$(pwd):/src/" dimagi/commcare-export-pyinstaller-linux | ||
|
||
- name: Upload release assets | ||
uses: AButler/[email protected] | ||
with: | ||
files: "./dist/linux/*" | ||
repo-token: ${{ secrets.GITHUB_TOKEN }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Compiling DET to running executable | ||
This folder contains relevant files needed (dockerfiles and scripts) for compiling the DET into an executable file. | ||
The file structure is segmented into the different operating systems the resultant executable will | ||
be compatible on. | ||
|
||
(Currently only Linux is supported; Windows coming soon) | ||
|
||
|
||
## How it works | ||
In order to compile the DET script into a working executable we use [pyinstaller](https://github.com/pyinstaller/pyinstaller) in a containerized | ||
environment. The dockerfile is an edited version from [cdrx/docker-pyinstaller](https://github.com/cdrx/docker-pyinstaller) | ||
which is slightly modified to suit our use-case. | ||
|
||
When a new release of the DET is published, a workflow is triggered which automatically compiles an executable from the latest | ||
code using the custom built docker image, `dimagi/commcare-export-pyinstaller-linux`, then uploads it to the release as an asset. | ||
|
||
If you ever have to compile the executable yourself you can follow the section below, *Compiling executable files locally*, on how to compile an executable locally. | ||
|
||
|
||
Compiling executable files locally | ||
----------------------------------- | ||
The DET executable files are compiled using a tool called [pyinstaller](https://pyinstaller.org/en/stable/). | ||
Pyinstaller is very easy to use, but only works out-of-the-box for Linux as support for cross-compilation was | ||
dropped in earlier releases. Another tool, [wine](https://www.winehq.org/), can be used in conjuction with | ||
pyinstaller to compile the Windows exe files (not yet supported). | ||
|
||
Luckily in the world we live containerization is a thing. We use a docker container, `dimagi/commcare-export-pyinstaller-linux` | ||
(based on [docker-pyinstaller](https://github.com/cdrx/docker-pyinstaller)), which allows you to seamlessly compile the Linux binary, so we don't ever have to worry about installing any additional packages ourselves. | ||
|
||
To compile a new linux binary, first make sure you have the docker image used to generate the executable: | ||
> docker pull dimagi/commcare-export-pyinstaller-linux:latest | ||
|
||
Now it's really as simple as running | ||
> docker run -v "$(pwd):/src/" dimagi/commcare-export-pyinstaller-linux | ||
|
||
Once you're done, the compiled file can be located at `./dist/linux/commcare-export`. | ||
|
||
The tool needs two files to make the process work: | ||
1. `commcare-export.spec`: this file is used by `pyinstaller` and is already defined and sits at the top of this project. | ||
It shouldn't be necessary for you to change any parameters in the file. | ||
2. `requirements.txt`: this file lists all the necessary packages needed for running commcare-export. | ||
|
||
|
||
## Updating the docker image | ||
Are you sure you need to update the image? | ||
|
||
Just checking... | ||
|
||
|
||
If it's needed to make any changes (for whatever reason) to the docker image you can rebuild the image as follows: | ||
> docker build -f ./build_exe/linux/Dockerfile-py3-amd64 -t dimagi/commcare-export-pyinstaller-linux:latest . | ||
|
||
Now upload the new image to dockerhub (remember to log in to the account first!): | ||
> docker image push dimagi/commcare-export-pyinstaller-linux:latest |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
FROM ubuntu:20.04 | ||
SHELL ["/bin/bash", "-i", "-c"] | ||
|
||
ARG PYTHON_VERSION=3.9.18 | ||
ARG PYINSTALLER_VERSION=6.4 | ||
|
||
ENV PYPI_URL=https://pypi.python.org/ | ||
ENV PYPI_INDEX_URL=https://pypi.python.org/simple | ||
ENV PYENV_VERSION=${PYTHON_VERSION} | ||
|
||
COPY ./build_exe/linux/entrypoint-linux.sh /entrypoint.sh | ||
|
||
RUN \ | ||
set -x \ | ||
# update system | ||
&& apt-get update \ | ||
# install requirements | ||
&& apt-get install -y --no-install-recommends \ | ||
build-essential \ | ||
ca-certificates \ | ||
curl \ | ||
wget \ | ||
git \ | ||
libbz2-dev \ | ||
libreadline-dev \ | ||
libsqlite3-dev \ | ||
libssl-dev \ | ||
zlib1g-dev \ | ||
libffi-dev \ | ||
# required because openSSL on Ubuntu 12.04 and 14.04 run out of support versions of OpenSSL | ||
&& mkdir openssl \ | ||
&& cd openssl \ | ||
# latest version, there won't be anything newer for this | ||
&& wget https://www.openssl.org/source/openssl-1.0.2u.tar.gz \ | ||
&& tar -xzvf openssl-1.0.2u.tar.gz \ | ||
&& cd openssl-1.0.2u \ | ||
&& ./config --prefix=$HOME/openssl --openssldir=$HOME/openssl shared zlib \ | ||
&& make \ | ||
&& make install \ | ||
# install pyenv | ||
&& echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc \ | ||
&& echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc \ | ||
&& source ~/.bashrc \ | ||
&& curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash \ | ||
&& echo 'eval "$(pyenv init -)"' >> ~/.bashrc \ | ||
&& source ~/.bashrc \ | ||
# install python | ||
&& PATH="$HOME/openssl:$PATH" CPPFLAGS="-O2 -I$HOME/openssl/include" CFLAGS="-I$HOME/openssl/include/" LDFLAGS="-L$HOME/openssl/lib -Wl,-rpath,$HOME/openssl/lib" LD_LIBRARY_PATH=$HOME/openssl/lib:$LD_LIBRARY_PATH LD_RUN_PATH="$HOME/openssl/lib" CONFIGURE_OPTS="--with-openssl=$HOME/openssl" PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install $PYTHON_VERSION \ | ||
&& pyenv global $PYTHON_VERSION \ | ||
&& pip install --upgrade pip \ | ||
# install pyinstaller | ||
&& pip install pyinstaller==$PYINSTALLER_VERSION \ | ||
&& mkdir /src/ \ | ||
&& chmod +x /entrypoint.sh | ||
|
||
VOLUME /src/ | ||
WORKDIR /src/ | ||
|
||
ENTRYPOINT ["/entrypoint.sh"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash -i | ||
|
||
# Fail on errors. | ||
set -e | ||
|
||
# Make sure .bashrc is sourced | ||
. /root/.bashrc | ||
|
||
cd /src | ||
|
||
pip install commcare-export | ||
pip install -r build_exe/requirements.txt | ||
|
||
pyinstaller --clean -y --dist ./dist/linux --workpath /tmp *.spec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This file is only used by pyinstaller to create the executable DET instance | ||
chardet | ||
psycopg2-binary | ||
pymysql | ||
pyodbc | ||
urllib3==1.26.7 | ||
xlwt | ||
openpyxl |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import os | ||
|
||
# This env variable is used to alter bundled behaviour | ||
os.environ['DET_EXECUTABLE'] = '1' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- mode: python ; coding: utf-8 -*- | ||
|
||
|
||
a = Analysis( | ||
['commcare_export/cli.py'], | ||
pathex=[], | ||
binaries=[], | ||
datas=[ | ||
('./commcare_export', './commcare_export'), | ||
('./migrations', './migrations'), | ||
], | ||
hiddenimports=[ | ||
'sqlalchemy.sql.default_comparator', | ||
], | ||
hookspath=[], | ||
runtime_hooks=['build_exe/runtime_hook.py'], | ||
excludes=[], | ||
) | ||
pyz = PYZ(a.pure) | ||
|
||
exe = EXE( | ||
pyz, | ||
a.scripts, | ||
a.binaries, | ||
a.datas, | ||
[], | ||
name='commcare-export', | ||
debug=False, | ||
bootloader_ignore_signals=False, | ||
strip=False, | ||
upx=True, | ||
upx_exclude=[], | ||
runtime_tmpdir=None, | ||
console=True, | ||
disable_windowed_traceback=False, | ||
argv_emulation=False, | ||
target_arch=None, | ||
codesign_identity=None, | ||
entitlements_file=None, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ def stored_version(): | |
|
||
|
||
def git_version(): | ||
if os.environ.get('DET_EXECUTABLE'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added so that running the binary will not output an error saying "git was not found". It's expected that folks using the binary might not have git installed so deducing the version from git as is done in this function is redundant for the bundled environment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be an issue for built versions of the tools since we write the version to a file and use that in preference to the git version. |
||
return None | ||
|
||
described_version_bytes = subprocess.Popen( | ||
['git', 'describe'], | ||
stdout=subprocess.PIPE | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was an issue where the version is not set, so doing
don't work; maybe something to look into in the future. Not sure how important it is right now since we're able to simply run
pip install commcare-export
(of course, runningpip install commcare-export
assumes the newest version is already pushed to pypi, which at this stage during the release process should be true).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need
-e
. And depending on the version of Python, you might need setuptools too:Disclaimer: I have not tested this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I tried it, but alas...it didn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried various other ways which was also a dead end or just unfeasible in the long run...I don't think this issue is really blocking, just annoying (let me know if you disagree), so I'll create a ticket to address it.