<commit_before>#!/bin/bash
#
# Script: instal-chef.sh
#
# Copyright (c) 2011 Dell Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Note : RED HAT 5.6 WORK IN PROGRESS !!!
#        Still a couple of unresolved issues with this script.
#        Search for FIXME to to find these

export FQDN="$1"
export PATH="/opt/dell/bin:$PATH"
die() { echo "$(date '+%F %T %z'): $@"; exit 1; }

# mac address and IP address matching routines
mac_match_re='link/ether ([0-9a-fA-F:]+)'
ip_match_re='inet ([0-9.]+)'
get_ip_and_mac() {
    local ip_line=$(ip addr show $1)
    [[ $ip_line =~ $mac_match_re ]] && MAC=${BASH_REMATCH[1]} || unset MAC
    [[ $ip_line =~ $ip_match_re ]] && IP=${BASH_REMATCH[1]} || unset IP
}

crowbar_up=

# Run a command and log its output.
log_to() {
    # $1 = install log to log to
    # $@ = rest of args
    local __logname="$1" _ret=0
    local __log="/var/log/install-$1"
    local __timestamp="$(date '+%F %T %z')"
    local log_skip_re='^gem|knife$'
    shift
    printf "\n%s\n" "$__timestamp: Running $*" | \
	tee -a "$__log.err" >> "$__log.log"
    "$@" 2>> "$__log.err" >>"$__log.log" || {
	_ret=$?
	if ! [[ $__logname =~ $log_skip_re ]]; then
	    echo "$__timestamp: $* failed."
	    echo "See $__log.log and $__log.err for more information."
	fi
    }
    printf "\n$s\n--------\n"  "$(date '+%F %T %z'): Done $*" | \
	tee -a "$__log.err" >> "$__log.log"
    return $_ret
}

chef_or_die() {
    log_to chef blocking_chef_client.sh && return
    if [[ $crowbar_up && $FQDN ]]; then
	crowbar crowbar transition "$FQDN" problem
    fi
    # If we were left without an IP address, rectify that.
    ip link set eth0 up
    ip addr add 192.168.124.10/24 dev eth0
    die "$@"
}

# Keep trying to start a service in a loop.
# $1 = service to restart
# $2 = status messae to print.
restart_svc_loop() {
    while service "$1" status | grep -qi fail
    do
        echo "$(date '+%F %T %z'): $2..."
	log_to svc service "$1" start
	sleep 1
    done
}

