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

Proposed by Mike Wilson
Status: Merged
Merged at revision: 52675b6f6e7bb83362b2eae91ca8c105635fcbc5
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 Approve
Review via email: mp+355505@code.launchpad.net

This proposal supersedes a proposal from 2018-09-20.

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

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) : Posted in a previous version of this proposal
Revision history for this message
Stuart Bishop (stub) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/charms/layer/snap.py b/lib/charms/layer/snap.py
2index 54d4cf8..acd6947 100644
3--- a/lib/charms/layer/snap.py
4+++ b/lib/charms/layer/snap.py
5@@ -25,6 +25,14 @@ from time import sleep
6 from datetime import datetime, timedelta
7
8
9+def get_installed_flag(snapname):
10+ return 'snap.installed.{}'.format(snapname)
11+
12+
13+def get_disabled_flag(snapname):
14+ return 'snap.disabled.{}'.format(snapname)
15+
16+
17 def install(snapname, **kw):
18 '''Install a snap.
19
20@@ -36,7 +44,7 @@ def install(snapname, **kw):
21 If the snap.installed.{snapname} state is already set then the refresh()
22 function is called.
23 '''
24- installed_state = 'snap.installed.{}'.format(snapname)
25+ installed_state = get_installed_flag(snapname)
26 if reactive.is_state(installed_state):
27 refresh(snapname, **kw)
28 else:
29@@ -52,11 +60,15 @@ def install(snapname, **kw):
30
31 # Installing any snap will first ensure that 'core' is installed. Set an
32 # appropriate flag for consumers that want to get/set core options.
33- core_installed = 'snap.installed.core'
34+ core_installed = get_installed_flag('core')
35 if not reactive.is_state(core_installed):
36 reactive.set_state(core_installed)
37
38
39+def is_installed(snapname):
40+ return reactive.is_state(get_installed_flag(snapname))
41+
42+
43 def refresh(snapname, **kw):
44 '''Update a snap.
45
46@@ -86,7 +98,7 @@ def remove(snapname):
47 hookenv.log('Removing snap {}'.format(snapname))
48 subprocess.check_call(['snap', 'remove', snapname],
49 universal_newlines=True)
50- reactive.remove_state('snap.installed.{}'.format(snapname))
51+ reactive.remove_state(get_installed_flag(snapname))
52
53
54 def connect(plug, slot):
55@@ -121,7 +133,7 @@ def disable(snapname):
56 exist
57 '''
58 hookenv.log('Disabling {} snap'.format(snapname))
59- if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):
60+ if not reactive.is_flag_set(get_installed_flag(snapname)):
61 hookenv.log(
62 'Cannot disable {} snap because it is not installed'.format(
63 snapname), hookenv.WARNING)
64@@ -129,7 +141,7 @@ def disable(snapname):
65
66 subprocess.check_call(['snap', 'disable', snapname],
67 universal_newlines=True)
68- reactive.set_flag('snap.disabled.{}'.format(snapname))
69+ reactive.set_flag(get_disabled_flag(snapname))
70
71
72 def enable(snapname):
73@@ -141,7 +153,7 @@ def enable(snapname):
74 exist
75 '''
76 hookenv.log('Enabling {} snap'.format(snapname))
77- if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):
78+ if not reactive.is_flag_set(get_installed_flag(snapname)):
79 hookenv.log(
80 'Cannot enable {} snap because it is not installed'.format(
81 snapname), hookenv.WARNING)
82@@ -149,7 +161,7 @@ def enable(snapname):
83
84 subprocess.check_call(['snap', 'enable', snapname],
85 universal_newlines=True)
86- reactive.clear_flag('snap.disabled.{}'.format(snapname))
87+ reactive.clear_flag(get_disabled_flag(snapname))
88
89
90 def restart(snapname):
91@@ -159,7 +171,7 @@ def restart(snapname):
92 exist
93 '''
94 hookenv.log('Restarting {} snap'.format(snapname))
95- if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):
96+ if not reactive.is_flag_set(get_installed_flag(snapname)):
97 hookenv.log(
98 'Cannot restart {} snap because it is not installed'.format(
99 snapname), hookenv.WARNING)
100@@ -175,7 +187,7 @@ def set(snapname, key, value):
101 This method will fail if snapname is not an installed snap
102 '''
103 hookenv.log('Set config {}={} for snap {}'.format(key, value, snapname))
104- if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):
105+ if not reactive.is_flag_set(get_installed_flag(snapname)):
106 hookenv.log(
107 'Cannot set {} snap config because it is not installed'.format(
108 snapname), hookenv.WARNING)
109@@ -227,7 +239,7 @@ def get(snapname, key):
110 This method will fail if snapname is not an installed snap
111 '''
112 hookenv.log('Get config {} for snap {}'.format(key, snapname))
113- if not reactive.is_flag_set('snap.installed.{}'.format(snapname)):
114+ if not reactive.is_flag_set(get_installed_flag(snapname)):
115 hookenv.log(
116 'Cannot get {} snap config because it is not installed'.format(
117 snapname), hookenv.WARNING)

Subscribers

People subscribed via source and target branches

to all changes: