Skip to content

Commit

Permalink
run sh without provide dst-path; add build_time_prefix;
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-soft2 committed Jan 3, 2022
1 parent a47dcf9 commit 210cdc8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
88 changes: 85 additions & 3 deletions pydocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class DockerFile(pydocker.DockerFile):
v1.0.5 Sat Jun 15 11:21:02 UTC 2019 jen
- add error trace for RUN_bash_script
v1.0.6 Mon Jan 3 22:35:21 UTC 2022 jen
- add DockerFileV105a
- allow run sh script without provide dst-path
- unique tag - useful for development new dockerfile
- add generate_img_name
--------------------------------------------------------------------------------
contributors:
Expand Down Expand Up @@ -105,6 +112,8 @@ class DockerFile(pydocker.DockerFile):
import re
import json
import subprocess
import hashlib
import datetime

import logging

Expand All @@ -114,7 +123,7 @@ class DockerFile(pydocker.DockerFile):


# ############################################################################ #
class DockerFile(object):
class DockerFileV105(object):
# https://docs.docker.com/engine/reference/builder/

FROM, LABEL, COPY, RUN, WORKDIR, ENV, SHELL, EXPOSE, ENTRYPOINT, CMD, \
Expand Down Expand Up @@ -149,7 +158,7 @@ def get_img_name(self):

def __setattr__(self, key, value):
if key.startswith('_'):
return super(DockerFile, self).__setattr__(key, value)
return super(DockerFileV105, self).__setattr__(key, value)
#
if not isinstance(value, str):
value = json.dumps(value)
Expand Down Expand Up @@ -298,6 +307,79 @@ def build_img(self, remove_out_files=True):

# ############################################################################ #

class DockerFileV106(DockerFileV105):
"""
add features:
- allow run sh script without provide dst-path
- unique tag - useful for development new dockerfile
- add generate_img_name
"""
def __init__(self, base_img, name, use_build_time_prefix=False):
super(DockerFileV106, self).__init__(base_img, name)
self._use_build_time_prefix = use_build_time_prefix

def RUN_bash_script(self, dst_path: str, content=None, keep_file=False):
if content is None:
# feature - allow run sh script without provide dst-path
assert keep_file is False
content = dst_path
_name_sold = hashlib.sha256(
content.encode()
).hexdigest()[:24]
_counter = getattr(self, '_counter', 0)
_counter += 1
setattr(self, '_counter', _counter)
dst_path = f'/opt/random_{_counter:02d}_{_name_sold}.sh'
#
return super(DockerFileV106, self).RUN_bash_script(dst_path, content, keep_file)

def get_img_name(self):
_img_name = getattr(self, '_img_name', None)
if _img_name is None:
# use cashing for ime_prefix feature
_img_name = self.generate_img_name()
setattr(self, '_img_name', _img_name)
#
return _img_name

def generate_img_name(self):
if self._use_build_time_prefix is True:
# feature - unique tag - useful for development new dockerfile
# need to check if docker-file has new changes
_build_time_id = f'{datetime.datetime.utcnow():%y%m%d%H%M%S}'
return f'{self._namespace}/{self._name}' \
f':' \
f'{self._version}-build-{_build_time_id}'
#
return super(DockerFileV106, self).get_img_name()

# ############################################################################ #


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
DockerFileLatestStable = DockerFileV105 # stable using for long time
DockerFileLatestAlpha = DockerFileV106 # newest version for more features

DockerFile = DockerFileLatestStable # by default - stable

# it's allow you to use more appropriate release in your code
# you can user full compatibility version DockerFileLatestStable
# or maximum feature DockerFileLatestAlpha
# or take direct DockerFileV106 for fixation any api changes in feature


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# aliases - using naming Dockerfile instead DockerFile
DockerfileV105 = DockerFileV105
DockerfileV106 = DockerFileV106

DockerfileLatestStable = DockerFileLatestStable
DockerfileLatestAlpha = DockerFileLatestAlpha
Dockerfile = DockerFile





Dockerfile = DockerFile # alias

8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# https://test.pypi.org/project/pydocker
# pip install --no-cache-dir -U -i https://test.pypi.org/pypi pydocker
# git tag -a v1.0.5 -m 'version 1.0.5'
# git tag -a v1.0.6 -m 'version 1.0.6'
# git push origin --tags
# ------------------------------------------------------------------------------
Expand All @@ -43,7 +43,7 @@
python3.4 setup.py sdist # EGG
python3.4 -m twine upload dist/* -r pypi
# pip install --no-cache-dir -U pydocker==1.0.5
# pip install --no-cache-dir -U pydocker==1.0.6
# ls -lah /usr/local/lib/python2.7/dist-packages | grep pydocker
# ------------------------------------------------------------------------------
Expand All @@ -59,7 +59,7 @@

setup(
name='pydocker',
version='1.0.5',
version='1.0.6',

description='Easy generator Dockerfile for humans.',
long_description=long_description,
Expand All @@ -74,7 +74,7 @@
maintainer_email='jen.soft.master@gmail.com',

platforms=['any', ],
download_url='https://github.com/jen-soft/pydocker/archive/v1.0.5.zip',
download_url='https://github.com/jen-soft/pydocker/archive/v1.0.6.zip',

# packages=['pydocker', ],
py_modules=['pydocker', ],
Expand Down

0 comments on commit 210cdc8

Please sign in to comment.