Skip to content

Instantly share code, notes, and snippets.

@johnnyasantoss
Created March 17, 2017 21:02
Show Gist options
  • Save johnnyasantoss/7d36ebcd59db3afdade528d5d1549a69 to your computer and use it in GitHub Desktop.
Save johnnyasantoss/7d36ebcd59db3afdade528d5d1549a69 to your computer and use it in GitHub Desktop.
Deploy usando cake build
#addin "Cake.IIS"
#addin "Cake.Http"
#addin "Cake.Services"
using System;
string servidor = null;
var servidores = Argument<string>("AppServers", string.Empty).Split(',');
var appPoolOrService = Argument<string>("AppPoolOrService", string.Empty);
var isService = Argument<bool>("WindowsService", false);
string haProxyServer = null;
var haProxyUrl = Argument<string>("HaProxyUrl", "https://www.mydomain.com.br/haproxy?stats");
var haProxyServers = Argument<string>("HaProxyServers", string.Empty).Split(',');
var haProxyAction = Argument<string>("HaProxyAction", string.Empty);
var haProxyBackend = Argument<string>("HaProxyBackend", string.Empty);
var usuario = Argument<string>("User", string.Empty);
var senha = Argument<string>("Password", string.Empty);
string urlTest = null;
var urlsTest = Argument<string>("UrlsTest", string.Empty).Split(',');
var delay = Argument<int>("Delay", 10) * 1000;
string destination = null;
var source = Argument<string>("Source", string.Empty);
var destinations = Argument<string>("Destinations", string.Empty).Split(',');
Task("AppStop")
.Description("Para a ApplicationPool especificada pelo {appPoolOrService} no servidores do {IISServers}")
.Does(() =>
{
if(string.IsNullOrWhiteSpace(appPoolOrService))
throw new ArgumentNullException("appPoolOrService");
if(isService){
Information("Desligando serviço {0} - {1}...", servidor, appPoolOrService);
StopService(appPoolOrService, servidor);
} else {
Information("Desligando Application Pool {0} - {1}...", servidor, appPoolOrService);
StopPool(servidor, appPoolOrService);
}
});
Task("AppStart")
.Description("Inicia a ApplicationPool especificada pelo {AppPoolOrService} no servidores do {IISServers}")
.Does(() =>
{
if(string.IsNullOrWhiteSpace(appPoolOrService))
throw new ArgumentNullException("appPoolOrService");
if(isService){
Information("Ligando serviço {0} - {1}...", servidor, appPoolOrService);
StartService(appPoolOrService, servidor);
} else {
Information("Ligando Application Pool {0} - {1}...", servidor, appPoolOrService);
StartPool(servidor, appPoolOrService);
}
});
Task("TestServer")
.Description("Testa se um servidor está online fazendo um ping no mesmo.")
.Does(() =>
{
var uri = new Uri(urlTest).ToString();
Information("Testando servidor com a url \"{0}\"...", uri);
HttpGet(uri, new HttpSettings {
EnsureSuccessStatusCode = true
});
})
.OnError(e => {
if(e is AggregateException)
throw e.InnerException;
throw e;
});
Task("CopyFiles")
.Description("Copia o código fonte em um destino.")
.Does(() =>
{
if(string.IsNullOrWhiteSpace(source))
throw new ArgumentNullException("source");
if(string.IsNullOrWhiteSpace(destination))
throw new ArgumentNullException("destination");
Information("Copiando arquivos de \"{0}\" para \"{1}\"", source, destination);
CopyFiles(source, destination, true);
});
Task("HAProxyPost")
.Description("Executa uma ação no HAProxy.")
.Does(() =>
{
if(string.IsNullOrWhiteSpace(haProxyUrl))
throw new ArgumentNullException("haProxyUrl");
if(string.IsNullOrWhiteSpace(usuario))
throw new ArgumentNullException("usuario");
if(string.IsNullOrWhiteSpace(senha))
throw new ArgumentNullException("senha");
if(string.IsNullOrWhiteSpace(haProxyServer))
throw new ArgumentNullException("haProxyServer");
if(string.IsNullOrWhiteSpace(haProxyAction))
throw new ArgumentNullException("haProxyAction");
if(string.IsNullOrWhiteSpace(haProxyBackend))
throw new ArgumentNullException("haProxyBackend");
Information("Executando ação \"{0}\" no servidor {1} - {2}...", haProxyAction, haProxyServer, haProxyBackend);
HttpPost(haProxyUrl, settings => {
var formData = new Dictionary<string, string> {
{ "s", haProxyServer },
{ "action", haProxyAction },
{ "b", haProxyBackend }
};
settings.SetNoCache()
.UseBasicAuthorization(usuario, senha)
.SetFormUrlEncodedRequestBody(formData);
});
});
Task("Delay")
.Description("Espera por um tempo determinado.")
.Does(() =>
{
Information("Esperando por {0} segundos...", delay / 1000);
System.Threading.Thread.Sleep(delay);
});
Task("Deploy")
.Description("Faz o processo completo de envio.")
.Does(() =>
{
var iteracoes = servidores.Length;
for(int i = 0; i < iteracoes; i++){
haProxyServer = haProxyServers[i];
servidor = servidores[i];
urlTest = urlsTest[i];
destination = destinations[i];
//Deixa um servidor do balance em drain para parar de vir requisições
haProxyAction = "drain";
RunTarget("HAProxyPost");
//Espera um tempo até as requisições terminarem
RunTarget("Delay");
//Para a aplication pool
RunTarget("AppStop");
//Desliga o balance para este servidor
haProxyAction = "hdown";
RunTarget("HAProxyPost");
//Espera até a pool parar de vez
RunTarget("Delay");
//Copia os arquivos para o servidor
RunTarget("CopyFiles");
//Para a aplication pool
RunTarget("AppStart");
//Espera mais um pouco
RunTarget("Delay");
//Liga o balance
haProxyAction = "ready";
RunTarget("HAProxyPost");
//Espera mais ainda para o HAProxy subir ~1.5min
var oldDelay = delay;
delay = 90000;
RunTarget("Delay");
delay = oldDelay;
//Testa a conexão com o servidor específico
RunTarget("TestServer");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment