| OLD | NEW |
| (Empty) |
| 1 #!/bin/sh | |
| 2 | |
| 3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 # Script to push the output of build_image.sh to a remote image server | |
| 8 | |
| 9 # TODO(pstew): Apparently the script files are in transition from | |
| 10 # src/scripts to src/scripts/bin. However this state has existed | |
| 11 # for months now, therefore we need to look for the common libs in | |
| 12 # both places | |
| 13 script_root=$(dirname $0) | |
| 14 if [ -f ${script_root}/../common.sh ] ; then | |
| 15 script_root=${script_root}/.. | |
| 16 fi | |
| 17 | |
| 18 . "${script_root}/common.sh" | |
| 19 | |
| 20 # Flags | |
| 21 DEFINE_string upgrade_server "" "SSH-capable host for upgrade server install" | |
| 22 DEFINE_string dest_path "" "Directory on host to do install" | |
| 23 DEFINE_string client_address "" "IP Address of netbook to update" | |
| 24 DEFINE_string server_address "" "IP Address of upgrade server" | |
| 25 DEFINE_string client_prefix "ChromeOSUpdateEngine" \ | |
| 26 "client_prefix arg to devserver. Old version is MementoSoftwareUpdate" | |
| 27 DEFINE_boolean old_prefix ${FLAGS_FALSE} "Use old MementoSoftwareUpdate" | |
| 28 DEFINE_boolean start_server ${FLAGS_TRUE} "Start up the server" | |
| 29 DEFINE_boolean stop_server ${FLAGS_FALSE} "Start up the server" | |
| 30 DEFINE_boolean no_copy_archive ${FLAGS_FALSE} "Skip copy of files to server" | |
| 31 DEFINE_string from "" "Image directory to upload to server" | |
| 32 | |
| 33 # Parse command line | |
| 34 FLAGS "$@" || exit 1 | |
| 35 eval set -- "${FLAGS_ARGV}" | |
| 36 | |
| 37 set -e | |
| 38 | |
| 39 # Make sure dev server argument has been set | |
| 40 require_upgrade_server () { | |
| 41 if [ -z "${FLAGS_upgrade_server}" ] ; then | |
| 42 echo "The --upgrade-server= argument is mandatory" | |
| 43 exit 1 | |
| 44 fi | |
| 45 } | |
| 46 | |
| 47 # Make sure a pointer to the latest image has been created | |
| 48 require_latest_image () { | |
| 49 [ -n "$latest_image" ] && return | |
| 50 if [ -n "${FLAGS_from}" ] ; then | |
| 51 latest_image=$(readlink -f ${FLAGS_from}) | |
| 52 else | |
| 53 latest_image=$(env CHROMEOS_BUILD_ROOT=${SCRIPTS_DIR}/../build \ | |
| 54 ${SCRIPTS_DIR}/get_latest_image.sh) | |
| 55 fi | |
| 56 } | |
| 57 | |
| 58 validate_devserver_path () { | |
| 59 if [ $(expr "${FLAGS_dest_path}" : '\.\.') != 0 ]; then | |
| 60 echo "Error: --dest_path argument (${FLAGS_dest_path}) must not be relative" | |
| 61 exit 1 | |
| 62 fi | |
| 63 FLAGS_dest_path=/tmp/devserver/${FLAGS_dest_path##/tmp/devserver/} | |
| 64 } | |
| 65 | |
| 66 # Copy the various bits of the dev server scripts over to our remote host | |
| 67 create_devserver () { | |
| 68 FLAGS_dest_path=$1 | |
| 69 validate_devserver_path | |
| 70 | |
| 71 echo "Creating dev server in ${FLAGS_upgrade_server}:${FLAGS_dest_path}..." | |
| 72 | |
| 73 require_upgrade_server | |
| 74 # Create new empty directory to hold server components | |
| 75 ssh "${FLAGS_upgrade_server}" rm -rf "${FLAGS_dest_path}" || true | |
| 76 ssh "${FLAGS_upgrade_server}" mkdir -p "${FLAGS_dest_path}/python" | |
| 77 | |
| 78 # Copy server components into place | |
| 79 (cd ${SCRIPTS_DIR}/../.. && \ | |
| 80 tar zcfh - --exclude=.git --exclude=.svn --exclude=pkgroot \ | |
| 81 src/scripts/lib \ | |
| 82 src/scripts/start_devserver \ | |
| 83 src/scripts/cros_generate_update_payload \ | |
| 84 src/scripts/chromeos-common.sh \ | |
| 85 src/scripts/{common,get_latest_image,mk_memento_images}.sh \ | |
| 86 src/platform/dev) | \ | |
| 87 ssh ${FLAGS_upgrade_server} "cd ${FLAGS_dest_path} && tar zxf -" | |
| 88 | |
| 89 # Copy Python web library into place out of the chroot | |
| 90 (cd ${SCRIPTS_DIR}/../../chroot/usr/lib/python*/site-packages && \ | |
| 91 tar zcf - web*) | \ | |
| 92 ssh ${FLAGS_upgrade_server} "cd ${FLAGS_dest_path}/python && tar zxf -" | |
| 93 } | |
| 94 | |
| 95 # Copy the latest image over to archive server | |
| 96 create_archive_dir () { | |
| 97 archive_dir=$1 | |
| 98 | |
| 99 echo "Creating archive dir in ${FLAGS_upgrade_server}:${archive_dir}..." | |
| 100 | |
| 101 require_upgrade_server | |
| 102 require_latest_image | |
| 103 | |
| 104 # Copy the latest image into the newly created archive | |
| 105 ssh "${FLAGS_upgrade_server}" "mkdir -p ${archive_dir}" | |
| 106 | |
| 107 image_path=${latest_image##*build/} | |
| 108 | |
| 109 (cd ${SCRIPTS_DIR}/../build && tar zcf - ${image_path}) | \ | |
| 110 ssh ${FLAGS_upgrade_server} "cd ${archive_dir} && tar zxf -" | |
| 111 | |
| 112 # unpack_partitions.sh lies in its hashbang. It really wants bash | |
| 113 unpack_script=${archive_dir}/${image_path}/unpack_partitions.sh | |
| 114 ssh ${FLAGS_upgrade_server} "sed -e 's/^#!\/bin\/sh/#!\/bin\/bash/' < ${unpack
_script} > ${unpack_script}.new && chmod 755 ${unpack_script}.new && mv ${unpack
_script}.new ${unpack_script}" | |
| 115 | |
| 116 # Since we are in static-only mode, we need to create a few links | |
| 117 for file in update.gz stateful.image.gz ; do | |
| 118 ssh ${FLAGS_upgrade_server} "cd ${archive_dir} && ln -sf ${image_path}/$file
." | |
| 119 ssh ${FLAGS_upgrade_server} "ln -sf ${archive_dir}/$file ${FLAGS_dest_path}/
src/platform/dev/static" | |
| 120 done | |
| 121 } | |
| 122 | |
| 123 stop_server () { | |
| 124 require_upgrade_server | |
| 125 echo "Stopping remote devserver..." | |
| 126 echo "(Fast restart using \"$0 --upgrade_server=${FLAGS_upgrade_server} --dest
_path=${FLAGS_dest_path} --no_copy_archive\")" | |
| 127 ssh ${FLAGS_upgrade_server} pkill -f ${archive_dir} || /bin/true | |
| 128 } | |
| 129 | |
| 130 # Start remote server | |
| 131 start_server () { | |
| 132 require_upgrade_server | |
| 133 echo "Starting remote devserver..." | |
| 134 server_logfile=/tmp/devserver_log.$$ | |
| 135 portlist=/tmp/devserver_portlist.$$ | |
| 136 echo "Server will be logging locally to $server_logfile" | |
| 137 | |
| 138 # Find a TCP listen socket that is not in use | |
| 139 ssh ${FLAGS_upgrade_server} "netstat -lnt" | awk '{ print $4 }' > $portlist | |
| 140 server_port=8081 | |
| 141 while grep -q ":${server_port}$" $portlist; do | |
| 142 server_port=$[server_port + 1] | |
| 143 done | |
| 144 rm -f $portlist | |
| 145 | |
| 146 if [ "${FLAGS_old_prefix}" -eq ${FLAGS_TRUE} ] ; then | |
| 147 FLAGS_client_prefix=MementoSoftwareUpdate | |
| 148 fi | |
| 149 | |
| 150 ssh ${FLAGS_upgrade_server} "cd ${FLAGS_dest_path}/src/scripts && env PYTHONPA
TH=${remote_root}${FLAGS_dest_path}/python CHROMEOS_BUILD_ROOT=${archive_dir} ./
start_devserver --archive_dir ${archive_dir} --client_prefix ${FLAGS_client_pref
ix} $server_port" > $server_logfile 2>&1 & | |
| 151 server_pid=$! | |
| 152 | |
| 153 trap server_cleanup 2 | |
| 154 | |
| 155 # Wait for server to startup | |
| 156 while sleep 1; do | |
| 157 if fgrep -q 'Serving images from' $server_logfile; then | |
| 158 echo "Server is ready" | |
| 159 break | |
| 160 elif kill -0 ${server_pid}; then | |
| 161 continue | |
| 162 else | |
| 163 echo "Server failed to startup" | |
| 164 exit 1 | |
| 165 fi | |
| 166 done | |
| 167 } | |
| 168 | |
| 169 server_cleanup () { | |
| 170 trap '' 2 | |
| 171 stop_server | |
| 172 exit 0 | |
| 173 } | |
| 174 | |
| 175 # If destination path wasn't set on command line, create one from scratch | |
| 176 if [ -z "${FLAGS_dest_path}" -a ${FLAGS_stop_server} -eq ${FLAGS_FALSE} ] ; then | |
| 177 require_latest_image | |
| 178 hostname=$(uname -n) | |
| 179 hostname=${hostname%%.*} | |
| 180 image_name=${latest_image##*/} | |
| 181 create_devserver ${hostname}_${image_name} | |
| 182 FLAGS_start_server=${FLAGS_TRUE} | |
| 183 else | |
| 184 validate_devserver_path | |
| 185 fi | |
| 186 | |
| 187 if [ ${FLAGS_stop_server} -eq ${FLAGS_FALSE} -a \ | |
| 188 ${FLAGS_no_copy_archive} -eq ${FLAGS_FALSE} ] ; then | |
| 189 create_archive_dir "${FLAGS_dest_path}/archive" | |
| 190 FLAGS_start_server=${FLAGS_TRUE} | |
| 191 else | |
| 192 archive_dir="${FLAGS_dest_path}/archive" | |
| 193 fi | |
| 194 | |
| 195 if [ "${FLAGS_stop_server}" -eq ${FLAGS_TRUE} ] ; then | |
| 196 stop_server | |
| 197 exit 0 | |
| 198 fi | |
| 199 | |
| 200 # Make sure old devserver is dead, then restart it | |
| 201 if [ "${FLAGS_start_server}" -eq ${FLAGS_TRUE} ] ; then | |
| 202 stop_server | |
| 203 start_server | |
| 204 | |
| 205 tail -f ${server_logfile} & | |
| 206 | |
| 207 # Now tell the client to load from the server | |
| 208 if [ -z "${FLAGS_server_address}" ] ; then | |
| 209 FLAGS_server_address=${FLAGS_upgrade_server} | |
| 210 fi | |
| 211 live_args="--update_url=http://${FLAGS_server_address}:${server_port}/update \ | |
| 212 --remote=${FLAGS_client_address}" | |
| 213 if [ -n "${FLAGS_client_address}" ] ; then | |
| 214 echo "Running ${SCRIPTS_DIR}/image_to_live.sh $live_args" | |
| 215 ${SCRIPTS_DIR}/image_to_live.sh $live_args & | |
| 216 else | |
| 217 echo "Start client upgrade using:" | |
| 218 echo " ${SCRIPTS_DIR}/image_to_live.sh ${live_args}<client_ip_address>" | |
| 219 fi | |
| 220 | |
| 221 wait ${server_pid} | |
| 222 fi | |
| 223 | |
| OLD | NEW |