Ejemplo n.º 1
0
def monitor_simulator(constellation_name,
                      ssh_client,
                      sim_state_key='sim_state'):
    """
    Detects if the simulator is running and writes the
    result into the "gazebo" dictionary key
    """
    if ssh_client is None:
        #constellation.set_value("gazebo", "not running")
        return False

    constellation = ConstellationState(constellation_name)
    simulation_state = constellation.get_value(sim_state_key)
    if machine_states.index(simulation_state) >= \
       machine_states.index('running'):
        gl_state = constellation.get_value("sim_glx_state")
        if gl_state == "running":
            try:
                out = ssh_client.cmd("bash cloudsim/ping_gazebo.bash")
                #log("ping_gazebo returned [%s]" % out)
                if out == "":
                    constellation.set_value("gazebo", "not running")
                    return False
            except Exception, e:
                log("monitor: cloudsim/ping_gazebo.bash error: %s" % e)
                constellation.set_value("gazebo", "not running")
                return False
Ejemplo n.º 2
0
def acquire_aws_constellation(constellation_name,
                              credentials_ec2,
                              machines,
                              scripts,
                              tags):
    """
    Creates a virtual network with machines inside. Each machine has
    - an elastic IP
    - a security group
    - a key (for the ubuntu user)
    """
    constellation = ConstellationState(constellation_name)

    constellation.set_value('machines', machines)

    ec2conn, vpcconn = aws_connect(credentials_ec2)
    vpc_id, subnet_id = _acquire_vpc(constellation_name,
                                     vpcconn)
    log("VPC %s" % vpc_id)
    roles_to_reservations = {}
    for machine_name, machine_data in machines.iteritems():
        aws_key_name = _acquire_key_pair(constellation_name,
                          machine_name, ec2conn)
        security_group_data = machines[machine_name]['security_group']
        _, security_group_id = _acquire_security_group(
                                    constellation_name,
                                    machine_name,
                                    security_group_data,
                                    vpc_id,
                                    ec2conn)
        startup_srcript = scripts[machine_name]
        reservation_id = _acquire_vpc_server(constellation_name,
                                             machine_name,
                                             aws_key_name,
                                             machine_data,
                                             startup_srcript,
                                             subnet_id,
                                             security_group_id,
                                             ec2conn)
        roles_to_reservations[machine_name] = reservation_id

    machines_to_awsid = wait_for_multiple_machines_to_run(ec2conn,
                                            roles_to_reservations,
                                            tags,
                                            constellation,
                                            max_retries=500,
                                            final_state='packages_setup')

    for machine_name, aws_id in machines_to_awsid.iteritems():
        m = "acquiring public Internet IP"
        constellation.set_value("%s_launch_msg" % machine_name, m)
        _acquire_vpc_elastic_ip(constellation_name,
                                machine_name,
                                aws_id,
                                ec2conn)
        if machine_name == "router":
            router_instance = get_ec2_instance(ec2conn, aws_id)
            router_instance.modify_attribute('sourceDestCheck', False)

    log("running machines %s" % machines_to_awsid)
Ejemplo n.º 3
0
def _acquire_vpc_server(constellation_name,
                        machine_prefix,
                        key_pair_name,
                        machine_data,
                        startup_script,
                        subnet_id,
                        security_group_id,
                        ec2conn):
    constellation = ConstellationState(constellation_name)
    try:
        constellation.set_value('%s_launch_msg' % machine_prefix, "booting")
        aws_image = machine_data['software']
        aws_instance = machine_data['hardware']
        ip = machine_data['ip']
        bdm = __get_block_device_mapping(aws_instance)
        constellation_directory = constellation.get_value(
                                                    'constellation_directory')
        # save local startup script copy
        script_fname = os.path.join(constellation_directory,
                                    "%s_startup_script.txt" % machine_prefix)
        with open(script_fname, 'w') as f:
            f.write(startup_script)

        res = ec2conn.run_instances(aws_image,
                         instance_type=aws_instance,
                         subnet_id=subnet_id,
                         private_ip_address=ip,
                         security_group_ids=[security_group_id],
                         key_name=key_pair_name,
                         user_data=startup_script,
                         block_device_map=bdm)
        return res.id
    except:
        raise
Ejemplo n.º 4
0
def _monitor_ping(constellation_name, ping_data_key, ping_str):
    """
    internal implementation for monitor_cloudsim_ping and monitor_ssh_ping
    """
    constellation = ConstellationState(constellation_name)
    latency = constellation.get_value(ping_data_key)
    latency = record_ping_result(latency, ping_str, LATENCY_TIME_BUFFER)
    constellation.set_value(ping_data_key, latency)