# Make sure there is something of a domain name
DOMAINNAME=${FQDN#*.}
[[ $DOMAINNAME = $FQDN || $DOMAINNAME = ${DOMAINNAME#*.} ]] && \
    die "Please specify an FQDN for the admin name"

# Setup hostname from config file
echo "$(date '+%F %T %z'): Setting Hostname..."
update_hostname.sh $FQDN

# Set up our eth0 IP address way in advance.
# Deploying Crowbar should also do this for us, but sometimes it does not.
# When it does not, things get hard to debug pretty quick.
ip link set eth0 up
ip addr add 192.168.124.10/24 dev eth0

# Replace the domainname in the default template
DOMAINNAME=$(dnsdomainname)
sed -i "s/pod.cloud.openstack.org/$DOMAINNAME/g" /opt/dell/chef/data_bags/crowbar/bc-template-dns.json

# once our hostname is correct, bounce rsyslog to let it know.
log_to svc service rsyslog restart

# Make sure we only try to install x86_64 packages.
echo 'exclude = *.i386' >>/etc/yum.conf

#
# Install the base rpm packages
#
echo "$(date '+%F %T %z'): Installing Chef Server..."
log_to yum yum -q -y update

# Install the rpm and gem packages
log_to yum yum -q -y install rubygem-chef-server rubygem-kwalify ruby-devel \
    curl-devel gcc make ruby-shadow

# Install ruby gems
echo "$(date '+%F %T %z'): Installing Gems..."
for ((n=0; n<3; n++)); do
    for g in gems/*.gem; do
	echo "install $g"
	log_to gem gem install --local --no-ri --no-rdoc "$g"
    done
    sync
    sleep 5
done

echo "$(date '+%F %T %z'): Building Keys..."
# Generate root's SSH pubkey
if [ ! -e /root/.ssh/id_rsa ] ; then
  log_to keys ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
fi

# add our own key to authorized_keys
cat /root/.ssh/id_rsa.pub >>/root/.ssh/authorized_keys

########## FIXME
# Need to create the redhat-install area with keys or
# use the ubuntu copies.
#########################################################
# and trick Chef into pushing it out to everyone.
cp /root/.ssh/authorized_keys \
    /opt/dell/chef/cookbooks/redhat-install/files/default/authorized_keys

# generate the machine install username and password
if [[ ! -e /etc/crowbar.install.key ]]; then
    dd if=/dev/urandom bs=65536 count=1 2>/dev/null |sha512sum - 2>/dev/null | \
	(read key rest; echo "machine-install:$key" >/etc/crowbar.install.key)
    export CROWBAR_KEY=$(cat /etc/crowbar.install.key)
    printf "${CROWBAR_KEY%%:*}:Crowbar:${CROWBAR_KEY##*:}" |md5sum - | (
	read key rest
	printf "\n${CROWBAR_KEY%%:*}:Crowbar:$key\n" >> \
	    /opt/dell/openstack_manager/htdigest)
else
  export CROWBAR_KEY=$(cat /etc/crowbar.install.key)
fi

# Crowbar will hack up the pxeboot files appropriatly.
# Set Version in Crowbar UI
VERSION=$(cat /opt/.dell-install/Version)
sed -i "s/CROWBAR_VERSION = .*/CROWBAR_VERSION = \"${VERSION:=Dev}\"/" \
    /opt/dell/openstack_manager/config/environments/production.rb

./start-chef-server.sh
<commit_msg>Make sure that the machine key gets set correctly.<commit_after>#!/bin/bash
#
# Script: instal-chef.sh
#
# Copyright (c) 2011 Dell Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Note : RED HAT 5.6 WORK IN PROGRESS !!!
#        Still a couple of unresolved issues with this script.
#        Search for FIXME to to find these

export FQDN="$1"
export PATH="/opt/dell/bin:$PATH"
die() { echo "$(date '+%F %T %z'): $@"; exit 1; }

# mac address and IP address matching routines
mac_match_re='link/ether ([0-9a-fA-F:]+)'
ip_match_re='inet ([0-9.]+)'
get_ip_and_mac() {
    local ip_line=$(ip addr show $1)
    [[ $ip_line =~ $mac_match_re ]] && MAC=${BASH_REMATCH[1]} || unset MAC
    [[ $ip_line =~ $ip_match_re ]] && IP=${BASH_REMATCH[1]} || unset IP
}

crowbar_up=

# Run a command and log its output.
log_to() {
    # $1 = install log to log to
    # $@ = rest of args
    local __logname="$1" _ret=0
    local __log="/var/log/install-$1"
    local __timestamp="$(date '+%F %T %z')"
    local log_skip_re='^gem|knife$'
    shift
    printf "\n%s\n" "$__timestamp: Running $*" | \
	tee -a "$__log.err" >> "$__log.log"
    "$@" 2>> "$__log.err" >>"$__log.log" || {
	_ret=$?
	if ! [[ $__logname =~ $log_skip_re ]]; then
	    echo "$__timestamp: $* failed."
	    echo "See $__log.log and $__log.err for more information."
	fi
    }
    printf "\n$s\n--------\n"  "$(date '+%F %T %z'): Done $*" | \
	tee -a "$__log.err" >> "$__log.log"
    return $_ret
}

chef_or_die() {
    log_to chef blocking_chef_client.sh && return
    if [[ $crowbar_up && $FQDN ]]; then
	crowbar crowbar transition "$FQDN" problem
    fi
    # If we were left without an IP address, rectify that.
    ip link set eth0 up
    ip addr add 192.168.124.10/24 dev eth0
    die "$@"
}

# Keep trying to start a service in a loop.
# $1 = service to restart
# $2 = status messae to print.
restart_svc_loop() {
    while service "$1" status | grep -qi fail
    do
        echo "$(date '+%F %T %z'): $2..."
	log_to svc service "$1" start
	sleep 1
    done
}

# Make sure there is something of a domain name
DOMAINNAME=${FQDN#*.}
[[ $DOMAINNAME = $FQDN || $DOMAINNAME = ${DOMAINNAME#*.} ]] && \
    die "Please specify an FQDN for the admin name"

# Setup hostname from config file
echo "$(date '+%F %T %z'): Setting Hostname..."
update_hostname.sh $FQDN

# Set up our eth0 IP address way in advance.
# Deploying Crowbar should also do this for us, but sometimes it does not.
# When it does not, things get hard to debug pretty quick.
ip link set eth0 up
ip addr add 192.168.124.10/24 dev eth0

# Replace the domainname in the default template
DOMAINNAME=$(dnsdomainname)
sed -i "s/pod.cloud.openstack.org/$DOMAINNAME/g" /opt/dell/chef/data_bags/crowbar/bc-template-dns.json

# once our hostname is correct, bounce rsyslog to let it know.
log_to svc service rsyslog restart

# Make sure we only try to install x86_64 packages.
echo 'exclude = *.i386' >>/etc/yum.conf

#
# Install the base rpm packages
#
echo "$(date '+%F %T %z'): Installing Chef Server..."
log_to yum yum -q -y update

# Install the rpm and gem packages
log_to yum yum -q -y install rubygem-chef-server rubygem-kwalify ruby-devel \
    curl-devel gcc make ruby-shadow

# Install ruby gems
echo "$(date '+%F %T %z'): Installing Gems..."
for ((n=0; n<3; n++)); do
    for g in gems/*.gem; do
	echo "install $g"
	log_to gem gem install --local --no-ri --no-rdoc "$g"
    done
    sync
    sleep 5
done

echo "$(date '+%F %T %z'): Building Keys..."
# Generate root's SSH pubkey
if [ ! -e /root/.ssh/id_rsa ] ; then
  log_to keys ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
fi

# add our own key to authorized_keys
cat /root/.ssh/id_rsa.pub >>/root/.ssh/authorized_keys

########## FIXME
# Need to create the redhat-install area with keys or
# use the ubuntu copies.
#########################################################
# and trick Chef into pushing it out to everyone.
cp /root/.ssh/authorized_keys \
    /opt/dell/chef/cookbooks/redhat-install/files/default/authorized_keys

# generate the machine install username and password
if [[ ! -e /etc/crowbar.install.key ]]; then
    dd if=/dev/urandom bs=65536 count=1 2>/dev/null |sha512sum - 2>/dev/null | \
	(read key rest; echo "machine-install:$key" >/etc/crowbar.install.key)
    export CROWBAR_KEY=$(cat /etc/crowbar.install.key)
    printf "${CROWBAR_KEY%%:*}:Crowbar:${CROWBAR_KEY##*:}" |md5sum - | (
	read key rest
	printf "\n${CROWBAR_KEY%%:*}:Crowbar:$key\n" >> \
	    /opt/dell/openstack_manager/htdigest)
else
  export CROWBAR_KEY=$(cat /etc/crowbar.install.key)
fi
sed -i "s/machine_password/${CROWBAR_KEY##*:}/g" \
    /opt/dell/chef/data_bags/crowbar/bc-template-crowbar.json

# Crowbar will hack up the pxeboot files appropriatly.
# Set Version in Crowbar UI
VERSION=$(cat /opt/.dell-install/Version)
sed -i "s/CROWBAR_VERSION = .*/CROWBAR_VERSION = \"${VERSION:=Dev}\"/" \
    /opt/dell/openstack_manager/config/environments/production.rb

./start-chef-server.sh

{'fraction_non_alphanumeric': 0.1585003711952487, 'fraction_numerical': 0.01540460282108389, 'mean_word_length': 3.4005716619028177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '5428045', 'n_tokens_mistral': 4031, 'n_tokens_neox': 3781, 'n_words': 1244}