bash
Archived091
A channel for bash scripting
E
erik12 months ago
archived the channel
Jasonover 1 year ago
Hi guys, I need help with this bash script please. For some reason, my flags are not being passed through. I'm using getopts,
Could someone take a look and let me know where I am going wrong?
Could someone take a look and let me know where I am going wrong?
#!/bin/bash
### Pass Flags
#Define the help function
function help(){
echo "Options:";
echo "-rg Resource Group Name"
echo "-loc Location of Resource Group"
echo "-clus Cluster Name"
echo "-e Email for LetsEncrypt"
echo "-hos Hostname for Nginx"
exit 1;
}
#Initialize the default values for the variables.
rg="rg";
loc="location";
clus="cluster";
e="email";
hos="hostname";
#Define the getopts variables
options="rg:loc:clus:e:hos:h";
#Start the getopts code
while getopts $options opt; do
case $opt in
rg) #Get the username
rg=$OPTARG
;;
loc) #Get the password
location=$OPTARG
;;
clus) #Get the repository name
cluster=$OPTARG
;;
e) #Get the service name
email=$OPTARG
;;
hos) #Get the branch name
hostname=$OPTARG
;;
h) #Execute the help function
"echo here"
help;
;;
\?) #unrecognized option - show help
echo "Invalid option."
help;
;;
esac
done
#This tells getopts to move on to the next argument.
shift $((OPTIND-1))
#End getopts code
#rg="jc-test29-aks-rg"
#location="francecentral"
#cluster="jc-aks-test29-cluster"
## Create RG
echo "Creating Resource Group $rg"
az group create --name $rg --location $location
## Create AKS Cluster
echo "Creating AKS Cluster $cluster"
az aks create -g $rg -n $cluster --enable-managed-identity --node-vm-size Standard_B2s --node-count 3 --load-balancer-sku Standard
## Conect to Cluster
echo "Connecting to AKS Cluster"
az aks get-credentials --resource-group $rg --name $cluster --overwrite-existing
## Wait for the Cluster to Come online.
sleep 5m
## Add the Repos
helm repo add ingress-nginx <https://kubernetes.github.io/ingress-nginx>
helm repo add cert-manager <https://charts.jetstack.io>
helm repo add argo <https://argoproj.github.io/argo-helm>
## Install nginx-ingress
helm install nginx-ingress ingress-nginx/ingress-nginx \
--namespace ingress-nginx --create-namespace \
--set controller.replicaCount=2 \
--set controller.admissionWebhooks.patch.nodeSelector."kubernetes\.io/os"=linux \
--set controller.service.annotations."service\.beta\.kubernetes\.io/azure-load-balancer-health-probe-request-path"=/healthz \
--set controller.service.externalTrafficPolicy=Local \
--set controller.nodeSelector."kubernetes\.io/os"=linux \
--set defaultBackend.nodeSelector."kubernetes\.io/os"=linux
## Install Cert Manager
helm install cert-manager cert-manager/cert-manager \
--namespace cert-manager --create-namespace \
--version v1.14.5 \
--set nodeSelector."kubernetes\.io/os"=linux \
--set installCRDs=true \
--set 'extraArgs={--dns01-recursive-nameservers=1.1.1.1:53}'
## Install ArgoCD
helm install argocd argo/argo-cd \
--namespace argocd --create-namespace \
--version "6.9.2" \
--set installCRDs=true \
-f argocd-values.yaml \
-f deployenvcongifmap.yaml
cat << EOF | kubectl -
port-forward svc/argocd-server 8080:443
EOF
#git clone <https://github.com/cloudflare/origin-ca-issuer.git>
cd origin-ca-issuer
kubectl apply -f deploy/crds
kubectl apply -f deploy/rbac
kubectl apply -f deploy/manifests
kubectl create secret generic jasons-api-key \
-n cert-manager\
--from-literal api-token='VALUE'
sleep 60
cat << EOF | kubectl apply -f -
apiVersion: <http://cert-manager.io/v1|cert-manager.io/v1>
kind: ClusterIssuer
metadata:
name: lets-encrypt-jasons-cert
namespace: cert-manager
spec:
acme:
email: $email
server: <https://acme-v02.api.letsencrypt.org/directory>
privateKeySecretRef:
# Secret resource that will be used to store the account's private key.
name: lets-encrypt-jasons-cert
solvers:
- dns01:
cloudflare:
email: $email
apiTokenSecretRef:
name: jasons-api-key
key: api-token
selector:
dnsZones:
- '<http://companydomain.com|companydomain.com>'
EOF
cat << EOF | kubectl apply -f -
apiVersion: <http://cert-manager.io/v1|cert-manager.io/v1>
kind: Certificate
metadata:
name: letsencrypt-jasons-cert
spec:
secretName: letsencrypt-jasons-cert
issuerRef:
name: lets-encrypt-jasons-cert
kind: ClusterIssuer
commonName: '<http://testing-jc2.companydomain.com|testing-jc2.companydomain.com>'
dnsNames:
- '<http://testing-jc2.companydoamin.com|testing-jc2.companydoamin.com>'
EOF
cat << EOF | kubectl replace -f -
apiVersion: v1
kind: Service
metadata:
labels:
<http://app.kubernetes.io/component|app.kubernetes.io/component>: server
<http://app.kubernetes.io/name|app.kubernetes.io/name>: argocd-server
<http://app.kubernetes.io/part-of|app.kubernetes.io/part-of>: argocd
name: argocd-server
namespace: argocd
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
- name: https
port: 443
protocol: TCP
targetPort: 8080
selector:
<http://app.kubernetes.io/name|app.kubernetes.io/name>: argocd-server
EOF
cat << EOF | kubectl apply -f -
apiVersion: <http://networking.k8s.io/v1|networking.k8s.io/v1>
kind: Ingress
metadata:
name: argocd-server-ingress
namespace: argocd
annotations:
<http://cert-manager.io/cluster-issuer|cert-manager.io/cluster-issuer>: lets-encrypt-jasons-cert
<http://kubernetes.io/ingress.class|kubernetes.io/ingress.class>: nginx
<http://kubernetes.io/tls-acme|kubernetes.io/tls-acme>: 'true'
<http://nginx.ingress.kubernetes.io/ssl-passthrough|nginx.ingress.kubernetes.io/ssl-passthrough>: 'true'
<http://nginx.ingress.kubernetes.io/backend-protocol|nginx.ingress.kubernetes.io/backend-protocol>: 'HTTPS'
spec:
rules:
- host: $hostname
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
name: https
tls:
- hosts:
- $hostname
secretName: argocdingress-cert
EOF
echo "Deployment Complete"Jasonover 1 year ago
I need some people that know #bash please
Brij Salmost 5 years ago
does anyone know how to convert a list (output from awscli) into JSON with key:value?
Brij Sover 5 years ago
Hey bash experts!
when I run that curl command in shell, it works fine but when I put that into a shell script it just returns null, any ideas why?
workspace_id=$(curl -s --header "Authorization: Bearer ${TFE_TOKEN}" --header "Content-Type: application/vnd.api+json" "<https://some/giant/url/here>" | jq -r .data.id)
echo "Workspace ID is ${workspace_id}"when I run that curl command in shell, it works fine but when I put that into a shell script it just returns null, any ideas why?
bradymalmost 6 years ago
jq '.[][].Name'Hemanthalmost 6 years ago
How do i jq only the values of the Name, if the json is like this
[
[
{
"Name": "AB"
}
],
[
{
"Name": "BA"
}
],
[
{
"Name": "CD"
}
],
] Erik Osterman (Cloud Posse)almost 6 years ago
no prob!
Shawn Petersenalmost 6 years ago
thanks man!
Shawn Petersenalmost 6 years ago
sometimes have to think outside the box
Shawn Petersenalmost 6 years ago
lol yep that works
Erik Osterman (Cloud Posse)almost 6 years ago
grep -Po '(?<=<version>)(\d.+.+?)(?=</version>)' file.xml | head -2 | tail -1Erik Osterman (Cloud Posse)almost 6 years ago
One quick way:
Shawn Petersenalmost 6 years ago
Hi all, hoping for some help. I need a regex to pull only the second occurence of the result of this expression..
grep -Po '(?<=<version>)(\d.+.+?)(?=</version>)' file.xml
so far nothing is working and I get all 17 results from the file.
grep -Po '(?<=<version>)(\d.+.+?)(?=</version>)' file.xml
so far nothing is working and I get all 17 results from the file.
Erik Osterman (Cloud Posse)almost 6 years ago
Adding @U010XGY9B46 bot
Jillian Rowealmost 6 years ago
This is great! All those damn kids can get off my lawn and we can all just run everything with bash and ssh!! 😉
J
Jillian Rowealmost 6 years ago
@Jillian Rowe has joined the channel
J
Joe Bagdonalmost 6 years ago
@Joe Bagdon has joined the channel
bradymalmost 6 years ago
That's a really broad question, could you give more details on what you're trying to do?
Takanalmost 6 years ago
Do you guys know how to write a script to call for a service to run ( i am using a tool and need to call it for launching in CI pipeline)
Takanalmost 6 years ago
Hi guys
T
Takanalmost 6 years ago
@Takan has joined the channel
M
Mark Hennemanalmost 6 years ago
@Mark Henneman has joined the channel
D
David Lozanoalmost 6 years ago
@David Lozano has joined the channel
J
Jann Speyeralmost 6 years ago
@Jann Speyer has joined the channel
Jeremy G (Cloud Posse)almost 6 years ago
@Hemanth try this:
for i in $(aws ec2 describe-instances | jq -r '.["Reservations"]|.[]|.Instances|.[]| .InstanceId' | sort -n); do echo "$i,$(aws cloudwatch get-metric-statistics --metric-name CPUUtilization --start-time 2020-02-20T12:00:00 --end-time 2020-02-20T13:00:00 --period 60 --namespace AWS/EC2 --extended-statistics p80 --dimensions Name=InstanceId,Value=$i | jq -r '[.Datapoints[]|select(.ExtendedStatistics.p80 >=0).ExtendedStatistics.p80] | @csv')" | grep -v ',$'; doneM
MorganGeekalmost 6 years ago
@MorganGeek has joined the channel
T
Tertius Standeralmost 6 years ago
@Tertius Stander has joined the channel
E
erik-stephensalmost 6 years ago
@erik-stephens has joined the channel
C
Chris Fowlesalmost 6 years ago
@Chris Fowles has joined the channel
K
Kendall Linkalmost 6 years ago
@Kendall Link has joined the channel
B
bradymalmost 6 years ago
@bradym has joined the channel
Erik Osterman (Cloud Posse)almost 6 years ago
Yea, it's awesome
Hemanthalmost 6 years ago
yes i am amazed at
jqErik Osterman (Cloud Posse)almost 6 years ago
It may be easier to get the query right rather than offload it to bash.
Hemanthalmost 6 years ago
sure
Hemanthalmost 6 years ago
trying to work my way through
Erik Osterman (Cloud Posse)almost 6 years ago
Since
jq is so powerful, better to share that part again.Hemanthalmost 6 years ago
yes
Erik Osterman (Cloud Posse)almost 6 years ago
Oh, I think this is stemming from an earlier question that involved
jq?Hemanthalmost 6 years ago(edited)
I am trying to do some sorting stuff, my output is like (those are aws instance IDs)
I want to remove those instances Id's in that list that doesn't have numbers under them
Instance-BCD222
19.5140495
12.7219977
13.3188209
Instance-OIUQWE
Instance-QOWIEU
Instance-QOWIEU
Instance-BCD123
19.5140495
12.7219977
13.3188209
Instance-QOWIEU
Instance-QWOEIU
Instance-ASLDKJ
Instance-BCD231
19.5140495
12.7219977
13.3188209I want to remove those instances Id's in that list that doesn't have numbers under them
Hemanthalmost 6 years ago
@Erik Osterman (Cloud Posse) haha yes 😄
I
Ievgenii Shepeliukalmost 6 years ago
@Ievgenii Shepeliuk has joined the channel
A
Andriy Knysh (Cloud Posse)almost 6 years ago
@Andriy Knysh (Cloud Posse) has joined the channel
J
Jeremy G (Cloud Posse)almost 6 years ago
@Jeremy G (Cloud Posse) has joined the channel
Erik Osterman (Cloud Posse)almost 6 years ago
@Hemanth any burning bash questions?
T
Tyrone Meijnalmost 6 years ago
@Tyrone Meijn has joined the channel
E
erikalmost 6 years ago
@Erik Osterman (Cloud Posse) has joined the channel
A
Adam Crewsalmost 6 years ago
@Adam Crews has joined the channel
H
Hemanthalmost 6 years ago
set the channel description: A channel for bash scripting