Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature centos7 wakame init #771

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions rpmbuild/SPECS/wakame-init-centos7.spec
@@ -0,0 +1,71 @@
%define _vdc_git_uri git://github.com/axsh/wakame-vdc.git
%define oname wakame-init

# * rpmbuild -bb ./wakame-init.spec \
# --define "release_tag [ tag ]"
# --define "version_tag [ tag ]"
# --define "build_id $(../helpers/gen-release-id.sh)"
# --define "build_id $(../helpers/gen-release-id.sh [ commit-hash ])"
# --define "repo_uri git://github.com/axsh/wakame-vdc.git"

%define version_id 16.1
%define release_id 1.daily
%{?version_tag:%define version_id %{version_tag}}
%{?build_id:%define release_id %{build_id}}
%{?release_tag:%define release_id %{release_tag}}
%{?repo_uri:%define _vdc_git_uri %{repo_uri}}

Name: %{oname}
Version: %{version_id}
Release: %{release_id}%{?dist}
Summary: sysvinit script set for wakame custom image.
Group: Development/Languages
Vendor: Axsh Co. LTD <dev@axsh.net>
URL: http://wakame-vdc.org/
Source: %{_vdc_git_uri}
License: see https://github.com/axsh/wakame-vdc/blob/master/README.md
BuildArch: noarch

%description
Initialize virtual machine settings.

## rpmbuild -bp
%prep
mkdir -p %{name}-%{version}
[ -d %{name}-%{version} ] && rm -rf %{name}-%{version}
git clone %{_vdc_git_uri} %{name}-%{version}
cd %{name}-%{version}
[ -z "%{build_id}" ] || {
build_id=%{build_id}
git checkout ${build_id##*git}
unset build_id
} && :

%setup -T -D

## rpmbuild -bc
%build

## rpmbuild -bi
%install
[ -d ${RPM_BUILD_ROOT} ] && rm -rf ${RPM_BUILD_ROOT}
mkdir -p ${RPM_BUILD_ROOT}/etc/init.d/
mkdir -p ${RPM_BUILD_ROOT}/etc/default/
rsync -aHA `pwd`/wakame-init/rhel/7/wakame-init ${RPM_BUILD_ROOT}/etc/
rsync -aHA `pwd`/wakame-init/rhel/7/init.d/wakame-init ${RPM_BUILD_ROOT}/etc/init.d/
rsync -aHA `pwd`/wakame-init/rhel/7/default/wakame-init ${RPM_BUILD_ROOT}/etc/default/

%clean
rm -rf ${RPM_BUILD_ROOT}

%post
/sbin/chkconfig --add wakame-init
/sbin/chkconfig wakame-init on

%files
%defattr(-,root,root)
/etc/wakame-init
/etc/init.d/wakame-init
%config(noreplace) /etc/default/wakame-init

%changelog
5 changes: 5 additions & 0 deletions wakame-init/rhel/7/default/wakame-init
@@ -0,0 +1,5 @@
## wakame-init params
#USER=root
#IPV6INIT=no
#IPV6_AUTOCONF=no
#METADATA_LOCATION=drive
50 changes: 50 additions & 0 deletions wakame-init/rhel/7/init.d/wakame-init
@@ -0,0 +1,50 @@
#!/bin/bash
#
# /etc/rc.d/init.d/wakame-init
#
#
# chkconfig: 2345 9 91
# description: initialize virtual machine settings.
# processname: wakame-init

# Source function library.
. /etc/rc.d/init.d/functions

[ -f /etc/default/wakame-init ] && . /etc/default/wakame-init

RETVAL=0

# valiable
USER=${USER:-"root"}
IPV6INIT=${IPV6INIT:-"no"}
IPV6_AUTOCONF=${IPV6_AUTOCONF:-"no"}
METADATA_LOCATION=${METADATA_LOCATION:-"drive"}

#
# See how we were called.
#

start() {
USER=${USER} IPV6INIT=${IPV6INIT} IPV6_AUTOCONF=${IPV6_AUTOCONF} METADATA_LOCATION=${METADATA_LOCATION} /etc/wakame-init
RETVAL=$?
return $RETVAL
}

stop() {
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage: $0 {start|stop}"
RETVAL=2
esac

exit ${RETVAL}

246 changes: 246 additions & 0 deletions wakame-init/rhel/7/wakame-init
@@ -0,0 +1,246 @@
#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin
LANG=C
LC_ALL=C

#set -e

## variables

LOGFILE=/var/log/$(basename $0).log
USER=${USER:-root}
SSHDIR=$(getent passwd ${USER} | awk -F: '{print $6}')/.ssh/
KEYSPATH=${SSHDIR}/authorized_keys

METADATA_LOCATION=${METADATA_LOCATION:-drive}
METADATA_DRIVE_MOUNTPOINT=/metadata

## functions

function get_metadata_from_server() {
local param=$1
curl -s --retry 3 http://169.254.169.254/latest/meta-data/${param}
}

function get_metadata_from_drive() {
local param=$1
local param_path=${METADATA_DRIVE_MOUNTPOINT}/meta-data/${param}

if [ -d ${param_path} ]; then
ls ${param_path}
elif [ -f ${param_path} ]; then
cat ${param_path}
else
exit 1
fi
}

function get_metadata() {
local param=$1
for i in {1..10}; do
get_metadata_from_${METADATA_LOCATION} ${param} && exit 0
sleep 3
done
exit 1
}

function logger() {
tee -a ${LOGFILE}
}

### mount metadata drive

function set_mount_metadata_drive() {
if [[ -f /proc/vz/veinfo ]]; then
# OpenVZ mounts metadata drive from the outside script.
METADATA_DRIVE_MOUNTPOINT="/metadata"
else
mount_output=`mount -l | grep -w METADATA | cut -d " " -f3`
[[ -z "${mount_output}" ]] || {
# already mounted
METADATA_DRIVE_MOUNTPOINT="${mount_output}"
}
fi

if mountpoint -q "${METADATA_DRIVE_MOUNTPOINT}"; then
echo "Metadata drive already mounted on: ${METADATA_DRIVE_MOUNTPOINT}"
else
if [ ! -d ${METADATA_DRIVE_MOUNTPOINT} ]; then
echo "Creating directory: ${METADATA_DRIVE_MOUNTPOINT}"
mkdir ${METADATA_DRIVE_MOUNTPOINT}
fi
echo "Mounting metadata drive on: ${METADATA_DRIVE_MOUNTPOINT}"
mount LABEL=METADATA ${METADATA_DRIVE_MOUNTPOINT} || {
echo "no such labeled device: METADATA"
exit 1
}
echo "Mounted metadata drive successfully on: ${METADATA_DRIVE_MOUNTPOINT}"
fi
}

### Set up the host name

function set_host_name() {
HN=`get_metadata local-hostname`
if [ -n "$HN" ]; then
echo "Setting the hostname"
grep -q "HOSTNAME=$HN" /etc/sysconfig/network
if [ $? -ne 0 ]; then
hostname $HN
sed -i "s/HOSTNAME=.*/HOSTNAME=$HN/" /etc/sysconfig/network
fi
grep -q "$HN" /etc/hostname
if [ $? -ne 0 ]; then
echo $HN > /etc/hostname
fi

# Add it to the hosts file if not there yet
grep -q "$HN" /etc/hosts
if [ $? -ne 0 ]; then
sed -i "/127.0.0.1.*localhost/a\127.0.0.1 $HN" /etc/hosts
fi
fi
}

### Set up the authorized keys for the users to login

function set_authorized_keys() {
KEYS=`get_metadata public-keys/0/openssh-key`
if [ -n "$KEYS" ]; then
echo "Setting authorized keys"
[ -d ${SSHDIR} ] || {
mkdir -m 700 ${SSHDIR}
chown ${USER}:${USER} ${SSHDIR}
}
# Check if the keys are already authorized
[ -f $KEYSPATH ] && grep -q "$KEYS" $KEYSPATH
if [ $? -ne 0 ]; then
echo $KEYS >> $KEYSPATH
chmod 600 $KEYSPATH
chown ${USER} $KEYSPATH
fi
fi
}

### Generate ssh host keys

function set_generate_ssh_host_keys() {
if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
echo "Generating DSA host key"
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
fi
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
echo "Generating RSA host key"
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
fi
}

### Set up network configuration

function set_network_configuration() {
echo "Detecting default gateway device."
gw_if_mac=$(
for macaddr in $(get_metadata network/interfaces/macs/); do
mac_path=network/interfaces/macs/${macaddr%%/}
metric=`get_metadata ${mac_path}/x-metric`
echo ${metric} ${macaddr}
done | sort -n -k 1 | head -1 | while read metric macaddr; do
echo ${macaddr}
done
)
if [[ -n "$gw_if_mac" ]]; then
echo "Detected gateway device is ${gw_if_mac}"
else
echo "None of gateway device is detected"
fi

for macaddr in $(get_metadata network/interfaces/macs/); do
mac_path=network/interfaces/macs/${macaddr%%/}
mac=`get_metadata ${mac_path}/mac`

IFS=/ read xempty xsys xclass xnet nic therest <<<"$(grep "$mac" /sys/class/net/*/address)"

ip=`get_metadata ${mac_path}/local-ipv4s`
broadcast=`get_metadata ${mac_path}/x-broadcast`

gateway=`get_metadata ${mac_path}/x-gateway`
metric=`get_metadata ${mac_path}/x-metric`
netmask=`get_metadata ${mac_path}/x-netmask`
network=`get_metadata ${mac_path}/x-network`

cat <<_IFCFG > "/etc/sysconfig/network-scripts/ifcfg-${nic}"
DEVICE="${nic}"
BOOTPROTO="static"
HWADDR="${mac}"
IPV6INIT="${IPV6INIT:-yes}"
IPV6_AUTOCONF="${IPV6_AUTOCONF:-yes}"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR="${ip}"
NETMASK="${netmask}"
_IFCFG

[ -n "$gw_if_mac" -a "$macaddr" = "$gw_if_mac" -a -n "$gateway" ] && {
cat <<_IFCFG >> "/etc/sysconfig/network-scripts/ifcfg-${nic}"
GATEWAY="${gateway}"
_IFCFG
}

done
}

### Setup etc hosts.

function set_extra_hosts() {
host_path="${METADATA_DRIVE_MOUNTPOINT}/meta-data/extra-hosts"
if [ -d ${host_path} ]; then
comment="# Please do not modify lines from here by your hand since wakame-init will place entries from metadata."
egrep -w -q "^${comment}$" /etc/hosts && {
sed -i -n "1,/^${comment}$/p" /etc/hosts
} || {
echo "${comment}" >> /etc/hosts
}
for i in `ls ${host_path}/*`; do
hostname=`basename $i`
hostip=`cat $i`
echo "${hostip} ${hostname}" >> /etc/hosts
done
chmod 0644 /etc/hosts
fi
}

### Add the metadata server to the routing table

# function set_routing_table() {
# for i in {1..1200}; do
# DEFAULT_GW=`ip route get 8.8.8.8 | head -n 1 | cut -d ' ' -f3`
# echo ... ${i} DEFAULT_GW=${DEFAULT_GW}
# [ -z "${DEFAULT_GW}" ] || break
# sleep 3
# done
# [ -z ${DEFAULT_GW} ] || route add 169.254.169.254 gateway $DEFAULT_GW
# }

function set_configuration() {
case "$METADATA_LOCATION" in
drive)
set_mount_metadata_drive
set_network_configuration
;;
*)
;;
esac
set_host_name
set_authorized_keys
set_generate_ssh_host_keys
set_extra_hosts
}

## exec

set_configuration | logger

# Important for remote storage.
sync