From 16d02d4c2135da0aee8746250151d65a04fceb39 Mon Sep 17 00:00:00 2001 From: Ethan Weber Date: Sat, 10 Sep 2022 14:33:43 -0700 Subject: [PATCH] Maintain a viewer version (#347) * initial commit * starting deploy script * filename update * requirements * file * branch name * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * lazy commit * Update viewer_build_deploy.yml * reading version from package json * still deploy to branches * update version --- .github/workflows/viewer_build_deploy.yml | 45 +++++-- nerfactory/viewer/app/package.json | 2 +- nerfactory/viewer/app/requirements.txt | 2 + nerfactory/viewer/app/run_deploy.py | 138 ++++++++++++++++++++++ pyproject.toml | 1 + 5 files changed, 176 insertions(+), 12 deletions(-) create mode 100644 nerfactory/viewer/app/requirements.txt create mode 100644 nerfactory/viewer/app/run_deploy.py diff --git a/.github/workflows/viewer_build_deploy.yml b/.github/workflows/viewer_build_deploy.yml index 1c003ea98..c6d7cd357 100644 --- a/.github/workflows/viewer_build_deploy.yml +++ b/.github/workflows/viewer_build_deploy.yml @@ -46,6 +46,16 @@ jobs: SSH_KEY: ${{secrets.SSH_KEY}} steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.8.12 + uses: actions/setup-python@v4 + with: + python-version: '3.8.12' + + - name: Install dependencies + run: | + pip install -r ./nerfactory/viewer/app/requirements.txt + - name: Download artifact uses: actions/download-artifact@v2 with: @@ -62,14 +72,27 @@ jobs: shell: bash run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV - - run: mkdir ~/.ssh - - run: 'echo "$SSH_KEY" >> ~/.ssh/github_actions_user_key_filename' - - run: chmod 400 ~/.ssh/github_actions_user_key_filename - - run: echo -e "Host github_action_remote_machine\n\tUser eweb0124\n\tHostname 34.102.68.79\n\tIdentityFile ~/.ssh/github_actions_user_key_filename\n\tStrictHostKeyChecking No" >> ~/.ssh/config - - run: ssh github_action_remote_machine 'rm -rf /home/eweb0124/build' - # ${{ env.BRANCH_NAME }} is the branch name - - run: ssh github_action_remote_machine 'mkdir -p /home/eweb0124/viewer/branch/${{ env.BRANCH_NAME }}' - - run: ssh github_action_remote_machine 'rm -rf /home/eweb0124/viewer/branch/${{ env.BRANCH_NAME }}' - - run: scp -r ./nerfactory/viewer/app/build github_action_remote_machine:/home/eweb0124/ - # TODO(ethan): move the current version to an older folder - - run: ssh github_action_remote_machine 'mv /home/eweb0124/build /home/eweb0124/viewer/branch/${{ env.BRANCH_NAME }}' + # TODO: detect file or scheme changes of the viewer and only + # increment the version.txt file when there is a change. + # Update the version.txt code and push to master when things change. + # https://github.com/marketplace/actions/changed-files + - name: Run changed-files with defaults on the dir1 + id: changed-files-for-dir1 + uses: tj-actions/changed-files@v29.0.3 + with: + path: nerfactory/viewer/app + + - name: List all added files in dir1 + run: | + for file in ${{ steps.changed-files-for-dir1.outputs.modified_files }}; do + echo "$file was modified" + done + + - run: | + python ./nerfactory/viewer/app/run_deploy.py \ + --branch-name ${{ env.BRANCH_NAME }} \ + --ssh-key-string "$SSH_KEY" \ + --local-folder ./nerfactory/viewer/app/build \ + --package-json-filename ./nerfactory/viewer/app/package.json \ + --increment-version "False" + - run: cat ~/.ssh/config diff --git a/nerfactory/viewer/app/package.json b/nerfactory/viewer/app/package.json index 6b8803b3e..a5b070339 100644 --- a/nerfactory/viewer/app/package.json +++ b/nerfactory/viewer/app/package.json @@ -1,7 +1,7 @@ { "name": "viewer", "homepage": ".", - "version": "0.0.0", + "version": "22-09-10-0", "private": true, "dependencies": { "@emotion/react": "^11.10.4", diff --git a/nerfactory/viewer/app/requirements.txt b/nerfactory/viewer/app/requirements.txt new file mode 100644 index 000000000..851b17347 --- /dev/null +++ b/nerfactory/viewer/app/requirements.txt @@ -0,0 +1,2 @@ +dcargs>=0.2.6 +sshconf==0.2.5 \ No newline at end of file diff --git a/nerfactory/viewer/app/run_deploy.py b/nerfactory/viewer/app/run_deploy.py new file mode 100644 index 000000000..64893cf19 --- /dev/null +++ b/nerfactory/viewer/app/run_deploy.py @@ -0,0 +1,138 @@ +# Copyright 2022 The Plenoptix Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Code for deploying the built viewer folder to a server and handing versioning. +We use the library sshconf (https://github.com/sorend/sshconf) for working with the ssh config file. +""" +import json +import os +import subprocess +from os.path import expanduser +from pathlib import Path +from typing import Optional + +import dcargs +from sshconf import empty_ssh_config_file, read_ssh_config + + +def run_cmd(cmd: str): + """Run a command in the terminal.""" + print("cmd:", cmd) + print("output:") + subprocess.Popen(cmd, shell=True).wait() + + +def main( + branch_name: str = "", + ssh_key_string: Optional[str] = None, + ssh_key_filename: str = " ~/.ssh/github_actions_user_key_filename", + hostname_or_ip_address: str = "34.102.68.79", + local_folder: str = "/home/eweb0124/build", + remote_folder: str = "/home/eweb0124/viewer", + host: str = "viewer_deploy_host", + user: str = "eweb0124", + package_json_filename: str = "package.json", + increment_version: str = "False", +): + """Copy a local folder to a remote machine and handle versioning. + + Args: + ssh_key: The private ssh key needed to ssh. + hostname_or_ip_address: The hostname or ip_address of the remote machine. + """ + + print() + print("branch_name", branch_name) + print("ssh_key_string", ssh_key_string) + print("ssh_key_filename", ssh_key_filename) + print("hostname_or_ip_address", hostname_or_ip_address) + print("local_folder", local_folder) + print("remote_folder", remote_folder) + print("host", host) + print("user", user) + print("package_json_filename", package_json_filename) + print("increment_version", increment_version) + print() + + # save the ssh key to a file + run_cmd("mkdir ~/.ssh") + if ssh_key_string: + run_cmd(f"""rm -f {ssh_key_filename}""") + run_cmd(f"""echo "{ssh_key_string}" >> {ssh_key_filename}""") + run_cmd(f"chmod 400 {ssh_key_filename}") + + # setup the config in ~/.ssh/config + config_filename = expanduser("~/.ssh/config") + Path(config_filename).parent.mkdir(exist_ok=True) + try: + config = read_ssh_config(config_filename) + except FileNotFoundError: + config = empty_ssh_config_file() + config.write(config_filename) + config = read_ssh_config(config_filename) + + # add the host if it doesn't exist + if not config.host(host): + config.add(host) + + config.set( + host, + Hostname=hostname_or_ip_address, + User=user, + IdentityFile=ssh_key_filename, + StrictHostKeyChecking="No", + ) + + # show that the config is correct + run_cmd("cat ~/.ssh/config") + + # save the config file + config.save() + + # get the version + with open(package_json_filename, "r", encoding="utf-8") as f: + package_json = json.load(f) + + # TODO: add logic to increment the version number + if increment_version == "True": + raise NotImplementedError() + else: + version = package_json["version"] + + print(f"\nusing version: {version}") + + # write to the /home/eweb0124/build folder + run_cmd(f"""ssh {host} 'rm -rf /home/eweb0124/build'""") + run_cmd(f"""scp -r {local_folder} {host}:/home/eweb0124/build""") + + # update the symlink of latest + if branch_name == "master": + + # move the build folder to the correct location + run_cmd(f"""ssh {host} 'rm -rf {remote_folder}/{version}'""") + run_cmd(f"""ssh {host} 'cp -R /home/eweb0124/build {remote_folder}/{version}'""") + + run_cmd(f"""ssh {host} 'rm {remote_folder}/latest'""") + run_cmd(f"""ssh {host} 'ln -s {remote_folder}/{version} {remote_folder}/latest'""") + + # otherwise just move to some branch folder + else: + updated_branch_name = branch_name.replace("/", "-") + run_cmd(f"""ssh {host} 'rm -rf {remote_folder}/branch/{updated_branch_name}'""") + run_cmd(f"""ssh {host} 'cp -R /home/eweb0124/build {remote_folder}/branch/{updated_branch_name}'""") + + +if __name__ == "__main__": + dcargs.cli(main) diff --git a/pyproject.toml b/pyproject.toml index 49d62a7b4..9e88b30b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,7 @@ dependencies = [ "Documentation" = "https://plenoptix-nerfactory.readthedocs-hosted.com/en/latest/?badge=latest" [project.optional-dependencies] +# for building the docs docs = [ "furo==2022.4.7", "readthedocs-sphinx-search==0.1.2",