1
This commit is contained in:
parent
e01ba723d4
commit
6e3d16458c
95
deploy_docker.py
Normal file
95
deploy_docker.py
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# in prod-salt-01 ,run start ,bound,remove docker ,get gz file from 10.10.2.3
|
||||||
|
from fabric import Connection
|
||||||
|
import os
|
||||||
|
import pdb
|
||||||
|
|
||||||
|
|
||||||
|
#todo :remove dock image && docker
|
||||||
|
class DeployDocker:
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.target = os.path.abspath('.').split('/')[-1].strip()
|
||||||
|
self.image_name = f"{self.target.split('.')[0]}:{'.'.join(self.target.split('.')[1:])}"
|
||||||
|
self.hostip = '10.10.2.3'
|
||||||
|
self.remote_base_dir = "/tmp/miles"
|
||||||
|
self.base_image = "k_ub20:v1"
|
||||||
|
self.remote_dir = f"{self.remote_base_dir}/{self.target}"
|
||||||
|
self.user = "root"
|
||||||
|
self.docker_files = "Dockerfile"
|
||||||
|
|
||||||
|
def _check_result(self, data):
|
||||||
|
if data.exited == 0:
|
||||||
|
print(f"run {data.command} Success!\n{data.stdout.strip()}")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"run {data.command} Failed!\n{data.stdout.strip()}\t{data.stderr.strip()}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _pre_start(self):
|
||||||
|
sync_cmd = f"rsync -av ../{self.target} root@{self.hostip}:{self.remote_base_dir}"
|
||||||
|
with Connection(host=self.hostip, user=self.user) as c:
|
||||||
|
result = c.local(sync_cmd)
|
||||||
|
assert result.exited == 0
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _build_docker(self):
|
||||||
|
with Connection(host=self.hostip, user=self.user) as c:
|
||||||
|
|
||||||
|
cmd = f"cd {self.remote_dir} && docker build -t {self.image_name} -f {self.docker_files} ."
|
||||||
|
result = c.run(cmd)
|
||||||
|
assert result.exited == 0
|
||||||
|
#self._check_result(result)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _run_docker(self):
|
||||||
|
with Connection(host=self.hostip, user=self.user) as c:
|
||||||
|
#pdb.set_trace()
|
||||||
|
run_docker = f"docker run -d {self.image_name} bash"
|
||||||
|
result = c.run(run_docker)
|
||||||
|
assert result.exited == 0
|
||||||
|
find_docker = f"docker ps -a --filter ancestor={self.image_name}"
|
||||||
|
result_find = c.run(find_docker).stdout.strip()
|
||||||
|
container_id = result_find.split('\n')[-1].split(" ")[0]
|
||||||
|
assert container_id
|
||||||
|
get_gz_cmd = f"docker cp {container_id}:/app/target/app.tar.gz {self.remote_dir}/app.tar.gz"
|
||||||
|
result_get_docker = c.run(get_gz_cmd)
|
||||||
|
assert result_get_docker.exited == 0
|
||||||
|
remote_file = f"{self.remote_dir}/app.tar.gz"
|
||||||
|
local_file = f"./target/{self.target}.tar.gz"
|
||||||
|
result_get_remote = c.local(
|
||||||
|
f"rsync root@{self.hostip}:/{remote_file} {local_file}")
|
||||||
|
assert result_get_remote.exited == 0
|
||||||
|
self._clean()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _clean(self):
|
||||||
|
with Connection(host=self.hostip, user=self.user) as c:
|
||||||
|
find_docker = f"docker ps -a --filter ancestor={self.image_name}"
|
||||||
|
result_find = c.run(find_docker).stdout.strip()
|
||||||
|
container_id = result_find.split('\n')[-1].split(" ")[0]
|
||||||
|
assert container_id
|
||||||
|
remove_docker = f"docker stop {container_id} && docker rm {container_id} &&docker image rm {self.image_name}"
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _start(self):
|
||||||
|
if self._build_docker():
|
||||||
|
if self._run_docker():
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._clean()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
if self._pre_start():
|
||||||
|
if self._start():
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
dd = DeployDocker()
|
||||||
|
dd.run()
|
Loading…
x
Reference in New Issue
Block a user