Skip to content

Commit 3b0f753

Browse files
committed
initial commit
0 parents  commit 3b0f753

15 files changed

+320
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 nightshift2k
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Ansible Role: Freqtrade-Docker
2+
3+
Pulls and/or builds [Freqtrade](https://www.freqtrade.io) Docker images on Debian/Ubuntu based systems.
4+
5+
## Information
6+
7+
This Ansible Role is designed to pull or build Docker images for Freqtrade - a crypto-currency algorithmic trading software developed in Python. It also allows to build a custom image based on the official Freqtrade sources, to enhance or customize it with own Python packages and/or commands.
8+
9+
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. If you are new to Ansible and/or Freqtrade, it is highly recommended to get familiar with the concept of configuration management and infrastructure as code (IaaC). Please see the latest [Ansible documentation](https://docs.ansible.com/ansible/latest/index.html) for further information.
10+
11+
## Requirements
12+
13+
Currently only compatible with Debian/Ubuntu based systems. Tested on Ubuntu 20.04. **At the moment it is not possible to deploy it on Rasberry Pi's (armv7l) architecture!**
14+
15+
*If you want to see other OS flavors supported, you can very much submit a pull request with the desired additions.*
16+
17+
Note that this role requires root access, so either run it in a playbook with a global `become: yes`, or invoke the role in your playbook like:
18+
19+
- hosts: tradingbots
20+
roles:
21+
- role: nightshift2k.freqtrade-docker
22+
become: yes
23+
24+
## Installation
25+
26+
### Via Github
27+
28+
ansible-galaxy install git+https://github.com/nightshift2k/ansible-role-freqtrade-docker.git
29+
30+
### From Ansible Galaxy
31+
32+
ansible-galaxy install nightshift2k.freqtrade-docker
33+
34+
## Role Variables
35+
36+
Available variables are listed below, along with default values (see `defaults/main.yml`):
37+
38+
39+
freqtrade_branch: "stable"
40+
41+
The branch in GitHub for the checkout of source files, and also the tag used when pulling the Docker image from registry.
42+
43+
docker_build_directory: "/tmp/build/docker"
44+
45+
Temporary directory for the Docker images. GitHub checkout will happen into subfolder, also if custom image will be build, a Dockerfile will be created there.
46+
47+
build_freqtrade: false
48+
49+
When set to true, will build the image from GitHub sources, instead of pulling it from Docker Registry.
50+
51+
build_freqtrade_custom: false
52+
53+
Will build an additional image with custom Dockerfile and Prefix (see below).
54+
55+
freqtrade_custom_prefix : "custom"
56+
57+
Needs to be defined, when `build_freqtrade_custom` equals `true` which will define the docker image name prefix (repository), so the custom image will be created as `custom/freqtrade:stable`on the target.
58+
59+
freqtrade_custom_commands:
60+
- "RUN pip install --upgrade pip"
61+
62+
Commands that will be placed into the Dockerfile, anything that is supported in Dockerfiles can be placed here as a list.
63+
64+
freqtrade_custom_pips:
65+
- finta
66+
- ta
67+
- technical
68+
69+
Will be placed after `freqtrade_custom_commands` to install custom Python packages via pip, see `templates/Dockerfile.j2` inside of the role for details.
70+
71+
72+
## Dependencies
73+
74+
This role is dependent on `geeringguy.docker` and also the collection `community.docker` please use `ansible-galaxy` to install the role and collection beforehand.
75+
76+
ansible-galaxy collection install community.docker
77+
ansible-galaxy install geerlingguy.docker
78+
79+
## Example Playbook
80+
81+
- hosts: tradingbots
82+
vars_files:
83+
- vars/freqtrade-docker.yml
84+
roles:
85+
- role: nightshift2k.freqtrade-docker
86+
become: yes
87+
88+
*Inside `vars/freqtrade-docker.yml`*:
89+
90+
freqtrade_branch: "develop"
91+
docker_build_directory: "/tmp/build/docker"
92+
build_freqtrade: true
93+
build_freqtrade_custom: true
94+
freqtrade_custom_prefix : "nightshift2k"
95+
freqtrade_custom_commands:
96+
- "RUN pip install --upgrade pip"
97+
freqtrade_custom_pips:
98+
- finta
99+
- ta
100+
- technical
101+
- mergedeep
102+
...
103+
104+
## License
105+
106+
MIT / BSD

defaults/main.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# branch from github
3+
freqtrade_branch: "stable"
4+
5+
# build directory for docker
6+
docker_build_directory: "/tmp/build/docker"
7+
8+
# build freqtrade container from source
9+
build_freqtrade: false
10+
11+
# build freqtrade custom image
12+
build_freqtrade_custom: false
13+
14+
# prefix of the container for custom image
15+
freqtrade_custom_prefix : "custom"
16+
17+
# commands for Dockerfile to be executed in the custom image
18+
freqtrade_custom_commands:
19+
- "RUN pip install --upgrade pip"
20+
21+
freqtrade_custom_pips:
22+
- finta
23+
- ta
24+
- technical

meta/main.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
dependencies:
3+
- role: geerlingguy.docker
4+
5+
galaxy_info:
6+
role_name: freqtrade-docker
7+
author: nightshift2k
8+
description: Freqtrade algorithmic trading bot for Debian/Ubuntu - dockerized.
9+
license: "license (BSD, MIT)"
10+
min_ansible_version: 2.8
11+
platforms:
12+
- name: Ubuntu
13+
versions:
14+
- all
15+
- name: Debian
16+
versions:
17+
- all
18+
galaxy_tags:
19+
- freqtrade
20+
- algotrading

tasks/directories.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: 'Docker build directory tasks.'
3+
block:
4+
- name: "remove Docker build directory {{ docker_build_directory }} from previous runs."
5+
file:
6+
path: "{{ docker_build_directory }}"
7+
state: absent
8+
9+
- name: "create Docker build directory {{ docker_build_directory }}."
10+
file:
11+
path: "{{ docker_build_directory }}"
12+
state: directory
13+
14+
- name: "create Docker build directory {{ docker_build_directory }}/freqtrade-custom"
15+
file:
16+
path: "{{ docker_build_directory }}/freqtrade-custom"
17+
state: directory
18+
when: build_freqtrade_custom|bool and freqtrade_custom_prefix is defined and (freqtrade_custom_commands is iterable or freqtrade_custom_pips is iterable)
19+
20+
when: build_freqtrade|bool or ( (build_freqtrade_custom|bool and freqtrade_custom_prefix is defined) and (freqtrade_custom_commands is iterable or freqtrade_custom_pips is iterable) )

tasks/image-build-custom.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
- name: "Docker custom image tasks."
3+
block:
4+
- name: "create Dockerfile for {{ freqtrade_custom_prefix }}/freqtrade:{{ freqtrade_branch }}."
5+
template:
6+
src: "Dockerfile.j2"
7+
dest: "{{ docker_build_directory }}/freqtrade-custom/Dockerfile"
8+
9+
- name: "Build Docker image for {{ freqtrade_custom_prefix }}/freqtrade:{{ freqtrade_branch }}."
10+
community.docker.docker_image:
11+
name: "{{ freqtrade_custom_prefix }}/freqtrade"
12+
tag: "{{ freqtrade_branch }}"
13+
build:
14+
dockerfile: "{{ docker_build_directory }}/freqtrade-custom/Dockerfile"
15+
path: "{{ docker_build_directory }}/freqtrade-custom"
16+
source: build
17+
18+
when: build_freqtrade_custom|bool and freqtrade_custom_prefix is defined and (freqtrade_custom_commands is iterable or freqtrade_custom_pips is iterable)

tasks/image-build.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: "Build freqtrade image from GitHub"
3+
block:
4+
- name: "Clone freqtrade {{ freqtrade_branch }} from GitHub to build directory {{ docker_build_directory }}."
5+
git:
6+
repo: https://github.com/freqtrade/freqtrade.git
7+
dest: "{{ docker_build_directory }}/freqtrade"
8+
version: "{{ freqtrade_branch }}"
9+
force: true
10+
11+
- name: "Build Docker image for freqtradeorg/freqtrade:{{ freqtrade_branch }}."
12+
community.docker.docker_image:
13+
name: freqtradeorg/freqtrade
14+
tag: "{{ freqtrade_branch }}"
15+
build:
16+
dockerfile: "{{ docker_build_directory }}/freqtrade/Dockerfile"
17+
path: "{{ docker_build_directory }}/freqtrade"
18+
source: build
19+
20+
when: build_freqtrade|bool

tasks/image-cleanup.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: "remove Docker image for {{ freqtrade_custom_prefix }}/freqtrade:{{ freqtrade_branch }}."
3+
community.docker.docker_image:
4+
name: "{{ freqtrade_custom_prefix }}/freqtrade"
5+
tag: "{{ freqtrade_branch }}"
6+
state: absent
7+
when: build_freqtrade_custom|bool and freqtrade_custom_prefix is defined
8+
9+
- name: "remove Docker image for freqtradeorg/freqtrade:{{ freqtrade_branch }}."
10+
community.docker.docker_image:
11+
name: freqtradeorg/freqtrade
12+
tag: "{{ freqtrade_branch }}"
13+
state: absent
14+

tasks/image-pull.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: "Pull Docker image for freqtradeorg/freqtrade:{{ freqtrade_branch }} from Registry."
3+
community.docker.docker_image:
4+
name: freqtradeorg/freqtrade
5+
tag: "{{ freqtrade_branch }}"
6+
source: pull
7+
when: not build_freqtrade|bool

tasks/main.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# Variable configuration.
3+
- include_tasks: variables.yml
4+
5+
# OS specific prerequisites - currently Debian / Ubuntu only
6+
- include_tasks: prerequisites-Debian.yml
7+
when: ansible_os_family == 'Debian'
8+
9+
# some directory prerequisits that need to be met first
10+
- include_tasks: directories.yml
11+
12+
# perform image cleanup before building them
13+
- include_tasks: image-cleanup.yml
14+
15+
# Build image from source (when configured)
16+
- include_tasks: image-build.yml
17+
18+
# Pull image from Docker registry (if not build from source)
19+
- include_tasks: image-pull.yml
20+
21+
# Build custom image based on the previously build or pulled image
22+
- include_tasks: image-build-custom.yml
23+
24+
# Cleanup
25+
- include_tasks: post-cleanup.yml

tasks/post-cleanup.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: "remove Docker build directory {{ docker_build_directory }}."
3+
file:
4+
path: "{{ docker_build_directory }}"
5+
state: absent

tasks/prerequisites-Debian.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: "Ensure necessary packages are installed"
3+
apt:
4+
name: "{{ prerequisite_packages }}"
5+
state: present
6+
update_cache: true
7+
register: deb_prerequisite_install_packages
8+

tasks/variables.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
# Variable configuration.
3+
- name: "Include OS-specific variables"
4+
include_vars: "{{ item }}"
5+
with_first_found:
6+
- files:
7+
- "vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
8+
- "vars/{{ ansible_os_family }}.yml"
9+
skip: true
10+
11+
- name: "Define prerequisite packages"
12+
set_fact:
13+
prerequisite_packages: "{{ __prerequisite_packages | list }}"
14+
when: prerequisite_packages is not defined

templates/Dockerfile.j2

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
##
2+
## This file is maintained by Ansible - ALL MODIFICATIONS WILL BE REVERTED
3+
##
4+
FROM freqtradeorg/freqtrade:{{ freqtrade_branch }}
5+
6+
# custom commands
7+
{% for command in freqtrade_custom_commands %}
8+
{{ command }}
9+
{% endfor %}
10+
11+
# custom pip installs
12+
{% for pip in freqtrade_custom_pips %}
13+
RUN pip install --upgrade {{ pip }}
14+
{% endfor %}

vars/Debian.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
__prerequisite_packages:
3+
- python3-docker
4+
- python3-distutils

0 commit comments

Comments
 (0)