diff options
Diffstat (limited to 'juju')
-rwxr-xr-x | juju/adapter.sh | 28 | ||||
-rwxr-xr-x | juju/juju_launch.sh | 167 | ||||
-rwxr-xr-x | juju/juju_setup.sh | 89 |
3 files changed, 284 insertions, 0 deletions
diff --git a/juju/adapter.sh b/juju/adapter.sh new file mode 100755 index 0000000..f4c2d52 --- /dev/null +++ b/juju/adapter.sh @@ -0,0 +1,28 @@ +#!/bin/bash +############################################################################## +# Copyright (c) 2017 HUAWEI TECHNOLOGIES CO.,LTD and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +function generate_compass_openrc() +{ + ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" + MGMT_IP=192.168.200.2 + exec_command="sshpass -p root ssh $ssh_options root@$MGMT_IP" + + RUN_DIR=$(eval "$exec_command ls /var/ansible/run") + + cmd="cat /var/ansible/run/$RUN_DIR/group_vars/all | grep -A 3 public_vip: | sed -n '2p' | sed -e 's/ ip: //g'" + PUBLIC_VIP=$(eval "$exec_command $cmd") + + echo -e "export OS_PASSWORD=console \n\ +export OS_TENANT_NAME=admin \n\ +export OS_AUTH_URL=http://$PUBLIC_VIP:35357/v2.0 \n\ +export OS_USERNAME=admin \n\ +export OS_VOLUME_API_VERSION=2 \n\ +export OS_REGION_NAME=RegionOne " > $WORK_DIR/admin-openrc.sh + +} diff --git a/juju/juju_launch.sh b/juju/juju_launch.sh new file mode 100755 index 0000000..10a35bb --- /dev/null +++ b/juju/juju_launch.sh @@ -0,0 +1,167 @@ +#!/bin/bash +############################################################################## +# Copyright (c) 2016-2017 HUAWEI TECHNOLOGIES CO.,LTD and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +ssh_options="-o StrictHostKeyChecking=no" + +function launch_juju_vm() +{ + NET_ID=$(neutron net-list | grep juju-net | awk '{print $2}') + + if [[ ! $(nova list | grep juju-client-vm) ]]; then + nova boot --flavor m1.small --image Xenial_x86_64 --nic net-id=$NET_ID \ + --key-name jump-key --security-group juju-default juju-client-vm + fi + + if [[ ! $(nova list | grep juju-metadata-vm) ]]; then + nova boot --flavor m1.small --image Xenial_x86_64 --nic net-id=$NET_ID \ + --key-name jump-key --security-group juju-default juju-metadata-vm + fi + + set +x + until [[ $(nova list | grep juju-metadata-vm | grep ACTIVE) ]] + do + sleep 1 + done + set -x + + if [ ! $(nova list | grep juju-client-vm | awk '{print $14}') ]; then + floating_ip_client=$(neutron floatingip-create ext-net | grep floating_ip_address | awk '{print $4}') + nova floating-ip-associate juju-client-vm $floating_ip_client + else + floating_ip_client=$(nova list | grep juju-client-vm | awk '{print $13}') + fi + + if [ ! $(nova list | grep juju-metadata-vm | awk '{print $14}') ]; then + floating_ip_metadata=$(neutron floatingip-create ext-net | grep floating_ip_address | awk '{print $4}') + nova floating-ip-associate juju-metadata-vm $floating_ip_metadata + else + floating_ip_metadata=$(nova list | grep juju-metadata-vm | awk '{print $13}') + fi + + sleep 60 + + export floating_ip_client=$floating_ip_client + export floating_ip_metadata=$floating_ip_metadata +} + +function exec_cmd_on_client() +{ + ssh $ssh_options ubuntu@$floating_ip_client "$@" +} + +function exec_cmd_on_metadata() +{ + ssh $ssh_options ubuntu@$floating_ip_metadata "$@" +} + +function juju_metadata_prepare() +{ + cmd="sudo apt update -y; \ + sudo apt-get install nginx -y" + exec_cmd_on_metadata $cmd + + if [ ! $(exec_cmd_on_metadata sudo ps -aux | grep nginx) ]; then + log_error "juju-metadata nginx error" + exit 1 + fi +} + +function juju_client_prepare() +{ + cmd1="sudo add-apt-repository ppa:juju/stable; \ + sudo apt update -y; \ + sudo apt install juju zfsutils-linux -y" + exec_cmd_on_client $cmd1 + + exec_cmd_on_client "echo 'clouds: + openstack: + type: openstack + auth-types: [access-key, userpass] + regions: + $OS_REGION_NAME: + endpoint: $OS_AUTH_URL' > clouds.yaml" + + cmd2="juju add-cloud openstack clouds.yaml --replace" + exec_cmd_on_client $cmd2 + + if [[ ! $(exec_cmd_on_client "juju list-clouds | grep openstack") ]]; then + log_error "juju-client add cloud error" + exit 1 + fi + + cmd3='ssh-keygen -q -t rsa -f /home/ubuntu/.ssh/id_rsa -N ""' + exec_cmd_on_client $cmd3 + + client_key=`exec_cmd_on_client sudo cat /home/ubuntu/.ssh/id_rsa.pub` + cmd4="echo $client_key >> /home/ubuntu/.ssh/authorized_keys" + exec_cmd_on_metadata $cmd4 + + exec_cmd_on_client "echo 'credentials: + openstack: + openstack: + auth-type: userpass + password: $OS_PASSWORD + tenant-name: $OS_TENANT_NAME + username: $OS_USERNAME' > os-creds.yaml" + + # credential uses keystone url V3 + cmd3="juju add-credential openstack -f os-creds.yaml --replace" + exec_cmd_on_client $cmd3 +} + +function juju_generate_metadata() +{ + exec_cmd_on_client mkdir -p mt + + for((i=0;i<${#JUJU_IMG_NAME[@]};i++)) + do + IMAGE_ID=$(glance image-list | grep ${JUJU_IMG_NAME[i]} | awk '{print $2}') + cmd="juju metadata generate-image -s ${JUJU_IMG_NAME[i]%%_*} -i $IMAGE_ID \ + -r $OS_REGION_NAME -d mt -u $OS_AUTH_URL" + exec_cmd_on_client $cmd + done + + cmd1="juju metadata generate-tools -d mt" + exec_cmd_on_client $cmd1 + + cmd2="rsync -e 'ssh $ssh_options' -av mt ubuntu@$floating_ip_metadata:~/" + exec_cmd_on_client $cmd2 + + cmd3="sudo cp -a mt/tools/ /var/www/html; \ + sudo cp -a mt/images/ /var/www/html; \ + sudo chmod a+rx /var/www/html/ -R" + exec_cmd_on_metadata $cmd3 + + wget -O - http://$floating_ip_metadata/images/streams/v1/index.json + if [ $? -ne 0 ]; then + log_error "juju metadata generate error" + exit 1 + fi +} + +function bootstrap_juju_controller() +{ + cmd="juju bootstrap openstack openstack \ + --config image-metadata-url=http://$floating_ip_metadata/images \ + --config network=juju-net \ + --verbose --debug" + exec_cmd_on_client $cmd +} + +function launch_juju() +{ + log_info "launch_juju enter" + + launch_juju_vm + juju_metadata_prepare + juju_client_prepare + juju_generate_metadata + bootstrap_juju_controller +} diff --git a/juju/juju_setup.sh b/juju/juju_setup.sh new file mode 100755 index 0000000..61e0152 --- /dev/null +++ b/juju/juju_setup.sh @@ -0,0 +1,89 @@ +#!/bin/bash +############################################################################## +# Copyright (c) 2016-2017 HUAWEI TECHNOLOGIES CO.,LTD and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +IMG_DIR=${WORK_DIR}/img + +function juju_env_prepare() +{ + mkdir -p ${WORK_DIR}/venv + sudo pip install --upgrade virtualenv + virtualenv ${WORK_DIR}/venv + + source ${WORK_DIR}/venv/bin/activate + pip install --upgrade python-openstackclient python-neutronclient +} + +function juju_download_img() +{ + if [ ! -e ${IMG_DIR}/${1##*/} ];then + wget -O ${IMG_DIR}/${1##*/} $1 + fi +} + +function juju_prepare() +{ + log_info "juju_prepare enter" + + mkdir -p $IMG_DIR + + for((i=0;i<${#JUJU_IMG_NAME[@]};i++)) + do + juju_download_img ${JUJU_IMG_URL[i]} + if [[ ! $(glance image-list | grep ${JUJU_IMG_NAME[i]}) ]]; then + glance image-create --name=${JUJU_IMG_NAME[i]} \ + --disk-format qcow2 --container-format=bare \ + --visibility=public --file ${IMG_DIR}/${JUJU_IMG_URL[i]##*/} + fi + done + + if [[ ! $(neutron net-list | grep juju-net) ]]; then + neutron net-create juju-net + fi + + if [[ ! $(neutron subnet-list | grep juju-subnet) ]]; then + neutron subnet-create juju-net $JUJU_NET_CIDR --name juju-subnet --gateway $JUJU_NET_GW \ + --dns_nameservers list=true 8.8.8.8 + fi + + if [[ ! $(neutron router-list | grep juju-router) ]]; then + neutron router-create juju-router + neutron router-interface-add juju-router juju-subnet + neutron router-gateway-set juju-router ext-net + fi + + if [[ ! $(neutron security-group-rule-list | grep "juju-default") ]]; then + neutron security-group-create juju-default --description "juju default security group" + fi + + if [[ ! $(neutron security-group-rule-list | grep juju-default | grep "icmp") ]]; then + neutron security-group-rule-create --direction ingress --protocol icmp \ + --remote-ip-prefix 0.0.0.0/0 juju-default + fi + + if [[ ! $(neutron security-group-rule-list | grep juju-default | grep "22/tcp") ]]; then + neutron security-group-rule-create --direction ingress --protocol tcp \ + --port_range_min 22 --port_range_max 22 \ + --remote-ip-prefix 0.0.0.0/0 juju-default + fi + + if [[ ! $(neutron security-group-rule-list | grep juju-default | grep "80/tcp") ]]; then + neutron security-group-rule-create --direction ingress --protocol tcp \ + --port_range_min 80 --port_range_max 80 \ + --remote-ip-prefix 0.0.0.0/0 juju-default + fi + + openstack keypair list | grep jump-key || openstack keypair create --public-key ~/.ssh/id_rsa.pub jump-key + + openstack flavor show m1.tiny || openstack flavor create --ram 512 --disk 5 --vcpus 1 --public m1.tiny + openstack flavor show m1.small || openstack flavor create --ram 1024 --disk 10 --vcpus 1 --public m1.small + openstack flavor show m1.medium || openstack flavor create --ram 2048 --disk 10 --vcpus 2 --public m1.medium + openstack flavor show m1.large || openstack flavor create --ram 3072 --disk 10 --vcpus 2 --public m1.large + openstack flavor show m1.xlarge || openstack flavor create --ram 8096 --disk 30 --vcpus 4 --public m1.xlarge +} |