Last active
August 16, 2016 10:35
-
-
Save tsing/d5d769f3c752ec7b1ce3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
role_controller() { | |
[ "${Role}" == "controller" ] && return 0 | |
return 1 | |
} | |
role_agent() { | |
[ "${Role}" == "agent" ] && return 0 | |
return 1 | |
} | |
gen_authkey() { | |
head -n 100 /dev/urandom|tr -dc 'a-zA-Z0-9'|head -c 32 | |
} | |
get_inetcfg() { | |
local inet="${1}" ip= gateway= dns= | |
ip=$( ip -d -o -f inet -4 -s addr 2>&- | awk '($2=="'${inet}'"){print $4;exit;}' ) | |
gateway=$( route -n 2>&- | awk '($1=="0.0.0.0" && $4~/UG/){print $2;exit;}' ) | |
dns=$( awk '(NF==2 && $1=="nameserver"){print $2;exit}' /etc/resolv.conf 2>&- ) | |
[ -z "${dns}" ] && dns="${gateway}" | |
cat << EOF | |
[Network] | |
DHCP=no | |
Address=${ip} | |
Gateway=${gateway} | |
DNS=${dns} | |
EOF | |
return 0 | |
} | |
gen_cloudconfig() { | |
local tmp= | |
## section hostname, users, coreos(units) | |
cat << EOF | |
#cloud-config | |
hostname: ${Role}-${EtcdName} | |
users: | |
- name: ${DefaultUser} | |
passwd: ${Password} | |
groups: | |
- sudo | |
- docker | |
- wheel | |
- systemd-journal | |
- portage | |
coreos: | |
update: | |
group: stable | |
reboot-strategy: off | |
server: http://upgrade.csphere.cn/update | |
units: | |
- name: settimezone.service | |
command: start | |
enable: true | |
content: | | |
[Unit] | |
Description=Set Time Zone +0800 | |
[Service] | |
ExecStart=/usr/bin/timedatectl set-timezone Asia/Shanghai | |
RemainAfterExit=yes | |
Type=oneshot | |
- name: docker.service | |
enable: false | |
EOF | |
# setup startup services | |
if role_controller; then | |
cat <<EOF | |
- name: csphere-prepare.service | |
command: start | |
enable: true | |
- name: csphere-backup.service | |
command: start | |
enable: true | |
- name: csphere-backup.timer | |
command: start | |
enable: true | |
- name: ntpd.service | |
command: start | |
enable: true | |
- name: csphere-mongodb.service | |
command: start | |
enable: true | |
- name: csphere-etcd2-controller.service | |
command: start | |
enable: true | |
- name: csphere-docker-controller.service | |
command: start | |
enable: true | |
EOF | |
if [ "${MONGOREPL}" == "YES" ]; then | |
cat <<EOF | |
- name: csphere-prometheus.service | |
enable: false | |
- name: csphere-controller.service | |
enable: false | |
- name: csphere-agent.service | |
command: start | |
enable: true | |
EOF | |
else | |
cat <<EOF | |
- name: csphere-prometheus.service | |
command: start | |
enable: true | |
- name: csphere-controller.service | |
command: start | |
enable: true | |
- name: csphere-agent.service | |
command: start | |
enable: true | |
EOF | |
fi | |
elif role_agent; then | |
cat <<EOF | |
- name: csphere-prepare.service | |
command: start | |
enable: true | |
- name: systemd-timesyncd.service | |
command: start | |
enable: true | |
- name: csphere-etcd2-agent.service | |
command: start | |
enable: true | |
- name: csphere-skydns.service | |
command: start | |
enable: true | |
- name: csphere-dockeripam.service | |
command: start | |
enable: true | |
- name: csphere-docker-agent.service | |
command: start | |
enable: true | |
- name: csphere-agent.service | |
command: start | |
enable: true | |
EOF | |
fi | |
# append inet config | |
local tmp=$( get_inetcfg eth0 ) | |
tmp=$(echo -e "${tmp}" | sed -e 's/^/ /') | |
cat <<EOF | |
- name: eth0-static.network | |
runtime: false | |
content: | | |
[Match] | |
Name=eth0 | |
[Network] | |
Bridge=br0 | |
- name: br0-static.network | |
runtime: false | |
content: | | |
[Match] | |
Name=br0 | |
${tmp} | |
EOF | |
# section write_files | |
cat <<EOF | |
write_files: | |
- path: /etc/csphere/inst-opts.env | |
permissions: 0644 | |
owner: root | |
content: | | |
COS_ROLE=${Role} | |
COS_CONTROLLER=${Controller} | |
COS_CONTROLLER_PORT=${ControllerPort} | |
COS_AUTH_KEY=${AuthKey} | |
COS_INST_CODE=${InstCode} | |
COS_DISCOVERY_URL=${DiscoveryUrl} | |
COS_SVRPOOL_ID=${SvrPoolID} | |
COS_CLUSTER_SIZE=${ClusterSize} | |
COS_ETCD_NAME=${Role}-${EtcdName} | |
COS_NETMODE=${NetMode} | |
COS_INETDEV=${InetDev} | |
COS_MONGOREPL=${MONGOREPL} | |
COS_CUSTOM_DOCKERGW= | |
COS_CUSTOM_DOCKERDNS= | |
- path: /etc/csphere/csphere-prepare.bash | |
permissions: 0744 | |
owner: root | |
content: | | |
#!/bin/sh | |
/usr/lib/csphere/etc/bin/csphere-prepare.bash | |
content=\$(cat /etc/csphere/csphere-docker-agent.env) | |
source /etc/csphere/csphere-public.env | |
echo "DOCKER_START_OPTS=daemon --iptables=true --ip-forward=true --storage-driver=overlay --cluster-store=etcd://127.0.0.1:2379 --cluster-advertise=\${LOCAL_IP}:0" > /etc/csphere/csphere-docker-agent.env | |
sed -i 's/^DEFAULT_NETWORK=.*/DEFAULT_NETWORK=${OverlayNetwork}/g' /etc/csphere/csphere-agent.env | |
- path: /etc/csphere/create-network.sh | |
permissions: 0744 | |
owner: root | |
content: | | |
#!/bin/sh | |
SUBNET=\$1 | |
IP_START=\$2 | |
IP_END=\$3 | |
net-plugin ip-range --ip-start=\${IP_START}/24 --ip-end=\${IP_END}/24 | |
systemctl restart csphere-dockeripam | |
docker network create -d overlay --ipam-driver=csphere --subnet=\${SUBNET} ${OverlayNetwork} | |
EOF | |
} | |
Role=${ROLE:-agent} | |
Password=${PASSWORD:-cos} | |
Password="$( openssl passwd -1 "${Password}" 2>/dev/null)" | |
OverlayNetwork=${OVERLAY:-qingcloud} | |
ClusterSize=${CLUSTER_SIZE:-3} | |
MONGOREPL=${MONGOREPL:-NO} | |
AuthKey= | |
InstCode="" | |
SvrPoolID="" | |
if role_agent; then | |
Controller=${CONTROLLER} | |
InstCode=${CODE} | |
else | |
Controller=127.0.0.1:80 | |
SvrPoolID="csphere-internal" | |
AuthKey="$(gen_authkey 2>&-)" | |
fi | |
ControllerPort=80 | |
InetDev=eth0 | |
NetMode=bridge | |
DiscoveryUrl="http://${Controller%%:*}:2379/v2/keys/discovery/hellocsphere" | |
DefaultUser=cos | |
EtcdName=$(mktemp -u XXXX) | |
mkdir -p /var/lib/coreos-install/ | |
gen_cloudconfig > /var/lib/coreos-install/user_data | |
echo "Will apply cloudconfig and reboot....." | |
nohup coreos-cloudinit --from-file=/var/lib/coreos-install/user_data; reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment