Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

[Kube Runtime] Refine init and runtime scripts in k8s pods. #3245

Merged
merged 4 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/kube-runtime/build/kube-runtime.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ WORKDIR /pai-runtime
COPY --from=frameworkcontroller/frameworkbarrier:v0.3.0 $BARRIER_DIR/frameworkbarrier .
COPY src/ ./

CMD ["/bin/sh", "-c", "/pai-runtime/entry"]
CMD ["/bin/sh", "-c", "/pai-runtime/init"]
27 changes: 12 additions & 15 deletions src/kube-runtime/src/entry → src/kube-runtime/src/init
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# This script is entrypoint of pai runtime, this will copy run to /usr/local/pai/run
# which will be the entrypoint of task container. So `entry` running in runtime
# container, `/usr/local/pai/run` running in task container, and will be pid 1 in
# task container
# This init script will be executed inside init container,
# all scripts under init.d will be executed in priority order.
# Init scripts will do preparations before user container starts.
# Runtime script will be executed as the entrypoint of user container
# and will be pid 1 process in user container.

PAI_DIR=/usr/local/pai
INIT_DIR=${PAI_DIR}/init
RUNTIME_DIR=${PAI_DIR}/runtime.d
PAI_LOG_DIR=${PAI_DIR}/logs

MAIN_SH=${INIT_DIR}/main.sh
LOG_FILE=${PAI_LOG_DIR}/${FC_POD_UID}_entry.log

mkdir -p $PAI_LOG_DIR
cp /pai-runtime/run $PAI_DIR
cp -r /pai-runtime/init $PAI_DIR
cp /pai-runtime/runtime $PAI_DIR
cp -r /pai-runtime/runtime.d $PAI_DIR

# do preparation for each script
for i in `find /pai-runtime/prep/ -type f -regex ".*.sh"` ; do
for i in `find /pai-runtime/init.d/ -type f -regex ".*.sh"` ; do
file_name=`basename $i`
echo -e "##### [${i} start] ##### \n"
$i >> ${PAI_LOG_DIR}/${FC_POD_UID}_prep.log 2>&1
Expand All @@ -47,14 +47,11 @@ cd $PAI_LOG_DIR

/pai-runtime/frameworkbarrier > $PAI_LOG_DIR/${FC_POD_UID}_barrier.log 2>&1
echo "barrier returns $?" >> $LOG_FILE
python /pai-runtime/parse.py framework.json > $PAI_DIR/runtime_env.sh 2> $PAI_LOG_DIR/${FC_POD_UID}_parse.log
python /pai-runtime/runtime.d/parse.py framework.json > $PAI_DIR/runtime_env.sh 2> $PAI_LOG_DIR/${FC_POD_UID}_parse.log
echo "parser.py returns $?" >> $LOG_FILE

# prepare main.sh, which is where user command get started
echo '#!/bin/sh' >> $MAIN_SH
echo -e "\n\n$USER_CMD" >> $MAIN_SH

chmod +x $MAIN_SH
# prepare user.sh, which is where user command get started
echo -e "\n$USER_CMD" >> ${RUNTIME_DIR}/user.sh

# debug
echo -e "finished entry\nmain.sh has:" >> $LOG_FILE
Expand Down
File renamed without changes.
37 changes: 15 additions & 22 deletions src/kube-runtime/src/run → src/kube-runtime/src/runtime
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,25 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


# This script tries to behave like initd, will execute shell scripts under
# `/usr/local/pai/init`, main.sh will get a special treat: it will start after
# all other script and if main.sh exit this initd will kill all other processes in
# container.
# This runtime script will be executed inside user container,
# all scripts under runtime.d will be executed in priority order.
# User's commands will start in the end, and whole runtime script
# will exit after user's commands exit.

PAI_DIR=/usr/local/pai
INIT_DIR=${PAI_DIR}/init
RUNTIME_DIR=${PAI_DIR}/runtime.d
#PAI_LOG_DIR=/usr/local/pai/logs/attempt-${FC_FRAMEWORK_ATTEMPT_ID}/role-${FC_TASKROLE_NAME}/idx-${FC_TASK_INDEX}/attempt-${FC_TASK_ATTEMPT_ID}/
PAI_LOG_DIR=${PAI_DIR}/logs

. $PAI_DIR/runtime_env.sh

for i in `find $INIT_DIR/ -type f -regex ".*.sh"` ; do
file_name=`basename $i`
if [ $file_name = "main.sh" ] ; then
echo "skip main.sh for now"
continue
else
echo "starting ${file_name}"
$i > ${PAI_LOG_DIR}/${FC_POD_UID}_${file_name}_init.log 2>&1 &
fi
done

echo "starting main.sh"
$INIT_DIR/main.sh 2>&1 | tee ${PAI_LOG_DIR}/${FC_POD_UID}_main.log & # TODO tee may not exist in user's container
MAIN_PID=$!

echo "wait for main"
wait $MAIN_PID
# To run other runtime scripts in user container,
# execute them here in order.
# ${RUNTIME_DIR}/runtime.sh > ${PAI_LOG_DIR}/${FC_POD_UID}_runtime.log 2>&1 &

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call all scripts in runtime.d here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script owner should add entrypoint here by themselves.

printf "[INFO] USER COMMAND START\n"
${RUNTIME_DIR}/user.sh 2>&1 | tee ${PAI_LOG_DIR}/${FC_POD_UID}_main.log & # TODO tee may not exist in user's container
USER_PID=$!

printf "[INFO] USER COMMAND END\n"
wait ${USER_PID}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# TODO prepare sshd binary to user

# user's commands here