-
Notifications
You must be signed in to change notification settings - Fork 47
/
push.sh
executable file
·140 lines (121 loc) · 3.37 KB
/
push.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
set -ueo pipefail
usage() {
cat << EOF
Push Helm Chart to Nexus repository
This plugin provides ability to push a Helm Chart directory or package to a
remote Nexus Helm repository.
Usage:
helm nexus-push [repo] login [flags] Setup login information for repo
helm nexus-push [repo] logout [flags] Remove login information for repo
helm nexus-push [repo] [CHART] [flags] Pushes chart to repo
Flags:
-u, --username string Username for authenticated repo (assumes anonymous access if unspecified)
-p, --password string Password for authenticated repo (prompts if unspecified and -u specified)
EOF
}
declare USERNAME
declare PASSWORD
declare -a POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
usage
exit 0
;;
-u|--username)
if [[ -z "${2:-}" ]]; then
echo "Must specify username!"
echo "---"
usage
exit 1
fi
shift
USERNAME=$1
;;
-p|--password)
if [[ -n "${2:-}" ]]; then
shift
PASSWORD=$1
else
PASSWORD=
fi
;;
*)
POSITIONAL_ARGS+=("$1")
;;
esac
shift
done
[[ ${#POSITIONAL_ARGS[@]} -ne 0 ]] && set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [[ $# -lt 2 ]]; then
echo "Missing arguments!"
echo "---"
usage
exit 1
fi
indent() { sed 's/^/ /'; }
declare HELM3_VERSION="$(helm version --client --short | grep "v3\.")"
declare REPO=$1
declare REPO_URL="$(helm repo list | grep "^$REPO" | awk '{print $2}')/"
if [[ -n $HELM3_VERSION ]]; then
declare REPO_AUTH_FILE="$HOME/.config/helm/auth.$REPO"
else
declare REPO_AUTH_FILE="$(helm home)/repository/auth.$REPO"
fi
if [[ -z "$REPO_URL" ]]; then
echo "Invalid repo specified! Must specify one of these repos..."
helm repo list
echo "---"
usage
exit 1
fi
declare CMD
declare AUTH
declare CHART
case "$2" in
login)
if [[ -z "$USERNAME" ]]; then
read -p "Username: " USERNAME
fi
if [[ -z "$PASSWORD" ]]; then
read -s -p "Password: " PASSWORD
echo
fi
echo "$USERNAME:$PASSWORD" > "$REPO_AUTH_FILE"
;;
logout)
rm -f "$REPO_AUTH_FILE"
;;
*)
CMD=push
CHART=$2
if [[ -z "$USERNAME" ]] || [[ -z "$PASSWORD" ]]; then
if [[ -f "$REPO_AUTH_FILE" ]]; then
echo "Using cached login creds..."
AUTH="$(cat $REPO_AUTH_FILE)"
else
if [[ -z "$USERNAME" ]]; then
read -p "Username: " USERNAME
fi
if [[ -z "$PASSWORD" ]]; then
read -s -p "Password: " PASSWORD
echo
fi
AUTH="$USERNAME:$PASSWORD"
fi
else
AUTH="$USERNAME:$PASSWORD"
fi
if [[ -d "$CHART" ]]; then
CHART_PACKAGE="$(helm package "$CHART" | cut -d":" -f2 | tr -d '[:space:]')"
else
CHART_PACKAGE="$CHART"
fi
echo "Pushing $CHART to repo $REPO_URL..."
curl -is -u "$AUTH" "$REPO_URL" --upload-file "$CHART_PACKAGE" | indent
echo "Done"
;;
esac
exit 0