Merge ~knobby/layer-snap:mwilson/is-installed into ~stub/layer-snap:master

Proposed by Mike Wilson
Status: Superseded
Proposed branch: ~knobby/layer-snap:mwilson/is-installed
Merge into: ~stub/layer-snap:master
Diff against target: 117 lines (+22/-10)
1 file modified
lib/charms/layer/snap.py (+22/-10)
Reviewer Review Type Date Requested Status
Stuart Bishop Needs Fixing
Review via email: mp+355414@code.launchpad.net

This proposal has been superseded by a proposal from 2018-09-21.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

The idea is fine. I need the get_installed_state_name and get_disabled_state_name function names changed to match recommended nomenclature before I can land it (get_installed_flag and get_disabled_flag rather than _state_name)

My use cases just hard code the documented flag names, but I can see that the new methods will be useful when the set of snaps is configurable.

review: Needs Fixing
Revision history for this message
Stuart Bishop (stub) :
3ec404e... by Mike Wilson

Making requested changes to function names.

Unmerged commits

3ec404e... by Mike Wilson

Making requested changes to function names.

0771924... by Mike Wilson

Adding is_installed function for snap layer

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/charms/layer/snap.py b/lib/charms/layer/snap.py
index 54d4cf8..acd6947 100644
--- a/lib/charms/layer/snap.py
+++ b/lib/charms/layer/snap.py
@@ -25,6 +25,14 @@ from time import sleep
25from datetime import datetime, timedelta25from datetime import datetime, timedelta
2626
2727
28def get_installed_flag(snapname):
29 return 'snap.installed.{}'.format(snapname)
30
31
32def get_disabled_flag(snapname):
33 return 'snap.disabled.{}'.format(snapname)
34
35
28def install(snapname, **kw):36def install(snapname, **kw):
29 '''Install a snap.37 '''Install a snap.
3038
@@ -36,7 +44,7 @@ def install(snapname, **kw):
36 If the snap.installed.{snapname} state is already set then the refresh()44 If the snap.installed.{snapname} state is already set then the refresh()
37 function is called.45 function is called.
38 '''46 '''
39 installed_state = 'snap.installed.{}'.format(snapname)47 installed_state = get_installed_flag(snapname)
40 if reactive.is_state(installed_state):48 if reactive.is_state(installed_state):
41 refresh(snapname, **kw)49 refresh(snapname, **kw)
42 else:50 else:
@@ -52,11 +60,15 @@ def install(snapname, **kw):
5260
53 # Installing any snap will first ensure that 'core' is installed. Set an61 # Installing any snap will first ensure that 'core' is installed. Set an
54 # appropriate flag for consumers that want to get/set core options.62 # appropriate flag for consumers that want to get/set core options.
55 core_installed = 'snap.installed.core'63 core_installed = get_installed_flag('core')
56 if not reactive.is_state(core_installed):64 if not reactive.is_state(core_installed):
57 reactive.set_state(core_installed)65 reactive.set_state(core_installed)
5866
5967
68def is_installed(snapname):
69 return reactive.is_state(get_installed_flag(snapname))
70
71
60def refresh(snapname, **kw):72def refresh(snapname, **kw):
61 '''Update a snap.73 '''Update a snap.
6274
@@ -86,7 +98,7 @@ def remove(snapname):
86 hookenv.log('Removing snap {}'.format(snapname))98 hookenv.log('Removing snap {}'.format(snapname))
87 subprocess.check_call(['snap', 'remove', snapname],99 subprocess.check_call(['snap', 'remove', snapname],
88 universal_newlines=True)100 universal_newlines=True)
89 reactive.remove_state('snap.installed.{}'.format(snapname))101 reactive.remove_state(get_installed_flag(snapname))
90102
91103
92def connect(plug, slot):104def connect(plug, slot):
@@ -121,7 +133,7 @@ def disable(snapname):
121 exist133 exist
122 '''134 '''
123 hookenv.log('Disabling {} snap'.format(snapname))135 hookenv.log('Disabling {} snap'.format(snapname))
124 if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):136 if not reactive.is_flag_set(get_installed_flag(snapname)):
125 hookenv.log(137 hookenv.log(
126 'Cannot disable {} snap because it is not installed'.format(138 'Cannot disable {} snap because it is not installed'.format(
127 snapname), hookenv.WARNING)139 snapname), hookenv.WARNING)
@@ -129,7 +141,7 @@ def disable(snapname):
129141
130 subprocess.check_call(['snap', 'disable', snapname],142 subprocess.check_call(['snap', 'disable', snapname],
131 universal_newlines=True)143 universal_newlines=True)
132 reactive.set_flag('snap.disabled.{}'.format(snapname))144 reactive.set_flag(get_disabled_flag(snapname))
133145
134146
135def enable(snapname):147def enable(snapname):
@@ -141,7 +153,7 @@ def enable(snapname):
141 exist153 exist
142 '''154 '''
143 hookenv.log('Enabling {} snap'.format(snapname))155 hookenv.log('Enabling {} snap'.format(snapname))
144 if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):156 if not reactive.is_flag_set(get_installed_flag(snapname)):
145 hookenv.log(157 hookenv.log(
146 'Cannot enable {} snap because it is not installed'.format(158 'Cannot enable {} snap because it is not installed'.format(
147 snapname), hookenv.WARNING)159 snapname), hookenv.WARNING)
@@ -149,7 +161,7 @@ def enable(snapname):
149161
150 subprocess.check_call(['snap', 'enable', snapname],162 subprocess.check_call(['snap', 'enable', snapname],
151 universal_newlines=True)163 universal_newlines=True)
152 reactive.clear_flag('snap.disabled.{}'.format(snapname))164 reactive.clear_flag(get_disabled_flag(snapname))
153165
154166
155def restart(snapname):167def restart(snapname):
@@ -159,7 +171,7 @@ def restart(snapname):
159 exist171 exist
160 '''172 '''
161 hookenv.log('Restarting {} snap'.format(snapname))173 hookenv.log('Restarting {} snap'.format(snapname))
162 if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):174 if not reactive.is_flag_set(get_installed_flag(snapname)):
163 hookenv.log(175 hookenv.log(
164 'Cannot restart {} snap because it is not installed'.format(176 'Cannot restart {} snap because it is not installed'.format(
165 snapname), hookenv.WARNING)177 snapname), hookenv.WARNING)
@@ -175,7 +187,7 @@ def set(snapname, key, value):
175 This method will fail if snapname is not an installed snap187 This method will fail if snapname is not an installed snap
176 '''188 '''
177 hookenv.log('Set config {}={} for snap {}'.format(key, value, snapname))189 hookenv.log('Set config {}={} for snap {}'.format(key, value, snapname))
178 if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):190 if not reactive.is_flag_set(get_installed_flag(snapname)):
179 hookenv.log(191 hookenv.log(
180 'Cannot set {} snap config because it is not installed'.format(192 'Cannot set {} snap config because it is not installed'.format(
181 snapname), hookenv.WARNING)193 snapname), hookenv.WARNING)
@@ -227,7 +239,7 @@ def get(snapname, key):
227 This method will fail if snapname is not an installed snap239 This method will fail if snapname is not an installed snap
228 '''240 '''
229 hookenv.log('Get config {} for snap {}'.format(key, snapname))241 hookenv.log('Get config {} for snap {}'.format(key, snapname))
230 if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):242 if not reactive.is_flag_set(get_installed_flag(snapname)):
231 hookenv.log(243 hookenv.log(
232 'Cannot get {} snap config because it is not installed'.format(244 'Cannot get {} snap config because it is not installed'.format(
233 snapname), hookenv.WARNING)245 snapname), hookenv.WARNING)

Subscribers

People subscribed via source and target branches

to all changes: