aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/xds-docker-create-container.sh
blob: 706a6a4563ec0ac5301c6a451c5e797dad4c4374 (plain)
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
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
/*                                                                                                */
/* This program is free software: you can redistribute it and/or modify                           */
/* it under the terms of the GNU General Public License as published by                           */
/* the Free Software Foundation, either version 2 of the License, or                              */
/* (at your option) any later version.                                                            */
/*                                                                                                */
/* This program is distributed in the hope that it will be useful,                                */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
/* GNU General Public License for more details.                                                   */
/*                                                                                                */
/* You should have received a copy of the GNU General Public License                              */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
/*                                                                                                */
/* You may also obtain this software under a propriety license from Microchip.                    */
/* Please contact Microchip for further information.                                              */
/*------------------------------------------------------------------------------------------------*/

/*!
 * \file
 * \brief Internal header file of the CNodeObserver class
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_NODEOBSERVER
 * @{
 */

#ifndef UCS_NODEOBSERVER_H
#define UCS_NODEOBSERVER_H

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_base.h"
#include "ucs_nodedis.h"
#include "ucs_rtm.h"
#include "ucs_nodeobserver_pb.h"

#ifdef __cplusplus
extern "C"
{
#endif

/*------------------------------------------------------------------------------------------------*/
/* Internal constants                                                                             */
/*------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------*/
/* Types                                                                                          */
/*------------------------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------------------------*/
/* Class                                                                                          */
/*------------------------------------------------------------------------------------------------*/

/*! \brief      CNodeObserver Class
 *  \details    Implements the NodeObserver
 */
typedef struct CNodeObserver_
{
    CBase               *base_ptr;              /*!< \brief Reference to base services */
    CNodeDiscovery      *nd_ptr;                /*!< \brief Reference to node discovery */
    CRouteManagement    *rtm_ptr;               /*!< \brief Reference to route management */
    Ucs_Mgr_InitData_t   init_data;             /*!< \brief Initialization data describing nodes and routes*/
    CMaskedObserver      event_observer;        /*!< \brief Observes init complete event */

    Ucs_Signature_t      eval_signature;
    Ucs_Nd_CheckResult_t eval_action;
    Ucs_Rm_Node_t       *eval_node_ptr;
    CTimer               wakeup_timer;          /*!< \brief Timer wakes up processing, sets current 
                                                 *          node available and restarts NodeDiscovery
                                                 */

} CNodeObserver;

/*------------------------------------------------------------------------------------------------*/
/* Methods                                                                                        */
/*------------------------------------------------------------------------------------------------*/
extern void Nobs_Ctor(CNodeObserver *self, CBase *base_ptr, CNodeDiscovery *nd_ptr, CRouteManagement *rtm_ptr, Ucs_Mgr_InitData_t *init_ptr);
extern Ucs_Nd_CheckResult_t Nobs_OnNdEvaluate(void *self, Ucs_Signature_t *signature_ptr);
extern void Nobs_OnNdReport(void *self, Ucs_Nd_ResCode_t code, Ucs_Signature_t *signature_ptr);

#ifdef __cplusplus
}               /* extern "C" */
#endif

#endif          /* UCS_NODEOBSERVER_H */

/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/
shift if [[ "$1" =~ .*:.* ]]; then USER_VOLUME_OPTION="-v $1" else echo "Invalid volume option, format must be /InDockerPath:/hostPath" exit 1 fi ;; -id) shift ID=$1 ;; *) if [[ "$1" =~ ^[\.0-9]+$ ]]; then IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$1 else IMAGE=$1 fi ;; esac shift done [ "$ID" = "" ] && ID=0 # Dynamically retrieve image name if [ "$IMAGE" = "" ]; then VERSION_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR --format '{{.Tag}}') VER_NUM=$(echo "$VERSION_LIST" | wc -l) if [ $VER_NUM -gt 1 ]; then echo "ERROR: more than one xds image found, please set explicitly the image to use !" echo "List of found images:" echo "$VERSION_LIST" exit 1 elif [ $VER_NUM -lt 1 ]; then echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR" exit 1 fi if [ "$VERSION_LIST" = "" ]; then echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR" usage exit 1 fi IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION_LIST fi USER=$(id -un) echo "Using instance ID #$ID (user $(id -un))" NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER docker ps -a |grep "$NAME" > /dev/null [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; } XDS_WKS=$HOME/xds-workspace XDTDIR=$XDS_WKS/.xdt_$ID SSH_PORT=$((2222 + ID)) WWW_PORT=$((8000 + ID)) BOOT_PORT=$((69 + ID)) NBD_PORT=$((10809 + ID)) # Delete container on error creation_done=false trap "cleanExit" 0 1 2 15 cleanExit () { if [ "$creation_done" != "true" -a "$NO_CLEANUP" != "true" ]; then docker rm -f "${NAME}" > /dev/null 2>&1 fi } ### Create the new container mkdir -p $XDS_WKS $XDTDIR || exit 1 docker run \ --publish=${SSH_PORT}:22 \ --publish=${WWW_PORT}:8000 \ --publish=${BOOT_PORT}:69/udp \ --publish=${NBD_PORT}:10809 \ --detach=true \ --hostname="$NAME" --name="$NAME" \ --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \ -v $XDTDIR:/xdt \ $USER_VOLUME_OPTION \ -it $IMAGE if [ "$?" != "0" ]; then echo "An error was encountered while creating docker container." exit 1 fi ### Ssh key echo "Copying your identity to container $NAME" echo -n wait ssh service . res=3 max=30 count=0 while [ $res -ne 0 ] && [ $count -le $max ]; do sleep 1 docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2 res=$? echo -n "." count=$(expr $count + 1); done echo ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh" docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys docker exec ${NAME} bash -c "chown $DOCKER_USER:$DOCKER_USER -R /home/$DOCKER_USER/.ssh ;chmod 0700 /home/$DOCKER_USER/.ssh; chmod 0600 /home/$DOCKER_USER/.ssh/*" ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit echo "You can now login using:" echo " ssh -p $SSH_PORT $DOCKER_USER@localhost" ### User / Group id if ($UPDATE_UID); then echo -n "Setup docker user and group id to match yours " docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel" res=3 max=30 count=0 while [ $res -ne 1 ] && [ $count -le $max ]; do sleep 1 docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2 res=$? echo -n "." count=$(expr $count + 1); done echo -n " ." # Set uid docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1 if [ "$?" = "0" -a "$(id -u)" != "1664" ]; then echo "Cannot set docker devel user id to your id: conflict id $(id -u) !" exit 1 fi docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1 echo -n "." # Set gid docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1 if [ "$?" = "0" ]; then docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1 else docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1 fi echo -n "." docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1 echo -n "." docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*" echo -n "." docker exec -t ${NAME} bash -c "systemctl start autologin" echo -n "." ssh -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user start xds-server" || exit 1 echo "." docker restart ${NAME} fi creation_done=true ### Force xds-server restart if ($FORCE_RESTART); then echo "Restart xds-server..." ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user restart xds-server" || exit 1 fi echo "Done, docker container $NAME is ready to be used."