Merge lp:~simpoir/landscape-charm/bootstrap_with_key into lp:~landscape/landscape-charm/trunk

Proposed by Simon Poirier
Status: Merged
Approved by: Simon Poirier
Approved revision: 399
Merged at revision: 398
Proposed branch: lp:~simpoir/landscape-charm/bootstrap_with_key
Merge into: lp:~landscape/landscape-charm/trunk
Diff against target: 296 lines (+96/-42)
8 files modified
actions.yaml (+3/-0)
lib/apt.py (+1/-1)
lib/bootstrap.py (+21/-15)
lib/paths.py (+1/-0)
lib/tests/test_apt.py (+10/-4)
lib/tests/test_bootstrap.py (+46/-13)
lib/tests/test_install.py (+2/-1)
lib/tests/test_upgrade.py (+12/-8)
To merge this branch: bzr merge lp:~simpoir/landscape-charm/bootstrap_with_key
Reviewer Review Type Date Requested Status
Adam Collard (community) Approve
🤖 Landscape Builder test results Approve
Review via email: mp+368020@code.launchpad.net

Commit message

Use API to bootstrap to enable passing registration_key.

Description of the change

Use API to bootstrap to enable passing registration_key.

Testing instructions:

make bundles-local-charm
juju deploy bundles/build/landscape-scalable/bundle.yaml
juju run-action --wait landscape-server/0 bootstrap <email address hidden> admin-password=tedted admin-name=Ted registration-key=landscapeiscool

go to landscape and log with <email address hidden>/tedted and check the registration key is set on the account.

To post a comment you must log in.
Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: make ci-test
Result: Fail
Revno: 398
Branch: lp:~simpoir/landscape-charm/bootstrap_with_key
Jenkins: https://ci.lscape.net/job/latch-test-xenial/3953/

review: Needs Fixing (test results)
399. By Simon Poirier

fix test

Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: make ci-test
Result: Success
Revno: 399
Branch: lp:~simpoir/landscape-charm/bootstrap_with_key
Jenkins: https://ci.lscape.net/job/latch-test-xenial/3954/

review: Approve (test results)
Revision history for this message
Adam Collard (adam-collard) wrote :

LGTM, +1

review: Approve
Revision history for this message
David Coronel (davecore) wrote :

Tested with juju 2.6.2. LGTM, great feature, thanks!

juju bootstrap localhost
bzr branch lp:landscape-charm
cd landscape-charm/
bzr merge lp:~simpoir/landscape-charm/bootstrap_with_key
make bundles-local-charm
juju deploy bundles/build/landscape-scalable/bundle.yaml
juju run-action --wait landscape-server/0 bootstrap <email address hidden> admin-password=tedted admin-name=Ted registration-key=landscapeiscool

Logged in Landscape with ted email and saw the registration key already configured.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'actions.yaml'
2--- actions.yaml 2015-06-25 16:01:22 +0000
3+++ actions.yaml 2019-05-29 20:31:13 +0000
4@@ -25,5 +25,8 @@
5 admin-password:
6 type: string
7 description: Password for the administrator to add.
8+ registration-key:
9+ type: string
10+ description: Registration key to set on the account.
11 required: [admin-name, admin-email, admin-password]
12 additionalProperties: false
13
14=== modified file 'lib/apt.py'
15--- lib/apt.py 2019-01-17 14:23:49 +0000
16+++ lib/apt.py 2019-05-29 20:31:13 +0000
17@@ -12,7 +12,7 @@
18 from lib.paths import default_paths
19 from lib.utils import CommandRunner
20
21-LANDSCAPE_PACKAGES = ("landscape-server", "landscape-hashids")
22+LANDSCAPE_PACKAGES = ("landscape-server", "landscape-hashids", "landscape-api")
23 INSTALL_PACKAGES = LANDSCAPE_PACKAGES + ("python-minimal", "python-psutil")
24 PACKAGES_DEV = (
25 "dpkg-dev", "devscripts", "pbuilder", "aptitude", "build-essential")
26
27=== modified file 'lib/bootstrap.py'
28--- lib/bootstrap.py 2015-09-17 15:25:30 +0000
29+++ lib/bootstrap.py 2019-05-29 20:31:13 +0000
30@@ -1,11 +1,10 @@
31+import json
32 import subprocess
33
34 from charmhelpers.core import hookenv
35
36 from lib.action import Action
37-from lib.paths import SCHEMA_SCRIPT
38-
39-CREDENTIALS_MARKER = "API credentials:"
40+from lib.paths import API_SCRIPT
41
42
43 class BootstrapAction(Action):
44@@ -19,12 +18,22 @@
45 admin_name = self._hookenv.action_get("admin-name")
46 admin_email = self._hookenv.action_get("admin-email")
47 admin_password = self._hookenv.action_get("admin-password")
48-
49- cmd = (SCHEMA_SCRIPT, "--create-lds-account-only", "--admin-name",
50- admin_name, "--admin-email", admin_email,
51- "--admin-password", admin_password)
52-
53- output = self._subprocess.check_output(cmd)
54+ registration_key = self._hookenv.action_get("registration-key")
55+
56+ environment = {
57+ "LANDSCAPE_API_KEY": "anonymous",
58+ "LANDSCAPE_API_SECRET": "anonymous",
59+ "LANDSCAPE_API_URI": "http://localhost:9080/api/",
60+ }
61+ cmd = [
62+ API_SCRIPT, "call", "BootstrapLDS", "--json",
63+ "admin_name={}".format(admin_name),
64+ "admin_email={}".format(admin_email),
65+ "admin_password={}".format(admin_password)]
66+ if registration_key:
67+ cmd.append("registration_key={}".format(registration_key))
68+
69+ output = self._subprocess.check_output(cmd, env=environment)
70 key, secret = self._parse_schema_output(output)
71 result = {"api-credentials": {"key": key, "secret": secret}}
72
73@@ -32,10 +41,7 @@
74
75 def _parse_schema_output(self, output):
76 """Extract API credentials from the schema bootstrap output."""
77- key = None
78- secret = None
79- for line in output.split("\n"):
80- if line.startswith(CREDENTIALS_MARKER):
81- line = line[len(CREDENTIALS_MARKER) + 1:]
82- key, secret = line.split(" ")[2:4]
83+ credentials = json.loads(output)
84+ key = credentials.get("LANDSCAPE_API_KEY")
85+ secret = credentials.get("LANDSCAPE_API_SECRET")
86 return key, secret
87
88=== modified file 'lib/paths.py'
89--- lib/paths.py 2015-09-29 10:10:33 +0000
90+++ lib/paths.py 2019-05-29 20:31:13 +0000
91@@ -9,6 +9,7 @@
92 CONFIG_DIR = INSTALL_DIR + "/configs/standalone"
93 OFFLINE_DIR = INSTALL_DIR + "/canonical/landscape/offline"
94
95+API_SCRIPT = "/usr/bin/landscape-api"
96 SCHEMA_SCRIPT = "/usr/bin/landscape-schema"
97
98 LICENSE_FILE = "/etc/landscape/license.txt"
99
100=== modified file 'lib/tests/test_apt.py'
101--- lib/tests/test_apt.py 2019-01-17 14:23:49 +0000
102+++ lib/tests/test_apt.py 2019-05-29 20:31:13 +0000
103@@ -271,8 +271,8 @@
104 installed.
105 """
106 self.assertEqual(
107- ("landscape-server", "landscape-hashids", "python-minimal",
108- "python-psutil"),
109+ ("landscape-server", "landscape-hashids", "landscape-api",
110+ "python-minimal", "python-psutil"),
111 INSTALL_PACKAGES)
112
113 def test_install(self):
114@@ -356,7 +356,10 @@
115 """
116 self.apt.hold_packages()
117 self.assertEqual(
118- ["apt-mark", "hold", "landscape-server", "landscape-hashids"],
119+ [
120+ "apt-mark", "hold", "landscape-server", "landscape-hashids",
121+ "landscape-api",
122+ ],
123 self.subprocess.calls[0][0])
124
125 def test_unhold_packages(self):
126@@ -366,5 +369,8 @@
127 """
128 self.apt.unhold_packages()
129 self.assertEqual(
130- ["apt-mark", "unhold", "landscape-server", "landscape-hashids"],
131+ [
132+ "apt-mark", "unhold", "landscape-server", "landscape-hashids",
133+ "landscape-api",
134+ ],
135 self.subprocess.calls[0][0])
136
137=== modified file 'lib/tests/test_bootstrap.py'
138--- lib/tests/test_bootstrap.py 2015-09-17 15:25:30 +0000
139+++ lib/tests/test_bootstrap.py 2019-05-29 20:31:13 +0000
140@@ -1,13 +1,10 @@
141 from lib.tests.helpers import HookenvTest
142 from lib.tests.stubs import SubprocessStub
143 from lib.bootstrap import BootstrapAction
144-from lib.paths import SCHEMA_SCRIPT
145+from lib.paths import API_SCRIPT
146
147-SCHEMA_SCRIPT_STDOUT = """
148-Setting up database schemas (will timeout after 86400 seconds) ...
149-Setting up sample data...
150-Generating 72 snapshots from 1442480400 to 1442502186.
151-API credentials: free.ekanayaka@canonical.com standalone key-xyz secret-123
152+API_SCRIPT_STDOUT = """
153+{"LANDSCAPE_API_KEY": "key-xyz", "LANDSCAPE_API_SECRET": "secret-123"}
154 """
155
156
157@@ -17,14 +14,14 @@
158 super(BootstrapActionTest, self).setUp()
159 self.subprocess = SubprocessStub()
160 self.subprocess.add_fake_executable(
161- SCHEMA_SCRIPT, stdout=SCHEMA_SCRIPT_STDOUT)
162+ API_SCRIPT, stdout=API_SCRIPT_STDOUT)
163 self.subprocess.add_fake_executable("service")
164 self.action = BootstrapAction(
165 hookenv=self.hookenv, subprocess=self.subprocess)
166
167 def test_run(self):
168 """
169- The BootstrapAction calls the landscape-schema script to create an
170+ The BootstrapAction calls the landscape-api to create an
171 administrator account.
172 """
173 self.action()
174@@ -32,11 +29,47 @@
175 # the value for each 'key'.
176 [(command, kwargs)] = self.subprocess.calls
177 self.assertEqual(
178- (("/usr/bin/landscape-schema", "--create-lds-account-only",
179- "--admin-name", "admin-name-value",
180- "--admin-email", "admin-email-value",
181- "--admin-password", "admin-password-value"), {}),
182- (command, kwargs))
183+ ["/usr/bin/landscape-api", "call",
184+ "BootstrapLDS", "--json",
185+ "admin_name=admin-name-value",
186+ "admin_email=admin-email-value",
187+ "admin_password=admin-password-value",
188+ "registration_key=registration-key-value"],
189+ command)
190+ self.assertEqual(
191+ {'env': {'LANDSCAPE_API_KEY': 'anonymous',
192+ 'LANDSCAPE_API_SECRET': 'anonymous',
193+ 'LANDSCAPE_API_URI': 'http://localhost:9080/api/'}},
194+ kwargs)
195+ self.assertEqual(
196+ [{"api-credentials": {"secret": "secret-123", "key": "key-xyz"}}],
197+ self.hookenv.action_sets)
198+
199+ def test_run_without_registration_key(self):
200+ """
201+ The BootstrapAction calls the landscape-api to create an
202+ administrator account without setting the registration_key.
203+ """
204+ # patch the stub with fixed values
205+ self.hookenv.action_get = {
206+ "admin-name": "admin-name-value",
207+ "admin-email": "admin-email-value",
208+ "admin-password": "admin-password-value"}.get
209+
210+ self.action()
211+ [(command, kwargs)] = self.subprocess.calls
212+ self.assertEqual(
213+ ["/usr/bin/landscape-api", "call",
214+ "BootstrapLDS", "--json",
215+ "admin_name=admin-name-value",
216+ "admin_email=admin-email-value",
217+ "admin_password=admin-password-value"],
218+ command)
219+ self.assertEqual(
220+ {'env': {'LANDSCAPE_API_KEY': 'anonymous',
221+ 'LANDSCAPE_API_SECRET': 'anonymous',
222+ 'LANDSCAPE_API_URI': 'http://localhost:9080/api/'}},
223+ kwargs)
224 self.assertEqual(
225 [{"api-credentials": {"secret": "secret-123", "key": "key-xyz"}}],
226 self.hookenv.action_sets)
227
228=== modified file 'lib/tests/test_install.py'
229--- lib/tests/test_install.py 2019-01-17 14:23:49 +0000
230+++ lib/tests/test_install.py 2019-05-29 20:31:13 +0000
231@@ -83,5 +83,6 @@
232 self.hookenv.config()["source"] = "ppa:landscape/14.10"
233 self.hook()
234 expected_call = [
235- "apt-mark", "hold", "landscape-server", "landscape-hashids"]
236+ "apt-mark", "hold", "landscape-server", "landscape-hashids",
237+ "landscape-api"]
238 self.assertEqual(expected_call, self.subprocess.calls[0][0])
239
240=== modified file 'lib/tests/test_upgrade.py'
241--- lib/tests/test_upgrade.py 2019-01-17 14:23:49 +0000
242+++ lib/tests/test_upgrade.py 2019-05-29 20:31:13 +0000
243@@ -70,9 +70,11 @@
244 action()
245
246 unhold_call = [
247- "apt-mark", "unhold", "landscape-server", "landscape-hashids"]
248+ "apt-mark", "unhold", "landscape-server", "landscape-hashids",
249+ "landscape-api"]
250 hold_call = [
251- "apt-mark", "hold", "landscape-server", "landscape-hashids"]
252+ "apt-mark", "hold", "landscape-server", "landscape-hashids",
253+ "landscape-api"]
254 self.assertEqual(unhold_call, self.subprocess.calls[0][0])
255 self.assertEqual(hold_call, self.subprocess.calls[1][0])
256
257@@ -95,10 +97,10 @@
258 [('Running action UpgradeAction', None),
259 ('Adding repository: ppa:my-ppa', None),
260 ('running \'apt-mark unhold landscape-server '
261- 'landscape-hashids\'',
262+ 'landscape-hashids landscape-api\'',
263 hookenv.DEBUG),
264 ('running \'apt-mark hold landscape-server '
265- 'landscape-hashids\'',
266+ 'landscape-hashids landscape-api\'',
267 hookenv.DEBUG),
268 ])
269
270@@ -109,7 +111,8 @@
271 """
272 self.subprocess.add_fake_executable('apt-mark',
273 ['unhold', 'landscape-server',
274- 'landscape-hashids'],
275+ 'landscape-hashids',
276+ 'landscape-api'],
277 return_code=1)
278 self.hookenv.status_set("maintenance", "")
279 self.hookenv.config()["source"] = "ppa:my-ppa"
280@@ -122,12 +125,13 @@
281 [('Running action UpgradeAction', None),
282 ('Adding repository: ppa:my-ppa', None),
283 ('running \'apt-mark unhold landscape-server '
284- 'landscape-hashids\'',
285+ 'landscape-hashids landscape-api\'',
286 hookenv.DEBUG),
287 ('got return code 1 running \'apt-mark unhold '
288- 'landscape-server landscape-hashids\'',
289+ 'landscape-server landscape-hashids '
290+ 'landscape-api\'',
291 hookenv.ERROR),
292 ])
293 self.assertEqual(self.hookenv.action_fails,
294 ['command failed (see unit logs): apt-mark unhold '
295- 'landscape-server landscape-hashids'])
296+ 'landscape-server landscape-hashids landscape-api'])

Subscribers

People subscribed via source and target branches