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

Subscribers

People subscribed via source and target branches

to all changes: