From 2f42739033803a1b727b1c8db7db4ce4394629ce Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Mon, 21 Sep 2015 16:36:37 +0800 Subject: [PATCH] Add firstapp sec2&3 for fog Adds section 2 and 3 for the fog SDK for FirstApp tutorial, as well as fixes the repo that the faafo is cloned from in section 1. Co-Author: Caleb Boylan Co-Author: Tom Fifield Partial-Bug: #1449329 Change-Id: I553af22e904261d3465eda4301a664d24fe91b31 --- firstapp/samples/fog/introduction.rb | 134 +++++++++++++++++++++++++++ firstapp/samples/fog/scaling_out.rb | 98 ++++++++++++++++++++ firstapp/source/introduction.rst | 88 +++++++++++++++++- firstapp/source/scaling_out.rst | 34 +++++++ 4 files changed, 353 insertions(+), 1 deletion(-) create mode 100755 firstapp/samples/fog/introduction.rb create mode 100755 firstapp/samples/fog/scaling_out.rb diff --git a/firstapp/samples/fog/introduction.rb b/firstapp/samples/fog/introduction.rb new file mode 100755 index 000000000..2290f1f51 --- /dev/null +++ b/firstapp/samples/fog/introduction.rb @@ -0,0 +1,134 @@ +# step-1 +userdata = '''#!/usr/bin/env bash +curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i faafo -i messaging -r api -r worker -r demo +''' + +instance_name = 'all-in-one' +testing_instance = conn.servers.create(:name => instance_name, :flavor_ref => flavor.id, :image_ref => image.id, :key_name => keypair_name, :user_data => userdata, :security_groups => [all_in_one_security_group["name"]]) + +# step-2 +userdata = '''#!/usr/bin/env bash +curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i messaging -i faafo -r api -r worker -r demo +''' + +# step-3 +all_in_one_security_group_response = conn.create_security_group('all-in-one', 'network access for all-in-one application.') +all_in_one_security_group = all_in_one_security_group_response.body["security_group"] +conn.create_security_group_rule(all_in_one_security_group["id"], 'TCP', '80', '80', '0.0.0.0/0') +conn.create_security_group_rule(all_in_one_security_group["id"], 'TCP', '22', '22', '0.0.0.0/0') + +# step-4 +conn.list_security_groups.body["security_groups"] + +# step-5 +conn.delete_security_group_rule(rule["id"]) +conn.delete_security_group(security_group["id"]) + +# step-6 +conn.list_security_groups(testing_instance["id"]) + +# step-7 +unused_floating_ips = nil +for floating_ip in conn.list_all_addresses.body["floating_ips"] + if not floating_ip.server_id + unused_floating_ip = floating_ip + puts "Found an unused Floating IP: %s" % floating_ip + break + end +end + + +# step-8 +pool = conn.list_address_pools.body["floating_ip_pools"][0] + +# step-9 +unused_floating_ip = conn.allocate_address(pool) + +# step-10 +conn.associate_address(testing_instance, unused_floating_ip) + +# step-11 +# XXX TODO TBC +worker_group_response = conn.create_security_group('worker', 'for services that run on a worker node') +worker_group = worker_group_response.body["security_group"] +conn.create_security_group_rule(worker_group["id"], 'TCP', '22', '22', '0.0.0.0/0') + +controller_group_response = conn.create_security_group('control', 'for services that run on a control node') +controller_group = controller_group_response.body["security_group"] +conn.create_security_group_rule(controller_group["id"], 'TCP', '80', '80', '0.0.0.0/0') +conn.create_security_group_rule(controller_group["id"], 'TCP', '22', '22', '0.0.0.0/0') +conn.create_security_group_rule(controller_group["id"], 'TCP', '5672', '5672', '0.0.0.0/0', worker_group["id"]) + +userdata = '''#!/usr/bin/env bash +curl -L -s http://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i messaging -i faafo -r api +''' + +instance_controller_1 = conn.servers.create(:name => 'app-controller', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [controller_group["name"]]) + +instance_controller_1.wait_for { ready? } + +puts 'Checking for unused Floating IP...' +unused_floating_ip = nil +for floating_ip in conn.list_all_addresses.body["floating_ips"] + if not floating_ip["instance_id"] + unused_floating_ip = floating_ip + break + end +end + + +if not unused_floating_ip + pool = conn.list_address_pools.body["floating_ip_pools"][0] + puts "Allocating new Floating IP from pool: #{pool['name']}" + unused_floating_ip = conn.allocate_address(pool["name"]).body["floating_ip"] +end + +instance_controller_1.associate_address(unused_floating_ip["ip"]) +puts 'Application will be deployed to http://%s' % unused_floating_ip["ip"] + +# step-12 +# XXX TODO TBC +instance_controller_addresses = instance_controller_1.all_addresses +if instance_controller_addresses[0]["ip"] + ip_controller = instance_controller_addresses[0]["ip"] +else + ip_controller = instance_controller_addresses[0]["fixed_ip"] +end + +userdata = "#!/usr/bin/env bash +curl -L -s http://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i faafo -r worker -e 'http://#{ip_controller}' -m 'amqp://guest:guest@#{ip_controller}:5672/' +" + +instance_worker_1 = conn.servers.create(:name => 'app-worker-1', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [worker_group["name"]]) + +instance_worker_1.wait_for { ready? } + +puts 'Checking for unused Floating IP...' +unused_floating_ip = nil +for floating_ip in conn.list_all_addresses.body["floating_ips"] + if not floating_ip["instance_id"] + unused_floating_ip = floating_ip + break + end +end + +if not unused_floating_ip + pool = conn.list_address_pools.body["floating_ip_pools"][0] + puts "Allocating new Floating IP from pool: #{pool['name']}" + unused_floating_ip = conn.allocate_address(pool["name"]).body["floating_ip"] +end + +instance_worker_1.associate_address(unused_floating_ip["ip"]) +puts 'The worker will be available for SSH at %s' % unused_floating_ip["ip"] + + +# step-13 +# XXX TODO TBC +ip_instance_worker_1 = instance_worker_1.all_addresses[0]["fixed_ip"] +puts ip_instance_worker_1 + +# step-14 diff --git a/firstapp/samples/fog/scaling_out.rb b/firstapp/samples/fog/scaling_out.rb new file mode 100755 index 000000000..7b60cfb6f --- /dev/null +++ b/firstapp/samples/fog/scaling_out.rb @@ -0,0 +1,98 @@ +# step-1 +for instance in conn.servers + if ['all-in-one','app-worker-1', 'app-worker-2', 'app-controller'].include?(instance.name) + puts 'Destroying Instance: %s' % instance.name + conn.delete_server(instance.id) + end +end + +for group in conn.list_security_groups.body["security_groups"] + if ['control', 'worker', 'api', 'services'].include?(group["name"]) + puts 'Deleting security group: %s' % group['name'] + conn.delete_security_group(group['id']) + end +end + +# step-2 +api_group_response = conn.create_security_group('api', 'for API services only') +api_group = api_group_response.body["security_group"] +conn.create_security_group_rule(api_group["id"], 'TCP', '80', '80', '0.0.0.0/0') +conn.create_security_group_rule(api_group["id"], 'TCP', '22', '22', '0.0.0.0/0') + +worker_group_response = conn.create_security_group('worker', 'for services that run on a worker node') +worker_group = worker_group_response.body["security_group"] +conn.create_security_group_rule(worker_group["id"], 'TCP', '22', '22', '0.0.0.0/0') + +services_group_response = conn.create_security_group('services', 'for DB and AMQP services only') +services_group = services_group_response.body["security_group"] +conn.create_security_group_rule(services_group["id"], 'TCP', '80', '80', '0.0.0.0/0') +conn.create_security_group_rule(services_group["id"], 'TCP', '22', '22', '0.0.0.0/0') +conn.create_security_group_rule(services_group["id"], 'TCP', '3306', '3306', '0.0.0.0/0', api_group["id"]) +conn.create_security_group_rule(services_group["id"], 'TCP', '5672', '5672', '0.0.0.0/0', worker_group["id"]) +conn.create_security_group_rule(services_group["id"], 'TCP', '5672', '5672', '0.0.0.0/0', api_group["id"]) + +# step-3 +def get_floating_ip (conn) + '''A helper function to re-use available Floating IPs''' + unused_floating_ip = nil + for floating_ip in conn.list_all_addresses.body["floating_ips"] + if not floating_ip["instance_id"] + unused_floating_ip = floating_ip + break + end + end + if not unused_floating_ip + pool = conn.list_address_pools.body["floating_ip_pools"][0] + puts 'Allocating new Floating IP from pool: {}'. pool + unused_floating_ip = conn.allocate_address(pool) + end + return unused_floating_ip +end + +# step-4 +userdata = '''#!/usr/bin/env bash +curl -L -s http://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i database -i messaging +''' + +instance_services= conn.servers.create(:name => 'app-services', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [services_group["name"]]) + +instance_services.wait_for { ready? } +services_ip = instance_services.accessIPv4 + + +# step-5 +userdata = "#!/usr/bin/env bash +curl -L -s http://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i faafo -r api -m 'amqp://guest:guest@#{services_ip}:5672/' \ + -d 'mysql://faafo:password@#{services_ip}:3306/faafo' +" + +instance_api_1 = conn.servers.create(:name => 'app-api-1', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [api_group["name"]]) +instance_api_2 = conn.servers.create(:name => 'app-api-2', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [api_group["name"]]) + + +instance_api_1.wait_for { ready? } +api_1_ip = instance_api_1.accessIPv4 +instance_api_2.wait_for { ready? } +api_2_ip = instance_api_2.accessIPv4 + + + +for instance in [instance_api_1, instance_api_2] + floating_ip = get_floating_ip(conn) + instance.associate_address(floating_ip["ip"]) + puts "allocated #{floating_ip["ip"]} to #{instance.name}" +end + +# step-6 +userdata = "#!/usr/bin/env bash +curl -L -s http://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \ + -i faafo -r worker -e 'http://#{api_1_ip}' -m 'amqp://guest:guest@#{services_ip}:5672/' +" + +instance_worker_1 = conn.servers.create(:name => 'app-worker-1', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [worker_group["name"]]) +instance_worker_2 = conn.servers.create(:name => 'app-worker-2', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [worker_group["name"]]) +instance_worker_3 = conn.servers.create(:name => 'app-worker-3', :flavor_ref => flavor.id, :image_ref => image.id, :key_name => 'demokey', :user_data => userdata, :security_groups => [worker_group["name"]]) + +# step-7 diff --git a/firstapp/source/introduction.rst b/firstapp/source/introduction.rst index b0bb927a2..e847d6208 100644 --- a/firstapp/source/introduction.rst +++ b/firstapp/source/introduction.rst @@ -14,7 +14,7 @@ particular. It also describes some commands in the previous section. .. only:: fog - .. warning:: This section has not yet been completed for the fog SDK. + .. highlight:: ruby .. only:: jclouds @@ -203,6 +203,12 @@ do not need to execute these commands again.) :start-after: step-1 :end-before: step-2 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-1 + :end-before: step-2 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -253,6 +259,12 @@ your cloud provider to confirm the user name. :start-after: step-2 :end-before: step-3 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-2 + :end-before: step-3 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -307,6 +319,12 @@ port 22): :start-after: step-3 :end-before: step-4 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-3 + :end-before: step-4 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -332,6 +350,12 @@ You can list available security groups with: :start-after: step-4 :end-before: step-5 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-4 + :end-before: step-5 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -353,6 +377,12 @@ Once you have created a rule or group, you can also delete it: :start-after: step-5 :end-before: step-6 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-5 + :end-before: step-6 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -374,6 +404,12 @@ To see which security groups apply to an instance, you can: :start-after: step-6 :end-before: step-7 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-6 + :end-before: step-7 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -418,6 +454,27 @@ then associate it to your instance's network interface. the IP address of the instance, it causes OpenStack to establish the network translation rules to allow an *additional* IP address. +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-7 + :end-before: step-8 + + If you have no free floating IPs that have been previously allocated + for your project, first select a floating IP pool offered by your + provider. In this example, we have selected the first one and assume + that it has available IP addresses. + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-8 + :end-before: step-9 + + Now request that an address from this pool be allocated to your project. + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-9 + :end-before: step-10 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -478,6 +535,12 @@ project, attach it to an instance. :start-after: step-10 :end-before: step-11 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-10 + :end-before: step-11 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -529,6 +592,12 @@ Parameter Description Values :start-after: step-11 :end-before: step-12 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-11 + :end-before: step-12 + .. only:: libcloud @@ -557,6 +626,12 @@ Next, start a second instance, which will be the worker instance: :start-after: step-12 :end-before: step-13 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-12 + :end-before: step-13 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -605,6 +680,12 @@ address of the worker: :start-after: step-13 :end-before: step-14 +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :start-after: step-13 + :end-before: step-14 + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py @@ -745,6 +826,11 @@ information, the flavor ID, and image ID. .. literalinclude:: ../samples/shade/introduction.py :language: python +.. only:: fog + + .. literalinclude:: ../samples/fog/introduction.rb + :language: ruby + .. only:: libcloud .. literalinclude:: ../samples/libcloud/introduction.py diff --git a/firstapp/source/scaling_out.rst b/firstapp/source/scaling_out.rst index 282a7bcbc..3d4218021 100644 --- a/firstapp/source/scaling_out.rst +++ b/firstapp/source/scaling_out.rst @@ -159,6 +159,11 @@ are no longer working, remove them and re-create something new. .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-1 :end-before: step-2 @@ -180,6 +185,11 @@ required security groups. .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-2 :end-before: step-3 @@ -200,6 +210,11 @@ reaching your Floating IP quota too quickly. .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-3 :end-before: step-4 @@ -222,6 +237,11 @@ between the services. .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-4 :end-before: step-5 @@ -246,6 +266,11 @@ multiple API services: .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-5 :end-before: step-6 @@ -281,6 +306,11 @@ To increase the overall capacity, we will now add 3 workers: .. literalinclude:: ../samples/shade/scaling_out.py :language: python + +.. only:: fog + + .. literalinclude:: ../samples/fog/scaling_out.rb + :language: ruby :start-after: step-6 :end-before: step-7 @@ -439,6 +469,10 @@ and run the code 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/scaling_out.rb + :language: ruby .. only:: shade