Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CONF
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UvA
CONF
Commits
0dc5d2c7
Commit
0dc5d2c7
authored
Sep 26, 2017
by
Yang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update docker_swarm
parent
3f0dac53
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
44 deletions
+44
-44
docker_swarm.py
drip-deployer/docker_swarm.py
+44
-44
No files found.
drip-deployer/docker_swarm.py
View file @
0dc5d2c7
#! /usr/bin/env python
#! /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.
# 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'
...
...
@@ -26,21 +26,28 @@ def install_manager(vm):
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 info | grep Swarm"
)
temp_list
=
stdout
.
readlines
()
temp_str
=
""
for
i
in
temp_list
:
temp_str
+=
i
if
temp_str
.
find
(
"Swarm: active"
)
!=
-
1
:
return
"SUCCESS"
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker info | grep 'Swarm'"
)
temp_list1
=
stdout
.
readlines
()
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker info | grep 'Is Manager'"
)
temp_list2
=
stdout
.
readlines
()
if
temp_list1
[
0
]
.
find
(
"Swarm: active"
)
!=
-
1
:
if
temp_list2
[
0
]
.
find
(
"Is Manager: true"
)
!=
-
1
:
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker swarm join-token worker"
)
retstr
=
stdout
.
readlines
()
return
retstr
[
2
]
.
encode
()
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker swarm leave --force"
)
stdout
.
read
()
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker swarm init --advertise-addr
%
s"
%
(
vm
.
ip
))
stdout
.
read
()
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker swarm join-token worker"
)
retstr
=
stdout
.
readlines
()
ret
=
retstr
[
2
]
.
encode
()
print
"
%
s: ========= Swarm Manager Installed ========="
%
(
vm
.
ip
)
except
Exception
as
e
:
print
'
%
s:
%
s'
%
(
vm
.
ip
,
e
)
return
"ERROR:"
+
vm
.
ip
+
" "
+
str
(
e
)
return
"ERROR:"
+
vm
.
ip
+
" "
+
str
(
e
)
ssh
.
close
()
return
ret
str
[
4
]
+
retstr
[
5
]
+
retstr
[
6
]
return
ret
def
install_worker
(
join_cmd
,
vm
):
try
:
...
...
@@ -49,6 +56,9 @@ def install_worker(join_cmd, vm):
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 info | grep 'Swarm'"
)
temp_list1
=
stdout
.
readlines
()
if
temp_list1
[
0
]
.
find
(
"Swarm: active"
)
!=
-
1
:
return
"SUCCESS"
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo docker swarm leave --force"
)
stdout
.
read
()
stdin
,
stdout
,
stderr
=
ssh
.
exec_command
(
"sudo
%
s"
%
(
join_cmd
))
...
...
@@ -56,38 +66,28 @@ def install_worker(join_cmd, vm):
print
"
%
s: ========= Swarm Worker Installed ========="
%
(
vm
.
ip
)
except
Exception
as
e
:
print
'
%
s:
%
s'
%
(
vm
.
ip
,
e
)
return
"ERROR:"
+
vm
.
ip
+
" "
+
str
(
e
)
return
"ERROR:"
+
vm
.
ip
+
" "
+
str
(
e
)
ssh
.
close
()
return
"SUCCESS"
def
run
(
vm_list
):
for
i
in
vm_list
:
parentDir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
i
.
key
))
os
.
chmod
(
parentDir
,
0o700
)
os
.
chmod
(
i
.
key
,
0o600
)
if
i
.
role
==
"master"
:
if
i
.
role
==
"master"
:
join_cmd
=
install_manager
(
i
)
if
"ERROR"
in
join_cmd
:
if
"ERROR
:
"
in
join_cmd
:
return
join_cmd
elif
"SUCCESS"
in
join_cmd
:
swarm_file
=
open
(
i
.
key
)
swarm_string
=
swarm_file
.
read
()
swarm_file
.
close
()
return
swarm_string
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
()
parentDir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
i
.
key
))
os
.
chmod
(
parentDir
,
0o700
)
os
.
chmod
(
i
.
key
,
0o600
)
swarm_file
=
open
(
i
.
key
)
swarm_string
=
swarm_file
.
read
()
swarm_file
.
close
()
break
for
i
in
vm_list
:
if
i
.
role
==
"slave"
:
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
swarm_string
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment