blob: 7acb1dfef58362a1d3ebfde5fb06fcacf8255a65 [file] [log] [blame]
Attila Fazekasece6a332012-11-29 14:19:41 +01001# lib/swift
2# Functions to control the configuration and operation of the swift service
3
4# Dependencies:
5# ``functions`` file
6# ``DEST``, ``SCREEN_NAME``, `SWIFT_HASH` must be defined
7# ``SWIFT_DATA_DIR`` or ``DATA_DIR`` must be defined
8# ``lib/keystone`` file
9# ``stack.sh`` calls the entry points in this order:
10#
11# install_swift
12# configure_swift
13# init_swift
14# start_swift
15# stop_swift
16# cleanup_swift
17
18# Save trace setting
19XTRACE=$(set +o | grep xtrace)
20set +o xtrace
21
22
23# Defaults
24# --------
25
26# <define global variables here that belong to this project>
27
28# Set up default directories
29
30SWIFT_DIR=$DEST/swift
31SWIFTCLIENT_DIR=$DEST/python-swiftclient
32
33# TODO: add logging to different location.
34
35# Set ``SWIFT_DATA_DIR`` to the location of swift drives and objects.
36# Default is the common DevStack data directory.
37SWIFT_DATA_DIR=${SWIFT_DATA_DIR:-${DATA_DIR}/swift}
38
39# Set ``SWIFT_CONFIG_DIR`` to the location of the configuration files.
40# Default is ``/etc/swift``.
41SWIFT_CONFIG_DIR=${SWIFT_CONFIG_DIR:-/etc/swift}
42
43# DevStack will create a loop-back disk formatted as XFS to store the
44# swift data. Set ``SWIFT_LOOPBACK_DISK_SIZE`` to the disk size in bytes.
45# 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
73# Entry Points
74# ------------
75
76# cleanup_swift() - Remove residual data files
77function cleanup_swift() {
78 rm -f ${SWIFT_CONFIG_DIR}{*.builder,*.ring.gz,backups/*.builder,backups/*.ring.gz}
79 if egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
80 sudo umount ${SWIFT_DATA_DIR}/drives/sdb1
81 fi
82 if [[ -e ${SWIFT_DATA_DIR}/drives/images/swift.img ]]; then
83 rm ${SWIFT_DATA_DIR}/drives/images/swift.img
84 fi
85}
86
87# configure_swift() - Set config files, create data dirs and loop image
88function configure_swift() {
89 local swift_auth_server
90 local node_number
91 local swift_node_config
92 local swift_log_dir
93
94 setup_develop $SWIFT_DIR
95
96 # Make sure to kill all swift processes first
97 swift-init all stop || true
98
99 # First do a bit of setup by creating the directories and
100 # changing the permissions so we can run it as our user.
101
102 USER_GROUP=$(id -g)
103 sudo mkdir -p ${SWIFT_DATA_DIR}/drives
104 sudo chown -R $USER:${USER_GROUP} ${SWIFT_DATA_DIR}
105
106 # Create a loopback disk and format it to XFS.
107 if [[ -e ${SWIFT_DATA_DIR}/drives/images/swift.img ]]; then
108 if egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
109 sudo umount ${SWIFT_DATA_DIR}/drives/sdb1
110 fi
111 else
112 mkdir -p ${SWIFT_DATA_DIR}/drives/images
113 sudo touch ${SWIFT_DATA_DIR}/drives/images/swift.img
114 sudo chown $USER: ${SWIFT_DATA_DIR}/drives/images/swift.img
115
116 dd if=/dev/zero of=${SWIFT_DATA_DIR}/drives/images/swift.img \
117 bs=1024 count=0 seek=${SWIFT_LOOPBACK_DISK_SIZE}
118 fi
119
120 # Make a fresh XFS filesystem
121 mkfs.xfs -f -i size=1024 ${SWIFT_DATA_DIR}/drives/images/swift.img
122
123 # Mount the disk with mount options to make it as efficient as possible
124 mkdir -p ${SWIFT_DATA_DIR}/drives/sdb1
125 if ! egrep -q ${SWIFT_DATA_DIR}/drives/sdb1 /proc/mounts; then
126 sudo mount -t xfs -o loop,noatime,nodiratime,nobarrier,logbufs=8 \
127 ${SWIFT_DATA_DIR}/drives/images/swift.img ${SWIFT_DATA_DIR}/drives/sdb1
128 fi
129
130 # Create a link to the above mount and
131 # create all of the directories needed to emulate a few different servers
132 for node_number in ${SWIFT_REPLICAS_SEQ}; do
133 sudo ln -sf ${SWIFT_DATA_DIR}/drives/sdb1/$node_number ${SWIFT_DATA_DIR}/$node_number;
134 drive=${SWIFT_DATA_DIR}/drives/sdb1/${node_number}
135 node=${SWIFT_DATA_DIR}/${node_number}/node
136 node_device=${node}/sdb1
137 [[ -d $node ]] && continue
138 [[ -d $drive ]] && continue
139 sudo install -o ${USER} -g $USER_GROUP -d $drive
140 sudo install -o ${USER} -g $USER_GROUP -d $node_device
141 sudo chown -R $USER: ${node}
142 done
143
144 sudo mkdir -p ${SWIFT_CONFIG_DIR}/{object,container,account}-server /var/run/swift
145 sudo chown -R $USER: ${SWIFT_CONFIG_DIR} /var/run/swift
146
147 if [[ "$SWIFT_CONFIG_DIR" != "/etc/swift" ]]; then
148 # Some swift tools are hard-coded to use ``/etc/swift`` and are apparently not going to be fixed.
149 # Create a symlink if the config dir is moved
150 sudo ln -sf ${SWIFT_CONFIG_DIR} /etc/swift
151 fi
152
153 # Swift use rsync to synchronize between all the different
154 # partitions (which make more sense when you have a multi-node
155 # setup) we configure it with our version of rsync.
156 sed -e "
157 s/%GROUP%/${USER_GROUP}/;
158 s/%USER%/$USER/;
159 s,%SWIFT_DATA_DIR%,$SWIFT_DATA_DIR,;
160 " $FILES/swift/rsyncd.conf | sudo tee /etc/rsyncd.conf
161 # rsyncd.conf just prepared for 4 nodes
162 if [[ "$os_PACKAGE" = "deb" ]]; then
163 sudo sed -i '/^RSYNC_ENABLE=false/ { s/false/true/ }' /etc/default/rsync
164 else
165 sudo sed -i '/disable *= *yes/ { s/yes/no/ }' /etc/xinetd.d/rsync
166 fi
167
168 if is_service_enabled swift3;then
169 swift_auth_server="s3token "
170 fi
171
172 # By default Swift will be installed with the tempauth middleware
173 # which has some default username and password if you have
174 # configured keystone it will checkout the directory.
175 if is_service_enabled key; then
176 swift_auth_server+="authtoken keystoneauth"
177 else
178 swift_auth_server=tempauth
179 fi
180
181 SWIFT_CONFIG_PROXY_SERVER=${SWIFT_CONFIG_DIR}/proxy-server.conf
182 cp ${SWIFT_DIR}/etc/proxy-server.conf-sample ${SWIFT_CONFIG_PROXY_SERVER}
183
184 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user
185 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT user ${USER}
186
187 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir
188 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
189
190 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers
191 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT workers 1
192
193 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level
194 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT log_level DEBUG
195
196 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port
197 iniset ${SWIFT_CONFIG_PROXY_SERVER} DEFAULT bind_port ${SWIFT_DEFAULT_BIND_PORT:-8080}
198
199 # Only enable Swift3 if we have it enabled in ENABLED_SERVICES
200 is_service_enabled swift3 && swift3=swift3 || swift3=""
201
202 iniset ${SWIFT_CONFIG_PROXY_SERVER} pipeline:main pipeline "catch_errors healthcheck cache ratelimit ${swift3} ${swift_auth_server} proxy-logging proxy-server"
203
204 iniset ${SWIFT_CONFIG_PROXY_SERVER} app:proxy-server account_autocreate true
205
206 # Configure Keystone
207 sed -i '/^# \[filter:authtoken\]/,/^# \[filter:keystoneauth\]$/ s/^#[ \t]*//' ${SWIFT_CONFIG_PROXY_SERVER}
208 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_host $KEYSTONE_AUTH_HOST
209 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_port $KEYSTONE_AUTH_PORT
210 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_protocol $KEYSTONE_AUTH_PROTOCOL
211 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken auth_uri $KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_SERVICE_PORT/
212 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_tenant_name $SERVICE_TENANT_NAME
213 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_user swift
214 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:authtoken admin_password $SERVICE_PASSWORD
215
216 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth use
217 iniuncomment ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles
218 iniset ${SWIFT_CONFIG_PROXY_SERVER} filter:keystoneauth operator_roles "Member, admin"
219
220 if is_service_enabled swift3; then
221 cat <<EOF >>${SWIFT_CONFIG_PROXY_SERVER}
222# NOTE(chmou): s3token middleware is not updated yet to use only
223# username and password.
224[filter:s3token]
225paste.filter_factory = keystone.middleware.s3_token:filter_factory
226auth_port = ${KEYSTONE_AUTH_PORT}
227auth_host = ${KEYSTONE_AUTH_HOST}
228auth_protocol = ${KEYSTONE_AUTH_PROTOCOL}
229auth_token = ${SERVICE_TOKEN}
230admin_token = ${SERVICE_TOKEN}
231
232[filter:swift3]
233use = egg:swift3#swift3
234EOF
235 fi
236
237 cp ${SWIFT_DIR}/etc/swift.conf-sample ${SWIFT_CONFIG_DIR}/swift.conf
238 iniset ${SWIFT_CONFIG_DIR}/swift.conf swift-hash swift_hash_path_suffix ${SWIFT_HASH}
239
240 # This function generates an object/account/proxy configuration
241 # emulating 4 nodes on different ports
242 function generate_swift_config() {
243 local swift_node_config=$1
244 local node_id=$2
245 local bind_port=$3
246
247 log_facility=$[ node_id - 1 ]
248 node_path=${SWIFT_DATA_DIR}/${node_number}
249
250 iniuncomment ${swift_node_config} DEFAULT user
251 iniset ${swift_node_config} DEFAULT user ${USER}
252
253 iniuncomment ${swift_node_config} DEFAULT bind_port
254 iniset ${swift_node_config} DEFAULT bind_port ${bind_port}
255
256 iniuncomment ${swift_node_config} DEFAULT swift_dir
257 iniset ${swift_node_config} DEFAULT swift_dir ${SWIFT_CONFIG_DIR}
258
259 iniuncomment ${swift_node_config} DEFAULT devices
260 iniset ${swift_node_config} DEFAULT devices ${node_path}
261
262 iniuncomment ${swift_node_config} DEFAULT log_facility
263 iniset ${swift_node_config} DEFAULT log_facility LOG_LOCAL${log_facility}
264
265 iniuncomment ${swift_node_config} DEFAULT mount_check
266 iniset ${swift_node_config} DEFAULT mount_check false
267
268 iniuncomment ${swift_node_config} ${server_type}-replicator vm_test_mode
269 iniset ${swift_node_config} ${server_type}-replicator vm_test_mode yes
270 }
271
272 for node_number in ${SWIFT_REPLICAS_SEQ}; do
273 swift_node_config=${SWIFT_CONFIG_DIR}/object-server/${node_number}.conf
274 cp ${SWIFT_DIR}/etc/object-server.conf-sample ${swift_node_config}
275 generate_swift_config ${swift_node_config} ${node_number} $[OBJECT_PORT_BASE + 10 * (node_number - 1)]
276
277 swift_node_config=${SWIFT_CONFIG_DIR}/container-server/${node_number}.conf
278 cp ${SWIFT_DIR}/etc/container-server.conf-sample ${swift_node_config}
279 generate_swift_config ${swift_node_config} ${node_number} $[CONTAINER_PORT_BASE + 10 * (node_number - 1)]
280
281 swift_node_config=${SWIFT_CONFIG_DIR}/account-server/${node_number}.conf
282 cp ${SWIFT_DIR}/etc/account-server.conf-sample ${swift_node_config}
283 generate_swift_config ${swift_node_config} ${node_number} $[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]
284 done
285
286 swift_log_dir=${SWIFT_DATA_DIR}/logs
287 rm -rf ${swift_log_dir}
288 mkdir -p ${swift_log_dir}/hourly
289 sudo chown -R $USER:adm ${swift_log_dir}
290 sed "s,%SWIFT_LOGDIR%,${swift_log_dir}," $FILES/swift/rsyslog.conf | sudo \
291 tee /etc/rsyslog.d/10-swift.conf
292
293}
294
295# configure_swiftclient() - Set config files, create data dirs, etc
296function configure_swiftclient() {
297 setup_develop $SWIFTCLIENT_DIR
298}
299
300# init_swift() - Initialize rings
301function init_swift() {
302 local node_number
303 # Make sure to kill all swift processes first
304 swift-init all stop || true
305
306 # This is where we create three different rings for swift with
307 # different object servers binding on different ports.
308 pushd ${SWIFT_CONFIG_DIR} >/dev/null && {
309
310 rm -f *.builder *.ring.gz backups/*.builder backups/*.ring.gz
311
312 swift-ring-builder object.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
313 swift-ring-builder container.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
314 swift-ring-builder account.builder create ${SWIFT_PARTITION_POWER_SIZE} ${SWIFT_REPLICAS} 1
315
316 for node_number in ${SWIFT_REPLICAS_SEQ}; do
317 swift-ring-builder object.builder add z${node_number}-127.0.0.1:$[OBJECT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
318 swift-ring-builder container.builder add z${node_number}-127.0.0.1:$[CONTAINER_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
319 swift-ring-builder account.builder add z${node_number}-127.0.0.1:$[ACCOUNT_PORT_BASE + 10 * (node_number - 1)]/sdb1 1
320 done
321 swift-ring-builder object.builder rebalance
322 swift-ring-builder container.builder rebalance
323 swift-ring-builder account.builder rebalance
324 } && popd >/dev/null
325
326}
327
328function install_swift() {
329 git_clone $SWIFT_REPO $SWIFT_DIR $SWIFT_BRANCH
330}
331
332function install_swiftclient() {
333 git_clone $SWIFTCLIENT_REPO $SWIFTCLIENT_DIR $SWIFTCLIENT_BRANCH
334}
335
336
337# start_swift() - Start running processes, including screen
338function start_swift() {
339 # (re)start rsyslog
340 restart_service rsyslog
341 # Start rsync
342 if [[ "$os_PACKAGE" = "deb" ]]; then
343 sudo /etc/init.d/rsync restart || :
344 else
345 sudo systemctl start xinetd.service
346 fi
347
348 # First spawn all the swift services then kill the
349 # proxy service so we can run it in foreground in screen.
350 # ``swift-init ... {stop|restart}`` exits with '1' if no servers are running,
351 # ignore it just in case
352 swift-init all restart || true
353 swift-init proxy stop || true
354 screen_it swift "cd $SWIFT_DIR && $SWIFT_DIR/bin/swift-proxy-server ${SWIFT_CONFIG_DIR}/proxy-server.conf -v"
355}
356
357# stop_swift() - Stop running processes (non-screen)
358function stop_swift() {
359 # screen normally killed by unstack.sh
360 swift-init all stop || true
361}
362
363# Restore xtrace
364$XTRACE