Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Created January 13, 2017 22:34
Show Gist options
  • Select an option

  • Save nicolai86/aed6dab52bdf19ed13a5a06c6400445d to your computer and use it in GitHub Desktop.

Select an option

Save nicolai86/aed6dab52bdf19ed13a5a06c6400445d to your computer and use it in GitHub Desktop.

Revisions

  1. nicolai86 created this gist Jan 13, 2017.
    170 changes: 170 additions & 0 deletions reset-scaleway.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    #!/usr/bin/env bash

    if [ -z "$(which curl)" ]; then
    echo please install curl
    exit 1
    fi

    if [ -z "$(which jq)" ]; then
    echo please install jq
    exit 1
    fi

    if [[ "$SCALEWAY_PASSWORD" == "" || "$SCALEWAY_EMAIL" == "" ]]; then
    echo please export your scalway credentials as \$SCALEWAY_PASSWORD and \$SCALEWAY_EMAIL
    exit 2
    fi

    account_endpoint="https://account.scaleway.com"

    #
    # session helpers
    #

    function scw_login() {
    curl -s \
    -H "content-type: application/json" \
    -d "{\"password\":\"$SCALEWAY_PASSWORD\",\"email\":\"$SCALEWAY_EMAIL\"}" \
    "$account_endpoint/tokens" | jq -r '.token.id'
    }

    function scw_logout() {
    curl -s \
    -H "content-type: application/json" \
    -X DELETE \
    "$account_endpoint/tokens/$1"
    }

    #
    # resources
    #
    #

    function scw_resource() {
    curl -s \
    -H "content-type: application/json" \
    -H "x-auth-token: $1" \
    "$api_endpoint/$2?page=1&per_page=50"
    }

    function scw_servers() {
    scw_resource "$1" "servers"
    }
    function scw_volumes() {
    scw_resource "$1" "volumes"
    }
    function scw_ips() {
    scw_resource "$1" "ips"
    }
    function scw_security_groups() {
    scw_resource "$1" "security_groups"
    }

    #
    # actions
    #

    function scw_terminate_server() {
    curl -s \
    -H "x-auth-token: $1" \
    -H "content-type: application/json" \
    -d '{"action": "terminate"}' \
    "$api_endpoint/servers/$2/action"
    }

    function scw_delete_resource() {
    curl -s \
    -H "x-auth-token: $1" \
    -H "content-type: application/json" \
    -X DELETE \
    "$api_endpoint/$2/$3"
    }

    function scw_delete_server() {
    scw_delete_resource "$1" "servers" "$2"
    }
    function scw_delete_volume() {
    scw_delete_resource "$1" "volumes" "$2"
    }
    function scw_delete_ip() {
    scw_delete_resource "$1" "ips" "$2"
    }
    function scw_delete_security_group() {
    scw_delete_resource "$1" "security_groups" "$2"
    }

    #
    # controllers
    #

    function scw_remove_servers() {
    servers=$(scw_servers "$1")
    for id in $(echo "$servers" | jq -r ".servers[].id"); do
    state=$(echo "$servers" | jq -r ".servers[] | select(.id==\"$id\") | .state")
    if [[ "$state" == "stopped" ]]; then
    scw_delete_server "$1" "$id"
    else
    scw_terminate_server "$1" "$id"
    fi
    done
    }

    function scw_remove_volumes() {
    volumes=$(scw_volumes "$1")
    for id in $(echo "$volumes" | jq -r ".volumes[].id"); do
    scw_delete_volume "$1" "$id"
    done
    }

    function scw_remove_ips() {
    ips=$(scw_ips "$1")
    for id in $(echo "$ips" | jq -r ".ips[].id"); do
    scw_delete_ip "$1" "$id"
    done
    }

    function scw_remove_security_groups() {
    security_groups=$(scw_security_groups "$1")
    for id in $(echo "$security_groups" | jq -r ".security_groups[].id"); do
    is_default=$(echo "$security_groups" | jq -r ".security_groups[] | select(.id==\"$id\") | .organization_default")
    if [[ "$is_default" != "true" ]]; then
    scw_delete_security_group "$1" "$id"
    fi
    done
    }

    #
    # misc
    #

    function scw_dashboard() {
    curl -s \
    -H "x-auth-token: $1" \
    -H "content-type: application/json" \
    "$api_endpoint/dashboard"
    }

    #
    # main
    #

    token=$(scw_login)

    api_endpoints=(https://cp-ams1.scaleway.com https://cp-par1.scaleway.com)
    for api_endpoint in "${api_endpoints[@]}"; do
    while [[ $(scw_dashboard "$token" | jq -r ".dashboard.servers_count") != "0" ]]; do
    scw_remove_servers "$token"
    done

    while [[ $(scw_dashboard "$token" | jq -r ".dashboard.volumes_count") != "0" ]]; do
    scw_remove_volumes "$token"
    done

    while [[ $(scw_dashboard "$token" | jq -r ".dashboard.ips_count") != "0" ]]; do
    scw_remove_ips "$token"
    done

    scw_remove_security_groups "$token"
    done

    scw_logout "$token"