Skip to content

Commit ad3e7eb

Browse files
authored
Merge pull request #153 from victormlg/demo-admin2
ENT-13007: Fixed admin demo user for cf-remote install --demo
2 parents 271605d + 2e2e6b7 commit ad3e7eb

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

cf_remote/commands.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ def install(
182182
assert (trust_keys is None) or hasattr(trust_keys, "__iter__")
183183
# These assertions are checked/ensured in main.py
184184

185+
if demo and not bootstrap:
186+
log.error(
187+
"Cannot start a demo on a non-bootstrapped host (CFEngine would be installed but not started)."
188+
"Please re-run cf-remote install with --bootstrap <hub_name>."
189+
)
190+
return 1
191+
185192
# If there are URLs in any of the package strings and remote_download is FALSE, download and replace with path:
186193
packages = (package, hub_package, client_package)
187194
if remote_download:

cf_remote/demo.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@
66
from cf_remote.utils import save_file
77
from cf_remote.ssh import scp, ssh_sudo, ssh_cmd, auto_connect
88

9+
SET_ADMIN_PASSWORD_QUERY = """UPDATE
10+
\"system\"
11+
SET
12+
\"value\" = 'true'
13+
WHERE
14+
\"key\" = 'is_setup_complete';
15+
INSERT
16+
INTO
17+
\"users\"
18+
(\"username\", \"password\", \"salt\", \"name\", \"email\", \"external\", \"active\", \"roles\", \"changetimestamp\") SELECT
19+
'admin',
20+
'SHA=7f062dc2ef82d2b87f012fc17d70c372aa4e2883d9b6c5c1cc7382a5c868b724',
21+
'eWAbKQmxNP',
22+
'admin',
23+
24+
false,
25+
'1',
26+
'{admin,cf_remoteagent}',
27+
now()
28+
ON CONFLICT (username,
29+
external) DO UPDATE
30+
31+
SET
32+
password = 'SHA=7f062dc2ef82d2b87f012fc17d70c372aa4e2883d9b6c5c1cc7382a5c868b724',
33+
salt = 'eWAbKQmxNP';"""
34+
935

1036
@auto_connect
1137
def agent_run(data, *, connection=None):
@@ -21,15 +47,13 @@ def agent_run(data, *, connection=None):
2147
log.debug(output)
2248

2349

24-
def disable_password_dialog(host):
50+
@auto_connect
51+
def disable_password_dialog(host, *, connection=None):
2552
print("Disabling password change on hub: '{}'".format(host))
26-
api = "https://{}/api/user/admin".format(host)
27-
d = json.dumps({"password": "password"})
28-
creds = "admin:admin"
29-
header = "Content-Type: application/json"
30-
c = "curl -X POST -k {} -u {} -H '{}' -d '{}'".format(api, creds, header, d)
31-
log.debug(c)
32-
os.system(c)
53+
ssh_sudo(
54+
connection,
55+
'/var/cfengine/bin/psql cfsettings -c "{}"'.format(SET_ADMIN_PASSWORD_QUERY),
56+
)
3357

3458

3559
def def_json(call_collect=False):

cf_remote/remote.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def get_info(host, *, users=None, connection=None):
283283

284284

285285
@auto_connect
286-
def install_package(host, pkg, data, *, connection=None):
286+
def install_package(host, pkg, data, demo, *, connection=None):
287287
print("Installing: '{}' on '{}'".format(pkg, host))
288288
output = None
289289
if ".deb" in pkg:
@@ -325,7 +325,7 @@ def install_package(host, pkg, data, *, connection=None):
325325

326326
if output is None:
327327
log.error("Installation failed on '{}'".format(host))
328-
else:
328+
elif not demo:
329329
m = re.search(r"\#+[^\#]+\#+", output) # filtrate out junk output
330330
if m:
331331
print("\n{}\n".format(m.group(0)))
@@ -567,7 +567,7 @@ def install_host(
567567
scp(package, host, connection=connection)
568568
package = basename(package)
569569

570-
success = install_package(host, package, data, connection=connection)
570+
success = install_package(host, package, data, demo, connection=connection)
571571
if not success:
572572
# errors already logged
573573
return 1
@@ -613,7 +613,7 @@ def install_host(
613613
host, connection=connection, call_collect=call_collect
614614
)
615615
demo_lib.agent_run(data, connection=connection)
616-
demo_lib.disable_password_dialog(host)
616+
demo_lib.disable_password_dialog(host, connection=connection)
617617
demo_lib.agent_run(data, connection=connection)
618618
return 0
619619

cf_remote/ssh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ def ssh_sudo(connection, cmd, errors=False, needs_pty=False):
225225
assert connection
226226

227227
if connection.needs_sudo:
228-
escaped = cmd.replace('"', r"\"")
229-
cmd = "sudo %s" % escaped
228+
cmd = "sudo %s" % cmd
230229

231230
if needs_pty:
232231
cmd = 'script -qec "%s" /dev/null' % cmd

0 commit comments

Comments
 (0)