-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfabfile.py
146 lines (131 loc) · 3.89 KB
/
fabfile.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import json
from fabric.api import task, local
region = '<YOUR_AWS_REGION>'
image = '<YOUR_IMAGE_NAME>' # ex: pinktest:latest
account = '<YOUR_AWS_ACCOUNT>'
endpoint = f'{account}.dkr.ecr.{region}.amazonaws.com/{image}'
environments = {
'staging' : '<YOUR_STAGING_EB_ENVIRONMENT>',
'production': '<YOUR_PRODUCTION_EB_ENVIRONMENT>'
}
####################################################################################
################################## BUILD TASKS #####################################
####################################################################################
'''
Build the single image using the only Dockerfile
required_args: spec -- {'staging', 'production'}, specify which settings to use
'''
@task
def build(spec):
assert spec in {'staging', 'production'}
spec = 'config.settings.' + spec
local(
'docker build' +
' -t ' + image +
' -f Dockerfile' +
' --build-arg DJANGO_SETTINGS_MODULE=' + spec +
' .'
)
'''
Removes old images with 'none' tag, cleans up your docker images
'''
@task
def clean():
try:
local('docker rmi -f $(docker images | grep none | awk \'{print $3}\')')
except:
print('No images removed, exiting')
####################################################################################
################################# DEPLOYMENT TASKS #################################
####################################################################################
'''
If AWS not letting you push, execute this from your aws cli environment.
Gets login and executes resulting output
'''
@task
def login():
local(
'$(' +
'aws ecr get-login' +
' --region ' + region +
' --no-include-email' +
')'
)
'''
Tags image for aws ecs repository then pushes to that repository.
'''
@task
def push():
print(f'tagging {image}...')
local(f'docker tag {image} {endpoint}')
print(f'tagged image: {endpoint}')
local(f'docker push {endpoint}')
'''
Deploys using eb deploy, requires the correct .elasticbeanstalk/config.yml
and a Dockerrun.aws.json file. Make sure you are in your aws cli environment
This generates an Dockerrun.aws.json file that is the sole file uploaded using eb
'''
@task
def eb(spec):
dockerrun_dict = {
'AWSEBDockerrunVersion': '1',
'Image': {
'Name': endpoint,
'Update': 'true'
},
'Ports': [
{
'ContainerPort': '80'
}
],
'Logging': '/var/log/nginx'
}
with open('Dockerrun.aws.json', 'w') as f:
f.write(json.dumps(dockerrun_dict, indent=4))
e = environments[spec]
print(f'Calling eb deploy for {e}')
awk = 'awk \'{print $3}\''
v = local(f'eb status {e} | grep \'Deployed Version\' | {awk}', capture=True)
v_new = input(f'Current running version is {v}. Specify new version: ')
local(f'eb deploy -l {v_new} {e}')
local('rm Dockerrun.aws.json')
####################################################################################
################################### RUN TASKS ######################################
####################################################################################
'''
start the server inside the docker container
'''
@task
def start(spec=None):
if spec == 'dev':
local('python src/manage.py runserver')
if spec == 'bash':
try:
local(f'docker run -it {image} /bin/bash')
except:
print(f'Unable to run {image}, please check the image or docker ps')
else:
try:
local(f'docker run -p 80:80 {image}')
except:
print(f'Unable to run {image}, please check the image or docker ps')
'''
stop the container running latest image
'''
@task
def stop():
try:
local('docker stop $(docker ps | grep ' + image + ' | awk \'{print $1}\')')
except:
print(f'No processes with {image} to stop, exiting')
'''
attach to the container running the latest image, run bash
need to have started it already
'''
@task
def attach():
try:
local('docker exec -it $(docker ps | grep ' + image + ' | awk \'{print $1}\') /bin/bash')
except:
print(f'No processes with {image} to attach, exiting')