-
Notifications
You must be signed in to change notification settings - Fork 6
/
collect-logs
executable file
·766 lines (659 loc) · 26.7 KB
/
collect-logs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#!/usr/bin/python
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from collections import namedtuple
import errno
from functools import partial
import logging
import multiprocessing
import os
import shutil
from subprocess import (
CalledProcessError, check_call, check_output, call, STDOUT)
import sys
from tempfile import mkdtemp
import yaml
log = logging.getLogger("collect-logs")
PRG = os.path.abspath(__file__)
LOGS = [
# Basics
"/var/log",
"/etc/hosts",
"/etc/network",
# for any core files
"/var/crash",
# for the landscape client
"/var/lib/landscape/client",
# for the landscape server charm
"/etc/apache2",
"/etc/haproxy",
# XXX This should be handled by the per-unit logs.
"/var/lib/lxc/*/rootfs/var/log",
# logs and configs for juju 1 using LXC (not LXD)
"/var/lib/juju/containers",
# for openstack
"/etc/nova",
"/etc/swift",
"/etc/neutron",
"/etc/ceph",
"/etc/glance",
]
EXCLUDED = ["/var/lib/landscape/client/package/hash-id",
"/var/lib/juju/containers/juju-*-lxc-template"]
LANDSCAPE_JUJU_HOME = "/var/lib/landscape/juju-homes"
# ps_mem is used for memory footprint collection
# The original repo is https://github.com/pixelb/ps_mem
PS_MEM_REPO = "https://github.com/fginther/ps_mem.git"
JUJU1 = "juju"
# XXX This is going to break once juju-2.1 happens.
# See https://bugs.launchpad.net/juju-core/+bug/1613864.
JUJU2 = "juju-2.1"
JUJU = JUJU1
DEFAULT_MODEL = object()
VERBOSE = False
JujuHost = namedtuple("JujuHost", ["name", "ip"])
JujuUnit = namedtuple("JujuUnit", ["name", "ip"])
# This contant is a marker to indicate that public-address wasn't found for a
# JujuUnit. In these cases, 'juju ssh <unit_name>' will be used instead of
# 'ssh ubuntu@<unit_ip>'
NO_PUBLIC_ADDRESS = "No public-address found"
if VERBOSE:
def call(args, env=None, _call=call):
print(" running {!r}".format(" ".join(args)))
return _call(args, env=env)
def check_call(args, env=None, _check_call=check_call):
print(" running {!r}".format(" ".join(args)))
return _check_call(args, env=env)
def check_output(args, stderr=None, env=None, _check_output=check_output):
print(" running {!r}".format(" ".join(args)))
return _check_output(args, stderr=stderr, env=env)
class Juju(object):
"""A wrapper around a juju binary."""
def __init__(self, binary_path=None, model=None, cfgdir=None, sudo=None,
juju_ssh=True):
if binary_path is None:
binary_path = JUJU
if model is DEFAULT_MODEL:
model = None
self.binary_path = binary_path
self.model = model
self.cfgdir = cfgdir
self.sudo = sudo
self.juju_ssh = juju_ssh
if binary_path == JUJU1:
self.envvar = "JUJU_HOME"
else:
self.envvar = "JUJU_DATA"
self.env = None
if cfgdir is not None:
self.env = dict(os.environ, **{self.envvar: cfgdir})
self.ssh_key = os.path.join(cfgdir, "ssh", "juju_id_rsa")
else:
# XXX This default config directory will only work on JUJU2
default_cfgdir = os.path.join(
os.path.expanduser("~"), ".local", "share", "juju")
cfgdir = os.environ.get(self.envvar, default_cfgdir)
self.ssh_key = os.path.join(cfgdir, "ssh", "juju_id_rsa")
def __repr__(self):
args = ", ".join("{}={!r}".format(name, getattr(self, name))
for name in ("binary_path", "model", "cfgdir"))
return "{}({})".format(self.__class__.__name__, args)
def __eq__(self, other):
if self.binary_path != other.binary_path:
return False
if self.model != other.model:
return False
if self.cfgdir != other.cfgdir:
return False
if self.sudo != other.sudo:
return False
return True
def __ne__(self, other):
return not(self == other)
@property
def envstr(self):
if not self.cfgdir:
return ""
else:
return "{}={}".format(self.envvar, self.cfgdir)
def status_args(self):
"""Return the subprocess.* args for a status command."""
args = self._resolve("status", "--format=yaml")
return args
def format_status(self):
"""Return the formatted juju status command."""
args = self.status_args()
return self._format(args)
def set_model_config_args(self, key, value):
item = "{}={}".format(key, value)
if self.binary_path == JUJU1:
args = self._resolve("set-env", item)
else:
args = self._resolve("model-config", item)
return args
def format_set_model_config(self, key, value):
"""Return the formatted model config command."""
args = self.set_model_config_args(key, value)
return self._format(args)
def _direct_ssh_args(self, ssh_cmd):
"""Return argument list for ssh commands using juju's private key."""
# Don't use juju ssh commands per lp:1473069
return [
"/usr/bin/{}".format(ssh_cmd), "-o", "StrictHostKeyChecking=no",
"-i", self.ssh_key]
def ssh_args(self, unit, cmd):
"""Return the subprocess.* args for an SSH command."""
if self.juju_ssh or unit.ip == NO_PUBLIC_ADDRESS:
return self._resolve("ssh", unit.name, cmd)
direct_ssh_args = self._direct_ssh_args("ssh")
return direct_ssh_args + ["ubuntu@{}".format(unit.ip), cmd]
def pull_args(self, unit, source, target="."):
"""Return the subprocess.* args for an SCP command."""
if self.juju_ssh or unit.ip == NO_PUBLIC_ADDRESS:
source = "{}:{}".format(unit.name, source)
return self._resolve("scp", source, target)
source = "ubuntu@{}:{}".format(unit.ip, source)
return self._direct_ssh_args("scp") + [source, target]
def push_args(self, unit, source, target):
"""Return the subprocess.* args for an SCP command."""
if self.juju_ssh or unit.ip == NO_PUBLIC_ADDRESS:
target = "{}:{}".format(unit.name, target)
return self._resolve("scp", source, target)
target = "ubuntu@{}:{}".format(unit.ip, target)
return self._direct_ssh_args("scp") + [source, target]
def format(self, cmd, *subargs):
"""Return the formatted command.
"sudo" and the juju config dir env var are set if appropriate.
"""
args = [cmd]
args.extend(subargs)
return self._format(args)
def _format(self, args):
"""Return the formatted args.
"sudo" and the juju config dir env var are set if appropriate.
"""
if self.cfgdir:
args.insert(0, self.envstr)
if self.sudo == "" or self.sudo is True:
args.insert(0, "sudo")
elif self.sudo:
args.insert(0, "sudo -u {}".format(self.sudo))
return " ".join(args)
def _resolve(self, sub, *subargs):
"""Return the subprocess.* args for the juju subcommand."""
args = [self.binary_path, sub]
if self.model:
if self.binary_path == JUJU1:
args.append("-e")
else:
args.append("-m")
args.append(self.model)
args.extend(subargs)
return args
def format_collect_logs(juju, script, target, inner=True):
"""Return the formatted command for the collect_logs script."""
if inner:
args = [script, "--inner"]
else:
args = [script]
args.extend(["--juju", juju.binary_path])
if juju.model and juju.model is not DEFAULT_MODEL:
args.extend(["--model", juju.model])
if juju.cfgdir:
args.extend(["--cfgdir", juju.cfgdir])
args.append(target)
return juju._format(args)
def juju_status(juju):
"""Return a juju status structure."""
output = check_output(juju.status_args(), env=juju.env)
output = output.decode("utf-8").strip()
return yaml.load(output)
def get_bootstrap_ip(juju, status=None):
if status is None:
status = juju_status(juju)
if "machines" not in status:
sys.exit("ERROR, no machines found. Make sure the right juju "
"environment is set.")
return status["machines"]["0"]["dns-name"]
def get_units(juju, status=None):
"""Return a list of JujuUnits."""
if status is None:
status = juju_status(juju)
juju_units = []
if "services" in status:
applications = status["services"]
else:
applications = status["applications"]
for application in applications:
# skip subordinate charms
if "subordinate-to" in applications[application].keys():
continue
if "units" in applications[application]:
for name, unit in applications[application]["units"].items():
if not unit.get("public-address"):
log.warning(
"Couldn't obtain public-address for unit {}".format(
name))
juju_units.append(JujuUnit(name, NO_PUBLIC_ADDRESS))
else:
juju_units.append(JujuUnit(name, unit["public-address"]))
if len(juju_units) == 0:
sys.exit("ERROR, no units found. Make sure the right juju environment"
"is set.")
return juju_units
def get_hosts(juju, status=None):
"""Return a list of machine hosts (not lxds)."""
if status is None:
status = juju_status(juju)
if "machines" not in status:
sys.exit("ERROR, no machines found. Make sure the right juju "
"environment is set.")
juju_hosts = []
for name, machine in status["machines"].items():
juju_hosts.append(JujuHost(name, machine["dns-name"]))
return juju_hosts
def _get_ps_mem(ps_mem, repo, repo_path):
if os.path.isfile(ps_mem):
# ps_mem already exists here via the push from the outer environment
log.info("Using uploaded {}".format(ps_mem))
return ps_mem
message = "Cloning ps_mem git repo {}".format(repo)
log.info(message)
try:
cmd = ["git", "clone", repo, repo_path]
check_output(cmd, stderr=STDOUT)
except CalledProcessError as e:
log.warning("Failed: " + message)
log.warning(e.output)
log.warning(e.returncode)
raise
return os.path.join(repo_path, "ps_mem.py")
def upload_ps_mem(juju, unit):
"""
Clone the ps_mem repository and upload ps_mem.py to the given unit.
"""
ps_mem_tmpdir = mkdtemp()
ps_mem = "/tmp/ps_mem.py"
try:
# Fetch the ps_mem utility from the github repo
# or the host unit if this is running on the inner model
ps_mem_source = _get_ps_mem(
ps_mem, PS_MEM_REPO, os.path.join(ps_mem_tmpdir, "ps_mem"))
# Upload ps_mem to the unit
args = juju.push_args(unit, ps_mem_source, ps_mem)
call(args, env=juju.env)
except CalledProcessError:
# Error messages are provided by _get_ps_mem_repo()
# Treat these exceptions as non-fatal and continue collecting logs
pass
finally:
shutil.rmtree(ps_mem_tmpdir)
def _run_cmd(juju, unit, cmd, description):
"""Helper method for running a command on a juju unit or host."""
log.info(description)
args = juju.ssh_args(unit, cmd)
try:
check_output(args, stderr=STDOUT, env=juju.env)
except CalledProcessError as e:
log.warning("Failed: " + description)
log.warning(e.output)
log.warning(e.returncode)
raise
def _create_ps_output_file(juju, unit):
"""List running processes and redirect them to a file."""
message = "Collecting ps output on unit {}".format(unit.name)
ps_cmd = "ps fauxww | sudo tee /var/log/ps-fauxww.txt"
try:
_run_cmd(juju, unit, ps_cmd, message)
except CalledProcessError:
# Error messages are provided by _run_cmd()
pass
def _create_ps_mem_output_file(juju, unit):
"""
Gather the aggregate memory footprint of each process using ps_mem.
"""
# The ps_mem utility is already present thanks to upload_ps_mem()
ps_mem = "/tmp/ps_mem.py"
try:
# ps_mem requries python 2, make sure it is installed
message = "Installing python for ps_mem on unit {}".format(unit.name)
apt_cmd = "if ! python -V; then sudo apt-get install -y python; fi"
_run_cmd(juju, unit, apt_cmd, message)
# Run ps_mem.py with -S to collect memory and swap footprint
ps_mem_cmd = "sudo {} -S | sudo tee /var/log/ps_mem.txt".format(ps_mem)
message = "Collecting {} output on unit {}".format(
ps_mem, unit.name)
_run_cmd(juju, unit, ps_mem_cmd, message)
except CalledProcessError:
# Error messages are provided by _run_cmd()
# Treat these exceptions as non-fatal and continue collecting logs
pass
def _create_log_tarball(juju, unit):
log.info("Creating tarball on unit {}".format(unit.name))
exclude = " ".join(["--exclude=%s" % x for x in EXCLUDED])
logs = "$(sudo sh -c \"ls -1d %s 2>/dev/null\")" % " ".join(LOGS)
# --ignore-failed-read avoids failure for unreadable files (not for files
# being written)
tar_cmd = "sudo tar --ignore-failed-read"
logsuffix = unit.name.replace("/", "-")
if unit.name == "0":
logsuffix = "bootstrap"
cmd = "{} {} -cf /tmp/logs_{}.tar {}".format(
tar_cmd, exclude, logsuffix, logs)
args = juju.ssh_args(unit, cmd)
ATTEMPTS = 5
for i in range(ATTEMPTS):
log.info("...attempt {} of {}".format(i+1, ATTEMPTS))
try:
check_output(args, stderr=STDOUT, env=juju.env)
except CalledProcessError as e:
# Note: tar command returns 1 for everything it considers a
# warning, 2 for fatal errors. Since we are backing up
# log files that are actively being written, or part of a live
# system, logging and ignoring such warnings (return code 1) is
# what we can do now.
# Everything else we might retry as usual.
if e.returncode == 1:
log.warning(
"tar returned 1, proceeding anyway: {}".format(e.output))
break
log.warning(
"Failed to archive log files on unit {}".format(unit.name))
log.warning(e.output)
log.warning(e.returncode)
if i < 4:
log.warning("...retrying...")
cmd = "{} {} --update -f /tmp/logs_{}.tar {}".format(
tar_cmd, exclude, logsuffix, logs)
args = juju.ssh_args(unit, cmd)
else:
# The command succeeded so we stop the retry loop.
break
else:
# Don't bother compressing.
log.warning("...{} attempts failed; giving up".format(ATTEMPTS))
return
cmd = "sudo gzip -f /tmp/logs_{}.tar".format(logsuffix)
args = juju.ssh_args(unit, cmd)
try:
check_output(args, stderr=STDOUT, env=juju.env)
except CalledProcessError as e:
log.warning(
"Failed to create remote log tarball on unit {}".format(unit.name))
log.warning(e.output)
log.warning(e.returncode)
def download_log_from_unit(juju, unit):
log.info("Downloading tarball from unit %s" % unit.name)
unit_filename = unit.name.replace("/", "-")
if unit.name == "0":
unit_filename = "bootstrap"
remote_filename = "logs_%s.tar.gz" % unit_filename
try:
args = juju.pull_args(unit, "/tmp/" + remote_filename)
call(args, env=juju.env)
os.mkdir(unit_filename)
args = ["tar", "-C", unit_filename, "-xzf", remote_filename]
call(args)
os.unlink(remote_filename)
except:
log.warning("error collecting logs from %s, skipping" % unit.name)
finally:
if os.path.exists(remote_filename):
os.unlink(remote_filename)
def collect_logs(juju):
"""
Remotely, on each unit, create a tarball with the requested log files
or directories, if they exist. If a requested log does not exist on a
particular unit, it's ignored.
After each tarball is created, it's downloaded to the current directory
and expanded, and the tarball is then deleted.
"""
units = get_units(juju)
# include bootstrap as one of the units
units.append(JujuUnit("0", get_bootstrap_ip(juju)))
log.info("Collecting running processes for all units including bootstrap")
map(partial(_create_ps_output_file, juju), units)
log.info("Collecting ps_mem output for all hosts including bootstrap")
hosts = get_hosts(juju)
map(partial(upload_ps_mem, juju), hosts)
map(partial(_create_ps_mem_output_file, juju), hosts)
log.info("Creating remote tarball in parallel for units %s" % (
",".join([u.name for u in units])))
map(partial(_create_log_tarball, juju), units)
log.info("Downloading logs from units")
_mp_map(partial(download_log_from_unit, juju), units)
def _mp_map(func, args):
pool = multiprocessing.Pool(processes=4)
pool.map(func, args)
def get_landscape_unit(units):
"""Return the landscape unit among the units list."""
units = [
unit for unit in units
if unit.name.startswith("landscape-server/") or
unit.name.startswith("landscape/")]
if len(units) == 0:
return None
else:
# XXX we don't yet support multiple landscape units. We would have to
# find out which one has the juju home
return units[0]
def get_inner_model(version, inner_model=DEFAULT_MODEL):
"""Return a best-effort guess at the inner model name."""
if inner_model is not DEFAULT_MODEL:
return inner_model
if version == JUJU1:
return None
# We assume that this is a Landscape-bootstrapped controller.
return "controller"
def disable_inner_ssh_proxy(juju, landscape_unit, inner_model=DEFAULT_MODEL):
"""
Workaround for #1607076: disable the proxy-ssh juju environment setting
for the inner cloud so we can juju ssh into it.
"""
log.info("Disabling proxy-ssh in the juju environment on "
"{}".format(landscape_unit))
cfgdir = "{0}/`sudo ls -rt {0}/ | tail -1`".format(LANDSCAPE_JUJU_HOME)
key = "proxy-ssh"
value = "false"
# Try Juju 2.
model2 = get_inner_model(JUJU2, inner_model)
inner2 = Juju(JUJU2, model=model2, cfgdir=cfgdir, sudo=True)
cmd2 = inner2.format_set_model_config(key, value)
args2 = juju.ssh_args(landscape_unit, cmd2)
try:
check_output(args2, stderr=STDOUT, env=juju.env)
except CalledProcessError as e:
log.warning("Couldn't disable proxy-ssh in the inner environment "
"using Juju 2, attempting Juju 1.")
log.warning("Error was:\n{}".format(e.output))
else:
return
# Try Juju 1.
model1 = get_inner_model(JUJU1, inner_model)
inner1 = Juju(JUJU1, model=model1, cfgdir=cfgdir, sudo=True)
cmd1 = inner1.format_set_model_config(key, value)
args1 = juju.ssh_args(landscape_unit, cmd1)
try:
check_output(args1, stderr=STDOUT, env=juju.env)
except CalledProcessError as e:
log.warning("Couldn't disable proxy-ssh in the inner environment "
"using Juju 1, collecting inner logs might fail.")
log.warning("Error was:\n{}".format(e.output))
def find_inner_juju(juju, landscape_unit, inner_model=DEFAULT_MODEL):
"""Return the juju dir and binary path, if any."""
# Identify the most recent juju "home" that landscape is using.
cmd = "sudo ls -rt {}/".format(LANDSCAPE_JUJU_HOME)
args = juju.ssh_args(landscape_unit, cmd)
try:
output = check_output(args, env=juju.env).strip()
except CalledProcessError:
return None
if output.startswith("sudo: "):
_, _, output = output.partition("\r\n")
indices = [m for m in output.split() if m and m.isdigit()]
if not indices:
return None
index = indices[-1]
juju_dir = os.path.join(LANDSCAPE_JUJU_HOME, index)
# Try Juju 2.
model2 = get_inner_model(JUJU2, inner_model)
inner2 = Juju(JUJU2, model=model2, cfgdir=juju_dir, sudo="")
cmd2 = inner2.format_status()
args2 = juju.ssh_args(landscape_unit, cmd2)
if call(args2, env=juju.env) == 0:
log.info("using Juju 2 for inner model")
return inner2
# Try Juju 1.
model1 = get_inner_model(JUJU1, inner_model)
inner1 = Juju(JUJU1, model=model1, cfgdir=juju_dir, sudo="landscape")
cmd1 = inner1.format_status()
args1 = juju.ssh_args(landscape_unit, cmd1)
if call(args1, env=juju.env) == 0:
log.info("using Juju 1 for inner model")
return inner1
# We didn't find an inner model.
return None
def collect_inner_logs(juju, inner_model=DEFAULT_MODEL):
"""Collect logs from an inner landscape[-server]/0 unit."""
log.info("Collecting logs on inner environment")
units = get_units(juju)
landscape_unit = get_landscape_unit(units)
if not landscape_unit:
log.info("No landscape[-server]/N found, skipping")
return
log.info("Found landscape unit {}".format(landscape_unit))
disable_inner_ssh_proxy(juju, landscape_unit, inner_model)
# Look up the inner model.
inner_juju = find_inner_juju(juju, landscape_unit, inner_model)
if inner_juju is None:
log.info(("No active inner environment found on {}, skipping"
).format(landscape_unit))
return
# Prepare to get the logs from the inner model.
collect_logs = "/tmp/collect-logs"
args = juju.push_args(landscape_unit, PRG, collect_logs)
call(args, env=juju.env)
filename = "inner-logs.tar.gz"
inner_filename = os.path.join("/tmp", filename)
args = juju.ssh_args(landscape_unit, "sudo rm -rf " + inner_filename)
call(args, env=juju.env)
# Upload ps_mem.py to allow uploading to each host in the inner model
upload_ps_mem(juju, landscape_unit)
# Collect the logs for the inner model.
cmd = format_collect_logs(inner_juju, collect_logs, inner_filename)
args = juju.ssh_args(landscape_unit, cmd)
check_call(args, env=juju.env)
# Copy the inner logs into a local directory.
log.info("Copying inner environment back")
cwd = os.getcwd()
target = os.path.join(cwd, filename)
args = juju.pull_args(landscape_unit, inner_filename, target)
check_call(args, env=juju.env)
try:
inner_dir = "landscape-0-inner-logs"
os.mkdir(inner_dir)
os.chdir(inner_dir)
try:
check_call(["tar", "-zxf", os.path.join(cwd, filename)])
finally:
os.chdir(cwd)
finally:
try:
os.remove(target)
except OSError as e:
if e.errno != errno.ENOENT:
log.warning(
"failed to remove inner logs tarball: {}".format(e))
def bundle_logs(tmpdir, tarfile, extrafiles=[]):
"""
Create a tarball with the directories under tmpdir and the
specified extra files. The tar command is executed outside
of tmpdir, so we need to a) use an absolute path that
includes tmpdir; and b) strip the tmpdir so it's not part of
the tarball.
We don't want absolute paths for extra files because otherwise
this path would be inside the tarball.
This allows you to have a tarball where this:
/tmp/tmpdir/foo
/tmp/tmpdir/bar
/home/ubuntu/data/log
Becomes this inside the tarball:
foo
bar
data/log
If collect-logs is run with CWD=/home/ubuntu and given data/log as
the extra file.
"""
args = ["tar", "czf", tarfile]
# get rid of the tmpdir prefix
args.extend(["--transform", "s,{}/,,".format(tmpdir[1:])])
# need absolute paths since tmpdir isn't the cwd
args.extend(os.path.join(tmpdir, d) for d in sorted(os.listdir(tmpdir)))
if extrafiles:
args.extend(extrafiles)
call(args)
def get_juju(binary_path, model=DEFAULT_MODEL, cfgdir=None, inner=False,
juju_ssh=True):
"""Return a Juju for the provided info."""
if model is DEFAULT_MODEL and inner and binary_path != JUJU1:
# We assume that this is a Landscape-bootstrapped controller.
model = "controller"
return Juju(binary_path, model=model, cfgdir=cfgdir, juju_ssh=juju_ssh)
def get_option_parser():
description = ("Collect logs from current juju environment and, if an "
"inner autopilot cloud is detected, include that.")
parser = ArgumentParser(description=description,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("--inner", action="store_true", default=False,
help="Collect logs for an inner model.")
parser.add_argument("--juju", default=JUJU2,
help="The Juju binary to use.")
parser.add_argument("--model", default=DEFAULT_MODEL,
help="The Juju model to use.")
parser.add_argument("--inner-model", default=DEFAULT_MODEL,
help="The Juju model to use for the inner juju.")
parser.add_argument("--cfgdir",
help="The Juju config dir to use.")
parser.add_argument("tarfile", help="Full path to tarfile to create.")
parser.add_argument("extrafiles", help="Optional full path to extra "
"logfiles to include, space separated", nargs="*")
return parser
def main(tarfile, extrafiles, juju=None, inner_model=DEFAULT_MODEL,
inner=False):
if juju is None:
juju = Juju()
# we need the absolute path because we will be changing
# the cwd
tmpdir = mkdtemp()
cwd = os.getcwd()
# logs are collected inside a temporary directory
os.chdir(tmpdir)
try:
collect_logs(juju)
if not inner:
try:
collect_inner_logs(juju, inner_model)
except:
log.warning("Collecting inner logs failed, continuing")
# we create the final tarball outside of tmpdir to we can
# add the extrafiles to the tarball root
os.chdir(cwd)
bundle_logs(tmpdir, tarfile, extrafiles)
log.info("created: %s" % tarfile)
finally:
call(["chmod", "-R", "u+w", tmpdir])
shutil.rmtree(tmpdir)
if __name__ == "__main__":
logging.basicConfig(
level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
parser = get_option_parser()
args = parser.parse_args(sys.argv[1:])
tarfile = os.path.abspath(args.tarfile)
juju = get_juju(
args.juju, args.model, args.cfgdir, args.inner, juju_ssh=False)
if args.inner:
log.info("# start inner ##############################")
try:
main(tarfile, args.extrafiles, juju, args.inner_model, args.inner)
finally:
if args.inner:
log.info("# end inner ################################")