diff --git a/firstapp/samples/fog/getting_started.rb b/firstapp/samples/fog/getting_started.rb old mode 100644 new mode 100755 index d21bc843c..6edaae0c1 --- a/firstapp/samples/fog/getting_started.rb +++ b/firstapp/samples/fog/getting_started.rb @@ -10,18 +10,18 @@ region_name = 'your_region_name' conn = Fog::Compute.new({ :provider => 'openstack', :openstack_auth_url => auth_url + '/v2.0/tokens', - :openstack_username => auth_username - :openstack_tenant => project_name - :openstack_api_key => auth_password + :openstack_username => auth_username, + :openstack_tenant => project_name, + :openstack_api_key => auth_password, }) # step-2 -images = conn.list_images -print images.body +images = conn.images.all +print images # step-3 -flavors = conn.list_flavors -print flavors.body +flavors = conn.flavors.all +print flavors # step-4 image_id = '2cccbea0-cea9-4f86-a3ed-065c652adda5' @@ -30,26 +30,128 @@ print image # step-5 flavor_id = '3' -image = conn.flavor.get flavor_id +flavor = conn.flavors.get flavor_id print flavor # step-6 instance_name = 'testing' -testing_instance = conn.servers.create(:name => instance_name, :flavor_ref => flavor.id, :image_ref => image.id) +testing_instance = conn.servers.create(:name => instance_name, + :flavor_ref => flavor.id, + :image_ref => image.id) # step-7 -conn.servers +print conn.servers # step-8 testing_instance.destroy # step-9 +key_pair_name = "demokey" + +pub_key_file = "~/.ssh/id_rsa.pub" + +puts "Checking for existing SSH keypair" + +for pair in conn.key_pairs.all() + if pair.name == key_pair_name + key_pair = pair + puts "Key pair \"" + key_pair.name + "\" exists, skipping +import" + break + end +end + +if not key_pair + puts "Uploading keypair from ~/.ssh/id_rsa.pub" + key_file = File.open(File.expand_path(pub_key_file)) + public_key = key_file.read + key_pair = conn.key_pairs.create :name => key_pair_name, + :public_key => public_key +end # step-10 -all_in_one_security_group = conn.create_security_group 'all-in-one' 'network access for all-in-one application.' -conn.create_security_group_rule all_in_one_security_group 'TCP' 80 80 -conn.create_security_group_rule all_in_one_security_group 'TCP' 22 22 +for security_group in conn.security_groups.all + if security_group.name == 'all-in-one' + all_in_one_security_group = security_group + break + end +end + +if not all_in_one_security_group + conn.create_security_group 'all-in-one', + 'network access for all-in-one application.' + for security_group in conn.security_groups.all + if security_group.name == 'all-in-one' + all_in_one_security_group = security_group + break + end + end + + conn.security_group_rules.create :ip_protocol => 'TCP', + :from_port => 22, + :to_port => 22, + :parent_group_id => all_in_one_security_group.id + conn.security_group_rules.create :ip_protocol => 'TCP', + :from_port => 80, + :to_port => 80, + :parent_group_id => all_in_one_security_group.id +end # step-11 +userdata = "#!/usr/bin/env bash +curl -L -s https://git.openstack.org/cgit/stackforge/faafo/plain/contrib/install.sh \ +| bash -s -- \ -i faafo -i messaging -r api -r worker -r demo" + # step-12 +puts "Checking for existing instance" +for server in conn.servers.all + if server.name == instance_name + instance = server + break + end +end + +if not instance + puts "No test instance found, creating one now" + instance = conn.servers.create :name => instance_name, + :flavor_ref => flavor.id, + :image_ref => image.id, + :key_name => key_pair.name, + :user_data => userdata, + :security_groups => all_in_one_security_group +end + +until instance.ready? + for server in conn.servers + if server.name == instance.name + instance = server + break + end + end +end + # step-13 +puts "Checking for unused Floating IP..." +for address in conn.addresses.all() + if not address.instance_id + ip_address = address + puts "Unused IP " + ip_address.ip + " found, it will be used + instead of creating a new IP" + break + end +end + +if not ip_address + puts "Allocating new Floating IP" + ip_address = conn.addresses.create() +end + +# step-14 +if instance.public_ip_addresses.length > 0 + puts "Instance already has a floating IP address" +else + instance.associate_address(ip_address.ip) +end + +# step-15 +puts "Fractals app will be deployed to http://#{ip_address.ip}" diff --git a/firstapp/source/getting_started.rst b/firstapp/source/getting_started.rst index d79fae2b8..a80145f28 100644 --- a/firstapp/source/getting_started.rst +++ b/firstapp/source/getting_started.rst @@ -189,6 +189,7 @@ to run code snippets in your language of choice. .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-1 :end-before: step-2 @@ -289,6 +290,7 @@ To list the images that are available in your cloud, run some API calls: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-2 :end-before: step-3 @@ -382,6 +384,7 @@ You can also get information about available flavors: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-3 :end-before: step-4 @@ -499,6 +502,7 @@ image that you picked in the previous section: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-4 :end-before: step-5 @@ -580,6 +584,7 @@ Next, choose which flavor you want to use: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-5 :end-before: step-6 @@ -682,6 +687,7 @@ Create the instance. .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-6 :end-before: step-7 @@ -745,6 +751,7 @@ If you list existing instances: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-7 :end-before: step-8 @@ -892,6 +899,7 @@ money. Destroy cloud resources to avoid unexpected expenses. .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-8 :end-before: step-9 @@ -952,7 +960,14 @@ instance: .. only:: fog - .. warning:: This section has not been completed. + In the following example, :code:`pub_key_file` should be set to + the location of your public SSH key file. + + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-9 + :end-before: step-10 + .. only:: libcloud @@ -993,6 +1008,7 @@ instance: .. only:: fog .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby :start-after: step-10 :end-before: step-11 @@ -1021,7 +1037,10 @@ instance: .. only:: fog - .. warning:: This section has not been completed. + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-11 + :end-before: step-12 .. only:: libcloud @@ -1051,7 +1070,10 @@ request the instance, wait for it to build. .. only:: fog - .. warning:: This section has not been completed. + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-12 + :end-before: step-13 .. only:: libcloud @@ -1091,7 +1113,18 @@ address to your instance. .. only:: fog - .. warning:: This section has not been completed. + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-13 + :end-before: step-14 + + This will get an ip address that you can assign to your instance + with: + + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-14 + :end-before: step-15 .. only:: libcloud @@ -1155,6 +1188,12 @@ time. Consider enjoying a cup of coffee while you wait. After the application deploys, you can visit the awesome graphic interface at the following link by using your preferred browser. +.. only:: fog + + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + :start-after: step-15 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/getting_started.py @@ -1206,6 +1245,11 @@ as a single script. Before you run this script, confirm that you have set your authentication information, the flavor ID, and image ID. +.. only:: fog + + .. literalinclude:: ../samples/fog/getting_started.rb + :language: ruby + .. only:: libcloud .. literalinclude:: ../samples/libcloud/getting_started.py