Ejemplo n.º 5
0
def monitor_cloudsim_notebook(constellation_name, ssh_client):
    constellation = ConstellationState(constellation_name)
    try:
        out = ssh_client.cmd("bash cloudsim/ping_cloudsim_notebook.bash")
        constellation.set_value(CLOUDSIM_NOTEBOOK_KEY, "running")
    except Exception, e:
        log("monitor: cloudsim/ping_cloudsim_notebook.bash error: %s" % e)
        constellation.set_value(CLOUDSIM_NOTEBOOK_KEY, "")
Ejemplo n.º 6
0
def _release_key_pair(constellation_name, machine_prefix, ec2conn):
    constellation = ConstellationState(constellation_name)
    key_pair_name = None
    try:
        key_pair_name = 'key-%s-%s' % (machine_prefix, constellation_name)
        ec2conn.delete_key_pair(key_pair_name)
    except Exception, e:
        error_msg = constellation.get_value('error')
        error_msg += "<b>Release key</b>: %s<br>" % e
        constellation.set_value('error', error_msg)
        log("error cleaning up simulation key %s: %s" % (key_pair_name, e))
Ejemplo n.º 7
0
class ReloadOsCallBack(object):
    def __init__(self, constellation_name, machines_dict):
        self.constellation_name = constellation_name
        self.machines_dict = machines_dict
        self.constellation_state = ConstellationState(constellation_name)

    def callback(self, machine_name, state):
        msg_key = self.machines_dict[machine_name]
        log("[%s] %s [%s] %s" %
            (self.constellation_name, machine_name, msg_key, state))

        self.constellation_state.set_value(msg_key, state)
Ejemplo n.º 8
0
def _change_ip_addresses(constellation_name, partial_deploy,
                         credentials_softlayer, constellation_directory):

    constellation = ConstellationState(constellation_name)
    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index(
            'change_ip'):
        return
    m = "Setting private IP address"
    constellation.set_value('sim_launch_msg', m)
    constellation.set_value('router_launch_msg', m)

    router_ip = constellation.get_value("router_public_ip")
    ssh_router = SshClient(constellation_directory, "key-router", 'ubuntu',
                           router_ip)
    # change ip on sim
    ssh_router.cmd("cloudsim/set_sim_ip.bash")
    # change ip on router
    ssh_router.cmd("nohup sudo bash change_ip.bash %s"
                   " > ssh_change_ip.out 2> ssh_change_ip.err < /dev/null &" %
                   ROUTER_IP)

    if not partial_deploy:
        constellation.set_value('fc1_launch_msg', m)
        constellation.set_value('fc2_launch_msg', m)
        ssh_router.cmd("cloudsim/set_fc1_ip.bash")
        ssh_router.cmd("cloudsim/set_fc2_ip.bash")

    constellation.set_value("launch_stage", "change_ip")
Ejemplo n.º 9
0
def _release_security_group(constellation_name, machine_prefix, ec2conn):
    constellation = ConstellationState(constellation_name)
    security_group_id = None
    try:
        sg_key = '%s_security_group_id' % machine_prefix
        security_group_id = constellation.get_value(sg_key)
        ec2conn.delete_security_group(group_id=security_group_id)
        return True
    except Exception, e:
        error_msg = constellation.get_value('error')
        error_msg += "<b>%s security group</b>: %s<br>" % (machine_prefix, e)
        constellation.set_value('error', error_msg)
        log("error cleaning up sim security group"
                            " %s: %s" % (security_group_id, e))
Ejemplo n.º 10
0
def _release_vpc_elastic_ip(constellation_name, machine_name_prefix, ec2conn):
    constellation = ConstellationState(constellation_name)
    try:
        allocation_id_key = __get_allocation_id_key(machine_name_prefix)
        eip_allocation_id = constellation.get_value(allocation_id_key)
        log("_release_vpc_elastic_ip %s machine %s id: %s" % (
                                    constellation_name,
                                    machine_name_prefix,
                                    eip_allocation_id))
        ec2conn.release_address(allocation_id=eip_allocation_id)
    except Exception, e:
        error_msg = constellation.get_value('error')
        error_msg += "<b>Router IP address</b>: %s<br>" % e
        constellation.set_value('error', error_msg)
        print("error cleaning up %s elastic ip: %s" % (machine_name_prefix, e))
Ejemplo n.º 11
0
def _acquire_key_pair(constellation_name, machine_prefix, ec2conn):
    constellation = ConstellationState(constellation_name)
    try:
        constellation_directory = constellation.get_value(
                                                    'constellation_directory')
        key_pair_name = 'key-%s-%s' % (machine_prefix, constellation_name)
        key_pair = ec2conn.create_key_pair(key_pair_name)
        key_pair.save(constellation_directory)
        src = os.path.join(constellation_directory, '%s.pem' % key_pair_name)
        dst = os.path.join(constellation_directory,
                                    'key-%s.pem' % machine_prefix)
        shutil.copy(src, dst)
        return key_pair_name
    except Exception, e:
        constellation.set_value('error', "key error: %s" % e)
        raise
Ejemplo n.º 12
0
def monitor_ssh_ping(constellation_name,
                     ssh_client,
                     ip_address,
                     ping_data_key):
    """
    Pings a machine and integrates the results with the existing data into the
    database. The ping is done from the ssh client (i.e router computer)
    """
    if ssh_client is None:
        return
    try:
        ping_str = ssh_client.cmd("ping -c3 %s" % ip_address)
        _monitor_ping(constellation_name, ping_data_key, ping_str)
    except:
        tb = traceback.format_exc()
        log("monitor_ssh_ping traceback:  %s" % tb)
        constellation = ConstellationState(constellation_name)
        constellation.set_value(ping_data_key, "[]")
Ejemplo n.º 13
0
def terminate_dedicated_sl_server(constellation_name, machine_name,
                                  osrf_creds_fname):

    constellation = ConstellationState(constellation_name)
    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index(
            'os_reload'):
        return

    osrf_creds = load_osrf_creds(osrf_creds_fname)
    # compute the softlayer machine names

    machine_names = [machine_name]
    pub_ip, priv_ip, password = get_machine_login_info(osrf_creds,
                                                       machine_names[0])
    log("reload os for machine %s [%s / %s] password %s " %
        (machine_names[0], pub_ip, priv_ip, password))
    reload_servers(osrf_creds, machine_names)
    constellation.set_value("launch_stage", "os_reload")
Ejemplo n.º 14
0
def _acquire_security_group(constellation_name,
                                machine_prefix,
                                security_group_data,
                                vpc_id,
                                ec2conn):
    constellation = ConstellationState(constellation_name)
    sg = None
    try:
        sg_name = '%s-sg-%s' % (machine_prefix, constellation_name)
        dsc = 'machine %s CloudSim constellation %s' % (machine_prefix,
                                            constellation_name,
                                            )
        sg = ec2conn.create_security_group(sg_name, dsc, vpc_id)

        max_try = 10
        i = 0
        while i < max_try:
            log("adding tag to %s/%s security group" % (constellation_name,
                                                machine_prefix))
            try:
                sg.add_tag('constellation', constellation_name)
                log("tag added")
                i = max_try
            except Exception, e:
                log("%s / %s: error: %s" % (i, max_try, e))
                i += 1
                time.sleep(i * 2)
                if i == max_try:
                    raise
        for rule in security_group_data:
            log("authorize %s" % (rule))
            sg.authorize(rule['protocol'],
                         rule['from_port'],
                         rule['to_port'],
                         rule['cidr'])

        security_group_id = sg.id
        security_group_name = sg.name

        constellation.set_value('%s_security_group_id' % machine_prefix,
                                security_group_id)
        constellation.set_value('%s_security_group_name' % machine_prefix,
                                security_group_name)
Ejemplo n.º 15
0
def monitor_launch_state(constellation_name, ssh_client,
                         machine_state,
                         dpkg_cmd,
                         launch_msg_key):

    if ssh_client is None:  # too early to verify
        return
    try:
        constellation = ConstellationState(constellation_name)
        constellation_state = constellation.get_value("constellation_state")
        if constellation_states.index(constellation_state) >= \
           constellation_states.index("launching"):
            if machine_state == 'packages_setup':
                dpkg_line = ssh_client.cmd(dpkg_cmd)
                package_msg = parse_dpkg_line(dpkg_line)
                current_value = constellation.get_value(launch_msg_key)
                if current_value != package_msg:
                    constellation.set_value(launch_msg_key, package_msg)
    except:
        tb = traceback.format_exc()
        log("monitor_launch_state traceback:  %s" % tb)
Ejemplo n.º 16
0
def terminate_softlayer_constellation(constellation_name, constellation_prefix,
                                      partial_reload, osrf_creds_fname):

    constellation = ConstellationState(constellation_name)

    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= \
                             launch_sequence.index('os_reload'):
        return
    else:
        osrf_creds = load_osrf_creds(osrf_creds_fname)
        # compute the softlayer machine names
        constellation.set_value('sim_launch_msg', 'reload OS')
        machine_names = [
            x + "-" + constellation_prefix for x in ('router', 'sim')
        ]
        if not partial_reload:
            machine_names.append('fc2-' + constellation_prefix)
            machine_names.append('fc1-' + constellation_prefix)
            constellation.set_value('fc1_launch_msg', 'reload OS')
            constellation.set_value('fc2_launch_msg', 'reload OS')
        # enable nics on machines with disconnected ones (not the router)
        enable_public_ips(osrf_creds, machine_names[1:])
        for server in machine_names[1:]:
            t = get_active_transaction(osrf_creds, server)
            log("Transaction before reload on %s: %s" % (server, t))
        reload_servers(osrf_creds, machine_names)
        constellation.set_value("launch_stage", "os_reload")
Ejemplo n.º 17
0
def monitor_gzweb(constellation_name, ssh_client, sim_state):
    """
    Detects if the gzweb is running and writes the
    url into the "gzweb" dictionary key
    """

    constellation = ConstellationState(constellation_name)
    simulation_state = constellation.get_value('sim_state')
    if machine_states.index(simulation_state) >= \
       machine_states.index('running'):
        gl_state = constellation.get_value("gazebo")
        if gl_state == "running":
            try:
                # current_state = constellation.get_value(GZWEB_KEY)
                out = ssh_client.cmd("bash cloudsim/ping_gzweb.bash")
                log("ping_gzweb returned [%s]" % out)
                if out == "":
                    constellation.set_value(GZWEB_KEY, "not running")
                    return False
                else:
                    constellation.set_value(GZWEB_KEY, "running")
                    return True
            except Exception, e:
                log("monitor: cloudsim/ping_gzweb.bash error: %s" % e)
                constellation.set_value(GZWEB_KEY, "")
                return False
Ejemplo n.º 18
0
def _shutdown_constellation_public_ips(constellation_name,
                                       constellation_prefix, partial_deploy,
                                       credentials_softlayer,
                                       constellation_directory):

    constellation = ConstellationState(constellation_name)
    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index(
            "block_public_ips"):
        return

    __wait_for_find_file(constellation_name, constellation_directory,
                         partial_deploy, "cloudsim/setup/done", "running")

    m = "Switching off public network interfaces"
    constellation.set_value('sim_launch_msg', m)
    #constellation.set_value('router_launch_msg', m)
    private_machines = ["sim-%s" % constellation_prefix]
    if not partial_deploy:
        private_machines.append("fc1-%s" % constellation_prefix)
        private_machines.append("fc2-%s" % constellation_prefix)
        constellation.set_value('fc1_launch_msg', m)
        constellation.set_value('fc2_launch_msg', m)

    osrf_creds = load_osrf_creds(credentials_softlayer)
    shutdown_public_ips(osrf_creds, private_machines)
    constellation.set_value("launch_stage", "block_public_ips")
Ejemplo n.º 19
0
def _provision_ssh_private_machine(constellation_name, ssh_router,
                                   machine_name_prefix, private_machine_ip,
                                   machine_password, startup_script,
                                   constellation_directory):

    constellation = ConstellationState(constellation_name)
    constellation.set_value('%s_launch_msg' % machine_name_prefix,
                            'User account setup')

    # execute script on router to add ubuntu user on the private machine
    cmd = "cd cloudsim; ./auto_ubuntu.bash %s %s ./key-%s.pem.pub" % (
        private_machine_ip, machine_password, machine_name_prefix)
    log(cmd)
    ssh_router.cmd(cmd)

    local_fname = os.path.join(constellation_directory,
                               '%s_startup.bash' % machine_name_prefix)
    with open(local_fname, 'w') as f:
        f.write(startup_script)
    remote_fname = 'cloudsim/%s_startup_script.bash' % machine_name_prefix
    # send startup script to router
    ssh_router.upload_file(local_fname, remote_fname)
Ejemplo n.º 20
0
def terminate_aws_constellation(constellation_name, credentials_ec2):
    """
    Releases a private network, machines and all its resources
    """
    boto.config = BotoConfig(credentials_ec2)
    ec2conn, vpcconn = aws_connect()
    constellation = ConstellationState(constellation_name)

    machines = constellation.get_value('machines')
    log("machines: %s" % machines.keys())

    running_machines = {}
    for machine_prefix in machines.keys():
        try:
            aws_id_key = '%s_aws_id' % machine_prefix
            aws_id = constellation.get_value(aws_id_key)
            log("%s aws id: %s" % (machine_prefix, aws_id))
            running_machines[machine_prefix] = aws_id
            m = "terminate machine instance"
            constellation.set_value("%s_launch_msg" % machine_prefix, m)
        except Exception, e:
            error_msg = constellation.get_value('error')
            error_msg += " get aws id error %s" % e
            constellation.set_value('error', error_msg)
Ejemplo n.º 21
0
def acquire_openstack_server(constellation_name, creds,
                             constellation_directory, machine_name, script):
    '''
    Calls the launch function.
    Stores the returned values in a redis database
    '''
    floating_ip, instance_name, keypair_name, security_group_name = \
    openstack_launch(constellation_name,
                     machine_name,
                     constellation_directory,
                     script,
                     creds)

    constellation = ConstellationState(constellation_name)
    constellation.set_value("security_group", security_group_name)
    constellation.set_value("keypair", keypair_name)
    constellation.set_value("instance", instance_name)
    constellation.set_value("floating_ip", floating_ip)
    return floating_ip, instance_name, keypair_name
Ejemplo n.º 22
0
def acquire_dedicated_sl_server(constellation_name, osrf_creds_fname,
                                constellation_directory):
    """
    Acquire a dedicated SoftLayer machine
    """
    constellation = ConstellationState(constellation_name)
    constellation_prefix = constellation_name.split("OSRF_CloudSim_")[1]

    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index('init'):
        return

    if os.path.exists(constellation_directory):
        shutil.rmtree(constellation_directory)
    os.makedirs(constellation_directory)

    machines_dict = {'cs-%s' % constellation_prefix: 'simulation_launch_msg'}

    osrf_creds = load_osrf_creds(osrf_creds_fname)
    reload_monitor = ReloadOsCallBack(constellation_name, machines_dict)

    # wait
    wait_for_server_reloads(osrf_creds, machines_dict.keys(),
                            reload_monitor.callback)
    constellation.set_value('simulation_aws_state', 'running')
    constellation.set_value('simulation_state', 'packages_setup')
    name = "cs-%s" % constellation_prefix

    pub_ip, priv_ip, password = get_machine_login_info(osrf_creds, name)
    log("ubuntu user setup for machine cs %s [%s / %s] " %
        (name, pub_ip, priv_ip))
    # dst_dir = os.path.abspath('.')

    log("machine details cs %s %s : %s" % (name, pub_ip, password))
    # __add_ubuntu_user_to_router(pub_ip, password, constellation_directory,
    #                          'key-cs')
    key_prefix = 'key-cs'
    clean_local_ssh_key_entry(pub_ip)
    create_ssh_key(key_prefix, constellation_directory)
    # setup a ubuntu sudoer no password user with an ssh key
    pub_key_path = os.path.join(constellation_directory,
                                "%s.pem.pub" % key_prefix)
    setup_ssh_key_access(pub_ip, password, pub_key_path)
    priv_key_path = os.path.join(constellation_directory,
                                 "%s.pem" % key_prefix)
    log("ssh -i %s ubuntu@%s" % (priv_key_path, pub_ip))

    constellation.set_value("launch_stage", "init")
    return pub_ip, pub_key_path, priv_key_path
Ejemplo n.º 23
0
def _acquire_vpc_elastic_ip(constellation_name,
                            machine_name_prefix,
                            aws_id,
                            ec2conn):
    constellation = ConstellationState(constellation_name)
    try:
        aws_elastic_ip = ec2conn.allocate_address('vpc')
        allocation_id = aws_elastic_ip.allocation_id
        allocation_id_key = __get_allocation_id_key(machine_name_prefix)
        constellation.set_value(allocation_id_key, allocation_id)

        public_ip = aws_elastic_ip.public_ip
        log("%s elastic ip %s" % (machine_name_prefix,
                                  aws_elastic_ip.public_ip))
        ip_key = '%s_public_ip' % machine_name_prefix
        constellation.set_value(ip_key, public_ip)
        #
        # <Errors><Error><Code>InvalidAllocationID.NotFound</Code>
        #
        time.sleep(5)
        max_ = 20
        i = 0
        while i < max_:
            try:
                time.sleep(i * 2)
                ec2conn.associate_address(aws_id, allocation_id=allocation_id)
                i = max_  # leave the loop
            except:
                i += 1
                if i == max_:
                    raise

        clean_local_ssh_key_entry(public_ip)
        return public_ip
    except Exception, e:
        constellation.set_value('error', "Elastic IP error: %s" % e)
        raise
Ejemplo n.º 24
0
def terminate_aws_server(constellation_name, credentials_fname):
    log("terminate AWS CloudSim [constellation %s]" % (constellation_name))
    constellation = ConstellationState(constellation_name)
    ec2conn = None
    machine_prefix = constellation.get_value('machine_name')
    try:
        constellation.set_value('%s_state' % machine_prefix, "terminating")
        constellation.set_value('%s_launch_msg' % machine_prefix,
                                "terminating")

        log("Terminate machine_prefix: %s" % machine_prefix)
        aws_id = constellation.get_value('%s_aws_id' % machine_prefix)
        log("Terminate aws_id: %s" % aws_id)
        running_machines = {machine_prefix: aws_id}
        ec2conn = aws_connect(credentials_fname)[0]
        wait_for_multiple_machines_to_terminate(ec2conn, running_machines,
                                                constellation, max_retries=150)
        constellation.set_value('%s_state' % machine_prefix, "terminated")
        print ('Waiting after killing instances...')
        time.sleep(10.0)
    except Exception as e:
        log("error killing instances: %s" % e)
    constellation.set_value('%s_launch_msg' % machine_prefix, "removing key")
    _release_key_pair(constellation_name, machine_prefix, ec2conn)
    constellation.set_value('%s_launch_msg' % machine_prefix,
                             "removing security group")
    _release_security_group(constellation_name, machine_prefix, ec2conn)
    constellation.set_value('%s_launch_msg' % machine_prefix, "terminated")
Ejemplo n.º 25
0
def _startup_scripts(constellation_name, partial_deploy):

    constellation = ConstellationState(constellation_name)
    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index('startup'):
        return

    constellation_directory = constellation.get_value(
        'constellation_directory')
    # if the change of ip was successful, the script should be in the home
    # directory of each machine
    __wait_for_find_file(constellation_name, constellation_directory,
                         partial_deploy, "change_ip.bash", "packages_setup")

    m = "Executing startup script"
    constellation.set_value('sim_launch_msg', m)
    constellation.set_value('router_launch_msg', m)

    router_ip = constellation.get_value("router_public_ip")
    ssh_router = SshClient(constellation_directory, "key-router", 'ubuntu',
                           router_ip)
    # load packages onto router
    ssh_router.cmd("nohup sudo bash cloudsim/router_startup_script.bash "
                   "> ssh_startup.out 2> ssh_startup.err < /dev/null &")
    # load packages onto simulator
    ssh_router.cmd("cloudsim/sim_init.bash")

    if not partial_deploy:
        constellation.set_value('fc1_launch_msg', m)
        constellation.set_value('fc2_launch_msg', m)
        # load packages onto fc1
        ssh_router.cmd("cloudsim/fc1_init.bash")
        # load packages onto fc2
        ssh_router.cmd("cloudsim/fc2_init.bash")
        constellation.set_value("fc1_state", "packages_setup")
        constellation.set_value("fc2_state", "packages_setup")

    constellation.set_value("sim_state", "packages_setup")
    constellation.set_value("router_state", "packages_setup")
    constellation.set_value("launch_stage", "startup")
Ejemplo n.º 26
0
def _initialize_private_machines(constellation_name, constellation_prefix,
                                 partial_deploy, credentials_softlayer,
                                 router_script, sim_script, fc1_script,
                                 fc2_script, constellation_directory):

    constellation = ConstellationState(constellation_name)
    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= \
                                    launch_sequence.index('init_privates'):
        return
    #
    # router machine
    #
    router_ip = constellation.get_value("router_public_ip")
    ssh_router = SshClient(constellation_directory, "key-router", 'ubuntu',
                           router_ip)

    local_fname = os.path.join(constellation_directory, 'router_startup.bash')
    with open(local_fname, 'w') as f:
        f.write(router_script)
    remote_fname = 'cloudsim/router_startup_script.bash'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))

    osrf_creds = load_osrf_creds(credentials_softlayer)
    #
    # sim machine
    #
    sim_pub_ip, sim_priv_ip, sim_root_password = get_machine_login_info(
        osrf_creds, "sim-%s" % constellation_prefix)

    log("provision sim [%s / %s] %s" %
        (sim_pub_ip, sim_priv_ip, sim_root_password))
    _provision_ssh_private_machine(constellation_name, ssh_router, "sim",
                                   sim_priv_ip, sim_root_password, sim_script,
                                   constellation_directory)
    if not partial_deploy:
        #
        # fc1 machine
        #
        fc1_pub_ip, fc1_priv_ip, fc1_root_password = get_machine_login_info(
            osrf_creds, "fc1-%s" % constellation_prefix)

        log("provision fc1 [%s / %s] %s" %
            (fc1_pub_ip, fc1_priv_ip, fc1_root_password))
        _provision_ssh_private_machine(constellation_name, ssh_router, "fc1",
                                       fc1_priv_ip, fc1_root_password,
                                       fc1_script, constellation_directory)
        #
        # fc2 machine
        #
        fc2_pub_ip, fc2_priv_ip, fc2_root_password = get_machine_login_info(
            osrf_creds, "fc2-%s" % constellation_prefix)

        log("provision fc2 [%s / %s] %s" %
            (fc2_pub_ip, fc2_priv_ip, fc2_root_password))

        _provision_ssh_private_machine(constellation_name, ssh_router, "fc2",
                                       fc2_priv_ip, fc2_root_password,
                                       fc2_script, constellation_directory)

    log('configure_machines done')
    constellation.set_value("launch_stage", "init_privates")
Ejemplo n.º 27
0
def _acquire_vpc(constellation_name, vpcconn):
    constellation = ConstellationState(constellation_name)
    vpc_id = None
    subnet_id = None
    try:
        constellation.set_value('router_launch_msg',
                                "creating virtual private network")
        aws_vpc = vpcconn.create_vpc(VPN_PRIVATE_SUBNET)
        vpc_id = aws_vpc.id
        constellation.set_value('vpc_id', vpc_id)

        # this operation fails on AWS end for no good reason sometimes
        aws_subnet = None
        time.sleep(5)
        max_ = 20
        i = 0
        while i < max_:
            try:
                time.sleep(i * 2)
                aws_subnet = vpcconn.create_subnet(vpc_id, VPN_PRIVATE_SUBNET)
                i = max_  # leave the loop
            except:
                i += 1
                if i == max_:
                    raise  # raise EC2 original error

        subnet_id = aws_subnet.id
        constellation.set_value('subnet_id', subnet_id)

        constellation.set_value('router_launch_msg',
                                "setting up internet gateway")

        igw_id = vpcconn.create_internet_gateway().id
        constellation.set_value('igw_id', igw_id)
        vpcconn.attach_internet_gateway(igw_id, vpc_id)

        constellation.set_value('router_launch_msg', "creating routing tables")
        route_table_id = vpcconn.create_route_table(vpc_id).id
        constellation.set_value('route_table_id', route_table_id)

        vpcconn.create_route(route_table_id, '0.0.0.0/0', igw_id)
        route_table_association_id = vpcconn.associate_route_table(
                                                            route_table_id,
                                                            subnet_id)
        constellation.set_value('route_table_association_id',
                                route_table_association_id)

        i = 0
        while i < 5:
            # add a tag to the vpc so we can identify it
            try:
                log('adding tag to VPC %s' % i)
                aws_vpc.add_tag('constellation', constellation_name)
                i = 10
            except:
                i += 1
                time.sleep(i * 2)
    except Exception as e:
        constellation.set_value('error', "%s" % e)
        raise
    return vpc_id, subnet_id
Ejemplo n.º 28
0
def _initialize_router(constellation_name, constellation_prefix,
                       partial_deploy, osrf_creds_fname,
                       constellation_directory):

    constellation = ConstellationState(constellation_name)

    launch_stage = constellation.get_value("launch_stage")
    if launch_sequence.index(launch_stage) >= launch_sequence.index(
            'init_router'):
        return

    osrf_creds = load_osrf_creds(osrf_creds_fname)
    router_name = "router-%s" % constellation_prefix

    m = "running"
    constellation.set_value('fc1_aws_state', m)
    constellation.set_value('fc2_aws_state', m)
    constellation.set_value('sim_aws_state', m)
    constellation.set_value('router_aws_state', m)

    constellation.set_value('router_launch_msg', 'user account setup')
    router_ip, priv_ip, password = get_machine_login_info(
        osrf_creds, router_name)
    constellation.set_value("router_public_ip", router_ip)
    log("router %s %s" % (router_ip, password))

    sim_pub_ip, sim_priv_ip, sim_root_password = get_machine_login_info(
        osrf_creds, "sim-%s" % constellation_prefix)
    log("sim %s %s" % (sim_pub_ip, sim_root_password))

    log("ubuntu user setup for machine router %s [%s / %s] " %
        (router_name, router_ip, priv_ip))

    log("router %s %s : %s" % (router_name, router_ip, password))
    __add_ubuntu_user_to_router(router_ip, password, constellation_directory)

    softlayer_scripts_dir = os.path.join(os.path.dirname(softlayer.__file__),
                                         'bash')

    ssh_router = SshClient(constellation_directory, "key-router", 'ubuntu',
                           router_ip)

    # create a remote cloudsim directory on the router
    ssh_router.cmd("mkdir -p cloudsim")

    openvpn_fname = os.path.join(constellation_directory, 'openvpn.key')
    create_openvpn_key(openvpn_fname)
    remote_fname = 'cloudsim/openvpn.key'
    ssh_router.upload_file(openvpn_fname, remote_fname)

    local_fname = os.path.join(softlayer_scripts_dir, 'router_init.bash')
    remote_fname = 'cloudsim/router_init.bash'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))

    # upload ubuntu user setup scripts
    local_fname = os.path.join(softlayer_scripts_dir, 'auto_ubuntu.bash')
    remote_fname = 'cloudsim/auto_ubuntu.bash'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))

    local_fname = os.path.join(softlayer_scripts_dir, 'create_ubuntu_user.exp')
    remote_fname = 'cloudsim/create_ubuntu_user.exp'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))

    local_fname = os.path.join(softlayer_scripts_dir, 'upload_key.exp')
    remote_fname = 'cloudsim/upload_key.exp'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))

    local_fname = os.path.join(softlayer_scripts_dir,
                               'process_remote_ssh_key.exp')
    remote_fname = 'cloudsim/process_remote_ssh_key.exp'
    ssh_router.upload_file(local_fname, remote_fname)
    log("upload %s to %s" % (local_fname, remote_fname))
    # avoid ssh error because our server has changed
    constellation.set_value("launch_stage", "init_router")

    create_ssh_key("key-sim", constellation_directory)

    fc1_priv_ip = "0.0.0.0"
    fc2_priv_ip = "0.0.0.0"

    if not partial_deploy:
        fc1_pub_ip, fc1_priv_ip, fc1_root_password = get_machine_login_info(
            osrf_creds, "fc1-%s" % constellation_prefix)
        log("fc1 %s %s" % (fc1_pub_ip, fc1_root_password))
        create_ssh_key("key-fc1", constellation_directory)

        fc2_pub_ip, fc2_priv_ip, fc2_root_password = get_machine_login_info(
            osrf_creds, "fc2-%s" % constellation_prefix)
        log("fc2 %s %s" % (fc2_pub_ip, fc2_root_password))
        create_ssh_key("key-fc2", constellation_directory)

    arg_str = "%s %s %s" % (sim_priv_ip, fc1_priv_ip, fc2_priv_ip)
    cmd = "cd cloudsim; ./router_init.bash /home/ubuntu/cloudsim %s" % arg_str
    # run the script
    constellation.set_value('router_launch_msg', 'generating bash scripts')
    ssh_router.cmd(cmd)

    constellation.set_value('router_launch_msg', 'uploading ssh key')
    __upload_ssh_keys_to_router(ssh_router, "router", constellation_directory)
    constellation.set_value('sim_launch_msg', 'uploading ssh key')
    __upload_ssh_keys_to_router(ssh_router, "sim", constellation_directory)
    constellation.set_value('fc1_launch_msg', 'uploading ssh key')
    __upload_ssh_keys_to_router(ssh_router, "fc1", constellation_directory)
    constellation.set_value('fc2_launch_msg', 'uploading ssh key')
    __upload_ssh_keys_to_router(ssh_router, "fc2", constellation_directory)
Ejemplo n.º 29
0
def acquire_aws_single_server(constellation_name,
                       credentials_ec2,
                       constellation_directory,
                       machine_prefix,  # name of machine, ie "sim"
                       machine_data,
                       startup_script,
                       tags):
    sim_machine_name = "%s_%s" % (machine_prefix, constellation_name)

    ec2conn, _ = aws_connect(credentials_ec2)
    aws_image = machine_data['software']
    aws_instance = machine_data['hardware']

    bdm = __get_block_device_mapping(aws_instance)

    constellation = ConstellationState(constellation_name)
    constellation.set_value('%s_launch_msg' % machine_prefix,
                            "setting up security groups")

    constellation_directory = constellation.get_value(
                                                    'constellation_directory')
    # save local startup script copy
    script_fname = os.path.join(constellation_directory,
                                    "%s_startup_script.txt" % machine_prefix)
    with open(script_fname, 'w') as f:
            f.write(startup_script)

    constellation.set_value('machine_name', machine_prefix)
    security_group_data = machine_data['security_group']
    security_group_name, _ = _acquire_security_group(
                                    constellation_name,
                                    machine_prefix,
                                    security_group_data,
                                    vpc_id=None,
                                    ec2conn=ec2conn)

    key_pair_name = _acquire_key_pair(constellation_name,
                                      machine_prefix,
                                      ec2conn)

    roles_to_reservations = {}
    try:
        constellation.set_value('%s_launch_msg' % machine_prefix,
                                "requesting machine")
        res = ec2conn.run_instances(image_id=aws_image,
            instance_type=aws_instance,
            #subnet_id      = subnet_id,
            #private_ip_address=SIM_IP,
            security_groups=[security_group_name],
            key_name=key_pair_name,
            user_data=startup_script,
            block_device_map=bdm)
        roles_to_reservations['simulation_state'] = res.id
    except:
        log("ouch!")
        raise

    aws_id = None
    count = 200
    done = False

    while not done:
        log("attempt %s" % count)
        time.sleep(2)
        count -= 1
        for r in ec2conn.get_all_instances():
            if count < 0:
                msg = ("timeout while waiting "
                       "for EC2 machine(s) %s" % sim_machine_name)
                raise LaunchException(msg)
            if r.id == res.id:
                state = r.instances[0].state
                if state == 'running':
                    aws_id = r.instances[0].id
                    constellation.set_value('%s_state' % machine_prefix,
                                            'network_setup')
                    done = True
                constellation.set_value('%s_aws_state' % machine_prefix, state)
    constellation.set_value('%s_launch_msg' % machine_prefix,
                            "machine running")
    constellation.set_value('%s_aws_id' % machine_prefix, aws_id)
    sim_tags = {'Name': sim_machine_name}
    sim_tags.update(tags)
    ec2conn.create_tags([aws_id], sim_tags)

    # ec2conn.associate_address(router_aws_id, allocation_id=eip_allocation_id)
    instance = get_ec2_instance(ec2conn, aws_id)
    ip = instance.ip_address
    clean_local_ssh_key_entry(ip)
    constellation.set_value('%s_public_ip' % (machine_prefix), ip)
    return ip, aws_id, key_pair_name