1 import os
2 import socket
3 from starcluster import exception
4 from starcluster import ssh
5 from starcluster.logger import log
24
43
45 """
46 This class represents a single compute node in a StarCluster.
47
48 It contains all useful metadata for the node such as the internal/external
49 hostnames, ips, etc as well as a paramiko ssh object for executing commands,
50 creating/modifying files on the node.
51
52 Takes boto.ec2.instance.Instance, key_location, and alias as input and
53 optionally a user to ssh as (defaults to root)
54 """
55 - def __init__(self, instance, key_location, alias, user='root'):
56 self.instance = instance
57 self.key_location = key_location
58 self.alias = alias
59 self.user = user
60 self._ssh = None
61
62 @property
65
66 @property
69
70 @property
73
74 @property
77
78 @property
81
82 @property
84 return self.instance.id
85
86 @property
89
90 @property
93
94 @property
96 return self.instance.state
97
98 @property
101
102 @property
105
106 @property
108 return self.instance.architecture
109
110 @property
113
114 @property
117
118 @property
121
122 @property
131
132 @property
134 return self.instance.spot_instance_request_id
135
137 return self.alias == "master"
138
140 return self.instance.stop()
141
143 timeout = 10.0
144 s = socket.socket()
145 s.settimeout(timeout)
146 try:
147 s.connect((self.dns_name, 22))
148 s.close()
149 return True
150 except socket.timeout:
151 log.debug(
152 "connecting to port 22 on timed out after % seconds" % timeout)
153 except socket.error:
154 log.debug("ssh not up for %s" % self.dns_name)
155 return False
156
158 self.update()
159 if not self.is_ssh_up():
160 return False
161 if self.private_ip_address is None:
162 log.debug("instance %s has no private_ip_address" % self.id)
163 log.debug(
164 "attempting to determine private_ip_address for instance %s" % \
165 self.id)
166 try:
167 private_ip = self.ssh.execute(
168 'python -c "import socket; print socket.gethostbyname(\'%s\')"' % \
169 self.private_dns_name)[0].strip()
170 log.debug("determined instance %s's private ip to be %s" % \
171 (self.id, private_ip))
172 self.instance.private_ip_address = private_ip
173 except Exception,e:
174 print e
175 return False
176 return True
177
185
186 @property
188 if not self._ssh:
189 self._ssh = ssh.Connection(self.instance.dns_name,
190 username=self.user,
191 private_key=self.key_location)
192 return self._ssh
193
194 - def get_hosts_entry(self):
195 """ Returns /etc/hosts entry for this node """
196 etc_hosts_line = "%(INTERNAL_IP)s %(INTERNAL_NAME)s %(INTERNAL_NAME_SHORT)s %(INTERNAL_ALIAS)s"
197 etc_hosts_line = etc_hosts_line % self.network_names
198 return etc_hosts_line
199
201 if self._ssh:
202 self._ssh.close()
203