Merge bootstack-ops:sosreport-python into bootstack-ops:master

Proposed by Joe Guo
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: bootstack-ops:sosreport-python
Merge into: bootstack-ops:master
Diff against target: 137 lines (+131/-0)
1 file modified
bootstack-ops/sosreport.py (+131/-0)
Reviewer Review Type Date Requested Status
Chris Sanders Pending
Joe Guo Pending
Review via email: mp+382629@code.launchpad.net

Commit message

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Unmerged commits

2d16501... by Joe Guo

sosreport.py: generate sosreport with python

convert shell script to python to generate sosreport
ref: https://wiki.canonical.com/CDO/IS/Bootstack/Playbooks/SOS-Report-and-STS-SF-file-attachments

Signed-off-by: Joe Guo <email address hidden>

4ff45ec... by Felipe Reyes

Use 'source' in ceph-radosgw when upgrading

Reviewed-on: https://code.launchpad.net/~freyes/bootstack-ops/+git/bootstack-ops/+merge/376537
Reviewed-by: Alvaro Uria <email address hidden>

c19d158... by Felipe Reyes

Use 'source' in ceph-radosgw when upgrading

dbec91e... by Peter Sabaini

Try harder to bring up ifaces when link checking

Reviewed-on: https://code.launchpad.net/~peter-sabaini/bootstack-ops/+git/bootstack-ops-1/+merge/374784
Reviewed-by: Ryan Farrell <email address hidden>

0699031... by Peter Sabaini

Try harder to bring up ifaces when link checking

4727c41... by Peter Sabaini

Add Mellanox network link status

Reviewed-on: https://code.launchpad.net/~peter-sabaini/bootstack-ops/+git/bootstack-ops-1/+merge/368552
Reviewed-by: Alvaro Uria <email address hidden>
Reviewed-by: Joel Sing <email address hidden>

bd21389... by Peter Sabaini

Address review comments

0b4851a... by Andrea Ieri

Changes to Cisco hardware settings:
* clear ipmi encryption key
* disable SD card
* disable strong password policy

4c31caf... by Andrea Ieri

Stage pacemaker to allow crm_attribute calls. Bump version.

Reviewed-on: https://code.launchpad.net/~aieri/bootstack-ops/+git/bootstack-ops/+merge/372419
Reviewed-by: David O Neill <email address hidden>

2b6323c... by Andrea Ieri

