def go(self): print "Using the following config:" print json.dumps(self.config) if "instance_id" in self.config: print "Fetching instance", self.config["instance_id"] self.conn = aws_util.connect(self.config["region"], self.aws_key, self.aws_secret_key) self.instance = aws_util.get_instance(conn, self.config["instance_id"]) else: print "Creating instance..." self.instance = self.create_instance() print "Ready to connect..." try: ssl_host = "@".join((self.ssl_user, self.instance.public_dns_name)) print "To connect to it:" print "ssh -i", self.ssl_key_path, ssl_host if not self.config.get("skip_ssh", False): env.key_filename = self.ssl_key_path env.host_string = ssl_host # Can't connect when using known hosts :( env.disable_known_hosts = True # Long-running commands may time out if we don't set this env.keepalive = 5 if aws_util.wait_for_ssh(self.config.get("ssl_retries", 3)): print "SSH Connection is ready." else: print "Failed to establish SSH Connection to", self.instance.id sys.exit(2) if not self.config.get("skip_bootstrap", False): self.bootstrap_instance(self.instance) self.run(self.instance) except Exception, e: print "Launch Error:", e
def get_running_instance(config): conn = aws_util.connect_cfg(config) instance = aws_util.get_instance(conn, config["instance_id"]) print "Instance", instance.id, "is", instance.state return conn, instance
from command import RemoteCommand, run_local_command, get_ssh_client, exec_command import aws_util # def testPing(): # with get_ssh_client('35.174.170.1', username='******', key_filename='/users/rukshani/.ssh/rukshani_cloudlab.pem') as ssh_client: # _, stdout, stderr = ssh_client.exec_command('ping -c 10 -I 172.31.87.253 128.105.145.35 | tail -1 | awk "{print $4}"') # line = stdout.readline() # stderr = stderr.read() # print(line) # print(stderr) # with get_ssh_client('35.170.191.239', username='******', key_filename='/users/rukshani/.ssh/rukshani_cloudlab.pem') as ssh_client: # _, stdout, stderr = ssh_client.exec_command('ping -c 10 -I 172.31.87.253 128.105.145.35 | tail -1 | awk "{print $4}"') # line = stdout.readline() # stderr = stderr.read() # print(line) # print(stderr) instance = aws_util.get_instance('us-east-1') ec2 = aws_util.get_ec2('us-east-1') with get_ssh_client(ip_addr='34.205.63.233', username='******', key_filename='/users/rukshani/.ssh/rukshani_cloudlab.pem' ) as ssh_client: _, stdout, stderr = ssh_client.exec_command( 'echo "TESTING SSH CONNECTION"') line = stdout.readline() stderr = stderr.read() print(line) print(stderr)
if len(sys.argv) < 2: print "Usage:", sys.argv[0], "/path/to/config_file.json" sys.exit(1) config_file = open(sys.argv[1], "r") config = json.load(config_file) config_file.close() print "Using the following config:" print json.dumps(config) if "instance_id" in config: print "Using already-created instance", config["instance_id"] conn = aws_util.connect_cfg(config) instance = aws_util.get_instance(conn, config["instance_id"]) print "Instance", instance.id, "is", instance.state else: conn, instance = aws_util.create_instance(config) # Set up Fabric: ssl_user = config.get("ssl_user", "ubuntu") ssl_key_path = config.get("ssl_key_path", "~/.ssh/id_rsa.pub") ssl_host = "@".join((ssl_user, instance.public_dns_name)) print "To connect to it:" print "ssh -i", ssl_key_path, ssl_host # Use ssh config to specify the correct key and username env.key_filename = config["ssl_key_path"] env.host_string = ssl_host
def _main(git_secret, force_create_instance=False, regions=None, networks=None, force=False): skip_regions = [ 'eu-north-1', 'ap-south-1', 'eu-west-3', 'eu-west-2', 'eu-west-1', 'ap-northeast-2', 'ap-northeast-1', 'sa-east-1', 'ca-central-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'us-east-2', 'us-west-1', 'us-west-2' ] #['us-east-1'] # skip_regions = [] if regions is None: regions = aws_util.get_all_regions() if networks is None: ntwrk_conditions = [(1, 35, 16), (1, 85, 32), (3, 35, 32), (3, 85, 64), (5, 35, 32), (5, 85, 64)] else: ntwrk_conditions = networks logging.info('Found {} regions: {}'.format(len(regions), regions)) # TODO: wait for all created images to be created created_images = [] num_completed_regions = 0 for region in regions: if region in skip_regions: logging.warning('Skipping region {}'.format(region)) continue instance = aws_util.get_instance(region) ec2_region = aws_util.get_ec2(region) if (instance is None) or (force_create_instance): if aws_util.get_key_name(ec2_region, 'rukshani') is None: logging.warning( 'Creating key pair for region {}'.format(region)) aws_util.create_key_pair(ec2_region, region) image = aws_util.get_region_image(region) if image is None: image_id = None else: image_id = image.id logging.info( 'Creating instance for region {} with image {}'.format( region, image.id)) instance = aws_util._region_start_instance(ec2_region, image_id) try: instance.wait_until_running() instance.load() if image is None: #If image is there, assumption is that it has all the cctestbed and everything setup logging.info('Setting up cctestbed on instance') aws_util.setup_ec2(ec2_region, instance, git_secret, ec2_username='******') except Exception as e: instance.stop() raise e aws_util.wait_for_ssh(ec2_region, instance, ec2_username='******') #need to install kernel modules every time aws_util.install_kernel_modules(ec2_region, instance, ec2_username='******')