forked from rhcarvalho/openshift-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-in-container
executable file
·57 lines (49 loc) · 1.98 KB
/
run-in-container
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
#!/bin/bash
# Use this script to run one-off commands inside a container of a pod where your
# Python application code lives in.
# You can accomplish the same results by using regular commands from OpenShift.
# This script is just wrapping calls to `oc` to make it a little more
# convenient to use. In the future, the `oc` cli tool might incorporate changes
# that make this script obsolete.
# Here is how you would run a command in a pod specified by label [1]:
#
# 1. Inspect the output of the command below to find the name of a pod that
# matches a given label:
#
# oc get pods -l <your-label-selector>
#
# 2. Open a bash shell in the pod of your choice.
# Because of how the images produced with CentOS and RHEL work currently [2],
# we need to wrap commands with `bash` to enable any Software Collections
# that may be used (done automatically inside every bash shell).
#
# oc exec -p <pod-name> -it -- bash
#
# 3. Finally, execute any command that you need and exit the shell.
#
# Related GitHub issues:
# [1] https://github.com/GoogleCloudPlatform/kubernetes/issues/8876
# [2] https://github.com/openshift/origin/issues/2001
# Usage examples:
#
# ./run-in-container.sh ./manage.py migrate
# ./run-in-container.sh ./manage.py createsuperuser
# ./run-in-container.sh ./manage.py shell
#
# If your Python pods are labeled with a name other than "django", you can use:
#
# POD_NAME=name ./run-in-container.sh ./manage.py check
#
# If there is more than one replica, you can also specify a POD by index:
#
# POD_INDEX=1 ./run-in-container.sh ./manage.py shell
#
# Or both together:
#
# POD_NAME=frontend POD_INDEX=2 ./run-in-container.sh ./manage.py shell
# Get name of a currently deployed pod by label and index
POD_INSTANCE_NAME=`oc get pods \
-l "name=${POD_NAME:-django}" \
-t "{{ with index .items ${POD_INDEX:-0} }}{{ .metadata.name }}{{ end }}"`
# Run command in a container of the specified pod:
oc exec -p "$POD_INSTANCE_NAME" -it -- bash -c "${@:-echo}"