Stage pacemaker to allow crm_attribute calls. Bump version.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/bootstack-ops/sosreport.py b/bootstack-ops/sosreport.py
0new file mode 1007550new file mode 100755
index 0000000..9ff82f2
--- /dev/null
+++ b/bootstack-ops/sosreport.py
@@ -0,0 +1,131 @@
1#!/usr/bin/env python3
2import argparse
3import json
4import os
5import shutil
6import subprocess
7
8TIMEOUT = 1800
9
10
11parser = argparse.ArgumentParser(
12 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
13 description='genererate sosreport')
14
15# make target exclusive, to simplify logic
16group = parser.add_mutually_exclusive_group(required=True)
17group.add_argument('-a', '--app', metavar='APP', dest='apps', help='comma separated application names')
18group.add_argument('-u', '--unit', metavar='UNIT', dest='units', help='comma separated unit names')
19group.add_argument('--machine', metavar='MACHINE', dest='machines', help='comma separated machine ids')
20parser.add_argument('case', metavar='CASE', help='salesforce case number, e.g.: 00272654')
21
22cli = parser.parse_args()
23CASE = cli.case
24
25
26if cli.apps:
27 JUJU_RUN_TARGETS = ['--app', cli.apps]
28 # expand apps to units for juju scp
29 juju_status_json = subprocess.check_output(['juju', 'status', '--format', 'json']).decode('utf8')
30 juju_status_dict = json.loads(juju_status_json)
31 juju_apps = juju_status_dict['applications']
32 JUJU_SCP_TARGETS = set([])
33 for app in cli.apps.split(','):
34 for unit in juju_apps[app]['units']:
35 JUJU_SCP_TARGETS.add(unit)
36elif cli.units:
37 JUJU_RUN_TARGETS = ['--unit', cli.units]
38 JUJU_SCP_TARGETS = set(cli.units.split(','))
39elif cli.machines:
40 JUJU_RUN_TARGETS = ['--machine', cli.machines]
41 JUJU_SCP_TARGETS = set(cli.machines.split(','))
42
43
44def juju_run(cmd):
45 # default itmeout is 5m, which is not enough for some case
46 fullcmd = [
47 'juju', 'run', '--show-log', '--timeout', '{}s'.format(TIMEOUT)
48 ] + JUJU_RUN_TARGETS + ['--', cmd]
49 print(' '.join(fullcmd))
50 subprocess.check_call(fullcmd)
51
52
53def juju_scp(target):
54 fullcmd = [
55 'juju', 'scp', '--no-host-key-checks', '--', '-r',
56 '{target}:{CASE_DIR}/*'.format(target=target, CASE_DIR=CASE_DIR),
57 CASE_DIR,
58 ]
59 print(' '.join(fullcmd))
60 subprocess.check_call(fullcmd)
61
62
63# use same dir path for both infra node and units
64CASE_DIR = '/tmp/sosreport'
65# rm and re-create the dir
66print('deleting local dir {} '.format(CASE_DIR))
67shutil.rmtree(CASE_DIR, ignore_errors=True)
68os.makedirs(CASE_DIR, exist_ok=False)
69
70cmds = [
71 'sudo rm -rf {CASE_DIR}'.format(CASE_DIR=CASE_DIR),
72 'mkdir -p {CASE_DIR}'.format(CASE_DIR=CASE_DIR),
73 'sudo apt-get install --yes sosreport',
74 'sudo ionice -c 3 sosreport --alloptions --all-logs --batch --case-id={CASE} --tmp-dir {CASE_DIR}'.format(CASE=CASE, CASE_DIR=CASE_DIR),
75 'sudo chown -R ubuntu {CASE_DIR}'.format(CASE_DIR=CASE_DIR),
76]
77
78# Do not use f string, which requires python 3.6+, but we still have 3.4 for many clouds
79for cmd in cmds:
80 juju_run(cmd)
81
82
83for target in JUJU_SCP_TARGETS:
84 juju_scp(target)
85
86
87print('removing reports ...')
88juju_run('rm --verbose -r {}'.format(CASE_DIR))
89
90# make sosreports readable to all users
91# in case you need to scp it back to your local machine with your own user
92subprocess.check_call(['chmod', '-R', 'a+r', CASE_DIR])
93print('sosreports are generated successfully and collected into {}:'.format(CASE_DIR))
94subprocess.check_call(['ls', '-alh', CASE_DIR])
95
96
97UPLOAD_TARGET = 'files.support.canonical.com'
98print('checking {UPLOAD_TARGET} ssh'.format(UPLOAD_TARGET=UPLOAD_TARGET))
99returncode = subprocess.call(['nc', '-w3', '-zv', UPLOAD_TARGET, '22'])
100can_upload = (returncode == 0)
101
102UPLOAD_CMD = ('scp '
103 '-o "StrictHostKeyChecking=no" '
104 '-o "PasswordAuthentication=yes" '
105 '-o "ConnectTimeout={TIMEOUT}" '
106 '{CASE_DIR}/* '
107 'ubuntu@{UPLOAD_TARGET}:').format(
108 UPLOAD_TARGET=UPLOAD_TARGET,
109 CASE_DIR=CASE_DIR,
110 TIMEOUT=TIMEOUT)
111
112HELP_YES = """
113Please check files in {CASE_DIR}. If they are ok, upload them with:
114
115 {UPLOAD_CMD}
116
117""".format(CASE_DIR=CASE_DIR, UPLOAD_CMD=UPLOAD_CMD)
118
119HELP_NO = """
120It seems this node has no access to {UPLOAD_TARGET}, run following cmds on your *local machine* to download files and upload from there instead:
121
122 rm -rf {CASE_DIR} && mkdir -p {CASE_DIR}
123 scp -r maas.XXXX.bootstack:{CASE_DIR}/* {CASE_DIR}
124 {UPLOAD_CMD}
125
126""".format(CASE_DIR=CASE_DIR, UPLOAD_TARGET=UPLOAD_TARGET, UPLOAD_CMD=UPLOAD_CMD)
127
128if can_upload:
129 print(HELP_YES)
130else:
131 print(HELP_NO)

Subscribers

People subscribed via source and target branches

to all changes: