def __init__(self, name, _path): self.name = name self.path = os.path.join(_path, name) # TODO it is must be choosen self.image = 'async' # [async, sync, other, base_dev] os.chdir(_path) self.cli = DC(base_url='unix:///var/run/docker.sock') self.settings = Settings()
def __init__(self, name, _path): self.name = name self.path = os.path.join(_path, name) os.chdir(_path) self.cli = DC(base_url='unix:///var/run/docker.sock') self.settings = Settings(self.path)
class Project(BaseObject): def __init__(self, name, _path): self.name = name self.path = os.path.join(_path, name) os.chdir(_path) self.cli = DC(base_url='unix:///var/run/docker.sock') self.settings = Settings(self.path) def create(self): # create project dir os.mkdir(self.name) os.chdir(self.name) # create docker image os.mkdir('docker-containers') os.chdir('docker-containers') os.mkdir('base_dev') os.chdir('base_dev') with open('Dockerfile', 'w') as d: d.write("""FROM ubuntu:14.04 RUN sed -i 's/archive.ubuntu.com/ua.archive.ubuntu.com/' /etc/apt/sources.list && \ apt-get update && \ apt-get install -y vim openssh-server screen git && \ update-rc.d ssh defaults && \ echo 'root:password' | chpasswd && \ sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config """) # print build container for x in [line for line in self.cli.build(path=os.getcwd(), tag='base_dev')]: print(json.loads(x.decode())['stream']) os.chdir('..') os.mkdir('async') with open('Dockerfile', 'w') as d: d.write("""FROM ubuntu:14.04 RUN sed -i 's/archive.ubuntu.com/ua.archive.ubuntu.com/' /etc/apt/sources.list && \ apt-get update && \ apt-get install -y vim openssh-server screen git && \ update-rc.d ssh defaults && \ echo 'root:password' | chpasswd && \ sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config RUN apt-get install -y python3-pip && pip3 install aiohttp """) # print build container for x in [line for line in self.cli.build(path=os.getcwd(), tag='async')]: print(json.loads(x.decode())['stream']) self.settings.add({'project': { 'name': self.name, 'path': self.path, 'image': 'base_dev', 'ports': (8090, 8090) }}) self.settings.save() print('create project with name: %s' % self.name) def delete(self): self.stop() shutil.rmtree(self.settings.source['project']['path']) print('remove project with name: %s' % self.name) def run(self): print('run connection api...') print('run database layer...') print('run all container...') for container_name in self.settings.source: if container_name != 'project': Container(container_name, os.path.sep.join( self.settings.source[container_name]['path'].split(os.path.sep)[:-1])).run() def stop(self): for container_name in self.settings.source: if container_name != 'project': Container(container_name, os.path.sep.join( self.settings.source[container_name]['path'].split(os.path.sep)[:-1])).stop() print('stop all container...') print('stop database layer...') print('stop connection api...')
class Container(BaseObject): def __init__(self, name, _path): self.name = name self.path = os.path.join(_path, name) # TODO it is must be choosen self.image = 'async' # [async, sync, other, base_dev] os.chdir(_path) self.cli = DC(base_url='unix:///var/run/docker.sock') self.settings = Settings() def create(self): # create async or sync container # add volume to setting for this name self.settings.add({self.name: {'path': self.path, 'template': self.image, 'image': self.image, 'ports': {8090: get_free_port()}, 'entry_point': 'python3 {path}/server.py'.format(path='/opt/' + self.name), } }) self.settings.save() os.mkdir(self.name) os.chdir(self.path) with open('server.py', 'w') as f: with open('/home/verbalist/PycharmProjects/aioframe/server.py') as r: f.write(r.read()) print('create container with name: %s' % self.name) def delete(self): self.stop() self.settings.source.pop(self.name) self.settings.save() shutil.rmtree(self.settings.source.get(self.name).get('path')) print('remove container with name: %s' % self.name) def run(self): container_settings = self.settings.source.get(self.name) container = {'image': self.image, 'volumes': ['/opt/' + self.name], 'command': container_settings.get('entry_point'), 'name': self.name, 'ports': [x for x in container_settings.get('ports')], 'host_config': self.cli.create_host_config( port_bindings=container_settings.get('ports'), binds={container_settings.get('path'): { 'bind': '/opt/' + self.name, }} )} _c = self.cli.create_container(**container) self.cli.start(container=_c.get('Id')) info = self.cli.inspect_container(container=_c.get('Id')) container_ip = info['NetworkSettings']['IPAddress'] print('http://' + container_ip + ':' + str(list(container_settings.get('ports').keys())[0])) print('http://localhost:%s' % str(list(container_settings.get('ports').values())[0])) self.settings.source[self.name]['container_id'] = _c.get('Id') # TODo print warning to log self.settings.save() def stop(self): container_id = self.settings.source.get(self.name).pop('container_id', None) if container_id is not None: self.cli.stop(container=container_id) self.cli.remove_container(container=container_id)