Skip to content

Commit

Permalink
DEPLOY.py 1.3.3 - output/wording tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
blukis committed Jul 15, 2023
1 parent c5e76ae commit 0196500
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
78 changes: 52 additions & 26 deletions python-gitdeployer/DEPLOY.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DEPLOY.py - v1.3.2 - https://github.com/blukis/publictools/blob/main/python-gitdeployer/
VERSION = "1.3.2"
# DEPLOY.py - v1.3.3 - https://github.com/blukis/publictools/blob/main/python-gitdeployer/
VERSION = "1.3.3"
HOME_URL = "https://github.com/blukis/publictools/blob/main/python-gitdeployer/"
# - Desc: Script to make deployments from Virtualmin server prompt interface
#
Expand Down Expand Up @@ -132,15 +132,25 @@ def GitCommitSubject(gitCmd, repoDir):
return cp.stdout.decode().strip()

def CommitVerStr(gitDate, gitHash): # Str to represent deployed ver e.g. "20230101_1a2b3c4d"
commitVerStr = datetime.datetime.strftime(gitDate, '%Y%m%d') + "_" + gitHash[0:8]
commitVerStr = gitHash[0:8] + "(" + datetime.datetime.strftime(gitDate, '%Y%m%d') + ")"
return commitVerStr

def LoadLastDeployedVer(appName):
def LoadLastDeployedObj(appName):
with open(lastDeploysFile) as file:
ldObj = json.load(file)
if appName not in ldObj or "commitVerStr" not in ldObj[appName]:
return ""
return ldObj[appName]["commitVerStr"]
return ldObj[appName] if appName in ldObj else {}

def LoadLastDeployedVer(appName):
aldObj = LoadLastDeployedObj(appName)
return aldObj["commitVerStr"] if "commitVerStr" in aldObj else ""

def PrintLastNDeploys(appEnvName, n):
appLogFile = logsDir + "/" + appEnvName + ".log"
with open(appLogFile) as file:
lines = file.readlines()
lastN = lines[-n:]
for line in lastN:
print(" " + line.strip())


def ListDeployables():
Expand All @@ -155,12 +165,12 @@ def ListDeployables():
maxLen = len(max(appEnvNames, key=len)) # https://www.geeksforgeeks.org/python-longest-string-in-list/
maxLen = max(maxLen, 13) # Ensure at least wide enough for headers.
print("")
print("DEPLOYABLE_NAME" + ("." * (maxLen-10)) + "LAST_DEPLOY")
print("DEPLOYABLE_NAME" + ("." * (maxLen-10)) + "LAST_DEPLOYMENT")
for appEnv in appEnvNames:
deployStr = LoadLastDeployedVer(appEnv)
deployStr = deployStr if deployStr else "NO_DATA"
print("* " + appEnv + ("." * (maxLen+3-len(appEnv))) + deployStr)
print("")
#print("")

def DeployApp(appEnvName, commitHash, checkSum):
if not appEnvName:
Expand Down Expand Up @@ -204,20 +214,28 @@ def DeployApp(appEnvName, commitHash, checkSum):
gitDateIso1 = GitDateIso(gitCmd, repoDir)
gitDate = datetime.datetime.strptime(gitDateIso1, '%Y-%m-%d %H:%M:%S %z')
gitDateIso2 = gitDate.isoformat()
lastDeployObj = LoadLastDeployedObj(appEnvName)
deployedTime = lastDeployObj["timeNowIso"] if "timeNowIso" in lastDeployObj else "??"
print("")
print("Last deployed ver: \"" + str(LoadLastDeployedVer(appEnvName)) + "\" - deployed at " + deployedTime)
print("")
print("DEPLOY '" + appEnvName + "' - (last deploy: " + str(LoadLastDeployedVer(appEnvName)) + "):")
print("- Commit-msg: \"" + gitSubject + "\"")
print("- Commit-date: " + gitDateIso1)
print("- Commit-hash: " + gitHash)
print("Latest commit:")
print(" - Commit-msg: \"" + gitSubject + "\"")
print(" - Commit-date: " + gitDateIso1)
print(" - Commit-hash: " + gitHash)
# TODO: warn if age of last commit is > a few days old.?
if printLog:
print("")
print("Recent deployments (10):")
PrintLastNDeploys(appEnvName, 10)

print("")
#print("DEPLOY the above commit to \"" + deployToDir + "\"?") # Disabled: can't ask for user input, in non-interactive-shell environment (Webmin/Virtualmin).
#answer = input("Type hash[0:3] to deploy... ")
#if answer.lower() != hash[0:3]:
# PrintAndQuit("Deploy cancelled.")
if (not checkSum):
PrintAndQuit("To deploy, supply additional argument [hash-1st-3-chars].")
PrintAndQuit("To deploy latest commit, supply additional argument [1st-3-chars-of-hash].")
if (checkSum != gitHash[0:3] and checkSum != "NOCHECK"):
PrintAndQuit("hash.left(3) argument does not match current repo; aborting!")

