Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working challenge1 with threading, minor printing issues, still needs…

… to accept input
  • Loading branch information...
commit cb686bfd1d3c27d1bb35dadae59cd1593bbe76fb 1 parent 4c26762
@madasi authored
Showing with 71 additions and 0 deletions.
  1. +71 −0 challenge1.py
View
71 challenge1.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright 2013 Mark Silence
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+
+Challenge 1: Write a script that builds three 512 MB Cloud Servers that following a similar naming convention.
+(ie., web1, web2, web3) and returns the IP and login credentials for each server.
+Use any image you want. Worth 1 point
+
+"""
+
+import os
+import pyrax
+import threading
+
+def server_done(server):
+ with threading.Semaphore() as s:
+ global completed
+ if server.status == 'ACTIVE':
+ inventory[server.name]["network"] = server.networks['public']
+ completed += 1
+ else:
+ del inventory[server.name]
+ server.delete()
+ replacement = cs.servers.create(server.name, centos63_image.id, flavor_512.id)
+ inventory[replacement.name] = {"password":replacement.adminPass,"name":replacement.name}
+ wait_thread = pyrax.utils.wait_until(replacement,"status",['ACTIVE','ERROR'],callback=server_done,interval=10,attempts=0,verbose=True,verbose_atts=["name","status"])
+ threads.append(wait_thread)
+ if completed >= 3:
+ goal_reached.set()
+
+
+if __name__ == '__main__':
+ goal_reached = threading.Event()
+ pyrax.set_credential_file(os.path.expanduser('~/.rackspace_cloud_credentials'))
+ cs = pyrax.cloudservers
+ name = "dev_{0:02d}"
+ centos63_image = [img for img in cs.images.list()
+ if "CentOS 6.3" in img.name][0]
+ flavor_512 = [flavor for flavor in cs.flavors.list()
+ if flavor.ram == 512][0]
+ inventory={}
+ threads=[]
+ completed = 0
+ for i in range (1,4):
+ started = cs.servers.create(name.format(i), centos63_image.id, flavor_512.id)
+ inventory[started.name] = {"password":started.adminPass,"name":started.name}
+ wait_thread = pyrax.utils.wait_until(started,"status",['ACTIVE','ERROR'],callback=server_done,interval=5,attempts=0,verbose=True,verbose_atts=["name","status"])
+ threads.append(wait_thread)
+ print "3 Servers are now building."
+ goal_reached.wait()
+ print "Your servers are ready!"
+ for server in inventory.values():
+ print server
+ print "Server ", server["name"], "-- Password: ", server["password"], " -- Network: ", server["network"]
+ for thread in threads:
+ thread.join()
Please sign in to comment.
Something went wrong with that request. Please try again.