Skip to content

Commit

Permalink
Merge pull request #6 from DaoCloud/develop
Browse files Browse the repository at this point in the history
1.1.0 release
  • Loading branch information
pacoxu authored Nov 20, 2018
2 parents 80167f9 + ce15905 commit b76a2a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 36 additions & 10 deletions dce_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding=utf-8

import base64
import json
import os
Expand All @@ -6,11 +8,11 @@
import urlparse

from .docker_client import DockerClient
from .docker_client import DockerException

__all__ = ['PluginSDK', 'PluginSDKException']

CONFIG_MAX_SIZE = 1024 * 1024
DCE_CONTROLLER_DB_PATH = os.getenv('DCE_CONTROLLER_DB_PATH') or '/var/local/dce/engine/controller.db'


class PluginSDKException(Exception):
Expand All @@ -20,34 +22,58 @@ class PluginSDKException(Exception):
class PluginSDK(object):
def __init__(self, base_url=None, timeout=None):
self.docker_client = DockerClient(base_url=base_url, timeout=timeout)

pass

# since 3.0
def _detect_controller_ips(self):
try:
with open(DCE_CONTROLLER_DB_PATH) as f:
controller_ips = [l.strip() for l in f.readlines()]
except IOError:
return []
return controller_ips

# 2.6-2.10 使用,检测当前主机 IP,插件一定部署在控制节点,因此相当于获取控制节点 IP。
# 3.0 直接读取本地的 controller.db 文件获取控制节点 IP
def _detect_host_ip(self):
controler_ips = self._detect_controller_ips()
if controler_ips:
return controler_ips[0]

# DCE 2.6 - 2.10 is using docker swarmkit, and plugins have to run on manager, so use host ip
info = self.docker_client.info()
host_ip = info.get('Swarm', {}).get('NodeAddr')
if not host_ip:
raise PluginSDKException("Detect node address failed")

return host_ip
if host_ip:
return host_ip
raise PluginSDKException("Detect node address failed")

def _detect_dce_ports(self):
"""
:return: (swarm_port, controller_port, controller_ssl_port)
"""
info = self.docker_client.info()
node_swarm_state = info.get('Swarm', {}).get('LocalNodeState')
# for DCE 3.0, swarm is inactive
if node_swarm_state is "inactive":
controller_port = os.getenv('CONTROLLER_EXPORTED_PORT') or 80
controller_ssl_port = os.getenv('CONTROLLER_SSL_EXPORTED_PORT') or 443
return None, int(controller_port), int(controller_ssl_port)

# DCE 2.6 - 2.10 is using docker swarmkit
dce_base = self.docker_client.service_inspect('dce_base')
environments = dce_base.get('Spec', {}).get('TaskTemplate', {}).get('ContainerSpec', {}).get('Env', [])
environments = dict(
[e.split('=', 1) for e in environments if '=' in e]
)
(swarm_port, controller_port, controller_ssl_port) = (
environments.get('SWARM_PORT'),
environments.get('CONTROLLER_PORT'),
environments.get('CONTROLLER_SSL_PORT')
environments.get('SWARM_PORT') or 2375,
environments.get('CONTROLLER_PORT') or 80,
environments.get('CONTROLLER_SSL_PORT') or 443
)
if not (swarm_port and controller_port and controller_ssl_port):
raise PluginSDKException("Detect DCE ports failed")

ports = int(swarm_port), int(controller_port), int(controller_ssl_port)

return ports

def _plugin_storage_url(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="dce-plugin-sdk",
version='0.1',
version='1.1.1',
description="DCE plugin SDK for Python.",
url='https://github.com/DaoCloud/dce-plugin-sdk-py',
packages=['dce_plugin'],
Expand Down

0 comments on commit b76a2a4

Please sign in to comment.