January 7, 2016

Classes
Tags fog compute

Managing VSI's and Bare Metal servers with fog-softlayer

Using the fog-softlayer gem to create, destroy, and get information about virtual and bare metal servers

Create a connection to SoftLayer Compute Service

	require 'fog/softlayer'
	@sl = Fog::Compute[:softlayer]

Use the Models

List all servers

@sl.servers # list all servers
@sl.servers.size # get a count of all servers

Get a server’s details

   server = @sl.servers.get(<server id>)
   server.name # => 'hostname.example.com'
   server.created_at # => DateTime the server was created
   server.state # => 'Running', 'Stopped', 'Terminated', etc.

Get a server’s details using ip address

   server = @sl.servers.get_by_ip(<server ip>)
   server.name # => 'hostname.example.com'
   server.created_at # => DateTime the server was created
   server.state # => 'Running', 'Stopped', 'Terminated', etc.

Get all servers tagged with certain tags.

	prod_fe_servers = @sl.servers.tagged_with(['production', 'frontend'])
	# => [ <Fog::Compute::Softlayer::Server>,
	#	<Fog::Compute::Softlayer::Server>,
	#	<Fog::Compute::Softlayer::Server>,
	#	<Fog::Compute::Softlayer::Server>,
	#	<Fog::Compute::Softlayer::Server>,]		

Get a server’s public/frontend VLAN

	server = @sl.servers.get(12345)
	server.vlan
	# => <Fog::Network::Softlayer::Network
    #	id=123456,
	#   name='frontend-staging-vlan',
	#   modify_date="2014-02-22T12:42:31-06:00",
	#   note=nil,
	#   tags=['sparkle', 'motion'],
	#   type="STANDARD",
	#   datacenter=    <Fog::Network::Softlayer::Datacenter
	#     id=168642,
	#     long_name="San Jose 1",
	#     name="sjc01"
	#   >,
	#   network_space="PUBLIC",
	#   router={"hostname"=>"fcr01a.sjc01", "id"=>82412, "datacenter"=>{"id"=>168642, "longName"=>"San Jose 1", "name"=>"sjc01"}}
  	# >

Get a server’s private/backend VLAN

	server = @sl.servers.get(12345)
	server.private_vlan
	# =>  <Fog::Network::Softlayer::Network
	#    id=123456,
	#    name='backend-staging-vlan',
	#    modify_date="2014-02-22T12:42:33-06:00",
	#    note=nil,
	#    tags=[],
	#    type="STANDARD",
	#    datacenter=    <Fog::Network::Softlayer::Datacenter
	#	    id=168642,
	#    	long_name="San Jose 1",
	#    	name="sjc01"
	#   >,
	#   network_space="PRIVATE",
    #	router={"hostname"=>"bcr01a.sjc01", "id"=>82461, "datacenter"=>{"id"=>168642, "longName"=>"San Jose 1", "name"=>"sjc01"}}
  	# >

Get a server’s tags

		server = @sl.servers.get(12345)
		server.tags
		# => ['production', 'frontend']

Add tags to a server

		server = @sl.servers.get(12345)
		server.tags
		# => ['production', 'frontend']
		server.add_tags(['sparkle', 'motion']
		# => true
		server.tags
		# => ['production', 'frontend', 'sparkle', 'motion']

Delete tags from a server

		server = @sl.servers.get(12345)
		server.tags
		# => ['production', 'frontend', 'sparkle', 'motion']
		server.delete_tags(['sparkle', 'motion']
		# => true
		server.tags
		# => ['production', 'frontend']

Provision a new VM with flavor (simple).

     opts = {
     	:flavor_id => "m1.small",
     	:image_id => "23f7f05f-3657-4330-8772-329ed2e816bc",
     	:name => "test",
     	:datacenter => "ams01"
     }
     new_server = @sl.servers.create(opts)
     new_server.id # => 1337

Provision a new Bare Metal instance with flavor (simple).

     opts = {
     	:flavor_id => "m1.medium",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test1",
     	:datacenter => "ams01",
     	:bare_metal => true
     }
     @sl.servers.create(opts)
     new_server.id # => 1338

Provision a new VM without flavor.

   	opts = {
     	:cpu => 2,
     	:ram => 2048,     	
     	:disk => [{'device' => 0, 'diskImage' => {'capacity' => 100 } }],
     	:ephemeral_storage => true,
     	:domain => "not-my-default.com",
     	:name => "hostname",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test2",
     	:datacenter => "ams01"     
     }

Provision a Bare Metal Instance without a flavor

   opts = {
     	:cpu => 8,
     	:ram => 16348,     	
     	:disk => {'capacity' => 500 },
     	:ephemeral_storage => true,
     	:domain => "not-my-default.com",
     	:name => "hostname",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test2",
     	:datacenter => "ams01",
     	:bare_metal => true
     }

Create a server with one or more key pairs (also see key_pairs examples on Github )

	the_first_key = @sl.key_pairs.by_label('my-new-key')
	# => <Fog::Compute::Softlayer::KeyPair>
	the_second_key = @sl.key_pairs.by_label('my-other-new-key')
	# => <Fog::Compute::Softlayer::KeyPair>

	opts = {
		:flavor_id => 'm1.small',
		:os_code => 'UBUNTU_LATEST',
		:datacenter => 'hkg02',
		:name => 'cphrmky',
		:key_pairs => [ the_first_key, the_second_key ]
	}
	@sl.servers.create(opts)
	# => <Fog::Compute::Softlayer::Server>

Delete a VM or Bare Metal instance.

   	  @sl.servers.get(<server id>).destroy

Provision a Server (works the same for VM and Bare Metal) into a specific VLAN

	# I want to launch another server to hold docker containers into my existing staging VLANs
	# I'll start by getting a staging server so I can use its vlans as a reference.
	staging_server = @sl.servers.tagged_with(['staging', 'docker']).first # => <Fog::Compute::Softlayer::Server>

	opts = {
	  :flavor_id => 'm1.large',
	  :image_id => '23f7f05f-3657-4330-8772-329ed2e816bc',  # Ubuntu Docker Image
	  :domain => 'staging.example.com',
	  :datacenter => 'ams01', # This needs to be the same datacenter as the target VLAN of course.
	  :name => 'additional-docker-host',
	  :vlan => staging.server.vlan, # Passing in a <Fog::Network::Softlayer::Network> object.
	  :private_vlan => staging.server.private_vlan.id, # Passing in an Integer (the id of a network/vlan) works too.
	}

	new_staging_server = @sl.servers.create(opts)
	# => <Fog::Compute::Softlayer::Server>

Provision a Server with only a private network.

	opts = {
	  :flavor_id => 'm1.large',
	  :os_code => 'UBUNTU_LATEST',
	  :domain => 'example.com',
	  :datacenter => 'ams01',
	  :name => 'private-eye',
	  :private_network_only => true
	}

	private_vm = @sl.servers.create(opts)
	# => <Fog::Compute::Softlayer::Server>

Provision a Server with 1Gbps network components.

	opts = {
	  :flavor_id => 'm1.large',
	  :os_code => 'UBUNTU_LATEST',
	  :domain => 'example.com',
	  :datacenter => 'wdc01',
	  :name => 'speedy-tubes',
	  :network_components => [ {:speed => 1000 } ],
	}

	private_vm = @sl.servers.create(opts)
	# => <Fog::Compute::Softlayer::Server>

Provision a Server with user metadata.

     opts = {
      :flavor_id => "m1.small",
      :image_id => "23f7f05f-3657-4330-8772-329ed2e816bc",
      :name => "test",
      :datacenter => "ams01",
      :user_data => "my-custom-user-metadata"
     }

     new_server = @sl.servers.create(opts)
     new_server.user_data # => "my-custom-user-metadata"
     new_server.user_data = "new-user-metadata"
     new_server.user_data # => "new-user-metadata"

Start, Stop, and Reboot a existing server (works the same for VMs and Bare Metal).

		srvr = @sl.servers.get(123456)
		srvr.ready? # true

		srvr.reboot # true

		srvr.stop # true
		srvr.ready? # false
		srvr.state # "Halted"

		srvr.start # true
		srvr.ready # true
		srvr.state # "Running"

Get all options to create a bare metal.

   @sl.servers.get_bm_create_options

Get all options to create a VM.

   @sl.servers.get_vm_create_options

Get all active tickets of a server.

    server = @sl.servers.get(123456)
    server.get_active_tickets

Get all users of a server.

    server = @sl.servers.get(123456)
    server.get_users

Get all upgrade options of a server.

    server = @sl.servers.get(123456)
    server.get_upgrade_options

Update a virtual guest server. Hash keys are the categories and the hash values are the capacity. You can retrieve them from upgrade options.

    new_attributes = {
      :guest_core => 2,
      :ram => 1, # this value is in GBs
      :port_speed => 100, # this value is in MPBSs
      :time => Time.now + 5.minutes # if you don't specify, time will be equal to now
    }

    server = @sl.servers.get(123456)
    server.update(new_attributes)

Update a bare metal server. Hash keys are the categories and the hash values are the capacity. You can retrieve them from upgrade options.

    new_attributes = {
      :ram => 4, # this value is in GBs
      :port_speed => 100, # this value is in MPBSs
      :maintenance_window => 1111 # should see examples/network "Get a datacenter maintenance windows."
    }

    server = @sl.servers.get(123456)
    server.update(new_attributes)

Generate an order template for VM with flavor (simple).

     opts = {
     	:flavor_id => "m1.small",
     	:image_id => "23f7f05f-3657-4330-8772-329ed2e816bc",
     	:name => "test",
     	:datacenter => "ams01"
     }
     new_server = @sl.servers.new(opts)
     new_server.generate_order_template

Generate an order template for Bare Metal instance with flavor (simple).

     opts = {
     	:flavor_id => "m1.medium",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test1",
     	:datacenter => "ams01",
     	:bare_metal => true
     }
     new_server = @sl.servers.new(opts)
     new_server.generate_order_template

Generate an order template for VM without flavor.

   	opts = {
     	:cpu => 2,
     	:ram => 2048,
     	:disk => [{'device' => 0, 'diskImage' => {'capacity' => 100 } }],
     	:ephemeral_storage => true,
     	:domain => "not-my-default.com",
     	:name => "hostname",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test2",
     	:datacenter => "ams01"
     }
     new_server = @sl.servers.new(opts)
     new_server.generate_order_template

Generate an order template for Bare Metal Instance without a flavor

   opts = {
     	:cpu => 8,
     	:ram => 16348,
     	:disk => {'capacity' => 500 },
     	:ephemeral_storage => true,
     	:domain => "not-my-default.com",
     	:name => "hostname",
     	:os_code => "UBUNTU_LATEST",
     	:name => "test2",
     	:datacenter => "ams01",
     	:bare_metal => true
     }
     new_server = @sl.servers.new(opts)
     new_server.generate_order_template