-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
82 lines (64 loc) · 2.12 KB
/
tasks.py
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
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
import datetime
import os
from invoke import task
import fotc.database
DEFAULT_REPOSITORY = "hstefanp/fotc"
DOCKERFILE = "./docker/Dockerfile"
@task
def docker_build(ctx, repository=DEFAULT_REPOSITORY):
"""
Builds and tags fotc's docker image
:type ctx: invoke.Context
:type repository: str
"""
commit = ctx.run("git describe --always --long --tags --broken")
if commit.failed:
print("Failed to get git version!")
return
commit = commit.stdout.strip()
date = datetime.datetime.utcnow().strftime("%Y%m%d%H%M")
tag = f"{repository}:{date}-{commit}"
latest = f"{repository}:latest"
ctx.run(f"docker build . -f {DOCKERFILE} -t {tag} -t {latest}")
def _docker_login_command():
username = os.environ["DOCKER_USERNAME"]
password = os.environ["DOCKER_PASSWORD"]
return f"docker login --username={username} --password={password}"
@task
def docker_push(ctx, repository=DEFAULT_REPOSITORY, tag_filter=None):
"""
Pushes locally built images to the Docker repository
:type ctx: invoke.Context
:type repository: str
:type tag_filter: str|None
"""
target = f"{repository}:{tag_filter}" if tag_filter else f"{repository}"
images_format = "{{.Repository}}:{{.Tag}}"
images_out = ctx.run(f"docker images {target} --format {images_format}").stdout
images = [x for x in images_out.split('\n') if x]
print(images)
with ctx.prefix(_docker_login_command()):
for image in images:
ctx.run(f"docker push {image}")
@task
def test(ctx, dir_path="./tests/"):
"""
Run test suite on dir_path
:type ctx: invoke.Context
:type dir_path: str
"""
ctx.run(f"python -m pytest {dir_path}")
@task()
def create_db_tables(_ctx, drop=False):
"""
Create database tables from the defined modules
:type _ctx: invoke.Context
"""
conn = fotc.database.get_default_engine().connect()
if drop:
with open('database/drop-all.sql') as drop:
conn.execute(drop.read())
with open('database/schema.sql') as schema:
conn.execute(schema.read())