Expand Down Expand Up @@ -293,8 +311,9 @@ def DeployApp(appEnvName, commitHash, checkSum):
checkSum = ""
nocheck = False
execMode = ""
printLog = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hlv", ["hash=", "help", "list", "nocheck", "skipcheck", "status", "version"])
opts, args = getopt.getopt(sys.argv[1:], "hv", ["hash=", "help", "nocheck", "skipcheck", "status", "printlogexp", "version"])
except getopt.GetoptError as err:
# print help information and exit:
print("getopt error: " + str(err)) # will print something like "option -a not recognized"
Expand All @@ -305,13 +324,15 @@ def DeployApp(appEnvName, commitHash, checkSum):
execMode = "help"
elif o in ("-v", "--version"):
execMode = "version"
elif o in ("-l", "--list", "--status"):
elif o in ("--status"):
execMode = "status"
else:
if o in ("-s", "--hash"):
commitHash = a
if o in ("--skipcheck", "--nocheck"): # bypass hash.left(3) check.
checkSum = "NOCHECK"
if o in ("--printlogexp"):
printLog = True
#else:
# print("opts error.") #assert False, "unhandled option"
if len(args) > 0:
Expand All @@ -320,21 +341,26 @@ def DeployApp(appEnvName, commitHash, checkSum):
checkSum = args[1]

if execMode == "help":
print("Call with no args: output configured deployables list & status.")
print("- To deploy, supply additional 'deployable_name' argument.")
print("Flags, optional:")
print("--hash=xxxx - deploy a specific hash (default:latest).")
print("--skipcheck - bypass hash.left(3) check.")
#print("--status - list all deployables")
PrintAndQuit("More documentation at " + HOME_URL)
print("Usage: DEPLOY.py [options] [deployablename] [hash3chrs]")
print("")
print("Options:")
print(" -h, --help Show this message")
print(" -v, --version Output DEPLOY.py version only")
print(" --hash=xxxx Deploy a specific hash (default:latest).")
print(" --skipcheck Bypass hash.left(3) check.")
print(" --printlogexp (experimental) output 10 recent deployments from log")
print("")
PrintAndQuit("To configure deployables, and more info, see " + HOME_URL)
elif execMode == "version":
PrintAndQuit(VERSION)
elif execMode == "status" or (not appEnvName):
if execMode != "status":
print("DEPLOY.py version " + VERSION)
print("Supply additional 'deployable_name' argrment. Configured deployables follow.")
print("For more options, `DEPLOY.py --help`")
print("DEPLOY.py version " + VERSION + " -- for options, `DEPLOY.py --help`")
#print("Supply additional 'deployable_name' argrment. Configured deployables follow.")
#print("For more options, `DEPLOY.py --help`")
ListDeployables()
print("")
print("To make a deployment, supply additional argument [deployablename].")
else:
DeployApp(appEnvName, commitHash, checkSum)

2 changes: 1 addition & 1 deletion python-gitdeployer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ Python script to deploy from a git repository to a local directory. Deseigned t
- Note (cp command): "*" in cp source path is a shell thing, not a command arg thing. [citation needed].
- Note: in Windows, "copy" command doesn't seem to work. Try xcopy, robocopy.

- "deployToDir": final deploy destination on the server.
- "deployToDir": final deploy destination on the server. Absolute path recommended.
13 changes: 13 additions & 0 deletions python-gitdeployer/RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Release Notes

### 1.3.3 (2023-07-15)
* Output, wording, formatting changes
* Support experimental "--printlogexp" option

### 1.3.2 (2023-02-23)
* Calling with no parameters now outputs status;
* Support --help, --version; deprecating --list;
* Support executing from different cwd.;
* BREAKING: Made createBuildCmd take/require BUILD_TEMP_DIR env variable;
* support buildCmd_successCode, Subprocess_run2() quits in unexpected returncode.
* cloneURL now properly optional.
* fixed config paths, support "~/" paths; minor output fix

### 1.2.0 (2022-12-22)
* Added --list argument, to list deployable configurations.
* Removed utils1.py, making DEPLOY.py a single-file script.
Expand Down
10 changes: 7 additions & 3 deletions python-gitdeployer/configs/config__APPENVNAME.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"//": "Config vals for GitDeployer",
"//": "Dirs are relative to DEPLOY.py; except createBuildCmd cwd=repoDir",
"//": "Detailed documentation at https://github.com/blukis/publictools/blob/main/python-gitdeployer/README.md",
"//": "Config values for GitDeployer 1.3.3",
"//": "More documentation at https://github.com/blukis/publictools/blob/main/python-gitdeployer/README.md",

"//": "Optional properties...",
"//": "- cloneUrl: git repo url to clone from, not needed if repoDir is already a git repo.",
"//": "- buildCmd_successCode: If createBuildCmd return code is something other than '0' to indicate success, override with this parameter.",
"//cloneUrl": "https://github.com/PATH",
"//buildCmd_successCode": 0,

"//": "Required properties...",
"//": "- repoDir, branchName: repo/branch to git pull from; relative paths are relative to DEPLOY.py",
"//": "- createBuildCmd: command string, run from repoDir, to create a build. Must include the destination dir as env variable 'TEMP_BUILD_DIR'. Relative path is relative to repoDir.",
"//": "- deployToDir: Once build command completes, build is copied from $TEMP_BUILD_DIR to this final (live) destination. Absolute path recommended.",
"repoDir": "repos/REPODIR_OR_ANY_PATH",
"branchName": "main",
"createBuildCmd": "rsync -a source/ $TEMP_BUILD_DIR",
Expand Down

0 comments on commit 0196500

Please sign in to comment.