Skip to content

Commit

Permalink
Merge pull request #8 from ZihengSun/main
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
ZihengSun authored Jun 5, 2023
2 parents 986029d + 8c2e690 commit 71befb6
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pygeoweaver/__pycache__/__main__.cpython-39.pyc
pygeoweaver/__pycache__/server.cpython-39.pyc
pygeoweaver/__pycache__/utils.cpython-39.pyc
.coverage
*.pyc
11 changes: 5 additions & 6 deletions pygeoweaver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
show_history, import_workflow, list_hosts, list_processes, list_workflows, \
start, stop, reset_password, run_process, run_worklfow, helpwith
from pygeoweaver.server import show
from pygeoweaver.utils import check_java


def main():
# start geoweaver
# start()
start()
# stop geoweaver
# stop()
# list resources
#list_hosts()
#list_processes()
# list_workflows()
list_hosts()
list_processes()
list_workflows()
# show history
#show_history("ll3u3W78eOEfklxhBJ")
# detail host
Expand All @@ -40,7 +39,7 @@ def main():

# reset localhost password for Geoweaver
# reset_password()
show()
# show()
# helpwith()
# check_java()

Expand Down
180 changes: 180 additions & 0 deletions pygeoweaver/jdk_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import platform
import os
import subprocess
import shutil
import sys
import tarfile
import zipfile
import urllib.request

from pygeoweaver.utils import get_home_dir, get_java_bin_path


def install_jdk():
system = platform.system()
architecture = platform.machine()

if system == 'Darwin':
if architecture == 'x86_64':
install_jdk_macos('11.0.18-10', 'jdk_x64_mac_hotspot')
elif architecture == 'arm64':
install_jdk_macos('11.0.18-10', 'jdk_aarch64_mac_hotspot')
else:
print('Unsupported architecture.')

elif system == 'Linux':
# https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.18_10.tar.gz
# https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18_10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.18_10.tar.gz
if architecture == 'x86_64':
install_jdk_linux('11.0.18-10', 'jdk_x64_linux_hotspot')
elif architecture == 'aarch64':
install_jdk_linux('11.0.18-10', 'jdk_aarch64_linux_hotspot')
else:
print('Unsupported architecture.')

elif system == 'Windows':
if architecture == 'AMD64' or architecture == 'x86_64':
install_jdk_windows('11.0.18-10', 'jdk_x64_windows_hotspot')
elif architecture == 'x86-32':
install_jdk_windows('11.0.18-10', 'jdk_x86-32_windows_hotspot')
else:
print('Unsupported architecture.')

else:
print('Unsupported platform.')


def install_jdk_macos(jdk_version, jdk_arch):
# jdk_aarch64_linux_hotspot
jdk_url = f'https://github.com/adoptium/temurin11-binaries/releases/download/jdk-{jdk_version.replace("-", "%2B")}/OpenJDK11U-{jdk_arch}_{jdk_version.replace("-", "_")}.tar.gz'
jdk_install_dir = os.path.expanduser('~/jdk')

# Download JDK archive
download_file(jdk_url, f'{get_home_dir()}/jdk.tar.gz')

# Extract JDK archive
extract_tar_archive(f'{get_home_dir()}/jdk.tar.gz', jdk_install_dir)

# Set JDK environment variables
set_jdk_env_vars(f'{jdk_install_dir}/jdk-{jdk_version.replace("-", "+")}')


def install_jdk_linux(jdk_version, jdk_arch):
jdk_url = f'https://github.com/adoptium/temurin11-binaries/releases/download/jdk-{jdk_version.replace("-", "%2B")}/OpenJDK11U-{jdk_arch}_{jdk_version.replace("-", "_")}.tar.gz'
jdk_install_dir = os.path.expanduser('~/jdk')

# Download JDK archive
download_file(jdk_url, 'jdk.tar.gz')

# Extract JDK archive
extract_tar_archive('jdk.tar.gz', jdk_install_dir)

# Set JDK environment variables
set_jdk_env_vars(f'{jdk_install_dir}/jdk-{jdk_version.replace("-", "+")}')


def install_jdk_windows(jdk_version, jdk_arch):
jdk_url = f'https://github.com/adoptium/temurin11-binaries/releases/download/jdk-{jdk_version.replace("-", "%2B")}/OpenJDK11U-{jdk_arch}_{jdk_version.replace("-", "_")}.zip'
jdk_install_dir = os.path.expanduser('~/jdk')

# Download JDK archive
download_file(jdk_url, 'jdk.zip')

# Extract JDK archive
extract_zip_archive('jdk.zip', jdk_install_dir)

# Set JDK environment variables
set_jdk_env_vars(f'{jdk_install_dir}/jdk-{jdk_version.replace("-", "+")}')


def download_file(url, filename):
if os.path.exists(filename):
print(f'{filename} already exists.')
return
print(f'Downloading {filename}...', url)
urllib.request.urlretrieve(url, filename)
print(f'{filename} downloaded.')


def extract_tar_archive(archive_path, destination_dir):
if not os.path.exists(destination_dir):
print(f'Extracting {archive_path}...')
with tarfile.open(archive_path, 'r:gz') as tar_ref:
tar_ref.extractall(destination_dir)
print(f'{archive_path} extracted to {destination_dir}.')


def extract_zip_archive(archive_path, destination_dir):
print(f'Extracting {archive_path}...')
with zipfile.ZipFile(archive_path, 'r') as zip_ref:
zip_ref.extractall(destination_dir)
print(f'{archive_path} extracted to {destination_dir}.')


def set_jdk_env_vars(jdk_install_dir):
print(f'Setting JDK environment variables...')

with open(os.path.expanduser("~/.bashrc"), "a") as bashrc:
bashrc.write(f'\nexport JAVA_HOME="{jdk_install_dir}"\n')
bashrc.write(f'export PATH="$JAVA_HOME/bin:$PATH"\n')

print('JDK environment variables set.')
subprocess.run(['bash', '-c', 'source ~/.bashrc'])



def install_java():
system = platform.system()
if system == "Darwin":
os.system(
"/bin/bash -c '/usr/bin/ruby -e \"$(curl -fsSL "
"https://raw.githubusercontent.com/Homebrew/install/master/install)\"'")
os.system("brew install openjdk")
elif system == "Linux":
# need to check if the package manager type is apt or yum
# arch / debian
package_manager = None
if os.path.exists("/usr/bin/apt"):
package_manager = "apt"
elif os.path.exists("/usr/bin/yum"):
package_manager = "yum"

if package_manager:
os.system(f"sudo {package_manager} update")
os.system(f"sudo {package_manager} install -y default-jre default-jdk")
else:
print("Package manager not found. Unable to install Java.")
sys.exit(1)
elif system == "Windows":
# note: this requires admin access to the pc, else it will fail saying
# Access to the path 'C:\ProgramData\chocolatey\lib-bad' is denied.
os.system(
"powershell -Command \"Set-ExecutionPolicy Bypass -Scope Process -Force; ["
"System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object "
"System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))\"")
os.system("choco install -y openjdk")
else:
print("Unsupported operating system.")
sys.exit(1)



