forked from oracle/weblogic-kubernetes-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-weblogic-credentials.sh
executable file
·120 lines (106 loc) · 3.68 KB
/
create-weblogic-credentials.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
# Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
#
# Description
# This sample script creates a Kubernetes secret for WebLogic domain admin credentials.
#
# The following pre-requisites must be handled prior to running this script:
# * The kubernetes namespace must already be created
#
# Secret name determination
# 1) secretName - if specified
# 2) domain1-weblogic-credentials - if secretName and domainUID are both not specified. This is the default out-of-the-box.
# 3) <domainUID>-weblogic-credentials - if secretName is not specified, and domainUID is specified.
# 4) weblogic-credentials - if secretName is not specified, and domainUID is specified as "".
#
# The generated secret will be labeled with
# weblogic.domainUID=$domainUID
# and
# weblogic.domainName=$domainUID
# Where the $domainUID is the value of the -d command line option, unless the value supplied is an empty String ""
#
script="${BASH_SOURCE[0]}"
#
# Function to exit and print an error message
# $1 - text of message
function fail {
echo [ERROR] $*
exit 1
}
# Try to execute kubectl to see whether kubectl is available
function validateKubectlAvailable {
if ! [ -x "$(command -v kubectl)" ]; then
fail "kubectl is not installed"
fi
}
function usage {
echo usage: ${script} -u username -p password [-d domainUID] [-n namespace] [-s secretName] [-h]
echo " -u username, must be specified."
echo " -p password, must be specified."
echo " -d domainUID, optional. The default value is domain1. If specified, the secret will be labeled with the domainUID unless the given value is an empty string."
echo " -n namespace, optional. Use the default namespace if not specified"
echo " -s secretName, optional. If not specified, the secret name will be determined based on the domainUID value"
echo " -h Help"
exit $1
}
#
# Parse the command line options
#
domainUID=domain1
namespace=default
while getopts "hu:p:n:d:s:" opt; do
case $opt in
u) username="${OPTARG}"
;;
p) password="${OPTARG}"
;;
n) namespace="${OPTARG}"
;;
d) domainUID="${OPTARG}"
;;
s) secretName="${OPTARG}"
;;
h) usage 0
;;
*) usage 1
;;
esac
done
if [ -z $secretName ]; then
if [ -z $domainUID ]; then
secretName=weblogic-credentials
else
secretName=$domainUID-weblogic-credentials
fi
fi
if [ -z ${username} ]; then
echo "${script}: -u must be specified."
missingRequiredOption="true"
fi
if [ -z ${password} ]; then
echo "${script}: -p must be specified."
missingRequiredOption="true"
fi
if [ "${missingRequiredOption}" == "true" ]; then
usage 1
fi
# check and see if the secret already exists
result=`kubectl get secret ${secretName} -n ${namespace} --ignore-not-found=true | grep ${secretName} | wc | awk ' { print $1; }'`
if [ "${result:=Error}" != "0" ]; then
fail "The secret ${secretName} already exists in namespace ${namespace}."
fi
# create the secret
kubectl -n $namespace create secret generic $secretName \
--from-literal=username=$username \
--from-literal=password=$password
# label the secret with domainUID if needed
if [ ! -z $domainUID ]; then
kubectl label secret ${secretName} -n $namespace weblogic.domainUID=$domainUID weblogic.domainName=$domainUID
fi
# Verify the secret exists
SECRET=`kubectl get secret ${secretName} -n ${namespace} | grep ${secretName} | wc | awk ' { print $1; }'`
if [ "${SECRET}" != "1" ]; then
fail "The secret ${secretName} was not found in namespace ${namespace}"
fi
echo "The secret ${secretName} has been successfully created in the ${namespace} namespace."