blob: 46c6eb2059329c8a461118d9bc068c3ef647cffe [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
98 swift-init all stop || true
99
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 Boudjnah8e5d2f02012-12-20 13:11:43 +0000104 sudo mkdir -p ${SWIFT_DATA_DIR}/{drives,cache}
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
111 fi
112 else
113 mkdir -p ${SWIFT_DATA_DIR}/drives/images
114 sudo touch ${SWIFT_DATA_DIR}/drives/images/swift.img
115 sudo chown $USER: ${SWIFT_DATA_DIR}/drives/images/swift.img
116
117 dd if=/dev/zero of=${SWIFT_DATA_DIR}/drives/images/swift.img \
118 bs=1024 count=0 seek=${SWIFT_LOOPBACK_DISK_SIZE}
119 fi
120
121 # Make a fresh XFS filesystem
122 mkfs.xfs -f -i size=1024 ${SWIFT_DATA_DIR}/drives/images/swift.img
123
124 # Mount the disk with mount options to make it as efficient as possible
125 mkdir -p ${SWIFT_DATA_DIR}/drives/sdb1
126 if ! egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
127 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
128 ${SWIFT_DATA_DIR}/drives/images/swift.img ${SWIFT_DATA_DIR}/drives/sdb1
129 fi
130
131 # Create a link to the above mount and
132 # create all of the directories needed to emulate a few different servers
133 for node_number in ${SWIFT_REPLICAS_SEQ}; do
134 sudo ln -sf ${SWIFT_DATA_DIR}/drives/sdb1/$node_number ${SWIFT_DATA_DIR}/$node_number;
135 drive=${SWIFT_DATA_DIR}/drives/sdb1/${node_number}
136 node=${SWIFT_DATA_DIR}/${node_number}/node
137 node_device=${node}/sdb1
138 [[ -d $node ]] && continue
139 [[ -d $drive ]] && continue
140 sudo install -o ${USER} -g $USER_GROUP -d $drive
141 sudo install -o ${USER} -g $USER_GROUP -d $node_device
142 sudo chown -R $USER: ${node}
143 done
144
145 sudo mkdir -p ${SWIFT_CONFIG_DIR}/{object,container,account}-server /var/run/swift
146 sudo chown -R $USER: ${SWIFT_CONFIG_DIR} /var/run/swift
147
148 if [[ "$SWIFT_CONFIG_DIR" != "/etc/swift" ]]; then
149 # Some swift tools are hard-coded to use ``/etc/swift`` and are apparently not going to be fixed.
150 # Create a symlink if the config dir is moved
151 sudo ln -sf ${SWIFT_CONFIG_DIR} /etc/swift
152 fi
153
154 # Swift use rsync to synchronize between all the different
155 # partitions (which make more sense when you have a multi-node
156 # setup) we configure it with our version of rsync.
157 sed -e "
158 s/%GROUP%/${USER_GROUP}/;
159 s/%USER%/$USER/;
160 s,%SWIFT_DATA_DIR%,$SWIFT_DATA_DIR,;
161 " $FILES/swift/rsyncd.conf | sudo tee /etc/rsyncd.conf
162 # rsyncd.conf just prepared for 4 nodes
Vincent Untzc18b9652012-12-04 12:36:34 +0100163 if is_ubuntu; then
Attila Fazekasece6a332012-11-29 14:19:41 +0100164 sudo sed -i '/^RSYNC_ENABLE=false/ { s/false/true/ }' /etc/default/rsync
165 else
166 sudo sed -i '/disable *= *yes/ { s/yes/no/ }' /etc/xinetd.d/rsync
167 fi
168
169 if is_service_enabled swift3;then
170 swift_auth_server="s3token "
171 fi
172
173 # By default Swift will be installed with the tempauth middleware
174 # which has some default username and password if you have
175 # configured keystone it will checkout the directory.
176 if is_service_enabled key; then
177 swift_auth_server+="authtoken keystoneauth"
178 else
179 swift_auth_server=tempauth
180 fi
181
182 SWIFT_CONFIG_PROXY_SERVER=${SWIFT_CONFIG_DIR}/proxy-server.conf
183 cp ${SWIFT_DIR}/etc/proxy-server.conf-sample ${SWIFT_CONFIG_PROXY_SERVER}
184
185 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user
186 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user ${USER}
187
188 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir
189 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
190
191 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers
192 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers 1
193
194 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level
195 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level DEBUG
196
197 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port
198 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port ${SWIFT_DEFAULT_BIND_PORT:-8080}
199
200 # Only enable Swift3 if we have it enabled in ENABLED_SERVICES
201 is_service_enabled swift3 && swift3=swift3 || swift3=""
202
203 iniset ${SWIFT_CONFIG_PROXY_SERVER} pipeline:main pipeline "catch_errors healthcheck cache ratelimit ${swift3} ${swift_auth_server} proxy-logging proxy-server"
204
205 iniset ${SWIFT_CONFIG_PROXY_SERVER} app:proxy-server account_autocreate true
206
207 # Configure Keystone
208 sed -i '/^# \[filter:authtoken\]/,/^# \[filter:keystoneauth\]$/ s/^#[ \t]*//' ${SWIFT_CONFIG_PROXY_SERVER}
209 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_host $KEYSTONE_AUTH_HOST
210 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_port $KEYSTONE_AUTH_PORT
211 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
212 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
213 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_tenant_name $SERVICE_TENANT_NAME
214 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_user swift
215 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_password $SERVICE_PASSWORD
Dean Troyer64ab7742012-12-28 15:38:28 -0600216 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken signing_dir $SWIFT_AUTH_CACHE_DIR
Attila Fazekasece6a332012-11-29 14:19:41 +0100217
218 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth use
219 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles
220 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles "Member, admin"
221
222 if is_service_enabled swift3; then
223 cat <<EOF >>${SWIFT_CONFIG_PROXY_SERVER}
224# NOTE(chmou): s3token middleware is not updated yet to use only
225# username and password.
226[filter:s3token]
227paste.filter_factory = keystone.middleware.s3_token:filter_factory
228auth_port = ${KEYSTONE_AUTH_PORT}
229auth_host = ${KEYSTONE_AUTH_HOST}
230auth_protocol = ${KEYSTONE_AUTH_PROTOCOL}
231auth_token = ${SERVICE_TOKEN}
232admin_token = ${SERVICE_TOKEN}
233
234[filter:swift3]
235use = egg:swift3#swift3
236EOF
237 fi
238
239 cp ${SWIFT_DIR}/etc/swift.conf-sample ${SWIFT_CONFIG_DIR}/swift.conf
240 iniset ${SWIFT_CONFIG_DIR}/swift.conf swift-hash swift_hash_path_suffix ${SWIFT_HASH}
241
242 # This function generates an object/account/proxy configuration
243 # emulating 4 nodes on different ports
244 function generate_swift_config() {
245 local swift_node_config=$1
246 local node_id=$2
247 local bind_port=$3
248
249 log_facility=$[ node_id - 1 ]
250 node_path=${SWIFT_DATA_DIR}/${node_number}
251
252 iniuncomment ${swift_node_config} DEFAULT user
253 iniset ${swift_node_config} DEFAULT user ${USER}
254
255 iniuncomment ${swift_node_config} DEFAULT bind_port
256 iniset ${swift_node_config} DEFAULT bind_port ${bind_port}
257
258 iniuncomment ${swift_node_config} DEFAULT swift_dir
259 iniset ${swift_node_config} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
260
261 iniuncomment ${swift_node_config} DEFAULT devices
262 iniset ${swift_node_config} DEFAULT devices ${node_path}
263
264 iniuncomment ${swift_node_config} DEFAULT log_facility
265 iniset ${swift_node_config} DEFAULT log_facility LOG_LOCAL${log_facility}
266
267 iniuncomment ${swift_node_config} DEFAULT mount_check
268 iniset ${swift_node_config} DEFAULT mount_check false
269
270 iniuncomment ${swift_node_config} ${server_type}-replicator vm_test_mode
271 iniset ${swift_node_config} ${server_type}-replicator vm_test_mode yes
272 }
273
274 for node_number in ${SWIFT_REPLICAS_SEQ}; do
275 swift_node_config=${SWIFT_CONFIG_DIR}/object-server/${node_number}.conf
276 cp ${SWIFT_DIR}/etc/object-server.conf-sample ${swift_node_config}
277 generate_swift_config ${swift_node_config} ${node_number} $[OBJECT_PORT_BASE + 10 * (node_number - 1)]
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000278 iniset ${swift_node_config} filter:recon recon_cache_path ${SWIFT_DATA_DIR}/cache
279 # Using a sed and not iniset/iniuncomment because we want to a global
280 # modification and make sure it works for new sections.
281 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100282
283 swift_node_config=${SWIFT_CONFIG_DIR}/container-server/${node_number}.conf
284 cp ${SWIFT_DIR}/etc/container-server.conf-sample ${swift_node_config}
285 generate_swift_config ${swift_node_config} ${node_number} $[CONTAINER_PORT_BASE + 10 * (node_number - 1)]
Attila Fazekas83e10952012-11-30 23:28:07 +0100286 iniuncomment ${swift_node_config} app:container-server allow_versions
287 iniset ${swift_node_config} app:container-server allow_versions "true"
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000288 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100289
290 swift_node_config=${SWIFT_CONFIG_DIR}/account-server/${node_number}.conf
291 cp ${SWIFT_DIR}/etc/account-server.conf-sample ${swift_node_config}
292 generate_swift_config ${swift_node_config} ${node_number} $[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]
Chmouel Boudjnah8e5d2f02012-12-20 13:11:43 +0000293 sed -i -e "s,#[ ]*recon_cache_path .*,recon_cache_path = ${SWIFT_DATA_DIR}/cache," ${swift_node_config}
Attila Fazekasece6a332012-11-29 14:19:41 +0100294 done
295
296 swift_log_dir=${SWIFT_DATA_DIR}/logs
297 rm -rf ${swift_log_dir}
298 mkdir -p ${swift_log_dir}/hourly
299 sudo chown -R $USER:adm ${swift_log_dir}
300 sed "s,%SWIFT_LOGDIR%,${swift_log_dir}," $FILES/swift/rsyslog.conf | sudo \
301 tee /etc/rsyslog.d/10-swift.conf
Attila Fazekasece6a332012-11-29 14:19:41 +0100302}
303
304# configure_swiftclient() - Set config files, create data dirs, etc
305function configure_swiftclient() {
306 setup_develop $SWIFTCLIENT_DIR
307}
308
309# init_swift() - Initialize rings
310function init_swift() {
311 local node_number
312 # Make sure to kill all swift processes first
313 swift-init all stop || true
314
315 # This is where we create three different rings for swift with
316 # different object servers binding on different ports.
317 pushd ${SWIFT_CONFIG_DIR} >/dev/null && {
318
319 rm -f *.builder *.ring.gz backups/*.builder backups/*.ring.gz
320
321 swift-ring-builder object.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
322 swift-ring-builder container.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
323 swift-ring-builder account.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
324
325 for node_number in ${SWIFT_REPLICAS_SEQ}; do
326 swift-ring-builder object.builder add z${node_number}-127.0.0.1:$[OBJECT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
327 swift-ring-builder container.builder add z${node_number}-127.0.0.1:$[CONTAINER_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
328 swift-ring-builder account.builder add z${node_number}-127.0.0.1:$[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
329 done
330 swift-ring-builder object.builder rebalance
331 swift-ring-builder container.builder rebalance
332 swift-ring-builder account.builder rebalance
333 } && popd >/dev/null
334
Dean Troyer64ab7742012-12-28 15:38:28 -0600335 # Create cache dir
336 sudo mkdir -p $SWIFT_AUTH_CACHE_DIR
Attila Fazekas91b8d132013-01-06 22:40:09 +0100337 sudo chown $STACK_USER $SWIFT_AUTH_CACHE_DIR
Dean Troyer64ab7742012-12-28 15:38:28 -0600338 rm -f $SWIFT_AUTH_CACHE_DIR/*
Attila Fazekasece6a332012-11-29 14:19:41 +0100339}
340
341function install_swift() {
342 git_clone $SWIFT_REPO $SWIFT_DIR $SWIFT_BRANCH
343}
344
345function install_swiftclient() {
346 git_clone $SWIFTCLIENT_REPO $SWIFTCLIENT_DIR $SWIFTCLIENT_BRANCH
347}
348
349
350# start_swift() - Start running processes, including screen
351function start_swift() {
352 # (re)start rsyslog
353 restart_service rsyslog
354 # Start rsync
Vincent Untzc18b9652012-12-04 12:36:34 +0100355 if is_ubuntu; then
Attila Fazekasece6a332012-11-29 14:19:41 +0100356 sudo /etc/init.d/rsync restart || :
357 else
358 sudo systemctl start xinetd.service
359 fi
360
361 # First spawn all the swift services then kill the
362 # proxy service so we can run it in foreground in screen.
363 # ``swift-init ... {stop|restart}`` exits with '1' if no servers are running,
364 # ignore it just in case
365 swift-init all restart || true
366 swift-init proxy stop || true
367 screen_it swift "cd $SWIFT_DIR && $SWIFT_DIR/bin/swift-proxy-server ${SWIFT_CONFIG_DIR}/proxy-server.conf -v"
368}
369
370# stop_swift() - Stop running processes (non-screen)
371function stop_swift() {
372 # screen normally killed by unstack.sh
373 swift-init all stop || true
374}
375
376# Restore xtrace
377$XTRACE