Commit 0d4ddf27 authored by Oceans's avatar Oceans

add swarm

parent fd6f2491
#! /usr/bin/env python
# Copyright 2017 --Yang Hu--
#
# 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.
__author__ = 'Yang Hu'
import paramiko, os
import threading
def install_engine(vm):
try:
print "%s: ====== Start Docker Engine Installing ======" % (vm.ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(vm.ip, username=vm.user, key_filename=vm.key)
sftp = ssh.open_sftp()
sftp.chdir('/tmp/')
file_path = os.path.dirname(os.path.abspath(__file__))
install_script = file_path + "/" + "docker_engine.sh"
sftp.put(install_script, "engine_setup.sh")
stdin, stdout, stderr = ssh.exec_command("sudo sh /tmp/engine_setup.sh")
stdout.read()
print "%s: ========= Docker Engine Installed =========" % (vm.ip)
except Exception as e:
print '%s: %s' % (vm.ip, e)
return "ERROR:"+vm.ip+" "+str(e)
ssh.close()
return "SUCCESS"
def run(vm_list):
for i in vm_list:
ret = install_engine(i)
if "ERROR" in ret: return ret
return "SUCCESS"
\ No newline at end of file
#! /bin/bash
sudo apt-get remove docker docker-engine
sudo apt-get update
sudo apt-get -y install linux-image-extra-$(uname -r) linux-image-extra-virtual
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get -y install docker-ce
...@@ -71,19 +71,26 @@ def install_worker(join_cmd, vm): ...@@ -71,19 +71,26 @@ def install_worker(join_cmd, vm):
print '%s: %s' % (vm.ip, e) print '%s: %s' % (vm.ip, e)
return "ERROR:"+vm.ip+" "+str(e) return "ERROR:"+vm.ip+" "+str(e)
ssh.close() ssh.close()
return "" return "SUCCESS"
def run(vm_list): def run(vm_list):
for i in vm_list: for i in vm_list:
if i.role == "master": join_cmd = install_manager(i) if i.role == "master":
join_cmd = install_manager(i)
if "ERROR" in join_cmd: if "ERROR" in join_cmd:
return join_cmd return join_cmd
else: else:
join_cmd = join_cmd.encode() join_cmd = join_cmd.encode()
join_cmd = join_cmd.strip() join_cmd = join_cmd.strip()
break
for i in vm_list: for i in vm_list:
if i.role == "slave": worker_cmd = install_worker(join_cmd, i) if i.role == "slave":
worker_cmd = install_worker(join_cmd, i)
if "ERROR" in worker_cmd: if "ERROR" in worker_cmd:
return worker_cmd return worker_cmd
return ""
\ No newline at end of file kuber_file = open(file_path + "/admin.conf", "r")
kuber_string = kuber_file.read()
kuber_file.close()
return kuber_string
\ No newline at end of file
#! /usr/bin/env python
# Copyright 2017 --Yang Hu--
#
# 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.
__author__ = 'Yang Hu'
import paramiko, os
from vm_info import VmInfo
def install_manager(vm):
try:
print "%s: ====== Start Swarm Manager Installing ======" % (vm.ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(vm.ip, username=vm.user, key_filename=vm.key)
stdin, stdout, stderr = ssh.exec_command("sudo docker swarm init --advertise-addr eth0")
retstr = stdout.readlines()
print "%s: ========= Swarm Manager Installed =========" % (vm.ip)
except Exception as e:
print '%s: %s' % (vm.ip, e)
return "ERROR:"+vm.ip+" "+str(e)
ssh.close()
return retstr[4] + retstr[5] + retstr[6]
def install_worker(join_cmd, vm):
try:
print "%s: ====== Start Swarm Worker Installing ======" % (vm.ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(vm.ip, username=vm.user, key_filename=vm.key)
stdin, stdout, stderr = ssh.exec_command("sudo %s" % (join_cmd))
stdout.read()
print "%s: ========= Swarm Worker Installed =========" % (vm.ip)
except Exception as e:
print '%s: %s' % (vm.ip, e)
return "ERROR:"+vm.ip+" "+str(e)
ssh.close()
return "SUCCESS"
def run(vm_list):
for i in vm_list:
if i.role == "master":
join_cmd = install_manager(i)
if "ERROR" in join_cmd:
return join_cmd
else:
join_cmd = join_cmd.encode()
join_cmd = join_cmd.replace("\n" , "")
join_cmd = join_cmd.replace("\\" , "")
join_cmd = join_cmd.strip()
swarm_file = open(i.key)
swarm_string = swarm_file.read()
swarm_file.close()
break
for i in vm_list:
if i.role == "slave":
worker_cmd = install_worker(join_cmd, i)
if "ERROR" in worker_cmd:
return worker_cmd
return swarm_string
\ No newline at end of file
...@@ -6,6 +6,8 @@ import time ...@@ -6,6 +6,8 @@ import time
from vm_info import VmInfo from vm_info import VmInfo
import docker_kubernetes import docker_kubernetes
import docker_engine
import docker_swarm
connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.3')) connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.3'))
channel = connection.channel() channel = connection.channel()
...@@ -19,6 +21,10 @@ def handleDelivery(message): ...@@ -19,6 +21,10 @@ def handleDelivery(message):
node_num = 0 node_num = 0
vm_list = [] vm_list = []
for param in params: for param in params:
name = param["name"]
if name == "cluster":
cluster_type = param["value"]
else:
value = param["value"] value = param["value"]
ip = param["attributes"]["IP"] ip = param["attributes"]["IP"]
user = param["attributes"]["user"] user = param["attributes"]["user"]
...@@ -30,21 +36,28 @@ def handleDelivery(message): ...@@ -30,21 +36,28 @@ def handleDelivery(message):
fo.close() fo.close()
vm = VmInfo(ip, user, key, role) vm = VmInfo(ip, user, key, role)
vm_list.append(vm) vm_list.append(vm)
return docker_kubernetes.run(vm_list)
if cluster_type == "kubernetes":
ret = docker_kubernetes.run(vm_list)
return ret
elif cluster_type == "swarm":
ret = docker_engine.run(vm_list)
if "ERROR" in ret: return ret
ret = docker_swarm.run(vm_list)
return ret
else:
return "ERROR: invalid cluster"
def on_request(ch, method, props, body): def on_request(ch, method, props, body):
ret = handleDelivery(body) ret = handleDelivery(body)
print ret print ret
if "ERROR" in ret: if "ERROR" in ret:
kuber_string = ret
res_name = "error" res_name = "error"
#print(" Message %s" % body)
else: else:
res_name = "credential" res_name = "credential"
kuber_file = open(path + "admin.conf", "r")
kuber_string = kuber_file.read()
kuber_file.close()
response = {} response = {}
outcontent = {} outcontent = {}
...@@ -55,7 +68,7 @@ def on_request(ch, method, props, body): ...@@ -55,7 +68,7 @@ def on_request(ch, method, props, body):
par["url"] = "null" par["url"] = "null"
par["encoding"] = "UTF-8" par["encoding"] = "UTF-8"
par["name"] = res_name par["name"] = res_name
par["value"] = kuber_string par["value"] = ret
par["attributes"] = "null" par["attributes"] = "null"
response["parameters"].append(par) response["parameters"].append(par)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment