| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
local packer = import "packer.libsonnet"; |
| 18 |
|
| 19 |
{ |
| 20 |
local cassandra = self, |
| 21 |
|
| 22 |
// The default Cassandra configuration. Override what you want to change. |
| 23 |
conf:: { |
| 24 |
authenticator: "AllowAllAuthenticator", |
| 25 |
authorizer: "AllowAllAuthorizer", |
| 26 |
auto_snapshot: true, |
| 27 |
batch_size_warn_threshold_in_kb: 5, |
| 28 |
batchlog_replay_throttle_in_kb: 1024, |
| 29 |
cas_contention_timeout_in_ms: 1000, |
| 30 |
client_encryption_options: { |
| 31 |
enabled: false, |
| 32 |
keystore: "conf/.keystore", |
| 33 |
keystore_password: "cassandra", |
| 34 |
}, |
| 35 |
cluster_name: "Unnamed Cluster", |
| 36 |
column_index_size_in_kb: 64, |
| 37 |
commit_failure_policy: "stop", |
| 38 |
commitlog_directory: "/var/lib/cassandra/commitlog", |
| 39 |
commitlog_segment_size_in_mb: 32, |
| 40 |
commitlog_sync: "periodic", |
| 41 |
commitlog_sync_period_in_ms: 10000, |
| 42 |
compaction_throughput_mb_per_sec: 16, |
| 43 |
concurrent_counter_writes: 32, |
| 44 |
concurrent_reads: 32, |
| 45 |
concurrent_writes: 32, |
| 46 |
counter_cache_save_period: 7200, |
| 47 |
counter_cache_size_in_mb: null, |
| 48 |
counter_write_request_timeout_in_ms: 5000, |
| 49 |
cross_node_timeout: false, |
| 50 |
data_file_directories: ["/var/lib/cassandra/data"], |
| 51 |
disk_failure_policy: "stop", |
| 52 |
dynamic_snitch_badness_threshold: 0.1, |
| 53 |
dynamic_snitch_reset_interval_in_ms: 600000, |
| 54 |
dynamic_snitch_update_interval_in_ms: 100, |
| 55 |
endpoint_snitch: "SimpleSnitch", |
| 56 |
hinted_handoff_enabled: true, |
| 57 |
hinted_handoff_throttle_in_kb: 1024, |
| 58 |
incremental_backups: false, |
| 59 |
index_summary_capacity_in_mb: null, |
| 60 |
index_summary_resize_interval_in_minutes: 60, |
| 61 |
inter_dc_tcp_nodelay: false, |
| 62 |
internode_compression: "all", |
| 63 |
key_cache_save_period: 14400, |
| 64 |
key_cache_size_in_mb: null, |
| 65 |
listen_address: "localhost", |
| 66 |
max_hint_window_in_ms: 10800000, |
| 67 |
max_hints_delivery_threads: 2, |
| 68 |
memtable_allocation_type: "heap_buffers", |
| 69 |
native_transport_port: 9042, |
| 70 |
num_tokens: 256, |
| 71 |
partitioner: "org.apache.cassandra.dht.Murmur3Partitioner", |
| 72 |
permissions_validity_in_ms: 2000, |
| 73 |
range_request_timeout_in_ms: 10000, |
| 74 |
read_request_timeout_in_ms: 5000, |
| 75 |
request_scheduler: "org.apache.cassandra.scheduler.NoScheduler", |
| 76 |
request_timeout_in_ms: 10000, |
| 77 |
row_cache_save_period: 0, |
| 78 |
row_cache_size_in_mb: 0, |
| 79 |
rpc_address: "localhost", |
| 80 |
rpc_keepalive: true, |
| 81 |
rpc_port: 9160, |
| 82 |
rpc_server_type: "sync", |
| 83 |
saved_caches_directory: "/var/lib/cassandra/saved_caches", |
| 84 |
seed_provider: [ |
| 85 |
{ |
| 86 |
class_name: "org.apache.cassandra.locator.SimpleSeedProvider", |
| 87 |
parameters: [{ seeds: "127.0.0.1" }], |
| 88 |
}, |
| 89 |
], |
| 90 |
server_encryption_options: { |
| 91 |
internode_encryption: "none", |
| 92 |
keystore: "conf/.keystore", |
| 93 |
keystore_password: "cassandra", |
| 94 |
truststore: "conf/.truststore", |
| 95 |
truststore_password: "cassandra", |
| 96 |
}, |
| 97 |
snapshot_before_compaction: false, |
| 98 |
ssl_storage_port: 7001, |
| 99 |
sstable_preemptive_open_interval_in_mb: 50, |
| 100 |
start_native_transport: true, |
| 101 |
start_rpc: true, |
| 102 |
storage_port: 7000, |
| 103 |
thrift_framed_transport_size_in_mb: 15, |
| 104 |
tombstone_failure_threshold: 100000, |
| 105 |
tombstone_warn_threshold: 1000, |
| 106 |
trickle_fsync: false, |
| 107 |
trickle_fsync_interval_in_kb: 10240, |
| 108 |
truncate_request_timeout_in_ms: 60000, |
| 109 |
write_request_timeout_in_ms: 2000, |
| 110 |
}, |
| 111 |
|
| 112 |
// Some firewall resources to open up Cassandra ports. |
| 113 |
GcpFirewall:: { |
| 114 |
cassandraTag:: "cassandra-server", |
| 115 |
source_ranges: ["0.0.0.0/0"], |
| 116 |
network: error "cassandra.GcpFirewall must have field: network", |
| 117 |
allow: { protocol: "tcp", ports: ["9042", "9160"] }, |
| 118 |
// From the Internet to these ports. |
| 119 |
target_tags: [self.cassandraTag], |
| 120 |
}, |
| 121 |
GcpFirewallGossip:: { |
| 122 |
cassandraTag:: "cassandra-server", |
| 123 |
source_ranges: ["0.0.0.0/0"], |
| 124 |
network: error "cassandra.GcpFirewallGossip must have field: network", |
| 125 |
allow: { protocol: "tcp", ports: ["7000", "7001", "7199"] }, |
| 126 |
// From these machines amongst themselves. |
| 127 |
source_tags: [self.cassandraTag], |
| 128 |
target_tags: [self.cassandraTag], |
| 129 |
}, |
| 130 |
|
| 131 |
// Sets the root password to something, while the server is listening only on localhost. |
| 132 |
GcpDebianImage: packer.GcpDebianImage { |
| 133 |
local image = self, |
| 134 |
|
| 135 |
rootPassword:: error "GcpDebianCassandraPrimedImage: must have field: rootPassword", |
| 136 |
clusterName:: error "GcpDebianCassandraPrimedImage: must have field: clusterName", |
| 137 |
|
| 138 |
aptKeyUrls+: ["https://www.apache.org/dist/cassandra/KEYS"], |
| 139 |
aptRepoLines+: { |
| 140 |
cassandra: "deb https://www.apache.org/dist/cassandra/debian 21x main", |
| 141 |
}, |
| 142 |
aptPackages+: ["cassandra"], |
| 143 |
|
| 144 |
conf:: cassandra.conf { |
| 145 |
authenticator: "PasswordAuthenticator", |
| 146 |
cluster_name: image.clusterName, |
| 147 |
}, |
| 148 |
|
| 149 |
provisioners+: [ |
| 150 |
packer.RootShell { |
| 151 |
inline: [ |
| 152 |
// Shut it down |
| 153 |
"/etc/init.d/cassandra stop", |
| 154 |
// Remove junk from unconfigured startup |
| 155 |
"rm -rfv /var/lib/cassandra/*", |
| 156 |
// Enable authentication |
| 157 |
local dest = "/etc/cassandra/cassandra.yaml"; |
| 158 |
"echo %s > %s" % [std.escapeStringBash("" + image.conf), dest], |
| 159 |
// Start it up again |
| 160 |
"/etc/init.d/cassandra start", |
| 161 |
// Wait for it to be ready |
| 162 |
cassandra.waitForCqlsh("cassandra", "cassandra", "localhost"), |
| 163 |
// Set root password |
| 164 |
local cql = "ALTER USER cassandra WITH PASSWORD '%s';" % image.rootPassword; |
| 165 |
"echo %s | cqlsh -u cassandra -p cassandra" % std.escapeStringBash(cql), |
| 166 |
], |
| 167 |
}, |
| 168 |
], |
| 169 |
}, |
| 170 |
|
| 171 |
// A line of bash that will wait for a Cassandra service to be "up". |
| 172 |
waitForCqlsh(user, pass, host):: |
| 173 |
"while ! echo show version | cqlsh -u %s -p %s %s ; do sleep 1; done" % [user, pass, host], |
| 174 |
|
| 175 |
// A line of bash that will wait for the given port to accept connections. |
| 176 |
waitForSeed(host, port):: |
| 177 |
"while ! nc %s %d < /dev/null; do sleep 1; done" % [host, port], |
| 178 |
|
| 179 |
// This mixin is intended to be used on a Google Cloud Platform Instance based on the above |
| 180 |
// GcpDebianImage. It adds commands to the startup script that bootstrap Cassandra using a |
| 181 |
// given CQL script and configuration. |
| 182 |
GcpStarterMixin: { |
| 183 |
|
| 184 |
startup_script+: [ |
| 185 |
// Wait for the misconfigured cassandra to start up. |
| 186 |
cassandra.waitForCqlsh("cassandra", self.rootPass, "localhost"), |
| 187 |
|
| 188 |
// Set up system_auth replication level |
| 189 |
"echo %s | cqlsh -u cassandra -p %s localhost" |
| 190 |
% [std.escapeStringBash("ALTER KEYSPACE system_auth WITH REPLICATION = %s;" |
| 191 |
% self.authReplication), |
| 192 |
self.rootPass], |
| 193 |
|
| 194 |
// Drop in the correct configuration. |
| 195 |
"echo %s > %s" |
| 196 |
% [std.escapeStringBash("" + self.conf), "/etc/cassandra/cassandra.yaml"], |
| 197 |
|
| 198 |
// Restart with new configuration. |
| 199 |
"/etc/init.d/cassandra restart", |
| 200 |
|
| 201 |
// Wait for the properly configured cassandra to start up and reach quorum. |
| 202 |
cassandra.waitForCqlsh("cassandra", self.rootPass, "$HOSTNAME"), |
| 203 |
|
| 204 |
// Set up users, empty tables, etc. |
| 205 |
"echo %s | cqlsh -u cassandra -p %s $HOSTNAME" |
| 206 |
% [std.escapeStringBash(std.lines(self.initCql)), self.rootPass], |
| 207 |
|
| 208 |
], |
| 209 |
}, |
| 210 |
|
| 211 |
// This mixin is intended to be used on a Google Cloud Platform Instance based on the above |
| 212 |
// GcpDebianImage. It adds commands to the startup script that bootstrap Cassandra by wiping |
| 213 |
// its slate clean and allowing it to join an existing cluster. |
| 214 |
GcpTopUpMixin: { |
| 215 |
startup_script+: [ |
| 216 |
// Wait for the misconfigured cassandra to start up. |
| 217 |
cassandra.waitForCqlsh("cassandra", self.rootPass, "localhost"), |
| 218 |
|
| 219 |
// Kill it. |
| 220 |
"/etc/init.d/cassandra stop", |
| 221 |
|
| 222 |
// Clean up the mess it caused due to being misconfigured. |
| 223 |
"rm -rf /var/lib/cassandra/*", |
| 224 |
|
| 225 |
// Drop in the correct configuration. |
| 226 |
"echo %s > %s" |
| 227 |
% [std.escapeStringBash("" + self.conf), "/etc/cassandra/cassandra.yaml"], |
| 228 |
|
| 229 |
// Start it up again. |
| 230 |
"/etc/init.d/cassandra start", |
| 231 |
], |
| 232 |
}, |
| 233 |
|
| 234 |
} |