blob: 4b694f33bf93d54ca8627a9e957e5380a9d2c1db [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Check uber shopfloor server, fetch and parse board config.
#
# Channel image URLs and checksums are sent out through stdout for shell
# to eval. The format:
#
# SRC_{CHANNEL}_URL=http://shopfloor_server:port/pathto/gzimage
# SRC_{CHANNEL}_CHECKSUM=plain_image_binarysha1_base64
#
# For memento updater compatibility, images should be generated by script
# make_factory_package.sh.
#
# Example:
# SHOPFLOOR="http://SF_SERVER:SF_PORT"
# BOARD="testboard"
# result=$(ping_shopfloor)
# rc="$?"
# if [ "$rc" = "0" ]; then
# eval "$result"
# fi
# if [ "$SHOPFLOOR_INSTALL" = "1" ]; then
# do_install
# ...
#
sf_log() {
echo "$(date) $*" >&2
}
ping_shopfloor() {
local channel_hash=""
local channel_file=""
local channel_name=""
local config_file="default.conf"
local line=""
local resource_url=""
local wget_output=""
local wget_rc="0"
local channels=""
local cleanup_files=""
# Get config file name from BOARD
if [ -n "$BOARD" ]; then
config_file="${BOARD}.conf"
fi
# Get resource URL from SHOPFLOOR or OMAHA
if [ -n "$SHOPFLOOR" ]; then
resource_url="$SHOPFLOOR/res"
elif [ -n "$OMAHA" ]; then
resource_url="${OMAHA%/update}/res"
else
sf_log "ping_shopfloor: SHOPFLOOR and OMAHA not found."
return 1
fi
# Try to get resource map from Umpire. ETH_INTERFACE is passed from
# factory_install.sh.
local sn="$(vpd -i RO_VPD -g serial_number)" || sn=""
local mlb_sn="$(vpd -i RO_VPD -g mlb_serial_number)" || mlb_sn=""
local board="${BOARD}"
local mac_addr="$(ip link show ${ETH_INTERFACE} | grep link/ether |
tr -s ' '| cut -d ' ' -f 3)"
local resourcemap_rc="0"
local resourcemap=""
local mac="mac.${ETH_INTERFACE}=${mac_addr};"
local values="sn=${sn}; mlb_sn=${mlb_sn}; board=${board}; ${mac}"
local empty_values="firmware=; ec=; stage=;"
local x_umpire_dut_header="X-Umpire-DUT: ${values} ${empty_values}"
local target="${resource_url%/res}/resourcemap"
sf_log "ping_shopfloor: ${x_umpire_dut_header}"
resourcemap="$(wget -O - --header "${x_umpire_dut_header}" "${target}")" ||
resourcemap_rc="$?"
if [ "$resourcemap_rc" != "0" ]; then
sf_log "ping_shopfloor: could not fetch resourcemap."
else
sf_log "ping_shopfloor: got resourcemap: ${resourcemap}"
config_file="$(echo "${resourcemap}" | grep download_conf |
cut -d ':' -f 2 | tr -d ' ')"
sf_log "ping_shopfloor: config_file: ${config_file}"
fi
# Ping shopfloor by fetching the config file
wget_output="$(mktemp "/tmp/conf_XXXXXXXX")"
cleanup_files="$wget_output"
wget -O $wget_output "$resource_url/$config_file" 2>/dev/null ||
wget_rc="$?"
if [ "$wget_rc" != "0" ]; then
sf_log "ping_shopfloor: could not fetch $resource_url/$config_file"
[ -f "$cleanup_files" ] && rm $cleanup_files
return 1
fi
# Fix shell read line without \n
echo >>"$wget_output"
# Parse channels and skip comments in a subprocess
channels="$(mktemp "/tmp/sf_XXXXXXXX")"
cleanup_files="$cleanup_files $channels"
while IFS= read line; do
# Remove leading whitespaces
line=$(echo $line | sed -e 's/^[ \t]*//')
# Skip comment and empty lines
[ -n "$(echo $line | grep -E '^#')" -o -z "$line" ] && continue
# Split channel, file, and hash
channel_name=$(echo $line | cut -d':' -f1)
channel_file=$(echo $line | cut -d':' -f2)
channel_hash=$(echo $line | cut -d':' -f3)
if [ -z "$channel_name" -o -z "$channel_file" -o -z "$channel_hash" ]; then
sf_log "ping_shopfloor: not a channel $resource_url/$config_file ($line)"
[ -n "$cleanup_files" ] && rm $cleanup_files
return 1
fi
echo "SRC_${channel_name}_URL=$resource_url/$channel_file" >> "$channels"
echo "SRC_${channel_name}_CHECKSUM=$channel_hash" >> "$channels"
done <"$wget_output"
if [ "$?" != "0" ]; then
[ -n "$cleanup_files" ] && rm $cleanup_files
return 1
fi
cat "$channels"
echo "SHOPFLOOR_INSTALL=1"
[ -n "$cleanup_files" ] && rm $cleanup_files
return 0
}