Ejemplo n.º 1
0
def provision():
    """Create a new cloud server instance"""
    if "server" not in env:
        util.exit("Please specify a target server")
    conn = cloud_connect()
    image = choose_cloud_option(conn.list_images, IMAGE_RE, "image")
    size = choose_cloud_option(conn.list_sizes, SIZE_RE, "size")
    root_password = getpass.getpass(
        "Choose a root password for the new server: ")
    ssh_key = util.get_ssh_key()
    users = ScriptDeployment(debian.make_user_script(os.environ["USER"], ssh_key))

    # a task that first installs the ssh key, and then runs the script
    msd = MultiStepDeployment([SSHKeyDeployment(ssh_key), users])
    out("Creating %s (%s) on %s" % (image.name, size.name, image.driver.name))
    node = conn.deploy_node(name=env.server["name"], image=image, size=size,
        deploy=msd)
    out(node)
    while get_node(node.uuid).state != 0:
        dot()
    out("Node is up.")
    env.hosts[0] = env.host_string = node.public_ips[0]
    conf = server_conf.read(SERVER_CONF_PATH)
    conf[env.server["label"]]["hostname"] = node.public_ips[0]
    server_conf.write(conf, SERVER_CONF_PATH)
    set_root_password(node.uuid, root_password)
    #Make my shell zsh
    with settings(user="******"):
        packages.apt("zsh")
        login = os.environ["USER"]
        util.script("chsh --shell /bin/zsh " + login, name="Use zshell")
        out("Please set a password for %s on %s" % (login, env.host_string))
        run("passwd " + login)
    return node
Ejemplo n.º 2
0
def choose_cloud_option(listFunc, regex, name):
    """Select an option from a cloud API response

    listFunc -- a function that will return data objects from a cloud API
    regex -- expression to test for
    name -- name of type of object (Image, Flavor, etc) for error message
    """
    item = [o for o in listFunc() if regex.match(o.name)]
    if len(item) != 1:
        exit("Could not find exactly one %s matching %s" %
            (regex.pattern, name), EC_SERVICE)
    return item[0]
Ejemplo n.º 3
0
def get_node(uuid, exit=True):
    """Get a cloud node by UUID

    Keyword arguments:
    exit -- exit the program if the node is not found (default True)
    """
    conn = cloud_connect()
    matches = [node for node in conn.list_nodes() if node.uuid == uuid]
    if len(matches) == 1:
        return matches[0]
    if exit:
        exit("No node with UUID %s" % uuid, EC_DATA)
Ejemplo n.º 4
0
def cloud_connect():
    """Prompt for credentials if needed and return cloud driver"""
    global api_key
    global username
    cert_path = os.path.join(os.path.dirname(__file__), "python", "cacert.pem")
    if not os.path.exists(cert_path):
        out("Installing CA Certificates for Cloud APIs")
        request = requests.get("http://curl.haxx.se/ca/cacert.pem")
        if request.status_code > 299:
            exit("Could not download CA Certs from %s", EC_NETWORK)
        with open(cert_path, "wb") as cert_file:
            cert_file.write(request.content)
    libcloud.security.VERIFY_SSL_CERT = True
    libcloud.security.CA_CERTS_PATH.append(cert_path)
    if not username:
        username = raw_input("RackSpace Username: "******"RackSpace API Key: ")
    Driver = get_driver(Provider.RACKSPACE)
    return Driver(username, api_key)