blob: 5ba7e56f3e4420580d6cd1aacb09c08a6a295950 [file] [log] [blame]
Attila Fazekasece6a332012-11-29 14:19:41 +01001# lib/swift
Dean Troyer6d04fd72012-12-21 11:03:37 -06002# Functions to control the configuration and operation of the **Swift** service
Attila Fazekasece6a332012-11-29 14:19:41 +01003
4# Dependencies:
5# ``functions`` file
6# ``DEST``, ``SCREEN_NAME``, `SWIFT_HASH` must be defined
Attila Fazekas91b8d132013-01-06 22:40:09 +01007# ``STACK_USER`` must be defined
Attila Fazekasece6a332012-11-29 14:19:41 +01008# ``SWIFT_DATA_DIR`` or ``DATA_DIR`` must be defined
9# ``lib/keystone`` file
10# ``stack.sh`` calls the entry points in this order:
11#
12# install_swift
13# configure_swift
14# init_swift
15# start_swift
16# stop_swift
17# cleanup_swift
18
19# Save trace setting
20XTRACE=$(set +o | grep xtrace)
21set +o xtrace
22
23
24# Defaults
25# --------
26
Attila Fazekasece6a332012-11-29 14:19:41 +010027# Set up default directories
Attila Fazekasece6a332012-11-29 14:19:41 +010028SWIFT_DIR=$DEST/swift
29SWIFTCLIENT_DIR=$DEST/python-swiftclient
Dean Troyer64ab7742012-12-28 15:38:28 -060030SWIFT_AUTH_CACHE_DIR=${SWIFT_AUTH_CACHE_DIR:-/var/cache/swift}
Attila Fazekasece6a332012-11-29 14:19:41 +010031
32# TODO: add logging to different location.
33
34# Set ``SWIFT_DATA_DIR`` to the location of swift drives and objects.
35# Default is the common DevStack data directory.
36SWIFT_DATA_DIR=${SWIFT_DATA_DIR:-${DATA_DIR}/swift}
37
38# Set ``SWIFT_CONFIG_DIR`` to the location of the configuration files.
39# Default is ``/etc/swift``.
40SWIFT_CONFIG_DIR=${SWIFT_CONFIG_DIR:-/etc/swift}
41
42# DevStack will create a loop-back disk formatted as XFS to store the
Kevin Lydad66c9652013-01-09 13:39:57 +000043# swift data. Set ``SWIFT_LOOPBACK_DISK_SIZE`` to the disk size in
44# kilobytes.
Attila Fazekasece6a332012-11-29 14:19:41 +010045# Default is 1 gigabyte.
46SWIFT_LOOPBACK_DISK_SIZE=${SWIFT_LOOPBACK_DISK_SIZE:-1000000}
47
48# The ring uses a configurable number of bits from a path’s MD5 hash as
49# a partition index that designates a device. The number of bits kept
50# from the hash is known as the partition power, and 2 to the partition
51# power indicates the partition count. Partitioning the full MD5 hash
52# ring allows other parts of the cluster to work in batches of items at
53# once which ends up either more efficient or at least less complex than
54# working with each item separately or the entire cluster all at once.
55# By default we define 9 for the partition count (which mean 512).
56SWIFT_PARTITION_POWER_SIZE=${SWIFT_PARTITION_POWER_SIZE:-9}
57
58# Set ``SWIFT_REPLICAS`` to configure how many replicas are to be
59# configured for your Swift cluster. By default the three replicas would need a
60# bit of IO and Memory on a VM you may want to lower that to 1 if you want to do
61# only some quick testing.
62SWIFT_REPLICAS=${SWIFT_REPLICAS:-3}
63SWIFT_REPLICAS_SEQ=$(seq ${SWIFT_REPLICAS})
64
65# Set ``OBJECT_PORT_BASE``, ``CONTAINER_PORT_BASE``, ``ACCOUNT_PORT_BASE``
66# Port bases used in port number calclution for the service "nodes"
67# The specified port number will be used, the additinal ports calculated by
68# base_port + node_num * 10
69OBJECT_PORT_BASE=6010
70CONTAINER_PORT_BASE=6011
71ACCOUNT_PORT_BASE=6012
72
Dean Troyer6d04fd72012-12-21 11:03:37 -060073
Attila Fazekasece6a332012-11-29 14:19:41 +010074# Entry Points
75# ------------
76
77# cleanup_swift() - Remove residual data files
78function cleanup_swift() {
79 rm -f ${SWIFT_CONFIG_DIR}{*.builder,*.ring.gz,backups/*.builder,backups/*.ring.gz}
80 if egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
81 sudo umount ${SWIFT_DATA_DIR}/drives/sdb1
82 fi
83 if [[ -e ${SWIFT_DATA_DIR}/drives/images/swift.img ]]; then
84 rm ${SWIFT_DATA_DIR}/drives/images/swift.img
85 fi
86}
87
88# configure_swift() - Set config files, create data dirs and loop image
89function configure_swift() {
90 local swift_auth_server
91 local node_number
92 local swift_node_config
93 local swift_log_dir
94
95 setup_develop $SWIFT_DIR
96
97 # Make sure to kill all swift processes first
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +010098 swift-init --run-dir=${SWIFT_DATA_DIR}/run all stop || true
Attila Fazekasece6a332012-11-29 14:19:41 +010099
100 # First do a bit of setup by creating the directories and
101 # changing the permissions so we can run it as our user.
102
103 USER_GROUP=$(id -g)
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +0100104 sudo mkdir -p ${SWIFT_DATA_DIR}/{drives,cache,run,logs}
Attila Fazekasece6a332012-11-29 14:19:41 +0100105 sudo chown -R $USER:${USER_GROUP} ${SWIFT_DATA_DIR}
106
107 # Create a loopback disk and format it to XFS.
108 if [[ -e ${SWIFT_DATA_DIR}/drives/images/swift.img ]]; then
109 if egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
110 sudo umount ${SWIFT_DATA_DIR}/drives/sdb1
Chmouel Boudjnah43eb0b32013-01-12 20:10:34 +0000111 sudo rm -f ${SWIFT_DATA_DIR}/drives/images/swift.img
Attila Fazekasece6a332012-11-29 14:19:41 +0100112 fi
Attila Fazekasece6a332012-11-29 14:19:41 +0100113 fi
114
Chmouel Boudjnah43eb0b32013-01-12 20:10:34 +0000115 mkdir -p ${SWIFT_DATA_DIR}/drives/images
116 sudo touch ${SWIFT_DATA_DIR}/drives/images/swift.img
117 sudo chown $USER: ${SWIFT_DATA_DIR}/drives/images/swift.img
118
119 dd if=/dev/zero of=${SWIFT_DATA_DIR}/drives/images/swift.img \
120 bs=1024 count=0 seek=${SWIFT_LOOPBACK_DISK_SIZE}
121
Attila Fazekasece6a332012-11-29 14:19:41 +0100122 # Make a fresh XFS filesystem
123 mkfs.xfs -f -i size=1024 ${SWIFT_DATA_DIR}/drives/images/swift.img
124
125 # Mount the disk with mount options to make it as efficient as possible
126 mkdir -p ${SWIFT_DATA_DIR}/drives/sdb1
127 if ! egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
128 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
129 ${SWIFT_DATA_DIR}/drives/images/swift.img ${SWIFT_DATA_DIR}/drives/sdb1
130 fi
131
132 # Create a link to the above mount and
133 # create all of the directories needed to emulate a few different servers
134 for node_number in ${SWIFT_REPLICAS_SEQ}; do
135 sudo ln -sf ${SWIFT_DATA_DIR}/drives/sdb1/$node_number ${SWIFT_DATA_DIR}/$node_number;
136 drive=${SWIFT_DATA_DIR}/drives/sdb1/${node_number}
137 node=${SWIFT_DATA_DIR}/${node_number}/node
138 node_device=${node}/sdb1
139 [[ -d $node ]] && continue
140 [[ -d $drive ]] && continue
141 sudo install -o ${USER} -g $USER_GROUP -d $drive
142 sudo install -o ${USER} -g $USER_GROUP -d $node_device
143 sudo chown -R $USER: ${node}
144 done
145
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +0100146 sudo mkdir -p ${SWIFT_CONFIG_DIR}/{object,container,account}-server
147 sudo chown -R $USER: ${SWIFT_CONFIG_DIR}
Attila Fazekasece6a332012-11-29 14:19:41 +0100148
149 if [[ "$SWIFT_CONFIG_DIR" != "/etc/swift" ]]; then
150 # Some swift tools are hard-coded to use ``/etc/swift`` and are apparently not going to be fixed.
151 # Create a symlink if the config dir is moved
152 sudo ln -sf ${SWIFT_CONFIG_DIR} /etc/swift
153 fi
154
155 # Swift use rsync to synchronize between all the different
156 # partitions (which make more sense when you have a multi-node
157 # setup) we configure it with our version of rsync.
158 sed -e "
159 s/%GROUP%/${USER_GROUP}/;
160 s/%USER%/$USER/;
161 s,%SWIFT_DATA_DIR%,$SWIFT_DATA_DIR,;
162 " $FILES/swift/rsyncd.conf | sudo tee /etc/rsyncd.conf
163 # rsyncd.conf just prepared for 4 nodes
Vincent Untzc18b9652012-12-04 12:36:34 +0100164 if is_ubuntu; then
Attila Fazekasece6a332012-11-29 14:19:41 +0100165 sudo sed -i '/^RSYNC_ENABLE=false/ { s/false/true/ }' /etc/default/rsync
166 else
167 sudo sed -i '/disable *= *yes/ { s/yes/no/ }' /etc/xinetd.d/rsync
168 fi
169
170 if is_service_enabled swift3;then
171 swift_auth_server="s3token "
172 fi
173
174 # By default Swift will be installed with the tempauth middleware
175 # which has some default username and password if you have
176 # configured keystone it will checkout the directory.
177 if is_service_enabled key; then
178 swift_auth_server+="authtoken keystoneauth"
179 else
180 swift_auth_server=tempauth
181 fi
182
183 SWIFT_CONFIG_PROXY_SERVER=${SWIFT_CONFIG_DIR}/proxy-server.conf
184 cp ${SWIFT_DIR}/etc/proxy-server.conf-sample ${SWIFT_CONFIG_PROXY_SERVER}
185
186 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user
187 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user ${USER}
188
189 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir
190 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
191
192 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers
193 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers 1
194
195 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level
196 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level DEBUG
197
198 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port
199 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port ${SWIFT_DEFAULT_BIND_PORT:-8080}
200
201 # Only enable Swift3 if we have it enabled in ENABLED_SERVICES
202 is_service_enabled swift3 && swift3=swift3 || swift3=""
203
204 iniset ${SWIFT_CONFIG_PROXY_SERVER} pipeline:main pipeline "catch_errors healthcheck cache ratelimit ${swift3} ${swift_auth_server} proxy-logging proxy-server"
205
206 iniset ${SWIFT_CONFIG_PROXY_SERVER} app:proxy-server account_autocreate true
207
208 # Configure Keystone
209 sed -i '/^# \[filter:authtoken\]/,/^# \[filter:keystoneauth\]$/ s/^#[ \t]*//' ${SWIFT_CONFIG_PROXY_SERVER}
210 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_host $KEYSTONE_AUTH_HOST
211 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_port $KEYSTONE_AUTH_PORT
212 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
213 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
214 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_tenant_name $SERVICE_TENANT_NAME
215 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_user swift
216 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_password $SERVICE_PASSWORD
Dean Troyer64ab7742012-12-28 15:38:28 -0600217 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken signing_dir $SWIFT_AUTH_CACHE_DIR
Attila Fazekasece6a332012-11-29 14:19:41 +0100218
219 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth use
220 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles
221 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles "Member, admin"
222
223 if is_service_enabled swift3; then
224 cat <<EOF >>${SWIFT_CONFIG_PROXY_SERVER}
225# NOTE(chmou): s3token middleware is not updated yet to use only
226# username and password.
227[filter:s3token]
228paste.filter_factory = keystone.middleware.s3_token:filter_factory
229auth_port = ${KEYSTONE_AUTH_PORT}
230auth_host = ${KEYSTONE_AUTH_HOST}
231auth_protocol = ${KEYSTONE_AUTH_PROTOCOL}
232auth_token = ${SERVICE_TOKEN}
233admin_token = ${SERVICE_TOKEN}
234
235[filter:swift3]
236use = egg:swift3#swift3
237EOF
238 fi
239
240 cp ${SWIFT_DIR}/etc/swift.conf-sample ${SWIFT_CONFIG_DIR}/swift.conf
241 iniset ${SWIFT_CONFIG_DIR}/swift.conf swift-hash swift_hash_path_suffix ${SWIFT_HASH}
242
243 # This function generates an object/account/proxy configuration
244 # emulating 4 nodes on different ports
245 function generate_swift_config() {
246 local swift_node_config=$1
247 local node_id=$2
248 local bind_port=$3
249
250 log_facility=$[ node_id - 1 ]
251 node_path=${SWIFT_DATA_DIR}/${node_number}
252
253 iniuncomment ${swift_node_config} DEFAULT user
254 iniset ${swift_node_config} DEFAULT user ${USER}
255
256 iniuncomment ${swift_node_config} DEFAULT bind_port
257 iniset ${swift_node_config} DEFAULT bind_port ${bind_port}
258
259 iniuncomment ${swift_node_config} DEFAULT swift_dir
260 iniset ${swift_node_config} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
261
262 iniuncomment ${swift_node_config} DEFAULT devices
263 iniset ${swift_node_config} DEFAULT devices ${node_path}
264
265 iniuncomment ${swift_node_config} DEFAULT log_facility
266 iniset ${swift_node_config} DEFAULT log_facility LOG_LOCAL${log_facility}
267
268 iniuncomment ${swift_node_config} DEFAULT mount_check
269 iniset ${swift_node_config} DEFAULT mount_check false
270
271 iniuncomment ${swift_node_config} ${server_type}-replicator vm_test_mode
272 iniset ${swift_node_config} ${server_type}-replicator vm_test_mode yes
273 }
274
275 for node_number in ${SWIFT_REPLICAS_SEQ}; do
276 swift_node_config=${SWIFT_CONFIG_DIR}/object-server/${node_number}.conf
277 cp ${SWIFT_DIR}/etc/object-server.conf-sample ${swift_node_config}
278 generate_swift_config ${swift_node_config} ${node_number} $[OBJECT_PORT_BASE + 10 * (node_number - 1)]
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000279 iniset ${swift_node_config} filter:recon recon_cache_path ${SWIFT_DATA_DIR}/cache
280 # Using a sed and not iniset/iniuncomment because we want to a global
281 # modification and make sure it works for new sections.
282 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100283
284 swift_node_config=${SWIFT_CONFIG_DIR}/container-server/${node_number}.conf
285 cp ${SWIFT_DIR}/etc/container-server.conf-sample ${swift_node_config}
286 generate_swift_config ${swift_node_config} ${node_number} $[CONTAINER_PORT_BASE + 10 * (node_number - 1)]
Attila Fazekas83e10952012-11-30 23:28:07 +0100287 iniuncomment ${swift_node_config} app:container-server allow_versions
288 iniset ${swift_node_config} app:container-server allow_versions "true"
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000289 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100290
291 swift_node_config=${SWIFT_CONFIG_DIR}/account-server/${node_number}.conf
292 cp ${SWIFT_DIR}/etc/account-server.conf-sample ${swift_node_config}
293 generate_swift_config ${swift_node_config} ${node_number} $[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000294 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100295 done
296
297 swift_log_dir=${SWIFT_DATA_DIR}/logs
298 rm -rf ${swift_log_dir}
299 mkdir -p ${swift_log_dir}/hourly
300 sudo chown -R $USER:adm ${swift_log_dir}
301 sed "s,%SWIFT_LOGDIR%,${swift_log_dir}," $FILES/swift/rsyslog.conf | sudo \
302 tee /etc/rsyslog.d/10-swift.conf
Attila Fazekasece6a332012-11-29 14:19:41 +0100303}
304
305# configure_swiftclient() - Set config files, create data dirs, etc
306function configure_swiftclient() {
307 setup_develop $SWIFTCLIENT_DIR
308}
309
310# init_swift() - Initialize rings
311function init_swift() {
312 local node_number
313 # Make sure to kill all swift processes first
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +0100314 swift-init --run-dir=${SWIFT_DATA_DIR}/run all stop || true
Attila Fazekasece6a332012-11-29 14:19:41 +0100315
316 # This is where we create three different rings for swift with
317 # different object servers binding on different ports.
318 pushd ${SWIFT_CONFIG_DIR} >/dev/null && {
319
320 rm -f *.builder *.ring.gz backups/*.builder backups/*.ring.gz
321
322 swift-ring-builder object.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
323 swift-ring-builder container.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
324 swift-ring-builder account.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
325
326 for node_number in ${SWIFT_REPLICAS_SEQ}; do
327 swift-ring-builder object.builder add z${node_number}-127.0.0.1:$[OBJECT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
328 swift-ring-builder container.builder add z${node_number}-127.0.0.1:$[CONTAINER_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
329 swift-ring-builder account.builder add z${node_number}-127.0.0.1:$[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
330 done
331 swift-ring-builder object.builder rebalance
332 swift-ring-builder container.builder rebalance
333 swift-ring-builder account.builder rebalance
334 } && popd >/dev/null
335
Dean Troyer64ab7742012-12-28 15:38:28 -0600336 # Create cache dir
337 sudo mkdir -p $SWIFT_AUTH_CACHE_DIR
Attila Fazekas91b8d132013-01-06 22:40:09 +0100338 sudo chown $STACK_USER $SWIFT_AUTH_CACHE_DIR
Dean Troyer64ab7742012-12-28 15:38:28 -0600339 rm -f $SWIFT_AUTH_CACHE_DIR/*
Attila Fazekasece6a332012-11-29 14:19:41 +0100340}
341
342function install_swift() {
343 git_clone $SWIFT_REPO $SWIFT_DIR $SWIFT_BRANCH
344}
345
346function install_swiftclient() {
347 git_clone $SWIFTCLIENT_REPO $SWIFTCLIENT_DIR $SWIFTCLIENT_BRANCH
348}
349
350
351# start_swift() - Start running processes, including screen
352function start_swift() {
353 # (re)start rsyslog
354 restart_service rsyslog
355 # Start rsync
Vincent Untzc18b9652012-12-04 12:36:34 +0100356 if is_ubuntu; then
Attila Fazekasece6a332012-11-29 14:19:41 +0100357 sudo /etc/init.d/rsync restart || :
358 else
359 sudo systemctl start xinetd.service
360 fi
361
362 # First spawn all the swift services then kill the
363 # proxy service so we can run it in foreground in screen.
364 # ``swift-init ... {stop|restart}`` exits with '1' if no servers are running,
365 # ignore it just in case
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +0100366 swift-init --run-dir=${SWIFT_DATA_DIR}/run all restart || true
367 swift-init --run-dir=${SWIFT_DATA_DIR}/run proxy stop || true
Attila Fazekasece6a332012-11-29 14:19:41 +0100368 screen_it swift "cd $SWIFT_DIR && $SWIFT_DIR/bin/swift-proxy-server ${SWIFT_CONFIG_DIR}/proxy-server.conf -v"
369}
370
371# stop_swift() - Stop running processes (non-screen)
372function stop_swift() {
373 # screen normally killed by unstack.sh
Chmouel Boudjnahad8b2762013-01-10 15:40:01 +0100374 swift-init --run-dir=${SWIFT_DATA_DIR}/run all stop || true
Attila Fazekasece6a332012-11-29 14:19:41 +0100375}
376
377# Restore xtrace
378$XTRACE