diff --git a/examples/jenkins/create.py b/examples/jenkins/create.py index 4f44ae0db..e9193fada 100644 --- a/examples/jenkins/create.py +++ b/examples/jenkins/create.py @@ -24,6 +24,7 @@ import sys from examples import common from examples import connection +from examples.keypair import create as keypair from examples import network @@ -33,6 +34,7 @@ def create_jenkins(conn, name, opts): ports = [9022, 443, 80, 8080, 422, 22] net = network.create(conn, name, opts, ports_to_open=ports) + keypair.create(conn, name) server = conn.compute.find_server(name) if server is None: @@ -67,6 +69,7 @@ def create_jenkins(conn, name, opts): ip = conn.get(ip) print("ssh -i jenkins ubuntu@%s" % ip.floating_ip_address) print("http://%s:8080" % ip.floating_ip_address) + print("login jenkins/demo") return diff --git a/examples/jenkins/delete.py b/examples/jenkins/delete.py index 69086d840..177c6ef7a 100644 --- a/examples/jenkins/delete.py +++ b/examples/jenkins/delete.py @@ -23,6 +23,7 @@ import sys from examples import common from examples import connection +from examples.keypair import delete as keypair from examples import network @@ -39,6 +40,7 @@ def delete_jenkins(conn, name, opts): conn.delete(ip) conn.delete(server) + keypair.delete(conn, name) network.delete(conn, name) diff --git a/examples/keypair/__init__.py b/examples/keypair/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/keypair/create.py b/examples/keypair/create.py new file mode 100644 index 000000000..e44b88013 --- /dev/null +++ b/examples/keypair/create.py @@ -0,0 +1,68 @@ +# 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. + +""" +Keypair example + +Create a working keypair. + +To run: + python examples/keypair/create.py +""" + +import os +import sys + +from examples import common +from examples import connection + + +def create(conn, name): + + kp = conn.compute.find_keypair(name) + if kp is None: + dirname = os.path.expanduser("~/.ssh") + try: + os.mkdir(dirname, 0o700) + except OSError: + pass + filename = os.path.join(dirname, name) + filenamepub = filename + '.pub' + args = {'name': name} + pubkey = None + try: + with open(filenamepub, 'r') as f: + pubkey = f.read() + args['public_key'] = pubkey + except IOError: + pass + kp = conn.compute.create_keypair(**args) + if pubkey is None: + with open(filename, 'w') as f: + f.write("%s" % kp.private_key) + with open(filenamepub, 'w') as f: + f.write("%s" % kp.public_key) + os.chmod(filename, 0o640) + os.chmod(filenamepub, 0o644) + print(str(kp)) + return kp + + +def run_keypair(opts): + name = opts.data.pop('name', 'pare') + conn = connection.make_connection(opts) + return(create(conn, name)) + + +if __name__ == "__main__": + opts = common.setup() + sys.exit(common.main(opts, run_keypair)) diff --git a/examples/keypair/delete.py b/examples/keypair/delete.py new file mode 100644 index 000000000..5de5f5fe6 --- /dev/null +++ b/examples/keypair/delete.py @@ -0,0 +1,43 @@ +# 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. + +""" +Keypair example + +Destroy a keypair. + +To run: + python examples/keypair/delete.py +""" + +import sys + +from examples import common +from examples import connection + + +def delete(conn, name): + kp = conn.compute.find_keypair(name) + if kp is not None: + print(str(kp)) + conn.delete(kp) + + +def run_keypair(opts): + name = opts.data.pop('name', 'pare') + conn = connection.make_connection(opts) + return(delete(conn, name)) + + +if __name__ == "__main__": + opts = common.setup() + sys.exit(common.main(opts, run_keypair)) diff --git a/examples/network.py b/examples/network.py index 72b77b41e..631ebb677 100644 --- a/examples/network.py +++ b/examples/network.py @@ -19,7 +19,6 @@ To run: python examples/network.py """ -import os import sys from examples import common @@ -66,46 +65,11 @@ def create(conn, name, opts, ports_to_open=[80, 22]): conn.network.security_group_allow_ping(sg.id) print(str(sg)) - kp = conn.compute.find_keypair(name) - if kp is None: - dirname = os.path.expanduser("~/.ssh") - try: - os.mkdir(dirname, 0o700) - except OSError: - pass - filename = os.path.join(dirname, name) - filenamepub = filename + '.pub' - args = {'name': name} - pubkey = None - try: - f = open(filenamepub, 'r') - pubkey = f.read() - f.close() - args['public_key'] = pubkey - except IOError: - pass - kp = conn.compute.create_keypair(**args) - if pubkey is None: - f = open(filename, 'w') - f.write("%s" % kp.private_key) - f.close() - f = open(filenamepub, 'w') - f.write("%s" % kp.public_key) - f.close() - os.chmod(filename, 0o640) - os.chmod(filenamepub, 0o644) - print(str(kp)) - return network def delete(conn, name): - kp = conn.compute.find_keypair(name) - if kp is not None: - print(str(kp)) - conn.delete(kp) - router = conn.network.find_router(name) if router is not None: print(str(router))