/usr/bin/cloud-run-instances is in cloud-utils 0.25-0ubuntu5.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | #!/usr/bin/python
#
# Copyright (C) 2010 Canonical Ltd.
#
# Authors: Dustin Kirkland <kirkland@canonical.com>
# Scott Moser <scott.moser@canonical.com>
# Clint Byrum <clint.byrum@canonical.com>
# Tom Ellis <tom.ellis@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import string
import sys
import signal
import re
import base64
from optparse import OptionParser
from socket import getaddrinfo
import time
import logging
from paramiko import SSHClient, AutoAddPolicy, AuthenticationException
import paramiko
from subprocess import Popen, PIPE
finished = "FINISHED"
CC_IMPORT_SSH = """#cloud-config
runcmd:
- [ sudo, -Hu, ubuntu, sh, '-c',
"c=ssh-import-id; which $c >/dev/null || c=ssh-import-lp-id; $c $1",
"--", "%s" ]
"""
class SafeConnectException(Exception):
pass
class Instance(object):
pass
class TemporaryMissingHostKeyPolicy(AutoAddPolicy):
""" does not save to known_hosts, but does save the keys in an array """
def __init__(self):
self._keys = []
AutoAddPolicy.__init__(self)
def missing_host_key(self, client, hostname, key):
self._keys.append(key)
def getKeys(self):
return self._keys
class PermanentMissingHostKeyPolicy(TemporaryMissingHostKeyPolicy):
""" also has the behavor of the parent AutoAddPolicy """
def missing_host_key(self, client, hostname, key):
#TemporaryMissingHostKeyPolicy.missing_host_key(self, client, hostname, key)
self._keys.append(key)
AutoAddPolicy.missing_host_key(self, client, hostname, key)
class ConsoleFingerprintScanner(object):
def __init__(self, instance_id, hostname, provider, options, sleeptime=30):
self.state = "working"
self.instance_id = instance_id
self.hostname = hostname
self.provider = provider
self.sleeptime = sleeptime
self.fingerprint = None
self.options = options
self.logger = logging.getLogger('console-scanner(%s)' % instance_id)
def scan(self):
self.logger.debug('scraping fingerprints for instance_id = %s', self.instance_id)
try:
while self.fingerprint is None:
console_data = self.get_console_output()
self.fingerprint = self.get_fingerprints_in_console_data(console_data)
if self.fingerprint is not None:
self.fingerprint = (int(self.fingerprint[0]), self.fingerprint[1], self.fingerprint[3])
else:
self.logger.debug('sleeping %d seconds', self.options.sleep_time)
time.sleep(self.options.sleep_time)
except None:
pass
return self.fingerprint
def get_console_output(self):
cmd = '%s-get-console-output' % self.provider
args = [ cmd ]
args.append(self.instance_id)
self.logger.debug('running %s', args)
rconsole = Popen(args, stdout=PIPE)
ret = []
try:
for line in rconsole.stdout:
ret.append(line.strip())
finally:
cmdout = rconsole.wait()
if bool(cmdout):
raise Exception('%s failed with return code = %d', cmd, cmdout)
return ret
def get_fingerprints_in_console_data(self, output):
# return an empty list on "no keys found"
# return a list of key fingerprint data on success
# where each key fingerprint data is an array like:
# (2048 c7:c8:1d:0f:d9:40:20:f9:47:6a:f9:1d:6f:0a:8a:fe localhost (RSA))
begin_marker="-----BEGIN SSH HOST KEY FINGERPRINTS----"
end_marker="----END SSH HOST KEY FINGERPRINTS-----"
i = 0
while i < len(output):
if output[i].find(begin_marker) > -1:
while i < len(output) and output[i].find(end_marker) == -1:
self.logger.debug(output[i].strip())
toks = output[i].split(" ")
self.logger.debug(toks)
if len(toks) == 5: toks=toks[1:] # rip off "ec2:"
if len(toks) == 4 and toks[3] == "(RSA)":
self.logger.debug('found %s on line %d', toks, i)
return((toks))
i=i+1
break
i=i+1
self.logger.debug('did not find any fingerprints in output! (lines=%d)', i)
return None
class SshKeyScanner(object):
def __init__(self, instance_id, hostname, options, sleeptime=30):
self.state = "working"
self.instance_id = instance_id
self.hostname = hostname
self.sleeptime = sleeptime
self.fingerprint = None
self.keys = None
self.options = options
self.port = 22
self.logger = logging.getLogger('ssh-key-scanner(%s)' % instance_id)
self.client = None
self.connected = False
def scan(self):
self.logger.debug('getting fingerprints for %s', self.hostname)
try:
fingerprints = self.get_fingerprints_for_host()
self.logger.debug('fingerprints = %s', fingerprints)
if(len(fingerprints)>0):
self.state = "finished"
self.fingerprint = fingerprints[0]
except None:
pass
return self.fingerprint
def get_fingerprints_for_host(self):
# return an empty list on "no keys found"
# return a list of key fingerprint data on success
# where each key fingerprint data is an array like:
# (2048 c7:c8:1d:0f:d9:40:20:f9:47:6a:f9:1d:6f:0a:8a:fe localhost (RSA))
# use paramiko here
self.client = SSHClient()
client = self.client
client.set_log_channel('ssh-key-scanner(%s)' % self.instance_id)
if self.options.known_hosts is not None:
policy = PermanentMissingHostKeyPolicy()
""" This step ensures we save the keys, otherwise that step will be
skipped in AutoAddPolicy.missing_host_key """
for path in self.options.known_hosts:
if not os.path.isfile(path):
# if the file doesn't exist, then
# create it empty
fp = open(path,"w")
fp.close()
client.load_host_keys(path)
else:
policy = TemporaryMissingHostKeyPolicy()
client.set_missing_host_key_policy(policy)
pkey = None
if self.options.privkey is not None:
# TODO support password protected key file
pkey = paramiko.RSAKey.from_private_key_file(self.options.privkey)
retries = 0
allkeys = []
while 1:
try:
client.connect(self.hostname,self.port,username=self.options.ssh_user,pkey=pkey)
self.connected = True
break
except AuthenticationException as (message):
self.logger.warning('auth failed (non fatal) %s', message)
break
except Exception as (e):
retries += 1
if retries > 5:
raise Exception('gave up after retrying ssh %d times' % retries)
self.logger.info(e)
self.logger.debug('retry #%d... sleeping %d seconds..', retries, self.options.sleep_time)
time.sleep(self.options.sleep_time)
rlist = []
allkeys.extend(policy.getKeys())
allkeys.append(client.get_transport().get_remote_server_key())
for key in allkeys:
if type(key) == paramiko.RSAKey or type(key) == paramiko.PKey:
keytype = '(RSA)'
elif type(key) == paramiko.DSSKey:
keytype = '(DSA)'
else:
raise Exception('Cannot handle type %s == %s' % (type(key).__name__, key))
fp = key.get_fingerprint().encode("hex")
fp = ':'.join(re.findall('..', fp))
rlist.append((key.get_bits(), fp, keytype))
return rlist
def run_commands(self):
if self.options.ssh_run_cmd is not None and len(self.options.ssh_run_cmd):
if not self.connected:
self.logger.critical('cannot run command, ssh did not connect')
sys.exit(1)
ecmd = ' '.join(self.options.ssh_run_cmd)
self.logger.debug('running %s', ecmd)
inouterr = self.client.exec_command(ecmd)
try:
for line in inouterr[1]:
print line,
except:
pass
try:
for line in inouterr[2]:
print >> sys.stderr(line)
except:
pass
if self.connected:
self.client.close()
self.connected = False
def get_auto_instance_type(ami_id, provider):
cmd = '%s-describe-images' % provider
args = [ cmd , ami_id ]
logging.debug('running %s', args)
rimages = Popen(args, stdout=PIPE)
deftype = { 'i386' : 'm1.small', 'x86_64' : 'm1.large' }
try:
for line in rimages.stdout:
# Just in case there are %'s, don't confusee logging
# XXX print these out instead
logging.debug(line.replace('%','%%').strip())
parts = line.split("\t")
if parts[0] == 'IMAGE':
itype = parts[7]
if itype in deftype:
logging.info('auto instance type = %s', deftype[itype])
return deftype[itype]
finally:
rcode = rimages.wait()
logging.warning('ami not found, returning default m1.small')
return("m1.small")
def timeout_handler(signum, frame):
logging.critical('timeout reached, exiting')
sys.exit(1)
def handle_runargs(option, opt_str, value, parser):
delim=getattr(parser.values,"runargs_delim",None)
cur=getattr(parser.values,"runargs",[])
if cur is None: cur = []
cur.extend(value.split(delim))
setattr(parser.values,"runargs",cur)
return
def main():
parser = OptionParser(usage="usage: %prog [options] ids|(-- raw args for provider scripts)")
parser.add_option("-t", "--instance-type", dest="inst_type",
help="instance type", metavar="TYPE",
default="auto")
parser.add_option("-k", "--key", dest="keypair_name",
help="keypair name", metavar="TYPE",
default="auto")
parser.add_option("-n", "--instance-count", dest="count",
help="instance count", metavar="TYPE", type="int",
default=1)
parser.add_option("", "--ssh-privkey", dest="privkey",
help="private key to connect with (ssh -i)", metavar="id_rsa",
default=None)
parser.add_option("", "--ssh-pubkey", dest="pubkey",
help="public key to insert into image)", metavar="id_rsa.pub",
default=None)
parser.add_option("", "--ssh-run-cmd", dest="ssh_run_cmd", action="append", nargs=0,
help="run this command when ssh'ing", default=None)
parser.add_option("","--ssh-user", dest="ssh_user",
help="connect with ssh as user", default=None)
parser.add_option("", "--associate-ip", dest="ip",
help="associate elastic IP with instance", metavar="IP_ADDR",
default=None)
parser.add_option("", "--attach-volume", dest="vol",
help="attach EBS volume with instance", metavar="VOLUME_ID",
default=None)
parser.add_option("", "--known-hosts", dest="known_hosts", action="append",
help="write host keys to specified known_hosts file. Specify multiple times to read keys from multiple files (only updates last one)",
metavar="KnownHosts", default=None)
parser.add_option("-l", "--launchpad-id", dest="launchpad_id", action="append",
help="launchpad ids to pull SSH keys from (multiple times adds to the list)",
metavar="lpid", default=None)
parser.add_option("-i", "--instance-ids", dest="instance_ids", action="store_true",
help="expect instance ids instead of ami ids, skips -run-instances", default=False)
parser.add_option("", "--all-instances", dest="all_instances", action="store_true",
help="query all instances already defined (running/pending/terminated/etc)", default=False)
parser.add_option("","--run-args", dest="runargs", action="callback",
callback=handle_runargs, type="string",
help="pass option through to run-instances")
parser.add_option("","--run-args-delim", dest="runargs_delim",
help="split run-args options with delimiter",
default=None)
parser.add_option("","--verify-ssh", dest="verify_ssh", action="store_true",
help="verify SSH keys against console output (implies --wait-for=ssh)",
default=False)
parser.add_option("","--wait-for", dest="wait_for",
help="wait for one of: ssh , running",default=None)
parser.add_option("-p","--provider", dest="provider",
help="either euca or ec2", default=None)
parser.add_option("-v", "--verbose", action="count", dest="loglevel",
help="increase logging level", default=3)
parser.add_option("-q", "--quiet", action="store_true", dest="quiet",
help="produce no output or error messages", default=False)
parser.add_option("", "--sleep-time", dest="sleep_time",
help="seconds to sleep between polling", default=2)
parser.add_option("", "--teardown", dest="teardown", action="store_true",
help="terminate instances at the end", default=False)
(options, args) = parser.parse_args()
if ( os.path.basename(sys.argv[0]).startswith("uec") and
os.getenv("CLOUD_UTILS_WARN_UEC","0") == "0"):
sys.stderr.write("WARNING: '%s' is now 'cloud-run-instances'. %s\n" %
(os.path.basename(sys.argv[0]), "Please update tools or docs"))
if len(args) < 1 and not options.all_instances:
parser.error('you must pass at least one ami ID')
# loglevel should be *reduced* every time -v is passed, see logging docs for more
if options.quiet:
sys.stderr = open('/dev/null','w')
sys.stdout = sys.stderr
else:
loglevel = 6 - options.loglevel
if loglevel < 1:
loglevel = 1
# logging module levels are 0,10,20,30 ...
loglevel = loglevel * 10
logging.basicConfig(level=loglevel,format="%(asctime)s %(name)s/%(levelname)s: %(message)s",stream=sys.stderr)
logging.debug("loglevel = %d", loglevel)
provider = options.provider
if options.provider is None:
provider = os.getenv('EC2PRE','euca')
if options.ssh_run_cmd == [()]:
options.ssh_run_cmd = args
if options.known_hosts is None:
options.known_hosts = [ os.path.expanduser('~/.ssh/known_hosts') ]
if options.known_hosts is not None and len(options.known_hosts):
path = None
for path in options.known_hosts:
if not os.access(path, os.R_OK):
logging.warning('known_hosts file %s is not readable!', path)
# paramiko writes to the last one
if not os.access(path, os.W_OK):
logging.critical('known_hosts file %s is not writable!', path)
logging.debug("provider = %s", provider)
logging.debug("instance type is %s", options.inst_type)
if options.instance_ids or options.all_instances:
if options.all_instances:
pending_instance_ids = ['']
else:
pending_instance_ids = args
else:
if len(args) < 1:
raise Exception('you must pass at least one AMI ID')
ami_id = args[0]
del(args[0])
logging.debug("ami_id = %s", ami_id)
if options.inst_type == "auto":
options.inst_type = get_auto_instance_type(ami_id, provider)
pending_instance_ids = []
cmd = '%s-run-instances' % provider
run_inst_args = [ cmd ]
# these variables pass through to run-instances
run_inst_pt = {
"instance-count" : options.count,
"instance-type" : options.inst_type,
"key" : options.keypair_name,
}
for key, val in run_inst_pt.iteritems():
if key is not None and key != "":
run_inst_args.append("--%s=%s" % (key,val))
if options.launchpad_id:
run_inst_args.append('--user-data')
run_inst_args.append(CC_IMPORT_SSH % ' '.join(options.launchpad_id))
if options.runargs is not None:
run_inst_args.extend(options.runargs)
run_inst_args.append(ami_id)
# run-instances with pass through args
logging.debug("executing %s",run_inst_args)
logging.info("starting instances with ami_id = %s", ami_id)
rinstances = Popen(run_inst_args, stdout=PIPE)
#INSTANCE i-32697259 ami-2d4aa444 pending 0 m1.small 2010-06-18T18:28:21+0000 us-east-1b aki-754aa41c monitoring-disabled instance-store
try:
for line in rinstances.stdout:
# Just in case there are %'s, don't confusee logging
# XXX print these out instead
logging.debug(line.replace('%','%%').strip())
parts = line.split("\t")
if parts[0] == 'INSTANCE':
pending_instance_ids.append(parts[1])
finally:
rcode = rinstances.wait()
logging.debug("command returned %d", rcode)
logging.info("instances started: %s", pending_instance_ids)
if bool(rcode):
raise Exception('%s failed' % cmd)
if len(pending_instance_ids) < 1:
raise Exception('no instances were started!')
cmd = '%s-describe-instances' % provider
instances = []
timeout_date = time.time() + 600
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(600)
logging.debug("timeout at %s", time.ctime(timeout_date))
# We must wait for ssh to run commands
if options.verify_ssh and not options.wait_for == 'ssh':
logging.info('--verify-ssh implies --wait-for=ssh')
options.wait_for = 'ssh'
if options.ssh_run_cmd and not options.wait_for == 'ssh':
logging.info('--ssh-run-cmd implies --wait-for=ssh')
options.wait_for = 'ssh'
while len(pending_instance_ids):
new_pending_instance_ids = []
describe_inst_args = [ cmd ]
# remove '', confuses underlying commands
pids = []
for iid in pending_instance_ids:
if len(iid):
pids.append(iid)
if len(pids):
describe_inst_args.extend(pending_instance_ids)
logging.debug('running %s',describe_inst_args)
rdescribe = Popen(describe_inst_args, stdout=PIPE)
try:
for line in rdescribe.stdout:
logging.debug(line.replace('%','%%').strip())
parts = line.split("\t")
if parts[0] == 'INSTANCE':
iid = parts[1]
istatus = parts[5]
if istatus == 'terminated':
logging.debug('%s is terminated, ignoring...', iid)
elif istatus != 'running' and options.wait_for:
logging.warning('%s is %s', iid, istatus)
new_pending_instance_ids.append(iid)
elif istatus != 'running' and options.vol:
logging.warning('%s is %s', iid, istatus)
new_pending_instance_ids.append(iid)
else:
logging.info("%s %s", iid, istatus)
inst = Instance()
inst.id = iid
inst.hostname = parts[3]
inst.output = line
instances.append(inst)
finally:
rcode = rdescribe.wait()
pending_instance_ids = new_pending_instance_ids
logging.debug("command returned %d", rcode)
logging.debug("pending instances: %s", pending_instance_ids)
if bool(rcode):
raise Exception('%s failed' % cmd)
if len(pending_instance_ids):
logging.debug('sleeping %d seconds', options.sleep_time)
time.sleep(options.sleep_time)
if options.ip:
ips = options.ip.split(',')
if len(ips) < len(instances):
logging.warning('only %d ips given, some instances will not get an ip', len(ips))
elif len(ips) > len(instances):
logging.warning('%d ips given, some ips will not be associated', len(ips))
rcmds = []
ips.reverse()
for inst in instances:
cmd = '%s-associate-address' % provider
if len(ips) < 1:
break
ip = ips.pop()
aargs = [ cmd , '-i', inst.id, ip ]
logging.debug('running %s',aargs)
rassociate = Popen(aargs, stdout=PIPE)
rcmds.append(rassociate)
for rcmd in rcmds:
# dump stdin into the inst object
try:
for line in rcmd.stdout:
logging.info(line)
finally:
ret = rcmd.wait()
if bool(ret):
logging.debug('associate-ip returned %d', ret)
if options.vol:
# as you can start multiple instances, support multiple vols like ips, instead of multiple volumes on one instance
vols = options.vol.split(',')
if len(vols) < len(instances):
logging.warning('only %d volumes given, some instances will not get a volume attached', len(vols))
elif len(vols) > len(instances):
logging.warning('%d volumes given, some volumes will not be associated', len(vols))
rcmds = []
vols.reverse()
for inst in instances:
# instance needs to be 'running' not 'pending' before attaching volume, otherwise it fails
logging.info('waiting for instance to run')
cmd = '%s-attach-volume' % provider
if len(vols) < 1:
break
vol = vols.pop()
dev = '/dev/sdb'
args = [ cmd , '-i', inst.id, '-d', dev, vol ]
logging.debug('running %s',args)
logging.info("attaching volume with id = %s to instance id = %s", vol, inst.id)
rattach = Popen(args, stdout=PIPE)
rcmds.append(rattach)
for rcmd in rcmds:
# dump stdin into the inst object
try:
for line in rcmd.stdout:
logging.info(line)
finally:
ret = rcmd.wait()
if bool(ret):
logging.debug('attach-volume returned %d', ret)
if options.wait_for == 'ssh':
logging.info('waiting for ssh access')
for inst in instances:
pid = os.fork()
if pid == 0:
ssh_key_scan = SshKeyScanner(inst.id, inst.hostname, options)
ssh_fingerprint = ssh_key_scan.scan()
if options.verify_ssh:
# For ec2, it can take 3.5 minutes or more to get console output, do this last, and only if
# we have to.
cons_fp_scan = ConsoleFingerprintScanner(inst.id, inst.hostname, provider, options)
console_fingerprint = cons_fp_scan.scan()
if console_fingerprint == ssh_fingerprint:
logging.debug('fingerprint match made for iid = %s',inst.id)
else:
raise Exception('fingerprints do not match for iid = %s' % inst.id)
ssh_key_scan.run_commands()
raise SystemExit
else:
logging.debug('child pid for %s is %d', inst.id, pid)
inst.child = pid
logging.info('Waiting for %d children', len(instances))
final_instances = []
for inst in instances:
try:
(pid, status) = os.waitpid(inst.child, 0)
except:
logging.critical('%s - %d doesn\'t exist anymore?', inst.id, pid)
logging.debug('%d returned status %d', pid, status)
if not bool(status):
final_instances.append(inst)
instances = final_instances
""" If we reach here, all has happened in the expected manner so
we should produce the expected output which is instance-id\\tip\\n """
final_instance_ids = []
for inst in instances:
final_instance_ids.append(inst.id)
if options.teardown:
terminate = [ '%s-terminate-instances' % provider ]
terminate.extend(final_instance_ids)
logging.debug('running %s', terminate)
logging.info('terminating instances...')
rterm = Popen(terminate,stdout=sys.stderr,stderr=sys.stderr)
rterm.wait()
if __name__ == "__main__":
main()
# vi:ts=4 noexpandtab
|