-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
110 lines (89 loc) · 3.18 KB
/
deploy.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
from os.path import expanduser
import paramiko
import warnings
import sys
sys.path.append("code")
from user_definition import *
warnings.filterwarnings("ignore")
def ssh_client():
"""Return ssh client object"""
return paramiko.SSHClient()
def ssh_connection(ssh, ec2_address, user, key_file):
"""Establish ssh connection"""
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(ec2_address,
username=user,
key_filename=expanduser("~") + key_file)
except TimeoutError:
return None
return ssh
def id_rsa_gen(ssh, id_rsa):
"""generate id_rsa key, move it to the right location
and make it private"""
ftp = ssh.open_sftp()
file = ftp.file('id_rsa', "a", -1)
file.write(id_rsa)
file.flush()
ftp.close()
ssh.exec_command("mv id_rsa ~/.ssh/id_rsa")
ssh.exec_command("chmod 400 ~/.ssh/id_rsa")
def id_rsa_pub_gen(ssh, id_rsa_pub):
"""generate id_rsa public key, move it
to the right location and make it private"""
ftp = ssh.open_sftp()
file = ftp.file('id_rsa.pub', "a", -1)
file.write(id_rsa_pub)
file.flush()
ftp.close()
ssh.exec_command("mv id_rsa.pub ~/.ssh/id_rsa.pub")
ssh.exec_command("chmod 400 ~/.ssh/id_rsa.pub")
def aws_gen(ssh, aws_info):
"""turn off ansible such that git clone won't throw error"""
ftp = ssh.open_sftp()
file = ftp.file('credentials', "a", -1)
file.write(aws_info)
file.flush()
ftp.close()
ssh.exec_command("mkdir -p ~/.aws/")
ssh.exec_command("mv credentials ~/.aws/credentials")
def git_clone_pull(ssh, ssh_rsa):
"""git clone the repo and if there is a repo pull it"""
ssh.exec_command("""echo "%s" >> ~/.ssh/known_hosts""" % ssh_rsa)
stdin, stdout, stderr = ssh.exec_command(
"git clone [email protected]:MSDS698/Pelicam.git")
if (b"already exists" in stderr.read()):
stdin, stdout, stderr = ssh.exec_command(
"cd Pelicam; git pull [email protected]:MSDS698/Pelicam.git")
stderr.read()
def main():
"""main function"""
# initialize and login ssh
print("setting up initial contact")
ssh = ssh_client()
ssh = ssh_connection(ssh, ec2_address, user, key_file)
if ssh is None:
# if it can't connect
print("Can't connect to the server")
else:
# upadte and install basic packges
print("acquire basic packges")
tmux_install = "sudo apt install -y htop tmux git"
stdin, stdout, stderr = ssh.exec_command("sudo apt update")
stdin, stdout, stderr = ssh.exec_command(tmux_install)
# update security files
print("updating security files")
id_rsa_gen(ssh, id_rsa)
id_rsa_pub_gen(ssh, id_rsa_pub)
aws_gen(ssh, aws_info)
# clone or pull docs
print("clone or pulling classroom repo")
git_clone_pull(ssh, ssh_rsa)
print("install remaining packages and start up servers")
ssh.exec_command("chmod +x ~/Pelicam/start_up.sh")
ssh.exec_command("~/Pelicam/start_up.sh > start_up_log")
print("Using port: 5001")
ssh.close()
print("Logged out.")
if __name__ == '__main__':
main()