Skip to content

Commit

Permalink
Maintain a viewer version (nerfstudio-project#347)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ethanweber authored Sep 10, 2022
1 parent adf249b commit 16d02d4
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 12 deletions.
45 changes: 34 additions & 11 deletions .github/workflows/viewer_build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/[email protected]
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
2 changes: 1 addition & 1 deletion nerfactory/viewer/app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "viewer",
"homepage": ".",
"version": "0.0.0",
"version": "22-09-10-0",
"private": true,
"dependencies": {
"@emotion/react": "^11.10.4",
Expand Down
2 changes: 2 additions & 0 deletions nerfactory/viewer/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dcargs>=0.2.6
sshconf==0.2.5
138 changes: 138 additions & 0 deletions nerfactory/viewer/app/run_deploy.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 16d02d4

Please sign in to comment.