summaryrefslogtreecommitdiffstats
path: root/plugins/tedge_docker_plugin/tedge_docker_plugin.sh
blob: 1bdb3bd1dd9b8364a41ce560240e10dd9a63504d (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
107
108
109
110
111
112
#!/bin/bash

usage() {
    cat << EOF
USAGE:
    docker <SUBCOMMAND>

SUBCOMMANDS:
    list           List all the installed modules
    prepare        Prepare a sequences of install/remove commands
    install        Install a module
    remove         Uninstall a module
    finalize       Finalize a sequences of install/remove commands
EOF
}

unsupported_args_check() {
    if [ -n "$1" ]; then
        echo "Unsupported arguments: $*"
        exit 1
    fi
}

extract_image_tag_from_args() {
    IMAGE_NAME="$1"
    if [ -z "$IMAGE_NAME" ]; then
        echo "Image name is a mandatory argument"
        exit 1
    fi
    shift   # Pop image name from args list
    IMAGE_TAG=$IMAGE_NAME

    if [ -n "$1" ]; then
        case "$1" in
            --module-version)
                IMAGE_VERSION="$2"
                IMAGE_TAG=$IMAGE_NAME:$IMAGE_VERSION
                shift 2  # Pop --version and the version value from the args list
                ;;
            *)
                echo "Unsupported argument option: $1"
                exit 1
                ;;
        esac
    fi

    unsupported_args_check "$@"

}

if [ -z "$1" ]; then
    echo -e "Provide at least one subcommand\n"
    usage
    exit 1
fi

COMMAND="$1"
shift   # Pop the command from args list

case "$COMMAND" in
    prepare)
        unsupported_args_check "$@"
        # nothing to do here
        ;;
    list)
        unsupported_args_check "$@"
        docker image list --format '{{.Repository}}\t{{.Tag}}' || exit 2
        ;;
    install)
        # Extract the docker image tag into the IMAGE_TAG variable
        extract_image_tag_from_args "$@"

        # Stop all containers using the provided image name
        containers=$(docker ps -a --format "{{.ID}} {{.Image}}" | grep "$IMAGE_NAME" | awk '{print $1}') || exit 2
        if [ -z "$containers" ]
        then
            echo "No containers to update. Spawning a new one."
            docker run -d "$IMAGE_TAG" || exit 2
        else
            echo "Updating existing containers."
            for container in $containers
            do
                docker rm "$(docker stop "$container")" || exit 2

                # Spawn new containers with the provided image name and version to replace the stopped one
                docker run -d "$IMAGE_TAG" || exit 2
            done
        fi
        ;;
    remove)
        extract_image_tag_from_args "$@"

        containers=$(docker ps -a --format "{{.ID}} {{.Image}}" | grep "$IMAGE_TAG" | awk '{print $1}') || exit 2
        if [ -z "$containers" ]
        then
            echo "No containers found for the image: $IMAGE_TAG"
        fi
        for container in $containers
        do
            docker rm "$(docker stop "$container")" || exit 2
        done
        ;;
    finalize)
        unsupported_args_check "$@"
        # Prune all the unused images. The --force command is used to avoid a [y/N] user prompt
        docker image prune --all --force || exit 2
        ;;
    *)
        echo "Unsupported command: $COMMAND"
        exit 1
        ;;
esac