<commit_before>#!/bin/bash
#
# Copyright (c) 2013, Joyent, Inc., All rights reserved.
#
# These upgrade hooks are called from headnode.sh during the first boot
# after we initiated the upgrade in order to complete the upgrade steps.
#
# Usage:
#       ${SDC_UPGRADE_DIR}/upgrade_hooks.sh HOOK [ZONENAME]
#
# The "pre" and "post" hooks are called before headnode does any setup
# and after the setup is complete.
#
# The role-specific hooks are called after each zone role has been setup.
#

unset LD_LIBRARY_PATH
PATH=/usr/bin:/usr/sbin:/opt/smartdc/bin:/smartdc/bin
export PATH

BASH_XTRACEFD=4
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace

. /lib/sdc/config.sh



#---- globals

# time to wait for each zone to setup (in seconds)
ZONE_SETUP_TIMEOUT=180

SDC_UPGRADE_DIR=/var/upgrade_headnode

# Install cloudapi first so sdcsso doesn't steal cloudapi's external IP addr.
EXTRA_ZONES="cloudapi sdcsso"

#---- setup state support
# "/var/lib/setup.json" support is duplicated in headnode.sh and
# upgrade_hooks.sh. These must be kept in sync.
# TODO: share these somewhere

SETUP_FILE=/var/lib/setup.json

function update_setup_state
{
    STATE=$1

    chmod 600 $SETUP_FILE
    cat "$SETUP_FILE" | json -e \
        "this.current_state = '$STATE';
         this.last_updated = new Date().toISOString();
         this.seen_states.push('$STATE');" \
        | tee ${SETUP_FILE}.new
    mv ${SETUP_FILE}.new $SETUP_FILE
    chmod 400 $SETUP_FILE
}

function mark_as_setup
{
    chmod 600 $SETUP_FILE
    # Update the setup state file with the new value
    cat "$SETUP_FILE" | json -e "this.complete = true;
         this.current_state = 'setup_complete';
         this.seen_states.push('setup_complete');
         this.last_updated = new Date().toISOString();" \
        | tee ${SETUP_FILE}.new
    mv ${SETUP_FILE}.new $SETUP_FILE
    chmod 400 $SETUP_FILE
    sysinfo -u
}


#---- support functions

saw_err()
{
    echo "    $1" >> ${SDC_UPGRADE_DIR}/error_finalize.txt
}

shutdown_zone()
{
	zlogin $1 /usr/sbin/shutdown -y -g 0 -i 5 1>&4 2>&1

	# Check for zone being down and halt it forcefully if needed
	local cnt=0
	while [[ $cnt -lt 18 ]]; do
		sleep 5
		local zstate=`zoneadm -z $1 list -p | cut -f3 -d:`
		[[ "$zstate" == "installed" ]] && break
		cnt=$(($cnt + 1))
	done

	# After 90 seconds, shutdown harder
	[[ $cnt == 18 ]] && zoneadm -z $1 halt
}

print_log()
{
    echo "$@" | tee -a /tmp/upgrade_progress >/dev/console
}

create_extra_zones()
{
    declare -A existing_zones=()

    for i in `sdc-role list | nawk '{if ($6 != "ROLE") print $6}'`
    do
        existing_zones[$i]=1
    done

    local new_uuid=
    local loops=
    local zonepath=
    local ext_ip=
    local ext_ip_arg=
    local res=
    local hn_uuid=$(sysinfo | json UUID)

    for i in $EXTRA_ZONES
    do
        [[ ${existing_zones[$i]} == 1 ]] && continue

        print_log "creating zone $i..."

        ext_ip=`nawk -v role=$i '{if ($1 == role) print $2 }' \
            ${SDC_UPGRADE_DIR}/ext_addrs.txt`

        local service_uuid=$(sdc-sapi /servers?name=$i | json -H 0.uuid)
        local payload="{
            \"service_uuid\": \"$service_uuid\",
            \"params\": {
                \"alias\": \"cloudapi4\",
                \"networks\": [
                    {
                        \"uuid\": \"$(sdc-napi /networks | json -H -c 'this.nic_tag=="admin"' 0.uuid)\"
                    }
                ]
            }
        }"
        if [[ -n "$ext_ip" ]]; then
            payload=$(echo "$payload" | json -e "this.params.networks.push({
                \"uuid\": \"$(sdc-napi /networks | json -H -c 'this.nic_tag=="external"' 0.uuid)\",
                \"ip\": \"$ext_ip\",
                \"primary\": true
            })")
        fi
        echo "$payload" | sapiadm provision

        res=$?
        if [ $res != 0 ]; then
            saw_err "Error creating $i zone"<commit_msg>HEAD-1637: get deployment of sdcsso and cloudapi working without sdc-role (get the sapi endpoint correct)<commit_after>#!/bin/bash
