This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgng.py
106 lines (97 loc) · 3.18 KB
/
gng.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
import argparse
#import toml
import pytoml as toml
import os
from argparse import ArgumentParser
from pivnet import PivNetUpdater, DBDumper, PivNetDownloader, PivNetUploader
parser = ArgumentParser()
parser.add_argument(
"--update",
action='store_true',
help="updates a local sqlite database of all products, releases, and files from Pivotal Network")
parser.add_argument(
"--dump-list",
nargs=1,
metavar='filename',
action="store",
help="Create a list of files from the local sql lite database so user can cut / paste the files they want to download")
parser.add_argument(
"--download",
nargs=1,
metavar="filename",
help="Parse the user-created file of downloads and download them to a path")
parser.add_argument(
"--upload",
nargs=1,
metavar="filename",
help="Scan a path of files, make sure they are valid (or ignore w/ --force) and upload to Ops Managers")
parser.add_argument(
"--path",
nargs=argparse.REMAINDER,
metavar="path",
help="Path to which either upload or download will happen")
parser.add_argument(
"--force",
action="store_true",
help="Forcing to updload the files which are not existing in DB")
conf = "conf.toml"
args = parser.parse_args()
vargs = vars(args)
# print(vargs)
if not os.path.exists(conf):
print("no valid conf.toml with key")
exit(-1)
with open(conf) as conffile:
config = toml.loads(conffile.read())
api_key = config.get('api_key')
if not api_key or len(api_key) <= 0:
print("no valid conf.toml with key")
exit(-1)
if "update" in vargs and vargs["update"]:
msg = PivNetUpdater(api_key).update_db()
if msg:
print(msg)
elif "dump_list" in vargs and vargs["dump_list"]:
filename = vargs["dump_list"][0]
print("Going to dump list to " + filename)
DBDumper().dump_list(filename)
elif "download" in vargs and vargs["download"]:
filename = vargs["download"][0]
if not os.path.exists(filename):
print('download list is required')
exit(-1)
if "path" in vargs and vargs["path"]:
download_path = vargs["path"][0]
if not os.path.exists(download_path):
print("Path is not valid")
exit(-1)
PivNetDownloader(api_key).download_files(filename, download_path)
else:
print("Path is required")
elif "upload" in vargs and vargs["upload"]:
filename = vargs["upload"][0]
if not filename or not os.path.exists(filename):
print('ops manager target list is required')
exit(-1)
try:
with open(filename, 'rb') as conffile:
opsmgr_config = toml.load(conffile)
except Exception as e:
print('Error: %s, %s' % (e.message, e.args))
print('Incorrect TOML format')
exit(-1)
if "path" in vargs and vargs["path"]:
upload_folder = vargs["path"][0]
if not os.path.exists(upload_folder):
print("Path is not valid")
exit(-1)
force = "force" in vargs
msg = PivNetUploader().upload_files(
config=opsmgr_config, folder_path=upload_folder, force=force)
if msg:
print(msg)
exit(-1)
else:
print("Path is required")
else:
parser.print_help()