|
|
@ -308,6 +308,7 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
self.client = client
|
|
|
|
self.client = client
|
|
|
|
self.results = results
|
|
|
|
self.results = results
|
|
|
|
self.check_mode = self.client.check_mode
|
|
|
|
self.check_mode = self.client.check_mode
|
|
|
|
|
|
|
|
self.swarm_info = {}
|
|
|
|
|
|
|
|
|
|
|
|
self.parameters = TaskParameters(client)
|
|
|
|
self.parameters = TaskParameters(client)
|
|
|
|
|
|
|
|
|
|
|
@ -346,17 +347,18 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
self.__update_swarm()
|
|
|
|
self.__update_swarm()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not self.check_mode:
|
|
|
|
self.client.init_swarm(
|
|
|
|
try:
|
|
|
|
advertise_addr=self.parameters.advertise_addr, listen_addr=self.parameters.listen_addr,
|
|
|
|
self.client.init_swarm(
|
|
|
|
force_new_cluster=self.parameters.force_new_cluster, swarm_spec=self.parameters.spec)
|
|
|
|
advertise_addr=self.parameters.advertise_addr, listen_addr=self.parameters.listen_addr,
|
|
|
|
except APIError as exc:
|
|
|
|
force_new_cluster=self.parameters.force_new_cluster, swarm_spec=self.parameters.spec)
|
|
|
|
self.client.fail("Can not create a new Swarm Cluster: %s" % to_native(exc))
|
|
|
|
except APIError as exc:
|
|
|
|
|
|
|
|
self.client.fail("Can not create a new Swarm Cluster: %s" % to_native(exc))
|
|
|
|
|
|
|
|
|
|
|
|
self.__isSwarmManager()
|
|
|
|
self.__isSwarmManager()
|
|
|
|
self.results['actions'].append("New Swarm cluster created: %s" % (self.swarm_info['ID']))
|
|
|
|
self.results['actions'].append("New Swarm cluster created: %s" % (self.swarm_info.get('ID')))
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['swarm_facts'] = {u'JoinTokens': self.swarm_info['JoinTokens']}
|
|
|
|
self.results['swarm_facts'] = {u'JoinTokens': self.swarm_info.get('JoinTokens')}
|
|
|
|
|
|
|
|
|
|
|
|
def __update_spec(self, spec):
|
|
|
|
def __update_spec(self, spec):
|
|
|
|
if (self.parameters.node_cert_expiry is None):
|
|
|
|
if (self.parameters.node_cert_expiry is None):
|
|
|
@ -406,9 +408,10 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
self.results['actions'].append("No modification")
|
|
|
|
self.results['actions'].append("No modification")
|
|
|
|
self.results['changed'] = False
|
|
|
|
self.results['changed'] = False
|
|
|
|
return
|
|
|
|
return
|
|
|
|
self.client.update_swarm(
|
|
|
|
if not self.check_mode:
|
|
|
|
version=version, swarm_spec=new_spec, rotate_worker_token=self.parameters.rotate_worker_token,
|
|
|
|
self.client.update_swarm(
|
|
|
|
rotate_manager_token=self.parameters.rotate_manager_token)
|
|
|
|
version=version, swarm_spec=new_spec, rotate_worker_token=self.parameters.rotate_worker_token,
|
|
|
|
|
|
|
|
rotate_manager_token=self.parameters.rotate_manager_token)
|
|
|
|
except APIError as exc:
|
|
|
|
except APIError as exc:
|
|
|
|
self.client.fail("Can not update a Swarm Cluster: %s" % to_native(exc))
|
|
|
|
self.client.fail("Can not update a Swarm Cluster: %s" % to_native(exc))
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -424,18 +427,21 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
self.swarm_info = json.loads(json_str)
|
|
|
|
self.swarm_info = json.loads(json_str)
|
|
|
|
if self.swarm_info['Swarm']['NodeID']:
|
|
|
|
if self.swarm_info['Swarm']['NodeID']:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
if self.swarm_info['Swarm']['LocalNodeState'] in ('active', 'pending', 'locked'):
|
|
|
|
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
def join(self):
|
|
|
|
if self.__isSwarmNode():
|
|
|
|
if self.__isSwarmNode():
|
|
|
|
self.results['actions'].append("This node is already part of a swarm.")
|
|
|
|
self.results['actions'].append("This node is already part of a swarm.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if not self.check_mode:
|
|
|
|
self.client.join_swarm(
|
|
|
|
try:
|
|
|
|
remote_addrs=self.parameters.remote_addrs, join_token=self.parameters.join_token, listen_addr=self.parameters.listen_addr,
|
|
|
|
self.client.join_swarm(
|
|
|
|
advertise_addr=self.parameters.advertise_addr)
|
|
|
|
remote_addrs=self.parameters.remote_addrs, join_token=self.parameters.join_token, listen_addr=self.parameters.listen_addr,
|
|
|
|
except APIError as exc:
|
|
|
|
advertise_addr=self.parameters.advertise_addr)
|
|
|
|
self.client.fail("Can not join the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
except APIError as exc:
|
|
|
|
|
|
|
|
self.client.fail("Can not join the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
self.results['actions'].append("New node is added to swarm cluster")
|
|
|
|
self.results['actions'].append("New node is added to swarm cluster")
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
@ -443,10 +449,11 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
if not(self.__isSwarmNode()):
|
|
|
|
if not(self.__isSwarmNode()):
|
|
|
|
self.results['actions'].append("This node is not part of a swarm.")
|
|
|
|
self.results['actions'].append("This node is not part of a swarm.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if not self.check_mode:
|
|
|
|
self.client.leave_swarm(force=self.parameters.force)
|
|
|
|
try:
|
|
|
|
except APIError as exc:
|
|
|
|
self.client.leave_swarm(force=self.parameters.force)
|
|
|
|
self.client.fail("This node can not leave the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
except APIError as exc:
|
|
|
|
|
|
|
|
self.client.fail("This node can not leave the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
self.results['actions'].append("Node has left the swarm cluster")
|
|
|
|
self.results['actions'].append("Node has left the swarm cluster")
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
@ -479,10 +486,11 @@ class SwarmManager(DockerBaseClass):
|
|
|
|
if not(status_down):
|
|
|
|
if not(status_down):
|
|
|
|
self.client.fail("Can not remove the node. The status node is ready and not down.")
|
|
|
|
self.client.fail("Can not remove the node. The status node is ready and not down.")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not self.check_mode:
|
|
|
|
self.client.remove_node(node_id=self.parameters.node_id, force=self.parameters.force)
|
|
|
|
try:
|
|
|
|
except APIError as exc:
|
|
|
|
self.client.remove_node(node_id=self.parameters.node_id, force=self.parameters.force)
|
|
|
|
self.client.fail("Can not remove the node from the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
except APIError as exc:
|
|
|
|
|
|
|
|
self.client.fail("Can not remove the node from the Swarm Cluster: %s" % to_native(exc))
|
|
|
|
self.results['actions'].append("Node is removed from swarm cluster.")
|
|
|
|
self.results['actions'].append("Node is removed from swarm cluster.")
|
|
|
|
self.results['changed'] = True
|
|
|
|
self.results['changed'] = True
|
|
|
|
|
|
|
|
|
|
|
|