Raise exception on KeyboardInterrupt

Using subprocess with tenacity can lead to issues when a keyboard
interrupt occurs because the logic we have will actually trigger a
retry. This can lead to the inability to stop some of the commands with
a ctrl+c.

Change-Id: I294910384fc9dd9ca8c7114d08842868909d9e9f
Closes-Bug: #1746724
(cherry picked from commit 9003b7ac5b)
This commit is contained in:
Alex Schultz 2020-01-27 14:52:11 -07:00
parent 7d42075848
commit 3e1c52853b
1 changed files with 65 additions and 49 deletions

View File

@ -1062,6 +1062,7 @@ class SkopeoImageUploader(BaseImageUploader):
cmd.append(target)
LOG.info('Running %s' % ' '.join(cmd))
env = os.environ.copy()
try:
process = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
universal_newlines=True)
@ -1070,6 +1071,8 @@ class SkopeoImageUploader(BaseImageUploader):
if process.returncode != 0:
raise ImageUploaderException('Error copying image:\n%s\n%s' %
(' '.join(cmd), err))
except KeyboardInterrupt:
raise Exception('Action interrupted with ctrl+c')
return out
def _delete(self, image_url, session=None):
@ -1083,6 +1086,7 @@ class SkopeoImageUploader(BaseImageUploader):
cmd.append(image)
LOG.info('Running %s' % ' '.join(cmd))
env = os.environ.copy()
try:
process = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
universal_newlines=True)
@ -1091,6 +1095,8 @@ class SkopeoImageUploader(BaseImageUploader):
if process.returncode != 0:
raise ImageUploaderException('Error deleting image:\n%s\n%s' %
(' '.join(cmd), err))
except KeyboardInterrupt:
raise Exception('Action interrupted with ctrl+c')
return out
def cleanup(self, local_images):
@ -1784,6 +1790,7 @@ class PythonImageUploader(BaseImageUploader):
cmd.append(pull_source)
LOG.info('Pulling %s' % pull_source)
LOG.info('Running %s' % ' '.join(cmd))
try:
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
@ -1803,6 +1810,8 @@ class PythonImageUploader(BaseImageUploader):
)
LOG.error(error_msg)
raise ImageUploaderException(error_msg)
except KeyboardInterrupt:
raise Exception('Action interrupted with ctrl+c')
return out
@classmethod
@ -1863,6 +1872,7 @@ class PythonImageUploader(BaseImageUploader):
'--compress'
]
LOG.debug(' '.join(cmd))
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
chunk_size = 2 ** 20
@ -1876,6 +1886,8 @@ class PythonImageUploader(BaseImageUploader):
p.wait()
if p.returncode != 0:
raise ImageUploaderException('Extracting layer failed')
except KeyboardInterrupt:
raise Exception('Action interrupted with ctrl+c')
@classmethod
@tenacity.retry( # Retry up to 5 times with jittered exponential backoff
@ -2145,13 +2157,17 @@ class PythonImageUploader(BaseImageUploader):
cmd = ['buildah', 'rmi', image_url.path]
LOG.info('Running %s' % ' '.join(cmd))
env = os.environ.copy()
try:
process = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE,
universal_newlines=True)
out, err = process.communicate()
LOG.info(out)
if process.returncode != 0:
LOG.warning('Error deleting image:\n%s\n%s' % (' '.join(cmd), err))
LOG.warning('Error deleting image:\n%s\n%s' %
(' '.join(cmd), err))
except KeyboardInterrupt:
raise Exception('Action interrupted with ctrl+c')
return out
def cleanup(self, local_images):