def is_java_installed():
try:
# Check if Java is installed by running "java -version" command
subprocess.run([get_java_bin_path(), "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except:
return False



def check_java():
# Check if Java is installed
if not is_java_installed():
print("Java is not installed. Installing...")
install_jdk()
print("Java installation complete.")



8 changes: 4 additions & 4 deletions pygeoweaver/sc_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
"""

import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def detail_workflow(workflow_id):
if not workflow_id:
raise RuntimeError("Workflow id is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "detail", f"--workflow-id={workflow_id}"],
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "detail", f"--workflow-id={workflow_id}"],
cwd=f"{get_root_dir()}/")


def detail_process(process_id):
if not process_id:
raise RuntimeError("Process id is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "detail", f"--process-id={process_id}"],
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "detail", f"--process-id={process_id}"],
cwd=f"{get_root_dir()}/")

def detail_host(host_id):
if not host_id:
raise RuntimeError("Host id is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "detail", f"--host-id={host_id}"],
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "detail", f"--host-id={host_id}"],
cwd=f"{get_root_dir()}/")
4 changes: 2 additions & 2 deletions pygeoweaver/sc_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def export_workflow(workflow_id, mode, target_file_path):
Expand All @@ -19,6 +19,6 @@ def export_workflow(workflow_id, mode, target_file_path):
if not workflow_id:
raise RuntimeError("Workflow id is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "export", "workflow",
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "export", "workflow",
f"--mode={mode}", workflow_id, target_file_path,],
cwd=f"{get_root_dir()}/")
4 changes: 2 additions & 2 deletions pygeoweaver/sc_help.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@


import subprocess
from pygeoweaver.utils import get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def helpwith(command_list: list=[], ):
target_cmd_args = ["java", "-jar", get_geoweaver_jar_path()]
target_cmd_args = [get_java_bin_path(), "-jar", get_geoweaver_jar_path()]
if len(command_list) > 0:
for i in range(len(command_list)-1):
target_cmd_args.append(command_list[i])
Expand Down
4 changes: 2 additions & 2 deletions pygeoweaver/sc_history.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def show_history(history_id):
Expand All @@ -9,4 +9,4 @@ def show_history(history_id):
if not history_id:
raise RuntimeError("history id is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "history", history_id], cwd=f"{get_root_dir()}/")
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "history", history_id], cwd=f"{get_root_dir()}/")
4 changes: 2 additions & 2 deletions pygeoweaver/sc_import.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def import_workflow(workflow_zip_file_path):
Expand All @@ -12,6 +12,6 @@ def import_workflow(workflow_zip_file_path):
if not workflow_zip_file_path:
raise RuntimeError("Workflow zip file path is missing")
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "import",
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "import",
"workflow", workflow_zip_file_path], cwd=f"{get_root_dir()}/")

8 changes: 4 additions & 4 deletions pygeoweaver/sc_list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def list_hosts():
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "list", "--host"], cwd=f"{get_root_dir()}/")
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "list", "--host"], cwd=f"{get_root_dir()}/")


def list_processes():
download_geoweaver_jar()
subprocess.run(["chmod", "+x", get_geoweaver_jar_path()], cwd=f"{get_root_dir()}/")
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "list", "--process"], cwd=f"{get_root_dir()}/")
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "list", "--process"], cwd=f"{get_root_dir()}/")


def list_workflows():
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "list", "--workflow"], cwd=f"{get_root_dir()}/")
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "list", "--workflow"], cwd=f"{get_root_dir()}/")

4 changes: 2 additions & 2 deletions pygeoweaver/sc_resetpassword.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def reset_password():
Expand All @@ -9,5 +9,5 @@ def reset_password():
Reset password for localhost
"""
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "resetpassword",])
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "resetpassword",])

10 changes: 5 additions & 5 deletions pygeoweaver/sc_run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import subprocess
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_root_dir
from pygeoweaver.utils import download_geoweaver_jar, get_geoweaver_jar_path, get_java_bin_path, get_root_dir


def run_process(*, process_id: str, host_id: str, password: str, environment: str=None):
Expand All @@ -12,7 +12,7 @@ def run_process(*, process_id: str, host_id: str, password: str, environment: st
environment - optional
"""
download_geoweaver_jar()
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "run", "process", f"--host={host_id}",
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "run", "process", f"--host={host_id}",
f"--password={password}", f"--environment={environment}", process_id],
cwd=f"{get_root_dir()}/")

Expand All @@ -39,23 +39,23 @@ def run_worklfow(*, workflow_id: str, workflow_folder_path: str=None, workflow_z
"folder path or zip path")

if workflow_id and not workflow_folder_path and not workflow_zip_file_path:
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
"-e", environment_list,
"-h", host_list,
"-p", password_list],
cwd=f"{get_root_dir()}/")

if workflow_folder_path and not workflow_zip_file_path:
# command to run workflow from folder
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
"-d", workflow_folder_path,
"-e", environment_list,
"-h", host_list,
"-p", password_list],
cwd=f"{get_root_dir()}/")

if not workflow_folder_path and workflow_zip_file_path:
subprocess.run(["java", "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
subprocess.run([get_java_bin_path(), "-jar", get_geoweaver_jar_path(), "run", "workflow", workflow_id,
"-e", environment_list,
"-f", workflow_zip_file_path,
"-h", host_list,
Expand Down
Loading

0 comments on commit 71befb6

Please sign in to comment.