Ich habe jetzt ein mehr oder weniger funktionierendes Beispiel, wie Sie unten sehen können, einige Pods in openshift zu töten.Trim String von Index
Es ist jetzt an diesem Punkt fest.
myindx = out.find(key)
print out[myindx:-1].rstrip()
out
ist die Ausgabe von oc get pod --all-namespaces ...
die wie folgt
-n namespace01 pod-name-9-xd45
-n namespace01 pod-name-9-xd67
-n namespace02 pod-name2-9-xd45
-n namespace02 pod-name2-9-rd45
mit out.find(key)
ich die richtige Linie zu finden sieht jetzt muss ich die ganze Linie bekommen oc delete $line
zu nennen.
In Perl würde ich alle Ausgaben von oc get pod --all-namespaces ...
in ein Array setzen und in jeder Zeile im Array suchen, aber wie kann ich das in Python tun?
Danke für jede Hilfe.
#!python
import StringIO
import sys
import re
import subprocess
import shlex
from collections import defaultdict
# Since you're keeping counts, we'll initialize this so that the values
# of the dictionary are `int`
myhash = defaultdict(lambda: 0)
if sys.argv[3] != "NodeReady":
sys.exit(0)
commandline = shlex.split("/bin/oc get pod --all-namespaces")
proc = subprocess.Popen(commandline,stdout=subprocess.PIPE)
out, err = proc.communicate()
buf = StringIO.StringIO(out)
for line in buf.readlines():
# print line
"""
Diese Projekte und pod Zeilen werden ignoriert
"""
if re.match('^NAMESPACE|.*-router|^logging|^default|.*infra-ipfailover|.*-(build|deploy)',line):
continue
linesplited = line.split()
prefix = re.split('(.*)-\d+-\w+$',linesplited[1])
#print linesplited[1]
#print prefix[1]
myhash[prefix[1]] += 1
commandline = shlex.split('/bin/oc get pod --all-namespaces -o template --template=\'{{range .items }} -n {{ .metadata.namespace }} {{.metadata.name}}\n{{end}}\'')
proc = subprocess.Popen(commandline,stdout=subprocess.PIPE)
out, err = proc.communicate()
#print out
buf = StringIO.StringIO(out)
#print buf
#for line in buf.readlines():
# print line
for key in myhash.keys():
print "<<<>>>"
# print myhash[key] % 2 , "\n"
print key,":",myhash[key]
if myhash[key] % 2 :
if myhash[key] > 1:
print "mod 2",key,":",myhash[key]
myindx = out.find(key)
print out[myindx].rstrip()
else:
print "not mod 2",key,":",myhash[key]
print out.find(key)
Vielen Dank. Ich habe es ein wenig adoptiert, aber das Konzept funktioniert ;-) – Aleksandar