Add deploy option
This commit is contained in:
parent
aa64c6dbd2
commit
1197228650
8
cli.py
8
cli.py
@ -62,9 +62,11 @@ def cli():
|
|||||||
remote_blocklist = blocklist_json_to_instances(json.load(f))
|
remote_blocklist = blocklist_json_to_instances(json.load(f))
|
||||||
else:
|
else:
|
||||||
remote_blocklist = load_remote_blocklist(server=args.server, token=args.token)
|
remote_blocklist = load_remote_blocklist(server=args.server, token=args.token)
|
||||||
|
if args.action == "diff":
|
||||||
Instance.show_diff(local_blocklist, remote_blocklist)
|
Instance.show_diffs(local_blocklist, remote_blocklist)
|
||||||
|
elif args.action == "deploy":
|
||||||
|
diffs = Instance.list_diffs(local_blocklist, remote_blocklist)
|
||||||
|
Instance.apply_blocks_from_diff(diffs, args.server, args.token)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cli()
|
cli()
|
||||||
|
56
models.py
56
models.py
@ -1,3 +1,6 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class Instance:
|
class Instance:
|
||||||
def __init__(self, instance_dict):
|
def __init__(self, instance_dict):
|
||||||
"""If obfuscate, reject_media or reject_reports are not specified default to False"""
|
"""If obfuscate, reject_media or reject_reports are not specified default to False"""
|
||||||
@ -28,7 +31,9 @@ class Instance:
|
|||||||
self.severity = instance_dict["severity"]
|
self.severity = instance_dict["severity"]
|
||||||
self.public_comment = instance_dict["public_comment"]
|
self.public_comment = instance_dict["public_comment"]
|
||||||
self.private_comment = instance_dict["private_comment"]
|
self.private_comment = instance_dict["private_comment"]
|
||||||
self.remote = True
|
self.obfuscate = instance_dict["obfuscate"]
|
||||||
|
self.reject_media = instance_dict["reject_media"]
|
||||||
|
self.reject_reports = instance_dict["reject_reports"]
|
||||||
|
|
||||||
def parse_local_block(self, instance_dict):
|
def parse_local_block(self, instance_dict):
|
||||||
self.name = instance_dict["name"]
|
self.name = instance_dict["name"]
|
||||||
@ -36,13 +41,40 @@ class Instance:
|
|||||||
self.severity = instance_dict["severity"]
|
self.severity = instance_dict["severity"]
|
||||||
self.public_comment = instance_dict["public_comment"]
|
self.public_comment = instance_dict["public_comment"]
|
||||||
self.private_comment = instance_dict["private_comment"]
|
self.private_comment = instance_dict["private_comment"]
|
||||||
self.remote = False
|
try:
|
||||||
|
self.obfuscate = instance_dict["obfuscate"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
self.reject_media = instance_dict["reject_media"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
self.reject_reports = instance_dict["reject_reports"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
def apply(self, instance, token):
|
def apply(self, server, token, block_id=None):
|
||||||
pass
|
headers = {
|
||||||
|
f'Authorization': f'Bearer {token}',
|
||||||
|
}
|
||||||
|
data = {"domain": self.domain,
|
||||||
|
"severity": self.severity,
|
||||||
|
"reject_media": self.reject_media,
|
||||||
|
"reject_reports": self.reject_reports,
|
||||||
|
"private_comment": self.private_comment,
|
||||||
|
"public_comment": self.public_comment,
|
||||||
|
"obfuscate": self.obfuscate}
|
||||||
|
"""If no id is given add a new block, else update the existing block"""
|
||||||
|
if block_id is None:
|
||||||
|
response = requests.post(f'https://{server}/api/v1/admin/domain_blocks', data=data, headers=headers)
|
||||||
|
else:
|
||||||
|
response = requests.post(f'https://{server}/api/v1/admin/domain_blocks/{block_id}', data=data, headers=headers)
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise ConnectionError(f"Could not apply block ({response.status_code}: {response.reason})")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_diff(local_blocklist, remote_blocklist):
|
def list_diffs(local_blocklist, remote_blocklist):
|
||||||
diffs = []
|
diffs = []
|
||||||
for local_instance in local_blocklist:
|
for local_instance in local_blocklist:
|
||||||
instance_found = False
|
instance_found = False
|
||||||
@ -65,7 +97,17 @@ class Instance:
|
|||||||
return diffs
|
return diffs
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def show_diff(local_blocklist, remote_blocklist):
|
def apply_blocks_from_diff(diffs, server, token):
|
||||||
|
for diff in diffs:
|
||||||
|
if diff["local"] is None:
|
||||||
|
pass
|
||||||
|
elif diff["remote"] is None:
|
||||||
|
diff["local"].apply(server, token)
|
||||||
|
else:
|
||||||
|
diff["local"].apply(server, token, block_id=diff["remote"].id)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def show_diffs(local_blocklist, remote_blocklist):
|
||||||
from rich.table import Table
|
from rich.table import Table
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
table = Table(title="Differences", expand=True, show_lines=True)
|
table = Table(title="Differences", expand=True, show_lines=True)
|
||||||
@ -73,7 +115,7 @@ class Instance:
|
|||||||
table.add_column("Domain", style="cyan")
|
table.add_column("Domain", style="cyan")
|
||||||
table.add_column("Current remote status", style="magenta")
|
table.add_column("Current remote status", style="magenta")
|
||||||
table.add_column("Local status", style="green")
|
table.add_column("Local status", style="green")
|
||||||
diffs = Instance.list_diff(local_blocklist, remote_blocklist)
|
diffs = Instance.list_diffs(local_blocklist, remote_blocklist)
|
||||||
for diff in diffs:
|
for diff in diffs:
|
||||||
if diff["local"] is None:
|
if diff["local"] is None:
|
||||||
table.add_row(diff["remote"].domain, None, diff["remote"].status_str())
|
table.add_row(diff["remote"].domain, None, diff["remote"].status_str())
|
||||||
|
Loading…
Reference in New Issue
Block a user