1
# Copyright 2014-2017 Canonical Limited.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
15
Charm helpers snap for classic charms.
17
If writing reactive charms, use the snap layer:
18
https://lists.ubuntu.com/archives/snapcraft/2016-September/001114.html
22
from time import sleep
23
from charmhelpers.core.hookenv import log
25
__author__ = 'Joseph Borg <joseph.borg@canonical.com>'
27
# The return code for "couldn't acquire lock" in Snap
28
# (hopefully this will be improved).
30
SNAP_NO_LOCK_RETRY_DELAY = 10 # Wait X seconds between Snap lock checks.
31
SNAP_NO_LOCK_RETRY_COUNT = 30 # Retry to acquire the lock X times.
40
class CouldNotAcquireLockException(Exception):
44
def _snap_exec(commands):
46
Execute snap commands.
48
:param commands: List commands
49
:return: Integer exit code
51
assert type(commands) == list
56
while return_code is None or return_code == SNAP_NO_LOCK:
58
return_code = subprocess.check_call(['snap'] + commands,
60
except subprocess.CalledProcessError as e:
62
if retry_count > SNAP_NO_LOCK_RETRY_COUNT:
63
raise CouldNotAcquireLockException(
64
'Could not aquire lock after {} attempts'
65
.format(SNAP_NO_LOCK_RETRY_COUNT))
66
return_code = e.returncode
67
log('Snap failed to acquire lock, trying again in {} seconds.'
68
.format(SNAP_NO_LOCK_RETRY_DELAY, level='WARN'))
69
sleep(SNAP_NO_LOCK_RETRY_DELAY)
74
def snap_install(packages, *flags):
76
Install a snap package.
78
:param packages: String or List String package name
79
:param flags: List String flags to pass to install command
80
:return: Integer return code from snap
82
if type(packages) is not list:
87
message = 'Installing snap(s) "%s"' % ', '.join(packages)
89
message += ' with option(s) "%s"' % ', '.join(flags)
91
log(message, level='INFO')
92
return _snap_exec(['install'] + flags + packages)
95
def snap_remove(packages, *flags):
97
Remove a snap package.
99
:param packages: String or List String package name
100
:param flags: List String flags to pass to remove command
101
:return: Integer return code from snap
103
if type(packages) is not list:
104
packages = [packages]
108
message = 'Removing snap(s) "%s"' % ', '.join(packages)
110
message += ' with options "%s"' % ', '.join(flags)
112
log(message, level='INFO')
113
return _snap_exec(['remove'] + flags + packages)
116
def snap_refresh(packages, *flags):
118
Refresh / Update snap package.
120
:param packages: String or List String package name
121
:param flags: List String flags to pass to refresh command
122
:return: Integer return code from snap
124
if type(packages) is not list:
125
packages = [packages]
129
message = 'Refreshing snap(s) "%s"' % ', '.join(packages)
131
message += ' with options "%s"' % ', '.join(flags)
133
log(message, level='INFO')
134
return _snap_exec(['refresh'] + flags + packages)