From d4b45739f14ecee22fc081dd146efccea0d58970 Mon Sep 17 00:00:00 2001 From: william mantly Date: Fri, 24 Jun 2016 00:18:50 -0400 Subject: [PATCH] tisk tisk --- nano.save | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++ routes/api.js | 39 +++++++++++---- 2 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 nano.save diff --git a/nano.save b/nano.save new file mode 100644 index 0000000..3059990 --- /dev/null +++ b/nano.save @@ -0,0 +1,136 @@ +Color_Off='\e[0m' # Text Reset + +# Regular Colors +Black='\e[0;30m' # Black +Red='\e[0;31m' # Red +Green='\e[0;32m' # Green +Yellow='\e[0;33m' # Yellow +Blue='\e[0;34m' # Blue +Purple='\e[0;35m' # Purple +Cyan='\e[0;36m' # Cyan +White='\e[0;37m' # White + +# Bold +BBlack='\e[1;30m' # Black +BRed='\e[1;31m' # Red +BGreen='\e[1;32m' # Green +BYellow='\e[1;33m' # Yellow +BBlue='\e[1;34m' # Blue +BPurple='\e[1;35m' # Purple +BCyan='\e[1;36m' # Cyan +BWhite='\e[1;37m' # White + +# Underline +UBlack='\e[4;30m' # Black +URed='\e[4;31m' # Red +UGreen='\e[4;32m' # Green +UYellow='\e[4;33m' # Yellow +UBlue='\e[4;34m' # Blue +UPurple='\e[4;35m' # Purple +UCyan='\e[4;36m' # Cyan +UWhite='\e[4;37m' # White + +# Background +On_Black='\e[40m' # Black +On_Red='\e[41m' # Red +On_Green='\e[42m' # Green +On_Yellow='\e[43m' # Yellow +On_Blue='\e[44m' # Blue +On_Purple='\e[45m' # Purple +On_Cyan='\e[46m' # Cyan +On_White='\e[47m' # White + +# High Intensity +IBlack='\e[0;90m' # Black +IRed='\e[0;91m' # Red +IGreen='\e[0;92m' # Green +IYellow='\e[0;93m' # Yellow +IBlue='\e[0;94m' # Blue +IPurple='\e[0;95m' # Purple +ICyan='\e[0;96m' # Cyan +IWhite='\e[0;97m' # White + +# Bold High Intensity +BIBlack='\e[1;90m' # Black +BIRed='\e[1;91m' # Red +BIGreen='\e[1;92m' # Green +BIYellow='\e[1;93m' # Yellow +BIBlue='\e[1;94m' # Blue +BIPurple='\e[1;95m' # Purple +BICyan='\e[1;96m' # Cyan +BIWhite='\e[1;97m' # White + +# High Intensity backgrounds +On_IBlack='\e[0;100m' # Black +On_IRed='\e[0;101m' # Red +On_IGreen='\e[0;102m' # Green +On_IYellow='\e[0;103m' # Yellow +On_IBlue='\e[0;104m' # Blue +On_IPurple='\e[0;105m' # Purple +On_ICyan='\e[0;106m' # Cyan +On_IWhite='\e[0;107m' # White + + +prompt_git() { + local s=''; + local branchName=''; + + # Check if the current directory is in a Git repository. + if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then + + # check if the current directory is in .git before running git checks + if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then + + # Ensure the index is up to date. + git update-index --really-refresh -q &>/dev/null; + + # Check for uncommitted changes in the index. + if ! $(git diff --quiet --ignore-submodules --cached); then + s+='+'; + fi; + + # Check for unstaged changes. + if ! $(git diff-files --quiet --ignore-submodules --); then + s+="${On_Red}!${Color_Off}"; + fi; + + # Check for untracked files. + if [ -n "$(git ls-files --others --exclude-standard)" ]; then + s+="${On_Yellow}?"; + fi; + + # Check for stashed files. + if $(git rev-parse --verify refs/stash &>/dev/null); then + s+="${On_Green}\$"; + fi; + + fi; + + # Get the short symbolic ref. + # If HEAD isn’t a symbolic ref, get the short SHA for the latest commit + # Otherwise, just give up. + + branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \ + git rev-parse --short HEAD 2> /dev/null || \ + echo '(unknown)')"; + + # [ -n "${s}" ] && s="${BBlue}[${s}${Color_Off}${BBlue}]"; + + echo -e "${1}${branchName}${Color_Off}${s}${Color_Off}"; + else + return; + fi; +} + +export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting + +# PS1='\[\033[01;35m\]\H\[\033[00m\]:\[\033[01;34m\]\w $(prompt_git) \[\033[01;36m\]\d \n' +PS1="\[${BPurple}\]\H\[${BWhite}\]:\[${BBlue}\]\w\$(prompt_git \"\[${BWhite}\] on \[${BPurple}\]\") \[${BCyan}\]\d \n" +PS1+="\[${BYellow}\]\@ \[${BGreen}\]\u\[${Color_Off}\]$ " + +export PS1; + +[[ $EUID == 0 ]] && PS1='\[\033[01;35m\]\H \[\033[00m\]: \[\033[01;34m\]\w \[\033[01;33m\] \t \[\033[01;36m\]\d\n!\[\033[01;31m\]\u \[\033[00m\]# ' + +alias open='xdg-open &>/dev/null' +alias c9ide='/home/william/dev/c9sdk/server.js' diff --git a/routes/api.js b/routes/api.js index 8ae4346..129336f 100644 --- a/routes/api.js +++ b/routes/api.js @@ -12,7 +12,6 @@ var workerSnapID = 'V1' // console.log = function(){}; var label2runner = {}; -var workers = []; var isCheckingWorkers = false; var workers = (function(){ @@ -28,15 +27,15 @@ var workers = (function(){ return doapi.dropletInfo(id, function(data){ var worker = JSON.parse(data)['droplet']; if(worker.status == 'active'){ - console.log('Droplet is now active, starting runners in 20 seconds'); + // console.log('Droplet is now active, starting runners in 20 seconds'); return setTimeout(function(worker){ - console.log('Ready to start runners!'); + // console.log('Ready to start runners!'); workers.startRunners(workers.makeWorkerObj(worker), true); workers.currentCreating--; }, 20000, worker); }else{ - console.log('Worker not ready, check again in ', time, 'MS'); + // console.log('Worker not ready, check again in ', time, 'MS'); return setTimeout(function(){ workers.checkDroplet(id); @@ -63,7 +62,7 @@ var workers = (function(){ workers.destroy = function(worker){ var worker = worker || workers.pop(); - return doapi.dropletDestroy(worker.id, function(){}); + return doapi.dropletDestroy(worker.id, console.log); }; workers.makeWorkerObj = function(worker){ @@ -76,7 +75,7 @@ var workers = (function(){ worker.index = workers.length, worker.getRunner = function(){ if(this.availrunners.length === 0) return false; - console.log('getting runner from ', worker.name, ' avail length ', this.availrunners.length); + // console.log('getting runner from ', worker.name, ' avail length ', this.availrunners.length); var runner = this.availrunners.pop(); this.usedrunner++; label2runner[runner.label] = runner; @@ -87,10 +86,20 @@ var workers = (function(){ return worker; }; + workers.__workersId = function(argument){ + return workers.map(function(item){ + return item.id; + }); + + }; + workers.destroyOld = function(){ + var currentIDs = workers.__workersId() doapi.dropletsByTag('clworker', function(data){ data = JSON.parse(data); data['droplets'].forEach(function(worker){ + if(worker.id in currentIDs) return false; + console.log('found old droplet, killing it'); doapi.dropletDestroy(worker.id, function(){}); }); @@ -105,7 +114,7 @@ var workers = (function(){ var name = 'crunner-'+(Math.random()*100).toString().slice(-4); return lxc.startEphemeral(name, 'crunner0', worker.ip, function(data){ if(!data.ip) return setTimeout(workers.startRunners(worker, newWorker),0); - console.log('started runner on', worker.name) + // console.log('started runner on', worker.name) if(newWorker) worker = workers[workers.push(worker)-1] worker.availrunners.push({ @@ -201,7 +210,7 @@ var getAvailrunner = function(runner){ var run = function(req, res, runner, count){ count = count || 0; - console.log('run start', count, runner); + console.log('run start', count); if(!runner){ console.log('no runner'); @@ -209,6 +218,12 @@ var run = function(req, res, runner, count){ return res.json({error: 'No runners, try again soon.'}); } + if(count > 3){ + console.log('to many reties on runner'); + res.status(400); + return res.json({error: 'Runner restarted to many times'}); + } + var httpOptions = { url: 'http://' + runner.worker.ip, headers: { @@ -235,9 +250,13 @@ var run = function(req, res, runner, count){ }; setTimeout(function(){ - console.log('Starting balance checking in 30 seconds') + // console.log('Starting balance checking in 30 seconds') setInterval(workers.checkBalance, 15000); -}, 180000); +}, 180000); + +setInterval(function(argument) { + workers.destroyOld(); +}, 3600000); // 1 hour workers.destroyOld(); workers.checkBalance();