Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
tox:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- name: Install dependencies
run: |
Expand All @@ -15,7 +15,7 @@ jobs:
- name: Run tox
run: tox
test:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- name: Install dependencies
run: |
Expand All @@ -27,10 +27,9 @@ jobs:
# SKIP growpart-lvm test that does not work on github c-i
run: sudo SKIP=growpart-lvm PATH=$PWD/bin:$PATH ./test/run-all
- name: Run tests against BusyBox-based minimal environment
# SKIP growpart-lvm test as LVM tools are not available in BusyBox
run: |
mkdir /tmp/busybox-bin
busybox --install -s /tmp/busybox-bin
ln -s $(command -v flock mkfs.ext4 qemu-img qemu-nbd resize2fs sfdisk sgdisk) /tmp/busybox-bin
# Skip growpart: needs BusyBox >= 1.31, not available in Ubuntu yet.
# Skip mic: needs BusyBox >= 1.31, not available in Ubuntu yet.
sudo SKIP=mic,growpart,growpart-lvm PATH=$PWD/bin:/tmp/busybox-bin ./test/run-all
sudo SKIP=growpart-lvm PATH=$PWD/bin:/tmp/busybox-bin ./test/run-all
23 changes: 11 additions & 12 deletions bin/ec2metadata
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ except ImportError:

instdata_host = "169.254.169.254"
instdata_ver = "2009-04-04"
instdata_url = "http://%s/%s" % (instdata_host, instdata_ver)
instdata_url = f"http://{instdata_host}/{instdata_ver}"

TOKEN_TTL_SECONDS = 21600
TOKEN_HEADER = "X-aws-ec2-metadata-token"
TOKEN_HEADER_TTL = "X-aws-ec2-metadata-token-ttl-seconds"

session_token_url = "http://%s/%s/%s" % (instdata_host, 'latest', 'api/token')
session_token_url = f"http://{instdata_host}/latest/api/token"

__doc__ = """
__doc__ = f"""
Query and display EC2 metadata.

If no options are provided, all options will be displayed
Expand Down Expand Up @@ -87,9 +87,9 @@ Options:
--public-keys display the openssh public keys
--user-data display the user data (not actually metadata)

-u | --url URL use URL (default: %s)
-u | --url URL use URL (default: {instdata_url})

""" % instdata_url
"""


METAOPTS = ['ami-id', 'ami-launch-index', 'ami-manifest-path',
Expand Down Expand Up @@ -127,8 +127,7 @@ class EC2Metadata: # pylint: disable=R0903
if s.port is None:
port = 80
if not self._test_connectivity(addr, port):
raise Error("could not establish connection to: %s:%s" %
(addr, port))
raise Error(f"could not establish connection to: {addr}:{port}")
self._imdsv2_ensure_token()

@staticmethod
Expand All @@ -154,7 +153,7 @@ class EC2Metadata: # pylint: disable=R0903
self.session_token = resp.read()

def _get(self, uri, decode=True):
url = "%s/%s" % (self.burl, uri)
url = f"{self.burl}/{uri}"
try:
resp = urllib_request.urlopen(
urllib_request.Request(
Expand Down Expand Up @@ -198,7 +197,7 @@ class EC2Metadata: # pylint: disable=R0903

public_keys = []
for keyid in keyids:
uri = 'meta-data/public-keys/%d/openssh-key' % int(keyid)
uri = f'meta-data/public-keys/{int(keyid)}/openssh-key'
public_keys.append(self._get(uri).rstrip())

return public_keys
Expand Down Expand Up @@ -226,7 +225,7 @@ def display(metaopts, burl, prefix=False):
value = "unavailable"

if prefix:
print("%s: %s" % (metaopt, value))
print(f"{metaopt}: {value}")
elif metaopt == "user-data":
# We want to avoid binary blob corruption while printing as string
print_binary(value)
Expand All @@ -239,8 +238,8 @@ def usage(s=None):

msg = ""
if s:
msg = "Error: %s\n" % s
msg += "Syntax: %s [options]\n" % sys.argv[0]
msg = f"Error: {s}\n"
msg += f"Syntax: {sys.argv[0]} [options]\n"
msg += __doc__
sys.stderr.write(msg + "\n")
sys.exit(1)
Expand Down
28 changes: 14 additions & 14 deletions bin/write-mime-multipart
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ def main():

maintype, subtype = mtype.split('/', 1)
if maintype == 'text':
fp = open(path)
# Note: we should handle calculating the charset
msg = MIMEText(fp.read(), _subtype=subtype)
fp.close()
with open(path, encoding='utf-8') as fp:
# Note: we should handle calculating the charset
msg = MIMEText(fp.read(), _subtype=subtype)
else:
fp = open(path, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
with open(path, 'rb') as fp:
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
# Encode the payload using Base64
encoders.encode_base64(msg)

Expand All @@ -107,18 +105,20 @@ def main():
ofile = sys.stdout.buffer
else:
ofile = sys.stdout
_write_output(ofile, outer, options)
else:
ofile = open(options.output, "wb")
with open(options.output, "wb") as ofile:
_write_output(ofile, outer, options)


def _write_output(ofile, outer, options):
"""Write the MIME message to the output file."""
if options.compress:
gfile = gzip.GzipFile(fileobj=ofile, filename=options.output)
gfile.write(outer.as_string().encode())
gfile.close()
with gzip.GzipFile(fileobj=ofile, filename=options.output) as gfile:
gfile.write(outer.as_string().encode())
else:
ofile.write(outer.as_string().encode())

ofile.close()


if __name__ == '__main__':
main()
Expand Down
2 changes: 1 addition & 1 deletion test/test-growpart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ if [ -z "$out" ]; then
exit 1
fi

echo === growpart stderr ===
echo "=== growpart stderr ==="
cat "$errfile"

echo "==== after ===="
Expand Down
28 changes: 5 additions & 23 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@ envlist =
shellcheck

[common]
pyfiles =
bin/ec2metadata
bin/write-mime-multipart
shfiles =
bin/cloud-localds
bin/growpart
bin/mount-image-callback
bin/resize-part-image
bin/vcs-run
test/test-growpart
test/test-growpart-fsimage
test/test-growpart-fsimage-middle
test/test-growpart-overprovision
test/test-growpart-lvm
test/test-growpart-start-matches-size
test/test-mic
test/run-all
tools/build-deb
tools/make-short-partition
tools/make-tarball
pyfiles = bin/ec2metadata bin/write-mime-multipart
shfiles = bin/cloud-localds bin/growpart bin/mount-image-callback bin/resize-part-image bin/vcs-run test/test-growpart test/test-growpart-fsimage test/test-growpart-fsimage-middle test/test-growpart-overprovision test/test-growpart-lvm test/test-growpart-start-matches-size test/test-mic test/run-all tools/build-deb tools/make-short-partition tools/make-tarball

[testenv:flake8]
deps = flake8==3.8.3
deps = flake8==7.3.0
commands = python3 -m flake8 {[common]pyfiles}

[testenv:pylint]
deps = pylint==2.5.3
deps = pylint==4.0.4
commands = python3 -m pylint {[common]pyfiles}

[testenv:shellcheck]
# shellcheck from Xenial is too old and doesn't support the --severity
# argument; let's pull a newer version using shellcheck-py. This also
# allows to pin the shellcheck version we check against.
deps = shellcheck-py==0.7.1.1
deps = shellcheck-py==0.11.0.1
commands = shellcheck --severity=error {[common]shfiles}

[testenv:tip-flake8]
Expand Down