Skip to content

Commit

Permalink
Store deploy related information in deploy.json
Browse files Browse the repository at this point in the history
  • Loading branch information
KenwoodFox authored and virtuald committed Mar 4, 2022
1 parent 9d9eb00 commit 507a907
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pyfrc/mains/cli_deploy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import argparse
import contextlib
import subprocess
import datetime
import socket
import inspect
import json
import os
import sys
import re
Expand Down Expand Up @@ -205,6 +209,42 @@ def run(self, options, robot_class, **static_options):
print("\nSUCCESS: Deploy was successful!")
return 0

def _generate_build_data(self, robot_path) -> dict:
"""
Generate a deploy.json
"""

deploy_data = {
"build-host": socket.gethostname(), # os.uname doesn't work on systems that use non-unix os
"builder": os.getlogin(),
"path": robot_path,
"build-date": datetime.datetime.now().replace(microsecond=0).isoformat(),
}

# Test if we're in a git repo or not
revParseProcess = subprocess.run(
args=["git", "rev-parse", "--is-inside-work-tree"],
capture_output=True,
)

# If we're in a git repo
if revParseProcess.stdout.decode().strip() == "true":
try:
# Describe this repo
repoProcess = subprocess.run(
args=["git", "describe", "--dirty=-dirty", "--always"],
capture_output=True,
)

# Insert this data into our deploy.json dict
deploy_data["git"] = repoProcess.stdout.decode().strip()
except subprocess.CalledProcessError as e:
logging.exception(e)
else:
logging.info("Not including git hash in deploy.json: Not a git repo.")

return deploy_data

def _check_large_files(self, robot_path):

large_sz = 250000
Expand Down Expand Up @@ -390,7 +430,14 @@ def _do_deploy(
tmp_dir = tempfile.mkdtemp()
try:
py_tmp_dir = join(tmp_dir, py_new_deploy_subdir)
# Copy robot path contents to new deploy subdir
self._copy_to_tmpdir(py_tmp_dir, robot_path)

# Copy 'build' artifacts to new deploy subdir
with open(join(py_tmp_dir, "deploy.json"), "w") as outf:
json.dump(self._generate_build_data(robot_path), outf)

# sftp new deploy subdir to robot
ssh.sftp(py_tmp_dir, deploy_dir, mkdir=not options.in_place)
finally:
shutil.rmtree(tmp_dir)
Expand Down

0 comments on commit 507a907

Please sign in to comment.