-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathon_premise.py
133 lines (120 loc) · 5.47 KB
/
on_premise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import sys
sys.path.insert(0,"./common")
from fabric_common import RemoteShell
from fabric.network import ssh
from fabric.context_managers import *
from datetime import datetime
from ConfigParser import SafeConfigParser
ssh.util.log_to_file("./logs/paramiko.log", 10)
class OnPremis():
def __init__(self):
kubeadm_join_command = ""
def DoError (self,Error) :
sys.exit(Error)
def parser_and_execute(self,conf_file="on_premise.conf", force=False):
"""."""
conf_file = os.path.abspath(conf_file)
config = SafeConfigParser()
if not os.path.isfile(conf_file):
print(colored("No such file found : %s" % conf_file, "red"))
sys.exit(1)
try:
config.read(conf_file)
except configparser.DuplicateSectionError as e:
print(e)
sys.exit(1)
except configparser.DuplicateOptionError as e:
print(e)
sys.exit(1)
except configparser.ParsingError as e:
print(colored('Parsing error in mini_ansible.conf', 'red'))
print(e)
sys.exit(1)
sections = config.sections();
for section in sections:
username = None
password = None
command_list = None
private_key = None
local_path = None
remote_path = None
print("-----------------> " + section)
for key,value in config.items(section):
if key == "ip":
ip = value
if key == "username":
username = value
if key == "password":
password = value
if key == "command":
command_list = value.split("\n")
if "slave" in section:
command_list.append("sudo " + self.kubeadm_join_command)
if key == "private_key":
private_key = value
if key == "local_path":
local_path = value
if key == "remote_path":
remote_path = value
if username and ip:
if private_key:
if local_path and remote_path:
self.send_file_remote(local_path,remote_path,ip,username,private_key=private_key)
print("File sent succesfully : " + remote_path)
if command_list:
self.execute_command_list_remotely(command_list,ip,username,private_key=private_key)
elif not local_path and not remote_path:
print("local_path or remote_path or command_list field are not found for " + str(section))
if password:
if local_path and remote_path:
self.send_file_remote(local_path,remote_path,ip,username,password=password)
print("File sent succesfully : " + remote_path)
if command_list:
self.execute_command_list_remotely(command_list,ip,username,password=password)
elif not local_path and not remote_path:
print("local_path or remote_path or command_list field are not found for " + str(section))
if not private_key and not password:
print("private_key or password field are not found for " + str(section))
else:
print("username or ip field are not found for "+ str(section))
def execute_command_list_remotely(self,command_list,ip,user,password=None,private_key=None):
try:
if private_key:
ssh_connect_remote = RemoteShell(hostname=ip,user=user,private_key=private_key)
else:
ssh_connect_remote = RemoteShell(hostname=ip,user=user,password=password)
for actual_command in command_list:
print("IP / DNS : " + ip )
print("Command : " + actual_command )
f_obj = ssh_connect_remote.run(cmd=actual_command)
status, res = ssh_connect_remote.return_status_message_fabric(f_obj)
if status:
print("Output : " + str(res))
if "kubeadm init" in actual_command:
self.kubeadm_join_command = res.split("Then you can join any number of worker nodes by running the following on each as root:")[1].replace("\\","").replace("\r\n","").strip()
else:
print("Output : Failed to execute the above command")
print("Error for your reference : " + str(res))
print("\n")
del ssh_connect_remote
except Exception as e:
self.DoError("Exception in execute_command :"+str(e) )
def send_file_remote(self,local_path,remote_path,ip,user,password=None,private_key=None):
try:
if private_key:
ssh_connect_remote = RemoteShell(hostname=ip,user=user,private_key=private_key)
else:
ssh_connect_remote = RemoteShell(hostname=ip,user=user,password=password)
ssh_connect_remote.file_send(local_path,remote_path)
del ssh_connect_remote
except Exception as e:
self.DoError("Exception in execute_command :"+str(e) )
def main():
obj = OnPremis()
try:
obj.parser_and_execute()
except Exception as e:
obj.DoError(str(e))
if __name__ == "__main__":
main()