🚧 This instance is under construction; expect occasional downtime. Runners available in /repos. Questions? Ask in #wikimedia-gitlab on libera.chat, or under GitLab on Phabricator.

Commit ea4e1e2d authored by Brennen Bearnes's avatar Brennen Bearnes
Browse files

move scripts & data to top level, add __init__.py and util.py

Moves user-facing commands and configuration to top of repo, moves a few
common functions into a util.py.
parent 135b7637
#!/usr/bin/env python3
"""
apply-settings - apply settings from settings.yaml to GitLab instance
"""
import copy
import json
import os
import gitlab
import requests
import sys
import yaml
import gitlab_settings
from gitlab_settings.util import confirm
from gitlab_settings.util import diffsettings
from gitlab_settings.util import get_token
token = get_token()
url = 'https://gitlab.wikimedia.org/api/v4/application/settings'
headers = {
'User-Agent': 'gitlab-settings/0.0.1',
'PRIVATE-TOKEN': token
}
# Get a baseline list of settings from API for diffing:
r = requests.get(url, headers=headers)
r.raise_for_status()
original_instance_settings = r.json()
# Get the settings we want to apply from YAML file in the repo:
with open('settings.yaml') as f:
local_settings = yaml.safe_load(f)
print()
print("Difference between local settings file and instance settings:")
desired_settings = copy.deepcopy(original_instance_settings)
desired_settings.update(local_settings)
diffsettings(original_instance_settings, desired_settings)
print()
if not confirm('Does the diff look good?'):
print("Exiting without changes.")
exit()
if not confirm('Did you log these changes in #wikimedia-releng?'):
print("Exiting without changes.")
exit()
# Do some type juggling so we pass strings instead of booleans to the API:
for x, y in local_settings.items():
if y == True:
local_settings[x] = 'true'
elif y == False:
local_settings[x] = 'false'
try:
r = requests.put(url, headers=headers, json=local_settings)
r.raise_for_status()
new_instance_settings = r.json()
except:
print(r.text)
exit()
# Display diff of applied settings:
print()
print("Changed settings: ")
diffsettings(original_instance_settings, new_instance_settings)
#!/usr/bin/env python3
"""
gitlab-settings - apply settings from settings.yaml to GitLab instance
"""
import copy
import difflib
import getpass
import json
import os
import requests
import sys
import yaml
import json
import getpass
def diffsettings(old, new):
d = difflib.Differ()
old_lines = json.dumps(old, indent=4).splitlines()
new_lines = json.dumps(new, indent=4).splitlines()
for thing in d.compare(old_lines, new_lines):
if thing.startswith('-') or thing.startswith('+') or thing.startswith('?'):
print(thing)
def get_token():
return getpass.getpass('GitLab token with api scope: ').strip()
def confirm(question="Continue?", default=False):
"""
......@@ -59,50 +44,10 @@ def confirm(question="Continue?", default=False):
return result
token = getpass.getpass('GitLab token with api scope: ').strip()
url = 'https://gitlab.wikimedia.org/api/v4/application/settings'
headers = {
'User-Agent': 'gitlab-settings/0.0.1',
'PRIVATE-TOKEN': token
}
# Get a baseline list of settings from API for diffing:
r = requests.get(url, headers=headers)
r.raise_for_status()
original_instance_settings = r.json()
# Get the settings we want to apply from YAML file in the repo:
with open('settings.yaml') as f:
local_settings = yaml.safe_load(f)
print()
print("Difference between local settings file and instance settings:")
desired_settings = copy.deepcopy(original_instance_settings)
desired_settings.update(local_settings)
diffsettings(original_instance_settings, desired_settings)
print()
if confirm('Does the diff look good?'):
if confirm('Did you log these changes in #wikimedia-releng?'):
# Do some type juggling so we pass strings instead of booleans to the API:
for x, y in local_settings.items():
if y == True:
local_settings[x] = 'true'
elif y == False:
local_settings[x] = 'false'
try:
r = requests.put(url, headers=headers, json=local_settings)
r.raise_for_status()
new_instance_settings = r.json()
except:
print(r.text)
exit()
# Display diff of applied settings:
print()
print("Changed settings: ")
diffsettings(original_instance_settings, new_instance_settings)
else:
print("Exiting without changes.")
def diffsettings(old, new):
d = difflib.Differ()
old_lines = json.dumps(old, indent=4).splitlines()
new_lines = json.dumps(new, indent=4).splitlines()
for thing in d.compare(old_lines, new_lines):
if thing.startswith('-') or thing.startswith('+') or thing.startswith('?'):
print(thing)
......@@ -6,14 +6,14 @@
import copy
import difflib
import getpass
import json
import os
import requests
import sys
import yaml
from gitlab_settings.util import get_token
token = getpass.getpass('GitLab token with api scope: ').strip()
token = get_token
url = 'https://gitlab.wikimedia.org/api/v4/application/settings'
headers = {
'User-Agent': 'gitlab-settings/0.0.1',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment