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
21
from os import environ
22
from time import sleep
23
from charmhelpers.core.hookenv import log
25
__author__ = 'Joseph Borg <joseph.borg@canonical.com>'
27
SNAP_NO_LOCK = 1 # The return code for "couldn't acquire lock" in Snap (hopefully this will be improved).
28
SNAP_NO_LOCK_RETRY_DELAY = 10 # Wait X seconds between Snap lock checks.
29
SNAP_NO_LOCK_RETRY_COUNT = 30 # Retry to acquire the lock X times.
32
class CouldNotAcquireLockException(Exception):
36
def _snap_exec(commands):
38
Execute snap commands.
40
:param commands: List commands
41
:return: Integer exit code
43
assert type(commands) == list
48
while return_code is None or return_code == SNAP_NO_LOCK:
50
return_code = subprocess.check_call(['snap'] + commands, env=environ)
51
except subprocess.CalledProcessError as e:
53
if retry_count > SNAP_NO_LOCK_RETRY_COUNT:
54
raise CouldNotAcquireLockException('Could not aquire lock after %s attempts' % SNAP_NO_LOCK_RETRY_COUNT)
55
return_code = e.returncode
56
log('Snap failed to acquire lock, trying again in %s seconds.' % SNAP_NO_LOCK_RETRY_DELAY, level='WARN')
57
sleep(SNAP_NO_LOCK_RETRY_DELAY)
62
def snap_install(packages, *flags):
64
Install a snap package.
66
:param packages: String or List String package name
67
:param flags: List String flags to pass to install command
68
:return: Integer return code from snap
70
if type(packages) is not list:
75
message = 'Installing snap(s) "%s"' % ', '.join(packages)
77
message += ' with option(s) "%s"' % ', '.join(flags)
79
log(message, level='INFO')
80
return _snap_exec(['install'] + flags + packages)
83
def snap_remove(packages, *flags):
85
Remove a snap package.
87
:param packages: String or List String package name
88
:param flags: List String flags to pass to remove command
89
:return: Integer return code from snap
91
if type(packages) is not list:
96
message = 'Removing snap(s) "%s"' % ', '.join(packages)
98
message += ' with options "%s"' % ', '.join(flags)
100
log(message, level='INFO')
101
return _snap_exec(['remove'] + flags + packages)
104
def snap_refresh(packages, *flags):
106
Refresh / Update snap package.
108
:param packages: String or List String package name
109
:param flags: List String flags to pass to refresh command
110
:return: Integer return code from snap
112
if type(packages) is not list:
113
packages = [packages]
117
message = 'Refreshing snap(s) "%s"' % ', '.join(packages)
119
message += ' with options "%s"' % ', '.join(flags)
121
log(message, level='INFO')
122
return _snap_exec(['refresh'] + flags + packages)