#
# Copyright (c) 2013, Joyent, Inc., All rights reserved.
#
# These upgrade hooks are called from headnode.sh during the first boot
# after we initiated the upgrade in order to complete the upgrade steps.
#
# Usage:
#       ${SDC_UPGRADE_DIR}/upgrade_hooks.sh HOOK [ZONENAME]
#
# The "pre" and "post" hooks are called before headnode does any setup
# and after the setup is complete.
#
# The role-specific hooks are called after each zone role has been setup.
#

unset LD_LIBRARY_PATH
PATH=/usr/bin:/usr/sbin:/opt/smartdc/bin:/smartdc/bin
export PATH

BASH_XTRACEFD=4
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace

. /lib/sdc/config.sh



#---- globals

# time to wait for each zone to setup (in seconds)
ZONE_SETUP_TIMEOUT=180

SDC_UPGRADE_DIR=/var/upgrade_headnode

# Install cloudapi first so sdcsso doesn't steal cloudapi's external IP addr.
EXTRA_ZONES="cloudapi sdcsso"

#---- setup state support
# "/var/lib/setup.json" support is duplicated in headnode.sh and
# upgrade_hooks.sh. These must be kept in sync.
# TODO: share these somewhere

SETUP_FILE=/var/lib/setup.json

function update_setup_state
{
    STATE=$1

    chmod 600 $SETUP_FILE
    cat "$SETUP_FILE" | json -e \
        "this.current_state = '$STATE';
         this.last_updated = new Date().toISOString();
         this.seen_states.push('$STATE');" \
        | tee ${SETUP_FILE}.new
    mv ${SETUP_FILE}.new $SETUP_FILE
    chmod 400 $SETUP_FILE
}

function mark_as_setup
{
    chmod 600 $SETUP_FILE
    # Update the setup state file with the new value
    cat "$SETUP_FILE" | json -e "this.complete = true;
         this.current_state = 'setup_complete';
         this.seen_states.push('setup_complete');
         this.last_updated = new Date().toISOString();" \
        | tee ${SETUP_FILE}.new
    mv ${SETUP_FILE}.new $SETUP_FILE
    chmod 400 $SETUP_FILE
    sysinfo -u
}


#---- support functions

saw_err()
{
    echo "    $1" >> ${SDC_UPGRADE_DIR}/error_finalize.txt
}

shutdown_zone()
{
	zlogin $1 /usr/sbin/shutdown -y -g 0 -i 5 1>&4 2>&1

	# Check for zone being down and halt it forcefully if needed
	local cnt=0
	while [[ $cnt -lt 18 ]]; do
		sleep 5
		local zstate=`zoneadm -z $1 list -p | cut -f3 -d:`
		[[ "$zstate" == "installed" ]] && break
		cnt=$(($cnt + 1))
	done

	# After 90 seconds, shutdown harder
	[[ $cnt == 18 ]] && zoneadm -z $1 halt
}

print_log()
{
    echo "$@" | tee -a /tmp/upgrade_progress >/dev/console
}

create_extra_zones()
{
    declare -A existing_zones=()

    for i in `sdc-role list | nawk '{if ($6 != "ROLE") print $6}'`
    do
        existing_zones[$i]=1
    done

    local new_uuid=
    local loops=
    local zonepath=
    local ext_ip=
    local ext_ip_arg=
    local res=
    local hn_uuid=$(sysinfo | json UUID)

    for i in $EXTRA_ZONES
    do
        [[ ${existing_zones[$i]} == 1 ]] && continue

        print_log "creating zone $i..."

        ext_ip=`nawk -v role=$i '{if ($1 == role) print $2 }' \
            ${SDC_UPGRADE_DIR}/ext_addrs.txt`

        local service_uuid=$(sdc-sapi /services?name=$i | json -H 0.uuid)
        local payload="{
            \"service_uuid\": \"$service_uuid\",
            \"params\": {
                \"alias\": \"cloudapi4\",
                \"networks\": [
                    {
                        \"uuid\": \"$(sdc-napi /networks | json -H -c 'this.nic_tag=="admin"' 0.uuid)\"
                    }
                ]
            }
        }"
        if [[ -n "$ext_ip" ]]; then
            payload=$(echo "$payload" | json -e "this.params.networks.push({
                \"uuid\": \"$(sdc-napi /networks | json -H -c 'this.nic_tag=="external"' 0.uuid)\",
                \"ip\": \"$ext_ip\",
                \"primary\": true
            })")
        fi
        echo "$payload" | sapiadm provision

        res=$?
        if [ $res != 0 ]; then
            saw_err "Error creating $i zone"
{'fraction_non_alphanumeric': 0.14603372891942537, 'fraction_numerical': 0.014241099312929419, 'mean_word_length': 2.3709473684210525, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807094', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2713, 'n_words': 753}