我需要这个 bash 脚本的帮助。由于某种原因,我的标志没有被传递。我正在使用 unix
getopts
函数,
有人可以看一下并让我知道我哪里出了问题吗?
#!/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: 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:
- 'companydomain.com'
EOF
cat << EOF | kubectl apply -f -
apiVersion: 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: 'testing-jc2.companydomain.com'
dnsNames:
- 'testing-jc2.companydoamin.com'
EOF
cat << EOF | kubectl replace -f -
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: server
app.kubernetes.io/name: argocd-server
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:
app.kubernetes.io/name: argocd-server
EOF
cat << EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-server-ingress
namespace: argocd
annotations:
cert-manager.io/cluster-issuer: lets-encrypt-jasons-cert
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: 'true'
nginx.ingress.kubernetes.io/ssl-passthrough: 'true'
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"
我通过将标志更改为单个字符来实现此目的。 Getops 不适用于多字符标志。
#!/bin/bash
### Pass Flags
#Define the help function
function help(){
echo "Options:";
echo "-r Resource Group Name"
echo "-l Location of Resource Group"
echo "-c Cluster Name"
echo "-e Email for LetsEncrypt"
echo "-d domainname for Nginx"
exit 1;
}
#Initialize the default values for the variables.
r="rg";
l="location";
c="cluster";
e="email";
d="domainname";
#Define the getopts variables
options=":r:l:c:e:d:h";
#Start the getopts code
while getopts ${options} opt; do
case $opt in
r) #Get the resourcegroupname
rg=${OPTARG}
;;
l) #Get the location
location=${OPTARG}
;;
c) #Get the cluster name
cluster=${OPTARG}
;;
e) #Get the email
email=${OPTARG}
;;
d) #Get the domain name
domainname=${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='APIKEY'
sleep 60
cat << EOF | kubectl apply -f -
apiVersion: 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:
- 'companydomain.com'
EOF
cat << EOF | kubectl apply -f -
apiVersion: 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: 'testing-jc2.companydomain.com'
dnsNames:
- 'testing-jc2.companydomain.com'
EOF
cat << EOF | kubectl replace -f -
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: server
app.kubernetes.io/name: argocd-server
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:
app.kubernetes.io/name: argocd-server
EOF
cat << EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-server-ingress
namespace: argocd
annotations:
cert-manager.io/cluster-issuer: lets-encrypt-jasons-cert
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: 'true'
nginx.ingress.kubernetes.io/ssl-passthrough: 'true'
nginx.ingress.kubernetes.io/backend-protocol: 'HTTPS'
spec:
rules:
- host: $domainname
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
name: https
tls:
- hosts:
- $domainname
secretName: argocdingress-cert
EOF
echo "Deployment Complete"