Exemple #1
0
def _get_system_volume(vm_):
    '''
    Construct VM system volume list from cloud profile config
    '''
    # Retrieve list of SSH public keys
    ssh_keys = get_public_keys(vm_)

    # Override system volume size if 'disk_size' is defined in cloud profile
    disk_size = get_size(vm_)['disk']
    if 'disk_size' in vm_:
        disk_size = vm_['disk_size']

    # Construct the system volume
    volume = Volume(
        name='{0} Storage'.format(vm_['name']),
        size=disk_size,
        image=get_image(vm_)['id'],
        disk_type=get_disk_type(vm_),
        ssh_keys=ssh_keys
    )

    # Set volume availability zone if defined in the cloud profile
    if 'disk_availability_zone' in vm_:
        volume.availability_zone = vm_['disk_availability_zone']

    return volume
Exemple #2
0
def _get_data_volumes(vm_):
    '''
    Construct a list of optional data volumes from the cloud profile
    '''
    ret = []
    volumes = vm_['volumes']
    for key, value in volumes.iteritems():
        # Verify the required 'disk_size' property is present in the cloud
        # profile config
        if 'disk_size' not in volumes[key].keys():
            raise SaltCloudConfigError(
                'The volume \'{0}\' is missing \'disk_size\''.format(key)
            )
        # Use 'HDD' if no 'disk_type' property is present in cloud profile
        if 'disk_type' not in volumes[key].keys():
            volumes[key]['disk_type'] = 'HDD'

        # Construct volume object and assign to a list.
        volume = Volume(
            name=key,
            size=volumes[key]['disk_size'],
            disk_type=volumes[key]['disk_type'],
            licence_type='OTHER'
        )

        # Set volume availability zone if defined in the cloud profile
        if 'disk_availability_zone' in volumes[key].keys():
            volume.availability_zone = volumes[key]['disk_availability_zone']

        ret.append(volume)

    return ret
    def setUpClass(cls):
        cls.resource = resource()
        cls.client = ProfitBricksService(username=configuration.USERNAME,
                                         password=configuration.PASSWORD,
                                         headers=configuration.HEADERS)

        # Create test datacenter
        cls.datacenter = cls.client.create_datacenter(datacenter=Datacenter(
            **cls.resource['datacenter']))
        cls.client.wait_for_completion(cls.datacenter)

        cls.image = find_image(cls.client, configuration.IMAGE_NAME)

        # Create test volume
        vol = Volume(**cls.resource['volume2'])
        vol.image = cls.image['id']

        cls.volume = cls.client.create_volume(
            datacenter_id=cls.datacenter['id'], volume=vol)
        cls.client.wait_for_completion(cls.volume)

        # Create snapshot1
        cls.snapshot1 = cls.client.create_snapshot(
            datacenter_id=cls.datacenter['id'],
            volume_id=cls.volume['id'],
            name=cls.resource['snapshot']['name'],
            description=cls.resource['snapshot']['description'])
        cls.client.wait_for_completion(cls.snapshot1, timeout=600)
Exemple #4
0
def _get_data_volumes(vm_):
    """
    Construct a list of optional data volumes from the cloud profile
    """
    ret = []
    volumes = vm_["volumes"]
    for key, value in volumes.items():
        # Verify the required 'disk_size' property is present in the cloud
        # profile config
        if "disk_size" not in volumes[key].keys():
            raise SaltCloudConfigError(
                "The volume '{}' is missing 'disk_size'".format(key))
        # Use 'HDD' if no 'disk_type' property is present in cloud profile
        if "disk_type" not in volumes[key].keys():
            volumes[key]["disk_type"] = "HDD"

        # Construct volume object and assign to a list.
        volume = Volume(
            name=key,
            size=volumes[key]["disk_size"],
            disk_type=volumes[key]["disk_type"],
            licence_type="OTHER",
        )

        # Set volume availability zone if defined in the cloud profile
        if "disk_availability_zone" in volumes[key].keys():
            volume.availability_zone = volumes[key]["disk_availability_zone"]

        ret.append(volume)

    return ret
def describe_datacenter(client):
    """ Creates a detailed description of the Datacenter according to the requirements"""
    # Define a public NIC
    public_nic = NIC(name=PUBLIC_NIC_NAME, dhcp=True, lan=1, nat=False)

    # Define a private NIC
    private_nic = NIC(name=PRIVATE_NIC_NAME, dhcp=True, lan=2)

    # get existing image with specific name
    images = find_images(client=client,
                         name=UBUNTU_IMAGE_NAME,
                         location=LOCATION)
    if images:
        ubuntu_image = images[0]
    else:
        raise Exception("There are no existing Ubuntu images")

    # Define a volume for backend
    backend_volume = Volume(name='Backend Volume',
                            size=20,
                            image=ubuntu_image['id'],
                            image_password='******',
                            ssh_keys=[get_public_key()],
                            availability_zone='ZONE_3')

    # Define volume for frontend
    frontend_volume = Volume(name='Data Volume 1',
                             size=20,
                             image=ubuntu_image['id'],
                             image_password='******',
                             ssh_keys=[get_public_key()],
                             availability_zone='ZONE_3')

    # Define a server with associated NICs and volumes
    backend_server = Server(name=BACKEND_NAME,
                            ram=DEFAULT_RAM_VALUE,
                            cores=DEFAULT_CORES_VALUES,
                            cpu_family='INTEL_XEON',
                            nics=[private_nic],
                            create_volumes=[backend_volume])

    # Define a server with associated NICs and volumes
    frontend_server = Server(name=FRONTEND_NAME,
                             ram=DEFAULT_RAM_VALUE,
                             cores=DEFAULT_CORES_VALUES,
                             cpu_family='INTEL_XEON',
                             nics=[public_nic, private_nic],
                             create_volumes=[frontend_volume])

    # Define a data center with the server
    datacenter = Datacenter(name=DATACENTER_NAME,
                            description="Yulia's test assignment datacenter",
                            location=LOCATION,
                            servers=[frontend_server, backend_server])

    return datacenter
Exemple #6
0
 def test_create_failure(self):
     try:
         volume = Volume(name=self.resource['volume2']['name'])
         self.client.create_volume(datacenter_id=self.datacenter['id'], volume=volume)
     except PBError as e:
         self.assertIn(self.resource['missing_attribute_error'] % 'size',
                       e.content[0]['message'])
Exemple #7
0
def _create_volume(module, profitbricks, datacenter, name):
    size = module.params.get('size')
    bus = module.params.get('bus')
    image = module.params.get('image')
    image_password = module.params.get('image_password')
    ssh_keys = module.params.get('ssh_keys')
    disk_type = module.params.get('disk_type')
    licence_type = module.params.get('licence_type')
    wait_timeout = module.params.get('wait_timeout')
    wait = module.params.get('wait')

    try:
        v = Volume(name=name,
                   size=size,
                   bus=bus,
                   image=image,
                   image_password=image_password,
                   ssh_keys=ssh_keys,
                   disk_type=disk_type,
                   licence_type=licence_type)

        volume_response = profitbricks.create_volume(datacenter, v)

        if wait:
            _wait_for_completion(profitbricks, volume_response, wait_timeout,
                                 "_create_volume")

    except Exception as e:
        module.fail_json(msg="failed to create the volume: %s" % str(e))

    return volume_response
Exemple #8
0
def _get_system_volume(vm_):
    """
    Construct VM system volume list from cloud profile config
    """

    # Override system volume size if 'disk_size' is defined in cloud profile
    disk_size = get_size(vm_)["disk"]
    if "disk_size" in vm_:
        disk_size = vm_["disk_size"]

    # Construct the system volume
    volume = Volume(
        name="{} Storage".format(vm_["name"]),
        size=disk_size,
        disk_type=get_disk_type(vm_),
    )

    if "image_password" in vm_:
        image_password = vm_["image_password"]
        volume.image_password = image_password

    # Retrieve list of SSH public keys
    ssh_keys = get_public_keys(vm_)
    volume.ssh_keys = ssh_keys

    if "image_alias" in vm_.keys():
        volume.image_alias = vm_["image_alias"]
    else:
        volume.image = get_image(vm_)["id"]
        # Set volume availability zone if defined in the cloud profile
        if "disk_availability_zone" in vm_:
            volume.availability_zone = vm_["disk_availability_zone"]

    return volume
def _create_volume(module, profitbricks, datacenter, name):
    size = module.params.get('size')
    bus = module.params.get('bus')
    image = module.params.get('image')
    image_password = module.params.get('image_password')
    ssh_keys = module.params.get('ssh_keys')
    disk_type = module.params.get('disk_type')
    availability_zone = module.params.get('availability_zone')
    licence_type = module.params.get('licence_type')
    wait_timeout = module.params.get('wait_timeout')
    wait = module.params.get('wait')

    if module.check_mode:
        module.exit_json(changed=True)

    try:
        v = Volume(
            name=name,
            size=size,
            bus=bus,
            image_password=image_password,
            ssh_keys=ssh_keys,
            disk_type=disk_type,
            licence_type=licence_type,
            availability_zone=availability_zone
        )

        try:
            UUID(image)
        except Exception:
            v.image_alias = image
        else:
            v.image = image

        volume_response = profitbricks.create_volume(datacenter, v)

        if wait:
            _wait_for_completion(profitbricks, volume_response,
                                 wait_timeout, "_create_volume")

    except Exception as e:
        module.fail_json(msg="failed to create the volume: %s" % to_native(e))

    return volume_response
    def setUpClass(self):
        self.resource = resource()
        self.client = ProfitBricksService(username=configuration.USERNAME,
                                          password=configuration.PASSWORD,
                                          headers=configuration.HEADERS)

        # Create test datacenter.
        self.datacenter = self.client.create_datacenter(datacenter=Datacenter(
            **self.resource['datacenter']))
        self.client.wait_for_completion(self.datacenter)

        # Create test volume
        volume = Volume(**self.resource['volume'])
        self.volume = self.client.create_volume(
            datacenter_id=self.datacenter['id'], volume=volume)

        self.client.wait_for_completion(self.volume)

        # Create test volume1
        volume1 = Volume(**self.resource['volume'])
        self.volume1 = self.client.create_volume(
            datacenter_id=self.datacenter['id'], volume=volume1)

        self.client.wait_for_completion(self.volume1)

        # Create test snapshot
        snapshot = Snapshot(**self.resource['snapshot'])
        self.snapshot1 = self.client.create_snapshot(
            datacenter_id=self.datacenter['id'],
            volume_id=self.volume['id'],
            name=snapshot.name,
            description=snapshot.description)

        self.client.wait_for_completion(self.snapshot1)

        # Create test snapshot2
        self.snapshot2 = self.client.create_snapshot(
            datacenter_id=self.datacenter['id'],
            volume_id=self.volume['id'],
            name="python sdk test snapshot",
            description="snapshot test description")

        self.client.wait_for_completion(self.snapshot2)
    def test_delete_volume(self):
        volume = self.client.create_volume(
            datacenter_id=self.datacenter['id'],
            volume=Volume(**self.resource['volume']))
        self.client.wait_for_completion(volume)

        volume = self.client.delete_volume(datacenter_id=self.datacenter['id'],
                                           volume_id=volume['id'])

        self.assertTrue(volume)
        assertRegex(self, volume['requestId'], self.resource['uuid_match'])
    def test_create_composite(self):
        fwrule = FirewallRule(**self.resource['fwrule'])
        nic = NIC(firewall_rules=[fwrule], **self.resource['nic'])
        volume = Volume(image=self.image['id'],
                        image_password='******',
                        ssh_keys=['ssh-rsa AAAAB3NzaC1'],
                        **self.resource['volume'])
        volume.availability_zone = 'ZONE_3'

        server = Server(nics=[nic],
                        create_volumes=[volume],
                        **self.resource['server'])

        composite_server = self.client.create_server(
            datacenter_id=self.datacenter['id'], server=server)
        self.client.wait_for_completion(composite_server, timeout=600)

        composite_server = self.client.get_server(
            datacenter_id=self.datacenter['id'],
            server_id=composite_server['id'])

        assertRegex(self, composite_server['id'], self.resource['uuid_match'])
        self.assertEqual(composite_server['properties']['name'],
                         self.resource['server']['name'])
        self.assertEqual(composite_server['properties']['cores'],
                         self.resource['server']['cores'])
        self.assertEqual(composite_server['properties']['ram'],
                         self.resource['server']['ram'])
        self.assertEqual(composite_server['properties']['availabilityZone'],
                         'ZONE_1')
        self.assertIn(composite_server['properties']['vmState'],
                      self.resource['vm_states'])
        self.assertGreater(
            len(composite_server['entities']['volumes']['items']), 0)
        self.assertGreater(len(composite_server['entities']['nics']['items']),
                           0)
    def test_create_composite(self):
        datacenter_resource = Datacenter(
            **self.resource['datacenter_composite'])
        datacenter_resource.servers = [Server(**self.resource['server'])]
        datacenter_resource.volumes = [Volume(**self.resource['volume'])]

        datacenter = self.client.create_datacenter(
            datacenter=datacenter_resource)
        self.client.wait_for_completion(datacenter)

        self.assertEqual(datacenter['type'], 'datacenter')
        self.assertEqual(datacenter['properties']['name'],
                         self.resource['datacenter_composite']['name'])
        self.assertEqual(datacenter['properties']['description'],
                         self.resource['datacenter_composite']['description'])
        self.assertEqual(datacenter['properties']['location'],
                         self.resource['datacenter_composite']['location'])
        self.assertGreater(len(datacenter['entities']['servers']), 0)
        self.assertGreater(len(datacenter['entities']['volumes']), 0)

        response = self.client.delete_datacenter(
            datacenter_id=datacenter['id'])
        self.assertTrue(response)
def getVolumeObject(defdict=None):
    if not defdict or not isinstance(defdict, dict):
        raise ValueError("argument 'defdict' must be non-empty dict")
    # TODO: can we set deviceNumber too? Nope, not supported in Volume()
    # AARGH! some of Volume's fields have different names -> need to convert
    # so make a copy and let source as is
    props = dict()
    for k, v in defdict['properties'].items():
        if k == 'type':
            props['disk_type'] = v
            continue
        if k == 'imagePassword':
            props['image_password'] = v
            continue
        if k == 'licenceType':
            props['licence_type'] = v
            continue
        if k == 'sshKeys':
            props['ssh_keys'] = v
            continue
        props[k] = v
    # end for(defdict)
    apiobj = Volume(**props)
    return apiobj
def _create_machine(module, profitbricks, datacenter, name):
    cores = module.params.get('cores')
    ram = module.params.get('ram')
    cpu_family = module.params.get('cpu_family')
    volume_size = module.params.get('volume_size')
    disk_type = module.params.get('disk_type')
    availability_zone = module.params.get('availability_zone')
    volume_availability_zone = module.params.get('volume_availability_zone')
    image_password = module.params.get('image_password')
    ssh_keys = module.params.get('ssh_keys')
    bus = module.params.get('bus')
    lan = module.params.get('lan')
    nat = module.params.get('nat')
    assign_public_ip = module.params.get('assign_public_ip')
    subscription_user = module.params.get('subscription_user')
    subscription_password = module.params.get('subscription_password')
    location = module.params.get('location')
    image = module.params.get('image')
    assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    if assign_public_ip:
        public_found = False

        lans = profitbricks.list_lans(datacenter)
        for lan in lans['items']:
            if lan['properties']['public']:
                public_found = True
                lan = lan['id']

        if not public_found:
            i = LAN(name='public', public=True)

            lan_response = profitbricks.create_lan(datacenter, i)
            _wait_for_completion(profitbricks, lan_response, wait_timeout,
                                 "_create_machine")
            lan = lan_response['id']

    v = Volume(name=str(uuid.uuid4()).replace('-', '')[:10],
               size=volume_size,
               image=image,
               image_password=image_password,
               ssh_keys=ssh_keys,
               disk_type=disk_type,
               availability_zone=volume_availability_zone,
               bus=bus)

    n = NIC(name=str(uuid.uuid4()).replace('-', '')[:10],
            nat=nat,
            lan=int(lan))

    s = Server(
        name=name,
        ram=ram,
        cores=cores,
        cpu_family=cpu_family,
        availability_zone=availability_zone,
        create_volumes=[v],
        nics=[n],
    )

    try:
        create_server_response = profitbricks.create_server(
            datacenter_id=datacenter, server=s)

        _wait_for_completion(profitbricks, create_server_response,
                             wait_timeout, "create_virtual_machine")

        server_response = profitbricks.get_server(
            datacenter_id=datacenter,
            server_id=create_server_response['id'],
            depth=3)
    except Exception as e:
        module.fail_json(msg="failed to create the new server: %s" % str(e))
    else:
        server_response['nic'] = server_response['entities']['nics']['items'][
            0]
        return server_response
    def setUpClass(cls):
        cls.resource = resource()
        cls.client = ProfitBricksService(username=configuration.USERNAME,
                                         password=configuration.PASSWORD,
                                         headers=configuration.HEADERS)

        # Create test datacenter.
        cls.datacenter = cls.client.create_datacenter(datacenter=Datacenter(
            **cls.resource['datacenter']))
        cls.client.wait_for_completion(cls.datacenter)

        # Create test volume1.
        cls.volume1 = cls.client.create_volume(
            datacenter_id=cls.datacenter['id'],
            volume=Volume(**cls.resource['volume']))
        cls.client.wait_for_completion(cls.volume1)

        # Create test volume2 (attach volume test).
        cls.volume2 = cls.client.create_volume(
            datacenter_id=cls.datacenter['id'],
            volume=Volume(**cls.resource['volume']))
        cls.client.wait_for_completion(cls.volume2)

        # Create test server.
        server = Server(**cls.resource['server'])
        server.attach_volumes = [cls.volume1['id']]
        cls.server = cls.client.create_server(
            datacenter_id=cls.datacenter['id'], server=server)
        cls.client.wait_for_completion(cls.server)

        # Create test NIC.
        cls.nic = cls.client.create_nic(datacenter_id=cls.datacenter['id'],
                                        server_id=cls.server['id'],
                                        nic=NIC(**cls.resource['nic']))
        cls.client.wait_for_completion(cls.nic)

        # Find an Ubuntu image for testing.
        for item in cls.client.list_images()['items']:
            if (configuration.IMAGE_NAME in item['properties']['name'] and
                    item['properties']['location'] == configuration.LOCATION):
                cls.image = item
        # Find a cdrom image
        images = cls.client.list_images(depth=5)
        usedIndex = 0
        for index, image in enumerate(images['items']):
            if (image['metadata']['state'] == "AVAILABLE"
                    and image['properties']['public'] is True
                    and image['properties']['imageType'] == "CDROM" and
                    image['properties']['location'] == configuration.LOCATION
                    and image['properties']['licenceType'] == "LINUX"):
                if usedIndex == 0:
                    cls.test_image1 = image
                    usedIndex = index
                else:
                    cls.test_image2 = image
                    break
        # Create test cdrom
        cls.cdrom = cls.client.attach_cdrom(datacenter_id=cls.datacenter['id'],
                                            server_id=cls.server['id'],
                                            cdrom_id=cls.test_image1['id'])
        cls.client.wait_for_completion(cls.cdrom)
# See the License for the specific language governing permissions and
# limitations under the License.

"""Create volume
"""

from profitbricks.client import ProfitBricksService, Volume

datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'

client = ProfitBricksService(
    username='******', password='******')

i = Volume(
    name='Explicitly created volume',
    size=56,
    image='<IMAGE/SNAPSHOT-ID>',
    bus='VIRTIO')

response = client.create_volume(
    datacenter_id=datacenter_id, volume=i)

"""Create snapshot
"""

from profitbricks.client import ProfitBricksService  # noqa

datacenter_id = '700e1cab-99b2-4c30-ba8c-1d273ddba022'
volume_id = '700e1cab-99b2-4c30-ba8c-1d273ddba025'

client = ProfitBricksService(
Exemple #18
0
    name='nic1',
    ips=['10.2.2.5'],
    dhcp='true',
    lan=1,
    firewall_active=True,
)

nic2 = NIC(
    name='nic2',
    ips=['10.2.3.6'],
    dhcp='true',
    lan=1,
    firewall_active=True,
)

volume1 = Volume(name='volume6', size=56, image=image_id, bus='VIRTIO')

volume2 = Volume(name='volume7', size=56, image=image_id, bus='VIRTIO')

nics = [nic1, nic2]
create_volumes = [volume1, volume2]

i = Server(name='server11',
           ram=4096,
           cores=4,
           nics=nics,
           create_volumes=create_volumes)

response = client.create_server(datacenter_id=datacenter_id, server=i)
"""Create Server with Existing Volume
"""
Exemple #19
0
def _create_machine(module, profitbricks, datacenter, name):
    image = module.params.get('image')
    cores = module.params.get('cores')
    ram = module.params.get('ram')
    volume_size = module.params.get('volume_size')
    bus = module.params.get('bus')
    lan = module.params.get('lan')
    assign_public_ip = module.params.get('assign_public_ip')
    subscription_user = module.params.get('subscription_user')
    subscription_password = module.params.get('subscription_password')
    location = module.params.get('location')
    image = module.params.get('image')
    assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    try:
        # Generate name, but grab first 10 chars so we don't
        # screw up the uuid match routine.
        v = Volume(
            name=str(uuid.uuid4()).replace('-','')[:10],
            size=volume_size,
            image=image,
            bus=bus)

        volume_response = profitbricks.create_volume(
            datacenter_id=datacenter, volume=v)

        # We're forced to wait on the volume creation since
        # server create relies upon this existing.

        _wait_for_completion(profitbricks, volume_response,
                             wait_timeout, "create_volume")
    except Exception as e:
        module.fail_json(msg="failed to create the new volume: %s" % str(e))

    if assign_public_ip:
        public_found = False

        lans = profitbricks.list_lans(datacenter)
        for lan in lans['items']:
            if lan['properties']['public']:
                public_found = True
                lan = lan['id']

        if not public_found:
            i = LAN(
                name='public',
                public=True)

            lan_response = profitbricks.create_lan(datacenter, i)

            lan = lan_response['id']

            _wait_for_completion(profitbricks, lan_response,
                                 wait_timeout, "_create_machine")

    try:
        n = NIC(
            lan=int(lan)
            )

        nics = [n]

        s = Server(
            name=name,
            ram=ram,
            cores=cores,
            nics=nics,
            boot_volume_id=volume_response['id']
            )

        server_response = profitbricks.create_server(
            datacenter_id=datacenter, server=s)

        if wait:
            _wait_for_completion(profitbricks, server_response,
                                 wait_timeout, "create_virtual_machine")


        return (server_response)
    except Exception as e:
        module.fail_json(msg="failed to create the new server: %s" % str(e))
Exemple #20
0
def create(vm_):
    '''
    Create a single VM from a data dict
    '''
    try:
        # Check for required profile parameters before sending any API calls.
        if vm_['profile'] and config.is_profile_configured(
                __opts__, __active_provider_name__ or 'profitbricks',
                vm_['profile']) is False:
            return False
    except AttributeError:
        pass

    datacenter_id = get_datacenter_id()
    conn = get_conn()
    data = None

    # Apply component overrides to the size from the cloud profile config.
    vm_size = override_size(vm_)

    # Retrieve list of SSH public keys
    ssh_keys = get_public_keys(vm_)

    # Fetch image and construct volume
    image = get_image(vm_)
    volume = Volume(name='{0} Storage'.format(vm_['name']),
                    size=vm_size['disk'],
                    image=image['id'],
                    disk_type=get_disk_type(vm_),
                    ssh_keys=ssh_keys)

    # Construct server
    server = Server(name=vm_['name'],
                    ram=vm_size['ram'],
                    cores=vm_size['cores'],
                    create_volumes=[volume])

    salt.utils.cloud.fire_event('event',
                                'requesting instance',
                                'salt/cloud/{0}/requesting'.format(
                                    vm_['name']),
                                args={'name': vm_['name']},
                                sock_dir=__opts__['sock_dir'],
                                transport=__opts__['transport'])

    try:
        data = conn.create_server(datacenter_id=datacenter_id, server=server)
        log.info('Create server request ID: {0}'.format(data['requestId']),
                 exc_info_on_loglevel=logging.DEBUG)

        _wait_for_completion(conn, data, get_wait_timeout(vm_),
                             "create_server")
    except Exception as exc:  # pylint: disable=W0703
        log.error(
            'Error creating {0} on ProfitBricks\n\n'
            'The following exception was thrown by the profitbricks library '
            'when trying to run the initial deployment: \n{1}'.format(
                vm_['name'], exc),
            exc_info_on_loglevel=logging.DEBUG)
        return False

    create_network_interfaces(conn, datacenter_id, data['id'], vm_)
    set_public_lan(conn, vm_)

    def __query_node_data(vm_, data):
        '''
        Query node data until node becomes available.
        '''
        running = False
        try:
            data = show_instance(vm_['name'], 'action')
            if not data:
                return False
            log.debug(
                'Loaded node data for {0}:\nname: {1}\nstate: {2}'.format(
                    vm_['name'], pprint.pformat(data['name']),
                    data['vmState']))
        except Exception as err:
            log.error(
                'Failed to get nodes list: {0}'.format(err),
                # Show the trackback if the debug logging level is enabled
                exc_info_on_loglevel=logging.DEBUG)
            # Trigger a failure in the wait for IP function
            return False

        running = data['vmState'] == 'RUNNING'
        if not running:
            # Still not running, trigger another iteration
            return

        if ssh_interface(vm_) == 'private_lan' and data['private_ips']:
            vm_['ssh_host'] = data['private_ips'][0]

        if ssh_interface(vm_) != 'private_lan' and data['public_ips']:
            vm_['ssh_host'] = data['public_ips'][0]

        return data

    try:
        data = salt.utils.cloud.wait_for_ip(
            __query_node_data,
            update_args=(vm_, data),
            timeout=config.get_cloud_config_value('wait_for_ip_timeout',
                                                  vm_,
                                                  __opts__,
                                                  default=10 * 60),
            interval=config.get_cloud_config_value('wait_for_ip_interval',
                                                   vm_,
                                                   __opts__,
                                                   default=10),
        )
    except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:
        try:
            # It might be already up, let's destroy it!
            destroy(vm_['name'])
        except SaltCloudSystemExit:
            pass
        finally:
            raise SaltCloudSystemExit(str(exc.message))

    log.debug('VM is now running')
    log.info('Created Cloud VM \'{0[name]}\''.format(vm_))
    log.debug('\'{0[name]}\' VM creation details:\n{1}'.format(
        vm_, pprint.pformat(data)))

    salt.utils.cloud.fire_event('event',
                                'created instance',
                                'salt/cloud/{0}/created'.format(vm_['name']),
                                args={
                                    'name': vm_['name'],
                                    'profile': vm_['profile'],
                                    'provider': vm_['driver'],
                                },
                                sock_dir=__opts__['sock_dir'],
                                transport=__opts__['transport'])

    if 'ssh_host' in vm_:
        vm_['key_filename'] = get_key_filename(vm_)
        ret = salt.utils.cloud.bootstrap(vm_, __opts__)
        ret.update(data)
        return ret
    else:
        raise SaltCloudSystemExit('A valid IP address was not found.')
Exemple #21
0
def main(argv=None):
    '''Command line options.'''

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = '''%s

  Created by Jürgen Buchhammer on %s.
  Copyright 2016 ProfitBricks GmbH. All rights reserved.

  Licensed under the Apache License 2.0
  http://www.apache.org/licenses/LICENSE-2.0

  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE
''' % (program_shortdesc, str(__date__))

    try:
        # Setup argument parser
        parser = ArgumentParser(description=program_license,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument('-u', '--user', dest='user', help='the login name')
        parser.add_argument('-p',
                            '--password',
                            dest='password',
                            help='the login password')
        parser.add_argument('-L',
                            '--Login',
                            dest='loginfile',
                            default=None,
                            help='the login file to use')
        parser.add_argument('-t',
                            '--type',
                            dest='metatype',
                            default="OVF",
                            help='type of VM meta data')
        parser.add_argument('-m',
                            '--metadata',
                            dest='metafile',
                            required=True,
                            default=None,
                            help='meta data file')
        parser.add_argument('-d',
                            '--datacenterid',
                            dest='datacenterid',
                            default=None,
                            help='datacenter of the new server')
        parser.add_argument('-D',
                            '--DCname',
                            dest='dcname',
                            default=None,
                            help='new datacenter name')
        parser.add_argument('-l',
                            '--location',
                            dest='location',
                            default=None,
                            help='location for new datacenter')
        parser.add_argument('-v',
                            '--verbose',
                            dest="verbose",
                            action="count",
                            default=0,
                            help="set verbosity level [default: %(default)s]")
        parser.add_argument('-V',
                            '--version',
                            action='version',
                            version=program_version_message)

        # Process arguments
        args = parser.parse_args()
        global verbose
        verbose = args.verbose

        if verbose > 0:
            print("Verbose mode on")
            print("start {} with args {}".format(program_name, str(args)))

        (user, password) = getLogin(args.loginfile, args.user, args.password)
        if user is None or password is None:
            raise ValueError("user or password resolved to None")
        pbclient = ProfitBricksService(user, password)

        if args.metatype == 'OVF':
            metadata = OFVData(args.metafile)
            metadata.parse()
        else:
            sys.stderr.write("Metadata type '{}' is not supported".format(
                args.metatype))
            return 1

        # we need the DC first to have the location defined
        dc_id = None
        if args.datacenterid is None:
            if args.dcname is None or args.location is None:
                sys.stderr.write(
                    "Either '-d <id>' or '-D <name> -l <loc>'  must be specified"
                )
                return 1
            # else: we will create the DC later after parsing the meta data
        else:
            dc_id = args.datacenterid

        if dc_id is None:
            location = args.location
            dc = Datacenter(name=args.dcname,
                            location=location,
                            description="created by pb_importVM")
            print("create new DC {}".format(str(dc)))
            response = pbclient.create_datacenter(dc)
            dc_id = response['id']
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(result))
        else:
            dc = pbclient.get_datacenter(dc_id)
            location = dc['properties']['location']
            print("use existing DC {} in location {}".format(
                dc['properties']['name'], location))

        # check if images exist
        for disk in metadata.disks:
            disk_name = disk['file']
            images = get_disk_image_by_name(pbclient, location, disk_name)
            if len(images) == 0:
                raise ValueError(
                    "No HDD image with name '{}' found in location {}".format(
                        disk_name, location))
            if len(images) > 1:
                raise ValueError(
                    "Ambigous image name '{}' in location {}".format(
                        disk_name, location))
            disk['image'] = images[0]['id']

        # now we're ready to create the VM
        # Server
        server = Server(name=metadata.name,
                        cores=metadata.cpus,
                        ram=metadata.ram)
        print("create server {}".format(str(Server)))
        response = pbclient.create_server(dc_id, server)
        srv_id = response['id']
        result = wait_for_request(pbclient, response['requestId'])
        print("wait loop returned {}".format(str(result)))
        # NICs (note that createing LANs may be implicit)
        for nic in metadata.nics:
            dcnic = NIC(name=nic['nic'], lan=nic['lanid'])
            print("create NIC {}".format(str(dcnic)))
            response = pbclient.create_nic(dc_id, srv_id, dcnic)
            nic_id = response['id']
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(str(result)))
            response = pbclient.get_nic(dc_id, srv_id, nic_id, 2)
            mac = response['properties']['mac']
            print("dcnic has MAC {} for {}".format(mac, nic_id))
        # end for(nics)
        # Volumes (we use the image name as volume name too
        requests = []
        for disk in metadata.disks:
            dcvol = Volume(name=disk['file'],
                           size=disk['capacity'],
                           image=disk['image'],
                           licence_type=metadata.licenseType)
            print("create Volume {}".format(str(dcvol)))
            response = pbclient.create_volume(dc_id, dcvol)
            requests.append(response['requestId'])
            disk['volume_id'] = response['id']
        # end for(disks)
        if len(requests) != 0:
            result = wait_for_requests(pbclient,
                                       requests,
                                       initial_wait=10,
                                       scaleup=15)
            print("wait loop returned {}".format(str(result)))
        for disk in metadata.disks:
            print("attach volume {}".format(disk))
            response = pbclient.attach_volume(dc_id, srv_id, disk['volume_id'])
            result = wait_for_request(pbclient, response['requestId'])
            print("wait loop returned {}".format(str(result)))
        # end for(disks)

        print("import of VM succesfully finished")
        return 0

    except KeyboardInterrupt:
        # handle keyboard interrupt
        return 0
    except Exception:
        traceback.print_exc()
        sys.stderr.write("\n" + program_name + ":  for help use --help\n")
        return 2
Exemple #22
0
def main(argv=None):
    '''Parse command line options and create a server/volume composite.'''

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = '''%s

  Created by J. Buchhammer on %s.
  Copyright 2016 ProfitBricks GmbH. All rights reserved.

  Licensed under the Apache License 2.0
  http://www.apache.org/licenses/LICENSE-2.0

  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE
''' % (program_shortdesc, str(__date__))

    try:
        # Setup argument parser
        parser = ArgumentParser(description=program_license,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument('-u', '--user', dest='user', help='the login name')
        parser.add_argument('-p', '--password', dest='password',
                            help='the login password')
        parser.add_argument('-L', '--Login', dest='loginfile', default=None,
                            help='the login file to use')
        parser.add_argument('-d', '--datacenterid', dest='datacenterid',
                            required=True, default=None,
                            help='datacenter of the new server')
        parser.add_argument('-l', '--lanid', dest='lanid', required=True,
                            default=None, help='LAN of the new server')
        parser.add_argument('-n', '--name', dest='servername',
                            default="SRV_"+datetime.now().isoformat(),
                            help='name of the new server')
        parser.add_argument('-c', '--cores', dest='cores', type=int,
                            default=2, help='CPU cores')
        parser.add_argument('-r', '--ram', dest='ram', type=int, default=4,
                            help='RAM in GB')
        parser.add_argument('-s', '--storage', dest='storage', type=int,
                            default=4, help='storage in GB')
        parser.add_argument('-b', '--boot', dest='bootdevice', default="HDD",
                            help='boot device')
        parser.add_argument('-i', '--imageid', dest='imageid', default=None,
                            help='installation image')
        parser.add_argument('-P', '--imagepassword', dest='imgpassword',
                            default=None, help='the image password')
        parser.add_argument('-v', '--verbose', dest="verbose", action="count",
                            help="set verbosity level [default: %(default)s]")
        parser.add_argument('-V', '--version', action='version',
                            version=program_version_message)

        # Process arguments
        args = parser.parse_args()
        global verbose
        verbose = args.verbose
        dc_id = args.datacenterid
        lan_id = args.lanid
        servername = args.servername

        if verbose > 0:
            print("Verbose mode on")
            print("start {} with args {}".format(program_name, str(args)))

        # Test images (location de/fra)
        # CDROM: 7fc885b3-c9a6-11e5-aa10-52540005ab80   # debian-8.3.0-amd64-netinst.iso
        # HDD:   28007a6d-c88a-11e5-aa10-52540005ab80   # CentOS-7-server-2016-02-01
        hdimage = args.imageid
        cdimage = None
        if args.bootdevice == "CDROM":
            hdimage = None
            cdimage = args.imageid
        print("using boot device {} with image {}"
              .format(args.bootdevice, args.imageid))

        (user, password) = getLogin(args.loginfile, args.user, args.password)
        if user is None or password is None:
            raise ValueError("user or password resolved to None")
        pbclient = ProfitBricksService(user, password)

        first_nic = NIC(name="local", ips=[], dhcp=True, lan=lan_id)
        volume = Volume(name=servername+"-Disk", size=args.storage,
                        image=hdimage, image_password=args.imgpassword)
        server = Server(name=servername, cores=args.cores, ram=args.ram*1024,
                        create_volumes=[volume], nics=[first_nic],
                        boot_cdrom=cdimage)
        print("creating server..")
        if verbose > 0:
            print("SERVER: {}".format(str(server)))
        response = pbclient.create_server(dc_id, server)
        print("wait for provisioning..")
        wait_for_request(pbclient, response["requestId"])
        server_id = response['id']
        print("Server provisioned with ID {}".format(server_id))
        nics = pbclient.list_nics(dc_id, server_id, 1)
        # server should have exactly one nic, but we only test empty nic list
        if len(nics['items']) == 0:
            raise CLIError("No NICs found for newly created server {}"
                           .format(server_id))
        nic0 = nics['items'][0]
        if verbose > 0:
            print("NIC0: {}".format(str(nic0)))
        (nic_id, nic_mac) = (nic0['id'], nic0['properties']['mac'])
        print("NIC of new Server has ID {} and MAC {}".format(nic_id, nic_mac))
        print("{} finished w/o errors".format(program_name))
        return 0

    except KeyboardInterrupt:
        # handle keyboard interrupt #
        return 0
    except Exception:
        traceback.print_exc()
        sys.stderr.write("\n" + program_name + ":  for help use --help\n")
        return 2
Exemple #23
0
    def setUpClass(self):
        self.resource = resource()
        self.client = ProfitBricksService(username=configuration.USERNAME,
                                          password=configuration.PASSWORD,
                                          headers=configuration.HEADERS)

        # Create datacenter resource
        self.datacenter = self.client.create_datacenter(datacenter=Datacenter(
            **self.resource['datacenter']))
        self.client.wait_for_completion(self.datacenter)

        # Create volume resource
        volume = Volume(**self.resource['volume'])
        self.volume = self.client.create_volume(
            datacenter_id=self.datacenter['id'], volume=volume)

        self.client.wait_for_completion(self.volume)

        self.image = find_image(self.client, configuration.IMAGE_NAME)

        # Create snapshot resource
        self.snapshot = self.client.create_snapshot(
            datacenter_id=self.datacenter['id'],
            volume_id=self.volume['id'],
            name=self.resource['snapshot']['name'])

        self.client.wait_for_completion(self.snapshot)

        # Reserve IP block resource
        self.ipblock = self.client.reserve_ipblock(
            IPBlock(**self.resource['ipblock']))

        # Create User 1
        self.user_dict1 = User(
            firstname='John',
            lastname='Doe',
            email='*****@*****.**' % randint(0, 9999999999999),
            password='******' % randint(0, 99999999),
            administrator=True,
            force_sec_auth=False)
        self.user1 = self.client.create_user(user=self.user_dict1)

        # Create User 2
        self.user_dict2 = User(
            firstname='John',
            lastname='Doe',
            email='*****@*****.**' % randint(0, 9999999999999),
            password='******' % randint(0, 99999999))
        self.user2 = self.client.create_user(user=self.user_dict2)

        # Create User 3
        self.user_dict3 = User(
            firstname='John',
            lastname='Doe',
            email='*****@*****.**' % randint(0, 9999999999999),
            password='******' % randint(0, 99999999))
        self.user3 = self.client.create_user(user=self.user_dict3)

        # Create Group 1
        group = Group(**self.resource['group'])
        self.group1 = self.client.create_group(group)

        # Create Group 2
        group.name = self.resource['group']['name'] + ' 2'
        self.group2 = self.client.create_group(group)

        # Create Group 3
        group.name = self.resource['group']['name'] + ' 3'
        self.group3 = self.client.create_group(group)

        # Create Share 1
        self.share1 = self.client.add_share(group_id=self.group3['id'],
                                            resource_id=self.datacenter['id'],
                                            edit_privilege=True,
                                            share_privilege=True)
           lan=1,
           firewall_active=True,
           firewall_rules=fw_rules)

