aws-samples / cloud9-to-power-vscode-blog

Artifacts used in the blog post Use AWS Cloud9 to Power your Visual Studio Code IDE
MIT No Attribution
41 stars 34 forks source link

Support for Ubuntu instances? #14

Open oieduardorabelo opened 8 months ago

oieduardorabelo commented 8 months ago

hello @nragusa , thank you so much for your article and guidance!

i'm trying to apply the learnings from amazon linux / ec2-user to an ubuntu 22 instance

i replaced all ec2-user from the script:

https://github.com/aws-samples/cloud9-to-power-vscode-blog/blob/main/scripts/stop-if-inactive.sh

and i noticed the instance is not shutting down

when i manually create an ubuntu 22 instance from the cloud9 dashboard

this is the stop-if-inactive.sh i see in my home directory and it works as expected:

#!/bin/bash
set -euo pipefail
CONFIG=$(cat /home/ubuntu/.c9/autoshutdown-configuration)
SHUTDOWN_TIMEOUT=${CONFIG#*=}
if ! [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]*$ ]]; then
    echo "shutdown timeout is invalid"
    exit 1
fi
is_shutting_down() {
    is_shutting_down_ubuntu &> /dev/null || is_shutting_down_al1 &> /dev/null || is_shutting_down_al2 &> /dev/null || is_shutting_down_al2023 &> /dev/null
}
is_shutting_down_ubuntu() {
    local TIMEOUT
    TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
    if [ "$?" -ne "0" ]; then
        return 1
    fi
    local SHUTDOWN_TIMESTAMP
    SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
    if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
        return 1
    else
        return 0
    fi
}
is_shutting_down_al1() {
    pgrep shutdown
}
is_shutting_down_al2() {
    local FILE
    FILE=/run/systemd/shutdown/scheduled
    if [[ -f "$FILE" ]]; then
        return 0
    else
        return 1
    fi
}
is_shutting_down_al2023() {
    local TIMEOUT
    TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
    if [ "$?" -ne "0" ]; then
        return 1
    fi
    local SHUTDOWN_TIMESTAMP
    SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
    if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
        return 1
    else
        return 0
    fi
}
is_vfs_connected() {
    pgrep -f vfs-worker >/dev/null
}

if is_shutting_down; then
    if [[ ! $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] || is_vfs_connected; then
        sudo shutdown -c
        echo > "/home/ubuntu/.c9/autoshutdown-timestamp"
    else
        TIMESTAMP=$(date +%s)
        echo "$TIMESTAMP" > "/home/ubuntu/.c9/autoshutdown-timestamp"
    fi
else
    if [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] && ! is_vfs_connected; then
        sudo shutdown -h $SHUTDOWN_TIMEOUT
    fi
fi

any guidance on how i can adjust your script to work with ubuntu 22?

or how can i debug the process?

thank you!

nragusa commented 8 months ago

Hi @oieduardorabelo -

I suspect this is because the stop-if-inactive.sh script provided assumes ec2-user exists, when in the case of an Ubuntu instance, the primary user is ubuntu. Try replacing the stop-if-inactive.sh script with this instead:

#!/bin/bash
set -euo pipefail
CONFIG=$(cat /home/ubuntu/.c9/autoshutdown-configuration)
SHUTDOWN_TIMEOUT=${CONFIG#*=}
if ! [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]*$ ]]; then
    echo "shutdown timeout is invalid"
    exit 1
fi
is_shutting_down() {
    is_shutting_down_ubuntu &> /dev/null || is_shutting_down_al1 &> /dev/null || is_shutting_down_al2 &> /dev/null || is_shutting_down_al2023 &> /dev/null
}
is_shutting_down_ubuntu() {
    local TIMEOUT
    TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
    if [ "$?" -ne "0" ]; then
        return 1
    fi
    local SHUTDOWN_TIMESTAMP
    SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
    if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
        return 1
    else
        return 0
    fi
}
is_shutting_down_al1() {
    pgrep shutdown
}
is_shutting_down_al2() {
    local FILE
    FILE=/run/systemd/shutdown/scheduled
    if [[ -f "$FILE" ]]; then
        return 0
    else
        return 1
    fi
}
is_shutting_down_al2023() {
    local TIMEOUT
    TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
    if [ "$?" -ne "0" ]; then
        return 1
    fi
    local SHUTDOWN_TIMESTAMP
    SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
    if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
        return 1
    else
        return 0
    fi
}
is_vfs_connected() {
    pgrep -f vfs-worker >/dev/null
}

is_vscode_connected() {
    pgrep -u ec2-user -f .vscode-server/bin/ -a | grep -v -F 'shellIntegration-bash.sh' >/dev/null
}

if is_shutting_down; then
    if [[ ! $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] || is_vfs_connected || is_vscode_connected; then
        sudo shutdown -c
        echo > "/home/ubuntu/.c9/autoshutdown-timestamp"
    else
        TIMESTAMP=$(date +%s)
        echo "$TIMESTAMP" > "/home/ubuntu/.c9/autoshutdown-timestamp"
    fi
else
    if [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] && ! is_vfs_connected && ! is_vscode_connected; then
        sudo shutdown -h $SHUTDOWN_TIMEOUT
    fi
fi