-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstallUpgrades.py
148 lines (118 loc) · 5.43 KB
/
InstallUpgrades.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
* File: InstallUpgrades.py
* Copyright (c) 2023 Loupe
* https://loupe.team
*
* This file is part of ASPython, licensed under the MIT License.
'''
"""
@title InstallUpgrades
@description This python script takes in command line arguments
and installs all AS upgrades in the specified directory
"""
#Python Modules
import argparse
import logging
import sys
import ctypes
import os
import subprocess
# import psutil # This is a third party library
from _version import __version__
# Sample call for this script:
# python InstallUpgrades.py "C:/Temp/downloading" -brp "C:\BrAutomation" -asp "C:\BrAutomation\AS49" -l INFO
# def getService(name):
# service = None
# try:
# service = psutil.win_service_get(name)
# service = service.as_dict()
# except Exception as ex:
# print(str(ex))
# return service
def installBRUpgrade(upgrade:str, brPath:str, asPath:str):
commandLine = []
commandLine.append(upgrade)
commandLine.append('-G=')
commandLine.append('"' + brPath + '"')
if brPath in asPath:
commandLine.append('-V=')
commandLine.append('"' + asPath + '"')
else:
commandLine.append('-V=')
commandLine.append('"' + brPath + '\\' + asPath + '"')
commandLine.append('-R')
# commandLine.append('Y')
# Execute the process, and retrieve the process object.
logging.info('Started installing upgrade ' + upgrade)
logging.info(commandLine)
process = subprocess.run(' '.join(commandLine), shell=False, capture_output=True)
# returncode = os.system(' '.join(commandLine))
# process = subprocess.CompletedProcess(' '.join(commandLine), returncode)
if process.returncode == 0:
logging.info('Finished install upgrade ' + upgrade)
else:
logging.error('Error while installing upgrade ' + upgrade + ' (return code = ' + str(process.returncode) + ')')
logging.debug('stderr: ' + str(process.stderr))
logging.debug('stdout: ' + str(process.stdout))
return process.returncode
def main():
#parse arguments from the command line
parser = argparse.ArgumentParser(description='Install AS upgrades')
parser.add_argument('upgradePath', type=str, help='Path to single upgrade or a folder containing upgrades')
parser.add_argument('-brp','--brpath', type=str, help='Global AS install path', default='C:\\BrAutomation')
parser.add_argument('-asp','--aspath', type=str, help='AS install path for the desired AS version')
parser.add_argument('-r', '--recursive', action='store_true', help='Recursively search for upgrades in the upgrade path')
parser.add_argument('-l', '--logLevel', type=str.upper, help='Log level', choices=['DEBUG','INFO','WARNING', 'ERROR'], default='')
parser.add_argument('-v', '--version', action='version', version='%(prog)s {version}'.format(version=__version__))
args = parser.parse_args()
# Allow setting log level via command line
if(args.logLevel):
lognum = getattr(logging, args.logLevel)
if not isinstance(lognum, int):
raise ValueError('Invalid log level: %s' % args.logLevel)
logging.getLogger().setLevel(level=lognum)
#save parsed information in to variables.
logging.debug('The upgrades Path is: %s', args.upgradePath)
logging.debug('The global AS install path is: %s', args.brpath)
logging.debug('The local AS install path is: %s', args.aspath)
logging.debug('The log level will be: %s', args.logLevel)
try:
is_admin = os.getuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
# service = getService('BrUpgrSrvAS45')
# print(service)
# upgradeServiceStatus = os.system('BR.AS.UpgradeService sshd status')
# logging.debug('Upgrade status %i', upgradeServiceStatus)
# upgradeServiceStatus = os.system('systemctl is-active --quiet BrUpgrSrvAS45')
# logging.debug('Upgrade status %i', upgradeServiceStatus)
logging.debug('Terminal is admin: %i', is_admin)
if not is_admin:
logging.error('Admin privileges required. Open terminal with as Administrator')
sys.exit(1)
if os.path.isdir(args.upgradePath):
# Move into upgrade folder.
os.chdir(args.upgradePath)
if args.recursive:
for root, dirs, files in os.walk(args.upgradePath):
for upgrade in files:
if upgrade.lower().endswith('.exe'):
installBRUpgrade(os.path.join(root, upgrade), args.brpath, args.aspath)
else:
for upgrade in os.listdir():
# If the item is a .exe file, try to install it.
if os.path.isfile(upgrade) and upgrade.lower().endswith('.exe'):
installBRUpgrade(upgrade, args.brpath, args.aspath)
elif os.path.isfile(args.upgradePath) and args.upgradePath.lower().endswith('.exe'):
os.chdir(os.path.dirname(args.upgradePath)) # We do this to match the case above
installBRUpgrade(args.upgradePath, args.brpath, args.aspath)
else:
logging.error('Path provided neither an upgrade or a directory: ' + args.upgradePath)
sys.exit(args.upgradePath + ' is not a valid AS upgrade')
sys.exit(0)
if __name__ == "__main__":
#configure colored logger
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
main()