# Examples for working with HP Cloud Compute Service v13.5
The latest HP Cloud deployment, version 13.5, takes advantage of more OpenStack functionality and the compute service uses slightly different commands (often noted by *v2* in the commands) than the previous 12.12 version. Verify which version of HP cloud you are working with.
The HP Cloud services provides compute support using two abstractions: [a model layer](#ModelLayer) and [a request layer](#RequestLayer). Both layers are detailed below. The code samples on this page can be executed from within a Ruby console (IRB):
To connect to the HP Cloud Compute V2 Service, follow these steps:
1. Enter IRB
irb
2. Require the Fog library
require 'fog'
3. Establish a connection to the HP Cloud Compute V2 service
conn = Fog::Compute.new(
:provider => "HP",
:version => :v2,
:hp_access_key => "<your_ACCESS_KEY>",
:hp_secret_key => "<your_SECRET_KEY>",
:hp_auth_uri => "<IDENTITY_ENDPOINT_URL>",
:hp_tenant_id => "<your_TENANT_ID>",
:hp_avl_zone => "<your_AVAILABILITY_ZONE>",
<otheroptionalparameters>
)
**Note**: You must use the `:hp_access_key` parameter rather than the now-deprecated `:hp_account_id` parameter you might have used in previous Ruby Fog versions.
You can find the values the access key, secret key, and other values by clicking the [`API Keys`](https://console.hpcloud.com/account/api_keys) button in the [Console Dashboard](https://console.hpcloud.com/dashboard).
server.flavor_id # returns id of the flavor used to create the server
server.image_id # returns id of the image used to create the server
server.addresses # returns a hash of public and private IP addresses
server.created_at # returns the date the server was created
server.state # returns the state of the server e.g. ACTIVE, BUILD
4. Create a new server:
new_server = conn.servers.create(
:name => "My Shiny Server",
:flavor_id => 101,
:image_id => "<server_id>"
)
new_server.id # returns the id of the server
new_server.name # => "My Shiny Server"
new_server.state # returns the state of the server e.g. BUILD
new_server.private_ip_address # returns the private ip address
new_server.public_ip_address # returns the public ip address, if any assigned
5. Create a server by passing in a keypair and security group:
new_server = conn.servers.create(
:name=> "My Shiny Server",
:flavor_id => 101,
:image_id => "<image_id>",
:key_name => "my_keypair",
:security_groups => ["My Security Group"]
)
6. Create a server by passing in a network_id:
new_server = conn.servers.create(
:name=> "My Shiny Server",
:flavor_id => 101,
:image_id => "<image_id>",
:key_name => "my_keypair",
:security_groups => ["My Security Group"],
:networks => ["<network_id>"]
)
7. Create a Linux-based persistent server by passing in a bootable volume:
new_server = conn.servers.create(
:name=> "My Sticky Server",
:flavor_id => 104,
:block_device_mapping => [{ 'volume_size' => '',
'volume_id' => "<volume_id>",
'delete_on_termination' => '0',
'device_name' => 'vda'
}]
)
**Note**: In *block_device_mapping*, *volume_size* is ignored; it is automatically retrieved from the specified bootable volume. To delete the bootable volume after the server instance is killed you can set *delete_on_termination* to `1`. To preserve the bootable volume, set it to `0` as shown above.
8. Create a new Linux-based server with advanced personalization options:
new_server = conn.servers.create(
:name => "My Personalized Server",
:flavor_id => 1,
:image_id => 2,
:key_name => "hpdefault",
:security_groups => ["aaa"],
:config_drive => true,
:user_data_encoded => ["This is some encoded user data"].pack('m'),
:personality => [{
'contents' => File.read("/path/to/sample.txt"),
'path' => "/path/to/sample.txt"
}]
)
new_server.id # returns the id of the server
new_server.name # => "My Personalised Server"
# Note: that un-encoded user data can also be provided by setting the user_data property
# although, encoding the data on the client is faster and efficient
new_server = conn.servers.new(
:name => "My Personalized Server",
...
...
)
new_server.user_data = "This is some un-encoded user data"
new_server.save
The personalization options are:
*config_drive*
: Disk accessible to the server that contains a FAT filesystem. If `config_drive` parameter is set to `true` at the time of server creation, the configuration drive is created.
*user_data_encoded* or *user_data*
: Allows additional metadata to be inserted during server creation by supplying a Base64-encoded string in the `user_data_encoded` parameter, or by providing an unencoded string with the `user_data` attribute. Note that encoding the data on the client is faster and more efficient.
*personality*
: Allows files to be injected into the server instance after its creation. The file `contents` are Base64 encoded and injected into the location specified by `path`.
**Note**: The above personalization options are not supported on Windows server instances.
9. Create a new Windows server instance and retrieve the encrypted password:
**Note**: You must retrieve the Windows password immediately after you create the Windows instance. Also, make sure you have a security rule defined to open RDP port 3389 so that you can connect to the Windows server.
10. Get console output:
server = conn.servers.get("<server_id>")
server.console_output(10) # returns 10 lines of console output
11. Get VNC console:
server = conn.servers.get("<server_id>")
server.vnc_console_url('novnc') # URL to access the VNC console of a server from a browser
12. Update a server:
server = conn.servers.get("<server_id>")
server.update_name("My Shiny Server Updated")
13. Reboot a server:
server = conn.servers.get("server_id>")
server.reboot # soft reboot by default
server.reboot("HARD") # hard reboot also possible
14. Rebuild a server:
server = conn.servers.get("<server_id>")
server.rebuild('server_id', 'My Shiny Server Rebuild')
15. Delete a server:
server = conn.servers.get("<server_id>").destroy
## Model Server Volume Operations
1. Attach a volume to a server:
server = conn.servers.get("<server_id>")
server.volume_attachments.create(
:server_id => s.id,
:volume_id => "<volumeid>",
:device => "/dev/sdf"
)
server.reload #reload the server
server.volume_attachments.all #list the attachments
2. Obtain details for an volume attached to a server:
**Note**: You must retrieve the Windows password immediately after you create the Windows instance. Also, make sure you have a security rule defined to open RDP port 3389 so that you can connect to the Windows server.
9. Create a new Linux-based persistent server with a bootable volume
conn.create_persistent_server(
"MyBootableServer",
103,
[{ "volume_size"=>"", # ignored
"volume_id"=>"65904",
"delete_on_termination"=>"0",
"device_name"=>"vda"
}] ,
{
'security_groups' => ["mysecgroup"],
'key_name' => "mykey"
}
)
**Note**: In *block_device_mapping*, *volume_size* is ignored; it is automatically retrieved from the specified bootable volume. To delete the bootable volume after the server instance is killed you can set *delete_on_termination* to `1`. To preserve the bootable volume, set it to `0` as shown above.
10. Create a new Linux-based server with advanced personalisation options:
response = conn.create_server(
"My Shiny Server",
flavor_id,
image_id,
{
'security_groups' => ["SecGroup1, SecGroup2"],
'key_name' => "MyKeyPair1",
'config_drive' => true,
'user_data_encoded' => ["This is some encoded user data"].pack('m'),
'personality' => [{
'contents' => File.read("/path/to/sample.txt"),
'path' => "/path/to/sample.txt"
}]
}
)
server = response.body['server']
server['id'] # returns the id of the new server
The personalization options are:
*config_drive*
: Disk accessible to the server that contains a FAT filesystem. If `config_drive` parameter is set to `true` at the time of server creation, the configuration drive is created.
*user_data_encoded*
: Allows additional metadata to be inserted during server creation by supplying a Base64-encoded string in the `user_data_encoded` parameter.
*personality*
: Allows files to be injected into the server instance after its creation. The file `contents` are Base64 encoded and injected into the location specified by `path`.
**Note**: The above personalization options are not supported on Windows server instances.
11. Update the name for a server:
address = conn.update_server("<server_id>", {'name' => "My Cool Server"})
response = conn.get_server_details("<server_id>")
response.body['server']['name'] # => "My Cool Server"