Skip to content

Instantly share code, notes, and snippets.

@zwetan
Last active January 31, 2018 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwetan/297813e2716fbfcd84babda42900a99a to your computer and use it in GitHub Desktop.
Save zwetan/297813e2716fbfcd84babda42900a99a to your computer and use it in GitHub Desktop.
Install as3shebang on Cloud Shell
#!/bin/bash -e
# functions
check_tool() {
command -v "$1" >/dev/null 2>&1 || (echo "You need to install '$1'" && exit 2)
}
find_tool() {
local result=`command -v "$1"`;
if [ -n "$result" ]; then
return 0;
fi
return 1;
}
find_file() {
local file="$1";
if [ -f $file ]; then
return 0;
fi
return 1;
}
detect_platform() {
local PLATFORM="";
case "$(uname -s)" in
Darwin)
PLATFORM="macintosh"
;;
Linux)
PLATFORM="linux"
;;
CYGWIN*)
PLATFORM="windows"
;;
*)
PLATFORM="unknown"
;;
esac
echo "${PLATFORM}";
}
current_dir() {
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
}
get_timer() {
echo `date +%s`;
}
time_diff() {
local start=$1;
local end=$2;
local secs=$( echo "$end - $start" | bc -l );
echo $( printf '%02dh:%02dm:%02ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60)) );
}
# --- main ---
# check platform
PLATFORM=`detect_platform`;
if [[ "$PLATFORM" == "unknown" ]]; then
echo "Could not detect the platform";
exit 1;
fi
if [[ "$PLATFORM" != "linux" ]]; then
echo "You need to run the setup script under a 'linux' operating system";
exit 1;
fi
# options
AS3SHEBANG_DEB="as3shebang_1.0.0_amd64.deb";
AS3SHEBANG_URL="https://github.com/Corsaair/as3shebang/releases/download/1.0.0/${AS3SHEBANG_DEB}";
NO_WARNING="~/.cloudshell/no-apt-get-warning";
# colors
RED="\e[1;31m"
YELLOW="\e[1;33m"
PURPLE="\e[1;35m"
NC="\e[0m"
setup_start=$(get_timer);
echo -e "${PURPLE}Running as3shebang setup...${NC}";
echo -e "${RED}Installing dependencies${NC}";
if ! find_file ${NO_WARNING}; then
if [ ! -d ".cloudshell/" ]; then
mkdir .cloudshell/;
fi
touch .cloudshell/no-apt-get-warning;
fi
if ! find_tool bc; then
sudo apt-get -qq -y install bc;
fi
if ! find_tool lsb_release; then
sudo apt-get -qq -y install lsb-release;
fi
PRETTY_NAME=`lsb_release -ds`;
if ! find_tool as3shebang; then
if ! find_file ${AS3SHEBANG_DEB}; then
echo -e "${PURPLE}Downloading as3shebang${NC}";
wget -q --show-progress ${AS3SHEBANG_URL} -O ${AS3SHEBANG_DEB};
fi
echo -e "${RED}Installing as3shebang${NC}";
sudo dpkg -i ${AS3SHEBANG_DEB};
fi
if [ ! -f "sysinfo" ]; then
echo -e "${RED}Creating 'sysinfo' script${NC}";
cat << 'EOF' > sysinfo
#!/usr/bin/as3shebang
import shell.Runtime;
import shell.OperatingSystem;
trace( "Welcome to Redtamarin v" + Runtime.version );
trace( "" );
trace( "Runtime:" );
trace( " |_ api : " + Runtime.api );
trace( " |_ apiVersion : " + Runtime.apiVersion );
trace( " |_ architecture : " + Runtime.architecture );
trace( " |_ AVMversion : " + Runtime.AVMversion );
trace( " |_ codename : " + Runtime.codename );
trace( " |_ description : " + Runtime.description );
trace( " |_ endian : " + Runtime.endian );
trace( " |_ label : " + Runtime.label );
trace( " |_ platform : " + Runtime.platform );
trace( " |_ redtamarin : " + Runtime.redtamarin );
trace( " |_ runmode : " + Runtime.runmode );
trace( " |_ swfVersion : " + Runtime.swfVersion );
trace( " |_ tag : " + Runtime.tag );
trace( " |_ version : " + Runtime.version );
trace( " |_ version" );
trace( " | |_ major : " + Runtime.version.major );
trace( " | |_ minor : " + Runtime.version.minor );
trace( " | |_ patch : " + Runtime.version.patch );
trace( " | |_ serie : " + Runtime.version.serie );
trace( " | |_ cycle : " + Runtime.version.cycle );
trace( " | |_ build : " + Runtime.version.build );
trace( " |_ features : " );
trace( " |_ " + Runtime.features.substr(0, Runtime.features.length-1).split( ";" ).join( "\n |_ " ) );
trace( "" );
trace( "Operating System:" );
trace( " |_ codename: " + OperatingSystem.codename );
trace( " |_ hostname: " + OperatingSystem.hostname );
trace( " |_ longDescription: " + OperatingSystem.longDescription );
trace( " |_ name: " + OperatingSystem.name );
trace( " |_ username: " + OperatingSystem.username );
trace( " |_ vendor: " + OperatingSystem.vendor );
trace( " |_ vendorBuild: " + OperatingSystem.vendorBuild );
trace( " |_ vendorDescription: " + OperatingSystem.vendorDescription );
trace( " |_ vendorName: " + OperatingSystem.vendorName );
trace( " |_ vendorVersion: " + OperatingSystem.vendorVersion );
trace( " |_ version: " + OperatingSystem.version );
trace( "" );
EOF
chmod +x sysinfo;
fi
if [ ! -f "uninstall" ]; then
echo -e "${RED}Creating 'uninstall' script${NC}";
cat << EOF > uninstall
#!/bin/bash
echo -e "${PURPLE}Uninstall running ...${NC}";
sudo rm sysinfo;
sudo rm ${AS3SHEBANG_DEB};
sudo rm /usr/bin/as3shebang;
sudo apt-get -qq -y remove lsb-release;
sudo apt-get -qq -y remove bc;
sudo rm -Rf ~/.cloudshell/;
sudo rm uninstall;
echo -e "${RED}Removed as3shebang and all dependencies${NC}";
echo "";
EOF
chmod +x uninstall;
fi
setup_end=$(get_timer);
setup_total=$(time_diff $setup_start $setup_end)
echo "";
echo -e "${RED}It took ${YELLOW}$setup_total${NC}${RED} to install as3shebang on ${PURPLE}${PRETTY_NAME}${NC}${NC}";
echo -e "${RED}Try to run the 'sysinfo' script with './sysinfo'${NC}";
echo "";
echo -e "${RED}If you want to revert everything and uninstall as3shebang${NC}";
echo -e "${RED}Run the 'uninstall' script with './uninstall'${NC}";
echo -e "${RED}(you will have to delete 'setup.sh' manually)${NC}";
echo "";
@zwetan
Copy link
Author

zwetan commented Jan 31, 2018

If you need to test as3shebang quickly and you have a google mail / gmail account

go to this URL
https://console.cloud.google.com/cloudshell/editor?pli=1&shellonly=true

and run this command
$ wget -q --show-progress https://gist.githubusercontent.com/zwetan/297813e2716fbfcd84babda42900a99a/raw/5698dce8c38468d78c5b79e3c558ce09b659561f/setup.sh -O setup.sh && chmod +x setup.sh && ./setup.sh

do not type the $, this is a convention to indicate you're typing a shell command

the setup output something like that
cloudshell_setup

if you run $ ./sysinfo it will output something like that
cloudshell_sysinfo

if you want to uninstall everything run $ ./uninstall and then $ rm setup.sh
cloudshell_uninstall

If you don't uninstall, the setup.sh script and the downloaded files will be kept around in your $HOME directory
thanks to Google Cloud Shell 5GB of Persistent Disk Storage

you can even automate this setup by editing your ~/.profile and adding those lines at the very end

if [ -f "$HOME/setup.sh" ] ; then
    $HOME/setup.sh
fi

that way, despite the limitations, each time you will run your Cloud Shell, as3shebang will be available to use :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment