Move keypair to standalone example
There was a keypair example in the Network, but keypair is a compute service. I think standalone keypair example would be best. Change-Id: I792ef22219d6c6d123521e2eb347f751a40bf2af
This commit is contained in:
parent
2e853e541d
commit
43ee0a6343
examples
@ -24,6 +24,7 @@ import sys
|
|||||||
|
|
||||||
from examples import common
|
from examples import common
|
||||||
from examples import connection
|
from examples import connection
|
||||||
|
from examples.keypair import create as keypair
|
||||||
from examples import network
|
from examples import network
|
||||||
|
|
||||||
|
|
||||||
@ -33,6 +34,7 @@ def create_jenkins(conn, name, opts):
|
|||||||
|
|
||||||
ports = [9022, 443, 80, 8080, 422, 22]
|
ports = [9022, 443, 80, 8080, 422, 22]
|
||||||
net = network.create(conn, name, opts, ports_to_open=ports)
|
net = network.create(conn, name, opts, ports_to_open=ports)
|
||||||
|
keypair.create(conn, name)
|
||||||
|
|
||||||
server = conn.compute.find_server(name)
|
server = conn.compute.find_server(name)
|
||||||
if server is None:
|
if server is None:
|
||||||
@ -67,6 +69,7 @@ def create_jenkins(conn, name, opts):
|
|||||||
ip = conn.get(ip)
|
ip = conn.get(ip)
|
||||||
print("ssh -i jenkins ubuntu@%s" % ip.floating_ip_address)
|
print("ssh -i jenkins ubuntu@%s" % ip.floating_ip_address)
|
||||||
print("http://%s:8080" % ip.floating_ip_address)
|
print("http://%s:8080" % ip.floating_ip_address)
|
||||||
|
print("login jenkins/demo")
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import sys
|
|||||||
|
|
||||||
from examples import common
|
from examples import common
|
||||||
from examples import connection
|
from examples import connection
|
||||||
|
from examples.keypair import delete as keypair
|
||||||
from examples import network
|
from examples import network
|
||||||
|
|
||||||
|
|
||||||
@ -39,6 +40,7 @@ def delete_jenkins(conn, name, opts):
|
|||||||
conn.delete(ip)
|
conn.delete(ip)
|
||||||
conn.delete(server)
|
conn.delete(server)
|
||||||
|
|
||||||
|
keypair.delete(conn, name)
|
||||||
network.delete(conn, name)
|
network.delete(conn, name)
|
||||||
|
|
||||||
|
|
||||||
|
0
examples/keypair/__init__.py
Normal file
0
examples/keypair/__init__.py
Normal file
68
examples/keypair/create.py
Normal file
68
examples/keypair/create.py
Normal file
@ -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))
|
43
examples/keypair/delete.py
Normal file
43
examples/keypair/delete.py
Normal file
@ -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))
|
@ -19,7 +19,6 @@ To run:
|
|||||||
python examples/network.py
|
python examples/network.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from examples import common
|
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)
|
conn.network.security_group_allow_ping(sg.id)
|
||||||
print(str(sg))
|
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
|
return network
|
||||||
|
|
||||||
|
|
||||||
def delete(conn, name):
|
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)
|
router = conn.network.find_router(name)
|
||||||
if router is not None:
|
if router is not None:
|
||||||
print(str(router))
|
print(str(router))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user