-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure_server.py
More file actions
132 lines (96 loc) · 3.05 KB
/
Copy pathconfigure_server.py
File metadata and controls
132 lines (96 loc) · 3.05 KB
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
"""
File Name : configure_server.py
Description: Configure servers based on user configuration
"""
from sys import argv
import yaml
import socket
import logging
from ssh2.session import Session
#set up log level
logging.basicConfig(level=logging.INFO)
# Config params
SERVER = []
USERNAME = " "
PASSWORD = " "
TASKS = []
def listToString(s):
"""
Function to convert list to string
:param : list
:return: string
"""
str1 = ""
#traverse in list
for ele in s:
str1 += ele
str1.strip('\n')
return str1
def configure_server():
"""
Function to configure servers
:param :
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((listToString(SERVER[0]), 22))
# assign to session class
session = Session()
# set up 3 way handshake
logging.info('Setting up 3-way handshake')
session.handshake(sock)
logging.debug('Username is %s', USERNAME)
logging.debug('password %s', PASSWORD)
session.userauth_password(USERNAME, PASSWORD)
channel = session.open_session()
channel.execute("apt-get update -y; apt-get install apache2 -y; systemctl start apache2.service")
size, data = channel.read()
# while (size > 0):
# print(data.decode())
# size, data = channel.read()
channel.close()
print("Exit status : {0}", format(channel.get_exit_status()))
def parse_configuration(f, docs):
"""
Function to parse yaml configuration file
:param : file,attributes
"""
global USERNAME
global PASSWORD
for doc in docs:
for key, value in doc.items():
logging.debug(key, "->", value)
if key == "hosts" and value[0] == "webservers":
with open(r'C:\Users\rajpo\PycharmProjects\SlackProj\hosts')as hostfile:
# if 'webservers' in hostfile.read():
for line in hostfile:
if 'webservers' in line:
for line in hostfile:
SERVER.append(line.strip().split('\t'))
logging.debug('Servers are %s', SERVER)
if key == "username":
USERNAME = listToString(value)
logging.debug('Username is %s', USERNAME)
if key == "password":
PASSWORD = listToString(value)
if key == "tasks":
for line in f:
TASKS.append(line)
logging.debug('Tasks are %s', TASKS)
# configure servers
configure_server()
def main():
"""
main function
"""
# access config file
try:
with open(r'configuration.yml') as f:
docs = yaml.load_all(f, Loader=yaml.FullLoader)
# function to parse yaml file
print(docs)
parse_configuration(f, docs)
except IOError:
logging.error('Cannot find Configuration file -configuration.yml')
exit()
if __name__ == "__main__":
main()