-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLnode.py
161 lines (142 loc) · 4.94 KB
/
CLnode.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
##############
# CLnode.py - ClusterNode object
##############
import os
import sys
import GF
class CLnode:
instID=''
instName=''
status=''
ami=''
key=''
size=''
date=''
ntype=''
url=''
master=False
sir=''
deployed=False
# Constructor
def __init__(self, instID='',instName='',status='',ami='',key='',size='',date='',ntype='',url='',master=False,sir='',deployed=False):
self.instID=instID
self.instName=instName
self.status=status
self.ami=ami
self.key=key
self.size=size
self.date=date
self.ntype=ntype
self.url=url
self.master=GF.str2bool(master)
self.sir=sir
self.deployed=GF.str2bool(deployed)
# Kill the current instance
def kill(self):
if self.status!="running":
print "Instance is not running, therefore will not shutdown"
return
try:
res=''
res = GF.run("ec2-terminate-instances "+self.instID)
print res
self.status='terminated'
except Exception as x:
print x, "\n", res
return -1
# print all enclose data in row form
def desc(self):
print self.ntype,self.master,self.instID,self.instName,self.status,self.url,self.ami,self.key,self.size,self.date,self.sir,self.deployed
# to string
def __repr__(self):
return self.instID+","+self.instName+","+self.status+","+self.ami+","+self.key+","+self.size+","+self.date+","+self.ntype+","+self.url+","+str(self.master)+","+self.sir+","+str(self.deployed)
# print all enclosed data in a block with descriptions
def desc_detail(self):
print "Instance Type:\t\t"+self.ntype
print "Is Master Node:\t\t"+str(self.master)
print "Has been deployed:\t"+str(self.deployed)
print "Instance ID:\t\t"+self.instID
print "Instance Name:\t\t"+self.instName
print "Status:\t\t\t"+self.status
print "Hostname/url:\t"+self.url
print "AMI:\t\t\t"+self.ami
print "Keypair:\t\t"+self.key
print "Instance Size:\t\t"+self.size
print "Date/Time started:\t"+self.date
print "Spot Inst Request:\t"+self.sir
print "===="
# returns status, not really needed since elements are public
def status(self):
return self.status
# returns boolean depending on status
def running(self):
if self.status=="running":
return True
else:
return False
# returns a copy of the current object
def copy(self):
return CLnode(self.instID,self.instName,self.status,self.ami,self.key,self.size,self.date,self.ntype,self.url,self.master,self.sir,self.deployed)
# Deploys the node, by copying the bundle.tar to this instance
# Then extracts it and runs the setup script
def deploy(self,payload,sshKey,launch=False):
# TODO: error handeling
# remove SIG with ssh-keygen -f "/home/madmaze/.ssh/known_hosts" -R ec2-184-73-46-186.compute-1.amazonaws.com
print "\n============================================"
print "Deploing",self.instID,"/",self.instName
print "====="
if self.master is False:
try:
res=GF.run("scp -o StrictHostKeyChecking=no -i "+sshKey+" "+payload+" ubuntu@"+self.url+":~/")
print "scp -o StrictHostKeyChecking=no -i "+sshKey+" "+payload+" ubuntu@"+self.url+":~/"
except Exception as x:
print x, "\n", res
return -1
# EXTRACT payload
try:
res=GF.run("ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'tar xvf ~/bundle.tar;'")
print "ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'tar xvf ~/bundle.tar;'"
except Exception as x:
print x, "\n", res
return -1
if launch is True:
# LAUNCH Payload
try:
print "ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'python ~/payload/setup.py&'"
res=GF.run("ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'screen -dm python ~/payload/setup.py'")
self.deployed = True
except Exception as x:
print x, "\n", res
return -1
else:
print "Master node: No need to deploy!"
def gather(self,logsDir,sshKey):
print "\n============================================"
print "Gathering logs: ",self.instID,"/",self.instName
print "====="
logsDir+='/'+self.instID+'-'+self.instName
try:
res=GF.run("mkdir -p "+logsDir)
res=GF.run("scp -r -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+":/var/tmp/log "+logsDir)
print "scp -r -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+":/var/tmp/log "+logsDir
except Exception as x:
print x, "\n", res
return -1
'''
# EXTRACT payload
try:
res=GF.run("ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'tar xvf ~/bundle.tar;'")
print "ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'tar xvf ~/bundle.tar;'"
except Exception as x:
print x, "\n", res
return -1
if launch is True:
# LAUNCH Payload
try:
res=GF.run("ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'python ~/payload/setup.py'")
print "ssh -o StrictHostKeyChecking=no -i "+sshKey+" ubuntu@"+self.url+" 'python ~/payload/setup.py'"
self.deployed = True
except Exception as x:
print x, "\n", res
return -1
'''