-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_handler.py
93 lines (78 loc) · 3.56 KB
/
docker_handler.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
import os
import git
import json
import docker
from docker import errors
class DockerHandler:
def __init__(self, config_path, logger):
self.logger = logger
self.logger.info('Starting docker handler.')
with open(config_path) as config_file:
self.config = json.load(config_file)
for service in self.config['services']:
service['full_name'] = f'{self.config["registry"]}/{service["name"]}:latest'
service['full_path'] = os.path.join(self.config['root_path'], service['path'])
self.logger.info(f'Obtaining client from {self.config["socket"]}.')
self.client = docker.DockerClient(
base_url=self.config['socket'],
)
self.logger.info('DockerHandler ready.')
def list_services_from_config(self):
return [service['name'] for service in self.config['services']]
def list_services_from_client(self):
return [service.name for service in self.client.services.list()]
def get_service_from_repo(self, repo, branch):
for service in self.config['services']:
if service['github_repo'] == repo and service['github_branch'] == branch:
return service
def git_pull(self, service):
self.logger.info(f'{service["name"]}: Pulling from repo to {service["full_path"]}.')
(git.cmd.Git(service['full_path'])
.pull(env={'GIT_SSH_COMMAND': 'ssh -o "StrictHostKeyChecking no"'}))
def build_image(self, service):
try:
self.logger.info(f'{service["name"]}: Building image on {service["full_path"]}.')
self.client.images.build(
path=service['full_path'],
tag=service['full_name'],
dockerfile='arm.Dockerfile'
)
except TypeError:
self.logger.error(f'Error on provided path {service["full_path"]}, check configuration file.')
except errors.BuildError:
self.logger.error(f'Failed to build {service["name"]}.')
except errors.APIError:
self.logger.error(f'Server error while building {service["name"]}.')
def push_image(self, service):
try:
self.logger.info(f'Pushing image {service["full_name"]}.')
self.client.images.push(repository=service['full_name'])
except errors.APIError:
self.logger.error(f'Server error while pushing {service["name"]}.')
def remove_service(self, service):
for running_service in self.client.services.list():
if running_service.name == service['name']:
self.logger.info(f'Service with name {service["name"]} detected, removing.')
try:
running_service.remove()
except errors.APIError:
self.logger.error(f'Server error while removing {service["name"]}.')
def create_service(self, service):
try:
self.logger.info(f'Creating service {service["name"]}')
self.client.services.create(
image=service['full_name'],
name=service['name'],
constraints=self.config['constraints'],
)
except errors.APIError:
self.logger.error(f'Server error while creating {service["name"]}.')
def service_full_deploy(self, service):
self.git_pull(service)
self.build_image(service)
self.push_image(service)
self.remove_service(service)
self.create_service(service)
def config_run(self):
for service in self.config['services']:
self.service_full_deploy(service)