-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JCES-273: Add load balancers to Data Center
- Loading branch information
Showing
6 changed files
with
130 additions
and
35 deletions.
There are no files selected for viewing
7 changes: 4 additions & 3 deletions
7
src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/Infrastructure.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package com.atlassian.performance.tools.infrastructure.api | ||
|
||
import com.atlassian.performance.tools.infrastructure.api.jira.install.TcpHost | ||
import com.atlassian.performance.tools.infrastructure.api.jira.node.JiraNode | ||
import com.atlassian.performance.tools.infrastructure.api.jira.node.JiraNodePlan | ||
|
||
interface Infrastructure : AutoCloseable { | ||
fun serve(jiraNodePlans: List<JiraNodePlan>): List<JiraNode> | ||
|
||
/** | ||
* @return can be reached by the caller via [TcpHost.publicIp] and by the rest of the infra via [TcpHost.privateIp] | ||
*/ | ||
fun serve(port: Int, name: String): TcpHost | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...kotlin/com/atlassian/performance/tools/infrastructure/api/loadbalancer/ApacheProxyPlan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.atlassian.performance.tools.infrastructure.api.loadbalancer | ||
|
||
import com.atlassian.performance.tools.infrastructure.api.Infrastructure | ||
import com.atlassian.performance.tools.infrastructure.api.Sed | ||
import com.atlassian.performance.tools.infrastructure.api.jira.install.InstalledJira | ||
import com.atlassian.performance.tools.infrastructure.api.jira.node.JiraNode | ||
import com.atlassian.performance.tools.infrastructure.api.jira.report.Reports | ||
import com.atlassian.performance.tools.infrastructure.api.jira.start.hook.PreStartHook | ||
import com.atlassian.performance.tools.infrastructure.api.jira.start.hook.PreStartHooks | ||
import com.atlassian.performance.tools.infrastructure.api.os.Ubuntu | ||
import com.atlassian.performance.tools.jvmtasks.api.ExponentialBackoff | ||
import com.atlassian.performance.tools.jvmtasks.api.IdempotentAction | ||
import com.atlassian.performance.tools.ssh.api.SshConnection | ||
import java.net.URI | ||
import java.time.Duration | ||
|
||
class ApacheProxyPlan( | ||
private val httpPort: Int, | ||
private val infrastructure: Infrastructure | ||
) : LoadBalancerPlan { | ||
|
||
private val configPath = "/etc/apache2/sites-enabled/000-default.conf" | ||
|
||
override fun materialize(nodes: List<JiraNode>): LoadBalancer { | ||
val proxyNode = infrastructure.serve(httpPort, "apache-proxy") | ||
IdempotentAction("Installing and configuring apache load balancer") { | ||
proxyNode.ssh.newConnection().use { connection -> | ||
tryToProvision(connection, nodes) | ||
} | ||
}.retry(2, ExponentialBackoff(Duration.ofSeconds(5))) | ||
val balancerEndpoint = URI("http://${proxyNode.privateIp}:$httpPort/") | ||
nodes.forEach { it.plan.hooks.preStart.insert(InjectProxy(balancerEndpoint)) } | ||
return ApacheProxy(balancerEndpoint) | ||
} | ||
|
||
private fun tryToProvision(ssh: SshConnection, nodes: List<JiraNode>) { | ||
Ubuntu().install(ssh, listOf("apache2")) | ||
ssh.execute("sudo rm $configPath") | ||
ssh.execute("sudo touch $configPath") | ||
val mods = listOf( | ||
"proxy", "proxy_ajp", "proxy_http", "rewrite", "deflate", "headers", "proxy_balancer", "proxy_connect", | ||
"proxy_html", "xml2enc", "lbmethod_byrequests" | ||
) | ||
ssh.execute("sudo a2enmod ${mods.joinToString(" ")}") | ||
appendConfig( | ||
ssh, | ||
"Header add Set-Cookie \\\"ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/\\\" env=BALANCER_ROUTE_CHANGED" | ||
) | ||
appendConfig(ssh, "<Proxy balancer://mycluster>") | ||
nodes.forEachIndexed { index, node -> | ||
appendConfig(ssh, "\tBalancerMember http://${node.host.publicIp}:${node.host.port} route=$index") | ||
} | ||
appendConfig(ssh, "</Proxy>\n") | ||
appendConfig(ssh, "ProxyPass / balancer://mycluster/ stickysession=ROUTEID") | ||
appendConfig(ssh, "ProxyPassReverse / balancer://mycluster/ stickysession=ROUTEID") | ||
ssh.execute("sudo service apache2 restart", Duration.ofMinutes(3)) | ||
} | ||
|
||
private fun appendConfig(connection: SshConnection, line: String) { | ||
connection.execute("echo \"$line\" | sudo tee -a $configPath") | ||
} | ||
|
||
private class ApacheProxy( | ||
override val uri: URI | ||
) : LoadBalancer { | ||
override fun waitUntilHealthy(timeout: Duration) {} | ||
} | ||
|
||
private class InjectProxy( | ||
private val proxy: URI | ||
) : PreStartHook { | ||
override fun call(ssh: SshConnection, jira: InstalledJira, hooks: PreStartHooks, reports: Reports) { | ||
Sed().replace( | ||
ssh, | ||
"bindOnInit=\"false\"", | ||
"bindOnInit=\"false\" scheme=\"http\" proxyName=\"${proxy.host}\" proxyPort=\"${proxy.port}\"", | ||
"${jira.installation.path}/conf/server.xml" | ||
) | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...otlin/com/atlassian/performance/tools/infrastructure/api/loadbalancer/LoadBalancerPlan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.atlassian.performance.tools.infrastructure.api.loadbalancer | ||
|
||
import com.atlassian.performance.tools.infrastructure.api.jira.node.JiraNode | ||
|
||
interface LoadBalancerPlan { | ||
fun materialize(nodes: List<JiraNode>): LoadBalancer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters