Ejemplo n.º 1
0
def launch_new_server(ramnode_account, is_TCS, plugins, multi_ip=False):

    ramnode = None
    ramnode_id = None
    ramnode_api = PsiRamnode(ramnode_account)  # Use new API interface

    if is_TCS:
        # New APIv4 require root_pass when create disk from image
        root_password = ramnode_account.tcs_base_root_password
        host_public_key = ramnode_account.tcs_base_host_public_key

    try:
        hostname = 'rn-' + ramnode_api.get_region(
            ramnode_api.region).lower() + ''.join(
                random.choice(string.ascii_lowercase) for x in range(8))

        # Create a new node
        new_root_password = psi_utils.generate_password()
        ramnode, datacenter_name, region = ramnode_api.create_ramnode(hostname)

        ramnode_ip_address = ramnode.addresses['Public'][0]['addr']
        egress_ip_address = None

        if is_TCS:
            # Ramnodes created by an image keep the image's hostname.  Override this
            set_host_name(ramnode_account, ramnode_ip_address, root_password,
                          host_public_key, hostname)
            stats_username = psi_utils.generate_stats_username()
            set_allowed_users(ramnode_account, ramnode_ip_address,
                              root_password, host_public_key, stats_username)

        # Change the new node's credentials
        new_stats_password = psi_utils.generate_password()
        new_host_public_key = refresh_credentials(
            ramnode_account, ramnode_ip_address, root_password,
            host_public_key, new_root_password, new_stats_password,
            stats_username)

    except Exception as ex:
        if ramnode:
            ramnode_api.remove_ramnode(ramnode.id)
        raise ex

    return (hostname, is_TCS, 'NATIVE' if is_TCS else None, None,
            str(ramnode.id), ramnode_ip_address, ramnode_account.base_ssh_port,
            'root', new_root_password,
            ' '.join(new_host_public_key.split(' ')[:2]), stats_username,
            new_stats_password, datacenter_name, region, None, None)
Ejemplo n.º 2
0
def launch_new_server(scaleway_account, is_TCS, plugins, multi_ip=False):

    scaleway = None
    scaleway_api = PsiScaleway(scaleway_account) # Use new API interface

    try:
        # Create a new scaleway
        region = scaleway_api.get_region()
        host_id = 'sw' + '-' + region.lower() + ''.join(random.choice(string.ascii_lowercase) for x in range(8))
        scaleway, datacenter_name, region = scaleway_api.create_scaleway(host_id)

        scaleway_ip_address = scaleway['public_ip']['address']
        scaleway_internal_ip_address = scaleway['private_ip']

        new_stats_username = psi_utils.generate_stats_username()
        # scaleways created by an image keep the image's hostname.  Override this
        set_host_name(scaleway_account, scaleway_ip_address, host_id)
        set_allowed_users(scaleway_account, scaleway_ip_address, new_stats_username)
        add_swap_file(scaleway_account, scaleway_ip_address)

        # Change the new scaleway's credentials
        new_root_password = psi_utils.generate_password()
        new_stats_password = psi_utils.generate_password()
        new_host_public_key = refresh_credentials(scaleway_account, scaleway_ip_address,
                                                  new_root_password, new_stats_password,
                                                  new_stats_username)

    except Exception as ex:
        if scaleway:
            scaleway_api.remove_scaleway(scaleway['id'])
        raise ex

    return (host_id, is_TCS, 'NATIVE' if is_TCS else None, None,
            scaleway_api.region + '_' + scaleway['id'], scaleway_ip_address,
            scaleway_account.base_ssh_port, 'root', new_root_password,
            ' '.join(new_host_public_key.split(' ')[:2]),
            new_stats_username, new_stats_password,
            datacenter_name, region, None, scaleway_internal_ip_address)
Ejemplo n.º 3
0
def launch_new_server(vpsnet_account, is_TCS, _, datacenter_city=None):
    """
        launch_new_server is called from psi_ops.py to create a new server.
    """

    # TODO-TCS: select base image based on is_TCS flag
    base_image_id = '8849'  # For VPS
    # base_image_id = '8850' # For Cloud Server

    try:
        VPSNetHost = collections.namedtuple('VPSNetHost', [
            'ssd_vps_plan', 'fqdn', 'system_template_id', 'cloud_id',
            'backups_enabled', 'rsync_backups_enabled', 'licenses'
        ])

        vpsnet_conn = get_vpsnet_connection(vpsnet_account)

        # Get a list of regions (clouds) that can be used
        vpsnet_clouds = vpsnet_conn.get_available_ssd_clouds()
        # vpsnet_clouds = vpsnet_conn.get_available_clouds()

        # Check each available cloud for a psiphon template to use.
        # Populate a list of templates and the cloud IDs.
        psiphon_templates = list()
        print 'Available Regions:\n'
        for region in vpsnet_clouds:
            print '%s -> %s' % (region['cloud']['id'],
                                region['cloud']['label'])
            for template in region['cloud']['system_templates']:
                if 'psiphond-template' in template['label'].lower() and str(
                        template['id']) == base_image_id:
                    print '\tFound psiphon template id %s in region %s' % (
                        template['id'], region['cloud']['id'])
                    template['cloud_id'] = region['cloud']['id']
                    template['cloud_label'] = region['cloud']['label']
                    psiphon_templates.append(template)

        if datacenter_city != None:
            region_template = [
                s for s in psiphon_templates
                if datacenter_city in s['cloud_label'].lower()
            ][0]
        else:
            region_template = random.choice(psiphon_templates)
        VPSNetHost.cloud_id = region_template['cloud_id']
        VPSNetHost.system_template_id = region_template['id']

        print 'Using template: %s with cloud_id: %s' % (
            VPSNetHost.system_template_id, VPSNetHost.cloud_id)
        '''
            package/plan for the new SSD server.
            (VPS 1GB - 1, VPS 2GB - 2, VPS 4GB - 3, VPS 8GB - 4, VPS 16GB - 5)
        '''

        host_id = 'vn-' + ''.join(
            random.choice(string.ascii_lowercase) for x in range(8))

        VPSNetHost.name = str(host_id)
        VPSNetHost.ssd_vps_plan = vpsnet_account.base_ssd_plan
        VPSNetHost.fqdn = str(host_id + '.vps.net')
        VPSNetHost.backups_enabled = False
        VPSNetHost.rsync_backups_enabled = False
        VPSNetHost.licenses = None

        node = vpsnet_conn.create_ssd_node(
            fqdn=VPSNetHost.fqdn,
            image_id=VPSNetHost.system_template_id,
            cloud_id=VPSNetHost.cloud_id,
            size=VPSNetHost.ssd_vps_plan,
            backups_enabled=VPSNetHost.backups_enabled,
            rsync_backups_enabled=VPSNetHost.rsync_backups_enabled,
        )

        # node = vpsnet_conn.create_node(
        #     name=VPSNetHost.name,
        #     image_id=VPSNetHost.system_template_id,
        #     cloud_id=VPSNetHost.cloud_id,
        #     size=VPSNetHost.ssd_vps_plan,
        #     backups_enabled=VPSNetHost.backups_enabled,
        #     rsync_backups_enabled=VPSNetHost.rsync_backups_enabled,
        #     ex_fqdn=VPSNetHost.fqdn,
        #     )

        if not wait_on_action(vpsnet_conn, node, 30):
            raise "Could not power on node"
        else:
            node = vpsnet_conn.get_ssd_node(node.id)
            #node = vpsnet_conn.get_node(node.id)

        generated_root_password = node.extra['password']

        # Get the Node IP address
        if isinstance(node.public_ips, list):
            for public_ip in node.public_ips:
                if 'ip_address' in (public_ip and public_ip['ip_address']):
                    public_ip_address = public_ip['ip_address']['ip_address']

        if is_TCS:
            stats_username = psi_utils.generate_stats_username()
        elif not is_TCS:
            stats_username = vpsnet_account.base_stats_username

        new_root_password = psi_utils.generate_password()
        new_stats_password = psi_utils.generate_password()
        node_public_key = refresh_credentials(
            vpsnet_account, public_ip_address, generated_root_password,
            new_root_password, new_stats_password, stats_username)
        set_allowed_users(vpsnet_account, public_ip_address, new_root_password,
                          stats_username)
    except Exception as e:
        print type(e), str(e)
        if node is not None:
            remove_server(vpsnet_account, node.id)
        else:
            print type(e), "No node to be destoryed: %s", str(node)
        raise

    return (
        host_id,
        is_TCS,
        'NATIVE' if is_TCS else None,
        None,
        node.id,
        public_ip_address,
        vpsnet_account.base_ssh_port,
        'root',
        new_root_password,
        ' '.join(node_public_key.split(' ')[:2]),
        stats_username,
        new_stats_password,
        region_template['cloud_label'],
        get_region_name(region_template),
        None,
        None,
        None,
        None,
        None,
        None,
        None,
    )
def launch_new_server(digitalocean_account, is_TCS, _, multi_ip=False):
    """
        Launches a new droplet and configures it to be a Psiphon server.
        This is called from psi_ops.py

        digitalocean_account    :   DigitalOcean account information

        returns:
            instance of a psinet server
    """

    # None TCS id '25617090' only for VPN + filebeat
    # Old working None TCS id: 17784624
    base_id = '25617090' if not is_TCS else '59795497'
    try:
        Droplet = collections.namedtuple(
            'Droplet', ['name', 'region', 'image', 'size', 'backups'])

        new_root_password = psi_utils.generate_password()
        new_stats_password = psi_utils.generate_password()

        stats_username = digitalocean_account.base_stats_username

        do_mgr = digitalocean.Manager(token=digitalocean_account.oauth_token)

        # Get the base image
        base_image = do_mgr.get_image(base_id)
        if not base_image:
            raise Exception("Base image with ID: %s is not found" % (base_id))
        Droplet.image = base_image.id

        # Set the default size
        droplet_sizes = do_mgr.get_all_sizes()
        if not unicode(digitalocean_account.base_size_slug) in [
                unicode(s.slug) for s in droplet_sizes
        ]:
            raise 'Size slug not found'

        Droplet.size = 's-2vcpu-4gb'

        droplet_regions = do_mgr.get_all_regions()
        common_regions = list(
            set([r.slug for r in droplet_regions
                 if r.available]).intersection(base_image.regions))

        Droplet.region = random.choice(common_regions)

        # Use new host_id standard
        Droplet.name = str('do-' +
                           get_datacenter_region(Droplet.region).lower() +
                           ''.join(
                               random.choice(string.ascii_lowercase)
                               for x in range(8)))

        sshkeys = do_mgr.get_all_sshkeys()
        # treat sshkey id as unique
        if not unicode(digitalocean_account.ssh_key_template_id) in [
                unicode(k.id) for k in sshkeys
        ]:
            raise 'No SSHKey found'

        droplet = digitalocean.Droplet(
            token=digitalocean_account.oauth_token,
            name=Droplet.name,
            region=Droplet.region,
            image=Droplet.image,
            size=Droplet.size,
            ssh_keys=[int(digitalocean_account.ssh_key_template_id)],
            backups=False)

        droplet.create()
        if not wait_on_action(do_mgr, droplet, None, 30, 'create',
                              'completed'):
            raise Exception('Event did not complete in time')

        droplet = do_mgr.get_droplet(droplet.id)

        region = get_datacenter_region(droplet.region['slug'])
        datacenter_name = 'Digital Ocean ' + droplet.region['name']

        if is_TCS:
            set_host_name(digitalocean_account, droplet.ip_address,
                          new_root_password, droplet.name)
            stats_username = psi_utils.generate_stats_username()
            set_allowed_users(digitalocean_account, droplet.ip_address,
                              new_root_password, stats_username)

        new_droplet_public_key = refresh_credentials(digitalocean_account,
                                                     droplet.ip_address,
                                                     new_root_password,
                                                     new_stats_password,
                                                     stats_username)
        assert (new_droplet_public_key)

    except Exception as e:
        print type(e), str(e)
        if droplet is not None:
            droplet.destroy()
        else:
            print type(e), "No droplet to be destroyed: ", str(droplet)
        raise

    return (droplet.name, is_TCS, 'NATIVE' if is_TCS else None, None,
            droplet.id, droplet.ip_address, digitalocean_account.base_ssh_port,
            'root', new_root_password,
            ' '.join(new_droplet_public_key.split(' ')[:2]), stats_username,
            new_stats_password, datacenter_name, region, None, None, None,
            None, None, None, None, None)
Ejemplo n.º 5
0
def launch_new_server(linode_account, is_TCS, plugins, multi_ip=False):

    linode = None
    linode_id = None
    linode_api = PsiLinode(linode_account)  # Use new API interface

    if is_TCS:
        # New APIv4 require root_pass when create disk from image
        root_password = linode_account.tcs_base_root_password
        host_public_key = linode_account.tcs_base_host_public_key
    else:
        # Power on the base image linode if it is not already running
        if linode_api.linode_status(linode_account.base_id) != 'running':
            linode_api.start_linode(
                linode_account.base_id)  # Lagecy psiphon servers, NOT TESTED

        # root_password = linode_account.base_root_password
        # in order to use New APIv4 and make sure the legacy host also working as before, generate a root_password ahead
        root_password = psi_utils.generate_password()
        host_public_key = linode_account.base_host_public_key

    try:
        # Create a new linode
        new_root_password = psi_utils.generate_password()
        linode, datacenter_name, region = linode_api.create_linode()

        if multi_ip:
            linode_second_ip_address = linode_api.pubip_allocate(linode)

        disk_ids = linode_api.create_linode_disks(linode, root_password,
                                                  is_TCS, plugins)
        bootstrap_config, psiphon3_host_config = linode_api.create_linode_configurations(
            linode, disk_ids, is_TCS, plugins, multi_ip)

        # Clone the base linode
        linode_ip_details = linode.ips.ipv4.public
        linode_ip_address = linode_ip_details[0].address
        egress_ip_address = None

        linode_rdns_name = linode_ip_details[0].rdns.split('.', 1)[0]
        host_id = 'li-' + region.lower() + ''.join(
            random.choice(string.ascii_lowercase) for x in range(8))

        if not is_TCS:
            # Lagecy psiphon servers, NOT TESTED
            linode_api.start_linode(linode.id, config=bootstrap_config_id)
            pave_linode(linode_account, linode_ip_address, root_password)
            linode_api.stop_linode(linode.id)

        linode_api.start_linode(linode.id, config=psiphon3_host_config)
        stats_username = linode_account.base_stats_username

        if is_TCS:
            print(linode.label)
            # Linodes created by an image keep the image's hostname.  Override this
            set_host_name(linode_account, linode_ip_address, root_password,
                          host_public_key, host_id)
            stats_username = psi_utils.generate_stats_username()
            set_allowed_users(linode_account, linode_ip_address, root_password,
                              host_public_key, stats_username)

        # Query hostname
        hostname = get_host_name(linode_account, linode_ip_address,
                                 root_password, host_public_key)

        # Change the new linode's credentials
        new_stats_password = psi_utils.generate_password()
        new_host_public_key = refresh_credentials(
            linode_account, linode_ip_address, root_password, host_public_key,
            new_root_password, new_stats_password, stats_username)

        if multi_ip:
            egress_ip_address = get_egress_ip_address(linode_account,
                                                      linode_ip_address,
                                                      new_root_password,
                                                      new_host_public_key)
            linode_ip_address = linode_ip_details[
                1].address if linode_ip_address == egress_ip_address else linode_ip_address

    except Exception as ex:
        if linode:
            linode_api.remove_linode(linode.id)
        raise ex
    finally:
        # Power down the base image linode
        #stop_linode(linode_api, linode_account.base_id)
        # New: we'll leave this on now due to parallelization
        pass

    return (hostname, is_TCS, 'NATIVE' if is_TCS else None, None, str(
        linode.id), linode_ip_address, linode_account.base_ssh_port, 'root',
            new_root_password, ' '.join(new_host_public_key.split(' ')[:2]),
            stats_username, new_stats_password, datacenter_name, region,
            egress_ip_address, None)
def launch_new_server(linode_account, is_TCS, plugins):

    linode_id = None
    linode_api = linode.api.Api(key=linode_account.api_key)

    if is_TCS:
        root_password = linode_account.tcs_base_root_password
        host_public_key = linode_account.tcs_base_host_public_key
    else:
        # Power on the base image linode if it is not already running
        if linode_api.linode_list(
                LinodeID=linode_account.base_id)[0]['STATUS'] != 1:
            start_linode(linode_api, linode_account.base_id, None)

        root_password = linode_account.base_root_password
        host_public_key = linode_account.base_host_public_key

    try:
        # Create a new linode
        new_root_password = psi_utils.generate_password()
        linode_id, datacenter_name, region = create_linode(linode_api)

        disk_ids = create_linode_disks(linode_api, linode_id,
                                       new_root_password, is_TCS, plugins)
        bootstrap_config_id, psiphon3_host_config_id = create_linode_configurations(
            linode_api, linode_id, ','.join(disk_ids), is_TCS, plugins)

        # Clone the base linode
        linode_ip_details = linode_api.linode_ip_list(LinodeID=linode_id)
        linode_ip_address = linode_ip_details[0]['IPADDRESS']
        linode_rdns_name = linode_ip_details[0]['RDNS_NAME'].split('.', 1)[0]

        if not is_TCS:
            start_linode(linode_api, linode_id, bootstrap_config_id)
            pave_linode(linode_account, linode_ip_address, new_root_password)
            stop_linode(linode_api, linode_id)

        start_linode(linode_api, linode_id, psiphon3_host_config_id)

        stats_username = linode_account.base_stats_username

        if is_TCS:
            # Linodes created by an image keep the image's hostname.  Override this
            set_host_name(linode_account, linode_ip_address, root_password,
                          host_public_key, linode_rdns_name)
            stats_username = psi_utils.generate_stats_username()
            set_allowed_users(linode_account, linode_ip_address, root_password,
                              host_public_key, stats_username)

        # Query hostname
        hostname = get_host_name(linode_account, linode_ip_address,
                                 root_password, host_public_key)

        # Change the new linode's credentials
        new_stats_password = psi_utils.generate_password()
        new_host_public_key = refresh_credentials(
            linode_account, linode_ip_address, root_password, host_public_key,
            new_root_password, new_stats_password, stats_username)
    except Exception as ex:
        if linode_id:
            remove_server(linode_account, linode_id)
        raise
    finally:
        # Power down the base image linode
        #stop_linode(linode_api, linode_account.base_id)
        # New: we'll leave this on now due to parallelization
        pass

    return (hostname, is_TCS, None, str(linode_id), linode_ip_address,
            linode_account.base_ssh_port, 'root', new_root_password,
            ' '.join(new_host_public_key.split(' ')[:2]), stats_username,
            new_stats_password, datacenter_name, region, None, None, None,
            None)
Ejemplo n.º 7
0
def launch_new_server(vps247_account, is_TCS, _, multi_ip=False):

    # Try launch New VPS 247 Server

    try:
        v247_api = v247.Api(key=vps247_account.api_key)

        # Get random choice region id or use default region id: 25 (ES)
        if vps247_account.base_region_id != 0:
            region_id = vps247_account.base_region_id
        else:
            region_id = get_region_id(
                random_pick_region(v247_api.get_all_regions()))

        # Get preset default package id
        package_id = vps247_account.base_package_id

        # Hostname generator
        hostname = generate_host_id()

        # Create VPS node
        instance_id = v247_api.create_vm(hostname, region_id, package_id)

        print 'Waiting for the instance to power on and get instance information'
        time.sleep(60)

        instance = v247_api.get_vm(str(instance_id))

        instance_ip_address = instance['ip_assignments'][0]['ip_address']
        instance_init_password = str(instance['initial_password'])

        print 'Reset init password to default root password'
        reset_root_password(vps247_account, instance_ip_address,
                            instance_init_password)
        print 'Installing TCS Dependencies..'
        install_tcs(vps247_account, instance_ip_address)

        print 'Waiting for TCS installation finished.'
        time.sleep(150)  # Time needed to reboot the instances

        upload_certs(vps247_account, instance_ip_address)
        add_swap_file(vps247_account, instance_ip_address)

        print 'Refreshing Credential..'
        new_root_password = psi_utils.generate_password()
        new_stats_password = psi_utils.generate_password()
        new_stats_username = psi_utils.generate_stats_username()

        new_host_public_key = refresh_credentials(vps247_account,
                                                  instance_ip_address,
                                                  new_root_password,
                                                  new_stats_password,
                                                  new_stats_username)

    except Exception as e:
        print type(e), str(e)
        if instance:
            # TODO: If instance exist, delete the instance.
            pass
        raise

    # Get all informations
    host_id = instance['name']
    provider_id = str(instance['id'])
    region_code = v247_api.get_region(str(
        instance['region_id']))['country_code']
    datacenter_name = 'VPS247 ' + region_code

    return (host_id, is_TCS, 'NATIVE' if is_TCS else None, None, provider_id,
            instance_ip_address, vps247_account.base_ssh_port, 'root',
            new_root_password, ' '.join(new_host_public_key.split(' ')[:2]),
            new_stats_username, new_stats_password, datacenter_name,
            region_code, None, None)