Skip to content

Commit

Permalink
added downloadURL & added config ability
Browse files Browse the repository at this point in the history
  • Loading branch information
emoscardini committed Jan 17, 2024
1 parent e22731a commit 7b557f1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2024-01-11

### Added

- Added downloadUrl argument
- Added local_config option to specify alternate base URL

## [1.2.0] - 2023-10-04

### Changed
Expand Down
59 changes: 51 additions & 8 deletions router_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import urllib3

def start_upgrade(requested_version,
download_url,
auto=False,
override=False):
"""
Expand Down Expand Up @@ -49,7 +50,7 @@ def start_upgrade(requested_version,
check_update_systemd(requested_version)

# download and extract file
download_bundle(requested_version)
download_bundle(download_url)

for service in service_list:
# check if service is enabled
Expand All @@ -61,6 +62,37 @@ def start_upgrade(requested_version,
else:
print("Already up to date")

def local_config(requested_version):
"""
Attempt to load config file & return download url
"""
download_url=("https://github.com/openziti/ziti/releases/download/v" + requested_version +
"/ziti-linux-amd64-" + requested_version + ".tar.gz")
# load custom url from file
if os.path.exists("/etc/script_config.json"):
try:
with open("/etc/script_config.json", 'r', encoding='utf-8') as config_file:
config = json.load(config_file)
if "repository_base_url" in config:
base_url = config.get('repository_base_url')
print("Loading repository_base_url from /etc/script_config.json")
print("Value: " + base_url)
download_url = (base_url +
"/openziti/ziti/releases/download/v" +
requested_version +
"/ziti-linux-amd64-" +
requested_version +
".tar.gz")
return download_url
print("WARNING: found /etc/script_config.json, "
"but was unable to find any keys")
return download_url
except json.JSONDecodeError:
print("WARNING: Unable to parse json file /etc/script_config.json")
return download_url
else:
return download_url

def check_update_systemd(ziti_version):
"""
Check
Expand All @@ -84,12 +116,11 @@ def downgrade_check(requested_version, running_version):
print("\033[0;31mERROR: Unable to downgrade, version is lower than 0.27.0")
sys.exit(1)

def download_bundle(ziti_version):
def download_bundle(download_url):
"""
Download ziti bundle & extract files
"""
download_url=("https://github.com/openziti/ziti/releases/download/v" + ziti_version +
"/ziti-linux-amd64-" + ziti_version + ".tar.gz")

try:
print("Downloading bundle")
file_name="router_upgrade.tar.gz"
Expand Down Expand Up @@ -279,15 +310,17 @@ def update_systemd_unitfile(binary_name):
"""
service_unit = "/etc/systemd/system/ziti-" + binary_name + ".service"
logging.debug("Update systemd unit file")
print("\033[0;31mWARN:\033[0m Upgraded to 0.27.0 and above. You can't use this program to downgrade to lower versions")
print("\033[0;31mWARN:\033[0m Upgraded to 0.27.0 and above. "
"You can't use this program to downgrade to lower versions")
try:
with open(service_unit, 'r',encoding='UTF-8') as openfile:
lines = openfile.readlines()
# Find the line to update
for i, line in enumerate(lines):
if line.startswith('ExecStart='):
if binary_name == "router":
lines[i] = ("ExecStart=/opt/netfoundry/ziti/ziti router run /opt/netfoundry/ziti/ziti-router/config.yml\n")
lines[i] = ("ExecStart=/opt/netfoundry/ziti/ziti router run "
"/opt/netfoundry/ziti/ziti-router/config.yml\n")
if binary_name == "tunnel":
lines[i] = 'ExecStart=/opt/netfoundry/ziti/ziti tunnel run\n'
break
Expand Down Expand Up @@ -346,7 +379,7 @@ def main():
"""
Main logic
"""
__version__ = '1.2.0'
__version__ = '1.3.0'
# Change log
# See https://github.com/netfoundry/edge-router-upgrade/blob/main/CHANGELOG.md

Expand All @@ -362,6 +395,8 @@ def main():
parser.add_argument('-d', '--debug',
action='store_true',
help='enable debug log in log file output')
parser.add_argument('--downloadUrl', type=str,
help='Specify bundle to download')
parser.add_argument('-v', '--version',
action='version',
version=__version__)
Expand Down Expand Up @@ -396,7 +431,15 @@ def main():
requested_version = get_ziti_controller_version("https://" + controller_ip)
override = False

start_upgrade(requested_version,
# determine download url
if args.DownloadUrl:
download_url = args.downloadUrl
else:
download_url = local_config(requested_version)

# start upgrade
start_upgrade(download_url,
requested_version,
auto_upgrade,
override)

Expand Down
39 changes: 36 additions & 3 deletions router_upgrade_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import time
import json
import subprocess
import tarfile
from tqdm import tqdm
Expand All @@ -29,6 +30,38 @@ def compare_dates(file_name):
return True
return False

def local_config():
"""
Attempt to load config file & return download url
"""
download_url = ("https://github.com/netfoundry/edge-router-upgrade/"
"releases/latest/download/router_upgrade.tar.gz")
# load custom url from file
if os.path.exists("/etc/script_config.json"):
try:
with open("/etc/script_config.json", 'r', encoding='utf-8') as config_file:
config = json.load(config_file)
if "upgrade_script_url" in config:
download_url = config.get('upgrade_script_url')
print("Loading upgrade_script_url from /etc/script_config.json")
print("Value:" + download_url)
return download_url
if "repository_base_url" in config:
base_url = config.get('repository_base_url')
print("Loading repository_base_url from /etc/script_config.json")
print("Value: " + base_url)
download_url = (base_url + "/edge-router-upgrade/releases/" +
"latest/download/router_upgrade.tar.gz")
return download_url
print("WARNING: found /etc/script_config.json, "
"but was unable to find any keys")
return download_url
except json.JSONDecodeError:
print("WARNING: Unable to parse json file /etc/script_config.json")
return download_url
else:
return download_url

def download_file(source_url):
"""
Download file & extract
Expand Down Expand Up @@ -69,14 +102,13 @@ def main():
#__version__ = '1.0.0'
# change log
# 1.0.0 - initial release
# 1.1.0 - Added config file

# define static variables
router_upgrade_script = "/opt/netfoundry/.router_upgrade"
download_url = ("https://github.com/netfoundry/edge-router-upgrade/"
"releases/latest/download/router_upgrade.tar.gz")

# run root check
#root_check()
root_check()

# only compare if file exists
do_update = False
Expand All @@ -91,6 +123,7 @@ def main():
# only download if update is needed
if do_update:
cleanup_file(router_upgrade_script)
download_url = local_config()
download_file(download_url)

# run script
Expand Down

0 comments on commit 7b557f1

Please sign in to comment.