Parse string in sh file - bash

For a gitlab ci/cd project, I need to find the url of a knative service (used to deploy a webservice) so that I can utilize it as my base url for load testing
I have found that I can find the url (and other information) with the command: kubectl get ksvc helloworld-go, which outputs:
NAME URL LATESTCREATED LATESTREADY READY REASON
helloworld-go http://helloworld-go.default.34.83.80.117.xip.io helloworld-go-96dtk helloworld-go-96dtk True
Can someone please provide me an easy way to extract only the url in a sh script? I believe the easiest way might be to find the text between the first and second space on the second line.

kubectl get ksvc helloworld-go | grep -oP "http://[^\t]*"
or
kubectl get ksvc helloworld-go | grep -Eo "http://[^[:space:]]*"

Related

openshift commands to catch POD name programmatically/scripting

I have pods in my open shift and want to work on multiple open shift applications. Lets say like below
sh-4.2$ oc get pods
NAME READY STATUS RESTARTS AGE
jenkins-7fb689fc66-fs2xb 1/1 Running 0 4d
jenkins-disk-check-1587834000 0/1 Completed 0 21h
NAME READY STATUS RESTARTS AGE
jenkins-7fb689fc66-gsz9j 0/1 Running 735 9d
jenkins-disk-check-1587834000
NAME READY STATUS RESTARTS AGE
jenkins-9euygc66-gsz9j 0/1 Running 735 9d
I have tried with below command
oc get pods
export POD=$(oc get pods | awk '{print $1}' | grep jenkins*)
I want to find the pods starting with numbers "jenkins-7fb689fc66-fs2xb",jenkins-9euygc66-gsz9j, etc... using scripting and need to ignore disk check pods. If i catch the above pods and need to execute the terminal and run some shell commands via programmatically. Can someone help me on this?
kubectl get (and by extension oc get) is a very versatile tool. Unfortunately, after looking around online for awhile, you will definitely not be able to do Regex without relying on an external tool like awk or grep. (I know this wasn't exactly what you were asking, but I figured I'd at least try to see if it's possible.
With that said, there are a couple of tricks you can rely on to filter your oc get output before you even have to pull in external tools (bonus points because this filtering occurs on the server before it even hits your local tools).
I first recommend running oc get pods --show-labels, because if the pods you need are appropriately labeled, you can use a label selector to get just the pods you want, e.g.:
oc get pods --selector name=jenkins
oc get pods --selector <label_key>=<label_value>
Second, if you only care about the Running pods (since the disk-check pods look like they're already Completed), you can use a field selector, e.g.:
oc get pods --field-selector status.phase=Running
oc get pods --field-selector <json_path>=<json_value>
Finally, if there's a specific value that you're after, you can pull that value into the CLI by specifying custom columns, and then greping on the value you care about, e.g.:
oc get pods -o custom-columns=NAME:.metadata.name,TYPES:.status.conditions[*].type | grep "Ready"
The best thing is, if you rely on the label selector and/or field selector, the filtering occurs server side to cut down on the data that ends up making it to your final custom columns, making everything that much more efficient.
For your specific use case, it appears that simply using the --field-selector would be enough, since the disk-check pods are already Completed. So, without further information on exactly how the Jenkins pod's JSON is constructed, this should be good enough for you:
oc get pods --field-selector status.phase=Running
Assuming that you need to print jenkins id in first field, could you please try following.
awk 'match($0,/jenkins[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file
Explanation: Adding explanation for above code.
awk ' ##Starting awk program from here.
match($0,/jenkins[^ ]*/){ ##Using match function in which mentioning regex jenkins till spacein current line.
print substr($0,RSTART,RLENGTH) ##Printing sub-string in current line where starting point is RSTART till RLENGTH value.
}
' Input_file ##Mentioning Input_file name here.
Adding this answer for others reference.
You could use in this way.
export POD=$(oc get pods | awk '{print $1}' | grep jenkins* | grep -v jenkins-disk-check)

Trying to make a list in the following format using linux bash commands (awk,cut, or any solution)

I've been trying to make one list of my local servers containing the credentials to automate process on my side. Here's the file I have right now:
head hosts-only.txt
192.168.2.101
192.168.2.102
192.168.2.103
192.168.2.105
192.168.2.107
head user-only.txt
admin
tomcat
oracle
head pass-only.txt
123456
secret
secure
ofc, password are not real below, just using them as an example. Now, what I am trying to accomplish is getting one 'list.txt' containing the information in the following format:
192.168.2.101:admin:123456
192.168.2.102:tomcat:secret
192.168.2.103:oracle:secure
Any help would be very appreciated,
Thanks !
Look at
paste -d: hosts-only.txt user-only.txt pass-only.txt

How to list the published container images in the Google Container Registry in a CLI in image size order

Using a CLI, I want to list the images in each repository in a Google Container Registry project but with the following conditions:
Lists the images with the latest tag only
Lists the human-readable size of the images
Lists the name of the images
The closest I've managed to get us through gsutil:
gsutil du -h gs://eu.artifacts.my-registry.appspot.com/containers/images
Resulting in:
33.77 MiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c1a2387ef6cb30a7428a46821f946d6a2c591a26cb2066891c55b2b6846ae2
1.27 MiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c1e7db6bf0140bd5fa34236a35453cb73cef01f6d89b98bc5995ae8ea07aaf
1.32 KiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c3c97495d60c68d37d04a7e6c9b3a48bb159ce5dde13d0d81b4e75e2a3f1d4
81.92 KiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c5483cb8ac9c9ae498507e15d68d909a11859a8e5238556b7188e0af4d9264
457.43 KiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c7f98faa1cfc05264e743e23ca2e118d24c57bfd67d5cb2e2c7a57e8124b6c
7.88 KiB gs://eu.artifacts.my-registry.appspot.com/containers/images/sha256:03c83b13d044844cd3f6b278382e408541f22029acaf55d9e7e5689b8d51eeea
But obviously this does not meet most of my criteria.
The information is available through the GUI like so on a per image basis:
Any ideas?
I'm open to gsutil, gcloud, docker, anything really which can be installed on a docker container.
You can use the Google Cloud UI to accomplish this. There's a column selector right next to the filter bar and it has an option for the image size.
Once the column is displayed, you'll be able to order by size.
Its seems you have only one outstanding issue with listing container images size after reading your comment at Jason's answer. So it is not possible to retrieve with gcloud command directly. Here are two work around I tested:
You can use gcloud container images describe command to see the size of the images. Make sure you use "--log-http" flag with it. Command should be like this:
$ gcloud container images describe gcr.io/myproject/myimage:tag --log-http
Another way to get the size of the image is using gsutil stat command.
So here's what I did:
a. Upon running below command, I listed all my images from the GCS bucket and saved it to a file called images.txt
$ gsutil ls "BUCKET URL" > images.txt
b. I ran gcloud stat command like below to read image names from the images.txt file and return size of the images chronologically.
$ for x in $(cat images.txt); do `gsutil stat $x | grep Content-Length | awk '{print $2}'`; done
You can customize this little script according to your need.
I understand these are not efficient workaround but thats all seems to be an option now. However, GCR just implements the docker container API, so may be you can read this document to see if you can find/do something of your own.
Hi here just to share a rudimental script which takes the first tag and get the size of the whole layers and write it on a report, it takes ages on 3TB repo but at least i know which repo is big.
echo "REPO,SIZE" > repository-size-report.csv
for REPO in $(gcloud container images list --repository eu.gcr.io/comerge-comerge01-171833 --format="table[no-heading](NAME)") ; do
for TAGS in $(gcloud container images list-tags $REPO --format="table[no-heading](TAGS)"); do
TAG=$(echo $TAGS | cut -d, -f1)
SUM=0
for SIZE in $(gcloud container images describe $REPO:$TAG --log-http 2>&1 | grep size | grep -o '[0-9][0-9]*') ; do
SUM=$((SUM + SIZE))
done
HSUM=$(echo $SUM | numfmt --to iec --format "%8f")
echo "$REPO:$TAG,$HSUM"
echo "$REPO:$TAG,$HSUM" >> repository-size-report.csv
done
done
You can use the command gcloud container images list command to accomplish this task; however, you will need to set the appropriate flags to fulfill your use case. You can read more about the command and the flag options here.

How to use WebAPI in bash for sonarqube?

I want to write a shell script to login and get bugs for a project. I want the dashboard values like bugs, Vulnerabilities, code smells and coverage.
The url of dashboard is: http://www.example.com/dashboard?id=example_project_name.
Here is what I tried:
curl GET -u username:password http://www.example.com/api/issues/search?project=example_project_name&types=BUG.
So, this prints all the data. I just need the value show in the below image:
Basically What I want to achieve is that I’m using a Sonarqube plugin in Jenkins, so I use extended email plugin to send email for job execution and in that email I want to give details like number of bugs in the repository after the build.
Is there any other way?
Finally after reading the documentation carefully, I got the values. Here is the script that I created.
#!/bin/bash
vul=$(curl -sX GET -u username:password 'http://www.example.com/api/issues/search?projectKeys=example_project_name&types=VULNERABILITY');
bug=$(curl -sX GET -u username:password 'http://www.example.com/api/issues/search?projectKeys=example_project_name&types=BUG');
no_vul=$(echo $vul | jq -r .total);
no_bug=$(echo $bug | jq -r .total);
echo "Total number of VULNERABILITIES are $no_vul"
echo "Total number of BUGS are $no_bug"
Here is the API documentation URL.

Jmeter - url from command line

I have been trying to pass the url for a web page from the command line to do performance testing using Jmeter.
I had set up the user defined variables like
NumberOfUsers ${__P(NumberOfUsers,2)}
HowManyTimesToRun ${__P(HowManyTimesToRun,2)}
RampUpTime ${__P(RampUpTime, 10)}
Host ${__P(Host)}
I tried using the Jmeter command as
./jmeter.sh -n -t Performance.jmx -l old88.jtl -JNumberOfUsers=5 -JRampUpTime=10JHowManyTimesToRun=2 -JHost=www.google.com
It seems to take all the values correctly except the host name. Is there a way to pass the url from command line. I use this property in HTTP request Defaults.
I do not see any issues here - except a space ' ' and '-' are missing for 'HowManyTimesToRun' property. Maybe it s a typo!!
./jmeter.sh -n -t Performance.jmx -l old88.jtl -JNumberOfUsers=5 -JRampUpTime=10 -JHowManyTimesToRun=2 -JHost=www.google.com

Resources