Skip to content

Commit

Permalink
Merge pull request #106 from SUSE-Enceladus/imdsVer
Browse files Browse the repository at this point in the history
Imds ver
  • Loading branch information
rjschwei committed Feb 23, 2024
2 parents c22c87a + 61a67ea commit 3a92af6
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
15 changes: 13 additions & 2 deletions ec2uploadimg
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ def parse_args(args):
dest='grub2',
help='The image uses the GRUB2 bootloader (Optional)'
)
help_msg = 'Set the protocol version to be used when instances are '
help_msg += 'launched from the image, supported values 2.0/v2.0. '
help_msg += '(Optional)'
parser.add_argument(
'--imds-support',
dest='imdsVersion',
help=help_msg,
choices=['', '2.0', 'v2.0'],
metavar='IMDS_VERSION'
)
parser.add_argument(
'-i', '--instance-id',
dest='runningID',
Expand Down Expand Up @@ -244,7 +254,7 @@ def parse_args(args):
metavar='SSH_TIME_OUT',
type=int
)
help_msg = 'The image supports NitroTPM, supported value 2.0/v2.0'
help_msg = 'The image supports NitroTPM, supported values 2.0/v2.0'
help_msg += ' (Optional)'
parser.add_argument(
'--tpm-support',
Expand Down Expand Up @@ -897,7 +907,8 @@ def get_uploader(
wait_count=args.waitCount,
log_callback=logger,
boot_mode=args.bootMode,
tpm_support=args.tpm
tpm_support=args.tpm,
imds_support=args.imdsVersion
)
return uploader
except EC2UploadImgException as e:
Expand Down
14 changes: 13 additions & 1 deletion lib/ec2imgutils/ec2uploadimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def __init__(self,
log_level=logging.INFO,
log_callback=None,
boot_mode=None,
tpm_support=None
tpm_support=None,
imds_support=None
):
EC2ImgUtils.__init__(
self,
Expand All @@ -80,6 +81,7 @@ def __init__(self,
self.image_description = image_description
self.image_name = image_name
self.image_virt_type = image_virt_type
self.imds_support = imds_support
self.inst_user_name = inst_user_name
self.launch_ami_id = launch_ami
self.launch_ins_type = launch_inst_type
Expand Down Expand Up @@ -121,6 +123,11 @@ def __init__(self,
raise EC2UploadImgException(
'tpm_support must be one of %s' % str(tpm_versions)
)
imds_versions = ['2.0', 'v2.0']
if imds_support and imds_support not in imds_versions:
raise EC2UploadImgException(
'imds_support must be one of %s' % str(imds_versions)
)

def abort(self):
"""
Expand Down Expand Up @@ -893,6 +900,11 @@ def _register_image(self, snapshot):
if not tpm_version.startswith('v'):
tpm_version = 'v%s' % tpm_version
register_args['TpmSupport'] = tpm_version
if self.imds_support:
imds_version = self.imds_support
if not imds_version.startswith('v'):
imds_version = 'v%s' % imds_version
register_args['ImdsSupport'] = imds_version

ami = self._connect().register_image(**register_args)

Expand Down
7 changes: 7 additions & 0 deletions man/man1/ec2uploadimg.1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ Setting this switch will select the
value from the configuration file or will use the value given with
.I --boot-kernel
as the aki ID when a para virtual image is being registered.
.IP "--imds-support"
Optionally specify the version for accessing the Instance MetaData Service
(IMDS). The default is to access the IMDS using the version 1.0 implementation.
Accepted values are
.I 2.0
alternatively prefixed with a
.I "v".
.IP "-m --machine ARCH"
Specifies the architecture for the VM to be created. Supported values
are
Expand Down
28 changes: 28 additions & 0 deletions python3-ec2imgutils.spec
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ License: GPL-3.0+
Group: System/Management
Url: https://github.com/SUSE-Enceladus/ec2imgutils
Source0: %{upstream_name}-%{version}.tar.bz2
%if 0%{?sle_version} >= 150400
Requires: python311
Requires: python311-boto3 >= 1.29.84
Requires: python311-dateutil
Requires: python311-paramiko >= 2.2.0
BuildRequires: python311-boto3 >= 1.29.84
BuildRequires: python311-dateutil
BuildRequires: python311-pip
BuildRequires: python311-setuptools
BuildRequires: python311-wheel
%else
Requires: python3
Requires: python3-boto3 >= 1.29.84
Requires: python3-dateutil
Requires: python3-paramiko >= 2.2.0
BuildRequires: python3-boto3 >= 1.29.84
BuildRequires: python3-dateutil
BuildRequires: python3-setuptools
%endif
BuildRequires: python-rpm-macros
BuildRoot: %{_tmppath}/%{name}-%{version}-build
BuildArch: noarch
Expand Down Expand Up @@ -67,12 +79,23 @@ A collection of image manipulation utilities for AWS EC2. These include:

%prep
%setup -q -n %{upstream_name}-%{version}
%if 0%{?sle_version} >= 150400
find . -type f -name "ec2*" | xargs grep -l '/usr/bin/' | xargs sed -i 's/python3/python3.11/'
%endif

%build
%if 0%{?sle_version} >= 150400
%python311_pyproject_wheel
%else
python3 setup.py build
%endif

%install
%if 0%{?sle_version} >= 150400
%python311_pyproject_install
%else
python3 setup.py install --prefix=%{_prefix} --root=%{buildroot}
%endif
install -d -m 755 %{buildroot}/%{_mandir}/man1
install -m 644 man/man1/* %{buildroot}/%{_mandir}/man1
gzip %{buildroot}/%{_mandir}/man1/*
Expand All @@ -82,8 +105,13 @@ gzip %{buildroot}/%{_mandir}/man1/*
%doc README.md
%license LICENSE
%{_mandir}/man*/*
%if 0%{?sle_version} >= 150400
%dir %{python311_sitelib}/ec2imgutils
%{python311_sitelib}/*
%else
%dir %{python3_sitelib}/ec2imgutils
%{python3_sitelib}/*
%endif
%{_bindir}/*

%changelog
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 10.0.1
current_version = 10.0.2
commit = True
tag = True

Expand Down
10 changes: 8 additions & 2 deletions tests/test_ec2uploadimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"--file",
"/path/to/configuration/file",
"--grub2",
"--imds-support",
"2.0",
"--instance-id",
"testInstanceId",
"--machine",
Expand Down Expand Up @@ -149,6 +151,7 @@ def test_args(cli_args):
assert parsed_args.waitCount == 8
assert parsed_args.useSnap is True
assert parsed_args.source == "testSource"
assert parsed_args.imdsVersion == "2.0"


# --------------------------------------------------------------------
Expand All @@ -174,6 +177,8 @@ def test_args(cli_args):
"--file",
"/path/to/configuration/file",
"--grub2",
"--imds-support",
"v2.0",
"--machine",
"x86_64",
"--name",
Expand All @@ -196,7 +201,7 @@ def test_args(cli_args):
"--ssh-timeout",
"257",
"--tpm-support",
"2.0",
"v2.0",
"--type",
"testType",
"--user",
Expand Down Expand Up @@ -244,7 +249,7 @@ def test_args_check(cli_args):
assert parsed_args.snapOnly is False
assert parsed_args.sriov is True
assert parsed_args.sshTimeout == 257
assert parsed_args.tpm == "2.0"
assert parsed_args.tpm == "v2.0"
assert parsed_args.instType == "testType"
assert parsed_args.sshUser == "testUser"
assert parsed_args.usePrivateIP is True
Expand All @@ -253,6 +258,7 @@ def test_args_check(cli_args):
assert parsed_args.vpcSubnetId == "testVpcSubnetId"
assert parsed_args.waitCount == 8
assert parsed_args.source == "testSource"
assert parsed_args.imdsVersion == "v2.0"


# --------------------------------------------------------------------
Expand Down
39 changes: 37 additions & 2 deletions tests/test_libec2uploadimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
# <http://www.gnu.org/licenses/>.
#

import inspect
import logging
import os
import pytest
import sys


from unittest.mock import patch, MagicMock, call

import ec2imgutils.ec2uploadimg as ec2upimg
# Load the module from the source tree not the one on the system
test_path = os.path.abspath(
os.path.dirname(inspect.getfile(inspect.currentframe())))
code_path = os.path.abspath('%s/../lib' % test_path)
sys.path.insert(0, code_path)

from ec2imgutils.ec2imgutilsExceptions import (
import ec2imgutils.ec2uploadimg as ec2upimg # noqa: E402
from ec2imgutils.ec2imgutilsExceptions import ( # noqa: E402
EC2UploadImgException
)

Expand Down Expand Up @@ -2099,3 +2108,29 @@ def test_create_image_use_root_swap(
show_progress_mock.assert_has_calls([call(), call()])
detach_volume_mock.assert_has_calls([call(None)])
attach_volume_mock.assert_has_calls([call('targetRootVol', None)])


def test_invalid_tpm_value():
with pytest.raises(EC2UploadImgException) as e:
# Instance creation
ec2upimg.EC2ImageUploader(
access_key='',
wait_count=1,
log_callback=logger,
tpm_support='1'
)
msg = "tpm_support must be one of ['2.0', 'v2.0']"
assert msg in str(e)


def test_invalid_imds_value():
with pytest.raises(EC2UploadImgException) as e:
# Instance creation
ec2upimg.EC2ImageUploader(
access_key='',
wait_count=1,
log_callback=logger,
imds_support='1'
)
msg = "imds_support must be one of ['2.0', 'v2.0']"
assert msg in str(e)

0 comments on commit 3a92af6

Please sign in to comment.