containerlab icon indicating copy to clipboard operation
containerlab copied to clipboard

Append container id to local hosts file

Open BurlyLuo opened this issue 3 months ago • 2 comments

        container_id=$(docker ps --filter "name=$container" --format "{{.ID}}")
        if [ -n "$container_id" ]; then
            sed -i "/$container/s/$/ $container_id/" /etc/hosts
            echo "append $container id $container_id to local hosts"
        fi

with vm kind which like sonic-vm. there is requirement to ssh to the qemu/kvm vm. So if we can append the container id into the lcoal hosts file. like:

###### CLAB-vs-START ######
172.20.20.4     clab-vs-server1
172.20.20.5     clab-vs-server2
172.20.20.3     clab-vs-sonic-bgp 5286ef3cde33
172.20.20.6     clab-vs-sonic1 58159003922d
172.20.20.2     clab-vs-sonic2 7c535a0c42d7
3fff:172:20:20::4       clab-vs-server1
3fff:172:20:20::5       clab-vs-server2
3fff:172:20:20::3       clab-vs-sonic-bgp 5286ef3cde33
3fff:172:20:20::6       clab-vs-sonic1 58159003922d
3fff:172:20:20::2       clab-vs-sonic2 7c535a0c42d7

Append the whole yaml:

[root@rowan> 4-sonic-bgp-evpn]# cat 1-setup-env.sh 
cat <<EOF>clab.yaml | clab deploy -t clab.yaml -
name: vs
topology:
  nodes:
    sonic-bgp:
      kind: sonic-vm
      image: 192.168.2.100:5000/sonic:202305

    sonic1:
      kind: sonic-vm
      image: 192.168.2.100:5000/sonic:202305

    sonic2:
      kind: sonic-vm
      image: 192.168.2.100:5000/sonic:202305

    server1:
      kind: linux
      image: 192.168.2.100:5000/nettool
      exec:
      - ip addr add 10.1.5.10/24 dev eth1

    server2:
      kind: linux
      image: 192.168.2.100:5000/nettool
      exec:
      - ip addr add 10.1.5.11/24 dev eth1

  links:
    # sonic-vm container uses the following mapping for its linux interfaces:
    # eth0 - management interface connected to the containerlab management network
    # eth1 - first data (front-panel port) interface that is mapped to Ethernet0 port
    # eth2 - second data interface that is mapped to Ethernet4 port. Any new port will result in a "previous interface + 4" (Ethernet4) mapping.
    # When containerlab launches sonic-vs node, it will assign IPv4/6 address to the eth0 interface. Data interface eth1 mapped to Ethernet0 port.
    - endpoints: ["sonic-bgp:eth2", "sonic1:eth2"]
    - endpoints: ["sonic-bgp:eth3", "sonic2:eth2"]
    - endpoints: ["sonic1:eth1", "server1:eth1"]
    - endpoints: ["sonic2:eth1", "server2:eth1"]
EOF


REMOTE_PATH="/home/admin/"
USERNAME="admin"
PASSWORD="admin"
CONFIG_BASE_DIR="startupconf"
NODES=("sonic-bgp" "sonic1" "sonic2")

wait_for_healthy() {
    local container=$1
    local max_attempts=20
    local attempt=1
    
    while [ $attempt -le $max_attempts ]; do
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] Wait $container come into healthy..."
        if docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null | grep -q "healthy"; then
            return 0
        fi
        sleep 8
        ((attempt++))
    done
    return 1
}

for node in "${NODES[@]}"; do
    container="clab-vs-$node"
    node_config_dir="$CONFIG_BASE_DIR/$node"
    
    if wait_for_healthy "$container"; then
        container_id=$(docker ps --filter "name=$container" --format "{{.ID}}")
        if [ -n "$container_id" ]; then
            sed -i "/$container/s/$/ $container_id/" /etc/hosts
            echo "append $container id $container_id to local hosts"
        fi
        if [ -d "$node_config_dir" ]; then
            for file in sonic.conf vtysh.conf; do
                config_file="$node_config_dir/$file"
                if [ -f "$config_file" ]; then
                    echo "transfer $config_file to $container..."
                    if sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no -o ConnectTimeout=5 "$config_file" "$USERNAME@$container:$REMOTE_PATH" 2>/dev/null; then
                        echo "success transfer $file to $container"
                    else
                        echo "transfer $file to $container failed"
                    fi
                else
                    echo "file $config_file not exist,pass"
                fi
            done
        else
            echo "config directory $node_config_dir not exist"
        fi
    fi
done

if [[ $? -eq 0 ]]; then
    echo "# docker ps -a"
    docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Names}}" | grep -Ev 'registry|gostwire|edgeshark|openwrt' | awk NF
fi

BurlyLuo avatar Nov 20 '25 12:11 BurlyLuo

I don't exactly follow. Is it that you just want to SSH using container ID instead of hostname/container name?

kaelemc avatar Nov 20 '25 13:11 kaelemc

I don't exactly follow. Is it that you just want to SSH using container ID instead of hostname/container name?

Both name and container id. if can native support from clab side. that's good.

To select a container's ID, one need only double-click the mouse, whereas selecting the name necessitates precise targeting. It is also noteworthy that the container ID is distinct.

BurlyLuo avatar Nov 21 '25 03:11 BurlyLuo