Skip to content

Commit

Permalink
Blacken VMware ESXi Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrager committed May 21, 2021
1 parent b6048b5 commit c927105
Show file tree
Hide file tree
Showing 10 changed files with 725 additions and 461 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ __pycache__
*.orig
output-qemu
packer_cache
.ve
*~
\#*\#
.#*
Expand Down
20 changes: 18 additions & 2 deletions vmware-esxi/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
PACKER ?= packer
ISO ?= ${VMWARE_ESXI_ISO_PATH}
VENV := .ve

.PHONY: all clean
.PHONY: all lint format clean

all: vmware-esxi.dd.gz

vmware-esxi.dd.gz: clean
sudo PACKER_LOG=1 ${PACKER} build -var "vmware_esxi_iso_path=${ISO}" vmware-esxi.json
reset

$(VENV): requirements-dev.txt requirements.txt
python3 -m venv --system-site-packages --clear $@
$(VENV)/bin/pip install $(foreach r,$^,-r $(r))

lint: $(VENV)
$(eval py_files := $(shell grep -R -m1 -l '#!/usr/bin/env python' maas/ curtin/))
$(VENV)/bin/isort --check-only --diff $(py_files)
$(VENV)/bin/black --check $(py_files)
$(VENV)/bin/flake8 --ignore E203,W503 $(py_files)

format: $(VENV)
$(eval py_files := $(shell grep -R -m1 -l '#!/usr/bin/env python' maas/ curtin/))
$(VENV)/bin/isort $(py_files)
$(VENV)/bin/black -q $(py_files)

clean:
sudo ${RM} -rf output-qemu vmware-esxi.dd.gz
sudo ${RM} -rf output-qemu vmware-esxi.dd.gz $(VENV)
27 changes: 14 additions & 13 deletions vmware-esxi/curtin/curtin-hooks
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from curtin.config import load_command_config
from curtin.util import load_command_environment

from subprocess import check_call
import os
import shutil
import tempfile
from subprocess import check_call

import yaml
from curtin.config import load_command_config
from curtin.util import load_command_environment


def write_config(target, config):
Expand All @@ -43,27 +43,28 @@ def write_config(target, config):
# the booted system. Use the block device for the partition Curtin was
# found on and change the partition to find /altbootbank which is
# visible on the booted system.
with open('/proc/mounts', 'r') as mounts:
with open("/proc/mounts", "r") as mounts:
for line in mounts.readlines():
mount = line.split()
blkdev = mount[0]
mount_point = mount[1]
if mount_point == target:
altbootbank_blkdev = '%s6' % blkdev[0:-1]
altbootbank_blkdev = "%s6" % blkdev[0:-1]
break

if altbootbank_blkdev is None:
raise Exception('ERROR: Unable to find /altbootbank block device!')
raise Exception("ERROR: Unable to find /altbootbank block device!")

altbootbank_mount_point = tempfile.mkdtemp(prefix='curtin-hooks-')
check_call(['mount', altbootbank_blkdev, altbootbank_mount_point])
altbootbank_mount_point = tempfile.mkdtemp(prefix="curtin-hooks-")
check_call(["mount", altbootbank_blkdev, altbootbank_mount_point])

curtin_cfg_path = os.path.join(
altbootbank_mount_point, 'maas', 'curtin.cfg')
with open(curtin_cfg_path, 'w') as f:
altbootbank_mount_point, "maas", "curtin.cfg"
)
with open(curtin_cfg_path, "w") as f:
f.write(yaml.dump(config))

check_call(['umount', altbootbank_blkdev])
check_call(["umount", altbootbank_blkdev])


def cleanup():
Expand All @@ -76,7 +77,7 @@ def main():
state = load_command_environment()
config = load_command_config(None, state)

write_config(state['target'], config)
write_config(state["target"], config)
cleanup()


Expand Down
58 changes: 31 additions & 27 deletions vmware-esxi/maas/maas-md-get
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,32 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from argparse import ArgumentParser
from email.utils import parsedate
import http.client
import socket
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from argparse import ArgumentParser
from email.utils import parsedate

import oauthlib.oauth1 as oauth
import yaml


def authenticate_headers(url, headers, creds, clockskew=0):
"""Update and sign a dict of request headers."""
if creds.get('consumer_key') is not None:
if creds.get("consumer_key") is not None:
timestamp = int(time.time()) + clockskew
client = oauth.Client(
client_key=creds['consumer_key'],
client_secret=creds.get('consumer_secret', ''),
resource_owner_key=creds['token_key'],
resource_owner_secret=creds['token_secret'],
client_key=creds["consumer_key"],
client_secret=creds.get("consumer_secret", ""),
resource_owner_key=creds["token_key"],
resource_owner_secret=creds["token_secret"],
signature_method=oauth.SIGNATURE_PLAINTEXT,
timestamp=str(timestamp))
timestamp=str(timestamp),
)
_, signed_headers, _ = client.sign(url)
headers.update(signed_headers)

Expand All @@ -67,20 +68,17 @@ def geturl(url, creds, headers=None, data=None):
return ret.read().decode()
except urllib.error.HTTPError as exc:
error = exc
if 'date' not in exc.headers:
if "date" not in exc.headers:
print("date field not in %d headers" % exc.code)
pass
elif exc.code in (http.client.UNAUTHORIZED, http.client.FORBIDDEN):
date = exc.headers['date']
try:
ret_time = time.mktime(parsedate(date))
clockskew = int(ret_time - time.time())
print("updated clock skew to %d" % clockskew)
except:
print("failed to convert date '%s'" % date)
date = exc.headers["date"]
ret_time = time.mktime(parsedate(date))
clockskew = int(ret_time - time.time())
print("updated clock skew to %d" % clockskew)
elif exc.code == http.client.NOT_FOUND:
# Nothing to download.
return ''
return ""

except Exception as exc:
error = exc
Expand All @@ -93,21 +91,27 @@ def geturl(url, creds, headers=None, data=None):

def main():
parser = ArgumentParser(
description='Get data from the MAAS metadata server')
description="Get data from the MAAS metadata server"
)
parser.add_argument(
'-c', '--config', help='Path to the MAAS metadata credentials file',
required=True)
parser.add_argument('entry', help='The metadata path to get')
"-c",
"--config",
help="Path to the MAAS metadata credentials file",
required=True,
)
parser.add_argument("entry", help="The metadata path to get")

args = parser.parse_args()

with open(args.config, 'r') as f:
with open(args.config, "r") as f:
cfg = yaml.safe_load(f)

if 'reporting' in cfg:
cfg = cfg['reporting']['maas']
url = '%s%s' % (
cfg['endpoint'][:cfg['endpoint'].find('status/')], args.entry)
if "reporting" in cfg:
cfg = cfg["reporting"]["maas"]
url = "%s%s" % (
cfg["endpoint"][: cfg["endpoint"].find("status/")],
args.entry,
)

try:
print(geturl(url, creds=cfg))
Expand All @@ -122,5 +126,5 @@ def main():
sys.exit(1)


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit c927105

Please sign in to comment.