nic2 = NIC(name='nic2',
           ips=['10.2.3.4'],
           dhcp='true',
           lan=1,
           firewall_active=True,
           firewall_rules=fw_rules)

nics = [nic1, nic2]

volume1 = Volume(name='volume1',
                 size=56,
                 image=image_id,
                 bus='VIRTIO',
                 image_password="******")

volume2 = Volume(name='volume2',
                 size=56,
                 image=image_id,
                 bus='VIRTIO',
                 image_password="******")

volumes = [volume2]

server1 = Server(name='My New Server1',
                 ram=4096,
                 cores=4,
                 nics=nics,
def _create_machine(module, profitbricks, datacenter, name):
    cores = module.params.get('cores')
    ram = module.params.get('ram')
    cpu_family = module.params.get('cpu_family')
    volume_size = module.params.get('volume_size')
    disk_type = module.params.get('disk_type')
    availability_zone = module.params.get('availability_zone')
    volume_availability_zone = module.params.get('volume_availability_zone')
    image_password = module.params.get('image_password')
    ssh_keys = module.params.get('ssh_keys')
    bus = module.params.get('bus')
    lan = module.params.get('lan')
    nat = module.params.get('nat')
    image = module.params.get('image')
    assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
    wait = module.params.get('wait')
    wait_timeout = module.params.get('wait_timeout')

    nics = []

    if assign_public_ip:
        public_lan = _get_lan_by_id_or_properties(
            profitbricks.list_lans(datacenter)['items'], public=True)

        public_ip_lan_id = public_lan['id'] if public_lan is not None else None

        if public_ip_lan_id is None:
            i = LAN(name='public', public=True)

            lan_response = profitbricks.create_lan(datacenter, i)
            _wait_for_completion(profitbricks, lan_response, wait_timeout,
                                 "_create_machine")

            public_ip_lan_id = lan_response['id']

        nics.append(
            NIC(name=str(uuid4()).replace('-', '')[:10],
                nat=nat,
                lan=int(public_ip_lan_id)))

    if lan is not None:
        matching_lan = _get_lan_by_id_or_properties(
            profitbricks.list_lans(datacenter)['items'], lan, name=lan)

        nics.append(
            NIC(name=str(uuid4()).replace('-', '')[:10],
                nat=nat,
                lan=int(matching_lan['id'])))

    v = Volume(name=str(uuid4()).replace('-', '')[:10],
               size=volume_size,
               image_password=image_password,
               ssh_keys=ssh_keys,
               disk_type=disk_type,
               availability_zone=volume_availability_zone,
               bus=bus)

    try:
        UUID(image)
    except Exception:
        v.image_alias = image
    else:
        v.image = image

    s = Server(
        name=name,
        ram=ram,
        cores=cores,
        cpu_family=cpu_family,
        availability_zone=availability_zone,
        create_volumes=[v],
        nics=nics,
    )

    try:
        create_server_response = profitbricks.create_server(
            datacenter_id=datacenter, server=s)

        _wait_for_completion(profitbricks, create_server_response,
                             wait_timeout, "create_virtual_machine")

        server_response = profitbricks.get_server(
            datacenter_id=datacenter,
            server_id=create_server_response['id'],
            depth=3)
    except Exception as e:
        module.fail_json(msg="failed to create the new server: %s" %
                         to_native(e))
    else:
        server_response['nic'] = server_response['entities']['nics']['items'][
            0]
        return server_response